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 
104     void DumpInfo() override;
105 
106 private:
107     void OnAttachToFrameNode() override;
108     void OnModifyDone() override;
109     void Tick(uint64_t duration);
110     void InitTextTimerController();
111 
112     void InitTimerDisplay();
113     void UpdateTextTimer(uint32_t elapsedTime);
114     void FireChangeEvent();
115 
116     void HandleStart();
117     void HandlePause();
118     void HandleReset();
119     uint64_t GetFormatDuration(uint64_t duration) const;
120     uint64_t GetMillisecondsDuration(uint64_t duration) const;
121 
122     static void UpdateTextLayoutProperty(
123         RefPtr<TextTimerLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
124     std::string GetFormat() const;
125     bool GetIsCountDown() const;
126     double GetInputCount() const;
127     RefPtr<FrameNode> GetTextNode();
128     void RegisterVisibleAreaChangeCallback();
129     void OnVisibleAreaChange(bool visible);
130     void FireBuilder();
131     RefPtr<FrameNode> BuildContentModifierNode();
132 
133     std::optional<TextTimerMakeCallback> makeFunc_;
134     RefPtr<FrameNode> contentModifierNode_;
135     RefPtr<TextTimerController> textTimerController_;
136     WeakPtr<Referenced> jsTextTimerController_;
137     RefPtr<Scheduler> scheduler_;
138     RefPtr<FrameNode> textNode_;
139     uint64_t elapsedTime_ = 0; // millisecond.
140     uint64_t lastElapsedTime_ = 0;
141     bool isCountDown_ = false;
142     double inputCount_ = 0.0;
143     std::optional<int32_t> textId_;
144     bool isRegisteredAreaCallback_ = false;
145     bool resetCount_ = false;
146 
147     ACE_DISALLOW_COPY_AND_MOVE(TextTimerPattern);
148 };
149 } // namespace OHOS::Ace::NG
150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_TIMER_PATTERN_H
151