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 
GetTextNode()77     RefPtr<FrameNode> GetTextNode() const
78     {
79         return text_;
80     }
81 
SetIconNode(const RefPtr<FrameNode> & icon)82     void SetIconNode(const RefPtr<FrameNode>& icon)
83     {
84         icon_ = icon;
85     }
86 
SetPasteButton(const RefPtr<FrameNode> & pasteButton)87     void SetPasteButton(const RefPtr<FrameNode>& pasteButton)
88     {
89         pasteButton_ = pasteButton;
90     }
91 
92     void SetBgColor(const Color& color);
93     // set font props
94     void SetFontSize(const Dimension& value);
95     void SetItalicFontStyle(const Ace::FontStyle& value);
96     void SetFontWeight(const FontWeight& value);
97     void SetFontFamily(const std::vector<std::string>& value);
98     void SetFontColor(const Color& color);
99 
100     Color GetBgColor();
101     // get font props
102     Dimension GetFontSize();
103     Ace::FontStyle GetItalicFontStyle();
104     FontWeight GetFontWeight();
105     std::vector<std::string> GetFontFamily();
106     Color GetFontColor();
107 
108     std::string GetText();
109 
110     // XTS inspector functions
111     std::string InspectorGetFont();
112 
113     float GetSelectOptionWidth();
114 
SetIcon(const std::string & src)115     void SetIcon(const std::string& src)
116     {
117         iconSrc_ = src;
118     }
119 
GetIcon()120     const std::string& GetIcon()
121     {
122         return iconSrc_;
123     }
124 
GetFocusPattern()125     FocusPattern GetFocusPattern() const override
126     {
127         return { FocusType::NODE, true, FocusStyleType::INNER_BORDER };
128     }
129 
130     void UpdateNextNodeDivider(bool needDivider);
131 
SetBgBlendColor(const Color & color)132     void SetBgBlendColor(const Color& color)
133     {
134         bgBlendColor_ = color;
135     }
136 
GetBgBlendColor()137     Color GetBgBlendColor() const
138     {
139         return bgBlendColor_;
140     }
141 
SetIsHover(bool isHover)142     void SetIsHover(bool isHover)
143     {
144         isHover_ = isHover;
145     }
146 
IsHover()147     bool IsHover() const
148     {
149         return isHover_;
150     }
151 
152     void PlayBgColorAnimation(bool isHoverChange = true);
153 
154     void UpdateText(const std::string& content);
155 
156     void UpdateIcon(const std::string& src, const std::function<void(WeakPtr<NG::FrameNode>)> symbolIcon);
157 
SetMenu(const WeakPtr<FrameNode> & menuWeak)158     void SetMenu(const WeakPtr<FrameNode>& menuWeak)
159     {
160         menuWeak_ = menuWeak;
161     }
162 
GetMenu()163     const WeakPtr<FrameNode>& GetMenu() const
164     {
165         return menuWeak_;
166     }
167 
SetIsWidthModifiedBySelect(bool isModified)168     void SetIsWidthModifiedBySelect(bool isModified)
169     {
170         isWidthModifiedBySelect_ = isModified;
171     }
172 
IsWidthModifiedBySelect()173     bool IsWidthModifiedBySelect() const
174     {
175         return isWidthModifiedBySelect_;
176     }
177 
SetIsSelectOption(bool isSelect)178     void SetIsSelectOption(bool isSelect)
179     {
180         isSelectOption_ = isSelect;
181     }
182 
IsSelectOption()183     bool IsSelectOption() const
184     {
185         return isSelectOption_;
186     }
187 
SetHasOptionWidth(bool hasOptionWidth)188     void SetHasOptionWidth(bool hasOptionWidth)
189     {
190         hasOptionWidth_ = hasOptionWidth;
191     }
192 
GetHasOptionWidth()193     bool GetHasOptionWidth()
194     {
195         return hasOptionWidth_;
196     }
197 
GetSelectTheme()198     const RefPtr<SelectTheme>& GetSelectTheme() const
199     {
200         return selectTheme_;
201     }
202 
SetSelected(bool selected)203     void SetSelected(bool selected)
204     {
205         selected_ = selected;
206     }
207 
IsSelected()208     bool IsSelected()
209     {
210         return selected_;
211     }
212 
213 private:
214     void OnAttachToFrameNode() override;
215     void OnModifyDone() override;
216     bool UseDefaultThemeIcon(const ImageSourceInfo& imageSourceInfo);
217     void UpdateIconSrc();
218     // make render after measure and layout
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)219     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override
220     {
221         return !(config.skipMeasure && config.skipLayout);
222     }
223 
224     // register option's callback
225     void RegisterOnClick();
226 
227     void RegisterOnTouch();
228     void RegisterOnHover();
229     void RegisterOnKeyEvent();
230 
231     // change option paint props on press
232     void OnPress(const TouchEventInfo& info);
233     void OnHover(bool isHover);
234     bool OnKeyEvent(const KeyEvent& event);
235 
236     void OnSelectProcess();
237     bool UpdateOptionFocus(KeyCode code);
238     void SetAccessibilityAction();
239     void UpdatePasteFontColor(const Color& fontColor);
240 
241     std::optional<Color> bgColor_;
242 
243     // src of icon image, used in XTS inspector
244     std::string iconSrc_;
245     WeakPtr<FrameNode> menuWeak_;
246     RefPtr<FrameNode> text_;
247     RefPtr<FrameNode> icon_;
248     RefPtr<FrameNode> pasteButton_;
249     RefPtr<TextTheme> textTheme_;
250     RefPtr<SelectTheme> selectTheme_;
251     // this option node's index in the menu
252     int index_ = -1;
253 
254     Color bgBlendColor_ = Color::TRANSPARENT;
255     bool isHover_ = false;
256     bool isWidthModifiedBySelect_ = false;
257     bool isSelectOption_ = false;
258     bool selected_ = false;
259     bool hasOptionWidth_ = false;
260 
261     ACE_DISALLOW_COPY_AND_MOVE(OptionPattern);
262 };
263 } // namespace OHOS::Ace::NG
264 
265 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_OPTION_OPTION_PATTERN_H
266