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_RENDER_SIDE_BAR_CONTAINER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RENDER_SIDE_BAR_CONTAINER_H
18 
19 #include "base/geometry/dimension.h"
20 #include "base/memory/referenced.h"
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/stack/render_stack.h"
23 #include "core/components/side_bar/side_bar_animation_controller.h"
24 #include "core/components/side_bar/side_bar_container_component.h"
25 #include "core/gestures/drag_recognizer.h"
26 #include "core/gestures/click_recognizer.h"
27 
28 namespace OHOS::Ace {
29 
30 constexpr Dimension DEFAUTL_MAX_SIDEBAR_WIDTH = 280.0_vp;
31 constexpr Dimension DEFAUTL_MIN_SIDEBAR_WIDTH = 200.0_vp;
32 constexpr Dimension DEFAUTL_SIDEBAR_WIDTH = 200.0_vp;
33 
34 
35 class RenderSideBarContainer : public RenderStack {
36     DECLARE_ACE_TYPE(RenderSideBarContainer, RenderStack);
37 public:
38     static RefPtr<RenderNode> Create();
39     void Update(const RefPtr<Component>& component) override;
40     void OnStatusChanged(RenderStatus renderStatus) override;
41     void UpdateElementPosition(double offset);
42     double GetSidebarWidth() const;
43     double GetSlidePosition() const;
44     bool TouchTest(const Point& globalPoint, const Point& parentLocalPoint,
45         const TouchRestrict& touchRestrict, TouchTestResult& result) override;
46 
47     void PerformLayout() override;
48 
GetShowSideBarContainer()49     bool GetShowSideBarContainer() const
50     {
51         return showSideBar_;
52     }
53 
GetShowControlButton()54     bool GetShowControlButton() const
55     {
56         return showControlButton_;
57     }
58 
GetStyle()59     std::string GetStyle() const
60     {
61         return style_;
62     }
63 
GetShowIcon()64     std::string GetShowIcon() const
65     {
66         return iconShow_;
67     }
68 
GetHiddenIcon()69     std::string GetHiddenIcon() const
70     {
71         return iconHidden_;
72     }
73 
GetSwitchIcon()74     std::string GetSwitchIcon() const
75     {
76         return iconSwitch_;
77     }
78 
GetButtonLeft()79     double GetButtonLeft() const
80     {
81         return buttonLeft_;
82     }
83 
GetButtonTop()84     double GetButtonTop() const
85     {
86         return buttonTop_;
87     }
88 
GetButtonWidth()89     double GetButtonWidth() const
90     {
91         return buttonWidth_;
92     }
93 
GetButtonHeight()94     double GetButtonHeight() const
95     {
96         return buttonHeight_;
97     }
98 
GetSideBarWidth()99     const Dimension& GetSideBarWidth() const
100     {
101         return sidebarWidth_;
102     }
103 
GetSideBarMinWidth()104     const Dimension& GetSideBarMinWidth() const
105     {
106         return minSidebarWidth_;
107     }
108 
GetSideBarMaxWidth()109     const Dimension& GetSideBarMaxWidth() const
110     {
111         return maxSidebarWidth_;
112     }
113 
GetSideBarPosition()114     SideBarPosition GetSideBarPosition() const
115     {
116         return sideBarPosition_;
117     }
118 
GetTouchRectList()119     const std::vector<Rect>& GetTouchRectList() override
120     {
121         touchRectList_.clear();
122         touchRectList_.emplace_back(GetPaintRect());
123         return touchRectList_;
124     }
125 
126 private:
127     void DoSideBarAnimation();
128     void SetChildrenStatus() override;
129     void HandleDragStart();
130     void HandleDragUpdate(double xOffset);
131     void HandleDragEnd();
132     void UpdateRenderImage();
133     void InitializeDragAndAnimation();
134     void CorrectWidth(const Dimension& width, const Dimension& minWidth, const Dimension& maxWidth);
135     void Initialize();
136     Dimension ConvertWidthToVp(const Dimension& dimension) const;
137     Dimension ConvertWidthToPercent(const Dimension& dimension) const;
138     void PlaceChildren();
139     void LayoutChildren();
140 
141     RefPtr<GestureRecognizer> dragRecognizer_;
142     RefPtr<SideBarAnimationController> animationController_;
143     RefPtr<SideBarContainerComponent> sideBar_;
144     WeakPtr<RenderNode> renderImage_;
145     SideStatus status_ = SideStatus::SHOW;
146     SideStatus pendingStatus_ = status_;
147 
148     bool isFocus_ = false;
149     bool isAnimation_ = false;
150     bool isInitialized_ = false;
151     bool showControlButton_ = true;
152     bool showSideBar_ = true;
153     std::string style_;
154     std::string iconShow_;
155     std::string iconHidden_;
156     std::string iconSwitch_;
157     double buttonLeft_ = 16.0;
158     double buttonTop_ = 48.0;
159     double buttonWidth_ = 32.0;
160     double buttonHeight_ = 32.0;
161 
162     Dimension sidebarWidth_ = DEFAUTL_SIDEBAR_WIDTH;
163     Dimension minSidebarWidth_ = DEFAUTL_MIN_SIDEBAR_WIDTH;
164     Dimension maxSidebarWidth_ = DEFAUTL_MAX_SIDEBAR_WIDTH;
165     Dimension curPosition_ = -sidebarWidth_;
166     Dimension preSidebarWidth_;
167     Rect exceptRegion_;
168     Dimension customSidebarWidth_;
169     bool autoHide_ = true;
170     SideBarPosition sideBarPosition_ = SideBarPosition::START;
171 };
172 
173 } // namespace OHOS::Ace
174 
175 #endif  // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RENDER_SIDE_BAR_CONTAINER_H
176