1 /*
2  * Copyright (c) 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_CALENDAR_PICKER_CALENDAR_PICKER_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_PICKER_PATTERN_H
18 
19 #include <optional>
20 
21 #include "core/components_ng/pattern/calendar_picker/calendar_picker_event_hub.h"
22 #include "core/components_ng/pattern/calendar_picker/calendar_picker_layout_algorithm.h"
23 #include "core/components_ng/pattern/calendar_picker/calendar_picker_layout_property.h"
24 #include "core/components_ng/pattern/calendar_picker/calendar_type_define.h"
25 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h"
26 #include "core/components_ng/pattern/pattern.h"
27 
28 namespace OHOS::Ace::NG {
29 class CalendarPickerPattern : public LinearLayoutPattern {
30     DECLARE_ACE_TYPE(CalendarPickerPattern, LinearLayoutPattern);
31 
32 public:
CalendarPickerPattern()33     CalendarPickerPattern() : LinearLayoutPattern(false) {};
34     ~CalendarPickerPattern() override = default;
35 
IsAtomicNode()36     bool IsAtomicNode() const override
37     {
38         return true;
39     }
40 
CreateEventHub()41     RefPtr<EventHub> CreateEventHub() override
42     {
43         return MakeRefPtr<CalendarPickerEventHub>();
44     }
45 
CreateLayoutProperty()46     RefPtr<LayoutProperty> CreateLayoutProperty() override
47     {
48         return MakeRefPtr<CalendarPickerLayoutProperty>();
49     }
50 
GetFocusPattern()51     FocusPattern GetFocusPattern() const override
52     {
53         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION };
54     }
55 
CreateLayoutAlgorithm()56     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
57     {
58         return MakeRefPtr<CalendarPickerLayoutAlgorithm>();
59     }
60 
SetCalendarEdgeAlign(CalendarEdgeAlign align)61     void SetCalendarEdgeAlign(CalendarEdgeAlign align)
62     {
63         align_ = align;
64     }
65 
GetCalendarEdgeAlign()66     CalendarEdgeAlign GetCalendarEdgeAlign() const
67     {
68         return align_;
69     }
70 
SetCalendarDialogOffset(const DimensionOffset & offset)71     void SetCalendarDialogOffset(const DimensionOffset& offset)
72     {
73         offset_ = offset;
74     }
75 
GetCalendarDialogOffset()76     DimensionOffset GetCalendarDialogOffset() const
77     {
78         return offset_;
79     }
80 
SetCalendarData(const CalendarSettingData & data)81     void SetCalendarData(const CalendarSettingData& data)
82     {
83         calendarData_ = data;
84     }
85 
GetCalendarData()86     CalendarSettingData GetCalendarData() const
87     {
88         return calendarData_;
89     }
90 
91     void HandleClickEvent(const Offset& globalLocation);
92     void HandleTextHoverEvent(bool state, int32_t index);
93     void HandleAddButtonClick();
94     void HandleSubButtonClick();
95     bool HandleNumberKeyEvent(const KeyEvent& event);
96 
97     bool HandleKeyEvent(const KeyEvent& event);
98     bool HandleFocusEvent(const KeyEvent& event);
99     bool HandleBlurEvent(const KeyEvent& event);
100 
101     void OnValueChange();
IsDialogShow()102     bool IsDialogShow() const
103     {
104         return isDialogShow_;
105     }
SetDialogShow(bool flag)106     void SetDialogShow(bool flag)
107     {
108         isDialogShow_ = flag;
109     }
110 
111     CalendarPickerSelectedType CheckRegion(const Offset& globalLocation);
GetSelectedType()112     CalendarPickerSelectedType GetSelectedType() const
113     {
114         return selected_;
115     }
116     void SetSelectedType(CalendarPickerSelectedType type);
117 
HasSubNode()118     bool HasSubNode() const
119     {
120         return subId_.has_value();
121     }
GetSubId()122     int32_t GetSubId()
123     {
124         if (!subId_.has_value()) {
125             subId_ = ElementRegister::GetInstance()->MakeUniqueId();
126         }
127         return subId_.value();
128     }
129 
HasContentNode()130     bool HasContentNode() const
131     {
132         return contentId_.has_value();
133     }
GetContentId()134     int32_t GetContentId()
135     {
136         if (!contentId_.has_value()) {
137             contentId_ = ElementRegister::GetInstance()->MakeUniqueId();
138         }
139         return contentId_.value();
140     }
141 
HasAddNode()142     bool HasAddNode() const
143     {
144         return addId_.has_value();
145     }
GetAddId()146     int32_t GetAddId()
147     {
148         if (!addId_.has_value()) {
149             addId_ = ElementRegister::GetInstance()->MakeUniqueId();
150         }
151         return addId_.value();
152     }
153 
HasButtonFlexNode()154     bool HasButtonFlexNode() const
155     {
156         return buttonFlexId_.has_value();
157     }
GetButtonFlexId()158     int32_t GetButtonFlexId()
159     {
160         if (!buttonFlexId_.has_value()) {
161             buttonFlexId_ = ElementRegister::GetInstance()->MakeUniqueId();
162         }
163         return buttonFlexId_.value();
164     }
165 
166     bool OnDirtyLayoutWrapperSwap(
167         const RefPtr<LayoutWrapper>& dirty, bool /* skipMeasure */, bool /* skipLayout */) override;
168 
169     OffsetF CalculateDialogOffset();
170     void HandleHoverEvent(bool state, const Offset& globalLocation);
171     void HandleTouchEvent(bool isPressed, const Offset& globalLocation);
172 
173     bool IsContainerModal();
174 
175     void SetDate(const std::string& info);
176 private:
177     void OnModifyDone() override;
178     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
179     void InitDateIndex();
180     void InitClickEvent();
181     void InitOnKeyEvent();
182     void InitOnHoverEvent();
183     void ShowDialog();
184     void InitDialogProperties(DialogProperties& properties);
185     void PostTaskToUI(const std::function<void()>& task, const std::string& name);
186     void HandleTaskCallback();
187     void HandleZeroStartTaskCallback();
188     void HandleTextFocusEvent(int32_t index);
189     void HandleBlurEvent();
190     void HandleButtonHoverEvent(bool state, int32_t index);
191     void HandleButtonTouchEvent(bool isPressed, int32_t index);
192     void ResetTextState();
193     void ResetTextStateByNode(const RefPtr<FrameNode>& textFrameNode);
194     void FlushTextStyle();
195     bool IsInNodeRegion(const RefPtr<FrameNode>& node, const PointF& point);
196     bool HandleYearKeyWaitingEvent(
197         const uint32_t number, const std::function<void()>& task, const std::function<void()>& zeroStartTask);
198     bool HandleYearKeyEvent(uint32_t number);
199     bool HandleMonthKeyEvent(uint32_t number);
200     bool HandleDayKeyEvent(uint32_t number);
201     void FireChangeEvents(const std::string& info);
202     void UpdateEntryButtonColor();
203     void UpdateEntryButtonBorderWidth();
204     void UpdateEdgeAlign();
205     void UpdateAccessibilityText();
206 
207     std::string GetEntryDateInfo();
208 
209     uint32_t yearEnterCount_ = 0;
210     uint32_t yearPrefixZeroCount_ = 0;
211     uint32_t monthPrefixZeroCount_ = 0;
212     uint32_t dayPrefixZeroCount_ = 0;
213 
214     int32_t yearIndex_ = 0;
215     int32_t monthIndex_ = 2;
216     int32_t dayIndex_ = 4;
217 
218     std::optional<int32_t> subId_;
219     std::optional<int32_t> contentId_;
220     std::optional<int32_t> addId_;
221     std::optional<int32_t> buttonFlexId_;
222     int32_t taskCount_ = 0;
223     CalendarEdgeAlign align_ = CalendarEdgeAlign::EDGE_ALIGN_END;
224     DimensionOffset offset_;
225     CalendarSettingData calendarData_;
226     bool isDialogShow_ = false;
227     bool isKeyWaiting_ = false;
228     bool isFirtFocus_ = true;
229     RefPtr<ClickEvent> clickListener_;
230     RefPtr<InputEvent> hoverListener_;
231     CalendarPickerSelectedType selected_ = CalendarPickerSelectedType::OTHER;
232     ACE_DISALLOW_COPY_AND_MOVE(CalendarPickerPattern);
233 };
234 } // namespace OHOS::Ace::NG
235 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_PICKER_PATTERN_H
236