1 /* 2 * Copyright (c) 2021 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_SELECT_SELECT_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_COMPONENT_H 18 19 #include <vector> 20 21 #include "base/geometry/dimension.h" 22 #include "core/components/box/box_component.h" 23 #include "core/components/common/properties/color.h" 24 #include "core/components/select/select_theme.h" 25 #include "core/components/select_popup/select_popup_component.h" 26 #include "core/components/text/text_component.h" 27 #include "core/components_v2/common/common_def.h" 28 #include "core/pipeline/base/sole_child_component.h" 29 30 namespace OHOS::Ace { 31 32 class ACE_EXPORT SelectComponent : public SoleChildComponent { 33 DECLARE_ACE_TYPE(SelectComponent, SoleChildComponent); 34 35 public: 36 SelectComponent(); 37 ~SelectComponent() override = default; 38 39 RefPtr<RenderNode> CreateRenderNode() override; 40 RefPtr<Element> CreateElement() override; 41 42 void InitTheme(const RefPtr<ThemeManager>& themeManager); 43 SetTextDirection(TextDirection direction)44 void SetTextDirection(TextDirection direction) override 45 { 46 SoleChildComponent::SetTextDirection(direction); 47 if (popup_) { 48 popup_->SetTextDirection(direction); 49 } else { 50 LOGE("popup component of select is null."); 51 } 52 } 53 GetDisabled()54 bool GetDisabled() const 55 { 56 return disabled_; 57 } SetDisabled(bool disabled)58 void SetDisabled(bool disabled) 59 { 60 disabled_ = disabled; 61 } 62 GetClicked()63 bool GetClicked() const 64 { 65 return clicked_; 66 } 67 void SetClicked(bool clicked, const Color& pressColor); 68 GetClickedColor()69 const Color& GetClickedColor() const 70 { 71 return theme_->GetClickedColor(); 72 } SetClickedColor(const Color & clickedColor)73 void SetClickedColor(const Color& clickedColor) 74 { 75 theme_->SetClickedColor(clickedColor); 76 } 77 GetSelectedColor()78 const Color& GetSelectedColor() const 79 { 80 return theme_->GetSelectedColor(); 81 } SetSelectedColor(const Color & selectedColor)82 void SetSelectedColor(const Color& selectedColor) 83 { 84 theme_->SetSelectedColor(selectedColor); 85 } 86 GetDisabledColor()87 const Color& GetDisabledColor() const 88 { 89 return theme_->GetDisabledColor(); 90 } SetDisabledColor(const Color & disabledColor)91 void SetDisabledColor(const Color& disabledColor) 92 { 93 theme_->SetDisabledColor(disabledColor); 94 } 95 IsAllowScale()96 bool IsAllowScale() const 97 { 98 return theme_->IsAllowScale(); 99 } SetAllowScale(bool value)100 void SetAllowScale(bool value) 101 { 102 theme_->SetAllowScale(value); 103 } 104 GetTextColor()105 const Color& GetTextColor() const 106 { 107 return theme_->GetFontColor(); 108 } SetTextColor(const Color & value)109 void SetTextColor(const Color& value) 110 { 111 theme_->SetFontColor(value); 112 } 113 GetFontWeight()114 FontWeight GetFontWeight() const 115 { 116 return theme_->GetFontWeight(); 117 } SetFontWeight(FontWeight value)118 void SetFontWeight(FontWeight value) 119 { 120 theme_->SetFontWeight(value); 121 } 122 GetTextDecoration()123 TextDecoration GetTextDecoration() const 124 { 125 return theme_->GetTextDecoration(); 126 } SetTextDecoration(TextDecoration value)127 void SetTextDecoration(TextDecoration value) 128 { 129 theme_->SetTextDecoration(value); 130 } 131 GetOnChanged()132 const EventMarker& GetOnChanged() const 133 { 134 return onChanged_; 135 } SetOnChanged(const EventMarker & onChanged)136 void SetOnChanged(const EventMarker& onChanged) 137 { 138 onChanged_ = onChanged; 139 } 140 GetTipText()141 const RefPtr<TextComponent>& GetTipText() const 142 { 143 return tipText_; 144 } SetTipText(const RefPtr<TextComponent> & tipText)145 void SetTipText(const RefPtr<TextComponent>& tipText) 146 { 147 tipText_ = tipText; 148 } 149 IsFontSet()150 bool IsFontSet() const 151 { 152 return isSelectFontSize_; 153 } 154 SetIsFontSetFlag(bool flag)155 void SetIsFontSetFlag(bool flag) 156 { 157 isSelectFontSize_ = flag; 158 } 159 GetFontSize()160 const Dimension& GetFontSize() const 161 { 162 return theme_->GetFontSize(); 163 } SetFontSize(const Dimension & fontSize)164 void SetFontSize(const Dimension& fontSize) 165 { 166 theme_->SetFontSize(fontSize); 167 } 168 GetFontFamily()169 const std::string& GetFontFamily() const 170 { 171 return theme_->GetFontFamily(); 172 } SetFontFamily(const std::string & fontFamily)173 void SetFontFamily(const std::string& fontFamily) 174 { 175 theme_->SetFontFamily(fontFamily); 176 } 177 GetOptionSize()178 std::size_t GetOptionSize() const 179 { 180 return theme_->GetOptionSize(); 181 } SetOptionSize(std::size_t optionSize)182 void SetOptionSize(std::size_t optionSize) 183 { 184 theme_->SetOptionSize(optionSize); 185 } 186 GetRRectSize()187 const Dimension& GetRRectSize() const 188 { 189 return theme_->GetRRectSize(); 190 } SetRRectSize(const Dimension & rrectSize)191 void SetRRectSize(const Dimension& rrectSize) 192 { 193 theme_->SetRRectSize(rrectSize); 194 } 195 GetPopupShadowWidth()196 const Dimension& GetPopupShadowWidth() const 197 { 198 return theme_->GetPopupShadowWidth(); 199 } SetPopupShadowWidth(const Dimension & popupShadowWidth)200 void SetPopupShadowWidth(const Dimension& popupShadowWidth) 201 { 202 theme_->SetPopupShadowWidth(popupShadowWidth); 203 } 204 GetPopupBorderWidth()205 const Dimension& GetPopupBorderWidth() const 206 { 207 return theme_->GetPopupBorderWidth(); 208 } SetPopupBorderWidth(const Dimension & popupBorderWidth)209 void SetPopupBorderWidth(const Dimension& popupBorderWidth) 210 { 211 theme_->SetPopupBorderWidth(popupBorderWidth); 212 } 213 GetNormalPadding()214 const Dimension& GetNormalPadding() const 215 { 216 return theme_->GetNormalPadding(); 217 } 218 SetOptionClickedCallback(const std::function<void (std::size_t)> & clickedCallback)219 void SetOptionClickedCallback(const std::function<void(std::size_t)>& clickedCallback) 220 { 221 if (popup_) { 222 popup_->SetOptionClickedCallback(clickedCallback); 223 } 224 } 225 SetOptionModifiedCallback(const std::function<void (std::size_t)> & value)226 void SetOptionModifiedCallback(const std::function<void(std::size_t)>& value) 227 { 228 if (popup_) { 229 popup_->SetOptionModifiedCallback(value); 230 } 231 } 232 GetSelectOptionCount()233 std::size_t GetSelectOptionCount() const 234 { 235 if (!popup_) { 236 return 0; 237 } 238 239 return popup_->GetSelectOptionCount(); 240 } 241 GetSelectOption(std::size_t index)242 RefPtr<OptionComponent> GetSelectOption(std::size_t index) 243 { 244 if (!popup_) { 245 return nullptr; 246 } 247 selected_ = index; 248 return popup_->GetSelectOption(index); 249 } 250 GetSelected()251 std::size_t GetSelected() const 252 { 253 return selected_; 254 } 255 ClearAllOptions()256 void ClearAllOptions() 257 { 258 if (popup_) { 259 popup_->ClearAllOptions(); 260 } 261 } 262 AppendSelectOption(const RefPtr<OptionComponent> & selectOption)263 void AppendSelectOption(const RefPtr<OptionComponent>& selectOption) 264 { 265 if (popup_) { 266 popup_->AppendSelectOption(selectOption); 267 } 268 } 269 RemoveSelectOption(const RefPtr<OptionComponent> & selectOption)270 void RemoveSelectOption(const RefPtr<OptionComponent>& selectOption) 271 { 272 if (popup_) { 273 popup_->RemoveSelectOption(selectOption); 274 } 275 } 276 InsertSelectOption(const RefPtr<OptionComponent> & selectOption,uint32_t index)277 void InsertSelectOption(const RefPtr<OptionComponent>& selectOption, uint32_t index) 278 { 279 if (popup_) { 280 popup_->InsertSelectOption(selectOption, index); 281 } 282 } 283 284 RefPtr<SelectPopupComponent> GetPopup() const; 285 286 RefPtr<BoxComponent> GetBoxComponent() const; 287 288 bool Initialize(); 289 FlushRefresh()290 void FlushRefresh() 291 { 292 if (flushRefreshCallback_) { 293 flushRefreshCallback_(); 294 } 295 } 296 SetBackgroundColor(const Color & value)297 void SetBackgroundColor(const Color& value) 298 { 299 backgroundColor_ = value; 300 } 301 SetInnerPadding(const Edge & innerPadding)302 void SetInnerPadding(const Edge& innerPadding) 303 { 304 innerPadding_ = innerPadding; 305 } 306 SetInnerSize(const Dimension & width,const Dimension & height)307 void SetInnerSize(const Dimension& width, const Dimension& height) 308 { 309 width_ = width; 310 height_ = height; 311 } 312 SetInnerBorder(const Border & border)313 void SetInnerBorder(const Border& border) 314 { 315 border_ = border; 316 } 317 GetInnerBorder()318 const Border& GetInnerBorder() const 319 { 320 return border_; 321 } 322 323 // used for inspector node in PC preview SetNodeId(const int32_t nodeId)324 void SetNodeId(const int32_t nodeId) 325 { 326 nodeId_ = nodeId; 327 } 328 329 // used for inspector node in PC preview GetNodeId()330 int32_t GetNodeId() const 331 { 332 return nodeId_; 333 } 334 GetSelectStyle()335 const TextStyle& GetSelectStyle() const 336 { 337 return theme_->GetTitleStyle(); 338 } 339 SetSelectStyle(const TextStyle & style)340 void SetSelectStyle(const TextStyle& style) 341 { 342 theme_->SetTitleStyle(style); 343 } 344 SetTheme(const RefPtr<SelectTheme> & theme)345 void SetTheme(const RefPtr<SelectTheme>& theme) 346 { 347 if (popup_) { 348 popup_->SetTheme(theme); 349 } 350 theme_ = theme; 351 } 352 ACE_DEFINE_COMPONENT_EVENT(OnSelected, void(int32_t, std::string)); 353 GetSelectPopupComponent()354 RefPtr<SelectPopupComponent> GetSelectPopupComponent() const 355 { 356 return popup_; 357 } 358 SetWidth(const Dimension & width)359 void SetWidth(const Dimension& width) 360 { 361 width_ = width; 362 } 363 SetHeight(const Dimension & height)364 void SetHeight(const Dimension& height) 365 { 366 height_ = height; 367 } 368 SetLeftPadding(const Dimension & padding)369 void SetLeftPadding(const Dimension& padding) 370 { 371 innerPadding_.SetLeft(padding);; 372 } 373 SetRightPadding(const Dimension & padding)374 void SetRightPadding(const Dimension& padding) 375 { 376 innerPadding_.SetRight(padding); 377 } 378 GetLeftPadding()379 const Dimension& GetLeftPadding() const 380 { 381 return innerPadding_.Left(); 382 } 383 GetRightPadding()384 const Dimension& GetRightPadding() const 385 { 386 return innerPadding_.Right(); 387 } 388 SetTopPadding(const Dimension & padding)389 void SetTopPadding(const Dimension& padding) 390 { 391 innerPadding_.SetTop(padding); 392 } 393 SetBottomPadding(const Dimension & padding)394 void SetBottomPadding(const Dimension& padding) 395 { 396 innerPadding_.SetBottom(padding); 397 } 398 GetTopPadding()399 const Dimension& GetTopPadding() const 400 { 401 return innerPadding_.Top(); 402 } 403 GetBottomPadding()404 const Dimension& GetBottomPadding() const 405 { 406 return innerPadding_.Bottom(); 407 } 408 GetHeight()409 const Dimension& GetHeight() const 410 { 411 return height_; 412 } 413 GetWidth()414 const Dimension& GetWidth() const 415 { 416 return width_; 417 } 418 419 private: 420 bool disabled_ { false }; 421 bool clicked_ { false }; 422 EventMarker onChanged_; 423 RefPtr<TextComponent> tipText_; 424 RefPtr<SelectPopupComponent> popup_; 425 RefPtr<BoxComponent> boxComponent_; 426 RefPtr<SelectTheme> theme_; 427 std::function<void()> flushRefreshCallback_; 428 Color backgroundColor_ = Color::TRANSPARENT; 429 std::size_t selected_ = 0; 430 // used for inspector node in PC preview 431 int32_t nodeId_ = -1; 432 433 // used in navigation 434 bool isSelectFontSize_ = false; 435 Edge innerPadding_ = Edge(8.0, 9.25, 8.0, 9.25, DimensionUnit::VP); 436 437 Dimension width_ = -1.0_px; 438 Dimension height_ = -1.0_px; 439 Border border_; 440 }; 441 442 } // namespace OHOS::Ace 443 444 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_SELECT_SELECT_COMPONENT_H 445