1 /* 2 * Copyright (c) 2023-2024 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_ng/pattern/calendar/calendar_pattern.h" 23 #include "core/components_ng/pattern/calendar_picker/calendar_dialog_layout_algorithm.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 CreateLayoutAlgorithm()52 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 53 { 54 return MakeRefPtr<CalendarDialogLayoutAlgorithm>(); 55 } 56 SetDialogOffset(const OffsetF & offset)57 void SetDialogOffset(const OffsetF& offset) 58 { 59 dialogOffset_ = offset; 60 } 61 OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,bool,bool)62 bool OnDirtyLayoutWrapperSwap( 63 const RefPtr<LayoutWrapper>& dirty, bool /* skipMeasure */, bool /* skipLayout */) override 64 { 65 if (isFirstAddhotZoneRect_) { 66 AddHotZoneRect(); 67 isFirstAddhotZoneRect_ = false; 68 } 69 return true; 70 } 71 72 void HandleClickEvent(const GestureEvent& info); 73 74 FocusPattern GetFocusPattern() const override; 75 SetHoverState(bool state)76 void SetHoverState(bool state) 77 { 78 hoverState_ = state; 79 } 80 GetHoverState()81 bool GetHoverState() const 82 { 83 return hoverState_; 84 } 85 86 void GetCalendarMonthData(int32_t year, int32_t month, ObtainedMonth& calendarMonthData); 87 CalendarMonth GetNextMonth(const CalendarMonth& calendarMonth); 88 CalendarMonth GetLastMonth(const CalendarMonth& calendarMonth); 89 void SetOptionsButtonUpdateColorFlags(size_t index, bool isUpdate); 90 SetCurrentButtonInfo(const std::vector<ButtonInfo> & buttonInfos)91 void SetCurrentButtonInfo(const std::vector<ButtonInfo>& buttonInfos) 92 { 93 currentButtonInfos_ = buttonInfos; 94 } 95 SetCurrentSettingData(const CalendarSettingData & settingData)96 void SetCurrentSettingData(const CalendarSettingData& settingData) 97 { 98 currentSettingData_ = settingData; 99 } 100 101 void OnColorConfigurationUpdate() override; 102 void OnLanguageConfigurationUpdate() override; 103 void OnFontScaleConfigurationUpdate() override; 104 105 void UpdateCaretInfoToController(); 106 HasSurfaceChangedCallback()107 bool HasSurfaceChangedCallback() 108 { 109 return surfaceChangedCallbackId_.has_value(); 110 } 111 UpdateSurfaceChangedCallbackId(int32_t id)112 void UpdateSurfaceChangedCallbackId(int32_t id) 113 { 114 surfaceChangedCallbackId_ = id; 115 } 116 117 void HandleSurfaceChanged(int32_t newWidth, int32_t newHeight, int32_t prevWidth, int32_t prevHeight); 118 119 void InitSurfaceChangedCallback(); SetTitleNode(RefPtr<FrameNode> & titleNode)120 inline void SetTitleNode(RefPtr<FrameNode>& titleNode) 121 { 122 titleNode_ = titleNode; 123 } 124 125 bool CanReportChangeEvent(const PickerDate& pickerDate); 126 127 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 128 129 private: 130 void OnModifyDone() override; 131 void InitClickEvent(); 132 void InitOnKeyEvent(); 133 void InitOnTouchEvent(); 134 void InitTitleArrowsEvent(); 135 void InitEntryChangeEvent(); 136 void InitHoverEvent(); 137 void AddHotZoneRect(); 138 bool HandleKeyEvent(const KeyEvent& event); 139 void GetInnerFocusPaintRect(RoundRect& paintRect); 140 void PaintFocusState(); 141 void HandleTitleArrowsClickEvent(int32_t nodeIndex); 142 void HandleEntryChange(const std::string& info); 143 void HandleEntryLayoutChange(); 144 void ClearCalendarFocusedState(); 145 bool HandleCalendarNodeKeyEvent(const KeyEvent& event); 146 bool ActClick(int32_t focusAreaID, int32_t focusAreaChildID); 147 void PaintCurrentMonthFocusState(); 148 void PaintNonCurrentMonthFocusState(int32_t focusedDayIndex); 149 void ChangeEntryState(); 150 void FocusedLastFocusedDay(); 151 int32_t GetIndexByFocusedDay(); 152 bool HandleTabKeyEvent(const KeyEvent& event); 153 void FireChangeByKeyEvent(PickerDate& selectedDay); 154 bool IsIndexInCurrentMonth(int32_t focusedDayIndex, const ObtainedMonth& currentMonthData); 155 bool IsInEntryRegion(const Offset& globalLocation); 156 void HandleEntryNodeHoverEvent(bool state, const Offset& globalLocation); 157 void HandleEntryNodeTouchEvent(bool isPressed, const Offset& globalLocation); 158 void UpdateDialogBackgroundColor(); 159 void UpdateTitleArrowsColor(); 160 void UpdateOptionsButtonColor(); 161 void OnEnterKeyEvent(const KeyEvent& event); 162 void UpdateTitleArrowsImage(); 163 void UpdateImage(const RefPtr<FrameNode>& buttonNode, const InternalResource::ResourceId& resourceId); 164 void UpdateOptionsButton(); 165 void UpdateCalendarLayout(); 166 void UpdateNonCurrentMonthFocusedDay(int32_t focusedDayIndex); 167 void PaintMonthFocusState(int32_t focusedDayIndex); 168 169 RefPtr<FrameNode> GetCalendarFrameNode(); 170 RefPtr<CalendarPattern> GetCalendarPattern(); 171 RefPtr<FrameNode> GetSwiperFrameNode(); 172 RefPtr<SwiperPattern> GetSwiperPattern(); 173 174 void UpdateSwiperNode(const ObtainedMonth& monthData, bool isPrev); 175 void UpdateSwiperNodeFocusedDay(const CalendarDay& focusedDay, bool isPrev); 176 void MarkMonthNodeDirty(); 177 178 int32_t focusAreaID_ = 0; 179 int32_t focusAreaIDWithoutWeek_ = 0; 180 int32_t focusAreaChildID_ = 0; 181 CalendarDay focusedDay_ = { .day = -1 }; 182 bool isFirstAddhotZoneRect_ = true; 183 bool isFocused_ = false; 184 bool isCalendarFirstFocused_ = false; 185 bool hoverState_ = false; 186 std::vector<bool> updateColorFlags = {true, true, true, true}; 187 OffsetF dialogOffset_; 188 WeakPtr<FrameNode> entryNode_ = nullptr; 189 std::map<int32_t, RefPtr<ClickEvent>> clickEvents_; 190 RefPtr<TouchEventImpl> touchListener_ = nullptr; 191 RefPtr<InputEvent> hoverListener_ = nullptr; 192 ACE_DISALLOW_COPY_AND_MOVE(CalendarDialogPattern); 193 bool hasTabKeyDown_ = false; 194 std::vector<ButtonInfo> currentButtonInfos_; 195 CalendarSettingData currentSettingData_; 196 std::optional<int32_t> surfaceChangedCallbackId_; 197 PickerDate reportedPickerDate_; 198 WeakPtr<FrameNode> titleNode_; 199 }; 200 } // namespace OHOS::Ace::NG 201 202 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_DIALOG_PATTERN_H 203