1 /*
2  * Copyright (c) 2021-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_WEB_RENDER_WEB_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RENDER_WEB_H
18 
19 #include "core/components/common/layout/constants.h"
20 #include "core/components/option/option_component.h"
21 #include "core/components/select_popup/select_popup_component.h"
22 #include "core/components/text/text_component.h"
23 #include "core/components/text_overlay/text_overlay_component.h"
24 #include "core/components/web/resource/web_delegate.h"
25 #include "core/components/web/web_component.h"
26 #include "core/gestures/pan_recognizer.h"
27 #include "core/gestures/raw_recognizer.h"
28 #include "core/pipeline/base/render_node.h"
29 
30 #include <chrono>
31 #include<queue>
32 
33 namespace OHOS::Ace {
34 namespace {
35 
36 struct MouseClickInfo {
37     double x = -1;
38     double y = -1;
39     TimeStamp start;
40 };
41 
42 #ifdef OHOS_STANDARD_SYSTEM
43 struct TouchInfo {
44     double x = -1;
45     double y = -1;
46     int32_t id = -1;
47 };
48 
49 struct TouchHandleState {
50     int32_t id = -1;
51     int32_t x = -1;
52     int32_t y = -1;
53     int32_t edge_height = 0;
54 };
55 
56 enum WebOverlayType {
57     INSERT_OVERLAY,
58     SELECTION_OVERLAY,
59     INVALID_OVERLAY
60 };
61 #endif
62 }
63 
64 class RenderWeb : public RenderNode, public DragDropEvent {
65     DECLARE_ACE_TYPE(RenderWeb, RenderNode, DragDropEvent);
66 
67 public:
68     static RefPtr<RenderNode> Create();
69 
70     RenderWeb();
71     ~RenderWeb() override = default;
72 
73     enum class VkState {
74         VK_NONE,
75         VK_SHOW,
76         VK_HIDE
77     };
78 
79     void Update(const RefPtr<Component>& component) override;
80     void PerformLayout() override;
81     void OnAttachContext() override;
82     void OnMouseEvent(const MouseEvent& event);
83     bool HandleMouseEvent(const MouseEvent& event) override;
84     void SendDoubleClickEvent(const MouseClickInfo& info);
85     bool HandleDoubleClickEvent(const MouseEvent& event);
86     bool HandleKeyEvent(const KeyEvent& keyEvent);
87 
88 #ifdef OHOS_STANDARD_SYSTEM
89     void OnAppShow() override;
90     void OnAppHide() override;
91     void OnGlobalPositionChanged() override;
92     void OnPositionChanged() override;
93     void OnSizeChanged() override;
94     void HandleTouchDown(const TouchEventInfo& info, bool fromOverlay);
95     void HandleTouchUp(const TouchEventInfo& info, bool fromOverlay);
96     void HandleTouchMove(const TouchEventInfo& info, bool fromOverlay);
97     void HandleTouchCancel(const TouchEventInfo& info);
98 
99     // Related to text overlay
100     void SetUpdateHandlePosition(
101         const std::function<void(const OverlayShowOption&, float, float)>& updateHandlePosition);
102     bool RunQuickMenu(
103         std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
104         std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
105     void OnQuickMenuDismissed();
106     void OnTouchSelectionChanged(
107         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
108         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
109         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
110     bool TextOverlayMenuShouldShow() const;
111     bool GetShowStartTouchHandle() const;
112     bool GetShowEndTouchHandle() const;
113     bool OnCursorChange(const OHOS::NWeb::CursorType& type, std::shared_ptr<OHOS::NWeb::NWebCursorInfo> info);
114     void OnSelectPopupMenu(
115         std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
116         std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback);
117 #endif
118 
SetDelegate(const RefPtr<WebDelegate> & delegate)119     void SetDelegate(const RefPtr<WebDelegate>& delegate)
120     {
121         delegate_ = delegate;
122     }
123 
GetDelegate()124     RefPtr<WebDelegate> GetDelegate() const
125     {
126         return delegate_;
127     }
128 
129     bool IsAxisScrollable(AxisDirection direction) override;
130     WeakPtr<RenderNode> CheckAxisNode() override;
131 
SetWebIsFocus(bool isFocus)132     void SetWebIsFocus(bool isFocus)
133     {
134         isFocus_ = isFocus;
135     }
136 
GetWebIsFocus()137     bool GetWebIsFocus() const
138     {
139         return isFocus_;
140     }
141 
SetNeedOnFocus(bool needOnFocus)142     void SetNeedOnFocus(bool needOnFocus)
143     {
144         needOnFocus_ = needOnFocus;
145     }
146 
GetNeedOnFocus()147     bool GetNeedOnFocus() const
148     {
149         return needOnFocus_;
150     }
151     void PanOnActionStart(const GestureEvent& info) override;
152     void PanOnActionUpdate(const GestureEvent& info) override;
153     void PanOnActionEnd(const GestureEvent& info) override;
154     void PanOnActionCancel() override;
155     DragItemInfo GenerateDragItemInfo(const RefPtr<PipelineContext>& context, const GestureEvent& info) override;
156     void InitEnhanceSurfaceFlag();
157 
158 protected:
159     RefPtr<WebDelegate> delegate_;
160     RefPtr<WebComponent> web_;
161     Size drawSize_;
162     Size drawSizeCache_;
163     bool isUrlLoaded_ = false;
164     Size preDrawSize_;
165     Offset position_;
166     Offset prePosition_;
167     bool isEnhanceSurface_ = false;
168     bool isCreateWebView_ = false;
169 
170 private:
171 #ifdef OHOS_STANDARD_SYSTEM
172     void Initialize();
173     void InitPanEvent();
174     void HandleDragMove(const GestureEvent& event);
175     bool ParseTouchInfo(const TouchEventInfo& info, std::list<TouchInfo>& touchInfos, const TouchType& touchType);
176     void OnTouchTestHit(const Offset& coordinateOffset, const TouchRestrict& touchRestrict,
177         TouchTestResult& result) override;
178     bool IsTouchHandleValid(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle);
179     bool IsTouchHandleShow(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle);
180     WebOverlayType GetTouchHandleOverlayType(
181         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
182         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
183         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
184     RefPtr<TextOverlayComponent> CreateTextOverlay(
185         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
186         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
187         std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle);
188     void PushTextOverlayToStack();
189     void PopTextOverlay();
190     Offset NormalizeTouchHandleOffset(float x, float y);
191     void RegisterTextOverlayCallback(
192         int32_t flags, std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback);
193     void OnDragWindowStartEvent(RefPtr<PipelineContext> pipelineContext, const GestureEvent& info,
194         const DragItemInfo& dragItemInfo);
195     void OnDragWindowMoveEvent(RefPtr<PipelineContext> pipelineContext, const GestureEvent& info);
196     void OnDragWindowDropEvent(RefPtr<PipelineContext> pipelineContext, const GestureEvent& info);
197     void UpdateGlobalPos();
198     RefPtr<OptionComponent> BuildSelectMenu(const std::string& value);
199 
200     RefPtr<RawRecognizer> touchRecognizer_ = nullptr;
201     RefPtr<PanRecognizer> panRecognizer_ = nullptr;
202     OnMouseCallback onMouse_;
203     OnKeyEventCallback onKeyEvent_;
204     std::function<bool(KeyEventInfo& keyEventInfo)> onPreKeyEvent_;
205     RefPtr<TextOverlayComponent> textOverlay_;
206     WeakPtr<StackElement> stackElement_;
207     std::function<void(const OverlayShowOption&, float, float)> updateHandlePosition_ = nullptr;
208 
209     bool showTextOveralyMenu_ = false;
210     bool showStartTouchHandle_ = false;
211     bool showEndTouchHandle_ = false;
212     bool isDragging_ = false;
213     bool isW3cDragEvent_ = false;
214 
215     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle_ = nullptr;
216     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle_ = nullptr;
217     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle_ = nullptr;
218 #endif
219     void RegistVirtualKeyBoardListener();
220     bool ProcessVirtualKeyBoard(int32_t width, int32_t height, double keyboard);
221     void SetRootView(int32_t width, int32_t height, int32_t offset);
222     void UpdateDelegate();
223     bool needUpdateWeb_ = true;
224     bool isFocus_ = false;
225     bool needOnFocus_ = false;
226     double offsetFix_ = 0.0;
227     VkState isVirtualKeyBoardShow_ { VkState::VK_NONE };
228     std::queue<MouseClickInfo> doubleClickQueue_;
229     RefPtr<SelectPopupComponent> popup_ = nullptr;
230     RefPtr<ThemeManager> themeManager_ = nullptr;
231     RefPtr<AccessibilityManager> accessibilityManager_ = nullptr;
232 };
233 
234 } // namespace OHOS::Ace
235 
236 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RENDER_WEB_H
237