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_FIELD_TEXT_FIELD_SELECT_OVERLAY_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_SELECT_OVERLAY_H
18 #include <optional>
19 
20 #include "base/geometry/ng/rect_t.h"
21 #include "base/memory/ace_type.h"
22 #include "base/memory/referenced.h"
23 #include "core/components_ng/pattern/select_overlay/select_overlay_property.h"
24 #include "core/components_ng/pattern/text/base_text_select_overlay.h"
25 #include "core/components_ng/pattern/text/text_base.h"
26 #include "core/event/ace_events.h"
27 namespace OHOS::Ace::NG {
28 
29 class TextFieldSelectOverlay : public BaseTextSelectOverlay {
30     DECLARE_ACE_TYPE(TextFieldSelectOverlay, BaseTextSelectOverlay);
31 
32 public:
TextFieldSelectOverlay(const WeakPtr<TextBase> & textBase)33     explicit TextFieldSelectOverlay(const WeakPtr<TextBase>& textBase) : BaseTextSelectOverlay(textBase) {}
34     ~TextFieldSelectOverlay() = default;
35 
36     // override BaseTextSelectOverlay
37     bool PreProcessOverlay(const OverlayRequest& request) override;
38     bool CheckHandleVisible(const RectF& paintRect) override;
39 
GetPasteMimeTypes()40     std::vector<std::string> GetPasteMimeTypes() override
41     {
42         return pasteMimeTypes_;
43     }
44 
45     void OnResetTextSelection() override;
46     void AfterCloseOverlay() override;
47     RectF GetFirstHandleLocalPaintRect() override;
48     RectF GetSecondHandleLocalPaintRect() override;
49     void OnAncestorNodeChanged(FrameNodeChangeInfoFlag flag) override;
50 
51     // override SelectOverlayHolder
52     std::optional<SelectHandleInfo> GetFirstHandleInfo() override;
53     std::optional<SelectHandleInfo> GetSecondHandleInfo() override;
54     void OnUpdateMenuInfo(SelectMenuInfo& menuInfo, SelectOverlayDirtyFlag dirtyFlag) override;
55     void OnUpdateSelectOverlayInfo(SelectOverlayInfo& overlayInfo, int32_t requestCode) override;
56     RectF GetSelectArea() override;
57     std::string GetSelectedText() override;
58 
59     // override SelectOverlayCallback
60     void OnMenuItemAction(OptionMenuActionId id, OptionMenuType type) override;
61     void OnHandleMove(const RectF& rect, bool isFirst) override;
62     void OnHandleMoveDone(const RectF& rect, bool isFirst) override;
63     void OnAfterSelectOverlayShow(bool isCreate) override;
64     void OnCloseOverlay(OptionMenuType menuType, CloseReason reason, RefPtr<OverlayInfo> info = nullptr) override;
65     void OnHandleGlobalTouchEvent(SourceType sourceType, TouchType touchType, bool touchInside = true) override;
66     void OnHandleIsHidden() override;
67     void OnHandleMoveStart(const GestureEvent& event, bool isFirst) override;
68 
69     void HandleOnShowMenu();
70 
71     void ProcessSelectAllOverlay(const OverlayRequest& request);
72 
SetLastSourceType(SourceType sourceType)73     void SetLastSourceType(SourceType sourceType)
74     {
75         lastSourceType_ = sourceType;
76     }
77 
IsUseTouchAtLast()78     bool IsUseTouchAtLast()
79     {
80         return lastSourceType_ == SourceType::TOUCH;
81     }
82 
83     void OnHandleLevelModeChanged(HandleLevelMode mode) override;
84     void OnOverlayClick(const GestureEvent& event, bool isFirst) override;
IsRegisterTouchCallback()85     bool IsRegisterTouchCallback() override
86     {
87         return true;
88     }
89 
90     int32_t GetCaretPositionOnHandleMove(const OffsetF& localOffset, bool isFirst);
IsClipHandleWithViewPort()91     bool IsClipHandleWithViewPort() override
92     {
93         return !HasRenderTransform();
94     }
95     std::optional<Color> GetHandleColor() override;
96 
97 private:
98     std::optional<SelectHandleInfo> GetHandleInfo(const RectF& handlePaintRect);
99     void UpdatePattern(const OverlayRequest& request);
100     int32_t GetTextAreaCaretPosition(const OffsetF& localOffset);
101     int32_t GetTextInputCaretPosition(const OffsetF& localOffset, bool isFirst);
102     void CloseMagnifier();
103     SourceType lastSourceType_ = SourceType::NONE;
104     std::vector<std::string> pasteMimeTypes_ = { "text/plain", "text/html" };
105 };
106 
107 } // namespace OHOS::Ace::NG
108 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERN_TEXT_FIELD_TEXT_FIELD_SELECT_OVERLAY_H
109