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_RATING_RATING_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RATING_RATING_PATTERN_H
18 
19 #include <cstdint>
20 
21 #include "core/components/rating/rating_theme.h"
22 #include "core/components/theme/icon_theme.h"
23 #include "core/components_ng/pattern/pattern.h"
24 #include "core/components_ng/pattern/rating/rating_accessibility_property.h"
25 #include "core/components_ng/pattern/rating/rating_event_hub.h"
26 #include "core/components_ng/pattern/rating/rating_layout_algorithm.h"
27 #include "core/components_ng/pattern/rating/rating_layout_property.h"
28 #include "core/components_ng/pattern/rating/rating_model_ng.h"
29 #include "core/components_ng/pattern/rating/rating_modifier.h"
30 #include "core/components_ng/pattern/rating/rating_render_property.h"
31 #include "core/components_ng/render/canvas_image.h"
32 
33 namespace OHOS::Ace::NG {
34 class InspectorFilter;
35 
36 
37 class RatingPattern : public Pattern {
38     DECLARE_ACE_TYPE(RatingPattern, Pattern);
39 
40 public:
41     RatingPattern() = default;
42     ~RatingPattern() override = default;
43 
44     RefPtr<NodePaintMethod> CreateNodePaintMethod() override;
45 
CreateLayoutProperty()46     RefPtr<LayoutProperty> CreateLayoutProperty() override
47     {
48         return MakeRefPtr<RatingLayoutProperty>();
49     }
50 
CreateLayoutAlgorithm()51     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
52     {
53         return MakeRefPtr<RatingLayoutAlgorithm>(
54             foregroundImageLoadingCtx_, secondaryImageLoadingCtx_, backgroundImageLoadingCtx_);
55     }
56 
CreatePaintProperty()57     RefPtr<PaintProperty> CreatePaintProperty() override
58     {
59         return MakeRefPtr<RatingRenderProperty>();
60     }
61 
CreateEventHub()62     RefPtr<EventHub> CreateEventHub() override
63     {
64         return MakeRefPtr<RatingEventHub>();
65     }
66 
CreateAccessibilityProperty()67     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
68     {
69         return MakeRefPtr<RatingAccessibilityProperty>();
70     }
71 
72     // Called on main thread to check if need rerender of the content.
73     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
74 
GetFocusPattern()75     FocusPattern GetFocusPattern() const override
76     {
77         FocusPaintParam focusPaintParams;
78         focusPaintParams.SetPaintWidth(themeBorderWidth_);
79         return { FocusType::NODE, true, FocusStyleType::CUSTOM_REGION, focusPaintParams };
80     }
81 
SetBuilderFunc(RatingMakeCallback && makeFunc)82     void SetBuilderFunc(RatingMakeCallback&& makeFunc)
83     {
84         if (makeFunc == nullptr) {
85             makeFunc_ = std::nullopt;
86             contentModifierNode_ = nullptr;
87             OnModifyDone();
88             return;
89         }
90         makeFunc_ = std::move(makeFunc);
91     }
92 
UseContentModifier()93     bool UseContentModifier()
94     {
95         return contentModifierNode_ != nullptr;
96     }
97 
98     void SetRatingScore(double value);
99 
100 private:
101     void OnAttachToFrameNode() override;
102     void UpdateRatingScore(double ratingScore);
103     void MarkDirtyNode(const PropertyChangeFlag& flag);
104     void OnModifyDone() override;
105     void ConstrainsRatingScore(const RefPtr<RatingLayoutProperty>& layoutProperty);
106     void LoadForeground(const RefPtr<RatingLayoutProperty>& layoutProperty, const RefPtr<RatingTheme>& ratingTheme,
107         const RefPtr<IconTheme>& iconTheme);
108     void LoadSecondary(const RefPtr<RatingLayoutProperty>& layoutProperty, const RefPtr<RatingTheme>& ratingTheme,
109         const RefPtr<IconTheme>& iconTheme);
110     void LoadBackground(const RefPtr<RatingLayoutProperty>& layoutProperty, const RefPtr<RatingTheme>& ratingTheme,
111         const RefPtr<IconTheme>& iconTheme);
112     void UpdatePaintConfig();
113     void PrepareAnimation(const RefPtr<CanvasImage>& image);
114     void SetRedrawCallback(const RefPtr<CanvasImage>& image);
115     void OnImageDataReady(int32_t imageFlag);
116     void OnImageLoadSuccess(int32_t imageFlag);
117     void CheckImageInfoHasChangedOrNot(
118         int32_t imageFlag, const ImageSourceInfo& sourceInfo, const std::string& lifeCycleTag);
119 
120     // Init pan recognizer to update render when drag updates, fire change event when drag ends.
121     void InitPanEvent(const RefPtr<GestureEventHub>& gestureHub);
122 
123     // Init touch event, show press effect when touch down, update render when touch up.
124     void InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub);
125 
126     // Init touch event, update render when click.
127     void InitClickEvent(const RefPtr<GestureEventHub>& gestureHub);
128 
129     // Init key event
130     void InitOnKeyEvent(const RefPtr<FocusHub>& focusHub);
131     void OnBlurEvent();
132     bool OnKeyEvent(const KeyEvent& event);
133     void PaintFocusState(double ratingScore);
134     void GetInnerFocusPaintRect(RoundRect& paintRect);
135 
136     // Init mouse event
137     void InitMouseEvent();
138     void HandleMouseEvent(MouseInfo& info);
139     void HandleHoverEvent(bool isHover);
140 
141     void HandleDragUpdate(const GestureEvent& info);
142     void HandleDragEnd();
143     void HandleTouchDown(const Offset& localPosition);
144     void HandleTouchUp();
145     void HandleClick(const GestureEvent& info);
146     void FireChangeEvent();
147     void RecalculatedRatingScoreBasedOnEventPoint(double eventPointX, bool isDrag);
148     bool IsIndicator();
149     void FireBuilder();
150     RefPtr<FrameNode> BuildContentModifierNode();
151 
152     std::optional<RatingMakeCallback> makeFunc_;
153     RefPtr<FrameNode> contentModifierNode_;
154 
155     RefPtr<PanEvent> panEvent_;
156     RefPtr<TouchEventImpl> touchEvent_;
157     RefPtr<ClickEvent> clickEvent_;
158     RefPtr<InputEvent> hoverEvent_;
159     RefPtr<InputEvent> mouseEvent_;
160 
161     DataReadyNotifyTask CreateDataReadyCallback(int32_t imageFlag);
162     LoadSuccessNotifyTask CreateLoadSuccessCallback(int32_t imageFlag);
163     LoadFailNotifyTask CreateLoadFailCallback(int32_t imageFlag);
164 
165     RefPtr<ImageLoadingContext> foregroundImageLoadingCtx_;
166     RefPtr<ImageLoadingContext> secondaryImageLoadingCtx_;
167     RefPtr<ImageLoadingContext> backgroundImageLoadingCtx_;
168 
169     RefPtr<RatingModifier> ratingModifier_;
170     RefPtr<CanvasImage> foregroundImageCanvas_;
171     RefPtr<CanvasImage> secondaryImageCanvas_;
172     RefPtr<CanvasImage> backgroundImageCanvas_;
173     ImagePaintConfig foregroundConfig_;
174     ImagePaintConfig secondaryConfig_;
175     ImagePaintConfig backgroundConfig_;
176     uint32_t imageReadyStateCode_ = 0;
177     uint32_t imageSuccessStateCode_ = 0;
178     bool hasInit_ = false;
179     bool isHover_ = false;
180     bool isfocus_ = false;
181     double focusRatingScore_ = 0.0;
182     double lastRatingScore_ = 0.0;
183     RatingModifier::RatingAnimationType state_;
184     float singleStarWidth_ = .0f;
185     int32_t themeStarNum_ = OHOS::Ace::DEFAULT_RATING_STAR_NUM;
186     double themeStepSize_ = OHOS::Ace::DEFAULT_RATING_STEP_SIZE;
187     double themeRatingScore_ = OHOS::Ace::DEFAULT_RATING_SCORE;
188     Dimension themeBorderWidth_ = 0.0_vp;
189 
190     bool isForegroundImageInfoFromTheme_ = false;
191     bool isSecondaryImageInfoFromTheme_ = false;
192     bool isBackgroundImageInfoFromTheme_ = false;
193     // get XTS inspector value
194     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
195     ACE_DISALLOW_COPY_AND_MOVE(RatingPattern);
196 };
197 
198 } // namespace OHOS::Ace::NG
199 
200 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_RATING_RATING_PATTERN_H
201