• 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_OPTION_OPTION_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H
18 
19 #include <optional>
20 
21 #include "base/memory/referenced.h"
22 #include "core/components/select/select_theme.h"
23 #include "core/components/text/text_theme.h"
24 #include "core/components_ng/base/frame_node.h"
25 #include "core/components_ng/pattern/option/option_accessibility_property.h"
26 #include "core/components_ng/pattern/option/option_event_hub.h"
27 #include "core/components_ng/pattern/option/option_layout_algorithm.h"
28 #include "core/components_ng/pattern/option/option_paint_method.h"
29 #include "core/components_ng/pattern/option/option_paint_property.h"
30 #include "core/components_ng/pattern/pattern.h"
31 #include "core/components_ng/render/paint_property.h"
32 #include "core/pipeline_ng/ui_task_scheduler.h"
33 
34 namespace OHOS::Ace::NG {
35 class OptionPattern : public Pattern {
36     DECLARE_ACE_TYPE(OptionPattern, Pattern);
37 
38 public:
OptionPattern(int index)39     explicit OptionPattern(int index) : index_(index) {}
40     ~OptionPattern() override = default;
41 
CreateNodePaintMethod()42     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
43     {
44         return MakeRefPtr<OptionPaintMethod>();
45     }
46 
CreatePaintProperty()47     RefPtr<PaintProperty> CreatePaintProperty() override
48     {
49         return MakeRefPtr<OptionPaintProperty>();
50     }
51 
CreateEventHub()52     RefPtr<EventHub> CreateEventHub() override
53     {
54         return MakeRefPtr<OptionEventHub>();
55     }
56 
CreateAccessibilityProperty()57     RefPtr<AccessibilityProperty> CreateAccessibilityProperty() override
58     {
59         return MakeRefPtr<OptionAccessibilityProperty>();
60     }
61 
CreateLayoutAlgorithm()62     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
63     {
64         return MakeRefPtr<OptionLayoutAlgorithm>();
65     }
66 
IsAtomicNode()67     bool IsAtomicNode() const override
68     {
69         return false;
70     }
71 
SetTextNode(const RefPtr<FrameNode> & text)72     void SetTextNode(const RefPtr<FrameNode>& text)
73     {
74         text_ = text;
75     }
76 
SetIconNode(const RefPtr<FrameNode> & icon)77     void SetIconNode(const RefPtr<FrameNode>& icon)
78     {
79         icon_ = icon;
80     }
81 
82     void SetBgColor(const Color& color);
83     // set font props
84     void SetFontSize(const Dimension& value);
85     void SetItalicFontStyle(const Ace::FontStyle& value);
86     void SetFontWeight(const FontWeight& value);
87     void SetFontFamily(const std::vector<std::string>& value);
88     void SetFontColor(const Color& color);
89 
90     Color GetBgColor();
91     // get font props
92     Dimension GetFontSize();
93     Ace::FontStyle GetItalicFontStyle();
94     FontWeight GetFontWeight();
95     std::vector<std::string> GetFontFamily();
96     Color GetFontColor();
97 
98     std::string GetText();
99 
100     // XTS inspector functions
101     std::string InspectorGetFont();
102 
SetIcon(const std::string & src)103     void SetIcon(const std::string& src)
104     {
105         iconSrc_ = src;
106     }
107 
GetIcon()108     const std::string& GetIcon()
109     {
110         return iconSrc_;
111     }
112 
GetFocusPattern()113     FocusPattern GetFocusPattern() const override
114     {
115         return { FocusType::NODE, true, FocusStyleType::INNER_BORDER };
116     }
117 
118     void UpdateNextNodeDivider(bool needDivider);
119 
SetBgBlendColor(const Color & color)120     void SetBgBlendColor(const Color& color)
121     {
122         bgBlendColor_ = color;
123     }
124 
GetBgBlendColor()125     Color GetBgBlendColor() const
126     {
127         return bgBlendColor_;
128     }
129 
SetIsHover(bool isHover)130     void SetIsHover(bool isHover)
131     {
132         isHover_ = isHover;
133     }
134 
IsHover()135     bool IsHover() const
136     {
137         return isHover_;
138     }
139 
140     void PlayBgColorAnimation(bool isHoverChange = true);
141 
142     void UpdateText(const std::string& content);
143     void UpdateIcon(const std::string& src);
144 
SetMenu(const WeakPtr<FrameNode> & menuWeak)145     void SetMenu(const WeakPtr<FrameNode>& menuWeak)
146     {
147         menuWeak_ = menuWeak;
148     }
149 
GetMenu()150     const WeakPtr<FrameNode>& GetMenu() const
151     {
152         return menuWeak_;
153     }
154 
155 private:
156     void OnAttachToFrameNode() override;
157     void OnModifyDone() override;
158     // make render after measure and layout
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)159     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override
160     {
161         return !(config.skipMeasure && config.skipLayout);
162     }
163 
164     // register option's callback
165     void RegisterOnClick();
166 
167     void RegisterOnTouch();
168     void RegisterOnHover();
169     void RegisterOnKeyEvent();
170 
171     // change option paint props on press
172     void OnPress(const TouchEventInfo& info);
173     void OnHover(bool isHover);
174     bool OnKeyEvent(const KeyEvent& event);
175 
176     void OnSelectProcess();
177     void SetAccessibilityAction();
178 
179     std::optional<Color> bgColor_;
180 
181     // src of icon image, used in XTS inspector
182     std::string iconSrc_;
183     WeakPtr<FrameNode> menuWeak_;
184     RefPtr<FrameNode> text_;
185     RefPtr<FrameNode> icon_;
186     RefPtr<TextTheme> textTheme_;
187     RefPtr<SelectTheme> selectTheme_;
188     // this option node's index in the menu
189     int index_ = -1;
190 
191     Color bgBlendColor_ = Color::TRANSPARENT;
192     bool isHover_ = false;
193 
194     ACE_DISALLOW_COPY_AND_MOVE(OptionPattern);
195 };
196 } // namespace OHOS::Ace::NG
197 
198 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H
199