• 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         isSetClickedColor_ = true;
73     }
74 
SetBlendColor(const std::optional<Color> & blendClickColor,const std::optional<Color> & blendHoverColor)75     void SetBlendColor(const std::optional<Color>& blendClickColor, const std::optional<Color>& blendHoverColor)
76     {
77         blendClickColor_ = blendClickColor;
78         blendHoverColor_ = blendHoverColor;
79     }
80 
SetFocusBorderColor(const Color & color)81     void SetFocusBorderColor(const Color& color)
82     {
83         focusBorderColor_ = color;
84     }
85 
SetFocusBorderWidth(const Dimension & width)86     void SetFocusBorderWidth(const Dimension& width)
87     {
88         focusBorderWidth_ = width;
89     }
90 
setComponentButtonType(const ComponentButtonType & buttonType)91     void setComponentButtonType(const ComponentButtonType& buttonType)
92     {
93         buttonType_ = buttonType;
94     }
95 
96     void ToTreeJson(std::unique_ptr<JsonValue>& json, const InspectorConfig& config) const override;
97 
SetLocalLocation(const Offset & localLocation)98     void SetLocalLocation(const Offset& localLocation)
99     {
100         localLocation_ = localLocation;
101     }
102 
GetLocalLocation()103     const Offset& GetLocalLocation() const
104     {
105         return localLocation_;
106     }
107 
SetInHover(bool inHover)108     void SetInHover(bool inHover)
109     {
110         isInHover_ = inHover;
111     }
112 
GetIsInHover()113     bool GetIsInHover() const
114     {
115         return isInHover_;
116     }
117 
GetHoverListener()118     RefPtr<InputEvent>& GetHoverListener()
119     {
120         return hoverListener_;
121     }
122 
GetTouchListener()123     RefPtr<TouchEventImpl>& GetTouchListener()
124     {
125         return touchListener_;
126     }
127 
128     void SetBuilderFunc(ButtonMakeCallback&& makeFunc);
129 
GetBuilderId()130     virtual int32_t GetBuilderId() const
131     {
132         return nodeId_;
133     }
134 
135     void SetButtonPress(double xPos, double yPos);
136 
UseContentModifier()137     virtual bool UseContentModifier() const
138     {
139         return contentModifierNode_ != nullptr;
140     }
141 
142     void OnColorConfigurationUpdate() override;
143 
SetSkipColorConfigurationUpdate()144     void SetSkipColorConfigurationUpdate()
145     {
146         isColorUpdateFlag_ = true;
147     }
148 
SetPreFrameSize(const SizeF & frameSize)149     void SetPreFrameSize(const SizeF& frameSize)
150     {
151         preFrameSize_.SetSizeT(frameSize);
152     }
153 
GetPreFrameSize()154     const SizeF& GetPreFrameSize() const
155     {
156         return preFrameSize_;
157     }
158 
SetHasCustomPadding(bool hasCustomPadding)159     void SetHasCustomPadding(bool hasCustomPadding)
160     {
161         hasCustomPadding_ = hasCustomPadding;
162     }
163 
GetHasCustomPadding()164     bool GetHasCustomPadding()
165     {
166         return hasCustomPadding_;
167     }
168 
SetIsFocus(bool isFocus)169     void SetIsFocus(bool isFocus)
170     {
171         isFocus_ = isFocus;
172     }
173 
174     FocusPattern GetFocusPattern() const override;
175 
176     bool IsNeedAdjustByAspectRatio() override;
177 
178     void ToJsonValue(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const override;
179 
180     void ToJsonValueAttribute(std::unique_ptr<JsonValue>& json, const InspectorFilter& filter) const;
181 
182     static std::string ConvertButtonRoleToString(ButtonRole buttonRole);
183 
184     static std::string ConvertButtonTypeToString(ButtonType buttonType);
185 
186     static std::string ConvertButtonStyleToString(ButtonStyleMode buttonStyle);
187 
188     static std::string ConvertControlSizeToString(ControlSize controlSize);
189 
190     bool IsDynamicSwitchButtonStyle(const BorderColorProperty& color, RefPtr<ButtonTheme>& buttonTheme);
191 
192     bool IsDynamicSwitchButtonStyle(const BorderWidthProperty& width, RefPtr<ButtonTheme>& buttonTheme);
193 
194 protected:
195     void OnModifyDone() override;
196     void OnAfterModifyDone() override;
197     void OnAttachToFrameNode() override;
198     void InitTouchEvent();
199     void InitHoverEvent();
200     void OnTouchDown();
201     void OnTouchUp();
202     void HandleHoverEvent(bool isHover);
203     void HandleBackgroundColor();
204     void UpdateTexOverflow(bool isMarqueeStart);
205     void HandleEnabled();
206     void InitButtonLabel();
207     void CheckLocalizedBorderRadiuses();
208     Color GetColorFromType(const RefPtr<ButtonTheme>& theme, const int32_t& type);
209     void AnimateTouchAndHover(RefPtr<RenderContext>& renderContext, int32_t typeFrom, int32_t typeTo, int32_t duration,
210         const RefPtr<Curve>& curve);
211     Color clickedColor_;
212 
213 private:
214     static void UpdateTextLayoutProperty(
215         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
216     static void UpdateTextStyle(
217         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
218     static bool NeedAgingUpdateText(RefPtr<ButtonLayoutProperty>& layoutProperty);
219     bool IsNeedToHandleHoverOpacity();
220     static void UpdateTextFontScale(
221         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<TextLayoutProperty>& textLayoutProperty);
222     void OnFontScaleConfigurationUpdate() override;
223     Color backgroundColor_;
224     Color focusBorderColor_;
225     Color themeBgColor_;
226     Color themeTextColor_;
227     bool isSetClickedColor_ = false;
228     ComponentButtonType buttonType_ = ComponentButtonType::BUTTON;
229     void FireBuilder();
230     RefPtr<FrameNode> BuildContentModifierNode();
231     GestureEventFunc tapEventFunc_;
232     std::optional<ButtonMakeCallback> makeFunc_;
233     RefPtr<FrameNode> contentModifierNode_;
234     std::optional<GestureEventFunc> clickEventFunc_;
235     int32_t nodeId_ = -1;
236     RefPtr<TouchEventImpl> touchListener_;
237     RefPtr<InputEvent> hoverListener_;
238     bool isHover_ = false;
239     bool isFocus_ = false;
240     bool isPress_ = false;
241     bool isApplyShadow_ = true;
242     bool isLayoutUpdate_ = false;
243 
244     bool isInHover_ = false;
245     Offset localLocation_;
246     Dimension focusBorderWidth_;
247 
248     std::optional<Color> blendClickColor_ = std::nullopt;
249     std::optional<Color> blendHoverColor_ = std::nullopt;
250 
251     bool isTextFadeOut_ = false;
252     bool isColorUpdateFlag_ = false;
253     SizeF preFrameSize_;
254     bool hasCustomPadding_ = false;
255     ACE_DISALLOW_COPY_AND_MOVE(ButtonPattern);
256     bool focusTextColorModify_ = false;
257     bool bgColorModify_ = false;
258     bool scaleModify_ = false;
259     bool shadowModify_ = false;
260     std::function<void(bool)> isFocusActiveUpdateEvent_;
261 
262     void HandleBorderAndShadow();
263     void HandleFocusStatusStyle();
264     void HandleFocusStyleTask();
265     void HandleBlurStyleTask();
266     void UpdateButtonStyle();
267     void SetFocusButtonStyle(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme,
268         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<FrameNode>& textNode);
269     void SetBlurButtonStyle(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme,
270         RefPtr<ButtonLayoutProperty>& layoutProperty, RefPtr<FrameNode>& textNode);
271     void AddIsFocusActiveUpdateEvent();
272     void RemoveIsFocusActiveUpdateEvent();
273     void HandleShadowStyle(ButtonStyleMode buttonStyle, ShadowStyle shadowStyle,
274         RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme);
275     Shadow GetShadowFromTheme(ShadowStyle shadowStyle);
276     void HandleFocusActiveStyle();
277     void SetButtonScale(RefPtr<RenderContext>& renderContext, RefPtr<ButtonTheme>& buttonTheme);
278 };
279 } // namespace OHOS::Ace::NG
280 
281 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_BUTTON_PATTERN_H
282