• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     FontWeight upFontWeight;
46     FontWeight fontWeight;
47     FontWeight downFontWeight;
48     Color upColor;
49     Color currentColor;
50     Color downColor;
51 };
52 
53 struct DatePickerOptionProperty {
54     float height = 0.0f;
55     float fontheight = 0.0f;
56     float prevDistance = 0.0f; // between the prev item and itself when scroll up
57     float nextDistance = 0.0f; // between the next item and itself when scroll down
58 };
59 
60 class DatePickerEventParam : public virtual AceType {
61     DECLARE_ACE_TYPE(DatePickerEventParam, AceType)
62 
63 public:
64     WeakPtr<FrameNode> instance_;
65     int32_t itemIndex_ = 0;
66     int32_t itemTotalCounts_ = 0;
67 };
68 
69 enum class DatePickerScrollDirection {
70     UP = 0,
71     DOWN,
72 };
73 enum class DatePickerOptionIndex {
74     COLUMN_INDEX_0 = 0,
75     COLUMN_INDEX_1,
76     COLUMN_INDEX_2,
77     COLUMN_INDEX_3,
78     COLUMN_INDEX_4,
79     COLUMN_INDEX_5,
80     COLUMN_INDEX_6,
81 };
82 
83 class DatePickerColumnPattern : public LinearLayoutPattern {
84     DECLARE_ACE_TYPE(DatePickerColumnPattern, LinearLayoutPattern);
85 
86 public:
DatePickerColumnPattern()87     DatePickerColumnPattern() : LinearLayoutPattern(true) {};
88 
89     ~DatePickerColumnPattern() override = default;
90 
CreateLayoutAlgorithm()91     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
92     {
93         auto layoutAlgorithm = MakeRefPtr<DatePickerColumnLayoutAlgorithm>();
94         if (algorithmOffset_.size() == 0) {
95             ResetAlgorithmOffset();
96         }
97         layoutAlgorithm->SetCurrentOffset(algorithmOffset_);
98         return layoutAlgorithm;
99     }
100 
CreateLayoutProperty()101     RefPtr<LayoutProperty> CreateLayoutProperty() override
102     {
103         return MakeRefPtr<DataPickerLayoutProperty>();
104     }
105 
CreateAccessibilityProperty()106     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
107     {
108         return MakeRefPtr<DatePickerColumnAccessibilityProperty>();
109     }
110 
111     void FlushCurrentOptions(
112         bool isDown = false, bool isUpateTextContentOnly = false, bool isUpdateAnimationProperties = false);
113 
114     bool NotLoopOptions() const;
115 
116     void UpdateColumnChildPosition(double offsetY);
117 
118     bool CanMove(bool isDown) const;
119 
120     bool InnerHandleScroll(bool isDown, bool isUpatePropertiesOnly = false, bool isUpdateAnimationProperties = false);
121 
GetCurrentIndex()122     uint32_t GetCurrentIndex() const
123     {
124         // currentIndex_ is year/month/day information, for example month [0, 11] is Equivalent to [1, 12]
125         return currentIndex_;
126     }
127 
SetCurrentIndex(uint32_t value)128     void SetCurrentIndex(uint32_t value)
129     {
130         currentIndex_ = value;
131     }
132 
GetCurrentOffset()133     double GetCurrentOffset() const
134     {
135         return deltaSize_;
136     }
137 
SetCurrentOffset(double deltaSize)138     void SetCurrentOffset(double deltaSize)
139     {
140         deltaSize_ = deltaSize;
141     }
142 
GetOptions()143     const std::map<WeakPtr<FrameNode>, std::vector<PickerDateF>>& GetOptions() const
144     {
145         return options_;
146     }
147 
SetOptions(const std::map<WeakPtr<FrameNode>,std::vector<PickerDateF>> & value)148     void SetOptions(const std::map<WeakPtr<FrameNode>, std::vector<PickerDateF>>& value)
149     {
150         options_ = value;
151     }
152 
GetShowCount()153     uint32_t GetShowCount() const
154     {
155         return showCount_;
156     }
157 
SetShowCount(const uint32_t showCount)158     void SetShowCount(const uint32_t showCount)
159     {
160         showCount_ = showCount;
161     }
162 
HandleChangeCallback(bool isAdd,bool needNotify)163     void HandleChangeCallback(bool isAdd, bool needNotify)
164     {
165         if (changeCallback_) {
166             changeCallback_(GetHost(), isAdd, GetCurrentIndex(), needNotify);
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         }
185     }
186 
GetEventCallback()187     const EventCallback& GetEventCallback() const
188     {
189         return EventCallback_;
190     }
191 
SetEventCallback(EventCallback && value)192     void SetEventCallback(EventCallback&& value)
193     {
194         EventCallback_ = value;
195     }
196 
GetToss()197     const RefPtr<TossAnimationController>& GetToss() const
198     {
199         return tossAnimationController_;
200     }
201 
SetLocalDownDistance(float value)202     void SetLocalDownDistance(float value)
203     {
204         localDownDistance_ = value;
205     }
206 
GetLocalDownDistance()207     float GetLocalDownDistance() const
208     {
209         return localDownDistance_;
210     }
211 
212     void UpdateToss(double offsetY);
213 
214     void UpdateFinishToss(double offsetY);
215 
216     void TossStoped();
217 
SetYLast(double value)218     void SetYLast(double value)
219     {
220         yLast_ = value;
221     }
GetOffset()222     double GetOffset()
223     {
224         return offsetCurSet_;
225     }
226     void PlayRestAnimation();
227 
228     void TossAnimationStoped();
229 
GetMidShiftDistance()230     std::vector<DatePickerOptionProperty> GetMidShiftDistance()
231     {
232         return optionProperties_;
233     }
234 
SetMainVelocity(double mainVelocity)235     void SetMainVelocity(double mainVelocity)
236     {
237         mainVelocity_ = mainVelocity;
238     }
239 
GetMainVelocity()240     double GetMainVelocity() const
241     {
242         return mainVelocity_;
243     }
244 
SetTossStatus(bool status)245     void SetTossStatus(bool status)
246     {
247         isTossStatus_ = status;
248     }
249 
GetTossStatus()250     bool GetTossStatus()
251     {
252         return isTossStatus_;
253     }
254 
SetYOffset(double value)255     void SetYOffset(double value)
256     {
257         yOffset_ = value;
258     }
259 
GetTouchBreakStatus()260     bool GetTouchBreakStatus()
261     {
262         return touchBreak_;
263     }
264 
GetClickBreakStatus()265     bool GetClickBreakStatus()
266     {
267         return clickBreak_;
268     }
269 
SetclickBreak(bool value)270     void SetclickBreak(bool value)
271     {
272         clickBreak_ = value;
273     }
274 
275 private:
276     void OnModifyDone() override;
277     void OnAttachToFrameNode() override;
278     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
279     void SetDividerHeight(uint32_t showOptionCount);
280     void OnTouchDown();
281     void OnTouchUp();
282     void InitMouseAndPressEvent();
283     void HandleMouseEvent(bool isHover);
284     void SetButtonBackgroundColor(const Color& pressColor);
285     void PlayPressAnimation(const Color& pressColor);
286     void PlayHoverAnimation(const Color& color);
287 
288     std::vector<DatePickerOptionProperty> optionProperties_;
289     RefPtr<ClickEvent> CreateItemClickEventListener(RefPtr<DatePickerEventParam> param);
290     RefPtr<TouchEventImpl> CreateItemTouchEventListener();
291     void OnAroundButtonClick(RefPtr<DatePickerEventParam> param);
292     std::vector<int32_t> algorithmOffset_;
293     void ResetAlgorithmOffset();
294     void CalcAlgorithmOffset(DatePickerScrollDirection dir, double distancePercent);
295     void SetOptionShiftDistance();
296     float GetShiftDistanceForLandscape(uint32_t index, DatePickerScrollDirection dir);
297     float GetShiftDistance(uint32_t index, DatePickerScrollDirection dir);
298     int32_t CalcScrollIndex(int32_t totalOptionCount, int32_t currentIndex, bool canLoop, int32_t step);
299     void ShiftOptionProp(RefPtr<FrameNode> curNode, RefPtr<FrameNode> shiftNode);
300 
301     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
302     void HandleDragStart(const GestureEvent& event);
303     void HandleDragMove(const GestureEvent& event);
304     void HandleDragEnd();
305     void CreateAnimation();
306     void CreateAnimation(double from, double to);
307     void ScrollOption(double delta, bool isJump = false);
308     void UpdatePickerTextProperties(uint32_t index, uint32_t showOptionCount,
309         const RefPtr<TextLayoutProperty>& textLayoutProperty,
310         const RefPtr<DataPickerRowLayoutProperty>& dataPickerRowLayoutProperty);
311     void UpdateDisappearTextProperties(const RefPtr<PickerTheme>& pickerTheme,
312         const RefPtr<TextLayoutProperty>& textLayoutProperty,
313         const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty);
314     void UpdateCandidateTextProperties(const RefPtr<PickerTheme>& pickerTheme,
315         const RefPtr<TextLayoutProperty>& textLayoutProperty,
316         const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty);
317     void UpdateSelectedTextProperties(const RefPtr<PickerTheme>& pickerTheme,
318         const RefPtr<TextLayoutProperty>& textLayoutProperty,
319         const RefPtr<DataPickerRowLayoutProperty>& timePickerLayoutProperty);
320     void AddAnimationTextProperties(uint32_t currentIndex, const RefPtr<TextLayoutProperty>& textLayoutProperty);
321     void UpdateTextPropertiesLinear(bool isDown, double scale);
322     void TextPropertiesLinearAnimation(const RefPtr<TextLayoutProperty>& textLayoutProperty, uint32_t index,
323         uint32_t showCount, bool isDown, double scale);
324     void FlushAnimationTextProperties(bool isDown);
325     Dimension LinearFontSize(const Dimension& startFontSize, const Dimension& endFontSize, double percent);
326     void SetAccessibilityAction();
327     void AddHotZoneRectToText();
328     float localDownDistance_ = 0.0f;
329     RefPtr<TouchEventImpl> touchListener_;
330     RefPtr<InputEvent> mouseEvent_;
331     std::map<WeakPtr<FrameNode>, std::vector<PickerDateF>> options_;
332     ColumnChangeCallback changeCallback_;
333     EventCallback EventCallback_;
334     uint32_t currentIndex_ = 0;
335     int32_t currentChildIndex_ = 0;
336     double yLast_ = 0.0;
337     double yOffset_ = 0.0;
338     double jumpInterval_;
339     uint32_t showCount_ = 0;
340     float gradientHeight_ = 0.0f;
341     float dividerHeight_ = 0.0f;
342     float dividerSpacingWidth_ = 0.0f;
343     double mainVelocity_ = 0.0;
344     float dividerSpacing_ = 0.0f;
345     FontWeight SelectedWeight_;
346     FontWeight CandidateWeight_;
347     double offsetCurSet_ = 0.0;
348     double distancePercent_ = 0.0;
349     Color pressColor_;
350     Color hoverColor_;
351     bool isTossStatus_ = false;
352     bool clickBreak_ = false;
353     bool touchBreak_ = false;
354     bool animationBreak_ = false;
355     double deltaSize_ = 0.0;
356     RefPtr<PanEvent> panEvent_;
357     bool pressed_ = false;
358     bool hoverd_ = false;
359     double scrollDelta_ = 0.0;
360     bool animationCreated_ = false;
361     OffsetF offset_;
362     SizeF size_;
363     RefPtr<TossAnimationController> tossAnimationController_ = AceType::MakeRefPtr<TossAnimationController>();
364     std::vector<DateTextProperties> animationProperties_;
365 
366     RefPtr<NodeAnimatablePropertyFloat> scrollProperty_;
367     RefPtr<NodeAnimatablePropertyFloat> aroundClickProperty_;
368     std::shared_ptr<AnimationUtils::Animation> animation_;
369 
370     ACE_DISALLOW_COPY_AND_MOVE(DatePickerColumnPattern);
371 };
372 } // namespace OHOS::Ace::NG
373 
374 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_DATE_PICKER_DATE_PICKER_COLUMN_PATTERN_H
375