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_DIALOG_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_DIALOG_PATTERN_H
18 
19 #include <optional>
20 
21 #include "core/components/calendar/calendar_data_adapter.h"
22 #include "core/components/picker/picker_theme.h"
23 #include "core/components_ng/pattern/calendar/calendar_pattern.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 #include "core/components_ng/pattern/swiper/swiper_pattern.h"
28 
29 namespace OHOS::Ace::NG {
30 class CalendarDialogPattern : public LinearLayoutPattern {
31     DECLARE_ACE_TYPE(CalendarDialogPattern, LinearLayoutPattern);
32 
33 public:
CalendarDialogPattern()34     CalendarDialogPattern() : LinearLayoutPattern(true) {};
35     ~CalendarDialogPattern() override = default;
36 
IsAtomicNode()37     bool IsAtomicNode() const override
38     {
39         return true;
40     }
41 
SetEntryNode(const WeakPtr<FrameNode> & node)42     void SetEntryNode(const WeakPtr<FrameNode>& node)
43     {
44         entryNode_ = node;
45     }
46 
GetDialogOffset()47     const OffsetF GetDialogOffset() const
48     {
49         return dialogOffset_;
50     }
51 
SetDialogOffset(const OffsetF & offset)52     void SetDialogOffset(const OffsetF& offset)
53     {
54         dialogOffset_ = offset;
55     }
56 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)57     bool OnDirtyLayoutWrapperSwap(
58         const RefPtr<LayoutWrapper>& dirty, bool /* skipMeasure */, bool /* skipLayout */) override
59     {
60         if (isFirstAddhotZoneRect_) {
61             AddHotZoneRect();
62             isFirstAddhotZoneRect_ = false;
63         }
64         return true;
65     }
66 
67     void HandleClickEvent(const GestureEvent& info);
68 
GetFocusPattern()69     FocusPattern GetFocusPattern() const override
70     {
71         auto pipeline = PipelineBase::GetCurrentContext();
72         CHECK_NULL_RETURN(pipeline, FocusPattern());
73         auto pickerTheme = pipeline->GetTheme<PickerTheme>();
74         RefPtr<CalendarTheme> calendarTheme = pipeline->GetTheme<CalendarTheme>();
75         CHECK_NULL_RETURN(pickerTheme, FocusPattern());
76         CHECK_NULL_RETURN(calendarTheme, FocusPattern());
77         auto focusColor = pickerTheme->GetFocusColor();
78         FocusPaintParam focusPaintParams;
79         focusPaintParams.SetPaintColor(focusColor);
80         auto focusPaintWidth = calendarTheme->GetCalendarDayKeyFocusedPenWidth();
81         focusPaintParams.SetPaintWidth(focusPaintWidth);
82         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams };
83     }
84 
SetHoverState(bool state)85     void SetHoverState(bool state)
86     {
87         hoverState_ = state;
88     }
89 
GetHoverState()90     bool GetHoverState() const
91     {
92         return hoverState_;
93     }
94 
95     void GetCalendarMonthData(int32_t year, int32_t month, ObtainedMonth& calendarMonthData);
96     CalendarMonth GetNextMonth(const CalendarMonth& calendarMonth);
97     CalendarMonth GetLastMonth(const CalendarMonth& calendarMonth);
98     void SetOptionsButtonUpdateColorFlags(size_t index, bool isUpdate);
99 
SetCurrentButtonInfo(const std::vector<ButtonInfo> & buttonInfos)100     void SetCurrentButtonInfo(const std::vector<ButtonInfo>& buttonInfos)
101     {
102         currentButtonInfos_ = buttonInfos;
103     }
104 
SetCurrentSettingData(const CalendarSettingData & settingData)105     void SetCurrentSettingData(const CalendarSettingData& settingData)
106     {
107         currentSettingData_ = settingData;
108     }
109 
110     void OnLanguageConfigurationUpdate() override;
111 
112     void UpdateCaretInfoToController();
113 
HasSurfaceChangedCallback()114     bool HasSurfaceChangedCallback()
115     {
116         return surfaceChangedCallbackId_.has_value();
117     }
118 
UpdateSurfaceChangedCallbackId(int32_t id)119     void UpdateSurfaceChangedCallbackId(int32_t id)
120     {
121         surfaceChangedCallbackId_ = id;
122     }
123 
124     void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight);
125 
126     void InitSurfaceChangedCallback();
127 
128 private:
129     void OnModifyDone() override;
130     void InitClickEvent();
131     void InitOnKeyEvent();
132     void InitOnTouchEvent();
133     void InitTitleArrowsEvent();
134     void InitEntryChangeEvent();
135     void InitHoverEvent();
136     void AddHotZoneRect();
137     bool HandleKeyEvent(const KeyEvent& event);
138     void GetInnerFocusPaintRect(RoundRect& paintRect);
139     void PaintFocusState();
140     void HandleTitleArrowsClickEvent(int32_t nodeIndex);
141     void HandleEntryChange(const std::string& info);
142     void HandleEntryLayoutChange();
143     void ClearCalendarFocusedState();
144     bool HandleCalendarNodeKeyEvent(const KeyEvent& event);
145     bool ActClick(int32_t focusAreaID, int32_t focusAreaChildID);
146     void PaintCurrentMonthFocusState();
147     void PaintNonCurrentMonthFocusState(int32_t focusedDayIndex);
148     void ChangeEntryState();
149     void FocusedLastFocusedDay();
150     int32_t GetIndexByFocusedDay();
151     bool HandleTabKeyEvent(const KeyEvent& event);
152     void FireChangeByKeyEvent(PickerDate& selectedDay);
153     bool IsIndexInCurrentMonth(int32_t focusedDayIndex, const ObtainedMonth& currentMonthData);
154     bool IsInEntryRegion(const Offset& globalLocation);
155     void HandleEntryNodeHoverEvent(bool state, const Offset& globalLocation);
156     void HandleEntryNodeTouchEvent(bool isPressed, const Offset& globalLocation);
157     void UpdateDialogBackgroundColor();
158     void UpdateTitleArrowsColor();
159     void UpdateOptionsButtonColor();
160     void OnEnterKeyEvent(const KeyEvent& event);
161     void UpdateTitleArrowsImage();
162     void UpdateImage(const RefPtr<FrameNode>& buttonNode, const InternalResource::ResourceId& resourceId);
163     void UpdateOptionsButton();
164     void UpdateCalendarLayout();
165     void UpdateNonCurrentMonthFocusedDay(int32_t focusedDayIndex);
166     void PaintMonthFocusState(int32_t focusedDayIndex);
167 
168     RefPtr<FrameNode> GetCalendarFrameNode();
169     RefPtr<CalendarPattern> GetCalendarPattern();
170     RefPtr<FrameNode> GetSwiperFrameNode();
171     RefPtr<SwiperPattern> GetSwiperPattern();
172 
173     void UpdateSwiperNode(const ObtainedMonth& monthData, bool isPrev);
174     void UpdateSwiperNodeFocusedDay(const CalendarDay& focusedDay, bool isPrev);
175 
176     int32_t focusAreaID_ = 0;
177     int32_t focusAreaChildID_ = 0;
178     CalendarDay focusedDay_ = { .day = -1 };
179     bool isFirstAddhotZoneRect_ = true;
180     bool isFocused_ = false;
181     bool isCalendarFirstFocused_ = false;
182     bool hoverState_ = false;
183     std::vector<bool> updateColorFlags = {true, true, true, true};
184     OffsetF dialogOffset_;
185     WeakPtr<FrameNode> entryNode_ = nullptr;
186     std::map<int32_t, RefPtr<ClickEvent>> clickEvents_;
187     RefPtr<TouchEventImpl> touchListener_ = nullptr;
188     RefPtr<InputEvent> hoverListener_ = nullptr;
189     ACE_DISALLOW_COPY_AND_MOVE(CalendarDialogPattern);
190     bool hasTabKeyDown_ = false;
191     std::vector<ButtonInfo> currentButtonInfos_;
192     CalendarSettingData currentSettingData_;
193     std::optional<int32_t> surfaceChangedCallbackId_;
194 };
195 } // namespace OHOS::Ace::NG
196 
197 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_DIALOG_PATTERN_H
198