• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_NG_PATTERNS_SELECT_OVERLAY_SELECT_OVERLAY_PATTERN_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_SELECT_OVERLAY_SELECT_OVERLAY_PATTERN_H
18 
19 #include <utility>
20 
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/memory/referenced.h"
23 #include "base/utils/noncopyable.h"
24 #include "base/utils/utils.h"
25 #include "core/components_ng/event/click_event.h"
26 #include "core/components_ng/pattern/menu/wrapper/menu_wrapper_pattern.h"
27 #include "core/components_ng/pattern/pattern.h"
28 #include "core/components_ng/pattern/select_overlay/select_overlay_content_modifier.h"
29 #include "core/components_ng/pattern/select_overlay/select_overlay_layout_algorithm.h"
30 #include "core/components_ng/pattern/select_overlay/select_overlay_modifier.h"
31 #include "core/components_ng/pattern/select_overlay/select_overlay_paint_method.h"
32 
33 namespace OHOS::Ace::NG {
34 
35 class ACE_EXPORT SelectOverlayPattern : public MenuWrapperPattern {
36     DECLARE_ACE_TYPE(SelectOverlayPattern, MenuWrapperPattern);
37 
38 public:
SelectOverlayPattern(std::shared_ptr<SelectOverlayInfo> info)39     explicit SelectOverlayPattern(std::shared_ptr<SelectOverlayInfo> info)
40         : MenuWrapperPattern(-1), info_(std::move(info))
41     {}
42     ~SelectOverlayPattern() override = default;
43 
IsMeasureBoundary()44     bool IsMeasureBoundary() const override
45     {
46         return true;
47     }
48 
IsAtomicNode()49     bool IsAtomicNode() const override
50     {
51         return false;
52     }
53 
CreateLayoutAlgorithm()54     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
55     {
56         return MakeRefPtr<SelectOverlayLayoutAlgorithm>(info_, defaultMenuEndOffset_, menuWidth_, menuHeight_);
57     }
58 
CreateNodePaintMethod()59     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
60     {
61         if (!selectOverlayModifier_) {
62             selectOverlayModifier_ = AceType::MakeRefPtr<SelectOverlayModifier>(defaultMenuEndOffset_);
63         }
64         if (!selectOverlayContentModifier_) {
65             selectOverlayContentModifier_ = AceType::MakeRefPtr<SelectOverlayContentModifier>();
66         }
67 
68         if (paintMethodCreated_) {
69             return MakeRefPtr<SelectOverlayPaintMethod>(selectOverlayModifier_, selectOverlayContentModifier_, *info_,
70                 defaultMenuEndOffset_, hasExtensionMenu_, hasShowAnimation_, true, isHiddenHandle_);
71         } else {
72             paintMethodCreated_ = true;
73             return MakeRefPtr<SelectOverlayPaintMethod>(selectOverlayModifier_, selectOverlayContentModifier_, *info_,
74                 defaultMenuEndOffset_, hasExtensionMenu_, hasShowAnimation_, false, isHiddenHandle_);
75         }
76     }
77 
GetSelectOverlayInfo()78     const std::shared_ptr<SelectOverlayInfo>& GetSelectOverlayInfo() const
79     {
80         return info_;
81     }
82 
83     void UpdateFirstSelectHandleInfo(const SelectHandleInfo& info);
84 
85     void UpdateSecondSelectHandleInfo(const SelectHandleInfo& info);
86 
87     void UpdateFirstAndSecondHandleInfo(const SelectHandleInfo& firstInfo, const SelectHandleInfo& secondInfo);
88 
89     void UpdateSelectMenuInfo(const SelectMenuInfo& info);
90 
91     void UpdateShowArea(const RectF& area);
92 
93     void SetHandleReverse(bool reverse);
94 
SetSelectInfo(const std::string & selectInfo)95     void SetSelectInfo(const std::string& selectInfo)
96     {
97         selectInfo_ = selectInfo;
98     }
99 
GetSelectInfo()100     const std::string& GetSelectInfo() const
101     {
102         return selectInfo_;
103     }
104 
GetOverlayModifier()105     const RefPtr<SelectOverlayModifier>& GetOverlayModifier()
106     {
107         return selectOverlayModifier_;
108     }
109 
GetContentModifier()110     const RefPtr<SelectOverlayContentModifier>& GetContentModifier()
111     {
112         return selectOverlayContentModifier_;
113     }
114 
GetDefaultMenuEndOffset()115     const OffsetF& GetDefaultMenuEndOffset()
116     {
117         return defaultMenuEndOffset_;
118     }
119 
GetMenuWidth()120     float GetMenuWidth() const
121     {
122         return menuWidth_.value_or(0);
123     }
124 
GetHandleRegion(bool isFirst)125     const RectF& GetHandleRegion(bool isFirst) const
126     {
127         if (isFirst) {
128             return firstHandleRegion_;
129         } else {
130             return secondHandleRegion_;
131         }
132     }
133 
134     void ShowOrHiddenMenu(bool isHidden);
135     void DisableMenu(bool isDisabled);
136 
SetClosedByGlobalTouchEvent(bool closedByGlobalTouch)137     void SetClosedByGlobalTouchEvent(bool closedByGlobalTouch)
138     {
139         closedByGlobalTouchEvent_ = closedByGlobalTouch;
140     }
141 
142     bool IsMenuShow();
143     bool IsHandleShow();
144 
SetHasShowAnimation(bool animation)145     void SetHasShowAnimation(bool animation)
146     {
147         hasShowAnimation_ = animation;
148     }
149 
IsCustomMenu()150     bool IsCustomMenu()
151     {
152         return info_ && info_->menuInfo.menuBuilder != nullptr;
153     }
154 
155     void BeforeCreateLayoutWrapper() override;
156 
157 private:
158     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
159     void UpdateHandleHotZone();
160     void OnAttachToFrameNode() override;
161     void OnDetachFromFrameNode(FrameNode* frameNode) override;
162 
163     void HandleOnClick(GestureEvent& info);
164     void HandleTouchEvent(const TouchEventInfo& info);
165     void HandleOnTouch(GestureEvent& info);
166     void HandlePanStart(GestureEvent& info);
167     void HandlePanMove(GestureEvent& info);
168     void HandlePanEnd(GestureEvent& info);
169     void HandlePanCancel();
170 
171     void CheckHandleReverse();
172     void StartHiddenHandleTask();
173     void StopHiddenHandleTask();
174     void HiddenHandle();
175     void AddMenuResponseRegion(std::vector<DimensionRect>& responseRegion);
176 
177     std::shared_ptr<SelectOverlayInfo> info_;
178     RefPtr<PanEvent> panEvent_;
179     RefPtr<ClickEvent> clickEvent_;
180     RefPtr<TouchEventImpl> touchEvent_;
181 
182     RectF firstHandleRegion_;
183     RectF secondHandleRegion_;
184 
185     bool firstHandleDrag_ = false;
186     bool secondHandleDrag_ = false;
187     // Used to record the original menu display status when the handle is moved.
188     bool orignMenuIsShow_ = false;
189     bool hasExtensionMenu_ = false;
190     bool hasShowAnimation_ = false;
191 
192     int32_t greatThanMaxWidthIndex_ = -1;
193     std::optional<float> menuWidth_;
194     std::optional<float> menuHeight_;
195 
196     std::string selectInfo_;
197 
198     OffsetF defaultMenuEndOffset_;
199 
200     RefPtr<SelectOverlayModifier> selectOverlayModifier_;
201 
202     RefPtr<SelectOverlayContentModifier> selectOverlayContentModifier_;
203 
204     bool paintMethodCreated_ = false;
205 
206     bool closedByGlobalTouchEvent_ = false;
207     CancelableCallback<void()> hiddenHandleTask_;
208     bool isHiddenHandle_ = false;
209 
210     ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayPattern);
211 };
212 } // namespace OHOS::Ace::NG
213 
214 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H
215