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_SELECT_SELECT_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_SELECT_PATTERN_H 18 19 #include <cstdint> 20 #include <optional> 21 22 #include "base/memory/referenced.h" 23 #include "base/utils/utils.h" 24 #include "core/components/common/properties/color.h" 25 #include "core/components/select/select_theme.h" 26 #include "core/components/theme/icon_theme.h" 27 #include "core/components_ng/base/frame_node.h" 28 #include "core/components_ng/event/event_hub.h" 29 #include "core/components_ng/pattern/option/option_paint_method.h" 30 #include "core/components_ng/pattern/option/option_paint_property.h" 31 #include "core/components_ng/pattern/pattern.h" 32 #include "core/components_ng/pattern/select/select_accessibility_property.h" 33 #include "core/components_ng/pattern/select/select_event_hub.h" 34 #include "core/components_ng/pattern/select/select_layout_algorithm.h" 35 #include "core/components_ng/pattern/select/select_model.h" 36 #include "core/components_ng/pattern/text/text_layout_property.h" 37 38 namespace OHOS::Ace::NG { 39 40 class SelectPattern : public Pattern { 41 DECLARE_ACE_TYPE(SelectPattern, Pattern); 42 43 public: 44 SelectPattern() = default; 45 ~SelectPattern() override = default; 46 IsAtomicNode()47 bool IsAtomicNode() const override 48 { 49 return false; 50 } 51 CreateAccessibilityProperty()52 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 53 { 54 return MakeRefPtr<SelectAccessibilityProperty>(); 55 } 56 CreateLayoutAlgorithm()57 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 58 { 59 return MakeRefPtr<SelectLayoutAlgorithm>(); 60 } 61 CreateEventHub()62 RefPtr<EventHub> CreateEventHub() override 63 { 64 return MakeRefPtr<SelectEventHub>(); 65 } 66 67 void BuildChild(); 68 SetMenuNode(const RefPtr<FrameNode> & menuWrapper)69 void SetMenuNode(const RefPtr<FrameNode>& menuWrapper) 70 { 71 menuWrapper_ = menuWrapper; 72 } 73 GetMenuWrapper()74 const RefPtr<FrameNode>& GetMenuWrapper() const 75 { 76 return menuWrapper_; 77 } 78 GetMenuNode()79 RefPtr<FrameNode> GetMenuNode() const 80 { 81 CHECK_NULL_RETURN(menuWrapper_, nullptr); 82 return DynamicCast<FrameNode>(menuWrapper_->GetChildAtIndex(0)); 83 } 84 SetSelectSize(const SizeF & selectSize)85 void SetSelectSize(const SizeF& selectSize) 86 { 87 selectSize_ = selectSize; 88 } GetSelectSize()89 SizeF GetSelectSize() const 90 { 91 return selectSize_; 92 } 93 94 void AddOptionNode(const RefPtr<FrameNode>& option); 95 ClearOptions()96 void ClearOptions() 97 { 98 options_.clear(); 99 } 100 InitSelected()101 void InitSelected() 102 { 103 selected_ = -1; 104 } 105 GetSelected()106 int32_t GetSelected() const 107 { 108 return selected_; 109 } 110 111 void SetSelected(int32_t index); 112 113 // set properties of text node 114 void SetValue(const std::string& value); 115 void SetFontSize(const Dimension& value); 116 void SetItalicFontStyle(const Ace::FontStyle& value); 117 void SetFontWeight(const FontWeight& value); 118 void SetFontFamily(const std::vector<std::string>& value); 119 void SetFontColor(const Color& color); 120 121 // set props of option nodes 122 void SetOptionBgColor(const Color& color); 123 void SetOptionFontSize(const Dimension& value); 124 void SetOptionItalicFontStyle(const Ace::FontStyle& value); 125 void SetOptionFontWeight(const FontWeight& value); 126 void SetOptionFontFamily(const std::vector<std::string>& value); 127 void SetOptionFontColor(const Color& color); 128 129 // set props of option node when selected 130 void SetSelectedOptionBgColor(const Color& color); 131 void SetSelectedOptionFontSize(const Dimension& value); 132 void SetSelectedOptionItalicFontStyle(const Ace::FontStyle& value); 133 void SetSelectedOptionFontWeight(const FontWeight& value); 134 void SetSelectedOptionFontFamily(const std::vector<std::string>& value); 135 void SetSelectedOptionFontColor(const Color& color); 136 137 // Get functions for unit tests 138 const std::vector<RefPtr<FrameNode>>& GetOptions(); 139 GetFocusPattern()140 FocusPattern GetFocusPattern() const override 141 { 142 return { FocusType::NODE, true, FocusStyleType::INNER_BORDER }; 143 } 144 145 // update selected option props 146 void UpdateSelectedProps(int32_t index); 147 148 void UpdateLastSelectedProps(int32_t index); 149 150 // reset options props when selected index is -1 151 void ResetOptionProps(); 152 SetBgBlendColor(const Color & color)153 void SetBgBlendColor(const Color& color) 154 { 155 bgBlendColor_ = color; 156 } 157 GetBgBlendColor()158 Color GetBgBlendColor() const 159 { 160 return bgBlendColor_; 161 } 162 SetIsHover(bool isHover)163 void SetIsHover(bool isHover) 164 { 165 isHover_ = isHover; 166 } 167 IsHover()168 bool IsHover() const 169 { 170 return isHover_; 171 } 172 173 void PlayBgColorAnimation(bool isHoverChange = true); 174 void SetSpace(const Dimension& value); 175 void SetArrowPosition(const ArrowPosition value); 176 void SetMenuAlign(const MenuAlign& menuAlign); 177 178 std::string GetValue(); 179 std::string ProvideRestoreInfo() override; 180 void OnRestoreInfo(const std::string& restoreInfo) override; 181 void OnColorConfigurationUpdate() override; 182 183 private: 184 void OnAttachToFrameNode() override; 185 void OnModifyDone() override; 186 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 187 HasRowNode()188 bool HasRowNode() const 189 { 190 return rowId_.has_value(); 191 } 192 HasTextNode()193 bool HasTextNode() const 194 { 195 return textId_.has_value(); 196 } 197 HasSpinnerNode()198 bool HasSpinnerNode() const 199 { 200 return spinnerId_.has_value(); 201 } 202 GetRowId()203 int32_t GetRowId() 204 { 205 if (!rowId_.has_value()) { 206 rowId_ = ElementRegister::GetInstance()->MakeUniqueId(); 207 } 208 return rowId_.value(); 209 } 210 GetTextId()211 int32_t GetTextId() 212 { 213 if (!textId_.has_value()) { 214 textId_ = ElementRegister::GetInstance()->MakeUniqueId(); 215 } 216 return textId_.value(); 217 } 218 GetSpinnerId()219 int32_t GetSpinnerId() 220 { 221 if (!spinnerId_.has_value()) { 222 spinnerId_ = ElementRegister::GetInstance()->MakeUniqueId(); 223 } 224 return spinnerId_.value(); 225 } 226 227 // change background color when pressed 228 void RegisterOnPress(); 229 // change background color when hovered 230 void RegisterOnHover(); 231 // add click event to show menu 232 void RegisterOnClick(); 233 234 void RegisterOnKeyEvent(); 235 bool OnKeyEvent(const KeyEvent& event); 236 237 // callback when an option is selected 238 void CreateSelectedCallback(); 239 // change text and spinner color if disabled 240 void SetDisabledStyle(); 241 242 void ShowSelectMenu(); 243 244 // update text to selected option's text 245 void UpdateText(int32_t index); 246 247 void InitTextProps(const RefPtr<TextLayoutProperty>& textProps, const RefPtr<SelectTheme>& theme); 248 void InitSpinner( 249 const RefPtr<FrameNode>& spinner, const RefPtr<IconTheme>& iconTheme, const RefPtr<SelectTheme>& selectTheme); 250 251 std::vector<RefPtr<FrameNode>> options_; 252 RefPtr<FrameNode> menuWrapper_ = nullptr; 253 RefPtr<FrameNode> text_ = nullptr; 254 RefPtr<FrameNode> spinner_ = nullptr; 255 SizeF selectSize_; 256 257 // index of selected option 258 int32_t selected_ = -1; 259 // props when selected 260 struct OptionFont { 261 // text style when selected 262 std::optional<Dimension> FontSize; 263 std::optional<Ace::FontStyle> FontStyle; 264 std::optional<FontWeight> FontWeight; 265 std::optional<std::vector<std::string>> FontFamily; 266 std::optional<Color> FontColor; 267 }; 268 OptionFont selectedFont_; 269 std::optional<Color> selectedBgColor_; 270 OptionFont optionFont_; 271 std::optional<Color> optionBgColor_; 272 273 void ToJsonValue(std::unique_ptr<JsonValue>& json) const override; 274 void ToJsonOptionAlign(std::unique_ptr<JsonValue>& json) const; 275 // XTS inspector helper functions 276 std::string InspectorGetOptions() const; 277 std::string InspectorGetSelectedFont() const; 278 279 std::optional<int32_t> rowId_; 280 std::optional<int32_t> textId_; 281 std::optional<int32_t> spinnerId_; 282 283 Color bgBlendColor_ = Color::TRANSPARENT; 284 bool isHover_ = false; 285 bool isSelected_ = false; 286 MenuAlign menuAlign_; 287 std::string selectValue_; 288 bool isColorConfigurationUpdate_ = false; 289 ACE_DISALLOW_COPY_AND_MOVE(SelectPattern); 290 }; 291 292 } // namespace OHOS::Ace::NG 293 294 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_SELECT_PATTERN_H 295