1 /* 2 * Copyright (c) 2021-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_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/picker/picker_data.h" 28 #include "core/components/stack/stack_element.h" 29 #include "core/components/text/text_component.h" 30 #include "core/event/back_end_event_manager.h" 31 #include "core/pipeline/base/sole_child_component.h" 32 #include "core/components_v2/common/common_def.h" 33 34 namespace OHOS::Ace { 35 36 class ACE_EXPORT DatePickerChangeEvent : public BaseEventInfo { 37 DECLARE_RELATIONSHIP_OF_CLASSES(DatePickerChangeEvent, BaseEventInfo); 38 39 public: DatePickerChangeEvent(const std::string & str)40 explicit DatePickerChangeEvent(const std::string& str) : BaseEventInfo 41 ("DatePickerChangeEvent"), selectedStr_(str) {} 42 ~DatePickerChangeEvent() = default; 43 GetSelectedStr()44 const std::string& GetSelectedStr() const 45 { 46 return selectedStr_; 47 } 48 49 private: 50 std::string selectedStr_; 51 }; 52 53 class ACE_EXPORT PickerBaseComponent : public SoleChildComponent { 54 DECLARE_ACE_TYPE(PickerBaseComponent, SoleChildComponent); 55 56 public: PickerBaseComponent()57 PickerBaseComponent() : rotationController_(AceType::MakeRefPtr<RotationController>()) {} 58 ~PickerBaseComponent() override = default; 59 60 RefPtr<RenderNode> CreateRenderNode() override; 61 RefPtr<Element> CreateElement() override; 62 63 void Initialize(const RefPtr<AccessibilityManager>& accessibilityManager, const RefPtr<ThemeManager>& themeManager); 64 SetDialogName(const std::string & dialogName)65 void SetDialogName(const std::string& dialogName) 66 { 67 dialogInsprctorTag_ = dialogName; 68 } 69 GetDialogName()70 const std::string& GetDialogName() 71 { 72 return dialogInsprctorTag_; 73 } 74 GetHasTitle()75 bool GetHasTitle() const 76 { 77 return hasTitle_; 78 } SetHasTitle(bool value)79 void SetHasTitle(bool value) 80 { 81 hasTitle_ = (!subsidiary_ && value); 82 } 83 GetHasLunar()84 bool GetHasLunar() const 85 { 86 return hasLunar_; 87 } SetHasLunar(bool value)88 void SetHasLunar(bool value) 89 { 90 hasLunar_ = SystemProperties::GetDeviceType() == DeviceType::PHONE && value; 91 } 92 GetHasButtons()93 bool GetHasButtons() const 94 { 95 return hasButtons_; 96 } SetHasButtons(bool value)97 void SetHasButtons(bool value) 98 { 99 hasButtons_ = (!subsidiary_ && value); 100 } 101 GetHasTriangle()102 bool GetHasTriangle() const 103 { 104 return hasTriangle_; 105 } SetHasTriangle(bool value)106 void SetHasTriangle(bool value) 107 { 108 hasTriangle_ = value; 109 } 110 GetIsDialog()111 bool GetIsDialog() const 112 { 113 return isDialog_; 114 } SetIsDialog(bool value)115 void SetIsDialog(bool value) 116 { 117 isDialog_ = value; 118 } 119 GetIsCreateDialogComponent()120 bool GetIsCreateDialogComponent() const 121 { 122 return isCreateDialogComponent_; 123 } SetIsCreateDialogComponent(bool value)124 void SetIsCreateDialogComponent(bool value) 125 { 126 isCreateDialogComponent_ = value; 127 } 128 GetSubsidiary()129 bool GetSubsidiary() const 130 { 131 return subsidiary_; 132 } SetSubsidiary(bool value)133 void SetSubsidiary(bool value) 134 { 135 subsidiary_ = value; 136 } 137 GetMasterHasLunar()138 bool GetMasterHasLunar() const 139 { 140 return masterHasLunar_; 141 } SetMasterHasLunar(bool value)142 void SetMasterHasLunar(bool value) 143 { 144 masterHasLunar_ = value; 145 } 146 GetOnChange()147 const EventMarker& GetOnChange() const 148 { 149 return onChange_; 150 } SetOnChange(const EventMarker & value)151 void SetOnChange(const EventMarker& value) 152 { 153 onChange_ = value; 154 } 155 GetOnColumnChange()156 const EventMarker& GetOnColumnChange() const 157 { 158 return onColumnChange_; 159 } SetOnColumnChange(const EventMarker & value)160 void SetOnColumnChange(const EventMarker& value) 161 { 162 onColumnChange_ = value; 163 } 164 GetOnCancel()165 const EventMarker& GetOnCancel() const 166 { 167 return onCancel_; 168 } SetOnCancel(const EventMarker & value)169 void SetOnCancel(const EventMarker& value) 170 { 171 onCancel_ = value; 172 } 173 GetDialogAcceptEvent()174 const EventMarker& GetDialogAcceptEvent() const 175 { 176 return OnDialogAccept_; 177 } SetDialogAcceptEvent(const EventMarker & value)178 void SetDialogAcceptEvent(const EventMarker& value) 179 { 180 OnDialogAccept_ = value; 181 } 182 GetDialogCancelEvent()183 const EventMarker& GetDialogCancelEvent() const 184 { 185 return OnDialogCancel_; 186 } SetDialogCancelEvent(const EventMarker & value)187 void SetDialogCancelEvent(const EventMarker& value) 188 { 189 OnDialogCancel_ = value; 190 } 191 GetDialogChangeEvent()192 const EventMarker& GetDialogChangeEvent() const 193 { 194 return OnDialogChange_; 195 } SetDialogChangeEvent(const EventMarker & value)196 void SetDialogChangeEvent(const EventMarker& value) 197 { 198 OnDialogChange_ = value; 199 } 200 201 void ClearColumns(); 202 203 void AppendColumn(const RefPtr<PickerColumnComponent>& column); 204 205 RefPtr<PickerColumnComponent> GetColumn(const std::string& tag) const; 206 GetColumnCount()207 virtual uint32_t GetColumnCount() const 208 { 209 return columns_.size(); 210 } 211 212 void RemoveColumn(const std::string& tag); 213 214 void SetFinishCallback(const ColumnFinishCallback& value); 215 216 void SetChangeCallback(const ColumnChangeCallback& value); 217 GetTheme()218 const RefPtr<PickerTheme>& GetTheme() const 219 { 220 return theme_; 221 } SetTheme(const RefPtr<PickerTheme> & value)222 void SetTheme(const RefPtr<PickerTheme>& value) 223 { 224 theme_ = value; 225 } 226 227 GetAnimationController()228 const RefPtr<PickerAnimationController>& GetAnimationController() const 229 { 230 return animationController_; 231 } 232 SetAnimationController(const RefPtr<PickerAnimationController> & value)233 void SetAnimationController(const RefPtr<PickerAnimationController>& value) 234 { 235 animationController_ = value; 236 } 237 GetTitle()238 const RefPtr<TextComponent>& GetTitle() const 239 { 240 return title_; 241 } 242 GetLunar()243 const RefPtr<CheckboxComponent>& GetLunar() const 244 { 245 return lunar_; 246 } 247 GetNodeId()248 int32_t GetNodeId() const 249 { 250 return nodeId_; 251 } 252 SetNodeId(int32_t value)253 void SetNodeId(int32_t value) 254 { 255 if (!GetIsDialog()) { 256 nodeId_ = value; 257 } 258 } 259 GetColumnHeight()260 const Dimension& GetColumnHeight() const 261 { 262 return columnHeight_; 263 } SetColumnHeight(const Dimension & value)264 void SetColumnHeight(const Dimension& value) 265 { 266 columnHeight_ = value; 267 } 268 GetDefaultHeight()269 bool GetDefaultHeight() const 270 { 271 return defaultHeight_; 272 } 273 SetDefaultHeight(bool value)274 void SetDefaultHeight(bool value) 275 { 276 defaultHeight_ = value; 277 } 278 GetStack()279 const RefPtr<StackElement>& GetStack() 280 { 281 return stack_; 282 } 283 284 void ShowDialog(const RefPtr<StackElement>& stack, bool disableTouchEvent = true); 285 bool HideDialog(); 286 void OpenDialog(const DialogProperties& properties); 287 void CloseDialog(); 288 IsDialogShowed()289 bool IsDialogShowed() 290 { 291 return dialogShowed_; 292 } 293 GetLunarAccessibility()294 const RefPtr<AccessibilityNode>& GetLunarAccessibility() const 295 { 296 return lunarAccessibility_; 297 } 298 GetSwitchAccessibility()299 const RefPtr<AccessibilityNode>& GetSwitchAccessibility() const 300 { 301 return switchAccessibility_; 302 } 303 GetTitleAccessibility()304 const RefPtr<AccessibilityNode>& GetTitleAccessibility() const 305 { 306 return titleAccessibility_; 307 } 308 GetCancelAccessibility()309 const RefPtr<AccessibilityNode>& GetCancelAccessibility() const 310 { 311 return cancelAccessibility_; 312 } 313 GetOkAccessibility()314 const RefPtr<AccessibilityNode>& GetOkAccessibility() const 315 { 316 return okAccessibility_; 317 } 318 319 // used for inspector node in PC preview GetPickerDialogAccessibility()320 const RefPtr<AccessibilityNode>& GetPickerDialogAccessibility() const 321 { 322 return rootAccessibility_; 323 } 324 SubsidiaryShowed()325 virtual bool SubsidiaryShowed() const 326 { 327 return false; 328 } 329 NeedRtlColumnOrder()330 virtual bool NeedRtlColumnOrder() const 331 { 332 return true; 333 } 334 IsShowLunar()335 virtual bool IsShowLunar() const 336 { 337 return false; 338 } 339 340 virtual void OnTitleBuilding(); 341 OnColumnsCreating()342 virtual void OnColumnsCreating() {} 343 OnColumnsBuilding()344 virtual void OnColumnsBuilding() {} 345 346 virtual std::string GetSelectedObject(bool isColumnChange, 347 const std::string& changeColumnTag, int32_t status = -1) const 348 { 349 return "{}"; 350 } 351 OnSelectedSaving()352 virtual void OnSelectedSaving() {} 353 OnDataLinking(const std::string & tag,bool isAdd,uint32_t index,std::vector<std::string> & resultTags)354 virtual void OnDataLinking(const std::string& tag, bool isAdd, uint32_t index, std::vector<std::string>& resultTags) 355 {} 356 OnLunarCallback(bool checked,std::vector<std::string> & resultTags)357 virtual void OnLunarCallback(bool checked, std::vector<std::string>& resultTags) {} OnTriangleCallback(bool value)358 virtual void OnTriangleCallback(bool value) {} OnAnimationPlaying()359 virtual void OnAnimationPlaying() {} 360 GetRotationController()361 const RefPtr<RotationController>& GetRotationController() const 362 { 363 return rotationController_; 364 } 365 GetOnCancelClickId()366 const EventMarker& GetOnCancelClickId() const 367 { 368 return onCancelClickId_; 369 } 370 GetOnOkClickId()371 const EventMarker& GetOnOkClickId() const 372 { 373 return onOkClickId_; 374 } 375 376 // used for inspector node in PC preview SetPickerBaseId(const int32_t nodeId)377 void SetPickerBaseId(const int32_t nodeId) 378 { 379 pickerId_ = nodeId; 380 } 381 382 // used for inspector node in PC preview GetPickerBaseId()383 int32_t GetPickerBaseId() const 384 { 385 return pickerId_; 386 } 387 388 ACE_DEFINE_COMPONENT_EVENT(OnTextCancel, void(void)); 389 ACE_DEFINE_COMPONENT_EVENT(OnTextAccept, void(const std::string&, double)); 390 ACE_DEFINE_COMPONENT_EVENT(OnTextChange, void(const std::string&, double)); 391 392 void SetNeedVibrate(bool needVibrate); 393 GetNeedVibrate()394 bool GetNeedVibrate() 395 { 396 return needVibrate_; 397 } 398 GetHasBackgroundColor()399 bool GetHasBackgroundColor() const 400 { 401 return hasBackgroundColor_; 402 } SetHasBackgroundColor(bool value)403 void SetHasBackgroundColor(bool value) 404 { 405 hasBackgroundColor_ = value; 406 } 407 408 static const char PICKER_YEAR_COLUMN[]; 409 static const char PICKER_MONTH_COLUMN[]; 410 static const char PICKER_DAY_COLUMN[]; 411 static const char PICKER_HOUR_COLUMN[]; 412 static const char PICKER_MINUTE_COLUMN[]; 413 static const char PICKER_SECOND_COLUMN[]; 414 static const char PICKER_TEXT_COLUMN[]; 415 static const char PICKER_MONTHDAY_COLUMN[]; 416 static const char PICKER_AMPM_COLUMN[]; 417 418 private: 419 void InitializeTitle(std::list<RefPtr<Component>>& outChildren); 420 421 void InitializeColumns( 422 std::list<RefPtr<Component>>& outChildren, const RefPtr<AccessibilityManager>& accessibilityManager); 423 424 void InitializeChildAccessibility(const RefPtr<AccessibilityManager>& accessibilityManager); 425 426 void ClearAccessibilityNodes(); 427 428 RefPtr<Component> GenerateAccessibilityComposed( 429 const std::string& name, const RefPtr<Component>& child, RefPtr<AccessibilityNode>& node); 430 431 void InitializeLunar(std::list<RefPtr<Component>>& outChildren, const RefPtr<ThemeManager>& themeManager); 432 433 void InitializeButtons(std::list<RefPtr<Component>>& outChildren, const RefPtr<ThemeManager>& themeManager); 434 435 void InitializeContainer(const std::list<RefPtr<Component>>& outChildren); 436 437 bool dialogShowed_ = false; 438 bool hasTitle_ = false; 439 bool hasLunar_ = false; 440 bool hasButtons_ = false; 441 bool hasTriangle_ = false; 442 bool isDialog_ = true; 443 bool subsidiary_ = false; 444 bool masterHasLunar_ = false; 445 bool needVibrate_ = true; 446 bool isCreateDialogComponent_ = false; 447 bool hasBackgroundColor_ = false; 448 int32_t nodeId_ = -1; // default of dialog's node id. 449 // used for inspector node in PC preview 450 int32_t pickerId_ = -1; 451 452 RefPtr<PickerTheme> theme_; 453 RefPtr<StackElement> stack_; 454 455 RefPtr<TextComponent> title_ = AceType::MakeRefPtr<TextComponent>(""); 456 RefPtr<CheckboxComponent> lunar_; 457 std::vector<RefPtr<PickerColumnComponent>> columns_; 458 WeakPtr<AccessibilityManager> accessibilityManager_; 459 int32_t rootAccessibilityId_ = -1; 460 RefPtr<AccessibilityNode> rootAccessibility_; 461 RefPtr<AccessibilityNode> titleAccessibility_; 462 RefPtr<AccessibilityNode> lunarAccessibility_; 463 RefPtr<AccessibilityNode> switchAccessibility_; 464 RefPtr<AccessibilityNode> cancelAccessibility_; 465 RefPtr<AccessibilityNode> okAccessibility_; 466 467 EventMarker onChange_; 468 EventMarker onColumnChange_; 469 EventMarker onCancel_; 470 EventMarker onOkClickId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker(); 471 EventMarker onCancelClickId_ = BackEndEventManager<void()>::GetInstance().GetAvailableMarker(); 472 EventMarker OnDialogAccept_; 473 EventMarker OnDialogCancel_; 474 EventMarker OnDialogChange_; 475 476 Dimension columnHeight_; 477 bool defaultHeight_ = false; 478 479 RefPtr<RotationController> rotationController_; 480 481 RefPtr<PickerAnimationController> animationController_; 482 RefPtr<DialogComponent> dialogComponent_; 483 std::string dialogInsprctorTag_; 484 }; 485 486 } // namespace OHOS::Ace 487 488 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PICKER_PICKER_BASE_COMPONENT_H 489