1 /* 2 * Copyright (c) 2025 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PICKER_UTILS_PICKER_COLUMN_PATTERN_H 16 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PICKER_UTILS_PICKER_COLUMN_PATTERN_H 17 #include <cstdint> 18 19 #include "adapter/ohos/entrance/picker/picker_haptic_factory.h" 20 #include "core/components_ng/pattern/linear_layout/linear_layout_pattern.h" 21 #include "core/components_ng/pattern/picker/datepicker_layout_property.h" 22 #include "core/components_ng/pattern/picker_utils/picker_column_pattern_utils.h" 23 #include "core/components_ng/pattern/picker_utils/picker_layout_property.h" 24 #include "core/components_ng/pattern/picker_utils/toss_animation_controller.h" 25 #include "core/components_ng/pattern/text/text_layout_property.h" 26 #ifdef SUPPORT_DIGITAL_CROWN 27 #include "core/event/crown_event.h" 28 #endif 29 namespace OHOS::Ace::NG { 30 31 using ColumnChangeCallback = std::function<void(const RefPtr<FrameNode>&, bool, uint32_t, bool)>; 32 using ColumnFinishCallback = std::function<void(bool)>; 33 using EventCallback = std::function<void(bool)>; 34 35 struct TextProperties { 36 Dimension upFontSize; 37 Dimension fontSize; 38 Dimension downFontSize; 39 FontWeight upFontWeight = FontWeight::W100; 40 FontWeight fontWeight = FontWeight::W100; 41 FontWeight downFontWeight = FontWeight::W100; 42 Color upColor; 43 Color currentColor; 44 Color downColor; 45 }; 46 47 struct PickerOptionProperty { 48 float height = 0.0f; 49 float fontheight = 0.0f; 50 float prevDistance = 0.0f; // between the prev item and itself when scroll up 51 float nextDistance = 0.0f; // between the next item and itself when scroll down 52 }; 53 54 class PickerEventParam : public virtual AceType { 55 DECLARE_ACE_TYPE(PickerEventParam, AceType); 56 57 public: 58 WeakPtr<FrameNode> instance_; 59 int32_t itemIndex_ = 0; 60 int32_t itemTotalCounts_ = 0; 61 }; 62 63 enum class PickerScrollDirection { 64 UP = 0, 65 DOWN, 66 }; 67 68 enum class PickerOptionIndex { 69 COLUMN_INDEX_0 = 0, 70 COLUMN_INDEX_1, 71 COLUMN_INDEX_2, 72 COLUMN_INDEX_3, 73 COLUMN_INDEX_4, 74 COLUMN_INDEX_5, 75 COLUMN_INDEX_6, 76 }; 77 78 class PickerColumnPattern : public LinearLayoutPattern { 79 DECLARE_ACE_TYPE(PickerColumnPattern, LinearLayoutPattern); 80 81 public: LinearLayoutPattern(isVertical)82 PickerColumnPattern(bool isVertical = false) : LinearLayoutPattern(isVertical) {}; ~PickerColumnPattern()83 virtual ~PickerColumnPattern() 84 { 85 if (circleUtils_) { 86 delete circleUtils_; 87 } 88 } 89 90 virtual void FlushCurrentOptions(bool isDown = false, bool isUpateTextContentOnly = false, 91 bool isUpdateAnimationProperties = false, bool isTossPlaying = false) = 0; 92 virtual void InitHapticController(const RefPtr<FrameNode>& host) = 0; 93 virtual void UpdateColumnChildPosition(double offsetY) = 0; 94 virtual void UpdateSelectedTextColor(const RefPtr<PickerTheme>& pickerTheme) = 0; 95 virtual void TextPropertiesLinearAnimation(const RefPtr<TextLayoutProperty>& textLayoutProperty, uint32_t index, 96 uint32_t showCount, bool isDown, double scale) = 0; 97 virtual void InitTextFontFamily() = 0; 98 virtual uint32_t GetOptionCount() const = 0; 99 virtual const Color& GetButtonHoverColor() const = 0; 100 virtual const Color& GetButtonBgColor() const = 0; 101 virtual const Color& GetButtonPressColor() const = 0; 102 virtual bool CanMove(bool isDown) const = 0; 103 virtual bool GetCanLoopFromLayoutProperty() const = 0; 104 virtual std::string GetCurrentOption() const = 0; 105 GetShowCount()106 virtual uint32_t GetShowCount() const 107 { 108 return showCount_; 109 } 110 SetTossStatus(bool status)111 virtual void SetTossStatus(bool status) 112 { 113 isTossStatus_ = status; 114 } 115 GetTossStatus()116 virtual bool GetTossStatus() const 117 { 118 return isTossStatus_; 119 } 120 GetToss()121 virtual const RefPtr<TossAnimationController>& GetToss() const 122 { 123 return tossAnimationController_; 124 } 125 SetYLast(double value)126 virtual void SetYLast(double value) 127 { 128 yLast_ = value; 129 } 130 SetLocalDownDistance(float value)131 virtual void SetLocalDownDistance(float value) 132 { 133 localDownDistance_ = value; 134 } 135 GetLocalDownDistance()136 virtual float GetLocalDownDistance() const 137 { 138 return localDownDistance_; 139 } SetMainVelocity(double mainVelocity)140 virtual void SetMainVelocity(double mainVelocity) 141 { 142 mainVelocity_ = mainVelocity; 143 } 144 GetMainVelocity()145 virtual double GetMainVelocity() const 146 { 147 return mainVelocity_; 148 } 149 GetMidShiftDistance()150 virtual std::vector<PickerOptionProperty> GetMidShiftDistance() 151 { 152 return optionProperties_; 153 } 154 SetCurrentIndex(uint32_t value)155 virtual void SetCurrentIndex(uint32_t value) 156 { 157 // minute : [0, 59]; 158 // AM_PM hour : [0, 11]; 24 hour : [0, 23] 159 currentIndex_ = value; 160 } 161 GetCurrentIndex()162 virtual uint32_t GetCurrentIndex() const 163 { 164 // currentIndex_ is year/month/day information, for example month [0, 11] is Equivalent to [1, 12] 165 return currentIndex_; 166 } 167 HandleChangeCallback(bool isAdd,bool needNotify)168 virtual void HandleChangeCallback(bool isAdd, bool needNotify) 169 { 170 if (changeCallback_) { 171 changeCallback_(GetHost(), isAdd, GetCurrentIndex(), needNotify); 172 } 173 } 174 GetChangeCallback()175 virtual const ColumnChangeCallback& GetChangeCallback() const 176 { 177 return changeCallback_; 178 } 179 SetChangeCallback(ColumnChangeCallback && value)180 virtual void SetChangeCallback(ColumnChangeCallback&& value) 181 { 182 changeCallback_ = value; 183 } 184 HandleEventCallback(bool refresh)185 virtual void HandleEventCallback(bool refresh) 186 { 187 if (EventCallback_) { 188 EventCallback_(refresh); 189 } 190 } 191 GetEventCallback()192 virtual const EventCallback& GetEventCallback() const 193 { 194 return EventCallback_; 195 } 196 SetEventCallback(EventCallback && value)197 virtual void SetEventCallback(EventCallback&& value) 198 { 199 EventCallback_ = value; 200 } 201 GetTouchBreakStatus()202 virtual bool GetTouchBreakStatus() const 203 { 204 return touchBreak_; 205 } 206 SetYOffset(double value)207 virtual void SetYOffset(double value) 208 { 209 yOffset_ = value; 210 } 211 GetOffset()212 virtual double GetOffset() const 213 { 214 return offsetCurSet_; 215 } 216 GetCurrentOffset()217 virtual float GetCurrentOffset() const 218 { 219 return deltaSize_; 220 } 221 SetCurrentOffset(float deltaSize)222 virtual void SetCurrentOffset(float deltaSize) 223 { 224 deltaSize_ = deltaSize; 225 } 226 GetClickBreakStatus()227 virtual bool GetClickBreakStatus() const 228 { 229 return clickBreak_; 230 } 231 SetclickBreak(bool value)232 virtual void SetclickBreak(bool value) 233 { 234 clickBreak_ = value; 235 } 236 IsTossNeedToStop()237 virtual bool IsTossNeedToStop() 238 { 239 return false; 240 } 241 242 public: 243 virtual void OnAttachToFrameNode() override; 244 virtual void OnDetachFromFrameNode(FrameNode* frameNode) override; 245 void OnAttachToMainTree() override; 246 void OnDetachFromMainTree() override; 247 OnAttachToFrameNodeMultiThread()248 void OnAttachToFrameNodeMultiThread() {} OnDetachFromFrameNodeMultiThread(FrameNode * frameNode)249 void OnDetachFromFrameNodeMultiThread(FrameNode* frameNode) {} 250 void OnAttachToMainTreeMultiThread(); 251 void OnDetachFromMainTreeMultiThread(); 252 253 virtual bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override; 254 virtual void CreateAnimation(double from, double to); 255 virtual void CreateAnimation(); 256 virtual void TossAnimationStoped(); 257 virtual void StopHaptic(); 258 virtual void SetSelectedMark(bool focus = true, bool notify = true, bool reRender = true); 259 virtual void SetSelectedMarkId(const std::string& strColumnId); 260 virtual void UpdateUserSetSelectColor(); 261 virtual Dimension LinearFontSize(const Dimension& startFontSize, const Dimension& endFontSize, double percent); 262 virtual void CalcAlgorithmOffset(PickerScrollDirection dir, double distancePercent); 263 virtual void SetSelectedMarkListener(const std::function<void(std::string& selectedColumnId)>& listener); 264 virtual RefPtr<TouchEventImpl> CreateItemTouchEventListener(); 265 virtual void OnAroundButtonClick(RefPtr<PickerEventParam> param); 266 virtual void OnTouchDown(); 267 virtual void OnTouchUp(); 268 virtual void ParseTouchListener(); 269 virtual void ParseMouseEvent(); 270 virtual void InitMouseAndPressEvent(); 271 virtual void HandleMouseEvent(bool isHover); 272 virtual void SetButtonBackgroundColor(const Color& pressColor); 273 virtual void PlayHoverAnimation(const Color& color); 274 virtual void PlayPressAnimation(const Color& pressColor); 275 virtual void RegisterWindowStateChangedCallback(); 276 virtual void UnregisterWindowStateChangedCallback(FrameNode* frameNode); 277 virtual void AddHotZoneRectToText(); 278 virtual DimensionRect CalculateHotZone( 279 int32_t index, int32_t midSize, float middleChildHeight, float otherChildHeight); 280 virtual void SetSelectedMarkPaint(bool paint); 281 virtual void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub); 282 virtual void HandleDragStart(const GestureEvent& event); 283 virtual void HandleDragEnd(); 284 virtual void ResetAlgorithmOffset(); 285 virtual void ShiftOptionProp(RefPtr<FrameNode> curNode, RefPtr<FrameNode> shiftNode); 286 virtual void AddAnimationTextProperties( 287 uint32_t currentIndex, const RefPtr<TextLayoutProperty>& textLayoutProperty); 288 virtual void UpdateFinishToss(double offsetY); 289 virtual void FlushAnimationTextProperties(bool isDown); 290 virtual void SetAccessibilityAction(); 291 virtual void OnWindowHide() override; 292 virtual void OnWindowShow() override; 293 virtual RefPtr<ClickEvent> CreateItemClickEventListener(RefPtr<PickerEventParam> param); 294 virtual bool InnerHandleScroll( 295 bool isDown, bool isUpatePropertiesOnly = false, bool isUpdateAnimationProperties = false); 296 virtual bool NotLoopOptions() const; 297 virtual void TossStoped(); 298 virtual void ScrollOption(double delta, bool isJump = false); 299 virtual void UpdateDisappearTextProperties(const RefPtr<PickerTheme>& pickerTheme, 300 const RefPtr<TextLayoutProperty>& textLayoutProperty, const RefPtr<PickerLayoutProperty>& pickerLayoutProperty); 301 virtual void UpdateCandidateTextProperties(const RefPtr<PickerTheme>& pickerTheme, 302 const RefPtr<TextLayoutProperty>& textLayoutProperty, const RefPtr<PickerLayoutProperty>& pickerLayoutProperty); 303 virtual void UpdateSelectedTextProperties(const RefPtr<PickerTheme>& pickerTheme, 304 const RefPtr<TextLayoutProperty>& textLayoutProperty, const RefPtr<PickerLayoutProperty>& pickerLayoutProperty); HandleEnterSelectedArea(double scrollDelta,float shiftDistance,PickerScrollDirection dir)305 virtual void HandleEnterSelectedArea(double scrollDelta, float shiftDistance, PickerScrollDirection dir) {} 306 virtual void HandleDragMove(const GestureEvent& event); 307 virtual void PlayRestAnimation(); 308 virtual void SetOptionShiftDistance(); 309 virtual bool GetOptionItemCount(uint32_t& itemCounts); 310 virtual bool IsLanscape(uint32_t itemCount); 311 virtual float GetShiftDistanceForLandscape(uint32_t index, PickerScrollDirection dir); 312 virtual float GetShiftDistance(uint32_t index, PickerScrollDirection dir); 313 virtual void UpdateTextPropertiesLinear(bool isDown, double scale); 314 virtual void UpdateToss(double offsetY); 315 virtual void HandleAccessibilityTextChange(); 316 #ifdef SUPPORT_DIGITAL_CROWN 317 virtual void HandleCrownBeginEvent(const CrownEvent& event); 318 virtual void HandleCrownMoveEvent(const CrownEvent& event); 319 virtual void HandleCrownEndEvent(const CrownEvent& event); 320 #endif 321 322 #ifdef SUPPORT_DIGITAL_CROWN 323 std::string& GetSelectedColumnId(); 324 bool IsCrownEventEnded(); 325 int32_t GetDigitalCrownSensitivity(); 326 void SetDigitalCrownSensitivity(int32_t crownSensitivity); 327 bool OnCrownEvent(const CrownEvent& event); 328 #endif 329 protected: 330 uint32_t showCount_ = 0; 331 float localDownDistance_ = 0.0f; 332 OffsetF offset_; 333 SizeF size_; 334 double scrollDelta_ = 0.0; 335 double yLast_ = 0.0; 336 double yOffset_ = 0.0; 337 double offsetCurSet_ = 0.0; 338 double distancePercent_ = 0.0; 339 double mainVelocity_ = 0.0; 340 double jumpInterval_ = 0.0; 341 double deltaSize_ = 0.0; 342 bool stopHaptic_ = false; 343 bool hovered_ = false; 344 bool useButtonFocusArea_ = false; 345 bool isFocusColumn_ = false; 346 bool isTossStatus_ = false; 347 bool clickBreak_ = false; 348 bool touchBreak_ = false; 349 bool animationBreak_ = false; 350 bool selectedMarkPaint_ = false; 351 bool pressed_ = false; 352 bool animationCreated_ = false; 353 bool isShow_ = true; 354 bool isEnableHaptic_ = true; 355 bool isHapticPlayOnce_ = true; 356 bool isTossPlaying_ = false; 357 std::shared_ptr<IPickerAudioHaptic> hapticController_ = nullptr; 358 RefPtr<NodeAnimatablePropertyFloat> scrollProperty_; 359 RefPtr<TossAnimationController> tossAnimationController_ = AceType::MakeRefPtr<TossAnimationController>(); 360 std::vector<TextProperties> animationProperties_; 361 RefPtr<NodeAnimatablePropertyFloat> aroundClickProperty_; 362 std::shared_ptr<AnimationUtils::Animation> animation_; 363 RefPtr<TouchEventImpl> touchListener_; 364 RefPtr<InputEvent> mouseEvent_; 365 RefPtr<PanEvent> panEvent_; 366 std::vector<int32_t> algorithmOffset_; 367 std::vector<PickerOptionProperty> optionProperties_; 368 uint32_t currentIndex_ = 0; 369 ColumnChangeCallback changeCallback_; 370 EventCallback EventCallback_; 371 FontWeight SelectedWeight_ = FontWeight::MEDIUM; 372 373 public: 374 PickerColumnPatternCircleUtils<PickerColumnPattern>* circleUtils_ = nullptr; 375 std::function<void(std::string& selectedColumnId)> focusedListerner_ = nullptr; 376 std::string selectedColumnId_ = ""; 377 bool isUserSetSelectColor_ = false; 378 #ifdef SUPPORT_DIGITAL_CROWN 379 bool isCrownEventEnded_ = true; 380 int32_t crownSensitivity_ = INVALID_CROWNSENSITIVITY; 381 #endif 382 ACE_DISALLOW_COPY_AND_MOVE(PickerColumnPattern); 383 }; 384 } // namespace OHOS::Ace::NG 385 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_PICKER_UTILS_PICKER_COLUMN_PATTERN_H 386