1 /* 2 * Copyright (c) 2023 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_ITEM_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_ITEM_PATTERN_H 18 19 #include "base/memory/referenced.h" 20 #include "base/utils/noncopyable.h" 21 #include "core/components/slider/render_slider.h" 22 #include "core/components_ng/pattern/menu/menu_item/menu_item_accessibility_property.h" 23 #include "core/components_ng/pattern/menu/menu_item/menu_item_event_hub.h" 24 #include "core/components_ng/pattern/menu/menu_item/menu_item_layout_algorithm.h" 25 #include "core/components_ng/pattern/menu/menu_item/menu_item_layout_property.h" 26 #include "core/components_ng/pattern/menu/menu_pattern.h" 27 #include "core/components_ng/pattern/pattern.h" 28 29 namespace OHOS::Ace::NG { 30 class ACE_EXPORT MenuItemPattern : public Pattern { 31 DECLARE_ACE_TYPE(MenuItemPattern, Pattern); 32 33 public: 34 MenuItemPattern() = default; 35 ~MenuItemPattern() override = default; 36 IsAtomicNode()37 bool IsAtomicNode() const override 38 { 39 return false; 40 } 41 GetFocusPattern()42 FocusPattern GetFocusPattern() const override 43 { 44 return { FocusType::NODE, true, FocusStyleType::INNER_BORDER }; 45 } 46 CreateEventHub()47 RefPtr<EventHub> CreateEventHub() override 48 { 49 return MakeRefPtr<MenuItemEventHub>(); 50 } 51 CreateLayoutProperty()52 RefPtr<LayoutProperty> CreateLayoutProperty() override 53 { 54 return MakeRefPtr<MenuItemLayoutProperty>(); 55 } 56 CreateAccessibilityProperty()57 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 58 { 59 return MakeRefPtr<MenuItemAccessibilityProperty>(); 60 } 61 CreateLayoutAlgorithm()62 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 63 { 64 return MakeRefPtr<MenuItemLayoutAlgorithm>(); 65 } 66 SetSelected(bool isSelected)67 void SetSelected(bool isSelected) 68 { 69 isSelected_ = isSelected; 70 GetHost()->MarkModifyDone(); 71 } 72 IsSelected()73 bool IsSelected() const 74 { 75 return isSelected_; 76 } 77 SetSubBuilder(const std::function<void ()> & subBuilderFunc)78 void SetSubBuilder(const std::function<void()>& subBuilderFunc) 79 { 80 subBuilderFunc_ = subBuilderFunc; 81 } 82 GetSubBuilder()83 std::function<void()>& GetSubBuilder() 84 { 85 return subBuilderFunc_; 86 } 87 IsSubMenuShowed()88 bool IsSubMenuShowed() const 89 { 90 return isSubMenuShowed_; 91 } 92 SetIsSubMenuShowed(bool isSubMenuShowed)93 void SetIsSubMenuShowed(bool isSubMenuShowed) 94 { 95 isSubMenuShowed_ = isSubMenuShowed; 96 } 97 IsSubMenuHovered()98 bool IsSubMenuHovered() const 99 { 100 return isSubMenuHovered_; 101 } 102 SetIsSubMenuHovered(bool isSubMenuHovered)103 void SetIsSubMenuHovered(bool isSubMenuHovered) 104 { 105 isSubMenuHovered_ = isSubMenuHovered; 106 } 107 108 void AddHoverRegions(const OffsetF& topLeftPoint, const OffsetF& bottomRightPoint); 109 110 bool IsInHoverRegions(double x, double y); 111 ClearHoverRegions()112 void ClearHoverRegions() 113 { 114 hoverRegions_.clear(); 115 } 116 ResetWrapperMouseEvent()117 void ResetWrapperMouseEvent() 118 { 119 wrapperMouseEvent_.Reset(); 120 } 121 SetChange()122 void SetChange() 123 { 124 isSelected_ = !isSelected_; 125 } 126 IsChange()127 bool IsChange() const 128 { 129 return isChanged_; 130 } 131 132 void CloseMenu(); 133 GetContentNode()134 RefPtr<FrameNode> GetContentNode() 135 { 136 return content_; 137 } 138 GetLabelNode()139 RefPtr<FrameNode> GetLabelNode() 140 { 141 return label_; 142 } 143 144 void PlayBgColorAnimation(bool isHoverChange = true); SetBgBlendColor(const Color & color)145 void SetBgBlendColor(const Color& color) 146 { 147 bgBlendColor_ = color; 148 } GetBgBlendColor()149 Color GetBgBlendColor() const 150 { 151 return bgBlendColor_; 152 } 153 154 RefPtr<FrameNode> GetMenu(bool needTopMenu = false); 155 RefPtr<MenuPattern> GetMenuPattern(bool needTopMenu = false); 156 void UpdateTextNodes(); 157 158 void OnAttachToFrameNode() override; 159 void OnModifyDone() override; 160 void OnMountToParentDone() override; 161 HasSelectIcon()162 bool HasSelectIcon() const 163 { 164 return selectIcon_ != nullptr; 165 } HasStartIcon()166 bool HasStartIcon() const 167 { 168 return startIcon_ != nullptr; 169 } 170 171 protected: 172 void RegisterOnKeyEvent(); 173 void RegisterOnTouch(); 174 175 private: 176 // register menu item's callback 177 void RegisterOnClick(); 178 void RegisterOnHover(); 179 virtual void OnTouch(const TouchEventInfo& info); 180 void OnHover(bool isHover); 181 virtual bool OnKeyEvent(const KeyEvent& event); 182 183 void RegisterWrapperMouseEvent(); 184 185 void AddSelectIcon(RefPtr<FrameNode>& row); 186 void UpdateIcon(RefPtr<FrameNode>& row, bool isStart); 187 void UpdateText(RefPtr<FrameNode>& row, RefPtr<MenuLayoutProperty>& menuProperty, bool isLabel); 188 189 bool IsDisabled(); 190 void UpdateDisabledStyle(); 191 192 RefPtr<FrameNode> GetMenuWrapper(); 193 194 void ShowSubMenu(); 195 196 OffsetF GetSubMenuPostion(const RefPtr<FrameNode>& targetNode); 197 198 void AddSelfHoverRegion(const RefPtr<FrameNode>& targetNode); 199 void SetAccessibilityAction(); 200 bool IsSelectOverlayMenu(); 201 202 std::list<TouchRegion> hoverRegions_; 203 204 RefPtr<InputEvent> wrapperMouseEvent_; 205 206 bool isSelected_ = false; 207 bool isSubMenuShowed_ = false; 208 bool isSubMenuHovered_ = false; 209 210 bool isChanged_ = false; 211 bool isHovered_ = false; 212 213 std::function<void()> subBuilderFunc_ = nullptr; 214 215 int32_t subMenuId_ = -1; 216 RefPtr<FrameNode> subMenu_; 217 RefPtr<FrameNode> content_ = nullptr; 218 RefPtr<FrameNode> label_ = nullptr; 219 RefPtr<FrameNode> startIcon_ = nullptr; 220 RefPtr<FrameNode> endIcon_ = nullptr; 221 RefPtr<FrameNode> selectIcon_ = nullptr; 222 223 Color bgBlendColor_ = Color::TRANSPARENT; 224 225 ACE_DISALLOW_COPY_AND_MOVE(MenuItemPattern); 226 }; 227 228 class CustomMenuItemPattern : public MenuItemPattern { 229 DECLARE_ACE_TYPE(CustomMenuItemPattern, MenuItemPattern); 230 231 public: CreateLayoutAlgorithm()232 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 233 { 234 return MakeRefPtr<BoxLayoutAlgorithm>(); 235 } 236 void OnAttachToFrameNode() override; 237 238 private: 239 void OnTouch(const TouchEventInfo& info) override; 240 bool OnKeyEvent(const KeyEvent& event) override; 241 std::unique_ptr<Offset> lastTouchOffset_; 242 }; 243 } // namespace OHOS::Ace::NG 244 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_ITEM_PATTERN_H 245