• 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_BUTTON_TOGGLE_BUTTON_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_PATTERN_H
18 
19 #include <optional>
20 
21 #include "core/components/common/properties/color.h"
22 #include "core/components_ng/event/event_hub.h"
23 #include "core/components_ng/pattern/button/button_pattern.h"
24 #include "core/components_ng/pattern/button/toggle_button_accessibility_property.h"
25 #include "core/components_ng/pattern/button/toggle_button_event_hub.h"
26 #include "core/components_ng/pattern/button/toggle_button_paint_property.h"
27 #include "core/components_ng/pattern/toggle/toggle_model_ng.h"
28 #include "core/components/toggle/toggle_theme.h"
29 
30 namespace OHOS::Ace::NG {
31 class ToggleButtonPattern : public ButtonPattern {
32     DECLARE_ACE_TYPE(ToggleButtonPattern, ButtonPattern);
33 
34 public:
35     ToggleButtonPattern() = default;
36 
37     ~ToggleButtonPattern() override = default;
38 
IsAtomicNode()39     bool IsAtomicNode() const override
40     {
41         return false;
42     }
43 
UseContentModifier()44     bool UseContentModifier() const override
45     {
46         return contentModifierNode_ != nullptr;
47     }
48 
CreateEventHub()49     RefPtr<EventHub> CreateEventHub() override
50     {
51         return MakeRefPtr<ToggleButtonEventHub>();
52     }
53 
CreatePaintProperty()54     RefPtr<PaintProperty> CreatePaintProperty() override
55     {
56         return MakeRefPtr<ToggleButtonPaintProperty>();
57     }
58 
CreateAccessibilityProperty()59     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
60     {
61         return MakeRefPtr<ToggleButtonAccessibilityProperty>();
62     }
63 
GetTouchListener()64     RefPtr<TouchEventImpl>& GetTouchListener()
65     {
66         return touchListener_;
67     }
68 
69     void SetToggleBuilderFunc(SwitchMakeCallback&& toggleMakeFunc);
70 
GetBuilderId()71     int32_t GetBuilderId() const override
72     {
73         return nodeId_;
74     }
75 
SetIsFocus(bool focus)76     void SetIsFocus(bool focus)
77     {
78         isFocus_ = focus;
79     }
80     std::string ProvideRestoreInfo() override;
81     void OnRestoreInfo(const std::string& restoreInfo) override;
82     void OnClick();
83     void OnColorConfigurationUpdate() override;
84     void MarkIsSelected(bool isSelected);
85     void SetButtonPress(bool value);
86     void ToTreeJson(std::unique_ptr<JsonValue>& json, const InspectorConfig& config) const override;
87     bool OnThemeScopeUpdate(int32_t themeScopeId) override;
88 
89 private:
90     void OnAttachToFrameNode() override;
91     void InitParameters();
92     void OnModifyDone() override;
93     void OnAfterModifyDone() override;
94     void InitClickEvent();
95     void InitButtonAndText();
96     void InitOnKeyEvent();
97     void HandleBlurEvent();
98     void HandleFocusEvent();
99     bool OnKeyEvent(const KeyEvent& event);
100     void SetAccessibilityAction();
101     void UpdateSelectStatus(bool isSelected);
102     void InitTouchEvent();
103     void OnTouchDown();
104     void OnTouchUp();
105     void FireBuilder();
106     void HandleOnOffStyle(bool isOnToOff, bool isFocus);
107     void HandleOverlayStyle();
108     void HandleBorderAndShadow();
109     void HandleFocusStyle();
110     void UpdateButtonStyle();
111     void SetBlurButtonStyle(RefPtr<FrameNode>& textNode, RefPtr<TextLayoutProperty>& textLayoutProperty,
112         bool isOnValueFalse, RefPtr<RenderContext>& renderContext);
113     void SetFocusButtonStyle(RefPtr<FrameNode>& textNode, RefPtr<TextLayoutProperty>& textLayoutProperty,
114         bool isOnValueFalse, RefPtr<RenderContext>& renderContext);
115     void HandleFocusShadowStyle(bool isOffState, RefPtr<RenderContext>& renderContext,
116         Shadow shadowValue, bool isbgColorFocus);
117     void AddIsFocusActiveUpdateEvent();
118     void RemoveIsFocusActiveUpdateEvent();
119     void GetIsTextFade();
120     void UpdateTexOverflow(bool isMarqueeStart, RefPtr<TextLayoutProperty>& textLayoutProperty);
121     void InitHoverEvent();
122     void HandleHoverEvent(bool isHover);
123     void InitEvent();
124     void SetToggleScale(RefPtr<RenderContext>& renderContext);
125 
126     RefPtr<FrameNode> BuildContentModifierNode();
127     std::function<void(bool)> isFocusActiveUpdateEvent_;
128     std::optional<SwitchMakeCallback> toggleMakeFunc_;
129     RefPtr<FrameNode> contentModifierNode_;
130     RefPtr<TouchEventImpl> touchListener_;
131     RefPtr<InputEvent> hoverListener_;
132     int32_t nodeId_ = -1;
133 
134     RefPtr<ClickEvent> clickListener_;
135     RefPtr<ToggleTheme> toggleTheme_;
136     std::optional<bool> isOn_;
137     Color checkedColor_;
138     Color unCheckedColor_;
139     Color backgroundColor_;
140     Dimension textMargin_;
141     Dimension buttonRadius_;
142     Dimension textFontSize_;
143     Color textColor_;
144     bool isShadow_ = false;
145     bool isScale_ = false;
146     bool isTextColor_ = false;
147     bool isCheckedShadow_ = false;
148     bool isbgColorFocus_ = false;
149     bool isFocus_ = false;
150     bool isPress_ = false;
151     bool IsNeedToHandleHoverOpacity();
152     bool isTextFadeOut_ = false;
153     bool isHover_ = false;
154     ACE_DISALLOW_COPY_AND_MOVE(ToggleButtonPattern);
155 };
156 } // namespace OHOS::Ace::NG
157 
158 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_BUTTON_TOGGLE_BUTTON_PATTERN_H
159