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_SELECT_OVERLAY_NODE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_NODE_H 18 19 #include <cstdint> 20 #include <memory> 21 22 #include "base/memory/referenced.h" 23 #include "base/utils/noncopyable.h" 24 #include "core/components_ng/base/frame_node.h" 25 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h" 26 #include "core/components_ng/property/property.h" 27 28 namespace OHOS::Ace::NG { 29 30 struct OptionParam; 31 32 const std::string SelectOverlayRrightClickMenuWrapper = "select_overlay_right_click_menuWrapper"; 33 34 enum class FrameNodeType { 35 SELECTMENU, 36 EXTENSIONMENU, 37 BACKBUTTON, 38 MENUONLY 39 }; 40 41 enum class FrameNodeStatus { 42 VISIBLE, 43 VISIBLETOGONE, 44 GONE, 45 GONETOVISIBLE 46 }; 47 48 enum class FrameNodeTrigger { 49 SHOW, 50 SHOWN, 51 HIDE, 52 HIDDEN 53 }; 54 55 class ACE_EXPORT SelectOverlayNode : public FrameNode { 56 DECLARE_ACE_TYPE(SelectOverlayNode, FrameNode) 57 public: 58 explicit SelectOverlayNode(const RefPtr<Pattern>& pattern); 59 ~SelectOverlayNode() override = default; 60 61 static RefPtr<FrameNode> CreateSelectOverlayNode( 62 const std::shared_ptr<SelectOverlayInfo>& info, SelectOverlayMode mode = SelectOverlayMode::ALL); 63 RefPtr<FrameNode> CreateMoreSelectOverlayNode(const std::vector<MenuOptionsParam>& menuOptionItems, int32_t index); 64 65 void UpdateToolBar(bool menuItemChanged, bool noAnimation = false); 66 67 void UpdateMenuOptions(const std::shared_ptr<SelectOverlayInfo>& info); 68 69 void UpdateMenuInner(const std::shared_ptr<SelectOverlayInfo>& info, bool noAnimation = false); 70 SetSelectInfo(const std::string & selectInfo)71 void SetSelectInfo(const std::string& selectInfo) 72 { 73 selectInfo_ = selectInfo; 74 } 75 GetSelectInfo()76 const std::string& GetSelectInfo() const 77 { 78 return selectInfo_; 79 } 80 81 void ChangeToolBar(bool isUseExtensionMenu); 82 83 void MoreOrBackAnimation(bool isMore, bool noAnimation = false); 84 85 bool IsInSelectedOrSelectOverlayArea(const PointF& point); 86 87 void SetClosedByGlobalEvent(bool closedByGlobalEvent); 88 GetAnimationStatus()89 bool GetAnimationStatus() 90 { 91 return isDoingAnimation_; 92 } 93 GetIsExtensionMenu()94 bool GetIsExtensionMenu() 95 { 96 return isExtensionMenu_; 97 } 98 99 void ShowSelectOverlay(bool animation); 100 101 void HideSelectOverlay(const std::function<void()>& callback); 102 void HideOrShowCirclesAndBackArrow(FrameNodeType type, float value); 103 104 void SwitchToOverlayMode(); 105 void UpdateSelectMenuBg(); 106 void AddCustomMenuCallbacks(const std::shared_ptr<SelectOverlayInfo>& info); 107 void OnCustomSelectMenuAppear(); 108 void FireCustomMenuChangeEvent(bool isMenuShow); 109 void OnDetachFromMainTree(bool recursive, PipelineContext* context) override; 110 void UpdateToolBarFromMainWindow(bool menuItemChanged, bool noAnimation = false); 111 112 private: 113 void CreateToolBar(); 114 void SelectMenuAndInnerInitProperty(); 115 void AddMenuItemByCreateMenuCallback(const std::shared_ptr<SelectOverlayInfo>& info, float maxWidth); 116 static const std::vector<MenuItemParam> GetSystemMenuItemParams(const std::shared_ptr<SelectOverlayInfo>& info); 117 static const MenuItemParam GetSystemMenuItemParam(const std::string& menuId, const std::string& menuButton); 118 int32_t AddCreateMenuItems(const std::vector<NG::MenuOptionsParam>& menuItems, 119 const std::shared_ptr<SelectOverlayInfo>& info, float maxWidth); 120 bool AddSystemDefaultOptions(float maxWidth, float& allocatedSize); 121 void LandscapeMenuAddMenuOptions(const std::vector<MenuOptionsParam>& menuOptionItems, bool isDefaultOverMaxWidth, 122 float maxWidth, float allocatedSize, int32_t& extensionOptionStartIndex); 123 void ShowCut(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 124 void ShowCopy(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 125 void ShowPaste(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 126 void ShowCopyAll(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 127 void ShowTranslate(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 128 void ShowSearch(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 129 void ShowShare(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 130 void ShowCamera(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 131 void ShowAIWrite(float maxWidth, float& allocatedSize, std::shared_ptr<SelectOverlayInfo>& info); 132 bool IsShowOnTargetAPIVersion(); 133 bool IsShowTranslateOnTargetAPIVersion(); 134 std::function<void()> GetDefaultOptionCallback(); 135 std::function<void(WeakPtr<NG::FrameNode>)> GetSymbolFunc(const std::string& symbolId); 136 std::vector<OptionParam> GetDefaultOptionsParams(const std::shared_ptr<SelectOverlayInfo>& info); 137 void GetFlexibleOptionsParams( 138 const std::shared_ptr<SelectOverlayInfo>& info, std::vector<OptionParam>& params); 139 void addMenuOptionItemsParams( 140 std::vector<OptionParam>& params, const std::shared_ptr<SelectOverlayInfo>& info, int32_t index); 141 void AddExtensionMenuOptions(const std::shared_ptr<SelectOverlayInfo>& info, int32_t index); 142 void AddCreateMenuExtensionMenuOptions(const std::vector<MenuOptionsParam>& menuOptionItems, 143 const std::shared_ptr<SelectOverlayInfo>& info, int32_t startIndex); 144 void AddCreateMenuExtensionMenuParams(const std::vector<MenuOptionsParam>& menuOptionItems, 145 const std::shared_ptr<SelectOverlayInfo>& info, int32_t startIndex, std::vector<OptionParam>& params); 146 std::function<void()> CreateExtensionMenuOptionCallback(int32_t id, const OnMenuItemCallback& onCreateCallback, 147 const std::function<void()>& systemEvent, const MenuOptionsParam& item); 148 void CreatExtensionMenu(std::vector<OptionParam>&& params); 149 void GetDefaultButtonAndMenuWidth(float& maxWidth); 150 151 void MoreAnimation(bool noAnimation); 152 void BackAnimation(bool noAnimation); 153 154 void DispatchVisibleState(FrameNodeType type, FrameNodeTrigger trigger); 155 void DispatchVisibleToGoneState(FrameNodeType type, FrameNodeTrigger trigger); 156 void DispatchGoneState(FrameNodeType type, FrameNodeTrigger trigger); 157 void DispatchGoneToVisibleState(FrameNodeType type, FrameNodeTrigger trigger); 158 void ExecuteOverlayStatus(FrameNodeType type, FrameNodeTrigger trigger); 159 void SetFrameNodeStatus(FrameNodeType type, FrameNodeStatus status); 160 void SetFrameNodeVisibility(FrameNodeType type, VisibleType visibleType); 161 void SetFrameNodeOpacity(FrameNodeType type, float opacity); 162 void SetSelectMenuOpacity(float value); 163 void SetExtensionMenuOpacity(float value); 164 void SetBackButtonOpacity(float value); 165 void HideFrameNodeImmediately(FrameNodeType type); 166 void CreateCustomSelectOverlay(const std::shared_ptr<SelectOverlayInfo>& info); 167 void UpdateMoreOrBackSymbolOptions(bool isMoreButton, bool isReplaceEffectEnable); 168 void UpdateMoreOrBackSymbolOptionsWithDelay(); 169 void MenuOnlyStatusChange(const std::shared_ptr<SelectOverlayInfo>& info, bool noAnimation); 170 void HideMenuOnlyImmediately(); 171 void InitSelectMenuStatus( 172 SelectOverlayMode mode, const std::shared_ptr<SelectOverlayInfo>& info, bool changeOpacity = true); 173 SetAnimationStatus(bool toDoAnimation)174 void SetAnimationStatus(bool toDoAnimation) 175 { 176 isDoingAnimation_ = toDoAnimation; 177 } 178 SetIsMoreOrBackSymbolIcon(bool isMoreOrBackSymbol)179 void SetIsMoreOrBackSymbolIcon(bool isMoreOrBackSymbol) 180 { 181 isMoreOrBackSymbolIcon_ = isMoreOrBackSymbol; 182 } 183 184 static RefPtr<FrameNode> CreateMenuNode(const std::shared_ptr<SelectOverlayInfo>& info); 185 static std::pair<std::vector<MenuOptionsParam>, bool> HandleCollaborationMenuItem( 186 const std::vector<MenuOptionsParam>& params); 187 188 void NotifyUpdateToolBar(bool itemChanged); 189 void SetSelectMenuInnerSize(); 190 191 using ExecuteStateFunc = void (SelectOverlayNode::*)(FrameNodeType type, FrameNodeTrigger trigger); 192 193 /* Text selection menu node structure. 194 -rootNode 195 -selectOverlay 196 -selectMenu_ 197 -selectMenuInner_ 198 -backButton_ 199 -extensionMenu_ 200 Attention: 201 If the user-defined selection menu is bound by bindSelectionMenu, there will be only the selectMenu_. 202 Then, selectMenuInner_, backButton_, extensionMenu_ will be null. 203 Text selection menu node structure whill be like bellow: 204 -rootNode 205 -selectOverlay(menuWrapper) 206 -selectMenu_(menu) 207 -scroller 208 -customBuilderMenu 209 */ 210 RefPtr<FrameNode> selectMenu_; 211 RefPtr<FrameNode> selectMenuInner_; 212 RefPtr<FrameNode> extensionMenu_; 213 RefPtr<FrameNode> backButton_; 214 RefPtr<FrameNode> moreOrBackSymbol_; 215 RefPtr<FrameNode> moreButton_; 216 217 FrameNodeStatus selectMenuStatus_ = FrameNodeStatus::VISIBLE; 218 FrameNodeStatus extensionMenuStatus_ = FrameNodeStatus::GONE; 219 FrameNodeStatus backButtonStatus_ = FrameNodeStatus::GONE; 220 FrameNodeStatus menuOnlyStatus_ = FrameNodeStatus::VISIBLE; 221 222 std::map<FrameNodeStatus, ExecuteStateFunc> stateFuncs_; 223 224 std::string selectInfo_; 225 226 // Marks whether it is currently in the animated state. 227 bool isDoingAnimation_ = false; 228 229 // Controls that only default menus can be converted to extended menus, and extended menus can be converted to 230 // default menus. 231 bool isExtensionMenu_ = false; 232 233 // Label whether the menu default button needs to appear within the extended menu 234 bool isShowInDefaultMenu_[9] = { false }; 235 236 bool isDefaultBtnOverMaxWidth_ = false; 237 238 bool isMoreOrBackSymbolIcon_ = false; 239 bool isCustomMenuAppear_ = false; 240 241 ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayNode); 242 }; 243 244 } // namespace OHOS::Ace::NG 245 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_NODE_H 246