1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ui/view/components/menu_view.h"
17
18 #include "core/components_ng/pattern/menu/menu_view.h"
19 #include "interfaces/inner_api/ace_kit/src/view/frame_node_impl.h"
20 #include "ui/view_stack/view_stack_processor.h"
21
22 #include "core/components_ng/pattern/menu/menu_model_ng.h"
23 #include "core/components_ng/pattern/menu/menu_item/menu_item_model_ng.h"
24 #include "core/components_ng/pattern/menu/menu_view.h"
25
26 namespace OHOS::Ace::Kit {
27 namespace {
28 const std::pair<RefPtr<FrameNode>, RefPtr<FrameNode>> NULL_MENU_PAIR = { nullptr, nullptr };
29 } // namespace
30
Create(std::vector<OptionParam> && params,MenuType type,const MenuParam & menuParam)31 RefPtr<FrameNode> MenuView::Create(std::vector<OptionParam>&& params, MenuType type, const MenuParam& menuParam)
32 {
33 std::vector<NG::OptionParam> aceParams;
34 for (const OptionParam& optionParam : params) {
35 NG::OptionParam aceOptionParam;
36 aceOptionParam.value = optionParam.value;
37 aceOptionParam.icon = optionParam.icon;
38 aceOptionParam.enabled = optionParam.enabled;
39 aceOptionParam.action = optionParam.action;
40 if (optionParam.symbol) {
41 aceOptionParam.symbol = [symbol = optionParam.symbol](WeakPtr<NG::FrameNode> weakAceFrameNode) {
42 RefPtr<NG::FrameNode> aceFrameNode = weakAceFrameNode.Upgrade();
43 CHECK_NULL_VOID(aceFrameNode);
44 symbol(reinterpret_cast<void*>(AceType::RawPtr(aceFrameNode)));
45 };
46 }
47 aceOptionParam.symbolUserDefinedIdealFontSize = optionParam.symbolUserDefinedIdealFontSize;
48 aceOptionParam.disableSystemClick = optionParam.disableSystemClick;
49 aceOptionParam.isPasteOption = optionParam.isPasteOption;
50 aceParams.push_back(aceOptionParam);
51 }
52
53 NG::MenuParam aceMenuParam;
54 aceMenuParam.isShowInSubWindow = menuParam.isShowInSubWindow;
55 aceMenuParam.placement = static_cast<Placement>(menuParam.placement);
56 RefPtr<NG::FrameNode> aceNode = OHOS::Ace::NG::MenuView::Create(std::move(aceParams), menuParam.targetId,
57 menuParam.targetTag, static_cast<NG::MenuType>(type), aceMenuParam);
58 RefPtr<FrameNode> node = AceType::MakeRefPtr<FrameNodeImpl>(aceNode);
59 return node;
60 }
61
NavigationCreateMenu(std::vector<OptionParam> && params,const MenuParam & menuParam)62 std::pair<RefPtr<FrameNode>, RefPtr<FrameNode>> MenuView::NavigationCreateMenu(
63 std::vector<OptionParam>&& params, const MenuParam& menuParam)
64 {
65 RefPtr<NG::FrameNode> menuAceNode = OHOS::Ace::NG::MenuModelNG::CreateMenu();
66 CHECK_NULL_RETURN(menuAceNode, NULL_MENU_PAIR);
67 RefPtr<FrameNode> menuNode = AceType::MakeRefPtr<FrameNodeImpl>(menuAceNode);
68 CHECK_NULL_RETURN(menuNode, NULL_MENU_PAIR);
69 menuAceNode->SetKitNode(menuNode);
70 NG::MenuParam aceMenuParam;
71 aceMenuParam.isShowInSubWindow = menuParam.isShowInSubWindow;
72 aceMenuParam.placement = static_cast<Placement>(menuParam.placement);
73 RefPtr<NG::FrameNode> menuWrapperAceNode =
74 OHOS::Ace::NG::MenuView::Create(menuAceNode, menuParam.targetId, menuParam.targetTag, aceMenuParam);
75 CHECK_NULL_RETURN(menuWrapperAceNode, NULL_MENU_PAIR);
76 RefPtr<FrameNode> menuWrapperNode = AceType::MakeRefPtr<FrameNodeImpl>(menuWrapperAceNode);
77 CHECK_NULL_RETURN(menuWrapperNode, NULL_MENU_PAIR);
78 menuWrapperAceNode->SetKitNode(menuWrapperNode);
79 for (const OptionParam& optionParam : params) {
80 NG::OptionParam aceOptionParam;
81 aceOptionParam.value = optionParam.value;
82 aceOptionParam.enabled = optionParam.enabled;
83 aceOptionParam.action = optionParam.action;
84 aceOptionParam.symbolUserDefinedIdealFontSize = optionParam.symbolUserDefinedIdealFontSize;
85 aceOptionParam.disableSystemClick = optionParam.disableSystemClick;
86 aceOptionParam.isPasteOption = optionParam.isPasteOption;
87 RefPtr<NG::FrameNode> menuItemAceNode =
88 OHOS::Ace::NG::MenuItemModelNG::CreateMenuItem(std::move(aceOptionParam), aceMenuParam);
89 if (menuItemAceNode) {
90 menuAceNode->AddChild(menuItemAceNode);
91 }
92 }
93 return { menuWrapperNode, menuNode };
94 }
95 } // OHOS::Ace::Kit