1 /* 2 * Copyright (c) 2024 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_TEXT_TEXT_BASE_SELECT_OVERLAY_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_SELECT_OVERLAY_H 18 #include <optional> 19 20 #include "base/geometry/ng/point_t.h" 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/manager/select_content_overlay/select_overlay_callback.h" 25 #include "core/components_ng/manager/select_content_overlay/select_overlay_holder.h" 26 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h" 27 #include "core/components_ng/pattern/text/text_base.h" 28 #include "core/components_ng/pattern/text/text_menu_extension.h" 29 #include "core/event/ace_events.h" 30 #include "core/event/touch_event.h" 31 32 namespace OHOS::Ace::NG { 33 struct OverlayRequest { 34 bool menuIsShow = true; 35 bool hideHandle = false; 36 bool animation = false; 37 bool hideHandleLine = false; 38 int32_t requestCode = 0; 39 }; 40 41 class BaseTextSelectOverlay : public SelectOverlayHolder, public SelectOverlayCallback { 42 DECLARE_ACE_TYPE(BaseTextSelectOverlay, SelectOverlayHolder, SelectOverlayCallback); 43 44 public: BaseTextSelectOverlay(const WeakPtr<TextBase> & textBase)45 explicit BaseTextSelectOverlay(const WeakPtr<TextBase>& textBase) : hostTextBase_(textBase) {} 46 ~BaseTextSelectOverlay() = default; 47 48 static RectF GetVisibleRect(const RefPtr<FrameNode>& node, const RectF& visibleRect); 49 50 template<class T> GetPattern()51 RefPtr<T> GetPattern() const 52 { 53 return DynamicCast<T>(hostTextBase_.Upgrade()); 54 } 55 GetHostTextBase()56 WeakPtr<TextBase> GetHostTextBase() 57 { 58 return hostTextBase_; 59 } 60 61 void ProcessOverlay(const OverlayRequest& request = OverlayRequest()); 62 void ProcessOverlayOnAreaChanged(const OverlayRequest& request = OverlayRequest()); PreProcessOverlay(const OverlayRequest & request)63 virtual bool PreProcessOverlay(const OverlayRequest& request) 64 { 65 return true; 66 } AfterCloseOverlay()67 virtual void AfterCloseOverlay() {} 68 69 // override SelectOverlayHolder 70 RefPtr<FrameNode> GetOwner() override; 71 void OnHandleGlobalTouchEvent(SourceType sourceType, TouchType touchType, bool touchInside = true) override; 72 bool CheckTouchInHostNode(const PointF& touchPoint) override; 73 void OnUpdateSelectOverlayInfo(SelectOverlayInfo& overlayInfo, int32_t requestCode) override; 74 bool CheckRestartHiddenHandleTask(int32_t requestCode) override; 75 std::optional<RectF> GetAncestorNodeViewPort() override; GetCallback()76 RefPtr<SelectOverlayCallback> GetCallback() override 77 { 78 return AceType::Claim(this); 79 } 80 // override SelectOverlayHolder end 81 82 // common method 83 bool SelectOverlayIsOn(); 84 bool SelectOverlayIsCreating(); 85 void CloseOverlay(bool animation, CloseReason reason); 86 void ToggleMenu(); 87 void ShowMenu(); 88 void HideMenu(bool noAnimation = false); 89 void DisableMenu(); 90 void EnableMenu(); 91 void UpdateAllHandlesOffset(); 92 void UpdateFirstHandleOffset(); 93 void UpdateSecondHandleOffset(); 94 void UpdateViewPort(); 95 bool IsShowMouseMenu(); 96 bool IsCurrentMenuVisibile(); 97 bool IsHandleReverse(); 98 void RemoveSelectionHoldCallback(); 99 void SetSelectionHoldCallback(); 100 IsSingleHandle()101 bool IsSingleHandle() 102 { 103 return isSingleHandle_; 104 } 105 IsUsingMouse()106 bool IsUsingMouse() 107 { 108 return isUsingMouse_; 109 } 110 SetIsSingleHandle(bool isSingleHandle)111 void SetIsSingleHandle(bool isSingleHandle) 112 { 113 isSingleHandle_ = isSingleHandle; 114 } 115 SetUsingMouse(bool isUsingMouse)116 void SetUsingMouse(bool isUsingMouse) 117 { 118 isUsingMouse_ = isUsingMouse; 119 } 120 SetShowPaste(bool isShowPaste)121 void SetShowPaste(bool isShowPaste) 122 { 123 isShowPaste_ = isShowPaste; 124 } 125 IsShowPaste()126 bool IsShowPaste() 127 { 128 return isShowPaste_; 129 } 130 SetMenuIsShow(bool isShowMenu)131 void SetMenuIsShow(bool isShowMenu) 132 { 133 isShowMenu_ = isShowMenu; 134 originalMenuIsShow_ = isShowMenu; 135 } 136 IsShowMenu()137 bool IsShowMenu() 138 { 139 return isShowMenu_; 140 } 141 SetIsShowHandleLine(bool isShowHandleLine)142 void SetIsShowHandleLine(bool isShowHandleLine) 143 { 144 isShowHandleLine_ = isShowHandleLine; 145 } 146 SetMouseMenuOffset(const OffsetF & offset)147 void SetMouseMenuOffset(const OffsetF& offset) 148 { 149 mouseMenuOffset_ = offset; 150 } 151 152 // common virtual methods. 153 virtual RectF GetVisibleContentRect(); 154 virtual bool CheckHandleVisible(const RectF& paintRect) = 0; 155 GetPasteMimeTypes()156 virtual std::vector<std::string> GetPasteMimeTypes() 157 { 158 return std::vector<std::string>(); 159 } 160 OnResetTextSelection()161 virtual void OnResetTextSelection() {} 162 163 virtual bool IsAcceptResetSelectionEvent(SourceType sourceType, TouchType touchType); 164 HasRenderTransform()165 bool HasRenderTransform() 166 { 167 UpdateTransformFlag(); 168 return hasTransform_; 169 } 170 // transformed global point to original local point. 171 void RevertLocalPointWithTransform(OffsetF& point); 172 // original local point to transformed global point. 173 void GetGlobalPointsWithTransform(std::vector<OffsetF>& points); 174 // original local rect to transformd global rect. 175 void GetGlobalRectWithTransform(RectF& rect); 176 std::vector<OffsetF> GetGlobalRectVertexWithTransform(const RectF& rect, float extendValue = 0.0f); 177 // original local point to transformed local point. 178 void GetLocalPointsWithTransform(std::vector<OffsetF>& localPoints); 179 void GetLocalPointWithTransform(OffsetF& localPoint); 180 // original local rect to transformed local rect. 181 void GetLocalRectWithTransform(RectF& localRect); 182 183 OffsetF GetPaintOffsetWithoutTransform(); 184 RectF GetPaintRectWithTransform(); 185 OffsetF GetPaintRectOffsetWithTransform(); 186 RectF GetVisibleContentRectWithTransform(float epsilon); 187 bool CheckHandleIsVisibleWithTransform(const OffsetF& startPoint, const OffsetF& endPoint, float epsilon); 188 bool IsPointInRect(const OffsetF& point, const OffsetF& leftBottom, const OffsetF& rightBottom, 189 const OffsetF& rightTop, const OffsetF& leftTop); 190 191 void OnSelectionMenuOptionsUpdate( 192 const NG::OnCreateMenuCallback&& onCreateMenuCallback, const NG::OnMenuItemClickCallback&& onMenuItemClick); 193 OnCreateMenuCallbackUpdate(const NG::OnCreateMenuCallback && onCreateMenuCallback)194 void OnCreateMenuCallbackUpdate(const NG::OnCreateMenuCallback&& onCreateMenuCallback) 195 { 196 onCreateMenuCallback_ = onCreateMenuCallback; 197 } 198 OnMenuItemClickCallbackUpdate(const NG::OnMenuItemClickCallback && onMenuItemClick)199 void OnMenuItemClickCallbackUpdate(const NG::OnMenuItemClickCallback&& onMenuItemClick) 200 { 201 onMenuItemClick_ = onMenuItemClick; 202 } 203 204 float GetHandleDiameter(); 205 VectorF GetHostScale(); 206 void SwitchToOverlayMode(); 207 void SwitchToEmbedMode(); 208 void SetHandleLevelMode(HandleLevelMode mode); IsOverlayMode()209 bool IsOverlayMode() 210 { 211 return handleLevelMode_ == HandleLevelMode::OVERLAY; 212 } 213 OnHandleLevelModeChanged(HandleLevelMode mode)214 void OnHandleLevelModeChanged(HandleLevelMode mode) override 215 { 216 SetHandleLevelMode(mode); 217 UpdateAllHandlesOffset(); 218 UpdateViewPort(); 219 } 220 virtual void OnAncestorNodeChanged(FrameNodeChangeInfoFlag flag); 221 void OnCloseOverlay(OptionMenuType menuType, CloseReason reason, RefPtr<OverlayInfo> info) override; OnHandleMoveStart(const GestureEvent & event,bool isFirst)222 void OnHandleMoveStart(const GestureEvent& event, bool isFirst) override 223 { 224 isHandleDragging_ = true; 225 } OnHandleMoveDone(const RectF & rect,bool isFirst)226 void OnHandleMoveDone(const RectF& rect, bool isFirst) override 227 { 228 isHandleDragging_ = false; 229 } GetIsHandleDragging()230 bool GetIsHandleDragging() 231 { 232 return isHandleDragging_; 233 } 234 bool IsTouchAtHandle(const TouchEventInfo& info); 235 bool IsClickAtHandle(const GestureEvent& info); 236 bool HasUnsupportedTransform(); 237 bool CheckSwitchToMode(HandleLevelMode mode) override; 238 OnUpdateOnCreateMenuCallback(SelectOverlayInfo & selectInfo)239 void OnUpdateOnCreateMenuCallback(SelectOverlayInfo& selectInfo) 240 { 241 selectInfo.onCreateCallback.onCreateMenuCallback = onCreateMenuCallback_; 242 selectInfo.onCreateCallback.onMenuItemClick = onMenuItemClick_; 243 auto textRange = [weak = GetHostTextBase()](int32_t& start, int32_t& end) { 244 auto pattern = weak.Upgrade(); 245 CHECK_NULL_VOID(pattern); 246 pattern->GetSelectIndex(start, end); 247 }; 248 selectInfo.onCreateCallback.textRangeCallback = textRange; 249 } 250 251 void MarkOverlayDirty(); 252 void OnHandleMarkInfoChange(const std::shared_ptr<SelectOverlayInfo> info, SelectOverlayDirtyFlag flag) override; 253 void UpdateHandleColor(); GetHandleColor()254 virtual std::optional<Color> GetHandleColor() 255 { 256 return std::nullopt; 257 } 258 IsEnableContainerModal()259 bool IsEnableContainerModal() override 260 { 261 return enableContainerModal_; 262 } 263 SetMenuTranslateIsSupport(bool menuTranslateIsSupport)264 void SetMenuTranslateIsSupport(bool menuTranslateIsSupport) 265 { 266 menuTranslateIsSupport_ = menuTranslateIsSupport; 267 } 268 269 protected: 270 RectF MergeSelectedBoxes( 271 const std::vector<RectF>& boxes, const RectF& contentRect, const RectF& textRect, const OffsetF& paintOffset); IsMouseClickDown(SourceType sourceType,TouchType touchType)272 bool IsMouseClickDown(SourceType sourceType, TouchType touchType) 273 { 274 return sourceType == SourceType::MOUSE && touchType == TouchType::DOWN; 275 } 276 IsTouchUp(SourceType sourceType,TouchType touchType)277 bool IsTouchUp(SourceType sourceType, TouchType touchType) 278 { 279 return (sourceType == SourceType::TOUCH || sourceType == SourceType::TOUCH_PAD) && touchType == TouchType::UP; 280 } 281 282 RectF ConvertPaintInfoToRect(const SelectHandlePaintInfo& paintInfo); 283 void SetTransformPaintInfo(SelectHandleInfo& handleInfo, const RectF& localHandleRect); 284 bool CheckHandleCanPaintInHost(const RectF& firstRect, const RectF& secondRect); 285 virtual RectF GetFirstHandleLocalPaintRect(); 286 virtual RectF GetSecondHandleLocalPaintRect(); 287 virtual void CalcHandleLevelMode(const RectF& firstLocalPaintRect, const RectF& secondLocalPaintRect); 288 bool IsAncestorNodeStartAnimation(FrameNodeChangeInfoFlag flag); 289 bool IsAncestorNodeGeometryChange(FrameNodeChangeInfoFlag flag); 290 bool IsAncestorNodeStartScroll(FrameNodeChangeInfoFlag flag); 291 bool IsAncestorNodeEndScroll(FrameNodeChangeInfoFlag flag); 292 bool IsAncestorNodeTransformChange(FrameNodeChangeInfoFlag flag); 293 bool IsAncestorNodeHasTransition(FrameNodeChangeInfoFlag flag); SetEnableHandleLevel(bool enableHandleLevel)294 void SetEnableHandleLevel(bool enableHandleLevel) 295 { 296 enableHandleLevel_ = enableHandleLevel; 297 } 298 void RegisterScrollingListener(const RefPtr<FrameNode> scrollableNode); 299 void OnHandleScrolling(const WeakPtr<FrameNode>& scrollingNode); 300 virtual void UpdateTransformFlag(); 301 bool CheckHasTransformAttr(); UpdateOriginalMenuIsShow()302 void UpdateOriginalMenuIsShow() 303 { 304 originalMenuIsShow_ = IsCurrentMenuVisibile(); 305 } 306 virtual void UpdateMenuWhileAncestorNodeChanged(bool shouldHideMenu, bool shouldShowMenu); 307 bool GetClipHandleViewPort(RectF& rect); 308 bool CalculateClippedRect(RectF& rect); UpdateClipHandleViewPort(RectF & rect)309 virtual void UpdateClipHandleViewPort(RectF& rect) {}; 310 bool GetFrameNodeContentRect(const RefPtr<FrameNode>& node, RectF& rect); IsClipHandleWithViewPort()311 virtual bool IsClipHandleWithViewPort() 312 { 313 return false; 314 } 315 void ApplySelectAreaWithKeyboard(RectF& selectArea); CheckEnableContainerModal()316 void CheckEnableContainerModal() 317 { 318 enableContainerModal_ = true; 319 } 320 bool IsNeedMenuTranslate(); 321 void HandleOnTranslate(); AllowTranslate()322 virtual bool AllowTranslate() 323 { 324 return false; 325 } 326 std::optional<OverlayRequest> latestReqeust_; 327 bool hasTransform_ = false; 328 HandleLevelMode handleLevelMode_ = HandleLevelMode::OVERLAY; 329 OnCreateMenuCallback onCreateMenuCallback_; 330 OnMenuItemClickCallback onMenuItemClick_; 331 bool isHandleMoving_ = false; 332 RectF ConvertWindowToScreenDomain(RectF rect); 333 EdgeF ConvertWindowToScreenDomain(EdgeF edge); 334 std::string GetTranslateParamRectStr(RectF rect, EdgeF rectLeftTop, EdgeF rectRightBottom); 335 336 private: 337 void FindScrollableParentAndSetCallback(const RefPtr<FrameNode>& host); 338 void RegisterParentScrollCallback(const RefPtr<FrameNode>& host, int32_t parentId); 339 void ShowSelectOverlay(const OverlayRequest& request, bool hasClipboardData); 340 void GetHandlePoints(const RectF& handleRect, std::vector<PointF>& points, bool handleOnTop); 341 bool IsPointsInRegion(const std::vector<PointF>& points, const RectF& regionRect); 342 bool CheckAndUpdateHostGlobalPaintRect(); 343 bool CheckHasTransformMatrix(const RefPtr<RenderContext>& context); 344 bool isHandleDragging_ = false; 345 bool isSingleHandle_ = false; 346 bool isShowPaste_ = false; 347 bool isShowMenu_ = true; 348 bool isShowHandleLine_ = true; 349 bool isUsingMouse_ = false; 350 OffsetF mouseMenuOffset_; 351 WeakPtr<TextBase> hostTextBase_; 352 bool enableHandleLevel_ = false; 353 bool touchAtHandle_ = false; 354 bool isChangeToOverlayModeAtEdge_ = true; 355 bool hasRegisterListener_ = false; 356 RectF globalPaintRect_; 357 bool originalMenuIsShow_ = true; 358 bool enableContainerModal_ = false; 359 bool menuTranslateIsSupport_ = false; 360 }; 361 362 } // namespace OHOS::Ace::NG 363 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_TEXT_BASE_SELECT_OVERLAY_H 364