• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_WRAPPER_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_WRAPPER_PATTERN_H
18 
19 #include "base/memory/ace_type.h"
20 #include "base/memory/referenced.h"
21 #include "base/subwindow/subwindow_manager.h"
22 #include "base/utils/string_utils.h"
23 #include "base/utils/utils.h"
24 #include "core/components/common/properties/color.h"
25 #include "core/components/common/properties/placement.h"
26 #include "core/components_ng/pattern/menu/menu_item/menu_item_pattern.h"
27 #include "core/components_ng/pattern/menu/menu_pattern.h"
28 #include "core/components_ng/pattern/menu/wrapper/menu_wrapper_layout_algorithm.h"
29 #include "core/components_ng/pattern/overlay/popup_base_pattern.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_v2/inspector/inspector_constants.h"
32 #include "core/pipeline_ng/ui_task_scheduler.h"
33 
34 namespace OHOS::Ace::NG {
35 
36 // has full screen size
37 // used for detecting clicks outside Menu area
38 class MenuWrapperPattern : public PopupBasePattern {
39     DECLARE_ACE_TYPE(MenuWrapperPattern, Pattern);
40 
41 public:
MenuWrapperPattern(int32_t Id)42     explicit MenuWrapperPattern(int32_t Id) : targetId_(Id) {}
43     ~MenuWrapperPattern() override = default;
44 
IsAtomicNode()45     bool IsAtomicNode() const override
46     {
47         return false;
48     }
49 
GetFocusPattern()50     FocusPattern GetFocusPattern() const override
51     {
52         return { FocusType::SCOPE, true };
53     }
54 
CreateLayoutAlgorithm()55     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
56     {
57         return MakeRefPtr<MenuWrapperLayoutAlgorithm>();
58     }
59 
60     void HandleMouseEvent(const MouseInfo& info, RefPtr<MenuItemPattern>& menuItem);
61 
GetTargetId()62     int32_t GetTargetId() const
63     {
64         return targetId_;
65     }
66 
67     void HideMenu();
68 
IsHided()69     bool IsHided() const
70     {
71         return isHided_;
72     }
73 
IsContextMenu()74     bool IsContextMenu() const
75     {
76         auto menu = GetMenu();
77         CHECK_NULL_RETURN(menu, false);
78         auto menuPattern = menu->GetPattern<MenuPattern>();
79         CHECK_NULL_RETURN(menuPattern, false);
80         return menuPattern->IsContextMenu();
81     }
82 
IsSelectMenu()83     bool IsSelectMenu() const
84     {
85         auto menu = GetMenu();
86         CHECK_NULL_RETURN(menu, false);
87         auto menuPattern = menu->GetPattern<MenuPattern>();
88         CHECK_NULL_RETURN(menuPattern, false);
89         return menuPattern->IsSelectMenu();
90     }
91 
92     void HideSubMenu();
93 
GetMenu()94     RefPtr<FrameNode> GetMenu() const
95     {
96         auto host = GetHost();
97         CHECK_NULL_RETURN(host, nullptr);
98         auto menu = AceType::DynamicCast<FrameNode>(host->GetChildAtIndex(0));
99         CHECK_NULL_RETURN(menu, nullptr);
100         return menu;
101     }
102     OffsetT<Dimension> GetAnimationOffset();
103     void SetAniamtinOption(const AnimationOption& animationOption);
104 
SetMenuPlacementAfterLayout(const Placement & placement)105     void SetMenuPlacementAfterLayout(const Placement& placement)
106     {
107         menuPlacement_ = placement;
108     }
109 
SetFirstShow()110     void SetFirstShow()
111     {
112         isFirstShow_ = true;
113     }
114 protected:
115     void OnTouchEvent(const TouchEventInfo& info);
116     void CheckAndShowAnimation();
117 
118 private:
ShouldAvoidKeyboard()119     bool ShouldAvoidKeyboard() const override
120     {
121         return false;
122     }
123     void OnAttachToFrameNode() override;
124     void RegisterOnTouch();
125     void OnModifyDone() override;
126     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
127     void SetHotAreas(const RefPtr<LayoutWrapper>& layoutWrapper);
128     void StartShowAnimation();
129 
130     void HideMenu(const RefPtr<FrameNode>& menu);
131 
132     RefPtr<TouchEventImpl> onTouch_;
133     // menuId in OverlayManager's map
134     int32_t targetId_ = -1;
135 
136     AnimationOption animationOption_;
137     Placement menuPlacement_ = Placement::NONE;
138     bool isFirstShow_ = true;
139     bool isHided_ = false;
140 
141     ACE_DISALLOW_COPY_AND_MOVE(MenuWrapperPattern);
142 };
143 } // namespace OHOS::Ace::NG
144 
145 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_WRAPPER_PATTERN_H
146