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_TEXT_CLOCK_RENDER_TEXT_CLOCK_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_CLOCK_RENDER_TEXT_CLOCK_H
18 
19 #include <string>
20 #include "base/i18n/localization.h"
21 #include "base/utils/time_util.h"
22 #include "core/components/image/render_image.h"
23 #include "core/components/text/render_text.h"
24 #include "core/components/text/text_component.h"
25 #include "core/components/text_clock/text_clock_component.h"
26 #include "core/pipeline/base/render_node.h"
27 
28 namespace OHOS::Ace {
29 using TimeCallback = std::function<void()>;
30 
31 class RenderTextClock : public RenderNode {
32     DECLARE_ACE_TYPE(RenderTextClock, RenderNode);
33 
34 public:
35     ~RenderTextClock() override = default;
36 
37     static RefPtr<RenderNode> Create();
38     void Update(const RefPtr<Component>& component) override;
39     void PerformLayout() override;
40     std::string GetFormatDateTime(const std::string& format) const;
41     DateTime GetDateTime() const;
42     void UpdateTimeText();
43     std::function<void(const std::string)> onDateChange;
SetOnDateChange(const std::function<void (const std::string)> & value)44     void SetOnDateChange(const std::function<void(const std::string)>& value)
45     {
46         onDateChange = value;
47     }
GetTextClockComponent()48     RefPtr<TextClockComponent> GetTextClockComponent()
49     {
50         return textClockComponent_;
51     }
52     void UpdateTimeTextCallBack();
53 
GetHoursWest()54     int32_t GetHoursWest() const
55     {
56         return hoursWest_;
57     }
58 
GeFormat()59     const std::string& GeFormat() const
60     {
61         return format_;
62     }
63 
GetRenderText()64     const RefPtr<RenderText>& GetRenderText() const
65     {
66         return renderTimeText_;
67     }
68 
69 protected:
70     RenderTextClock();
71 
72     std::string format_ = DEFAULT_FORMAT;
73     int32_t hoursWest_ = INT_MAX;
74     TextStyle textStyle_;
75     std::string currentTimeText_;
76     bool isStart_ = true;
77     RefPtr<TextClockComponent> textClockComponent_;
78     RefPtr<TextComponent> timeTextComponent_;
79     RefPtr<RenderText> renderTimeText_;
80     TimeCallback timeCallback_;
81     uint64_t currentMillisecond_ = 0;
82 };
83 } // namespace OHOS::Ace
84 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_TEXT_CLOCK_RENDER_TEXT_CLOCK_H