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