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_TEXT_TIMER_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TIMER_PATTERN_H 18 19 #include <string> 20 21 #include "base/geometry/dimension.h" 22 #include "base/memory/referenced.h" 23 #include "base/utils/noncopyable.h" 24 #include "core/components/texttimer/texttimer_controller.h" 25 #include "core/components_ng/pattern/pattern.h" 26 #include "core/components_ng/pattern/text/text_layout_property.h" 27 #include "core/components_ng/pattern/texttimer/text_timer_accessibility_property.h" 28 #include "core/components_ng/pattern/texttimer/text_timer_event_hub.h" 29 #include "core/components_ng/pattern/texttimer/text_timer_layout_algorithm.h" 30 #include "core/components_ng/pattern/texttimer/text_timer_layout_property.h" 31 #include "core/components_ng/pattern/texttimer/text_timer_model_ng.h" 32 #include "core/components_ng/property/property.h" 33 34 namespace OHOS::Ace::NG { 35 using TextTimerMakeCallback = 36 std::function<RefPtr<FrameNode>(const TextTimerConfiguration& textTimerConfiguration)>; 37 class TextTimerPattern : public Pattern { 38 DECLARE_ACE_TYPE(TextTimerPattern, Pattern); 39 40 public: 41 TextTimerPattern(); 42 ~TextTimerPattern() override = default; 43 CreateLayoutProperty()44 RefPtr<LayoutProperty> CreateLayoutProperty() override 45 { 46 return MakeRefPtr<TextTimerLayoutProperty>(); 47 } 48 CreateLayoutAlgorithm()49 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 50 { 51 return MakeRefPtr<TextTimerLayoutAlgorithm>(); 52 } 53 CreateAccessibilityProperty()54 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 55 { 56 return MakeRefPtr<TextTimerAccessibilityProperty>(); 57 } 58 CreateEventHub()59 RefPtr<EventHub> CreateEventHub() override 60 { 61 return MakeRefPtr<TextTimerEventHub>(); 62 } 63 GetTextTimerController()64 RefPtr<TextTimerController> GetTextTimerController() const 65 { 66 return textTimerController_; 67 } 68 GetTextId()69 int32_t GetTextId() 70 { 71 if (!textId_.has_value()) { 72 textId_ = ElementRegister::GetInstance()->MakeUniqueId(); 73 } 74 return textId_.value(); 75 } 76 void ResetCount(); 77 SetBuilderFunc(TextTimerMakeCallback && makeFunc)78 void SetBuilderFunc(TextTimerMakeCallback&& makeFunc) 79 { 80 if (makeFunc == nullptr) { 81 makeFunc_ = std::nullopt; 82 contentModifierNode_ = nullptr; 83 OnModifyDone(); 84 return; 85 } 86 makeFunc_ = std::move(makeFunc); 87 } 88 UseContentModifier()89 bool UseContentModifier() const 90 { 91 return contentModifierNode_ != nullptr; 92 } 93 94 void DumpInfo() override; 95 96 private: 97 void OnAttachToFrameNode() override; 98 void OnModifyDone() override; 99 void Tick(uint64_t duration); 100 void InitTextTimerController(); 101 102 void InitTimerDisplay(); 103 void UpdateTextTimer(uint32_t elapsedTime); 104 void FireChangeEvent(); 105 106 void HandleStart(); 107 void HandlePause(); 108 void HandleReset(); 109 uint64_t GetFormatDuration(uint64_t duration) const; 110 uint64_t GetMillisecondsDuration(uint64_t duration) const; 111 112 static void UpdateTextLayoutProperty( 113 RefPtr<TextTimerLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty); 114 std::string GetFormat() const; 115 bool GetIsCountDown() const; 116 double GetInputCount() const; 117 RefPtr<FrameNode> GetTextNode(); 118 void RegisterVisibleAreaChangeCallback(); 119 void OnVisibleAreaChange(bool visible); 120 void FireBuilder(); 121 RefPtr<FrameNode> BuildContentModifierNode(); 122 123 std::optional<TextTimerMakeCallback> makeFunc_; 124 RefPtr<FrameNode> contentModifierNode_; 125 RefPtr<TextTimerController> textTimerController_; 126 RefPtr<Scheduler> scheduler_; 127 RefPtr<FrameNode> textNode_; 128 uint64_t elapsedTime_ = 0; // millisecond. 129 uint64_t lastElapsedTime_ = 0; 130 bool isCountDown_ = false; 131 double inputCount_ = 0.0; 132 std::optional<int32_t> textId_; 133 bool isRegisteredAreaCallback_ = false; 134 bool resetCount_ = false; 135 136 ACE_DISALLOW_COPY_AND_MOVE(TextTimerPattern); 137 }; 138 } // namespace OHOS::Ace::NG 139 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TIMER_PATTERN_H 140