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 36 void BeforeCreateLayoutWrapper() override; 37 IsEnableMatchParent()38 bool IsEnableMatchParent() override 39 { 40 return true; 41 } 42 OnColorModeChange(uint32_t colorMode)43 void OnColorModeChange(uint32_t colorMode) override 44 { 45 LinearLayoutPattern::OnColorModeChange(colorMode); 46 auto host = GetHost(); 47 CHECK_NULL_VOID(host); 48 host->MarkModifyDone(); 49 } 50 IsAtomicNode()51 bool IsAtomicNode() const override 52 { 53 return true; 54 } 55 CreateEventHub()56 RefPtr<EventHub> CreateEventHub() override 57 { 58 return MakeRefPtr<CalendarPickerEventHub>(); 59 } 60 CreateLayoutProperty()61 RefPtr<LayoutProperty> CreateLayoutProperty() override 62 { 63 return MakeRefPtr<CalendarPickerLayoutProperty>(); 64 } 65 GetFocusPattern()66 FocusPattern GetFocusPattern() const override 67 { 68 return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION }; 69 } 70 CreateLayoutAlgorithm()71 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 72 { 73 return MakeRefPtr<CalendarPickerLayoutAlgorithm>(); 74 } 75 SetCalendarEdgeAlign(CalendarEdgeAlign align)76 void SetCalendarEdgeAlign(CalendarEdgeAlign align) 77 { 78 align_ = align; 79 } 80 GetCalendarEdgeAlign()81 CalendarEdgeAlign GetCalendarEdgeAlign() const 82 { 83 return align_; 84 } 85 SetCalendarDialogOffset(const DimensionOffset & offset)86 void SetCalendarDialogOffset(const DimensionOffset& offset) 87 { 88 offset_ = offset; 89 } 90 GetCalendarDialogOffset()91 DimensionOffset GetCalendarDialogOffset() const 92 { 93 return offset_; 94 } 95 SetCalendarData(const CalendarSettingData & data)96 void SetCalendarData(const CalendarSettingData& data) 97 { 98 CalendarSettingData settingData = data; 99 settingData.selectedDate = PickerDate::AdjustDateToRange(data.selectedDate, data.startDate, data.endDate); 100 calendarData_ = settingData; 101 } 102 GetCalendarData()103 CalendarSettingData GetCalendarData() const 104 { 105 return calendarData_; 106 } 107 108 void HandleClickEvent(const Offset& globalLocation); 109 void HandleTextHoverEvent(bool state, int32_t index); 110 void HandleAddButtonClick(); 111 void HandleSubButtonClick(); 112 bool HandleNumberKeyEvent(const KeyEvent& event); 113 114 bool HandleKeyEvent(const KeyEvent& event); 115 bool HandleFocusEvent(const KeyEvent& event); 116 bool HandleBlurEvent(const KeyEvent& event); 117 118 void OnValueChange(); IsDialogShow()119 bool IsDialogShow() const 120 { 121 return isDialogShow_; 122 } SetDialogShow(bool flag)123 void SetDialogShow(bool flag) 124 { 125 isDialogShow_ = flag; 126 } 127 128 CalendarPickerSelectedType CheckRegion(const Offset& globalLocation); GetSelectedType()129 CalendarPickerSelectedType GetSelectedType() const 130 { 131 return selected_; 132 } 133 void SetSelectedType(CalendarPickerSelectedType type); 134 HasSubNode()135 bool HasSubNode() const 136 { 137 return subId_.has_value(); 138 } GetSubId()139 int32_t GetSubId() 140 { 141 if (!subId_.has_value()) { 142 subId_ = ElementRegister::GetInstance()->MakeUniqueId(); 143 } 144 return subId_.value(); 145 } 146 HasContentNode()147 bool HasContentNode() const 148 { 149 return contentId_.has_value(); 150 } GetContentId()151 int32_t GetContentId() 152 { 153 if (!contentId_.has_value()) { 154 contentId_ = ElementRegister::GetInstance()->MakeUniqueId(); 155 } 156 return contentId_.value(); 157 } 158 HasAddNode()159 bool HasAddNode() const 160 { 161 return addId_.has_value(); 162 } GetAddId()163 int32_t GetAddId() 164 { 165 if (!addId_.has_value()) { 166 addId_ = ElementRegister::GetInstance()->MakeUniqueId(); 167 } 168 return addId_.value(); 169 } 170 HasButtonFlexNode()171 bool HasButtonFlexNode() const 172 { 173 return buttonFlexId_.has_value(); 174 } GetButtonFlexId()175 int32_t GetButtonFlexId() 176 { 177 if (!buttonFlexId_.has_value()) { 178 buttonFlexId_ = ElementRegister::GetInstance()->MakeUniqueId(); 179 } 180 return buttonFlexId_.value(); 181 } 182 183 bool OnDirtyLayoutWrapperSwap( 184 const RefPtr<LayoutWrapper>& dirty, bool /* skipMeasure */, bool /* skipLayout */) override; 185 186 OffsetF CalculateDialogOffset(); 187 void HandleHoverEvent(bool state, const Offset& globalLocation); 188 void HandleTouchEvent(bool isPressed, const Offset& globalLocation); 189 190 bool IsContainerModal(); 191 192 void SetDate(const std::string& info); 193 void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override; 194 void SetMarkToday(bool isMarkToday); 195 bool GetMarkToday(); 196 void SetDisabledDateRange(const std::vector<std::pair<PickerDate, PickerDate>>& disabledDateRange); 197 std::string GetDisabledDateRange(); 198 void UpdateTextStyle(const PickerTextStyle& textStyle); 199 200 private: 201 void OnModifyDone() override; 202 void OnWindowSizeChanged(int32_t width, int32_t height, WindowSizeChangeReason type) override; 203 void OnColorConfigurationUpdate() override; 204 void InitDateIndex(); 205 void InitClickEvent(); 206 void InitOnKeyEvent(); 207 void InitOnHoverEvent(); 208 void ShowDialog(); 209 void InitDialogProperties(DialogProperties& properties); 210 void PostTaskToUI(const std::function<void()>& task, const std::string& name); 211 void HandleTaskCallback(); 212 void HandleZeroStartTaskCallback(); 213 void HandleTextFocusEvent(int32_t index); 214 void HandleBlurEvent(); 215 void HandleButtonHoverEvent(bool state, int32_t index); 216 void HandleButtonTouchEvent(bool isPressed, int32_t index); 217 void HandleEnable(); 218 void ResetTextState(); 219 void ResetTextStateByNode(const RefPtr<FrameNode>& textFrameNode); 220 void FlushTextStyle(); 221 bool IsInNodeRegion(const RefPtr<FrameNode>& node, const PointF& point); 222 bool HandleYearKeyWaitingEvent( 223 const uint32_t number, const std::function<void()>& task, const std::function<void()>& zeroStartTask); 224 bool HandleYearKeyEvent(uint32_t number); 225 bool HandleMonthKeyEvent(uint32_t number); 226 bool HandleDayKeyEvent(uint32_t number); 227 void FireChangeEvents(const std::string& info); 228 void UpdateEntryButtonColor(); 229 void UpdateEntryButtonBorderWidth(); 230 void UpdateEdgeAlign(); 231 void UpdateAccessibilityText(); 232 void FlushAddAndSubButton(); 233 bool IsAddOrSubButtonEnable(int32_t buttonIndex); 234 void PrevDateBySelectedType(PickerDate& date); 235 void NextDateBySelectedType(PickerDate& date); 236 237 std::string GetEntryDateInfo(); 238 bool ReportChangeEvent(const std::string& compName, 239 const std::string& eventName, const std::string& eventData); 240 241 uint32_t yearEnterCount_ = 0; 242 uint32_t yearPrefixZeroCount_ = 0; 243 uint32_t monthPrefixZeroCount_ = 0; 244 uint32_t dayPrefixZeroCount_ = 0; 245 246 int32_t yearIndex_ = 0; 247 int32_t monthIndex_ = 2; 248 int32_t dayIndex_ = 4; 249 250 std::optional<int32_t> subId_; 251 std::optional<int32_t> contentId_; 252 std::optional<int32_t> addId_; 253 std::optional<int32_t> buttonFlexId_; 254 int32_t taskCount_ = 0; 255 CalendarEdgeAlign align_ = CalendarEdgeAlign::EDGE_ALIGN_END; 256 DimensionOffset offset_; 257 CalendarSettingData calendarData_; 258 bool isDialogShow_ = false; 259 bool isKeyWaiting_ = false; 260 bool isFirtFocus_ = true; 261 RefPtr<ClickEvent> clickListener_; 262 RefPtr<InputEvent> hoverListener_; 263 CalendarPickerSelectedType selected_ = CalendarPickerSelectedType::OTHER; 264 PickerDate reportedPickerDate_; 265 ACE_DISALLOW_COPY_AND_MOVE(CalendarPickerPattern); 266 bool isMarkToday_ = false; 267 }; 268 } // namespace OHOS::Ace::NG 269 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_CALENDAR_PICKER_CALENDAR_PICKER_PATTERN_H 270