• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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_BUTTON_BUTTON_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/memory/referenced.h"
22 #include "base/utils/utils.h"
23 #include "core/components/button/button_theme.h"
24 #include "core/components_ng/event/event_hub.h"
25 #include "core/components_ng/event/focus_hub.h"
26 #include "core/components_ng/pattern/button/button_event_hub.h"
27 #include "core/components_ng/pattern/button/button_layout_algorithm.h"
28 #include "core/components_ng/pattern/button/button_layout_property.h"
29 #include "core/components_ng/pattern/button/button_model_ng.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_ng/pattern/text/text_layout_property.h"
32 namespace OHOS::Ace::NG {
33 struct InspectorConfig;
34 class InspectorFilter;
35 enum class ComponentButtonType { POPUP, BUTTON, STEPPER, NAVIGATION };
36 class ButtonPattern : public Pattern {
37     DECLARE_ACE_TYPE(ButtonPattern, Pattern);
38 
39 public:
40     ButtonPattern() = default;
41 
42     ~ButtonPattern() override = default;
43 
SetApplyShadow(bool shadow)44     void SetApplyShadow(bool shadow)
45     {
46         isApplyShadow_ = shadow;
47     }
48 
IsAtomicNode()49     bool IsAtomicNode() const override
50     {
51         return false;
52     }
53 
CreateEventHub()54     RefPtr<EventHub> CreateEventHub() override
55     {
56         return MakeRefPtr<ButtonEventHub>();
57     }
58 
CreateLayoutAlgorithm()59     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
60     {
61         return MakeRefPtr<ButtonLayoutAlgorithm>();
62     }
63 
CreateLayoutProperty()64     RefPtr<LayoutProperty> CreateLayoutProperty() override
65     {
66         return MakeRefPtr<ButtonLayoutProperty>();
67     }
68 
SetClickedColor(const Color & color)69     void SetClickedColor(const Color& color)
70     {
71         clickedColor_ = color;
72     }
73 
SetBlendColor(const std::optional<Color> & blendClickColor,const std::optional<Color> & blendHoverColor)74     void SetBlendColor(const std::optional<Color>& blendClickColor, const std::optional<Color>& blendHoverColor)
75     {
76         blendClickColor_ = blendClickColor;
77         blendHoverColor_ = blendHoverColor;
78     }
79 
SetFocusBorderColor(const Color & color)80     void SetFocusBorderColor(const Color& color)
81     {
82         focusBorderColor_ = color;
83     }
84 
SetFocusBorderWidth(const Dimension & width)85     void SetFocusBorderWidth(const Dimension& width)
86     {
87         focusBorderWidth_ = width;
88     }
89 
setComponentButtonType(const ComponentButtonType & buttonType)90     void setComponentButtonType(const ComponentButtonType& buttonType)
91     {
92         buttonType_ = buttonType;
93     }
94 
95     void ToTreeJson(std::unique_ptr<JsonValue>& json, const InspectorConfig& config) const override;
96 
SetLocalLocation(const Offset & localLocation)97     void SetLocalLocation(const Offset& localLocation)
98     {
99         localLocation_ = localLocation;
100     }
101 
GetLocalLocation()102     const Offset& GetLocalLocation() const
103     {
104         return localLocation_;
105     }
106 
SetInHover(bool inHover)107     void SetInHover(bool inHover)
108     {
109         isInHover_ = inHover;
110     }
111 
GetIsInHover()112     bool GetIsInHover() const
113     {
114         return isInHover_;
115     }
116 
IsEnableMatchParent()117     bool IsEnableMatchParent() override
118     {
119         return true;
120     }
121 
IsEnableFix()122     bool IsEnableFix() override
123     {
124         return true;
125     }
126 
IsEnableChildrenMatchParent()127     bool IsEnableChildrenMatchParent() override
128     {
129         return true;
130     }
131 
GetHoverListener()132     RefPtr<InputEvent>& GetHoverListener()
133     {
134         return hoverListener_;
135     }
136 
GetTouchListener()137     std::function<void(UIState)>& GetTouchListener()
138     {
139         return touchListener_;
140     }
141 
142     void SetBuilderFunc(ButtonMakeCallback&& makeFunc);
143 
GetBuilderId()144     virtual int32_t GetBuilderId() const
145     {
146         return nodeId_;
147     }
148 
149     void SetButtonPress(double xPos, double yPos);
150 
UseContentModifier()151     virtual bool UseContentModifier() const
152     {
153         return contentModifierNode_ != nullptr;
154     }
155 
156     void OnColorConfigurationUpdate() override;
157 
SetSkipColorConfigurationUpdate()158     void SetSkipColorConfigurationUpdate()
159     {
160         isColorUpdateFlag_ = true;
161     }
162 
SetPreFrameSize(const SizeF & frameSize)163     void SetPreFrameSize(const SizeF& frameSize)
164     {
165         preFrameSize_.SetSizeT(frameSize);
166     }
167 
GetPreFrameSize()168     const SizeF& GetPreFrameSize() const
169     {
170         return preFrameSize_;
171     }
172 
SetHasCustomPadding(bool hasCustomPadding)173     void SetHasCustomPadding(bool hasCustomPadding)
174     {
175         hasCustomPadding_ = hasCustomPadding;
176     }
177 
GetHasCustomPadding()178     bool GetHasCustomPadding()
179     {
180         return hasCustomPadding_;
181     }
182 
SetIsFocus(bool isFocus)183     void SetIsFocus(bool isFocus)
184     {
185         isFocus_ = isFocus;
186     }
187 
188     FocusPattern GetFocusPattern() const override;
189 
190     bool IsNeedAdjustByAspectRatio() override;
191 
192     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
193 
194     void ToJsonValueAttribute(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
195 
196     static std::string ConvertButtonRoleToString(ButtonRole buttonRole);
197 
198     static std::string ConvertButtonTypeToString(ButtonType buttonType);
199 
200     static std::string ConvertButtonStyleToString(ButtonStyleMode buttonStyle);
201 
202     static std::string ConvertControlSizeToString(ControlSize controlSize);
203 
204     bool IsDynamicSwitchButtonStyle(const BorderColorProperty& color, RefPtr<ButtonTheme>& buttonTheme);
205 
206     bool IsDynamicSwitchButtonStyle(const BorderWidthProperty& width, RefPtr<ButtonTheme>& buttonTheme);
207 
208     void DumpInfo() override;
209 
210     void DumpSubInfo(RefPtr<ButtonLayoutProperty> layoutProperty);
211 
212     void HandleBackgroundColor();
213 
214     void UpdateComponentColor(const Color& color, const ButtonColorType buttonColorType);
215 
216     void UpdateComponentString(const std::string& value, const ButtonStringType buttonStringType);
217 
218     void UpdateComponentDimension(const CalcDimension value, const ButtonDimensionType buttonDimensionType);
219 
220     void UpdateComponentDouble(const double value, const ButtonDoubleType buttonDoubleType);
221 
222     void UpdateComponentFamilies(const std::vector<std::string>& value, const ButtonStringType buttonStringType);
223 
224     std::string VectorToString(const std::vector<std::string>& vec, const std::string& delimiter = " ");
225 
226     std::vector<std::string> StringToVector(const std::string& str, char delimiter = ' ');
227 
228 protected:
229     void OnModifyDone() override;
230     void OnAfterModifyDone() override;
231     void InitTouchEvent();
232     void InitHoverEvent();
233     void HandlePressedStyle();
234     void HandleNormalStyle();
235     void HandleHoverEvent(bool isHover);
236     void UpdateTexOverflow(bool isMarqueeStart);
237     void HandleEnabled();
238     void InitButtonLabel();
239     void CheckLocalizedBorderRadiuses();
240     Color GetColorFromType(const RefPtr<ButtonTheme>& theme, const int32_t& type);
241     void AnimateTouchAndHover(RefPtr<RenderContext>& renderContext, int32_t typeFrom, int32_t typeTo, int32_t duration,
242         const RefPtr<Curve>& curve);
243     std::optional<Color> clickedColor_ = std::nullopt;
244 
245 private:
246     static void UpdateTextLayoutProperty(
247         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
248     static void UpdateTextStyle(
249         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
250     static bool NeedAgingUpdateText(RefPtr<ButtonLayoutProperty>& layoutProperty);
251     bool IsNeedToHandleHoverOpacity();
252     static void UpdateTextFontScale(
253         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
254     void OnFontScaleConfigurationUpdate() override;
255     void InitButtonAlphaOffscreen();
256     Color backgroundColor_;
257     Color focusBorderColor_;
258     Color themeBgColor_;
259     Color themeTextColor_;
260     ComponentButtonType buttonType_ = ComponentButtonType::BUTTON;
261     void FireBuilder();
262     RefPtr<FrameNode> BuildContentModifierNode();
263     GestureEventFunc tapEventFunc_;
264     std::optional<ButtonMakeCallback> makeFunc_;
265     RefPtr<FrameNode> contentModifierNode_;
266     std::optional<GestureEventFunc> clickEventFunc_;
267     int32_t nodeId_ = -1;
268     std::function<void(UIState)> touchListener_;
269     RefPtr<InputEvent> hoverListener_;
270     bool isHover_ = false;
271     bool isFocus_ = false;
272     bool isPress_ = false;
273     bool isApplyShadow_ = true;
274     bool isLayoutUpdate_ = false;
275     bool isInitButtonAlphaOffscreen_ = false;
276 
277     bool isInHover_ = false;
278     Offset localLocation_;
279     Dimension focusBorderWidth_;
280 
281     std::optional<Color> blendClickColor_ = std::nullopt;
282     std::optional<Color> blendHoverColor_ = std::nullopt;
283 
284     bool isTextFadeOut_ = false;
285     bool isColorUpdateFlag_ = false;
286     SizeF preFrameSize_;
287     bool hasCustomPadding_ = false;
288     ACE_DISALLOW_COPY_AND_MOVE(ButtonPattern);
289     bool focusTextColorModify_ = false;
290     bool bgColorModify_ = false;
291     bool scaleModify_ = false;
292     bool shadowModify_ = false;
293     std::function<void(bool)> isFocusActiveUpdateEvent_;
294 
295     void HandleBorderAndShadow();
296     void HandleFocusStatusStyle();
297     void HandleFocusStyleTask();
298     void HandleBlurStyleTask();
299     void UpdateButtonStyle();
300     void SetFocusButtonStyle(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme,
301         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<FrameNode>& textNode);
302     void SetBlurButtonStyle(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme,
303         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<FrameNode>& textNode);
304     void AddIsFocusActiveUpdateEvent();
305     void RemoveIsFocusActiveUpdateEvent();
306     void HandleShadowStyle(ButtonStyleMode buttonStyle, ShadowStyle shadowStyle,
307         RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme);
308     Shadow GetShadowFromTheme(ShadowStyle shadowStyle);
309     void HandleFocusActiveStyle();
310     void SetButtonScale(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme);
311 };
312 } // namespace OHOS::Ace::NG
313 
314 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H
315