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 67 void MarkIsSelected(bool isSelected); SetSelected(bool isSelected)68 void SetSelected(bool isSelected) 69 { 70 isSelected_ = isSelected; 71 GetHost()->MarkModifyDone(); 72 } 73 IsSelected()74 bool IsSelected() const 75 { 76 return isSelected_; 77 } 78 SetSubBuilder(const std::function<void ()> & subBuilderFunc)79 void SetSubBuilder(const std::function<void()>& subBuilderFunc) 80 { 81 subBuilderFunc_ = subBuilderFunc; 82 } 83 GetSubBuilder()84 std::function<void()>& GetSubBuilder() 85 { 86 return subBuilderFunc_; 87 } 88 IsSubMenuShowed()89 bool IsSubMenuShowed() const 90 { 91 return isSubMenuShowed_; 92 } 93 SetIsSubMenuShowed(bool isSubMenuShowed)94 void SetIsSubMenuShowed(bool isSubMenuShowed) 95 { 96 isSubMenuShowed_ = isSubMenuShowed; 97 } 98 IsSubMenuHovered()99 bool IsSubMenuHovered() const 100 { 101 return isSubMenuHovered_; 102 } 103 SetIsSubMenuHovered(bool isSubMenuHovered)104 void SetIsSubMenuHovered(bool isSubMenuHovered) 105 { 106 isSubMenuHovered_ = isSubMenuHovered; 107 } 108 109 void AddHoverRegions(const OffsetF& topLeftPoint, const OffsetF& bottomRightPoint); 110 111 bool IsInHoverRegions(double x, double y); 112 ClearHoverRegions()113 void ClearHoverRegions() 114 { 115 hoverRegions_.clear(); 116 } 117 ResetWrapperMouseEvent()118 void ResetWrapperMouseEvent() 119 { 120 wrapperMouseEvent_.Reset(); 121 } 122 SetChange()123 void SetChange() 124 { 125 isSelected_ = !isSelected_; 126 } 127 IsChange()128 bool IsChange() const 129 { 130 return isChanged_; 131 } 132 133 void CloseMenu(); 134 GetContentNode()135 RefPtr<FrameNode> GetContentNode() 136 { 137 return content_; 138 } 139 GetLabelNode()140 RefPtr<FrameNode> GetLabelNode() 141 { 142 return label_; 143 } 144 145 void PlayBgColorAnimation(bool isHoverChange = true); SetBgBlendColor(const Color & color)146 void SetBgBlendColor(const Color& color) 147 { 148 bgBlendColor_ = color; 149 } GetBgBlendColor()150 Color GetBgBlendColor() const 151 { 152 return bgBlendColor_; 153 } 154 155 RefPtr<FrameNode> GetMenu(bool needTopMenu = false); 156 RefPtr<MenuPattern> GetMenuPattern(bool needTopMenu = false); 157 void UpdateTextNodes(); 158 159 void OnAttachToFrameNode() override; 160 void OnModifyDone() override; 161 void OnMountToParentDone() override; 162 HasSelectIcon()163 bool HasSelectIcon() const 164 { 165 return selectIcon_ != nullptr; 166 } HasStartIcon()167 bool HasStartIcon() const 168 { 169 return startIcon_ != nullptr; 170 } 171 172 void OnVisibleChange(bool isVisible) override; 173 174 protected: 175 void RegisterOnKeyEvent(); 176 void RegisterOnTouch(); 177 void OnAfterModifyDone() override; 178 179 private: 180 // register menu item's callback 181 void RegisterOnClick(); 182 void RegisterOnHover(); 183 virtual void OnTouch(const TouchEventInfo& info); 184 void OnHover(bool isHover); 185 virtual bool OnKeyEvent(const KeyEvent& event); 186 187 void RegisterWrapperMouseEvent(); 188 189 void AddSelectIcon(RefPtr<FrameNode>& row); 190 void UpdateIcon(RefPtr<FrameNode>& row, bool isStart); 191 void UpdateText(RefPtr<FrameNode>& row, RefPtr<MenuLayoutProperty>& menuProperty, bool isLabel); 192 193 bool IsDisabled(); 194 void UpdateDisabledStyle(); 195 196 RefPtr<FrameNode> GetMenuWrapper(); 197 198 void ShowSubMenu(); 199 200 OffsetF GetSubMenuPostion(const RefPtr<FrameNode>& targetNode); 201 202 void AddSelfHoverRegion(const RefPtr<FrameNode>& targetNode); 203 void SetAccessibilityAction(); 204 bool IsSelectOverlayMenu(); 205 206 void RecordChangeEvent() const; 207 208 std::list<TouchRegion> hoverRegions_; 209 210 RefPtr<InputEvent> wrapperMouseEvent_; 211 212 bool isSelected_ = false; 213 bool isSubMenuShowed_ = false; 214 bool isSubMenuHovered_ = false; 215 216 bool isChanged_ = false; 217 bool isHovered_ = false; 218 219 std::function<void()> subBuilderFunc_ = nullptr; 220 221 int32_t subMenuId_ = -1; 222 RefPtr<FrameNode> subMenu_; 223 RefPtr<FrameNode> content_ = nullptr; 224 RefPtr<FrameNode> label_ = nullptr; 225 RefPtr<FrameNode> startIcon_ = nullptr; 226 RefPtr<FrameNode> endIcon_ = nullptr; 227 RefPtr<FrameNode> selectIcon_ = nullptr; 228 229 Color bgBlendColor_ = Color::TRANSPARENT; 230 231 ACE_DISALLOW_COPY_AND_MOVE(MenuItemPattern); 232 }; 233 234 class CustomMenuItemPattern : public MenuItemPattern { 235 DECLARE_ACE_TYPE(CustomMenuItemPattern, MenuItemPattern); 236 237 public: CreateLayoutAlgorithm()238 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 239 { 240 return MakeRefPtr<BoxLayoutAlgorithm>(); 241 } 242 void OnAttachToFrameNode() override; 243 244 private: 245 void OnTouch(const TouchEventInfo& info) override; 246 bool OnKeyEvent(const KeyEvent& event) override; 247 std::unique_ptr<Offset> lastTouchOffset_; 248 }; 249 } // namespace OHOS::Ace::NG 250 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_MENU_MENU_ITEM_PATTERN_H 251