• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         CalendarSettingData settingData = data;
84         settingData.selectedDate = PickerDate::AdjustDateToRange(data.selectedDate, data.startDate, data.endDate);
85         calendarData_ = settingData;
86     }
87 
GetCalendarData()88     CalendarSettingData GetCalendarData() const
89     {
90         return calendarData_;
91     }
92 
93     void HandleClickEvent(const Offset& globalLocation);
94     void HandleTextHoverEvent(bool state, int32_t index);
95     void HandleAddButtonClick();
96     void HandleSubButtonClick();
97     bool HandleNumberKeyEvent(const KeyEvent& event);
98 
99     bool HandleKeyEvent(const KeyEvent& event);
100     bool HandleFocusEvent(const KeyEvent& event);
101     bool HandleBlurEvent(const KeyEvent& event);
102 
103     void OnValueChange();
IsDialogShow()104     bool IsDialogShow() const
105     {
106         return isDialogShow_;
107     }
SetDialogShow(bool flag)108     void SetDialogShow(bool flag)
109     {
110         isDialogShow_ = flag;
111     }
112 
113     CalendarPickerSelectedType CheckRegion(const Offset& globalLocation);
GetSelectedType()114     CalendarPickerSelectedType GetSelectedType() const
115     {
116         return selected_;
117     }
118     void SetSelectedType(CalendarPickerSelectedType type);
119 
HasSubNode()120     bool HasSubNode() const
121     {
122         return subId_.has_value();
123     }
GetSubId()124     int32_t GetSubId()
125     {
126         if (!subId_.has_value()) {
127             subId_ = ElementRegister::GetInstance()->MakeUniqueId();
128         }
129         return subId_.value();
130     }
131 
HasContentNode()132     bool HasContentNode() const
133     {
134         return contentId_.has_value();
135     }
GetContentId()136     int32_t GetContentId()
137     {
138         if (!contentId_.has_value()) {
139             contentId_ = ElementRegister::GetInstance()->MakeUniqueId();
140         }
141         return contentId_.value();
142     }
143 
HasAddNode()144     bool HasAddNode() const
145     {
146         return addId_.has_value();
147     }
GetAddId()148     int32_t GetAddId()
149     {
150         if (!addId_.has_value()) {
151             addId_ = ElementRegister::GetInstance()->MakeUniqueId();
152         }
153         return addId_.value();
154     }
155 
HasButtonFlexNode()156     bool HasButtonFlexNode() const
157     {
158         return buttonFlexId_.has_value();
159     }
GetButtonFlexId()160     int32_t GetButtonFlexId()
161     {
162         if (!buttonFlexId_.has_value()) {
163             buttonFlexId_ = ElementRegister::GetInstance()->MakeUniqueId();
164         }
165         return buttonFlexId_.value();
166     }
167 
168     bool OnDirtyLayoutWrapperSwap(
169         const RefPtr<LayoutWrapper>& dirty, bool /* skipMeasure */, bool /* skipLayout */) override;
170 
171     OffsetF CalculateDialogOffset();
172     void HandleHoverEvent(bool state, const Offset& globalLocation);
173     void HandleTouchEvent(bool isPressed, const Offset& globalLocation);
174 
175     bool IsContainerModal();
176 
177     void SetDate(const std::string& info);
178     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
179     void SetMarkToday(bool isMarkToday);
180     bool GetMarkToday();
181     void SetDisabledDateRange(const std::vector<std::pair<PickerDate, PickerDate>>& disabledDateRange);
182     std::string GetDisabledDateRange();
183 
184 private:
185     void OnModifyDone() override;
186     void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override;
187     void OnColorConfigurationUpdate() override;
188     void InitDateIndex();
189     void InitClickEvent();
190     void InitOnKeyEvent();
191     void InitOnHoverEvent();
192     void ShowDialog();
193     void InitDialogProperties(DialogProperties& properties);
194     void PostTaskToUI(const std::function<void()>& task, const std::string& name);
195     void HandleTaskCallback();
196     void HandleZeroStartTaskCallback();
197     void HandleTextFocusEvent(int32_t index);
198     void HandleBlurEvent();
199     void HandleButtonHoverEvent(bool state, int32_t index);
200     void HandleButtonTouchEvent(bool isPressed, int32_t index);
201     void HandleEnable();
202     void ResetTextState();
203     void ResetTextStateByNode(const RefPtr<FrameNode>& textFrameNode);
204     void FlushTextStyle();
205     bool IsInNodeRegion(const RefPtr<FrameNode>& node, const PointF& point);
206     bool HandleYearKeyWaitingEvent(
207         const uint32_t number, const std::function<void()>& task, const std::function<void()>& zeroStartTask);
208     bool HandleYearKeyEvent(uint32_t number);
209     bool HandleMonthKeyEvent(uint32_t number);
210     bool HandleDayKeyEvent(uint32_t number);
211     void FireChangeEvents(const std::string& info);
212     void UpdateEntryButtonColor();
213     void UpdateEntryButtonBorderWidth();
214     void UpdateEdgeAlign();
215     void UpdateAccessibilityText();
216     void FlushAddAndSubButton();
217     bool IsAddOrSubButtonEnable(int32_t buttonIndex);
218     void PrevDateBySelectedType(PickerDate& date);
219     void NextDateBySelectedType(PickerDate& date);
220 
221     std::string GetEntryDateInfo();
222     bool ReportChangeEvent(const std::string& compName,
223         const std::string& eventName, const std::string& eventData);
224 
225     uint32_t yearEnterCount_ = 0;
226     uint32_t yearPrefixZeroCount_ = 0;
227     uint32_t monthPrefixZeroCount_ = 0;
228     uint32_t dayPrefixZeroCount_ = 0;
229 
230     int32_t yearIndex_ = 0;
231     int32_t monthIndex_ = 2;
232     int32_t dayIndex_ = 4;
233 
234     std::optional<int32_t> subId_;
235     std::optional<int32_t> contentId_;
236     std::optional<int32_t> addId_;
237     std::optional<int32_t> buttonFlexId_;
238     int32_t taskCount_ = 0;
239     CalendarEdgeAlign align_ = CalendarEdgeAlign::EDGE_ALIGN_END;
240     DimensionOffset offset_;
241     CalendarSettingData calendarData_;
242     bool isDialogShow_ = false;
243     bool isKeyWaiting_ = false;
244     bool isFirtFocus_ = true;
245     RefPtr<ClickEvent> clickListener_;
246     RefPtr<InputEvent> hoverListener_;
247     CalendarPickerSelectedType selected_ = CalendarPickerSelectedType::OTHER;
248     PickerDate reportedPickerDate_;
249     ACE_DISALLOW_COPY_AND_MOVE(CalendarPickerPattern);
250     bool isMarkToday_ = false;
251 };
252 } // namespace OHOS::Ace::NG
253 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_PICKER_PATTERN_H
254