• 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         CheckHandleReverse();
43     }
44     ~SelectOverlayPattern() override = default;
45 
IsMeasureBoundary()46     bool IsMeasureBoundary() const override
47     {
48         return true;
49     }
50 
IsAtomicNode()51     bool IsAtomicNode() const override
52     {
53         return false;
54     }
55 
CreateLayoutAlgorithm()56     RefPtr<LayoutAlgorithm> CreateLayoutAlgorithm() override
57     {
58         return MakeRefPtr<SelectOverlayLayoutAlgorithm>(info_, defaultMenuEndOffset_, menuWidth_, menuHeight_);
59     }
60 
CreateNodePaintMethod()61     RefPtr<NodePaintMethod> CreateNodePaintMethod() override
62     {
63         if (!selectOverlayModifier_) {
64             selectOverlayModifier_ = AceType::MakeRefPtr<SelectOverlayModifier>(defaultMenuEndOffset_);
65         }
66         if (!selectOverlayContentModifier_) {
67             selectOverlayContentModifier_ = AceType::MakeRefPtr<SelectOverlayContentModifier>();
68         }
69 
70         if (paintMethodCreated_) {
71             return MakeRefPtr<SelectOverlayPaintMethod>(selectOverlayModifier_, selectOverlayContentModifier_, *info_,
72                 defaultMenuEndOffset_, hasExtensionMenu_, hasShowAnimation_, true, isHiddenHandle_);
73         } else {
74             paintMethodCreated_ = true;
75             return MakeRefPtr<SelectOverlayPaintMethod>(selectOverlayModifier_, selectOverlayContentModifier_, *info_,
76                 defaultMenuEndOffset_, hasExtensionMenu_, hasShowAnimation_, false, isHiddenHandle_);
77         }
78     }
79 
GetSelectOverlayInfo()80     const std::shared_ptr<SelectOverlayInfo>& GetSelectOverlayInfo() const
81     {
82         return info_;
83     }
84 
85     void UpdateFirstSelectHandleInfo(const SelectHandleInfo& info);
86 
87     void UpdateSecondSelectHandleInfo(const SelectHandleInfo& info);
88 
89     void UpdateFirstAndSecondHandleInfo(const SelectHandleInfo& firstInfo, const SelectHandleInfo& secondInfo);
90 
91     void UpdateSelectMenuInfo(const SelectMenuInfo& info);
92 
93     void UpdateSelectMenuInfo(std::function<void(SelectMenuInfo& menuInfo)> updateAction);
94 
95     void UpdateShowArea(const RectF& area);
96 
97     void SetSelectRegionVisible(bool isSelectRegionVisible);
98 
99     void SetHandleReverse(bool reverse);
100 
SetSelectInfo(const std::string & selectInfo)101     void SetSelectInfo(const std::string& selectInfo)
102     {
103         selectInfo_ = selectInfo;
104     }
105 
GetSelectInfo()106     const std::string& GetSelectInfo() const
107     {
108         return selectInfo_;
109     }
110 
GetOverlayModifier()111     const RefPtr<SelectOverlayModifier>& GetOverlayModifier()
112     {
113         return selectOverlayModifier_;
114     }
115 
GetContentModifier()116     const RefPtr<SelectOverlayContentModifier>& GetContentModifier()
117     {
118         return selectOverlayContentModifier_;
119     }
120 
GetDefaultMenuEndOffset()121     const OffsetF& GetDefaultMenuEndOffset()
122     {
123         return defaultMenuEndOffset_;
124     }
125 
GetMenuWidth()126     float GetMenuWidth() const
127     {
128         return menuWidth_.value_or(0);
129     }
130 
GetHandleRegion(bool isFirst)131     const RectF& GetHandleRegion(bool isFirst) const
132     {
133         if (isFirst) {
134             return firstHandleRegion_;
135         } else {
136             return secondHandleRegion_;
137         }
138     }
139 
140     void ShowOrHiddenMenu(bool isHidden, bool noAnimation = false);
141     void DisableMenu(bool isDisabled);
142 
SetClosedByGlobalTouchEvent(bool closedByGlobalTouch)143     void SetClosedByGlobalTouchEvent(bool closedByGlobalTouch)
144     {
145         closedByGlobalTouchEvent_ = closedByGlobalTouch;
146     }
147 
148     bool IsMenuShow();
149     bool IsSingleHandleMenuShow();
150     bool IsHandleShow();
151     bool IsSingleHandle();
152 
SetHasShowAnimation(bool animation)153     void SetHasShowAnimation(bool animation)
154     {
155         hasShowAnimation_ = animation;
156     }
157 
IsCustomMenu()158     bool IsCustomMenu()
159     {
160         return info_ && info_->menuInfo.menuBuilder != nullptr;
161     }
162 
163     void StartHiddenHandleTask(bool isDelay = true);
164     void UpdateSelectArea(const RectF& selectArea);
165 
166 private:
167     bool OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config) override;
168     void UpdateHandleHotZone();
169     void OnAttachToFrameNode() override;
170     void OnDetachFromFrameNode(FrameNode* frameNode) override;
171 
172     void HandleOnClick(GestureEvent& info);
173     void HandleTouchEvent(const TouchEventInfo& info);
174     void HandleOnTouch(GestureEvent& info);
175     void HandlePanStart(GestureEvent& info);
176     void HandlePanMove(GestureEvent& info);
177     void HandlePanEnd(GestureEvent& info);
178     void HandlePanCancel();
179 
180     void CheckHandleReverse();
181     bool IsHandlesInSameLine();
182     void StopHiddenHandleTask();
183     void HiddenHandle();
184     void AddMenuResponseRegion(std::vector<DimensionRect>& responseRegion);
185 
186     std::shared_ptr<SelectOverlayInfo> info_;
187     RefPtr<PanEvent> panEvent_;
188     RefPtr<ClickEvent> clickEvent_;
189     RefPtr<TouchEventImpl> touchEvent_;
190 
191     RectF firstHandleRegion_;
192     RectF secondHandleRegion_;
193 
194     bool firstHandleDrag_ = false;
195     bool secondHandleDrag_ = false;
196     // Used to record the original menu display status when the handle is moved.
197     bool orignMenuIsShow_ = false;
198     bool hasExtensionMenu_ = false;
199     bool hasShowAnimation_ = false;
200 
201     int32_t greatThanMaxWidthIndex_ = -1;
202     std::optional<float> menuWidth_;
203     std::optional<float> menuHeight_;
204 
205     std::string selectInfo_;
206 
207     OffsetF defaultMenuEndOffset_;
208 
209     RefPtr<SelectOverlayModifier> selectOverlayModifier_;
210 
211     RefPtr<SelectOverlayContentModifier> selectOverlayContentModifier_;
212 
213     bool paintMethodCreated_ = false;
214 
215     bool closedByGlobalTouchEvent_ = false;
216     CancelableCallback<void()> hiddenHandleTask_;
217     bool isHiddenHandle_ = false;
218 
219     ACE_DISALLOW_COPY_AND_MOVE(SelectOverlayPattern);
220 };
221 } // namespace OHOS::Ace::NG
222 
223 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_STAGE_PAGE_PATTERN_H
224