• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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_CLOCK_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_CLOCK_PATTERN_H
18 
19 #include <functional>
20 #include <string>
21 
22 #include "base/i18n/time_format.h"
23 #include "base/memory/referenced.h"
24 #include "base/utils/noncopyable.h"
25 #include "core/components/text_clock/text_clock_controller.h"
26 #include "core/components_ng/pattern/pattern.h"
27 #include "core/components_ng/pattern/text/text_pattern.h"
28 #include "core/components_ng/pattern/text_clock/text_clock_accessibility_property.h"
29 #include "core/components_ng/pattern/text_clock/text_clock_event_hub.h"
30 #include "core/components_ng/pattern/text_clock/text_clock_layout_algorithm.h"
31 #include "core/components_ng/pattern/text_clock/text_clock_layout_property.h"
32 #include "core/components_ng/pattern/text_clock/text_clock_model_ng.h"
33 #include "core/event/time/time_change_listener.h"
34 
35 namespace OHOS::Ace::NG {
36 struct TextClockFormatElement {
37     char elementKey;
38     int32_t formatElementNum = 0;
39     std::string formatElement = "";
40 };
41 
42 using TimeCallback = std::function<void()>;
43 class TextClockPattern : public Pattern, public TimeChangeListener {
44     DECLARE_ACE_TYPE(TextClockPattern, Pattern);
45 
46 public:
47     TextClockPattern();
48     ~TextClockPattern() override = default;
49 
CreateLayoutProperty()50     RefPtr<LayoutProperty> CreateLayoutProperty() override
51     {
52         return MakeRefPtr<TextClockLayoutProperty>();
53     }
54 
CreateAccessibilityProperty()55     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
56     {
57         return MakeRefPtr<TextClockAccessibilityProperty>();
58     }
59 
CreateEventHub()60     RefPtr<EventHub> CreateEventHub() override
61     {
62         return MakeRefPtr<TextClockEventHub>();
63     }
64 
CreateLayoutAlgorithm()65     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
66     {
67         return MakeRefPtr<TextClockLayoutAlgorithm>();
68     }
69 
GetTextClockController()70     RefPtr<TextClockController> GetTextClockController() const
71     {
72         return textClockController_;
73     }
74 
SetJSTextClockController(const RefPtr<Referenced> & jsController)75     void SetJSTextClockController(const RefPtr<Referenced>& jsController)
76     {
77         jsTextClockController_ = jsController;
78     }
79 
GetJSTextClockController()80     RefPtr<Referenced> GetJSTextClockController()
81     {
82         return jsTextClockController_.Upgrade();
83     }
84 
GetTextId()85     int32_t GetTextId()
86     {
87         if (!textId_.has_value()) {
88             textId_ = ElementRegister::GetInstance()->MakeUniqueId();
89         }
90         return textId_.value();
91     }
92 
93     void OnVisibleChange(bool isVisible) override;
94 
95     void OnTimeChange() override;
96 
SetBuilderFunc(TextClockMakeCallback && makeFunc)97     void SetBuilderFunc(TextClockMakeCallback&& makeFunc)
98     {
99         if (makeFunc == nullptr) {
100             makeFunc_ = std::nullopt;
101             contentModifierNode_ = nullptr;
102             OnModifyDone();
103             return;
104         }
105         makeFunc_ = std::move(makeFunc);
106     }
107 
UseContentModifier()108     bool UseContentModifier() const
109     {
110         return contentModifierNode_ != nullptr;
111     }
112 
GetBuilderId()113     int32_t GetBuilderId()
114     {
115         return nodeId_;
116     }
117 
GetPrefixHour()118     ZeroPrefixType GetPrefixHour()
119     {
120         auto textClockLayoutProperty = GetLayoutProperty<TextClockLayoutProperty>();
121         CHECK_NULL_RETURN(textClockLayoutProperty, prefixHour_);
122         return textClockLayoutProperty->GetPrefixHourValue(ZeroPrefixType::AUTO);
123     }
124 
IsEnableMatchParent()125     bool IsEnableMatchParent() override
126     {
127         return true;
128     }
129 
130     bool OnThemeScopeUpdate(int32_t themeScopeId) override;
131 
132     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
133 
134     void OnColorModeChange(uint32_t colorMode) override;
135     void UpdateTextColor(const Color& color);
136     void UpdateFontSize(const CalcDimension& fontSize);
137     void UpdateFontFamily(const std::vector<std::string>& fontFamilies);
138     void UpdateTextClockColor(const Color& color, bool isFirstLoad = false);
139     void UpdateTextClockFontSize(const CalcDimension& fontSize);
140     void UpdateTextClockFontFamily(const std::vector<std::string>& fontFamilies);
141     void UpdateTextClockFormat(const std::string& format);
142 
143 private:
144     void OnModifyDone() override;
145     void OnAttachToFrameNode() override;
146     void OnDetachFromFrameNode(FrameNode* frameNode) override;
147     void OnAttachToMainTree() override;
148     void OnDetachFromMainTree() override;
OnAttachToFrameNodeMultiThread()149     void OnAttachToFrameNodeMultiThread() {}
OnDetachFromFrameNodeMultiThread(FrameNode * frameNode)150     void OnDetachFromFrameNodeMultiThread(FrameNode* frameNode) {}
151     void OnAttachToMainTreeMultiThread();
152     void OnDetachFromMainTreeMultiThread();
153     void OnLanguageConfigurationUpdate() override;
154     void DumpInfo() override;
DumpSimplifyInfo(std::shared_ptr<JsonValue> & json)155     void DumpSimplifyInfo(std::shared_ptr<JsonValue>& json) override {}
156     void OnColorConfigurationUpdate() override;
157     void InitTextClockController();
158 
159     void InitUpdateTimeTextCallBack();
160     void UpdateTimeText(bool isTimeChange = false);
161     void RequestUpdateForNextSecond();
162     void FireChangeEvent() const;
163     std::string GetCurrentFormatDateTime();
164     std::string ParseDateTime(const std::string& dateTimeValue, int32_t week, int32_t month, int32_t hour);
165     void RegistVisibleAreaChangeCallback();
166     void OnVisibleAreaChange(bool visible);
167     static void UpdateTextLayoutProperty(RefPtr<TextClockLayoutProperty>& layoutProperty,
168         RefPtr<TextLayoutProperty>& textLayoutProperty, const TextStyle& textStyleTheme);
169     void ParseInputFormat();
170     std::vector<std::string> ParseDateTimeValue(const std::string& strDateTimeValue);
171     void GetDateTimeIndex(const char& element, TextClockFormatElement& tempFormatElement);
172     static std::string GetAmPm(int32_t hour);
173     static std::string Abstract(const std::string& strSource, const bool& abstractItem);
174     static int32_t GetDigitNumber(const std::string& strSource);
175     static std::string GetWeek(const bool& isShortType, const int32_t& week);
176     static std::string GetMonth(int32_t month);
177     std::string SpliceDateTime(const std::vector<std::string>& curDateTime);
178     static std::string CheckDateTimeElement(const std::vector<std::string>& curDateTime, const char& element,
179         const int32_t& elementIndex, const bool& oneElement);
180     std::string AddZeroPrefix(const std::string& strTimeValue);
181     std::string RemoveZeroPrefix(const std::string& strTimeValue);
182     std::string GetFormat() const;
183     float GetHoursWest() const;
184     RefPtr<FrameNode> GetTextNode();
185     void FireBuilder();
186     RefPtr<FrameNode> BuildContentModifierNode();
187 
188     std::optional<TextClockMakeCallback> makeFunc_ = std::nullopt;
189     RefPtr<FrameNode> contentModifierNode_ = nullptr;
190     int32_t nodeId_ = -1;
191     RefPtr<TextClockController> textClockController_;
192     WeakPtr<Referenced> jsTextClockController_;
193     float hourWest_ = 0.0f;
194     long timeValue_ = 0.0f;
195     std::optional<int32_t> textId_;
196     bool isStart_ = true;
197     bool is24H_ = SystemProperties::Is24HourClock();
198     bool isSetVisible_ = true;
199     bool isInVisibleArea_ = true;
200     bool isForm_ = false;
201     std::string prevTime_;
202     std::map<int32_t, TextClockFormatElement> formatElementMap_;
203     CancelableCallback<void()> delayTask_;
204     ZeroPrefixType prefixHour_ = ZeroPrefixType::AUTO;
205 
206     ACE_DISALLOW_COPY_AND_MOVE(TextClockPattern);
207 };
208 } // namespace OHOS::Ace::NG
209 
210 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_TEXT_CLOCK_PATTERN_H
211