1 /* 2 * Copyright (c) 2022-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_OPTION_OPTION_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H 18 19 #include <optional> 20 21 #include "base/memory/referenced.h" 22 #include "core/components/select/select_theme.h" 23 #include "core/components/text/text_theme.h" 24 #include "core/components_ng/base/frame_node.h" 25 #include "core/components_ng/pattern/option/option_accessibility_property.h" 26 #include "core/components_ng/pattern/option/option_event_hub.h" 27 #include "core/components_ng/pattern/option/option_layout_algorithm.h" 28 #include "core/components_ng/pattern/option/option_paint_method.h" 29 #include "core/components_ng/pattern/option/option_paint_property.h" 30 #include "core/components_ng/pattern/pattern.h" 31 #include "core/components_ng/render/paint_property.h" 32 #include "core/pipeline_ng/ui_task_scheduler.h" 33 34 namespace OHOS::Ace::NG { 35 class OptionPattern : public Pattern { 36 DECLARE_ACE_TYPE(OptionPattern, Pattern); 37 38 public: OptionPattern(int index)39 explicit OptionPattern(int index) : index_(index) {} 40 ~OptionPattern() override = default; 41 CreateNodePaintMethod()42 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 43 { 44 return MakeRefPtr<OptionPaintMethod>(); 45 } 46 CreatePaintProperty()47 RefPtr<PaintProperty> CreatePaintProperty() override 48 { 49 return MakeRefPtr<OptionPaintProperty>(); 50 } 51 CreateEventHub()52 RefPtr<EventHub> CreateEventHub() override 53 { 54 return MakeRefPtr<OptionEventHub>(); 55 } 56 CreateAccessibilityProperty()57 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 58 { 59 return MakeRefPtr<OptionAccessibilityProperty>(); 60 } 61 CreateLayoutAlgorithm()62 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 63 { 64 return MakeRefPtr<OptionLayoutAlgorithm>(); 65 } 66 IsAtomicNode()67 bool IsAtomicNode() const override 68 { 69 return false; 70 } 71 SetTextNode(const RefPtr<FrameNode> & text)72 void SetTextNode(const RefPtr<FrameNode>& text) 73 { 74 text_ = text; 75 } 76 SetIconNode(const RefPtr<FrameNode> & icon)77 void SetIconNode(const RefPtr<FrameNode>& icon) 78 { 79 icon_ = icon; 80 } 81 SetPasteButton(const RefPtr<FrameNode> & pasteButton)82 void SetPasteButton(const RefPtr<FrameNode>& pasteButton) 83 { 84 pasteButton_ = pasteButton; 85 } 86 87 void SetBgColor(const Color& color); 88 // set font props 89 void SetFontSize(const Dimension& value); 90 void SetItalicFontStyle(const Ace::FontStyle& value); 91 void SetFontWeight(const FontWeight& value); 92 void SetFontFamily(const std::vector<std::string>& value); 93 void SetFontColor(const Color& color); 94 95 Color GetBgColor(); 96 // get font props 97 Dimension GetFontSize(); 98 Ace::FontStyle GetItalicFontStyle(); 99 FontWeight GetFontWeight(); 100 std::vector<std::string> GetFontFamily(); 101 Color GetFontColor(); 102 103 std::string GetText(); 104 105 // XTS inspector functions 106 std::string InspectorGetFont(); 107 108 float GetSelectOptionWidth(); 109 SetIcon(const std::string & src)110 void SetIcon(const std::string& src) 111 { 112 iconSrc_ = src; 113 } 114 GetIcon()115 const std::string& GetIcon() 116 { 117 return iconSrc_; 118 } 119 GetFocusPattern()120 FocusPattern GetFocusPattern() const override 121 { 122 return { FocusType::NODE, true, FocusStyleType::INNER_BORDER }; 123 } 124 125 void UpdateNextNodeDivider(bool needDivider); 126 SetBgBlendColor(const Color & color)127 void SetBgBlendColor(const Color& color) 128 { 129 bgBlendColor_ = color; 130 } 131 GetBgBlendColor()132 Color GetBgBlendColor() const 133 { 134 return bgBlendColor_; 135 } 136 SetIsHover(bool isHover)137 void SetIsHover(bool isHover) 138 { 139 isHover_ = isHover; 140 } 141 IsHover()142 bool IsHover() const 143 { 144 return isHover_; 145 } 146 147 void PlayBgColorAnimation(bool isHoverChange = true); 148 149 void UpdateText(const std::string& content); 150 void UpdateIcon(const std::string& src); 151 SetMenu(const WeakPtr<FrameNode> & menuWeak)152 void SetMenu(const WeakPtr<FrameNode>& menuWeak) 153 { 154 menuWeak_ = menuWeak; 155 } 156 GetMenu()157 const WeakPtr<FrameNode>& GetMenu() const 158 { 159 return menuWeak_; 160 } 161 SetIsWidthModifiedBySelect(bool isModified)162 void SetIsWidthModifiedBySelect(bool isModified) 163 { 164 isWidthModifiedBySelect_ = isModified; 165 } 166 IsWidthModifiedBySelect()167 bool IsWidthModifiedBySelect() const 168 { 169 return isWidthModifiedBySelect_; 170 } 171 SetIsSelectOption(bool isSelect)172 void SetIsSelectOption(bool isSelect) 173 { 174 isSelectOption_ = isSelect; 175 } 176 IsSelectOption()177 bool IsSelectOption() const 178 { 179 return isSelectOption_; 180 } 181 SetHasOptionWidth(bool hasOptionWidth)182 void SetHasOptionWidth(bool hasOptionWidth) 183 { 184 hasOptionWidth_ = hasOptionWidth; 185 } 186 GetHasOptionWidth()187 bool GetHasOptionWidth() 188 { 189 return hasOptionWidth_; 190 } 191 private: 192 void OnAttachToFrameNode() override; 193 void OnModifyDone() override; 194 // make render after measure and layout OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)195 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override 196 { 197 return !(config.skipMeasure && config.skipLayout); 198 } 199 200 // register option's callback 201 void RegisterOnClick(); 202 203 void RegisterOnTouch(); 204 void RegisterOnHover(); 205 void RegisterOnKeyEvent(); 206 207 // change option paint props on press 208 void OnPress(const TouchEventInfo& info); 209 void OnHover(bool isHover); 210 bool OnKeyEvent(const KeyEvent& event); 211 212 void OnSelectProcess(); 213 bool UpdateOptionFocus(KeyCode code); 214 void SetAccessibilityAction(); 215 void UpdatePasteFontColor(const Color& fontColor); 216 217 std::optional<Color> bgColor_; 218 219 // src of icon image, used in XTS inspector 220 std::string iconSrc_; 221 WeakPtr<FrameNode> menuWeak_; 222 RefPtr<FrameNode> text_; 223 RefPtr<FrameNode> icon_; 224 RefPtr<FrameNode> pasteButton_; 225 RefPtr<TextTheme> textTheme_; 226 RefPtr<SelectTheme> selectTheme_; 227 // this option node's index in the menu 228 int index_ = -1; 229 230 Color bgBlendColor_ = Color::TRANSPARENT; 231 bool isHover_ = false; 232 bool isWidthModifiedBySelect_ = false; 233 bool isSelectOption_ = false; 234 bool hasOptionWidth_ = false; 235 236 ACE_DISALLOW_COPY_AND_MOVE(OptionPattern); 237 }; 238 } // namespace OHOS::Ace::NG 239 240 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H 241