1 /*
2 * Copyright (c) 2022 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 "core/components_ng/pattern/menu/wrapper/menu_wrapper_pattern.h"
17
18 #include "base/utils/utils.h"
19 #include "core/components/common/properties/shadow_config.h"
20 #include "core/components/select/select_theme.h"
21 #include "core/components_ng/event/click_event.h"
22 #include "core/event/touch_event.h"
23 #include "core/pipeline_ng/pipeline_context.h"
24
25 namespace OHOS::Ace::NG {
26
27 namespace {
GetPageOffset()28 OffsetF GetPageOffset()
29 {
30 auto pipeline = PipelineContext::GetCurrentContext();
31 CHECK_NULL_RETURN(pipeline, OffsetF());
32 auto stageManager = pipeline->GetStageManager();
33 CHECK_NULL_RETURN(stageManager, OffsetF());
34 auto page = stageManager->GetLastPage();
35 CHECK_NULL_RETURN(page, OffsetF());
36 return page->GetOffsetRelativeToWindow();
37 }
38 } // namespace
39
HideMenu(const RefPtr<FrameNode> & menu)40 void MenuWrapperPattern::HideMenu(const RefPtr<FrameNode>& menu)
41 {
42 isHided_ = true;
43
44 auto pipeline = PipelineContext::GetCurrentContext();
45 CHECK_NULL_VOID(pipeline);
46 auto overlayManager = pipeline->GetOverlayManager();
47 CHECK_NULL_VOID(overlayManager);
48 for (auto subMenuId : subMenuIds_) {
49 LOGI("MenuWrapperPattern::HideMenu subMenu id is %{public}d", subMenuId);
50 overlayManager->HideMenu(subMenuId);
51 }
52
53 auto menuPattern = menu->GetPattern<MenuPattern>();
54 CHECK_NULL_VOID(menuPattern);
55 LOGI("MenuWrapperPattern closing menu %{public}d", targetId_);
56 menuPattern->HideMenu();
57 }
58
OnModifyDone()59 void MenuWrapperPattern::OnModifyDone()
60 {
61 auto host = GetHost();
62 CHECK_NULL_VOID(host);
63 auto gestureHub = host->GetOrCreateGestureEventHub();
64
65 isHided_ = false;
66
67 // if already initialized touch event
68 CHECK_NULL_VOID_NOLOG(!onTouch_);
69
70 // hide menu when touched outside the menu region
71 auto callback = [weak = WeakClaim(RawPtr(host))](const TouchEventInfo& info) {
72 if (info.GetTouches().empty()) {
73 return;
74 }
75 auto touch = info.GetTouches().front();
76 // filter out other touch types
77 if (touch.GetTouchType() != TouchType::DOWN && touch.GetTouchType() != TouchType::UP) {
78 return;
79 }
80 auto host = weak.Upgrade();
81 CHECK_NULL_VOID(host);
82 auto pattern = host->GetPattern<MenuWrapperPattern>();
83 CHECK_NULL_VOID(pattern);
84 if (pattern->IsHided()) {
85 return;
86 }
87 OffsetF position = OffsetF(touch.GetGlobalLocation().GetX(), touch.GetGlobalLocation().GetY());
88 position -= GetPageOffset();
89 for (const auto& child : host->GetChildren()) {
90 // get menu frame node (child of menu wrapper)
91 auto menuNode = DynamicCast<FrameNode>(child);
92 CHECK_NULL_VOID(menuNode);
93
94 // get menuNode's touch region
95 auto menuZone = menuNode->GetGeometryNode()->GetFrameRect();
96 // if DOWN-touched outside the menu region, then hide menu
97 if (!menuZone.IsInRegion(PointF(position.GetX(), position.GetY()))) {
98 pattern->HideMenu(menuNode);
99 }
100 }
101 };
102 onTouch_ = MakeRefPtr<TouchEventImpl>(std::move(callback));
103 gestureHub->AddTouchEvent(onTouch_);
104 }
105
106 // close subMenu when mouse move outside
HandleMouseEvent(const MouseInfo & info,RefPtr<MenuItemPattern> & menuItemPattern)107 void MenuWrapperPattern::HandleMouseEvent(const MouseInfo& info, RefPtr<MenuItemPattern>& menuItemPattern)
108 {
109 const auto& mousePosition = info.GetGlobalLocation();
110 if (!menuItemPattern->IsInHoverRegions(mousePosition.GetX(), mousePosition.GetY()) &&
111 menuItemPattern->IsSubMenuShowed()) {
112 LOGI("MenuWrapperPattern Hide SubMenu");
113 HideSubMenu();
114 menuItemPattern->SetIsSubMenuShowed(false);
115 menuItemPattern->ClearHoverRegions();
116 menuItemPattern->ResetWrapperMouseEvent();
117 }
118 }
119
HideMenu()120 void MenuWrapperPattern::HideMenu()
121 {
122 auto host = GetHost();
123 CHECK_NULL_VOID(host);
124 auto menuNode = DynamicCast<FrameNode>(host->GetChildAtIndex(0));
125 CHECK_NULL_VOID(menuNode);
126 HideMenu(menuNode);
127 }
128
HideSubMenu()129 void MenuWrapperPattern::HideSubMenu()
130 {
131 auto host = GetHost();
132 CHECK_NULL_VOID(host);
133 if (host->GetChildren().size() <= 1) {
134 // sub menu not show
135 return;
136 }
137 auto subMenu = host->GetChildren().back();
138 host->RemoveChild(subMenu);
139 auto menuPattern = DynamicCast<FrameNode>(subMenu)->GetPattern<MenuPattern>();
140 if (menuPattern) {
141 menuPattern->RemoveParentHoverStyle();
142 }
143 host->MarkDirtyNode();
144 }
145 } // namespace OHOS::Ace::NG
146