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 SetJSTextTimerController(const RefPtr<Referenced> & jsController)69 void SetJSTextTimerController(const RefPtr<Referenced>& jsController) 70 { 71 jsTextTimerController_ = jsController; 72 } 73 GetJSTextTimerController()74 RefPtr<Referenced> GetJSTextTimerController() 75 { 76 return jsTextTimerController_.Upgrade(); 77 } 78 GetTextId()79 int32_t GetTextId() 80 { 81 if (!textId_.has_value()) { 82 textId_ = ElementRegister::GetInstance()->MakeUniqueId(); 83 } 84 return textId_.value(); 85 } 86 void ResetCount(); 87 SetBuilderFunc(TextTimerMakeCallback && makeFunc)88 void SetBuilderFunc(TextTimerMakeCallback&& makeFunc) 89 { 90 if (makeFunc == nullptr) { 91 makeFunc_ = std::nullopt; 92 contentModifierNode_ = nullptr; 93 OnModifyDone(); 94 return; 95 } 96 makeFunc_ = std::move(makeFunc); 97 } 98 UseContentModifier()99 bool UseContentModifier() const 100 { 101 return contentModifierNode_ != nullptr; 102 } 103 IsEnableMatchParent()104 bool IsEnableMatchParent() override 105 { 106 return true; 107 } 108 109 void DumpInfo() override; 110 void DumpInfo(std::unique_ptr<JsonValue>& json) override; DumpSimplifyInfo(std::shared_ptr<JsonValue> & json)111 void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override {} 112 void OnColorConfigurationUpdate() override; 113 114 void UpdateTextColor(const Color& color, bool isFirstLoad = false); 115 void UpdateFontWeight(const FontWeight& value, bool isFirstLoad = false); 116 void OnColorModeChange(uint32_t colorMode) override; 117 void UpdateFontSize(const Dimension& value, bool isFirstLoad = false); 118 void UpdateFontFamily(const std::vector<std::string>& fontFamilies, bool isFirstLoad); 119 120 private: 121 void OnAttachToFrameNode() override; 122 void OnModifyDone() override; 123 void Tick(uint64_t duration); 124 void InitTextTimerController(); 125 126 void InitTimerDisplay(); 127 void UpdateTextTimer(uint32_t elapsedTime); 128 void FireChangeEvent(); 129 130 void HandleStart(); 131 void HandlePause(); 132 void HandleReset(); 133 uint64_t GetFormatDuration(uint64_t duration) const; 134 uint64_t GetMillisecondsDuration(uint64_t duration) const; 135 136 static void UpdateTextLayoutProperty( 137 RefPtr<TextTimerLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty); 138 std::string GetFormat() const; 139 bool GetIsCountDown() const; 140 double GetInputCount() const; 141 RefPtr<FrameNode> GetTextNode(); 142 void RegisterVisibleAreaChangeCallback(); 143 void OnVisibleAreaChange(bool visible); 144 void FireBuilder(); 145 RefPtr<FrameNode> BuildContentModifierNode(); 146 147 std::optional<TextTimerMakeCallback> makeFunc_; 148 RefPtr<FrameNode> contentModifierNode_; 149 RefPtr<TextTimerController> textTimerController_; 150 WeakPtr<Referenced> jsTextTimerController_; 151 RefPtr<Scheduler> scheduler_; 152 RefPtr<FrameNode> textNode_; 153 uint64_t elapsedTime_ = 0; // millisecond. 154 uint64_t lastElapsedTime_ = 0; 155 bool isCountDown_ = false; 156 double inputCount_ = 0.0; 157 std::optional<int32_t> textId_; 158 bool isRegisteredAreaCallback_ = false; 159 bool resetCount_ = false; 160 161 ACE_DISALLOW_COPY_AND_MOVE(TextTimerPattern); 162 }; 163 } // namespace OHOS::Ace::NG 164 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TIMER_PATTERN_H 165