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_RICH_EDITOR_RICH_EDITOR_SELECT_OVERLAY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RICH_EDITOR_RICH_EDITOR_SELECT_OVERLAY_H
18 #include <optional>
19 
20 #include "core/components_ng/pattern/text/text_select_overlay.h"
21 
22 namespace OHOS::Ace::NG {
23 
24 enum class DragHandleIndex { NONE, FIRST, SECOND };
25 class RichEditorSelectOverlay : public TextSelectOverlay {
26     DECLARE_ACE_TYPE(RichEditorSelectOverlay, TextSelectOverlay);
27 
28 public:
RichEditorSelectOverlay(const WeakPtr<TextBase> & textBase)29     explicit RichEditorSelectOverlay(const WeakPtr<TextBase>& textBase) : TextSelectOverlay(textBase) {}
30     ~RichEditorSelectOverlay() = default;
31 
32     // override TextBaseSelectOverlay
33     bool PreProcessOverlay(const OverlayRequest& request) override;
34     bool CheckHandleVisible(const RectF& paintRect) override;
35     void OnResetTextSelection() override;
36     void AfterCloseOverlay() override;
37     void OnAncestorNodeChanged(FrameNodeChangeInfoFlag flag) override;
38 
39     // override SelectOverlayHolder
40     std::optional<SelectHandleInfo> GetFirstHandleInfo() override;
41     std::optional<SelectHandleInfo> GetSecondHandleInfo() override;
42     void OnUpdateMenuInfo(SelectMenuInfo& menuInfo, SelectOverlayDirtyFlag dirtyFlag) override;
43     void OnUpdateSelectOverlayInfo(SelectOverlayInfo& overlayInfo, int32_t requestCode) override;
44     RectF GetSelectArea() override;
45     std::string GetSelectedText() override;
46 
47     // override SelectOverlayCallback
48     void OnMenuItemAction(OptionMenuActionId id, OptionMenuType type) override;
49     void OnOverlayTouchDown(const TouchEventInfo& event) override;
50     void OnHandleMove(const RectF& rect, bool isFirst) override;
51     void GetLocalPointWithTransform(OffsetF& localPoint);
52     void OnHandleMoveDone(const RectF& rect, bool isFirst) override;
53     void OnCloseOverlay(OptionMenuType menuType, CloseReason reason, RefPtr<OverlayInfo> info = nullptr) override;
54     void OnHandleGlobalTouchEvent(SourceType sourceType, TouchType touchType, bool touchInside = true) override;
55     void OnHandleLevelModeChanged(HandleLevelMode mode) override;
56     void OnHandleMarkInfoChange(std::shared_ptr<SelectOverlayInfo> info, SelectOverlayDirtyFlag flag) override;
57     void UpdateHandleColor();
58     std::optional<SelectOverlayInfo> GetSelectOverlayInfo();
59     bool IsSingleHandleShow();
60     void UpdateMenuOffset();
61     bool IsBothHandlesShow();
62     bool IsHandleShow();
63     void OnHandleMoveStart(const GestureEvent& event, bool isFirst) override;
64     void UpdateSelectOverlayOnAreaChanged();
65     void UpdateHandleOffset();
66     void ToggleMenu();
67     void OnHandleIsHidden() override;
68     void OnOverlayClick(const GestureEvent& event, bool isFirst) override;
69     void OnHandleMouseEvent(const MouseInfo& event) override;
70     void OnAfterSelectOverlayShow(bool isCreate) override;
IsRegisterTouchCallback()71     bool IsRegisterTouchCallback() override
72     {
73         return true;
74     }
GetIsHandleMoving()75     bool GetIsHandleMoving()
76     {
77         return isHandleMoving_;
78     }
79     float GetHandleHotZoneRadius();
80 
81 private:
82     void RemoveAreaChangeInner();
83     void CloseMagnifier();
84     void UpdateSelectorOnHandleMove(const OffsetF& handleOffset, bool isFirstHandle) override;
85     void CheckMenuParamChange(SelectOverlayInfo& selectInfo, TextSpanType selectType, TextResponseType responseType);
86     void SwitchCaretState();
87     void ResumeTwinkling();
88     std::shared_ptr<SelectionMenuParams> lastMenuParams_ = nullptr;
89     std::pair<TextSpanType, TextResponseType> lastSelectResponseComb_;
90     bool needRefreshMenu_ = false;
91     bool handleIsHidden_ = true;
92     std::pair<int32_t, int32_t> initSelector_ = { 0, 0 };
93     DragHandleIndex dragHandleIndex_ = DragHandleIndex::NONE;
94 
95     ACE_DISALLOW_COPY_AND_MOVE(RichEditorSelectOverlay);
96 };
97 
98 } // namespace OHOS::Ace::NG
99 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_RICH_EDITOR_RICH_EDITOR_SELECT_OVERLAY_H
100