• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_PATTERN_WEB_SELECT_OVERLAY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WEB_SELECT_OVERLAY_H
18 
19 #include <optional>
20 
21 #include "base/geometry/ng/rect_t.h"
22 #include "base/memory/ace_type.h"
23 #include "base/memory/referenced.h"
24 #include "core/components_ng/pattern/text/base_text_select_overlay.h"
25 #include "core/components_ng/pattern/text/text_base.h"
26 
27 namespace OHOS::Ace {
28 class WebDelegateObserver;
29 }
30 
31 namespace OHOS::NWeb {
32     class NWebQuickMenuParams;
33     class NWebQuickMenuCallback;
34     class NWebTouchHandleState;
35     class NWebCursorInfo;
36     class NWebSelectPopupMenuParam;
37     class NWebDateTimeChooser;
38     class NWebDateTimeSuggestion;
39     class NWebSelectPopupMenuCallback;
40     class NWebDateTimeChooserCallback;
41     class NWebAccessibilityNodeInfo;
42     class NWebMessage;
43     class NWebKeyEvent;
44     class NWebSelectMenuBound;
45     enum class CursorType;
46 }
47 
48 namespace OHOS::Ace::NG {
49 
50 class ViewDataCommon;
51 class TransitionalNodeInfo;
52 
53 namespace {
54 struct TouchHandleState {
55     int32_t id = -1;
56     int32_t x = -1;
57     int32_t y = -1;
58     int32_t edge_height = 0;
59 };
60 
61 struct MenuAvoidStrategyMember {
62     LayoutWrapper* layoutWrapper = nullptr;
63     std::shared_ptr<SelectOverlayInfo> info;
64     bool hasKeyboard = false;
65     RectF upPaint;
66     RectF downPaint;
67     double topArea = 0.0;
68     double bottomArea = 0.0;
69     double selectionTop = 0.0;
70     double selectionBottom = 0.0;
71     double keyboardInsetStart = 0.0;
72     double keyboardHeight = 0.0;
73     double avoidFromText = 0.0;
74     double avoidPositionX = 0.0;
75     double avoidPositionY = 0.0;
76     double defaultAvoidY = 0.0;
77     double menuHeight = 0.0;
78     double menuWidth = 0.0;
79     OffsetF windowOffset;
80 };
81 
82 struct InitStrategyTools;
83 
84 enum WebOverlayType { INSERT_OVERLAY, SELECTION_OVERLAY, INVALID_OVERLAY };
85 }
86 
87 class WebSelectOverlay : public BaseTextSelectOverlay {
88     DECLARE_ACE_TYPE(WebSelectOverlay, BaseTextSelectOverlay);
89 
90 public:
WebSelectOverlay(const WeakPtr<TextBase> & textBase)91     explicit WebSelectOverlay(const WeakPtr<TextBase> &textBase) : BaseTextSelectOverlay(textBase)
92     {}
93     ~WebSelectOverlay() = default;
94 
95     // Web Handle Menu Entry
96     bool RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
97         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
98     // Chromium kernel notifies handle position change
99     void OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
100         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
101         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
102     void RegisterSelectOverlayEvent(SelectOverlayInfo& selectInfo);
103     void SetEditMenuOptions(SelectOverlayInfo& selectInfo);
104     void UpdateSelectHandleInfo();
105     bool IsSelectHandleReverse();
106     // Check whether the handle status is valid.
107     WebOverlayType GetTouchHandleOverlayType(
108         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
109         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
110         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
111     bool IsTouchHandleValid(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle);
112     void RegisterSelectOverLayOnClose(SelectOverlayInfo& selectInfo);
113     void CheckHandles(SelectHandleInfo& handleInfo, const std::shared_ptr<OHOS::NWeb::NWebTouchHandleState>& handle);
114     RectF ComputeTouchHandleRect(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> touchHandle);
115     void SetMenuOptions(SelectOverlayInfo& selectInfo,
116         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
117         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
118     void StartListenSelectOverlayParentScroll(const RefPtr<FrameNode>& host);
119     void RegisterSelectOverlayParentScrollCallback(int32_t parentId, int32_t callbackId);
120     void HideHandleAndQuickMenuIfNecessary(bool hide, bool isScroll = false);
121     void OnParentScrollStartOrEndCallback(bool isEnd);
122     void StopListenSelectOverlayParentScroll(const RefPtr<FrameNode>& host);
123     RectF ComputeMouseClippedSelectionBounds(int32_t x, int32_t y, int32_t w, int32_t h);
124     void UpdateRunQuickMenuSelectInfo(SelectOverlayInfo& selectInfo,
125         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
126         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertTouchHandle,
127         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> beginTouchHandle,
128         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endTouchHandle);
129     RectF ComputeClippedSelectionBounds(
130         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
131         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startHandle,
132         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endHandle,
133         bool& isNewAvoid);
134     void QuickMenuIsNeedNewAvoid(
135         SelectOverlayInfo& selectInfo,
136         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
137         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startHandle,
138         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endHandle);
139     void HideMagnifier();
140     void UpdateTouchHandleForOverlay(bool fromOverlay = false);
141     void OnMagnifierHandleMove(const RectF& handleRect, bool isFirst);
142     RectF ChangeHandleHeight(const GestureEvent& event, bool isFirst);
143     void OnSelectHandleStart(const GestureEvent& event, bool isFirst);
144     void SelectCancel();
145     void UpdateClippedSelectionBounds(int32_t x, int32_t y, int32_t w, int32_t h);
146     void ChangeVisibilityOfQuickMenu();
IsShowHandle()147     bool IsShowHandle() { return isShowHandle_; }
SetIsShowHandle(bool isShowHandle)148     void SetIsShowHandle(bool isShowHandle)
149     {
150         isShowHandle_ = isShowHandle;
151     }
152     void HandleOnAskCelia();
153 
154     // override BaseTextSelectOverlay
155     bool PreProcessOverlay(const OverlayRequest& request) override;
156     bool CheckHandleVisible(const RectF& paintRect) override;
OnResetTextSelection()157     void OnResetTextSelection() override {}
158     void AfterCloseOverlay() override;
AllowTranslate()159     bool AllowTranslate() override { return true; }
160     // override BaseTextSelectOverlay end
161 
162     // override SelectOverlayHolder
163     void OnUpdateSelectOverlayInfo(SelectOverlayInfo& selectInfo, int32_t requestCode) override;
164     std::optional<SelectHandleInfo> GetFirstHandleInfo() override;
165     std::optional<SelectHandleInfo> GetSecondHandleInfo() override;
166     void OnUpdateMenuInfo(SelectMenuInfo& menuInfo, SelectOverlayDirtyFlag dirtyFlag) override;
167     RectF GetSelectArea() override;
168     std::string GetSelectedText() override;
169     // override SelectOverlayHolder end
170 
171     // override SelectOverlayCallback
172     void OnMenuItemAction(OptionMenuActionId id, OptionMenuType type) override;
173     void OnMenuItemAction(OptionMenuActionId id, OptionMenuType type, const std::string& labelInfo) override;
174     void OnHandleMove(const RectF& rect, bool isFirst) override;
175     void OnHandleMoveStart(const GestureEvent& event, bool isFirst) override;
176     void OnHandleMoveDone(const RectF& rect, bool isFirst) override;
177     void OnCloseOverlay(OptionMenuType menuType, CloseReason reason, RefPtr<OverlayInfo> info = nullptr) override;
178     void OnHandleGlobalTouchEvent(SourceType sourceType, TouchType touchType, bool touchInside = true) override;
179     void OnOverlayClick(const GestureEvent& event, bool isClickCaret) override;
180     bool CheckTouchInHostNode(const PointF& touchPoint) override;
181     void OnHandleReverse(bool isReverse) override;
182     void OnHandleMarkInfoChange(const std::shared_ptr<SelectOverlayInfo> info, SelectOverlayDirtyFlag flag) override;
183     void OnAfterSelectOverlayShow(bool isCreated) override;
184     void OnHandleIsHidden() override;
185     // override SelectOverlayCallback end
186     void DetectSelectedText(const std::string& text);
187     void UpdateAISelectMenu(TextDataDetectType type, const std::string& content);
188     void UpdateSingleHandleVisible(bool isVisible);
189     bool IsSingleHandle();
190     void SetTouchHandleExistState(bool touchHandleExist);
191     double GetBottomWithKeyboard(double bottom);
192     void SetComputeMenuOffset(SelectOverlayInfo &info_);
193     bool ComputeMenuOffset(LayoutWrapper *layoutWrapper, OffsetF &menuOffset, const RectF &menuRect,
194         OffsetF &windowOffset, std::shared_ptr<SelectOverlayInfo> &info);
195     bool InitMenuAvoidStrategyMember(MenuAvoidStrategyMember& member);
196     void InitMenuAvoidStrategyAboutParam(MenuAvoidStrategyMember& member, InitStrategyTools& tools);
197     void InitMenuAvoidStrategyAboutKeyboard(MenuAvoidStrategyMember& member, InitStrategyTools& tools);
198     void InitMenuAvoidStrategyAboutTop(MenuAvoidStrategyMember& member, InitStrategyTools& tools);
199     void InitMenuAvoidStrategyAboutBottom(MenuAvoidStrategyMember& member, InitStrategyTools& tools);
200     void InitMenuAvoidStrategyAboutPosition(MenuAvoidStrategyMember& member, InitStrategyTools& tools);
201     void MenuAvoidStrategy(OffsetF& menuOffset, MenuAvoidStrategyMember& member);
202 private:
203     void UpdateSelectMenuOptions();
204     void UpdateIsSelectAll();
205     bool isShowHandle_ = false;
206     bool needResetHandleReverse_ = false;
207     bool isSelectAll_ = false;
208     SelectOverlayInfo webSelectInfo_;
209     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle_ = nullptr;
210     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle_ = nullptr;
211     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle_ = nullptr;
212     std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> quickMenuCallback_ = nullptr;
213     bool selectOverlayDragging_ = false;
214     bool isCurrentStartHandleDragging_ = false;
215     bool selectTemporarilyHidden_ = false;
216     bool selectTemporarilyHiddenByScroll_ = false;
217     bool isQuickMenuMouseTrigger_ = false;
218     ScrollableParentInfo scrollableParentInfo_;
219     std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> dropParams_ = nullptr;
220     SelectMenuInfo selectMenuInfo_;
221     ACE_DISALLOW_COPY_AND_MOVE(WebSelectOverlay);
222     OnCreateMenuCallback onCreateMenuCallback_;
223     OnMenuItemClickCallback onMenuItemClick_;
224     bool canShowAIMenu_ = false;
225     TextDataDetectType aiMenuType_ = TextDataDetectType::INVALID;
226     std::string aiMenucontent_;
227 };
228 } // namespace OHOS::Ace::NG
229 
230 #endif  //FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_WEB_SELECT_OVERLAY_H