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_PICKER_PICKER_BASE_COMPONENT_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_BASE_COMPONENT_H 18 19 #include "base/utils/system_properties.h" 20 #include "core/accessibility/accessibility_manager.h" 21 #include "core/common/container.h" 22 #include "core/components/checkable/checkable_component.h" 23 #include "core/components/common/rotation/rotation_controller.h" 24 #include "core/components/dialog/dialog_component.h" 25 #include "core/components/picker/picker_animation_controller.h" 26 #include "core/components/picker/picker_column_component.h" 27 #include "core/components/stack/stack_element.h" 28 #include "core/components/text/text_component.h" 29 #include "core/event/back_end_event_manager.h" 30 #include "core/pipeline/base/sole_child_component.h" 31 #include "core/components_v2/common/common_def.h" 32 33 namespace OHOS::Ace { 34 35 enum DatePickerType { 36 TIME = 0, 37 DATE, 38 }; 39 40 class ACE_EXPORT PickerDate final { 41 public: 42 PickerDate() = default; PickerDate(uint32_t year,uint32_t month,uint32_t day)43 PickerDate(uint32_t year, uint32_t month, uint32_t day) : year_(year), month_(month), day_(day) {} 44 45 ~PickerDate() = default; 46 47 static PickerDate Current(); 48 49 static uint32_t GetMaxDay(uint32_t year, uint32_t month); 50 51 static bool IsLeapYear(uint32_t year); 52 GetYear()53 uint32_t GetYear() const 54 { 55 return year_; 56 } SetYear(uint32_t value)57 void SetYear(uint32_t value) 58 { 59 year_ = value; 60 } 61 GetMonth()62 uint32_t GetMonth() const 63 { 64 return month_; 65 } SetMonth(uint32_t value)66 void SetMonth(uint32_t value) 67 { 68 month_ = value; 69 } 70 GetDay()71 uint32_t GetDay() const 72 { 73 return day_; 74 } SetDay(uint32_t value)75 void SetDay(uint32_t value) 76 { 77 day_ = value; 78 } 79 GetWeek()80 uint32_t GetWeek() const 81 { 82 return week_; 83 } SetWeek(uint32_t value)84 void SetWeek(uint32_t value) 85 { 86 week_ = value; 87 } 88 89 std::string ToString(bool jsonFormat, int32_t status = -1) const; 90 91 uint32_t ToDays() const; 92 void FromDays(uint32_t days); 93 94 private: 95 uint32_t year_ = 0; 96 uint32_t month_ = 0; 97 uint32_t day_ = 0; 98 uint32_t week_ = 0; 99 }; 100 101 class PickerTime final { 102 public: 103 PickerTime() = default; PickerTime(uint32_t hour,uint32_t minute,uint32_t second)104 PickerTime(uint32_t hour, uint32_t minute, uint32_t second) : hour_(hour), minute_(minute), second_(second) {} 105 106 ~PickerTime() = default; 107 108 static PickerTime Current(); 109 GetHour()110 uint32_t GetHour() const 111 { 112 return hour_; 113 } SetHour(uint32_t value)114 void SetHour(uint32_t value) 115 { 116 hour_ = value; 117 } 118 GetMinute()119 uint32_t GetMinute() const 120 { 121 return minute_; 122 } SetMinute(uint32_t value)123 void SetMinute(uint32_t value) 124 { 125 minute_ = value; 126 } 127 GetSecond()128 uint32_t GetSecond() const 129 { 130 return second_; 131 } SetSecond(uint32_t value)132 void SetSecond(uint32_t value) 133 { 134 second_ = value; 135 } 136 137 std::string ToString(bool jsonFormat, bool hasSecond, int32_t status = -1) const; 138 139 private: 140 uint32_t hour_ = 0; 141 uint32_t minute_ = 0; 142 uint32_t second_ = 0; 143 }; 144 145 class PickerDateTime final { 146 public: 147 PickerDateTime() = default; PickerDateTime(const PickerDate & date,const PickerTime & time)148 PickerDateTime(const PickerDate& date, const PickerTime& time) : date_(date), time_(time) {} 149 150 ~PickerDateTime() = default; 151 152 static PickerDateTime Current(); 153 GetDate()154 const PickerDate& GetDate() const 155 { 156 return date_; 157 } SetDate(const PickerDate & value)158 void SetDate(const PickerDate& value) 159 { 160 date_ = value; 161 } 162 GetTime()163 const PickerTime& GetTime() const 164 { 165 return time_; 166 } SetTime(const PickerTime & value)167 void SetTime(const PickerTime& value) 168 { 169 time_ = value; 170 } 171 172 std::string ToString(bool jsonFormat, int32_t status = -1) const; 173 174 private: 175 PickerDate date_; 176 PickerTime time_; 177 }; 178 179 class ACE_EXPORT PickerBaseComponent : public SoleChildComponent { 180 DECLARE_ACE_TYPE(PickerBaseComponent, SoleChildComponent); 181 182 public: PickerBaseComponent()183 PickerBaseComponent() : rotationController_(AceType::MakeRefPtr<RotationController>()) {} 184 ~PickerBaseComponent() override = default; 185 186 RefPtr<RenderNode> CreateRenderNode() override; 187 RefPtr<Element> CreateElement() override; 188 189 void Initialize(const RefPtr<AccessibilityManager>& accessibilityManager, const RefPtr<ThemeManager>& themeManager); 190 SetDialogName(const std::string & dialogName)191 void SetDialogName(const std::string& dialogName) 192 { 193 dialogInsprctorTag_ = dialogName; 194 } 195 GetDialogName()196 const std::string& GetDialogName() 197 { 198 return dialogInsprctorTag_; 199 } 200 GetHasTitle()201 bool GetHasTitle() const 202 { 203 return hasTitle_; 204 } SetHasTitle(bool value)205 void SetHasTitle(bool value) 206 { 207 hasTitle_ = (!subsidiary_ && value); 208 } 209 GetHasLunar()210 bool GetHasLunar() const 211 { 212 return hasLunar_; 213 } SetHasLunar(bool value)214 void SetHasLunar(bool value) 215 { 216 hasLunar_ = SystemProperties::GetDeviceType() == DeviceType::PHONE && value; 217 } 218 GetHasButtons()219 bool GetHasButtons() const 220 { 221 return hasButtons_; 222 } SetHasButtons(bool value)223 void SetHasButtons(bool value) 224 { 225 hasButtons_ = (!subsidiary_ && value); 226 } 227 GetHasTriangle()228 bool GetHasTriangle() const 229 { 230 return hasTriangle_; 231 } SetHasTriangle(bool value)232 void SetHasTriangle(bool value) 233 { 234 hasTriangle_ = value; 235 } 236 GetIsDialog()237 bool GetIsDialog() const 238 { 239 return isDialog_; 240 } SetIsDialog(bool value)241 void SetIsDialog(bool value) 242 { 243 isDialog_ = value; 244 } 245 GetIsCreateDialogComponent()246 bool GetIsCreateDialogComponent() const 247 { 248 return isCreateDialogComponent_; 249 } SetIsCreateDialogComponent(bool value)250 void SetIsCreateDialogComponent(bool value) 251 { 252 isCreateDialogComponent_ = value; 253 } 254 GetSubsidiary()255 bool GetSubsidiary() const 256 { 257 return subsidiary_; 258 } SetSubsidiary(bool value)259 void SetSubsidiary(bool value) 260 { 261 subsidiary_ = value; 262 } 263 GetMasterHasLunar()264 bool GetMasterHasLunar() const 265 { 266 return masterHasLunar_; 267 } SetMasterHasLunar(bool value)268 void SetMasterHasLunar(bool value) 269 { 270 masterHasLunar_ = value; 271 } 272 GetOnChange()273 const EventMarker& GetOnChange() const 274 { 275 return onChange_; 276 } SetOnChange(const EventMarker & value)277 void SetOnChange(const EventMarker& value) 278 { 279 onChange_ = value; 280 } 281 GetOnColumnChange()282 const EventMarker& GetOnColumnChange() const 283 { 284 return onColumnChange_; 285 } SetOnColumnChange(const EventMarker & value)286 void SetOnColumnChange(const EventMarker& value) 287 { 288 onColumnChange_ = value; 289 } 290 GetOnCancel()291 const EventMarker& GetOnCancel() const 292 { 293 return onCancel_; 294 } SetOnCancel(const EventMarker & value)295 void SetOnCancel(const EventMarker& value) 296 { 297 onCancel_ = value; 298 } 299 GetDialogAcceptEvent()300 const EventMarker& GetDialogAcceptEvent() const 301 { 302 return OnDialogAccept_; 303 } SetDialogAcceptEvent(const EventMarker & value)304 void SetDialogAcceptEvent(const EventMarker& value) 305 { 306 OnDialogAccept_ = value; 307 } 308 GetDialogCancelEvent()309 const EventMarker& GetDialogCancelEvent() const 310 { 311 return OnDialogCancel_; 312 } SetDialogCancelEvent(const EventMarker & value)313 void SetDialogCancelEvent(const EventMarker& value) 314 { 315 OnDialogCancel_ = value; 316 } 317 GetDialogChangeEvent()318 const EventMarker& GetDialogChangeEvent() const 319 { 320 return OnDialogChange_; 321 } SetDialogChangeEvent(const EventMarker & value)322 void SetDialogChangeEvent(const EventMarker& value) 323 { 324 OnDialogChange_ = value; 325 } 326 327 void ClearColumns(); 328 329 void AppendColumn(const RefPtr<PickerColumnComponent>& column); 330 331 RefPtr<PickerColumnComponent> GetColumn(const std::string& tag) const; 332 GetColumnCount()333 virtual uint32_t GetColumnCount() const 334 { 335 return columns_.size(); 336 } 337 338 void RemoveColumn(const std::string& tag); 339 340 void SetFinishCallback(const ColumnFinishCallback& value); 341 342 void SetChangeCallback(const ColumnChangeCallback& value); 343 GetTheme()344 const RefPtr<PickerTheme>& GetTheme() const 345 { 346 return theme_; 347 } SetTheme(const RefPtr<PickerTheme> & value)348 void SetTheme(const RefPtr<PickerTheme>& value) 349 { 350 theme_ = value; 351 } 352 353 GetAnimationController()354 const RefPtr<PickerAnimationController>& GetAnimationController() const 355 { 356 return animationController_; 357 } 358 SetAnimationController(const RefPtr<PickerAnimationController> & value)359 void SetAnimationController(const RefPtr<PickerAnimationController>& value) 360 { 361 animationController_ = value; 362 } 363 GetTitle()364 const RefPtr<TextComponent>& GetTitle() const 365 { 366 return title_; 367 } 368 GetLunar()369 const RefPtr<CheckboxComponent>& GetLunar() const 370 { 371 return lunar_; 372 } 373 GetNodeId()374 int32_t GetNodeId() const 375 { 376 return nodeId_; 377 } 378 SetNodeId(int32_t value)379 void SetNodeId(int32_t value) 380 { 381 if (!GetIsDialog()) { 382 nodeId_ = value; 383 } 384 } 385 GetColumnHeight()386 const Dimension& GetColumnHeight() const 387 { 388 return columnHeight_; 389 } SetColumnHeight(const Dimension & value)390 void SetColumnHeight(const Dimension& value) 391 { 392 columnHeight_ = value; 393 } 394 GetStack()395 const RefPtr<StackElement>& GetStack() 396 { 397 return stack_; 398 } 399 400 void ShowDialog(const RefPtr<StackElement>& stack, bool disableTouchEvent = true); 401 bool HideDialog(); 402 void OpenDialog(DialogProperties& properties); 403 void CloseDialog(); 404 IsDialogShowed()405 bool IsDialogShowed() 406 { 407 return dialogShowed_; 408 } 409 GetLunarAccessibility()410 const RefPtr<AccessibilityNode>& GetLunarAccessibility() const 411 { 412 return lunarAccessibility_; 413 } 414 GetSwitchAccessibility()415 const RefPtr<AccessibilityNode>& GetSwitchAccessibility() const 416 { 417 return switchAccessibility_; 418 } 419 GetTitleAccessibility()420 const RefPtr<AccessibilityNode>& GetTitleAccessibility() const 421 { 422 return titleAccessibility_; 423 } 424 GetCancelAccessibility()425 const RefPtr<AccessibilityNode>& GetCancelAccessibility() const 426 { 427 return cancelAccessibility_; 428 } 429 GetOkAccessibility()430 const RefPtr<AccessibilityNode>& GetOkAccessibility() const 431 { 432 return okAccessibility_; 433 } 434 435 // used for inspector node in PC preview GetPickerDialogAccessibility()436 const RefPtr<AccessibilityNode>& GetPickerDialogAccessibility() const 437 { 438 return rootAccessibility_; 439 } 440 SubsidiaryShowed()441 virtual bool SubsidiaryShowed() const 442 { 443 return false; 444 } 445 NeedRtlColumnOrder()446 virtual bool NeedRtlColumnOrder() const 447 { 448 return true; 449 } 450 IsShowLunar()451 virtual bool IsShowLunar() const 452 { 453 return false; 454 } 455 456 virtual void OnTitleBuilding(); 457 OnColumnsCreating()458 virtual void OnColumnsCreating() {} 459 OnColumnsBuilding()460 virtual void OnColumnsBuilding() {} 461 462 virtual std::string GetSelectedObject(bool isColumnChange, 463 const std::string& changeColumnTag, int32_t status = -1) const 464 { 465 return "{}"; 466 } 467 OnSelectedSaving()468 virtual void OnSelectedSaving() {} 469 OnDataLinking(const std::string & tag,bool isAdd,uint32_t index,std::vector<std::string> & resultTags)470 virtual void OnDataLinking(const std::string& tag, bool isAdd, uint32_t index, std::vector<std::string>& resultTags) 471 {} 472 OnLunarCallback(bool checked,std::vector<std::string> & resultTags)473 virtual void OnLunarCallback(bool checked, std::vector<std::string>& resultTags) {} OnTriangleCallback(bool value)474 virtual void OnTriangleCallback(bool value) {} OnAnimationPlaying()475 virtual void OnAnimationPlaying() {} 476 GetRotationController()477 const RefPtr<RotationController>& GetRotationController() const 478 { 479 return rotationController_; 480 } 481 GetOnCancelClickId()482 const EventMarker& GetOnCancelClickId() const 483 { 484 return onCancelClickId_; 485 } 486 GetOnOkClickId()487 const EventMarker& GetOnOkClickId() const 488 { 489 return onOkClickId_; 490 } 491 492 // used for inspector node in PC preview SetPickerBaseId(const int32_t nodeId)493 void SetPickerBaseId(const int32_t nodeId) 494 { 495 pickerId_ = nodeId; 496 } 497 498 // used for inspector node in PC preview GetPickerBaseId()499 int32_t GetPickerBaseId() const 500 { 501 return pickerId_; 502 } 503 504 ACE_DEFINE_COMPONENT_EVENT(OnTextCancel, void(void)); 505 ACE_DEFINE_COMPONENT_EVENT(OnTextAccept, void(const std::string&, double)); 506 ACE_DEFINE_COMPONENT_EVENT(OnTextChange, void(const std::string&, double)); 507 508 void SetNeedVibrate(bool needVibrate); 509 GetNeedVibrate()510 bool GetNeedVibrate() 511 { 512 return needVibrate_; 513 } 514 515 static const char PICKER_YEAR_COLUMN[]; 516 static const char PICKER_MONTH_COLUMN[]; 517 static const char PICKER_DAY_COLUMN[]; 518 static const char PICKER_HOUR_COLUMN[]; 519 static const char PICKER_MINUTE_COLUMN[]; 520 static const char PICKER_SECOND_COLUMN[]; 521 static const char PICKER_TEXT_COLUMN[]; 522 static const char PICKER_MONTHDAY_COLUMN[]; 523 static const char PICKER_AMPM_COLUMN[]; 524 525 private: 526 void InitializeTitle(std::list<RefPtr<Component>>& outChildren); 527 528 void InitializeColumns( 529 std::list<RefPtr<Component>>& outChildren, const RefPtr<AccessibilityManager>& accessibilityManager); 530 531 void InitializeChildAccessibility(const RefPtr<AccessibilityManager>& accessibilityManager); 532 533 void ClearAccessibilityNodes(); 534 535 RefPtr<Component> GenerateAccessibilityComposed( 536 const std::string& name, const RefPtr<Component>& child, RefPtr<AccessibilityNode>& node); 537 538 void InitializeLunar(std::list<RefPtr<Component>>& outChildren, const RefPtr<ThemeManager>& themeManager); 539 540 void InitializeButtons(std::list<RefPtr<Component>>& outChildren, const RefPtr<ThemeManager>& themeManager); 541 542 void InitializeContainer(const std::list<RefPtr<Component>>& outChildren); 543 544 bool dialogShowed_ = false; 545 bool hasTitle_ = false; 546 bool hasLunar_ = false; 547 bool hasButtons_ = false; 548 bool hasTriangle_ = false; 549 bool isDialog_ = true; 550 bool subsidiary_ = false; 551 bool masterHasLunar_ = false; 552 bool needVibrate_ = true; 553 bool isCreateDialogComponent_ = false; 554 int32_t nodeId_ = -1; // default of dialog's node id. 555 // used for inspector node in PC preview 556 int32_t pickerId_ = -1; 557 558 RefPtr<PickerTheme> theme_; 559 RefPtr<StackElement> stack_; 560 561 RefPtr<TextComponent> title_ = AceType::MakeRefPtr<TextComponent>(""); 562 RefPtr<CheckboxComponent> lunar_; 563 std::vector<RefPtr<PickerColumnComponent>> columns_; 564 WeakPtr<AccessibilityManager> accessibilityManager_; 565 int32_t rootAccessibilityId_ = -1; 566 RefPtr<AccessibilityNode> rootAccessibility_; 567 RefPtr<AccessibilityNode> titleAccessibility_; 568 RefPtr<AccessibilityNode> lunarAccessibility_; 569 RefPtr<AccessibilityNode> switchAccessibility_; 570 RefPtr<AccessibilityNode> cancelAccessibility_; 571 RefPtr<AccessibilityNode> okAccessibility_; 572 573 EventMarker onChange_; 574 EventMarker onColumnChange_; 575 EventMarker onCancel_; 576 EventMarker onOkClickId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker(); 577 EventMarker onCancelClickId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker(); 578 EventMarker OnDialogAccept_; 579 EventMarker OnDialogCancel_; 580 EventMarker OnDialogChange_; 581 582 Dimension columnHeight_; 583 584 RefPtr<RotationController> rotationController_; 585 586 RefPtr<PickerAnimationController> animationController_; 587 RefPtr<DialogComponent> dialogComponent_; 588 std::string dialogInsprctorTag_; 589 }; 590 591 } // namespace OHOS::Ace 592 593 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_BASE_COMPONENT_H 594