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_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_event_hub.h" 26 #include "core/components_ng/pattern/option/option_layout_algorithm.h" 27 #include "core/components_ng/pattern/option/option_paint_method.h" 28 #include "core/components_ng/pattern/option/option_paint_property.h" 29 #include "core/components_ng/pattern/pattern.h" 30 #include "core/components_ng/render/paint_property.h" 31 #include "core/pipeline_ng/ui_task_scheduler.h" 32 33 namespace OHOS::Ace::NG { 34 class OptionPattern : public Pattern { 35 DECLARE_ACE_TYPE(OptionPattern, Pattern); 36 37 public: OptionPattern(int index)38 explicit OptionPattern(int index) : index_(index) {} 39 ~OptionPattern() override = default; 40 CreateNodePaintMethod()41 RefPtr<NodePaintMethod> CreateNodePaintMethod() override 42 { 43 return MakeRefPtr<OptionPaintMethod>(); 44 } 45 CreatePaintProperty()46 RefPtr<PaintProperty> CreatePaintProperty() override 47 { 48 return MakeRefPtr<OptionPaintProperty>(); 49 } 50 CreateEventHub()51 RefPtr<EventHub> CreateEventHub() override 52 { 53 return MakeRefPtr<OptionEventHub>(); 54 } 55 CreateLayoutAlgorithm()56 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 57 { 58 return MakeRefPtr<OptionLayoutAlgorithm>(); 59 } 60 IsAtomicNode()61 bool IsAtomicNode() const override 62 { 63 return false; 64 } 65 SetTextNode(const RefPtr<FrameNode> & text)66 void SetTextNode(const RefPtr<FrameNode>& text) 67 { 68 text_ = text; 69 } 70 71 void SetBgColor(const Color& color); 72 // set font props 73 void SetFontSize(const Dimension& value); 74 void SetItalicFontStyle(const Ace::FontStyle& value); 75 void SetFontWeight(const FontWeight& value); 76 void SetFontFamily(const std::vector<std::string>& value); 77 void SetFontColor(const Color& color); 78 79 Color GetBgColor(); 80 // get font props 81 Dimension GetFontSize(); 82 Ace::FontStyle GetItalicFontStyle(); 83 FontWeight GetFontWeight(); 84 std::vector<std::string> GetFontFamily(); 85 Color GetFontColor(); 86 87 const std::string& GetText(); 88 89 // XTS inspector functions 90 std::string InspectorGetFont(); 91 SetIcon(const std::string & src)92 void SetIcon(const std::string& src) 93 { 94 iconSrc_ = src; 95 } 96 GetIcon()97 const std::string& GetIcon() 98 { 99 return iconSrc_; 100 } 101 GetFocusPattern()102 FocusPattern GetFocusPattern() const override 103 { 104 return { FocusType::NODE, true, FocusStyleType::INNER_BORDER }; 105 } 106 107 void UpdateNextNodeDivider(bool needDivider); 108 SetBgBlendColor(const Color & color)109 void SetBgBlendColor(const Color& color) 110 { 111 bgBlendColor_ = color; 112 } 113 GetBgBlendColor()114 Color GetBgBlendColor() const 115 { 116 return bgBlendColor_; 117 } 118 SetIsHover(bool isHover)119 void SetIsHover(bool isHover) 120 { 121 isHover_ = isHover; 122 } 123 IsHover()124 bool IsHover() const 125 { 126 return isHover_; 127 } 128 129 void PlayBgColorAnimation(bool isHoverChange = true); 130 SetMenu(const WeakPtr<FrameNode> & menuWeak)131 void SetMenu(const WeakPtr<FrameNode>& menuWeak) 132 { 133 menuWeak_ = menuWeak; 134 } 135 GetMenu()136 const WeakPtr<FrameNode>& GetMenu() const 137 { 138 return menuWeak_; 139 } 140 141 private: 142 void OnModifyDone() override; 143 // make render after measure and layout OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)144 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override 145 { 146 return !(config.skipMeasure && config.skipLayout); 147 } 148 149 // register option's callback 150 void RegisterOnClick(); 151 152 void RegisterOnTouch(); 153 void RegisterOnHover(); 154 void RegisterOnKeyEvent(const RefPtr<FocusHub>& focusHub); 155 156 // change option paint props on press 157 void OnPress(const TouchEventInfo& info); 158 void OnHover(bool isHover); 159 bool OnKeyEvent(const KeyEvent& event); 160 161 void OnSelectProcess(); 162 163 std::optional<Color> bgColor_; 164 165 // src of icon image, used in XTS inspector 166 std::string iconSrc_; 167 WeakPtr<FrameNode> menuWeak_; 168 RefPtr<FrameNode> text_ = nullptr; 169 RefPtr<TextTheme> textTheme_ = nullptr; 170 RefPtr<SelectTheme> selectTheme_ = nullptr; 171 // this option node's index in the menu 172 int index_ = -1; 173 174 Color bgBlendColor_ = Color::TRANSPARENT; 175 bool isHover_ = false; 176 177 ACE_DISALLOW_COPY_AND_MOVE(OptionPattern); 178 }; 179 } // namespace OHOS::Ace::NG 180 181 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H 182