1 /* 2 * Copyright (c) 2022-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_DATE_PICKER_DATE_PICKER_COLUMN_PATTERN_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DATE_PICKER_DATE_PICKER_COLUMN_PATTERN_H 18 19 #include <utility> 20 21 #include "base/i18n/localization.h" 22 #include "core/components/picker/picker_theme.h" 23 #include "core/components_ng/base/frame_node.h" 24 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h" 25 #include "core/components_ng/pattern/picker/datepicker_column_accessibility_property.h" 26 #include "core/components_ng/pattern/picker/datepicker_column_layout_algorithm.h" 27 #include "core/components_ng/pattern/picker/datepicker_event_hub.h" 28 #include "core/components_ng/pattern/picker/datepicker_layout_property.h" 29 #include "core/components_ng/pattern/picker/datepicker_paint_method.h" 30 #include "core/components_ng/pattern/picker/datepicker_row_layout_property.h" 31 #include "core/components_ng/pattern/picker/toss_animation_controller.h" 32 #include "core/components_ng/pattern/text/text_layout_property.h" 33 #include "core/pipeline_ng/ui_task_scheduler.h" 34 35 namespace OHOS::Ace::NG { 36 37 using ColumnChangeCallback = std::function<void(const RefPtr<FrameNode>&, bool, uint32_t, bool)>; 38 using ColumnFinishCallback = std::function<void(bool)>; 39 using EventCallback = std::function<void(bool)>; 40 41 struct DateTextProperties { 42 Dimension upFontSize; 43 Dimension fontSize; 44 Dimension downFontSize; 45 Color upColor; 46 Color currentColor; 47 Color downColor; 48 }; 49 50 struct DatePickerOptionProperty { 51 float height = 0.0f; 52 float fontheight = 0.0f; 53 float prevDistance = 0.0f; // between the prev item and itself when scroll up 54 float nextDistance = 0.0f; // between the next item and itself when scroll down 55 }; 56 57 class DatePickerEventParam : public virtual AceType { 58 DECLARE_ACE_TYPE(DatePickerEventParam, AceType) 59 60 public: 61 RefPtr<FrameNode> instance_; 62 int32_t itemIndex_ = 0; 63 int32_t itemTotalCounts_ = 0; 64 }; 65 66 enum class DatePickerScrollDirection { 67 UP = 0, 68 DOWN, 69 }; 70 enum class DatePickerOptionIndex { 71 COLUMN_INDEX_0 = 0, 72 COLUMN_INDEX_1, 73 COLUMN_INDEX_2, 74 COLUMN_INDEX_3, 75 COLUMN_INDEX_4, 76 }; 77 78 class DatePickerColumnPattern : public LinearLayoutPattern { 79 DECLARE_ACE_TYPE(DatePickerColumnPattern, LinearLayoutPattern); 80 81 public: DatePickerColumnPattern()82 DatePickerColumnPattern() : LinearLayoutPattern(true) {}; 83 84 ~DatePickerColumnPattern() override = default; 85 CreateLayoutAlgorithm()86 RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override 87 { 88 auto layoutAlgorithm = MakeRefPtr<DatePickerColumnLayoutAlgorithm>(); 89 if (algorithmOffset_.size() == 0) { 90 ResetAlgorithmOffset(); 91 } 92 layoutAlgorithm->SetCurrentOffset(algorithmOffset_); 93 return layoutAlgorithm; 94 } 95 CreateLayoutProperty()96 RefPtr<LayoutProperty> CreateLayoutProperty() override 97 { 98 return MakeRefPtr<DataPickerLayoutProperty>(); 99 } 100 CreateAccessibilityProperty()101 RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override 102 { 103 return MakeRefPtr<DatePickerColumnAccessibilityProperty>(); 104 } 105 106 void FlushCurrentOptions( 107 bool isDown = false, bool isUpateTextContentOnly = false, bool isUpdateAnimationProperties = false); 108 109 bool NotLoopOptions() const; 110 111 void UpdateColumnChildPosition(double offsetY); 112 113 bool CanMove(bool isDown) const; 114 115 bool InnerHandleScroll(bool isDown, bool isUpatePropertiesOnly = false, bool isUpdateAnimationProperties = false); 116 GetCurrentIndex()117 uint32_t GetCurrentIndex() const 118 { 119 // currentIndex_ is year/month/day information, for example month [0, 11] is Equivalent to [1, 12] 120 return currentIndex_; 121 } 122 SetCurrentIndex(uint32_t value)123 void SetCurrentIndex(uint32_t value) 124 { 125 currentIndex_ = value; 126 } 127 GetCurrentOffset()128 double GetCurrentOffset() const 129 { 130 return deltaSize_; 131 } 132 SetCurrentOffset(double deltaSize)133 void SetCurrentOffset(double deltaSize) 134 { 135 deltaSize_ = deltaSize; 136 } 137 GetOptions()138 const std::map<RefPtr<FrameNode>, std::vector<std::string>>& GetOptions() const 139 { 140 return options_; 141 } 142 SetOptions(const std::map<RefPtr<FrameNode>,std::vector<std::string>> & value)143 void SetOptions(const std::map<RefPtr<FrameNode>, std::vector<std::string>>& value) 144 { 145 options_ = value; 146 } 147 GetShowCount()148 uint32_t GetShowCount() const 149 { 150 return showCount_; 151 } 152 SetShowCount(const uint32_t showCount)153 void SetShowCount(const uint32_t showCount) 154 { 155 showCount_ = showCount; 156 } 157 HandleChangeCallback(bool isAdd,bool needNotify)158 void HandleChangeCallback(bool isAdd, bool needNotify) 159 { 160 if (changeCallback_) { 161 changeCallback_(GetHost(), isAdd, GetCurrentIndex(), needNotify); 162 } else { 163 LOGE("change callback is null."); 164 } 165 } 166 GetChangeCallback()167 const ColumnChangeCallback& GetChangeCallback() const 168 { 169 return changeCallback_; 170 } 171 SetChangeCallback(ColumnChangeCallback && value)172 void SetChangeCallback(ColumnChangeCallback&& value) 173 { 174 changeCallback_ = value; 175 } 176 HandleEventCallback(bool refresh)177 void HandleEventCallback(bool refresh) 178 { 179 if (EventCallback_) { 180 EventCallback_(refresh); 181 } else { 182 LOGE("event callback is null."); 183 } 184 } 185 GetEventCallback()186 const EventCallback& GetEventCallback() const 187 { 188 return EventCallback_; 189 } 190 SetEventCallback(EventCallback && value)191 void SetEventCallback(EventCallback&& value) 192 { 193 EventCallback_ = value; 194 } 195 GetToss()196 const RefPtr<TossAnimationController>& GetToss() const 197 { 198 return tossAnimationController_; 199 } 200 SetLocalDownDistance(float value)201 void SetLocalDownDistance(float value) 202 { 203 localDownDistance_ = value; 204 } 205 GetLocalDownDistance()206 float GetLocalDownDistance() const 207 { 208 return localDownDistance_; 209 } 210 211 void UpdateToss(double offsetY); 212 213 void TossStoped(); 214 215 private: 216 void OnModifyDone() override; 217 void OnAttachToFrameNode() override; 218 bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 219 void SetDividerHeight(uint32_t showOptionCount); 220 void OnTouchDown(); 221 void OnTouchUp(); 222 void InitMouseAndPressEvent(); 223 void HandleMouseEvent(bool isHover); 224 void SetButtonBackgroundColor(const Color& pressColor); 225 void PlayPressAnimation(const Color& pressColor); 226 void PlayHoverAnimation(const Color& color); 227 228 std::vector<DatePickerOptionProperty> optionProperties_; 229 RefPtr<ClickEvent> CreateItemClickEventListener(RefPtr<DatePickerEventParam> param); 230 void OnAroundButtonClick(RefPtr<DatePickerEventParam> param); 231 std::vector<int32_t> algorithmOffset_; 232 void ResetAlgorithmOffset(); 233 void CalcAlgorithmOffset(DatePickerScrollDirection dir, double distancePercent); 234 void SetOptionShiftDistance(); 235 float GetShiftDistanceForLandscape(uint32_t index, DatePickerScrollDirection dir); 236 float GetShiftDistance(uint32_t index, DatePickerScrollDirection dir); 237 int32_t CalcScrollIndex(int32_t totalOptionCount, int32_t currentIndex, bool canLoop, int32_t step); 238 void ShiftOptionProp(RefPtr<FrameNode> curNode, RefPtr<FrameNode> shiftNode); 239 240 void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); 241 void HandleDragStart(const GestureEvent& event); 242 void HandleDragMove(const GestureEvent& event); 243 void HandleDragEnd(); 244 void CreateAnimation(); 245 RefPtr<CurveAnimation<double>> CreateAnimation(double from, double to); 246 void HandleCurveStopped(); 247 void ScrollOption(double delta, bool isJump = false); 248 void UpdatePickerTextProperties(uint32_t index, uint32_t showOptionCount, 249 const RefPtr<TextLayoutProperty>& textLayoutProperty, 250 const RefPtr<DataPickerRowLayoutProperty>& dataPickerRowLayoutProperty); 251 void UpdateDisappearTextProperties(const RefPtr<PickerTheme>& pickerTheme, 252 const RefPtr<TextLayoutProperty>& textLayoutProperty, 253 const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty); 254 void UpdateCandidateTextProperties(const RefPtr<PickerTheme>& pickerTheme, 255 const RefPtr<TextLayoutProperty>& textLayoutProperty, 256 const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty); 257 void UpdateSelectedTextProperties(const RefPtr<PickerTheme>& pickerTheme, 258 const RefPtr<TextLayoutProperty>& textLayoutProperty, 259 const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty); 260 void AddAnimationTextProperties(uint32_t currentIndex, const RefPtr<TextLayoutProperty>& textLayoutProperty); 261 void UpdateTextPropertiesLinear(bool isDown, double scale); 262 void TextPropertiesLinearAnimation(const RefPtr<TextLayoutProperty>& textLayoutProperty, uint32_t index, 263 uint32_t showCount, bool isDown, double scale); 264 void FlushAnimationTextProperties(bool isDown); 265 Dimension LinearFontSize(const Dimension& startFontSize, const Dimension& endFontSize, double percent); 266 void SetAccessibilityAction(); 267 float localDownDistance_ = 0.0f; 268 RefPtr<TouchEventImpl> touchListener_; 269 RefPtr<InputEvent> mouseEvent_; 270 std::map<RefPtr<FrameNode>, std::vector<std::string>> options_; 271 ColumnChangeCallback changeCallback_; 272 EventCallback EventCallback_; 273 uint32_t currentIndex_ = 0; 274 int32_t currentChildIndex_ = 0; 275 double yLast_ = 0.0; 276 double yOffset_ = 0.0; 277 double jumpInterval_; 278 uint32_t showCount_ = 0; 279 float gradientHeight_; 280 float dividerHeight_; 281 float dividerSpacingWidth_; 282 283 float dividerSpacing_ = 0.0f; 284 FontWeight SelectedWeight_; 285 FontWeight CandidateWeight_; 286 Color pressColor_; 287 Color hoverColor_; 288 289 double deltaSize_ = 0.0; 290 RefPtr<PanEvent> panEvent_; 291 bool pressed_ = false; 292 bool hoverd_ = false; 293 double scrollDelta_ = 0.0; 294 bool animationCreated_ = false; 295 RefPtr<Animator> toController_; 296 RefPtr<Animator> fromController_; 297 RefPtr<CurveAnimation<double>> fromBottomCurve_; 298 RefPtr<CurveAnimation<double>> fromTopCurve_; 299 RefPtr<TossAnimationController> tossAnimationController_ = AceType::MakeRefPtr<TossAnimationController>(); 300 std::vector<DateTextProperties> animationProperties_; 301 302 ACE_DISALLOW_COPY_AND_MOVE(DatePickerColumnPattern); 303 }; 304 } // namespace OHOS::Ace::NG 305 306 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DATE_PICKER_DATE_PICKER_COLUMN_PATTERN_H 307