1 /*
2  * Copyright (c) 2021-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_OPTION_RENDER_OPTION_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_OPTION_RENDER_OPTION_H
18 
19 #include "base/utils/system_properties.h"
20 #include "core/components/box/render_box.h"
21 #include "core/components/common/properties/color.h"
22 #include "core/components/image/render_image.h"
23 #include "core/components/select/select_component.h"
24 #include "core/components/slider/render_slider.h"
25 #include "core/components/text/render_text.h"
26 #include "core/components/touch_listener/render_touch_listener.h"
27 #include "core/gestures/click_recognizer.h"
28 #include "core/gestures/raw_recognizer.h"
29 #include "core/pipeline/base/render_node.h"
30 
31 namespace OHOS::Ace {
32 
33 class RenderOption : public RenderNode {
34     DECLARE_ACE_TYPE(RenderOption, RenderNode);
35 
36 public:
37     static RefPtr<RenderNode> Create();
38 
39     ~RenderOption() override;
40 
41     void Update(const RefPtr<Component>& component) override;
42     void PerformLayout() override;
43     void OnPaintFinish() override;
44 
SetFixedWidth(double width)45     void SetFixedWidth(double width)
46     {
47         minWidth_ = width;
48         maxWidth_ = width;
49     }
50 
SetMaxWidth(double width)51     void SetMaxWidth(double width)
52     {
53         maxWidth_ = width;
54     }
55 
56     bool OnBack();
57 
58     void OnClick(bool focus);
59 
60     void OnFocus(bool focus);
61 
62     void OnTouch(bool down);
63 
SetWeakNode(const WeakPtr<FocusNode> & value)64     void SetWeakNode(const WeakPtr<FocusNode>& value)
65     {
66         weakNode_ = value;
67     }
68 
GetSelected()69     bool GetSelected() const
70     {
71         if (!data_) {
72             return false;
73         }
74         return data_->GetSelected();
75     }
76 
77     void UpdateSelfStatus();
78 
79     void ProcessTouchUp(const TouchEventInfo& info);
80     void ProcessTouchDown(const TouchEventInfo& info);
81 
82 protected:
83     void OnTouchTestHit(
84         const Offset& coordinateOffset, const TouchRestrict& touchRestrict, TouchTestResult& result) override;
85     void OnMouseHoverEnterTest() override;
86     void OnMouseHoverExitTest() override;
87     void HandleMouseHoverEvent(const MouseState mouseState) override;
88     bool HandleMouseEvent(const MouseEvent& event) override;
89 
90     void UpdateAccessibilityInfo(Size size, Offset offset, bool isSelected);
91 
92     RefPtr<RenderText> GetRenderText(const RefPtr<RenderNode>& render) const;
93     RefPtr<RenderImage> GetRenderImage(const RefPtr<RenderNode>& render) const;
94 
95     void InitClickEvent();
96     void InitTouchEvent();
97 
98     void LayoutText(const RefPtr<RenderText>& text);
99     void LayoutTextImage(const RefPtr<RenderText>& text, const RefPtr<RenderImage>& image);
100 
101     bool IsRTL() const;
102 
103     void RequestFocus();
104     void GetAllOptions(std::list<RefPtr<RenderOption>>& result, const RefPtr<RenderNode>& parent) const;
105     std::list<RefPtr<RenderOption>> GetAllOptions() const;
106     void OnSelect(uint32_t selectIndex);
107     void UpdateStatus();
108     void UpdateDownStatus();
109     void UpdateTvFocusedStatus();
110     void UpdateClickedStatus();
111     void UpdateHoveredStatus();
112     void UpdateSelectedStatus();
113     void UpdateOthersStatus();
114     RefPtr<RenderOption> GetUpOption() const;
115     RefPtr<RenderOption> GetDownOption() const;
116     bool IsNormalStatus() const;
117     void UpdateNormalText();
118     void UpdateSelectedText();
119     void UpdateFocusedText();
120     void UpdateTextColor(bool selected, bool focused);
121     void AdjustScrollPosition();
122     void PlayEventEffectAnimation(const Color& endColor, int32_t duration, bool isHoverExists = false);
123 
124     RefPtr<ClickRecognizer> click_;
125     RefPtr<RawRecognizer> touch_;
126     RefPtr<OptionComponent> data_;
127     WeakPtr<FocusNode> weakNode_;
128 
129     std::function<void()> onClickEvent_;
130 
131     double minWidth_ = 0.0;
132     double maxWidth_ = Size::INFINITE_SIZE;
133     bool focusJumped_ = false;
134     bool needLine_ = true;
135     bool needDrawDividerLine_ = true;
136     Color backColor_ = Color::TRANSPARENT;
137     Color clickedColor_ = Color::TRANSPARENT;
138     Color hoveredColor_ = Color::TRANSPARENT;
139     Color lineColor_;
140     bool isTv_ = SystemProperties::GetDeviceType() == DeviceType::TV;
141     RefPtr<Animator> eventEffectController_;
142     bool hovered_ = false;
143     TouchRegion optionRegion_;
144     Offset firstTouchDownOffset_;
145     Offset firstTouchUpOffset_;
146 };
147 
148 } // namespace OHOS::Ace
149 
150 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_OPTION_RENDER_OPTION_H
151