• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/texttimer/text_timer_pattern.h"
17 
18 #include <stack>
19 #include <string>
20 
21 #include "base/i18n/localization.h"
22 #include "base/utils/utils.h"
23 #include "core/components/common/layout/constants.h"
24 #include "core/components_ng/pattern/text/text_layout_property.h"
25 #include "core/components_ng/pattern/text/text_pattern.h"
26 #include "core/components_ng/pattern/texttimer/text_timer_layout_property.h"
27 #include "core/components_ng/property/property.h"
28 #include "core/pipeline_ng/pipeline_context.h"
29 
30 namespace OHOS::Ace::NG {
31 namespace {
32 constexpr int32_t TOTAL_MINUTE_OF_HOUR = 60;
33 constexpr int32_t TOTAL_SECONDS_OF_HOUR = 60 * 60;
34 constexpr int32_t SECONDS_OF_MILLISECOND = 1000;
35 constexpr double DEFAULT_COUNT = 60000.0;
36 const std::string DEFAULT_FORMAT = "HH:mm:ss.SS";
37 } // namespace
38 
TextTimerPattern()39 TextTimerPattern::TextTimerPattern()
40 {
41     textTimerController_ = MakeRefPtr<TextTimerController>();
42 }
43 
FireChangeEvent()44 void TextTimerPattern::FireChangeEvent()
45 {
46     auto textTimerEventHub = GetEventHub<TextTimerEventHub>();
47     CHECK_NULL_VOID(textTimerEventHub);
48     auto utcTime = GetFormatDuration(GetMilliseconds());
49     auto elapsedTime = GetFormatDuration(elapsedTime_);
50     if (elapsedTime - lastElapsedTime_ >= 1) {
51         textTimerEventHub->FireChangeEvent(utcTime, elapsedTime);
52         lastElapsedTime_ = elapsedTime;
53     }
54 }
55 
InitTextTimerController()56 void TextTimerPattern::InitTextTimerController()
57 {
58     if (textTimerController_) {
59         if (textTimerController_->HasInitialized()) {
60             return;
61         }
62         auto weak = AceType::WeakClaim(this);
63         textTimerController_->OnStart([weak]() {
64             auto timerRender = weak.Upgrade();
65             if (timerRender) {
66                 timerRender->HandleStart();
67             }
68         });
69         textTimerController_->OnPause([weak]() {
70             auto timerRender = weak.Upgrade();
71             if (timerRender) {
72                 timerRender->HandlePause();
73             }
74         });
75         textTimerController_->OnReset([weak]() {
76             auto timerRender = weak.Upgrade();
77             if (timerRender) {
78                 timerRender->HandleReset();
79             }
80         });
81     }
82 }
83 
InitTimerDisplay()84 void TextTimerPattern::InitTimerDisplay()
85 {
86     auto host = GetHost();
87     CHECK_NULL_VOID(host);
88     if (!scheduler_) {
89         resetCount_ = false;
90         auto weak = AceType::WeakClaim(this);
91         auto&& callback = [weak](uint64_t duration) {
92             auto timer = weak.Upgrade();
93             if (timer) {
94                 timer->Tick(duration);
95             }
96         };
97         auto context = PipelineContext::GetCurrentContext();
98         CHECK_NULL_VOID(context);
99         scheduler_ = SchedulerBuilder::Build(callback, context);
100         auto count = isCountDown_ ? inputCount_ : 0;
101         UpdateTextTimer(static_cast<uint32_t>(count));
102         return;
103     }
104     if (resetCount_) {
105         resetCount_ = false;
106         HandleReset();
107     }
108 }
109 
Tick(uint64_t duration)110 void TextTimerPattern::Tick(uint64_t duration)
111 {
112     elapsedTime_ += duration;
113     FireChangeEvent();
114 
115     auto tmpValue = static_cast<double>(elapsedTime_);
116     if (isCountDown_) {
117         auto elapsedTime = GetMillisecondsDuration(GetFormatDuration(elapsedTime_));
118         tmpValue =
119             (inputCount_ >= static_cast<double>(elapsedTime_)) ? (inputCount_ - static_cast<double>(elapsedTime)) : 0;
120     }
121     if (isCountDown_ && tmpValue <= 0) {
122         UpdateTextTimer(0);
123         HandlePause();
124         return;
125     }
126 
127     UpdateTextTimer(static_cast<uint32_t>(tmpValue));
128 }
129 
UpdateTextLayoutProperty(RefPtr<TextTimerLayoutProperty> & layoutProperty,RefPtr<TextLayoutProperty> & textLayoutProperty)130 void TextTimerPattern::UpdateTextLayoutProperty(
131     RefPtr<TextTimerLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty)
132 {
133     if (layoutProperty->GetFontSize().has_value()) {
134         textLayoutProperty->UpdateFontSize(layoutProperty->GetFontSize().value());
135     }
136     if (layoutProperty->GetFontWeight().has_value()) {
137         textLayoutProperty->UpdateFontWeight(layoutProperty->GetFontWeight().value());
138     }
139     if (layoutProperty->GetTextColor().has_value()) {
140         textLayoutProperty->UpdateTextColor(layoutProperty->GetTextColor().value());
141     }
142     if (layoutProperty->GetFontFamily().has_value()) {
143         textLayoutProperty->UpdateFontFamily(layoutProperty->GetFontFamily().value());
144     }
145     if (layoutProperty->GetItalicFontStyle().has_value()) {
146         textLayoutProperty->UpdateItalicFontStyle(layoutProperty->GetItalicFontStyle().value());
147     }
148     if (layoutProperty->GetTextShadow().has_value()) {
149         textLayoutProperty->UpdateTextShadow(layoutProperty->GetTextShadow().value());
150     }
151 }
152 
OnAttachToFrameNode()153 void TextTimerPattern::OnAttachToFrameNode()
154 {
155     auto host = GetHost();
156     CHECK_NULL_VOID(host);
157     auto textTimerProperty = host->GetLayoutProperty<TextTimerLayoutProperty>();
158     CHECK_NULL_VOID(textTimerProperty);
159     textTimerProperty->UpdateAlignment(Alignment::CENTER_LEFT);
160 }
161 
OnModifyDone()162 void TextTimerPattern::OnModifyDone()
163 {
164     auto host = GetHost();
165     CHECK_NULL_VOID(host);
166 
167     if (!textNode_) {
168         textNode_ = GetTextNode();
169     }
170     CHECK_NULL_VOID(textNode_);
171     auto textLayoutProperty = textNode_->GetLayoutProperty<TextLayoutProperty>();
172     CHECK_NULL_VOID(textLayoutProperty);
173     textLayoutProperty->UpdateTextOverflow(TextOverflow::NONE);
174     if (textLayoutProperty->GetPositionProperty()) {
175         textLayoutProperty->UpdateAlignment(
176             textLayoutProperty->GetPositionProperty()->GetAlignment().value_or(Alignment::CENTER));
177     } else {
178         textLayoutProperty->UpdateAlignment(Alignment::CENTER);
179     }
180     auto textTimerProperty = host->GetLayoutProperty<TextTimerLayoutProperty>();
181     CHECK_NULL_VOID(textTimerProperty);
182     textLayoutProperty->UpdateTextOverflow(TextOverflow::NONE);
183     UpdateTextLayoutProperty(textTimerProperty, textLayoutProperty);
184     auto textContext = textNode_->GetRenderContext();
185     CHECK_NULL_VOID(textContext);
186     textContext->SetClipToFrame(false);
187     textContext->UpdateClipEdge(false);
188     isCountDown_ = GetIsCountDown();
189     inputCount_ = GetInputCount();
190 
191     InitTextTimerController();
192     InitTimerDisplay();
193     textNode_->MarkModifyDone();
194     RegisterVisibleAreaChangeCallback();
195 }
196 
RegisterVisibleAreaChangeCallback()197 void TextTimerPattern::RegisterVisibleAreaChangeCallback()
198 {
199     if (isRegisteredAreaCallback_) {
200         return;
201     }
202     isRegisteredAreaCallback_ = true;
203     auto host = GetHost();
204     CHECK_NULL_VOID(host);
205     auto pipeline = PipelineContext::GetCurrentContext();
206     CHECK_NULL_VOID(pipeline);
207     auto callback = [weak = WeakClaim(this)](bool visible, double ratio) {
208         auto pattern = weak.Upgrade();
209         CHECK_NULL_VOID(pattern);
210         pattern->OnVisibleAreaChange(visible);
211     };
212     pipeline->AddVisibleAreaChangeNode(host, 0.0f, callback, false);
213 }
214 
OnVisibleAreaChange(bool visible)215 void TextTimerPattern::OnVisibleAreaChange(bool visible)
216 {
217     auto host = GetHost();
218     CHECK_NULL_VOID(host);
219     CHECK_NULL_VOID(textNode_);
220     if (visible) {
221         auto childNode = DynamicCast<FrameNode>(host->GetFirstChild());
222         if (!childNode) {
223             host->AddChild(textNode_);
224             host->RebuildRenderContextTree();
225         }
226     } else {
227         host->RemoveChild(textNode_);
228         host->RebuildRenderContextTree();
229     }
230 }
231 
UpdateTextTimer(uint32_t elapsedTime)232 void TextTimerPattern::UpdateTextTimer(uint32_t elapsedTime)
233 {
234     auto host = GetHost();
235     CHECK_NULL_VOID(host);
236     CHECK_NULL_VOID(textNode_);
237     auto textLayoutProperty = textNode_->GetLayoutProperty<TextLayoutProperty>();
238     CHECK_NULL_VOID(textLayoutProperty);
239 
240     // format time text.
241     std::string timerText = Localization::GetInstance()->FormatDuration(elapsedTime, GetFormat());
242     if (timerText.empty()) {
243         timerText = Localization::GetInstance()->FormatDuration(elapsedTime, DEFAULT_FORMAT);
244     }
245     textLayoutProperty->UpdateContent(timerText); // Update time text.
246     if (CheckMeasureFlag(textLayoutProperty->GetPropertyChangeFlag()) ||
247         CheckLayoutFlag(textLayoutProperty->GetPropertyChangeFlag())) {
248         textNode_->MarkModifyDone();
249         textNode_->MarkDirtyNode();
250     }
251 }
252 
GetFormat() const253 std::string TextTimerPattern::GetFormat() const
254 {
255     auto textTimerLayoutProperty = GetLayoutProperty<TextTimerLayoutProperty>();
256     CHECK_NULL_RETURN(textTimerLayoutProperty, DEFAULT_FORMAT);
257     return textTimerLayoutProperty->GetFormat().value_or(DEFAULT_FORMAT);
258 }
259 
GetIsCountDown() const260 bool TextTimerPattern::GetIsCountDown() const
261 {
262     auto textTimerLayoutProperty = GetLayoutProperty<TextTimerLayoutProperty>();
263     CHECK_NULL_RETURN(textTimerLayoutProperty, false);
264     return textTimerLayoutProperty->GetIsCountDown().value_or(false);
265 }
266 
GetInputCount() const267 double TextTimerPattern::GetInputCount() const
268 {
269     auto textTimerLayoutProperty = GetLayoutProperty<TextTimerLayoutProperty>();
270     CHECK_NULL_RETURN(textTimerLayoutProperty, DEFAULT_COUNT);
271     return textTimerLayoutProperty->GetInputCount().value_or(DEFAULT_COUNT);
272 }
273 
HandleStart()274 void TextTimerPattern::HandleStart()
275 {
276     if (scheduler_ && !scheduler_->IsActive()) {
277         scheduler_->Start();
278     }
279 }
280 
HandlePause()281 void TextTimerPattern::HandlePause()
282 {
283     if (scheduler_ && scheduler_->IsActive()) {
284         scheduler_->Stop();
285     }
286 }
287 
HandleReset()288 void TextTimerPattern::HandleReset()
289 {
290     if (scheduler_ && scheduler_->IsActive()) {
291         scheduler_->Stop();
292     }
293     elapsedTime_ = 0;
294     lastElapsedTime_ = 0;
295     auto count = isCountDown_ ? inputCount_ : 0;
296     UpdateTextTimer(static_cast<uint32_t>(count));
297 }
298 
GetTextNode()299 RefPtr<FrameNode> TextTimerPattern::GetTextNode()
300 {
301     auto host = GetHost();
302     CHECK_NULL_RETURN(host, nullptr);
303     auto textNode = AceType::DynamicCast<FrameNode>(host->GetLastChild());
304     CHECK_NULL_RETURN(textNode, nullptr);
305     if (textNode->GetTag() != V2::TEXT_ETS_TAG) {
306         return nullptr;
307     }
308     return textNode;
309 }
310 
GetFormatDuration(uint64_t duration) const311 uint64_t TextTimerPattern::GetFormatDuration(uint64_t duration) const
312 {
313     auto host = GetHost();
314     CHECK_NULL_RETURN(host, duration);
315     auto layoutProperty = host->GetLayoutProperty<TextTimerLayoutProperty>();
316     CHECK_NULL_RETURN(layoutProperty, duration);
317     auto format = layoutProperty->GetFormat().value_or(DEFAULT_FORMAT);
318     char lastWord = format.back();
319     switch (lastWord) {
320         case 's':
321             duration = duration / SECONDS_OF_MILLISECOND;
322             break;
323         case 'm':
324             duration = duration / (SECONDS_OF_MILLISECOND * TOTAL_MINUTE_OF_HOUR);
325             break;
326         case 'h':
327             duration = duration / (SECONDS_OF_MILLISECOND * TOTAL_SECONDS_OF_HOUR);
328             break;
329         default:
330             break;
331     }
332     return duration;
333 }
334 
GetMillisecondsDuration(uint64_t duration) const335 uint64_t TextTimerPattern::GetMillisecondsDuration(uint64_t duration) const
336 {
337     auto host = GetHost();
338     CHECK_NULL_RETURN(host, duration);
339     auto layoutProperty = host->GetLayoutProperty<TextTimerLayoutProperty>();
340     CHECK_NULL_RETURN(layoutProperty, duration);
341     auto format = layoutProperty->GetFormat().value_or(DEFAULT_FORMAT);
342     char lastWord = format.back();
343     switch (lastWord) {
344         case 's':
345             duration = duration * SECONDS_OF_MILLISECOND;
346             break;
347         case 'm':
348             duration = duration * (SECONDS_OF_MILLISECOND * TOTAL_MINUTE_OF_HOUR);
349             break;
350         case 'h':
351             duration = duration * (SECONDS_OF_MILLISECOND * TOTAL_SECONDS_OF_HOUR);
352             break;
353         default:
354             break;
355     }
356     return duration;
357 }
358 
ResetCount()359 void TextTimerPattern::ResetCount()
360 {
361     resetCount_ = true;
362 }
363 } // namespace OHOS::Ace::NG
364