• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "core/components_ng/pattern/web/web_pattern.h"
17 
18 #include "base/geometry/ng/offset_t.h"
19 #include "base/mousestyle/mouse_style.h"
20 #include "base/utils/linear_map.h"
21 #include "base/utils/utils.h"
22 #include "core/components/text_overlay/text_overlay_theme.h"
23 #include "core/components/web/resource/web_delegate.h"
24 #include "core/components/web/web_property.h"
25 #include "core/components_ng/pattern/web/web_event_hub.h"
26 #include "core/event/key_event.h"
27 #include "core/event/touch_event.h"
28 #include "core/pipeline_ng/pipeline_context.h"
29 
30 #include "core/components_ng/pattern/menu/menu_view.h"
31 #include "core/components_ng/base/view_stack_processor.h"
32 
33 namespace OHOS::Ace::NG {
34 namespace {
35 const LinearEnumMapNode<OHOS::NWeb::CursorType, MouseFormat> g_cursorTypeMap[] = {
36     { OHOS::NWeb::CursorType::CT_CROSS, MouseFormat::CROSS },
37     { OHOS::NWeb::CursorType::CT_HAND, MouseFormat::HAND_POINTING },
38     { OHOS::NWeb::CursorType::CT_IBEAM, MouseFormat::TEXT_CURSOR },
39     { OHOS::NWeb::CursorType::CT_HELP, MouseFormat::HELP },
40     { OHOS::NWeb::CursorType::CT_EASTRESIZE, MouseFormat::WEST_EAST },
41     { OHOS::NWeb::CursorType::CT_NORTHRESIZE, MouseFormat::NORTH_SOUTH },
42     { OHOS::NWeb::CursorType::CT_NORTHEASTRESIZE, MouseFormat::NORTH_EAST_SOUTH_WEST },
43     { OHOS::NWeb::CursorType::CT_NORTHWESTRESIZE, MouseFormat::NORTH_WEST_SOUTH_EAST },
44     { OHOS::NWeb::CursorType::CT_SOUTHRESIZE, MouseFormat::NORTH_SOUTH },
45     { OHOS::NWeb::CursorType::CT_SOUTHEASTRESIZE, MouseFormat::NORTH_WEST_SOUTH_EAST },
46     { OHOS::NWeb::CursorType::CT_SOUTHWESTRESIZE, MouseFormat::NORTH_EAST_SOUTH_WEST },
47     { OHOS::NWeb::CursorType::CT_WESTRESIZE, MouseFormat::WEST_EAST },
48     { OHOS::NWeb::CursorType::CT_NORTHSOUTHRESIZE, MouseFormat::NORTH_SOUTH },
49     { OHOS::NWeb::CursorType::CT_EASTWESTRESIZE, MouseFormat::WEST_EAST },
50     { OHOS::NWeb::CursorType::CT_NORTHEASTSOUTHWESTRESIZE, MouseFormat::NORTH_EAST_SOUTH_WEST },
51     { OHOS::NWeb::CursorType::CT_NORTHWESTSOUTHEASTRESIZE, MouseFormat::NORTH_WEST_SOUTH_EAST },
52     { OHOS::NWeb::CursorType::CT_COLUMNRESIZE, MouseFormat::RESIZE_LEFT_RIGHT },
53     { OHOS::NWeb::CursorType::CT_ROWRESIZE, MouseFormat::RESIZE_UP_DOWN },
54     { OHOS::NWeb::CursorType::CT_MOVE, MouseFormat::CURSOR_MOVE },
55     { OHOS::NWeb::CursorType::CT_NODROP, MouseFormat::CURSOR_FORBID },
56     { OHOS::NWeb::CursorType::CT_COPY, MouseFormat::CURSOR_COPY },
57     { OHOS::NWeb::CursorType::CT_NOTALLOWED, MouseFormat::CURSOR_FORBID },
58     { OHOS::NWeb::CursorType::CT_ZOOMIN, MouseFormat::ZOOM_IN },
59     { OHOS::NWeb::CursorType::CT_ZOOMOUT, MouseFormat::ZOOM_OUT },
60     { OHOS::NWeb::CursorType::CT_GRABBING, MouseFormat::HAND_GRABBING },
61 };
62 } // namespace
63 
64 constexpr int32_t SINGLE_CLICK_NUM = 1;
65 constexpr int32_t DOUBLE_CLICK_NUM = 2;
66 constexpr double DEFAULT_DBCLICK_INTERVAL = 0.5;
67 constexpr double DEFAULT_DBCLICK_OFFSET = 2.0;
68 constexpr double DEFAULT_AXIS_RATIO = -0.06;
69 
70 WebPattern::WebPattern() = default;
71 
WebPattern(std::string webSrc,const RefPtr<WebController> & webController)72 WebPattern::WebPattern(std::string webSrc, const RefPtr<WebController>& webController)
73     : webSrc_(std::move(webSrc)), webController_(webController)
74 {}
75 
WebPattern(std::string webSrc,const SetWebIdCallback & setWebIdCallback)76 WebPattern::WebPattern(std::string webSrc, const SetWebIdCallback& setWebIdCallback)
77     : webSrc_(std::move(webSrc)), setWebIdCallback_(setWebIdCallback)
78 {}
79 
~WebPattern()80 WebPattern::~WebPattern()
81 {
82     LOGD("WebPattern::~WebPattern");
83     if (observer_) {
84         LOGD("WebPattern::~WebPattern NotifyDestory");
85         observer_->NotifyDestory();
86     }
87 }
88 
OnAttachToFrameNode()89 void WebPattern::OnAttachToFrameNode()
90 {
91     auto host = GetHost();
92     CHECK_NULL_VOID(host);
93     host->GetRenderContext()->SetClipToFrame(true);
94     host->GetRenderContext()->UpdateBackgroundColor(Color::WHITE);
95     host->GetLayoutProperty()->UpdateMeasureType(MeasureType::MATCH_PARENT);
96 }
97 
OnDetachFromFrameNode(FrameNode * frameNode)98 void WebPattern::OnDetachFromFrameNode(FrameNode* frameNode)
99 {
100     CHECK_NULL_VOID(delegate_);
101     isFocus_ = false;
102     delegate_->OnBlur();
103     OnQuickMenuDismissed();
104 }
105 
InitEvent()106 void WebPattern::InitEvent()
107 {
108     auto host = GetHost();
109     CHECK_NULL_VOID(host);
110     auto eventHub = host->GetEventHub<WebEventHub>();
111     CHECK_NULL_VOID(eventHub);
112 
113     auto gestureHub = eventHub->GetOrCreateGestureEventHub();
114     CHECK_NULL_VOID(gestureHub);
115 
116     InitTouchEvent(gestureHub);
117     InitDragEvent(gestureHub);
118     InitPanEvent(gestureHub);
119 
120     auto inputHub = eventHub->GetOrCreateInputEventHub();
121     CHECK_NULL_VOID(inputHub);
122     InitMouseEvent(inputHub);
123 
124     auto focusHub = eventHub->GetOrCreateFocusHub();
125     CHECK_NULL_VOID(focusHub);
126     InitFocusEvent(focusHub);
127 
128     auto context = PipelineContext::GetCurrentContext();
129     CHECK_NULL_VOID(context);
130     auto langTask = [weak = AceType::WeakClaim(this)]() {
131         auto WebPattern = weak.Upgrade();
132         CHECK_NULL_VOID_NOLOG(WebPattern);
133         WebPattern->UpdateLocale();
134     };
135     context->SetConfigChangedCallback(std::move(langTask));
136 }
137 
InitPanEvent(const RefPtr<GestureEventHub> & gestureHub)138 void WebPattern::InitPanEvent(const RefPtr<GestureEventHub>& gestureHub)
139 {
140     if (panEvent_) {
141         return;
142     }
143     auto actionStartTask = [weak = WeakClaim(this)](const GestureEvent& event) {
144         return;
145     };
146     auto actionUpdateTask = [weak = WeakClaim(this)](const GestureEvent& event) {
147         auto pattern = weak.Upgrade();
148         CHECK_NULL_VOID_NOLOG(pattern);
149         pattern->HandleDragMove(event);
150     };
151     auto actionEndTask = [weak = WeakClaim(this)](const GestureEvent& info) {
152         return;
153     };
154     auto actionCancelTask = [weak = WeakClaim(this)]() {
155         return;
156     };
157     PanDirection panDirection;
158     panDirection.type = PanDirection::VERTICAL;
159     panEvent_ = MakeRefPtr<PanEvent>(
160         std::move(actionStartTask), std::move(actionUpdateTask), std::move(actionEndTask), std::move(actionCancelTask));
161     gestureHub->AddPanEvent(panEvent_, panDirection, DEFAULT_PAN_FINGER, DEFAULT_PAN_DISTANCE);
162 }
163 
HandleDragMove(const GestureEvent & event)164 void WebPattern::HandleDragMove(const GestureEvent& event)
165 {
166     if (event.GetInputEventType() == InputEventType::AXIS) {
167         CHECK_NULL_VOID(delegate_);
168         auto localLocation = event.GetLocalLocation();
169         delegate_->HandleAxisEvent(
170             localLocation.GetX(), localLocation.GetY(),
171             event.GetDelta().GetX() * DEFAULT_AXIS_RATIO,
172             event.GetDelta().GetY() * DEFAULT_AXIS_RATIO);
173     }
174 }
175 
InitTouchEvent(const RefPtr<GestureEventHub> & gestureHub)176 void WebPattern::InitTouchEvent(const RefPtr<GestureEventHub>& gestureHub)
177 {
178     if (touchEvent_) {
179         return;
180     }
181 
182     auto touchTask = [weak = WeakClaim(this)](const TouchEventInfo& info) {
183         auto pattern = weak.Upgrade();
184         CHECK_NULL_VOID(pattern);
185         if (info.GetChangedTouches().empty()) {
186             LOGE("touch point is null");
187             return;
188         }
189 
190         // only handle touch event
191         if (info.GetSourceDevice() != SourceType::TOUCH) {
192             return;
193         }
194 
195         const auto& changedPoint = info.GetChangedTouches().front();
196         if (changedPoint.GetTouchType() == TouchType::DOWN) {
197             pattern->HandleTouchDown(info, false);
198             return;
199         }
200         if (changedPoint.GetTouchType() == TouchType::MOVE) {
201             pattern->HandleTouchMove(info, false);
202             return;
203         }
204         if (changedPoint.GetTouchType() == TouchType::UP) {
205             pattern->HandleTouchUp(info, false);
206             return;
207         }
208         if (changedPoint.GetTouchType() == TouchType::CANCEL) {
209             pattern->HandleTouchCancel(info);
210             return;
211         }
212     };
213     touchEvent_ = MakeRefPtr<TouchEventImpl>(std::move(touchTask));
214     gestureHub->AddTouchEvent(touchEvent_);
215 }
216 
InitMouseEvent(const RefPtr<InputEventHub> & inputHub)217 void WebPattern::InitMouseEvent(const RefPtr<InputEventHub>& inputHub)
218 {
219     if (mouseEvent_) {
220         return;
221     }
222 
223     auto mouseTask = [weak = WeakClaim(this)](MouseInfo& info) {
224         auto pattern = weak.Upgrade();
225         CHECK_NULL_VOID_NOLOG(pattern);
226         pattern->HandleMouseEvent(info);
227     };
228 
229     mouseEvent_ = MakeRefPtr<InputEvent>(std::move(mouseTask));
230     inputHub->AddOnMouseEvent(mouseEvent_);
231 }
232 
HandleMouseEvent(MouseInfo & info)233 void WebPattern::HandleMouseEvent(MouseInfo& info)
234 {
235     WebOnMouseEvent(info);
236 
237     auto host = GetHost();
238     CHECK_NULL_VOID(host);
239     auto eventHub = host->GetEventHub<WebEventHub>();
240     CHECK_NULL_VOID(eventHub);
241     auto mouseEventCallback = eventHub->GetOnMouseEvent();
242     CHECK_NULL_VOID(mouseEventCallback);
243     mouseEventCallback(info);
244 }
245 
WebOnMouseEvent(const MouseInfo & info)246 void WebPattern::WebOnMouseEvent(const MouseInfo& info)
247 {
248     CHECK_NULL_VOID(delegate_);
249     if (info.GetAction() == MouseAction::RELEASE) {
250         WebRequestFocus();
251     }
252 
253     auto localLocation = info.GetLocalLocation();
254     if (!HandleDoubleClickEvent(info)) {
255         delegate_->OnMouseEvent(
256             localLocation.GetX(), localLocation.GetY(), info.GetButton(), info.GetAction(), SINGLE_CLICK_NUM);
257     }
258 }
259 
HandleDoubleClickEvent(const MouseInfo & info)260 bool WebPattern::HandleDoubleClickEvent(const MouseInfo& info)
261 {
262     if (info.GetButton() != MouseButton::LEFT_BUTTON || info.GetAction() != MouseAction::PRESS) {
263         return false;
264     }
265     auto localLocation = info.GetLocalLocation();
266     MouseClickInfo clickInfo;
267     clickInfo.x = localLocation.GetX();
268     clickInfo.y = localLocation.GetY();
269     clickInfo.start = info.GetTimeStamp();
270     if (doubleClickQueue_.empty()) {
271         doubleClickQueue_.push(clickInfo);
272         return false;
273     }
274     std::chrono::duration<float> timeout_ = clickInfo.start - doubleClickQueue_.back().start;
275     double offsetX = clickInfo.x - doubleClickQueue_.back().x;
276     double offsetY = clickInfo.y - doubleClickQueue_.back().y;
277     double offset = sqrt(offsetX * offsetX + offsetY * offsetY);
278     if (timeout_.count() < DEFAULT_DBCLICK_INTERVAL && offset < DEFAULT_DBCLICK_OFFSET) {
279         SendDoubleClickEvent(clickInfo);
280         std::queue<MouseClickInfo> empty;
281         swap(empty, doubleClickQueue_);
282         return true;
283     }
284     if (doubleClickQueue_.size() == 1) {
285         doubleClickQueue_.push(clickInfo);
286         return false;
287     }
288     doubleClickQueue_.pop();
289     doubleClickQueue_.push(clickInfo);
290     return false;
291 }
292 
SendDoubleClickEvent(const MouseClickInfo & info)293 void WebPattern::SendDoubleClickEvent(const MouseClickInfo& info)
294 {
295     CHECK_NULL_VOID(delegate_);
296     delegate_->OnMouseEvent(info.x, info.y, MouseButton::LEFT_BUTTON, MouseAction::PRESS, DOUBLE_CLICK_NUM);
297 }
298 
GenerateDragDropInfo(NG::DragDropInfo & dragDropInfo)299 bool WebPattern::GenerateDragDropInfo(NG::DragDropInfo& dragDropInfo)
300 {
301     if (delegate_) {
302         dragDropInfo.pixelMap = delegate_->GetDragPixelMap();
303     }
304 
305     if (dragDropInfo.pixelMap) {
306         LOGI("get w3c drag info success");
307         isW3cDragEvent_ = true;
308         return true;
309     }
310 
311     return false;
312 }
313 
InitCommonDragDropEvent(const RefPtr<GestureEventHub> & gestureHub)314 void WebPattern::InitCommonDragDropEvent(const RefPtr<GestureEventHub>& gestureHub)
315 {
316     auto host = GetHost();
317     CHECK_NULL_VOID(host);
318     auto eventHub = host->GetEventHub<WebEventHub>();
319     CHECK_NULL_VOID(eventHub);
320 
321     auto userOnDragStartFunc = eventHub->GetOnDragStart();
322     auto onDragStartId = [weak = WeakClaim(this), userOnDragStartFunc](const RefPtr<OHOS::Ace::DragEvent>& info,
323                                 const std::string& extraParams) -> NG::DragDropInfo {
324         auto pattern = weak.Upgrade();
325         NG::DragDropInfo dragDropInfo;
326         CHECK_NULL_RETURN_NOLOG(pattern, dragDropInfo);
327         if (pattern->GenerateDragDropInfo(dragDropInfo)) {
328             return dragDropInfo;
329         }
330 
331         LOGI("not w3c drag event, try to get drag info from user callback");
332         if (userOnDragStartFunc) {
333             LOGI("user callback has been set, get from user callback");
334             dragDropInfo = userOnDragStartFunc(info, extraParams);
335             return dragDropInfo;
336         }
337         return dragDropInfo;
338     };
339 
340     // init common drag drop event
341     gestureHub->InitDragDropEvent();
342     // set custom OnDragStart function
343     eventHub->SetOnDragStart(std::move(onDragStartId));
344 }
345 
InitDragEvent(const RefPtr<GestureEventHub> & gestureHub)346 void WebPattern::InitDragEvent(const RefPtr<GestureEventHub>& gestureHub)
347 {
348     if (dragEvent_) {
349         return;
350     }
351 
352     LOGI("web init DragEvent");
353     InitCommonDragDropEvent(gestureHub);
354 
355     auto actionStartTask = [weak = WeakClaim(this)](const GestureEvent& info) {
356         auto pattern = weak.Upgrade();
357         CHECK_NULL_VOID_NOLOG(pattern);
358         pattern->HandleDragStart(info);
359     };
360 
361     auto actionUpdateTask = [weak = WeakClaim(this)](const GestureEvent& info) {
362         auto pattern = weak.Upgrade();
363         CHECK_NULL_VOID_NOLOG(pattern);
364         pattern->HandleDragUpdate(info);
365     };
366 
367     auto actionEndTask = [weak = WeakClaim(this)](const GestureEvent& info) {
368         auto pattern = weak.Upgrade();
369         CHECK_NULL_VOID_NOLOG(pattern);
370         pattern->HandleDragEnd(info);
371     };
372 
373     auto actionCancelTask = [weak = WeakClaim(this)]() {
374         auto pattern = weak.Upgrade();
375         CHECK_NULL_VOID_NOLOG(pattern);
376         pattern->HandleDragCancel();
377     };
378 
379     float distance = DEFAULT_PAN_DISTANCE;
380     auto host = GetHost();
381     if (host) {
382         auto context = host->GetContext();
383         if (context) {
384             distance = static_cast<float>(context->NormalizeToPx(Dimension(DEFAULT_PAN_DISTANCE, DimensionUnit::VP)));
385         }
386     }
387 
388     dragEvent_ = MakeRefPtr<DragEvent>(
389         std::move(actionStartTask), std::move(actionUpdateTask), std::move(actionEndTask), std::move(actionCancelTask));
390     gestureHub->SetCustomDragEvent(dragEvent_, { PanDirection::ALL }, DEFAULT_PAN_FINGER, DEFAULT_PAN_DISTANCE);
391 }
392 
HandleDragStart(const GestureEvent & info)393 void WebPattern::HandleDragStart(const GestureEvent& info)
394 {
395     isDragging_ = true;
396     if (!isW3cDragEvent_ || !delegate_) {
397         LOGI("drag start, don't need to invoke web delegate interface");
398         return;
399     }
400 
401     LOGI("web drag action start");
402     auto host = GetHost();
403     CHECK_NULL_VOID(host);
404     auto pipelineContext = host->GetContext();
405     CHECK_NULL_VOID(pipelineContext);
406     int32_t globalX = static_cast<int32_t>(info.GetGlobalPoint().GetX());
407     int32_t globalY = static_cast<int32_t>(info.GetGlobalPoint().GetY());
408     auto viewScale = pipelineContext->GetViewScale();
409     auto offset = GetCoordinatePoint();
410     int32_t localX = static_cast<int32_t>(globalX - offset.value_or(OffsetF()).GetX());
411     int32_t localY = static_cast<int32_t>(globalY - offset.value_or(OffsetF()).GetY());
412     delegate_->HandleDragEvent(localX * viewScale, localY * viewScale, DragAction::DRAG_ENTER);
413 }
414 
HandleDragUpdate(const GestureEvent & info)415 void WebPattern::HandleDragUpdate(const GestureEvent& info)
416 {
417     if (!isW3cDragEvent_ || !delegate_) {
418         LOGD("drag update, don't need to invoke web delegate interface");
419         return;
420     }
421 
422     LOGD("web drag action update");
423     auto host = GetHost();
424     CHECK_NULL_VOID(host);
425     auto pipelineContext = host->GetContext();
426     CHECK_NULL_VOID(pipelineContext);
427     int32_t globalX = static_cast<int32_t>(info.GetGlobalPoint().GetX());
428     int32_t globalY = static_cast<int32_t>(info.GetGlobalPoint().GetY());
429     LOGD("web drag position update, x = %{public}d, y = %{public}d", globalX, globalY);
430     auto viewScale = pipelineContext->GetViewScale();
431     auto offset = GetCoordinatePoint();
432     int32_t localX = static_cast<int32_t>(globalX - offset.value_or(OffsetF()).GetX());
433     int32_t localY = static_cast<int32_t>(globalY - offset.value_or(OffsetF()).GetY());
434     delegate_->HandleDragEvent(localX * viewScale, localY * viewScale, DragAction::DRAG_OVER);
435 }
436 
HandleDragEnd(const GestureEvent & info)437 void WebPattern::HandleDragEnd(const GestureEvent& info)
438 {
439     isDragging_ = false;
440     if (!isW3cDragEvent_ || !delegate_) {
441         LOGI("drag end, don't need to invoke web delegate interface");
442         return;
443     }
444 
445     LOGI("web drag action end");
446     isW3cDragEvent_ = false;
447     auto host = GetHost();
448     CHECK_NULL_VOID(host);
449     auto pipelineContext = host->GetContext();
450     CHECK_NULL_VOID(pipelineContext);
451     auto viewScale = pipelineContext->GetViewScale();
452     auto offset = GetCoordinatePoint();
453     int32_t localX = static_cast<int32_t>(info.GetGlobalPoint().GetX() - offset.value_or(OffsetF()).GetX());
454     int32_t localY = static_cast<int32_t>(info.GetGlobalPoint().GetY() - offset.value_or(OffsetF()).GetY());
455     delegate_->HandleDragEvent(localX * viewScale, localY * viewScale, DragAction::DRAG_DROP);
456     delegate_->HandleDragEvent(localX * viewScale, localY * viewScale, DragAction::DRAG_END);
457 }
458 
HandleDragCancel()459 void WebPattern::HandleDragCancel()
460 {
461     isDragging_ = false;
462     if (!isW3cDragEvent_ || !delegate_) {
463         LOGI("drag cancel, don't need to invoke web delegate interface");
464         return;
465     }
466 
467     LOGI("web drag cancel");
468     isW3cDragEvent_ = false;
469     auto host = GetHost();
470     CHECK_NULL_VOID(host);
471     auto pipelineContext = host->GetContext();
472     CHECK_NULL_VOID(pipelineContext);
473     delegate_->HandleDragEvent(0, 0, DragAction::DRAG_CANCEL);
474 }
475 
InitFocusEvent(const RefPtr<FocusHub> & focusHub)476 void WebPattern::InitFocusEvent(const RefPtr<FocusHub>& focusHub)
477 {
478     auto focusTask = [weak = WeakClaim(this)]() {
479         auto pattern = weak.Upgrade();
480         CHECK_NULL_VOID_NOLOG(pattern);
481         pattern->HandleFocusEvent();
482     };
483     focusHub->SetOnFocusInternal(focusTask);
484 
485     auto blurTask = [weak = WeakClaim(this)](const BlurReason& blurReason) {
486         auto pattern = weak.Upgrade();
487         CHECK_NULL_VOID_NOLOG(pattern);
488         pattern->HandleBlurEvent(blurReason);
489     };
490     focusHub->SetOnBlurReasonInternal(blurTask);
491 
492     auto keyTask = [weak = WeakClaim(this)](const KeyEvent& keyEvent) -> bool {
493         auto pattern = weak.Upgrade();
494         CHECK_NULL_RETURN(pattern, false);
495         return pattern->HandleKeyEvent(keyEvent);
496     };
497     focusHub->SetOnKeyEventInternal(keyTask);
498 }
499 
HandleFocusEvent()500 void WebPattern::HandleFocusEvent()
501 {
502     CHECK_NULL_VOID(delegate_);
503     isFocus_ = true;
504     if (needOnFocus_) {
505         delegate_->OnFocus();
506     } else {
507         needOnFocus_ = true;
508     }
509 }
510 
HandleBlurEvent(const BlurReason & blurReason)511 void WebPattern::HandleBlurEvent(const BlurReason& blurReason)
512 {
513     CHECK_NULL_VOID(delegate_);
514     isFocus_ = false;
515     if (!selectPopupMenuShowing_) {
516         delegate_->SetBlurReason(static_cast<OHOS::NWeb::BlurReason>(blurReason));
517         delegate_->OnBlur();
518     }
519     OnQuickMenuDismissed();
520 }
521 
HandleKeyEvent(const KeyEvent & keyEvent)522 bool WebPattern::HandleKeyEvent(const KeyEvent& keyEvent)
523 {
524     bool ret = false;
525 
526     auto host = GetHost();
527     CHECK_NULL_RETURN(host, ret);
528     auto eventHub = host->GetEventHub<WebEventHub>();
529     CHECK_NULL_RETURN(eventHub, ret);
530 
531     KeyEventInfo info(keyEvent);
532     auto keyEventCallback = eventHub->GetOnKeyEvent();
533     if (keyEventCallback) {
534         keyEventCallback(info);
535     }
536 
537     auto preKeyEventCallback = eventHub->GetOnPreKeyEvent();
538     if (preKeyEventCallback) {
539         ret = preKeyEventCallback(info);
540         if (ret) {
541             LOGI("keyevent consumed in hap");
542             return ret;
543         }
544     }
545 
546     ret = WebOnKeyEvent(keyEvent);
547     return ret;
548 }
549 
WebOnKeyEvent(const KeyEvent & keyEvent)550 bool WebPattern::WebOnKeyEvent(const KeyEvent& keyEvent)
551 {
552     CHECK_NULL_RETURN(delegate_, false);
553     return delegate_->OnKeyEvent(static_cast<int32_t>(keyEvent.code), static_cast<int32_t>(keyEvent.action));
554 }
555 
WebRequestFocus()556 void WebPattern::WebRequestFocus()
557 {
558     auto host = GetHost();
559     CHECK_NULL_VOID(host);
560     auto eventHub = host->GetEventHub<WebEventHub>();
561     CHECK_NULL_VOID(eventHub);
562     auto focusHub = eventHub->GetOrCreateFocusHub();
563     CHECK_NULL_VOID(focusHub);
564 
565     focusHub->RequestFocusImmediately();
566 }
567 
OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper> & dirty,const DirtySwapConfig & config)568 bool WebPattern::OnDirtyLayoutWrapperSwap(const RefPtr<LayoutWrapper>& dirty, const DirtySwapConfig& config)
569 {
570     if (!config.contentSizeChange) {
571         return false;
572     }
573     CHECK_NULL_RETURN(delegate_, false);
574     CHECK_NULL_RETURN(dirty, false);
575     auto geometryNode = dirty->GetGeometryNode();
576     auto drawSize = Size(geometryNode->GetContentSize().Width(), geometryNode->GetContentSize().Height());
577     if (drawSize.IsInfinite() || drawSize.IsEmpty()) {
578         LOGE("resize invalid %{public}f, %{public}f", drawSize.Width(), drawSize.Height());
579         return false;
580     }
581 
582     drawSize_ = Size(drawSize.Width(), drawSize.Height());
583     drawSizeCache_ = drawSize_;
584     auto offset = Offset(GetCoordinatePoint()->GetX(), GetCoordinatePoint()->GetY());
585     delegate_->SetBoundsOrResize(drawSize_, offset);
586     // first update size to load url.
587     if (!isUrlLoaded_) {
588         isUrlLoaded_ = true;
589         if (webSrc_) {
590             delegate_->LoadUrl();
591         } else if (webData_) {
592             delegate_->LoadDataWithRichText();
593         }
594     }
595 
596     return false;
597 }
598 
OnAreaChangedInner()599 void WebPattern::OnAreaChangedInner()
600 {
601     auto offset = OffsetF(GetCoordinatePoint()->GetX(), GetCoordinatePoint()->GetY());
602     if (webOffset_ == offset) {
603         return;
604     }
605     webOffset_ = offset;
606     UpdateTouchHandleForOverlay();
607     auto resizeOffset = Offset(offset.GetX(), offset.GetY());
608     delegate_->SetBoundsOrResize(drawSize_, resizeOffset);
609 }
610 
OnWebSrcUpdate()611 void WebPattern::OnWebSrcUpdate()
612 {
613     if (delegate_ && isUrlLoaded_) {
614         delegate_->LoadUrl();
615     }
616 }
617 
OnWebDataUpdate()618 void WebPattern::OnWebDataUpdate()
619 {
620     if (delegate_ && isUrlLoaded_) {
621         delegate_->LoadDataWithRichText();
622     }
623 }
624 
OnJsEnabledUpdate(bool value)625 void WebPattern::OnJsEnabledUpdate(bool value)
626 {
627     if (delegate_) {
628         delegate_->UpdateJavaScriptEnabled(value);
629     }
630 }
631 
OnMediaPlayGestureAccessUpdate(bool value)632 void WebPattern::OnMediaPlayGestureAccessUpdate(bool value)
633 {
634     if (delegate_) {
635         delegate_->UpdateMediaPlayGestureAccess(value);
636     }
637 }
638 
OnFileAccessEnabledUpdate(bool value)639 void WebPattern::OnFileAccessEnabledUpdate(bool value)
640 {
641     if (delegate_) {
642         delegate_->UpdateAllowFileAccess(value);
643     }
644 }
645 
OnOnLineImageAccessEnabledUpdate(bool value)646 void WebPattern::OnOnLineImageAccessEnabledUpdate(bool value)
647 {
648     if (delegate_) {
649         delegate_->UpdateBlockNetworkImage(!value);
650     }
651 }
652 
OnDomStorageAccessEnabledUpdate(bool value)653 void WebPattern::OnDomStorageAccessEnabledUpdate(bool value)
654 {
655     if (delegate_) {
656         delegate_->UpdateDomStorageEnabled(value);
657     }
658 }
659 
OnImageAccessEnabledUpdate(bool value)660 void WebPattern::OnImageAccessEnabledUpdate(bool value)
661 {
662     if (delegate_) {
663         delegate_->UpdateLoadsImagesAutomatically(value);
664     }
665 }
666 
OnMixedModeUpdate(MixedModeContent value)667 void WebPattern::OnMixedModeUpdate(MixedModeContent value)
668 {
669     if (delegate_) {
670         delegate_->UpdateMixedContentMode(value);
671     }
672 }
673 
OnZoomAccessEnabledUpdate(bool value)674 void WebPattern::OnZoomAccessEnabledUpdate(bool value)
675 {
676     if (delegate_) {
677         delegate_->UpdateSupportZoom(value);
678     }
679 }
680 
OnGeolocationAccessEnabledUpdate(bool value)681 void WebPattern::OnGeolocationAccessEnabledUpdate(bool value)
682 {
683     if (delegate_) {
684         delegate_->UpdateGeolocationEnabled(value);
685     }
686 }
687 
OnUserAgentUpdate(const std::string & value)688 void WebPattern::OnUserAgentUpdate(const std::string& value)
689 {
690     if (delegate_) {
691         delegate_->UpdateUserAgent(value);
692     }
693 }
694 
OnCacheModeUpdate(WebCacheMode value)695 void WebPattern::OnCacheModeUpdate(WebCacheMode value)
696 {
697     if (delegate_) {
698         delegate_->UpdateCacheMode(value);
699     }
700 }
701 
OnDarkModeUpdate(WebDarkMode mode)702 void WebPattern::OnDarkModeUpdate(WebDarkMode mode)
703 {
704     if (delegate_) {
705         delegate_->UpdateDarkMode(mode);
706     }
707 }
708 
OnForceDarkAccessUpdate(bool access)709 void WebPattern::OnForceDarkAccessUpdate(bool access)
710 {
711     if (delegate_) {
712         delegate_->UpdateForceDarkAccess(access);
713     }
714 }
715 
OnOverviewModeAccessEnabledUpdate(bool value)716 void WebPattern::OnOverviewModeAccessEnabledUpdate(bool value)
717 {
718     if (delegate_) {
719         delegate_->UpdateOverviewModeEnabled(value);
720     }
721 }
722 
OnFileFromUrlAccessEnabledUpdate(bool value)723 void WebPattern::OnFileFromUrlAccessEnabledUpdate(bool value)
724 {
725     if (delegate_) {
726         delegate_->UpdateFileFromUrlEnabled(value);
727     }
728 }
729 
OnDatabaseAccessEnabledUpdate(bool value)730 void WebPattern::OnDatabaseAccessEnabledUpdate(bool value)
731 {
732     if (delegate_) {
733         delegate_->UpdateDatabaseEnabled(value);
734     }
735 }
736 
OnTextZoomRatioUpdate(int32_t value)737 void WebPattern::OnTextZoomRatioUpdate(int32_t value)
738 {
739     if (delegate_) {
740         delegate_->UpdateTextZoomRatio(value);
741     }
742 }
743 
OnWebDebuggingAccessEnabledUpdate(bool value)744 void WebPattern::OnWebDebuggingAccessEnabledUpdate(bool value)
745 {
746     if (delegate_) {
747         delegate_->UpdateWebDebuggingAccess(value);
748     }
749 }
750 
OnPinchSmoothModeEnabledUpdate(bool value)751 void WebPattern::OnPinchSmoothModeEnabledUpdate(bool value)
752 {
753     if (delegate_) {
754         delegate_->UpdatePinchSmoothModeEnabled(value);
755     }
756 }
757 
OnBackgroundColorUpdate(int32_t value)758 void WebPattern::OnBackgroundColorUpdate(int32_t value)
759 {
760     UpdateBackgroundColorRightNow(value);
761     if (delegate_) {
762         delegate_->UpdateBackgroundColor(value);
763     }
764 }
765 
OnInitialScaleUpdate(float value)766 void WebPattern::OnInitialScaleUpdate(float value)
767 {
768     if (delegate_) {
769         delegate_->UpdateInitialScale(value);
770     }
771 }
772 
OnMultiWindowAccessEnabledUpdate(bool value)773 void WebPattern::OnMultiWindowAccessEnabledUpdate(bool value)
774 {
775     if (delegate_) {
776         delegate_->UpdateMultiWindowAccess(value);
777     }
778 }
779 
OnWebCursiveFontUpdate(const std::string & value)780 void WebPattern::OnWebCursiveFontUpdate(const std::string& value)
781 {
782     if (delegate_) {
783         delegate_->UpdateWebCursiveFont(value);
784     }
785 }
786 
OnWebFantasyFontUpdate(const std::string & value)787 void WebPattern::OnWebFantasyFontUpdate(const std::string& value)
788 {
789     if (delegate_) {
790         delegate_->UpdateWebFantasyFont(value);
791     }
792 }
793 
OnWebFixedFontUpdate(const std::string & value)794 void WebPattern::OnWebFixedFontUpdate(const std::string& value)
795 {
796     if (delegate_) {
797         delegate_->UpdateWebFixedFont(value);
798     }
799 }
800 
OnWebSansSerifFontUpdate(const std::string & value)801 void WebPattern::OnWebSansSerifFontUpdate(const std::string& value)
802 {
803     if (delegate_) {
804         delegate_->UpdateWebSansSerifFont(value);
805     }
806 }
807 
OnWebSerifFontUpdate(const std::string & value)808 void WebPattern::OnWebSerifFontUpdate(const std::string& value)
809 {
810     if (delegate_) {
811         delegate_->UpdateWebSerifFont(value);
812     }
813 }
814 
OnWebStandardFontUpdate(const std::string & value)815 void WebPattern::OnWebStandardFontUpdate(const std::string& value)
816 {
817     if (delegate_) {
818         delegate_->UpdateWebStandardFont(value);
819     }
820 }
821 
OnDefaultFixedFontSizeUpdate(int32_t value)822 void WebPattern::OnDefaultFixedFontSizeUpdate(int32_t value)
823 {
824     if (delegate_) {
825         delegate_->UpdateDefaultFixedFontSize(value);
826     }
827 }
828 
OnDefaultFontSizeUpdate(int32_t value)829 void WebPattern::OnDefaultFontSizeUpdate(int32_t value)
830 {
831     if (delegate_) {
832         delegate_->UpdateDefaultFontSize(value);
833     }
834 }
835 
OnMinFontSizeUpdate(int32_t value)836 void WebPattern::OnMinFontSizeUpdate(int32_t value)
837 {
838     if (delegate_) {
839         delegate_->UpdateMinFontSize(value);
840     }
841 }
842 
OnMinLogicalFontSizeUpdate(int32_t value)843 void WebPattern::OnMinLogicalFontSizeUpdate(int32_t value)
844 {
845     if (delegate_) {
846         delegate_->UpdateMinLogicalFontSize(value);
847     }
848 }
849 
OnBlockNetworkUpdate(bool value)850 void WebPattern::OnBlockNetworkUpdate(bool value)
851 {
852     if (delegate_) {
853         delegate_->UpdateBlockNetwork(value);
854     }
855 }
856 
OnHorizontalScrollBarAccessEnabledUpdate(bool value)857 void WebPattern::OnHorizontalScrollBarAccessEnabledUpdate(bool value)
858 {
859     if (delegate_) {
860         delegate_->UpdateHorizontalScrollBarAccess(value);
861     }
862 }
863 
OnVerticalScrollBarAccessEnabledUpdate(bool value)864 void WebPattern::OnVerticalScrollBarAccessEnabledUpdate(bool value)
865 {
866     if (delegate_) {
867         delegate_->UpdateVerticalScrollBarAccess(value);
868     }
869 }
870 
RegistVirtualKeyBoardListener()871 void WebPattern::RegistVirtualKeyBoardListener()
872 {
873     if (!needUpdateWeb_) {
874         return;
875     }
876     auto pipelineContext = PipelineContext::GetCurrentContext();
877     CHECK_NULL_VOID_NOLOG(pipelineContext);
878     pipelineContext->SetVirtualKeyBoardCallback(
879         [weak = AceType::WeakClaim(this)](int32_t width, int32_t height, double keyboard) {
880             auto webPattern = weak.Upgrade();
881             CHECK_NULL_RETURN_NOLOG(webPattern, false);
882             return webPattern->ProcessVirtualKeyBoard(width, height, keyboard);
883         });
884     needUpdateWeb_ = false;
885 }
886 
InitEnhanceSurfaceFlag()887 void WebPattern::InitEnhanceSurfaceFlag()
888 {
889     if (SystemProperties::GetExtSurfaceEnabled()) {
890         isEnhanceSurface_ = true;
891     } else {
892         isEnhanceSurface_ = false;
893     }
894 }
895 
OnModifyDone()896 void WebPattern::OnModifyDone()
897 {
898     // called in each update function.
899     auto host = GetHost();
900     CHECK_NULL_VOID(host);
901     auto renderContext = host->GetRenderContext();
902     CHECK_NULL_VOID(renderContext);
903     RegistVirtualKeyBoardListener();
904     if (!delegate_) {
905         // first create case,
906         delegate_ = AceType::MakeRefPtr<WebDelegate>(PipelineContext::GetCurrentContext(), nullptr, "");
907         CHECK_NULL_VOID(delegate_);
908         observer_ = AceType::MakeRefPtr<WebDelegateObserver>(delegate_, PipelineContext::GetCurrentContext());
909         CHECK_NULL_VOID(observer_);
910         delegate_->SetObserver(observer_);
911         InitEnhanceSurfaceFlag();
912         delegate_->SetNGWebPattern(Claim(this));
913         delegate_->SetEnhanceSurfaceFlag(isEnhanceSurface_);
914         delegate_->SetPopup(isPopup_);
915         delegate_->SetParentNWebId(parentNWebId_);
916         if (isEnhanceSurface_) {
917             auto drawSize = Size(1, 1);
918             delegate_->SetDrawSize(drawSize);
919             delegate_->InitOHOSWeb(PipelineContext::GetCurrentContext());
920         } else {
921             renderSurface_->SetRenderContext(host->GetRenderContext());
922             renderSurface_->InitSurface();
923             delegate_->InitOHOSWeb(PipelineContext::GetCurrentContext(), renderSurface_);
924         }
925         delegate_->UpdateBackgroundColor(GetBackgroundColorValue(
926             static_cast<int32_t>(renderContext->GetBackgroundColor().value_or(Color::WHITE).GetValue())));
927         delegate_->UpdateJavaScriptEnabled(GetJsEnabledValue(true));
928         delegate_->UpdateBlockNetworkImage(!GetOnLineImageAccessEnabledValue(true));
929         delegate_->UpdateAllowFileAccess(GetFileAccessEnabledValue(true));
930         delegate_->UpdateLoadsImagesAutomatically(GetImageAccessEnabledValue(true));
931         delegate_->UpdateMixedContentMode(GetMixedModeValue(MixedModeContent::MIXED_CONTENT_NEVER_ALLOW));
932         delegate_->UpdateSupportZoom(GetZoomAccessEnabledValue(true));
933         delegate_->UpdateDomStorageEnabled(GetDomStorageAccessEnabledValue(false));
934         delegate_->UpdateGeolocationEnabled(GetGeolocationAccessEnabledValue(true));
935         delegate_->UpdateCacheMode(GetCacheModeValue(WebCacheMode::DEFAULT));
936         delegate_->UpdateDarkMode(GetDarkModeValue(WebDarkMode::Off));
937         delegate_->UpdateForceDarkAccess(GetForceDarkAccessValue(false));
938         delegate_->UpdateOverviewModeEnabled(GetOverviewModeAccessEnabledValue(true));
939         delegate_->UpdateFileFromUrlEnabled(GetFileFromUrlAccessEnabledValue(false));
940         delegate_->UpdateDatabaseEnabled(GetDatabaseAccessEnabledValue(false));
941         delegate_->UpdateTextZoomRatio(GetTextZoomRatioValue(DEFAULT_TEXT_ZOOM_RATIO));
942         delegate_->UpdateWebDebuggingAccess(GetWebDebuggingAccessEnabledValue(false));
943         delegate_->UpdateMediaPlayGestureAccess(GetMediaPlayGestureAccessValue(true));
944         delegate_->UpdatePinchSmoothModeEnabled(GetPinchSmoothModeEnabledValue(false));
945         delegate_->UpdateMultiWindowAccess(GetMultiWindowAccessEnabledValue(false));
946         delegate_->UpdateWebCursiveFont(GetWebCursiveFontValue(DEFAULT_CURSIVE_FONT_FAMILY));
947         delegate_->UpdateWebFantasyFont(GetWebFantasyFontValue(DEFAULT_FANTASY_FONT_FAMILY));
948         delegate_->UpdateWebFixedFont(GetWebFixedFontValue(DEFAULT_FIXED_fONT_FAMILY));
949         delegate_->UpdateWebSansSerifFont(GetWebSansSerifFontValue(DEFAULT_SANS_SERIF_FONT_FAMILY));
950         delegate_->UpdateWebSerifFont(GetWebSerifFontValue(DEFAULT_SERIF_FONT_FAMILY));
951         delegate_->UpdateWebStandardFont(GetWebStandardFontValue(DEFAULT_STANDARD_FONT_FAMILY));
952         delegate_->UpdateDefaultFixedFontSize(GetDefaultFixedFontSizeValue(DEFAULT_FIXED_FONT_SIZE));
953         delegate_->UpdateDefaultFontSize(GetDefaultFontSizeValue(DEFAULT_FONT_SIZE));
954         delegate_->UpdateMinFontSize(GetMinFontSizeValue(DEFAULT_MINIMUM_FONT_SIZE));
955         delegate_->UpdateMinLogicalFontSize(GetMinLogicalFontSizeValue(DEFAULT_MINIMUM_LOGICAL_FONT_SIZE));
956         delegate_->UpdateHorizontalScrollBarAccess(GetHorizontalScrollBarAccessEnabledValue(true));
957         delegate_->UpdateVerticalScrollBarAccess(GetVerticalScrollBarAccessEnabledValue(true));
958         if (GetBlockNetwork()) {
959             delegate_->UpdateBlockNetwork(GetBlockNetwork().value());
960         }
961         if (GetUserAgent()) {
962             delegate_->UpdateUserAgent(GetUserAgent().value());
963         }
964         if (GetInitialScale()) {
965             delegate_->UpdateInitialScale(GetInitialScale().value());
966         }
967     }
968 
969     // Initialize events such as keyboard, focus, etc.
970     InitEvent();
971 
972     auto pipelineContext = PipelineContext::GetCurrentContext();
973     CHECK_NULL_VOID(pipelineContext);
974     pipelineContext->AddOnAreaChangeNode(host->GetId());
975 }
976 
ProcessVirtualKeyBoard(int32_t width,int32_t height,double keyboard)977 bool WebPattern::ProcessVirtualKeyBoard(int32_t width, int32_t height, double keyboard)
978 {
979     LOGI("Web ProcessVirtualKeyBoard width=%{public}d height=%{public}d keyboard=%{public}f",
980         width, height, keyboard);
981     CHECK_NULL_RETURN(delegate_, false);
982     if (!isFocus_) {
983         if (isVirtualKeyBoardShow_ == VkState::VK_SHOW) {
984             drawSize_.SetSize(drawSizeCache_);
985             UpdateWebLayoutSize(width, height);
986             isVirtualKeyBoardShow_ = VkState::VK_HIDE;
987         }
988         return false;
989     }
990     if (NearZero(keyboard)) {
991         drawSize_.SetSize(drawSizeCache_);
992         UpdateWebLayoutSize(width, height);
993         isVirtualKeyBoardShow_ = VkState::VK_HIDE;
994     } else if (isVirtualKeyBoardShow_ != VkState::VK_SHOW) {
995         drawSizeCache_.SetSize(drawSize_);
996         if (drawSize_.Height() <= (height - keyboard - GetCoordinatePoint()->GetY())) {
997             isVirtualKeyBoardShow_ = VkState::VK_SHOW;
998             return true;
999         }
1000         if (height - GetCoordinatePoint()->GetY() < keyboard) {
1001             return true;
1002         }
1003         drawSize_.SetHeight(height - keyboard - GetCoordinatePoint()->GetY());
1004         UpdateWebLayoutSize(width, height);
1005         isVirtualKeyBoardShow_ = VkState::VK_SHOW;
1006     }
1007     return true;
1008 }
1009 
UpdateWebLayoutSize(int32_t width,int32_t height)1010 void WebPattern::UpdateWebLayoutSize(int32_t width, int32_t height)
1011 {
1012     auto frameNode = GetHost();
1013     CHECK_NULL_VOID(frameNode);
1014     auto rect = frameNode->GetGeometryNode()->GetFrameRect();
1015     auto offset = Offset(GetCoordinatePoint()->GetX(), GetCoordinatePoint()->GetY());
1016     delegate_->SetBoundsOrResize(drawSize_, offset);
1017     rect.SetSize(SizeF(drawSize_.Width(), drawSize_.Height()));
1018     frameNode->GetRenderContext()->SyncGeometryProperties(rect);
1019     frameNode->MarkDirtyNode(PROPERTY_UPDATE_MEASURE_SELF);
1020     auto context = PipelineContext::GetCurrentContext();
1021     CHECK_NULL_VOID(context);
1022     context->SetRootRect(width, height, 0);
1023 }
1024 
HandleTouchDown(const TouchEventInfo & info,bool fromOverlay)1025 void WebPattern::HandleTouchDown(const TouchEventInfo& info, bool fromOverlay)
1026 {
1027     CHECK_NULL_VOID(delegate_);
1028     Offset touchOffset = Offset(0, 0);
1029     std::list<TouchInfo> touchInfos;
1030     if (!ParseTouchInfo(info, touchInfos)) {
1031         LOGE("handle touch down error");
1032         return;
1033     }
1034     for (auto& touchPoint : touchInfos) {
1035         if (fromOverlay) {
1036             touchPoint.x -= webOffset_.GetX();
1037             touchPoint.y -= webOffset_.GetY();
1038         }
1039         delegate_->HandleTouchDown(touchPoint.id, touchPoint.x, touchPoint.y);
1040     }
1041 }
1042 
HandleTouchUp(const TouchEventInfo & info,bool fromOverlay)1043 void WebPattern::HandleTouchUp(const TouchEventInfo& info, bool fromOverlay)
1044 {
1045     CHECK_NULL_VOID(delegate_);
1046     std::list<TouchInfo> touchInfos;
1047     if (!ParseTouchInfo(info, touchInfos)) {
1048         LOGE("handle touch up error");
1049         return;
1050     }
1051     for (auto& touchPoint : touchInfos) {
1052         if (fromOverlay) {
1053             touchPoint.x -= webOffset_.GetX();
1054             touchPoint.y -= webOffset_.GetY();
1055         }
1056         delegate_->HandleTouchUp(touchPoint.id, touchPoint.x, touchPoint.y);
1057     }
1058     if (!touchInfos.empty()) {
1059         WebRequestFocus();
1060     }
1061 }
1062 
HandleTouchMove(const TouchEventInfo & info,bool fromOverlay)1063 void WebPattern::HandleTouchMove(const TouchEventInfo& info, bool fromOverlay)
1064 {
1065     if (isDragging_) {
1066         return;
1067     }
1068     CHECK_NULL_VOID(delegate_);
1069     std::list<TouchInfo> touchInfos;
1070     if (!ParseTouchInfo(info, touchInfos)) {
1071         LOGE("handle touch move error");
1072         return;
1073     }
1074     for (auto& touchPoint : touchInfos) {
1075         if (fromOverlay) {
1076             touchPoint.x -= webOffset_.GetX();
1077             touchPoint.y -= webOffset_.GetY();
1078         }
1079         delegate_->HandleTouchMove(touchPoint.id, touchPoint.x, touchPoint.y);
1080     }
1081 }
1082 
HandleTouchCancel(const TouchEventInfo &)1083 void WebPattern::HandleTouchCancel(const TouchEventInfo& /*info*/)
1084 {
1085     CHECK_NULL_VOID(delegate_);
1086     delegate_->HandleTouchCancel();
1087 }
1088 
ParseTouchInfo(const TouchEventInfo & info,std::list<TouchInfo> & touchInfos)1089 bool WebPattern::ParseTouchInfo(const TouchEventInfo& info, std::list<TouchInfo>& touchInfos)
1090 {
1091     auto context = PipelineContext::GetCurrentContext();
1092     CHECK_NULL_RETURN(context, false);
1093     auto viewScale = context->GetViewScale();
1094     if (info.GetTouches().empty()) {
1095         return false;
1096     }
1097     for (const auto& point : info.GetTouches()) {
1098         TouchInfo touchInfo;
1099         touchInfo.id = point.GetFingerId();
1100         const Offset& location = point.GetLocalLocation();
1101         touchInfo.x = static_cast<float>(location.GetX() * viewScale);
1102         touchInfo.y = static_cast<float>(location.GetY() * viewScale);
1103         touchInfos.emplace_back(touchInfo);
1104     }
1105     return true;
1106 }
1107 
RequestFullScreen()1108 void WebPattern::RequestFullScreen()
1109 {
1110     if (isFullScreen_) {
1111         LOGE("The Web is already full screen when FullScreen");
1112         return;
1113     }
1114     auto context = PipelineContext::GetCurrentContext();
1115     CHECK_NULL_VOID(context);
1116     auto host = GetHost();
1117     CHECK_NULL_VOID(host);
1118     auto fullScreenManager = context->GetFullScreenManager();
1119     CHECK_NULL_VOID(fullScreenManager);
1120     fullScreenManager->RequestFullScreen(host);
1121     isFullScreen_ = true;
1122 }
1123 
ExitFullScreen()1124 void WebPattern::ExitFullScreen()
1125 {
1126     if (!isFullScreen_) {
1127         LOGE("The Web is not full screen when ExitFullScreen");
1128         return;
1129     }
1130     auto context = PipelineContext::GetCurrentContext();
1131     CHECK_NULL_VOID(context);
1132     auto host = GetHost();
1133     CHECK_NULL_VOID(host);
1134     auto fullScreenManager = context->GetFullScreenManager();
1135     CHECK_NULL_VOID(fullScreenManager);
1136     fullScreenManager->ExitFullScreen(host);
1137     isFullScreen_ = false;
1138 }
1139 
IsTouchHandleValid(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle)1140 bool WebPattern::IsTouchHandleValid(
1141     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle)
1142 {
1143     return (handle != nullptr) && (handle->IsEnable());
1144 }
1145 
IsTouchHandleShow(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle)1146 bool WebPattern::IsTouchHandleShow(
1147     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> handle)
1148 {
1149     CHECK_NULL_RETURN_NOLOG(handle, false);
1150     auto pipeline = PipelineBase::GetCurrentContext();
1151     CHECK_NULL_RETURN_NOLOG(pipeline, false);
1152     int y = static_cast<int32_t>(handle->GetY() / pipeline->GetDipScale());
1153     int edgeHeight = static_cast<int32_t>(handle->GetEdgeHeight() / pipeline->GetDipScale()) - 1;
1154     if (handle->GetAlpha() > 0 && y >= edgeHeight &&
1155         GreatNotEqual(GetHostFrameSize().value_or(SizeF()).Height(), handle->GetY())) {
1156         return true;
1157     }
1158     return false;
1159 }
1160 
GetTouchHandleOverlayType(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)1161 WebOverlayType WebPattern::GetTouchHandleOverlayType(
1162     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
1163     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
1164     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)
1165 {
1166     if (IsTouchHandleValid(insertHandle) &&
1167         !IsTouchHandleValid(startSelectionHandle) &&
1168         !IsTouchHandleValid(endSelectionHandle)) {
1169         return INSERT_OVERLAY;
1170     }
1171 
1172     if (!IsTouchHandleValid(insertHandle) &&
1173         IsTouchHandleValid(startSelectionHandle) &&
1174         IsTouchHandleValid(endSelectionHandle)) {
1175         return SELECTION_OVERLAY;
1176     }
1177 
1178     return INVALID_OVERLAY;
1179 }
1180 
GetCoordinatePoint()1181 std::optional<OffsetF> WebPattern::GetCoordinatePoint()
1182 {
1183     auto frameNode = GetHost();
1184     CHECK_NULL_RETURN_NOLOG(frameNode, std::nullopt);
1185     return frameNode->GetTransformRelativeOffset();
1186 }
1187 
ComputeTouchHandleRect(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> touchHandle)1188 RectF WebPattern::ComputeTouchHandleRect(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> touchHandle)
1189 {
1190     RectF paintRect;
1191     auto offset = GetCoordinatePoint().value_or(OffsetF());
1192     auto size = GetHostFrameSize().value_or(SizeF());
1193     float edgeHeight = touchHandle->GetEdgeHeight();
1194     float x = touchHandle->GetX();
1195     float y = touchHandle->GetY();
1196     if (x > size.Width()) {
1197         x = offset.GetX() + size.Width();
1198     } else {
1199         x = x + offset.GetX();
1200     }
1201 
1202     if (y < 0) {
1203         y = offset.GetY();
1204     } else if (y > size.Height()) {
1205         y = offset.GetY() + size.Height();
1206     } else {
1207         y = y + offset.GetY();
1208         y = y - edgeHeight;
1209     }
1210 
1211     x = x - SelectHandleInfo::GetDefaultLineWidth().ConvertToPx() / 2;
1212     paintRect.SetOffset({ x, y });
1213     paintRect.SetSize({ SelectHandleInfo::GetDefaultLineWidth().ConvertToPx(), edgeHeight });
1214     return paintRect;
1215 }
1216 
CloseSelectOverlay()1217 void WebPattern::CloseSelectOverlay()
1218 {
1219     auto pipeline = PipelineContext::GetCurrentContext();
1220     CHECK_NULL_VOID(pipeline);
1221     if (selectOverlayProxy_) {
1222         selectOverlayProxy_->Close();
1223         pipeline->GetSelectOverlayManager()->DestroySelectOverlay(selectOverlayProxy_);
1224         selectOverlayProxy_ = nullptr;
1225     }
1226 }
1227 
RegisterSelectOverlayCallback(SelectOverlayInfo & selectInfo,std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback)1228 void WebPattern::RegisterSelectOverlayCallback(SelectOverlayInfo& selectInfo,
1229     std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
1230     std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback)
1231 {
1232     int32_t flags = params->GetEditStateFlags();
1233     if (flags & OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_CUT) {
1234         selectInfo.menuCallback.onCut = [weak = AceType::WeakClaim(this), callback]() {
1235             CHECK_NULL_VOID_NOLOG(callback);
1236             callback->Continue(OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_CUT,
1237                 OHOS::NWeb::MenuEventFlags::EF_LEFT_MOUSE_BUTTON);
1238         };
1239     } else {
1240         selectInfo.menuInfo.showCut = false;
1241     }
1242     if (flags & OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_COPY) {
1243         selectInfo.menuCallback.onCopy = [weak = AceType::WeakClaim(this), callback]() {
1244             CHECK_NULL_VOID_NOLOG(callback);
1245             callback->Continue(OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_COPY,
1246                 OHOS::NWeb::MenuEventFlags::EF_LEFT_MOUSE_BUTTON);
1247         };
1248     } else {
1249         selectInfo.menuInfo.showCopy = false;
1250     }
1251     if (flags & OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_PASTE) {
1252         selectInfo.menuCallback.onPaste = [weak = AceType::WeakClaim(this), callback]() {
1253             CHECK_NULL_VOID_NOLOG(callback);
1254             callback->Continue(OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_PASTE,
1255                 OHOS::NWeb::MenuEventFlags::EF_LEFT_MOUSE_BUTTON);
1256         };
1257     } else {
1258         selectInfo.menuInfo.showPaste = false;
1259     }
1260     if (flags & OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_SELECT_ALL) {
1261         selectInfo.menuCallback.onSelectAll = [weak = AceType::WeakClaim(this), callback]() {
1262             CHECK_NULL_VOID_NOLOG(callback);
1263             callback->Continue(OHOS::NWeb::NWebQuickMenuParams::QM_EF_CAN_SELECT_ALL,
1264                 OHOS::NWeb::MenuEventFlags::EF_LEFT_MOUSE_BUTTON);
1265         };
1266     } else {
1267         selectInfo.menuInfo.showCopyAll = false;
1268     }
1269 }
1270 
RegisterSelectOverlayEvent(SelectOverlayInfo & selectInfo)1271 void WebPattern::RegisterSelectOverlayEvent(SelectOverlayInfo& selectInfo)
1272 {
1273     selectInfo.onHandleMoveDone = [weak = AceType::WeakClaim(this)](const RectF& rectF, bool isFirst) {
1274         auto webPattern = weak.Upgrade();
1275         CHECK_NULL_VOID(webPattern);
1276         webPattern->UpdateTouchHandleForOverlay();
1277         webPattern->SetSelectOverlayDragging(false);
1278     };
1279     selectInfo.onTouchDown = [weak = AceType::WeakClaim(this)](const TouchEventInfo& info) {
1280         auto webPattern = weak.Upgrade();
1281         CHECK_NULL_VOID(webPattern);
1282         webPattern->HandleTouchDown(info, true);
1283     };
1284     selectInfo.onTouchUp = [weak = AceType::WeakClaim(this)](const TouchEventInfo& info) {
1285         auto webPattern = weak.Upgrade();
1286         CHECK_NULL_VOID(webPattern);
1287         webPattern->HandleTouchUp(info, true);
1288     };
1289     selectInfo.onTouchMove = [weak = AceType::WeakClaim(this)](const TouchEventInfo& info) {
1290         auto webPattern = weak.Upgrade();
1291         CHECK_NULL_VOID(webPattern);
1292         if (webPattern->IsSelectOverlayDragging()) {
1293             webPattern->HandleTouchMove(info, true);
1294         }
1295     };
1296     selectInfo.onHandleMoveStart = [weak = AceType::WeakClaim(this)](bool isFirst) {
1297         auto webPattern = weak.Upgrade();
1298         CHECK_NULL_VOID(webPattern);
1299         webPattern->SetSelectOverlayDragging(true);
1300     };
1301 }
1302 
RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback)1303 bool WebPattern::RunQuickMenu(std::shared_ptr<OHOS::NWeb::NWebQuickMenuParams> params,
1304     std::shared_ptr<OHOS::NWeb::NWebQuickMenuCallback> callback)
1305 {
1306     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertTouchHandle =
1307         params->GetTouchHandleState(OHOS::NWeb::NWebTouchHandleState::TouchHandleType::INSERT_HANDLE);
1308     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> beginTouchHandle =
1309         params->GetTouchHandleState(OHOS::NWeb::NWebTouchHandleState::TouchHandleType::SELECTION_BEGIN_HANDLE);
1310     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endTouchHandle =
1311         params->GetTouchHandleState(OHOS::NWeb::NWebTouchHandleState::TouchHandleType::SELECTION_END_HANDLE);
1312     WebOverlayType overlayType = GetTouchHandleOverlayType(insertTouchHandle,
1313                                                            beginTouchHandle,
1314                                                            endTouchHandle);
1315     if (overlayType == INVALID_OVERLAY) {
1316         return false;
1317     }
1318     if (selectOverlayProxy_) {
1319         CloseSelectOverlay();
1320     }
1321     auto pipeline = PipelineContext::GetCurrentContext();
1322     CHECK_NULL_RETURN(pipeline, false);
1323     auto theme = pipeline->GetTheme<TextOverlayTheme>();
1324     CHECK_NULL_RETURN(theme, false);
1325     selectHotZone_ = theme->GetHandleHotZoneRadius().ConvertToPx();
1326     SelectOverlayInfo selectInfo;
1327     selectInfo.isSingleHandle = (overlayType == INSERT_OVERLAY);
1328     selectInfo.hitTestMode = HitTestMode::HTMDEFAULT;
1329     if (selectInfo.isSingleHandle) {
1330         selectInfo.firstHandle.isShow = IsTouchHandleShow(insertTouchHandle);
1331         selectInfo.firstHandle.paintRect = ComputeTouchHandleRect(insertTouchHandle);
1332         selectInfo.secondHandle.isShow = false;
1333     } else {
1334         selectInfo.firstHandle.isShow = IsTouchHandleShow(beginTouchHandle);
1335         selectInfo.firstHandle.paintRect = ComputeTouchHandleRect(beginTouchHandle);
1336         selectInfo.secondHandle.isShow = IsTouchHandleShow(endTouchHandle);
1337         selectInfo.secondHandle.paintRect = ComputeTouchHandleRect(endTouchHandle);
1338     }
1339     selectInfo.menuInfo.menuIsShow = true;
1340     RegisterSelectOverlayCallback(selectInfo, params, callback);
1341     RegisterSelectOverlayEvent(selectInfo);
1342     selectOverlayProxy_ = pipeline->GetSelectOverlayManager()->CreateAndShowSelectOverlay(selectInfo);
1343     selectMenuInfo_ = selectInfo.menuInfo;
1344     insertHandle_ = insertTouchHandle;
1345     startSelectionHandle_ = beginTouchHandle;
1346     endSelectionHandle_ = endTouchHandle;
1347     return selectOverlayProxy_ ? true : false;
1348 }
1349 
OnQuickMenuDismissed()1350 void WebPattern::OnQuickMenuDismissed()
1351 {
1352     CloseSelectOverlay();
1353 }
1354 
OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)1355 void WebPattern::OnTouchSelectionChanged(std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> insertHandle,
1356     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> startSelectionHandle,
1357     std::shared_ptr<OHOS::NWeb::NWebTouchHandleState> endSelectionHandle)
1358 {
1359     WebOverlayType overlayType = GetTouchHandleOverlayType(insertHandle,
1360                                                            startSelectionHandle,
1361                                                            endSelectionHandle);
1362     if (overlayType == INVALID_OVERLAY) {
1363         CloseSelectOverlay();
1364         return;
1365     }
1366     auto pipeline = PipelineContext::GetCurrentContext();
1367     CHECK_NULL_VOID(pipeline);
1368     auto theme = pipeline->GetTheme<TextOverlayTheme>();
1369     CHECK_NULL_VOID(theme);
1370     selectHotZone_ = theme->GetHandleHotZoneRadius().ConvertToPx();
1371     insertHandle_ = insertHandle;
1372     startSelectionHandle_ = startSelectionHandle;
1373     endSelectionHandle_ = endSelectionHandle;
1374     if (!selectOverlayProxy_) {
1375         if (overlayType == INSERT_OVERLAY) {
1376             SelectOverlayInfo selectInfo;
1377             selectInfo.isSingleHandle = true;
1378             selectInfo.firstHandle.isShow = IsTouchHandleShow(insertHandle_);
1379             selectInfo.firstHandle.paintRect = ComputeTouchHandleRect(insertHandle_);
1380             selectInfo.secondHandle.isShow = false;
1381             selectInfo.menuInfo.menuDisable = true;
1382             selectInfo.menuInfo.menuIsShow = false;
1383             selectInfo.hitTestMode = HitTestMode::HTMDEFAULT;
1384             RegisterSelectOverlayEvent(selectInfo);
1385             selectOverlayProxy_ = pipeline->GetSelectOverlayManager()->CreateAndShowSelectOverlay(selectInfo);
1386         }
1387     } else {
1388         if (overlayType == INSERT_OVERLAY) {
1389             UpdateTouchHandleForOverlay();
1390         }
1391     }
1392 }
1393 
OnCursorChange(const OHOS::NWeb::CursorType & type,const OHOS::NWeb::NWebCursorInfo & info)1394 bool WebPattern::OnCursorChange(const OHOS::NWeb::CursorType& type, const OHOS::NWeb::NWebCursorInfo& info)
1395 {
1396     (void)info;
1397     auto pipeline = PipelineContext::GetCurrentContext();
1398     CHECK_NULL_RETURN(pipeline, false);
1399     auto windowId = pipeline->GetWindowId();
1400     auto mouseStyle = MouseStyle::CreateMouseStyle();
1401     int32_t curPointerStyle = 0;
1402     if (mouseStyle->GetPointerStyle(windowId, curPointerStyle) == -1) {
1403         LOGE("OnCursorChange GetPointerStyle failed");
1404         return false;
1405     }
1406     MouseFormat pointStyle = MouseFormat::DEFAULT;
1407     int64_t idx = BinarySearchFindIndex(g_cursorTypeMap, ArraySize(g_cursorTypeMap), type);
1408     if (idx >= 0) {
1409         pointStyle = g_cursorTypeMap[idx].value;
1410     }
1411     if ((int32_t)pointStyle != curPointerStyle) {
1412         mouseStyle->SetPointerStyle(windowId, pointStyle);
1413     }
1414     return true;
1415 }
1416 
OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)1417 void WebPattern::OnSelectPopupMenu(std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuParam> params,
1418     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)
1419 {
1420     CHECK_NULL_VOID(params);
1421     CHECK_NULL_VOID(callback);
1422     auto host = GetHost();
1423     CHECK_NULL_VOID(host);
1424     auto id = host->GetId();
1425     std::vector<SelectParam> selectParam;
1426     for (auto& item : params->menuItems) {
1427         selectParam.push_back({
1428             item.label, ""
1429         });
1430     }
1431     auto menu = MenuView::Create(selectParam, id);
1432     auto context = PipelineContext::GetCurrentContext();
1433     CHECK_NULL_VOID(context);
1434     auto eventHub = host->GetEventHub<WebEventHub>();
1435     auto destructor = [weak = WeakClaim(this), id]() {
1436         auto pattern = weak.Upgrade();
1437         CHECK_NULL_VOID(pattern);
1438         auto pipeline = NG::PipelineContext::GetCurrentContext();
1439         CHECK_NULL_VOID(pipeline);
1440         auto overlayManager = pipeline->GetOverlayManager();
1441         CHECK_NULL_VOID(overlayManager);
1442         pattern->SetSelectPopupMenuShowing(false);
1443         overlayManager->DeleteMenu(id);
1444     };
1445     CHECK_NULL_VOID(eventHub);
1446     eventHub->SetOnDisappear(destructor);
1447 
1448     WebPattern::RegisterSelectPopupCallback(menu, callback);
1449     auto overlayManager = context->GetOverlayManager();
1450     CHECK_NULL_VOID(overlayManager);
1451     overlayManager->RegisterOnHideMenu([weak = WeakClaim(this), callback]() {
1452         auto pattern = weak.Upgrade();
1453         CHECK_NULL_VOID(pattern);
1454         callback->Cancel();
1455         pattern->SetSelectPopupMenuShowing(false);
1456     });
1457     auto offset = GetSelectPopupPostion(params->bounds);
1458     selectPopupMenuShowing_ = true;
1459     overlayManager->ShowMenu(id, offset, menu);
1460 }
1461 
RegisterSelectPopupCallback(RefPtr<FrameNode> & menu,std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)1462 void WebPattern::RegisterSelectPopupCallback(RefPtr<FrameNode>& menu,
1463     std::shared_ptr<OHOS::NWeb::NWebSelectPopupMenuCallback> callback)
1464 {
1465     auto menuContainer = AceType::DynamicCast<FrameNode>(menu->GetChildAtIndex(0));
1466     CHECK_NULL_VOID(menuContainer);
1467     auto menuPattern = menuContainer->GetPattern<MenuPattern>();
1468     CHECK_NULL_VOID(menuPattern);
1469     auto options = menuPattern->GetOptions();
1470     for (auto&& option : options) {
1471         auto selectCallback = [callback](int32_t index) {
1472             std::vector<int32_t> indices { static_cast<int32_t>(index) };
1473             callback->Continue(indices);
1474         };
1475         auto optionNode = AceType::DynamicCast<FrameNode>(option);
1476         if (optionNode) {
1477             auto hub = optionNode->GetEventHub<OptionEventHub>();
1478             if (!hub) {
1479                 continue;
1480             }
1481             hub->SetOnSelect(std::move(selectCallback));
1482             optionNode->MarkModifyDone();
1483         }
1484     }
1485 }
1486 
GetSelectPopupPostion(const OHOS::NWeb::SelectMenuBound & bounds)1487 OffsetF WebPattern::GetSelectPopupPostion(const OHOS::NWeb::SelectMenuBound& bounds)
1488 {
1489     auto offset = GetCoordinatePoint().value_or(OffsetF());
1490     offset.AddX(bounds.x);
1491     offset.AddY(bounds.y + bounds.height);
1492     return offset;
1493 }
1494 
UpdateTouchHandleForOverlay()1495 void WebPattern::UpdateTouchHandleForOverlay()
1496 {
1497     WebOverlayType overlayType = GetTouchHandleOverlayType(insertHandle_,
1498                                                            startSelectionHandle_,
1499                                                            endSelectionHandle_);
1500     CHECK_NULL_VOID_NOLOG(selectOverlayProxy_);
1501     if (overlayType == INVALID_OVERLAY) {
1502         CloseSelectOverlay();
1503         return;
1504     }
1505     SelectHandleInfo firstHandleInfo;
1506     SelectHandleInfo secondHandleInfo;
1507     SelectMenuInfo menuInfo;
1508     if (overlayType == INSERT_OVERLAY) {
1509         firstHandleInfo.isShow = IsTouchHandleShow(insertHandle_);
1510         firstHandleInfo.paintRect = ComputeTouchHandleRect(insertHandle_);
1511         menuInfo.menuDisable = true;
1512         menuInfo.menuIsShow = false;
1513         selectOverlayProxy_->UpdateFirstSelectHandleInfo(firstHandleInfo);
1514         selectOverlayProxy_->UpdateSelectMenuInfo(menuInfo);
1515     } else {
1516         firstHandleInfo.isShow = IsTouchHandleShow(startSelectionHandle_);
1517         firstHandleInfo.paintRect = ComputeTouchHandleRect(startSelectionHandle_);
1518         secondHandleInfo.isShow = IsTouchHandleShow(endSelectionHandle_);
1519         secondHandleInfo.paintRect = ComputeTouchHandleRect(endSelectionHandle_);
1520         selectOverlayProxy_->UpdateFirstSelectHandleInfo(firstHandleInfo);
1521         selectOverlayProxy_->UpdateSecondSelectHandleInfo(secondHandleInfo);
1522         selectOverlayProxy_->UpdateSelectMenuInfo(selectMenuInfo_);
1523     }
1524 }
1525 
UpdateLocale()1526 void WebPattern::UpdateLocale()
1527 {
1528     CHECK_NULL_VOID(delegate_);
1529     delegate_->UpdateLocale();
1530 }
1531 
OnWindowShow()1532 void WebPattern::OnWindowShow()
1533 {
1534     if (isWindowShow_) {
1535         return;
1536     }
1537 
1538     LOGI("web OnWindowShow called");
1539     CHECK_NULL_VOID(delegate_);
1540     delegate_->ShowWebView();
1541     isWindowShow_ = true;
1542 }
1543 
OnWindowHide()1544 void WebPattern::OnWindowHide()
1545 {
1546     if (!isWindowShow_) {
1547         return;
1548     }
1549 
1550     LOGI("web OnWindowHide called");
1551     CHECK_NULL_VOID(delegate_);
1552     delegate_->HideWebView();
1553     needOnFocus_ = false;
1554     isWindowShow_ = false;
1555 }
1556 
OnInActive()1557 void WebPattern::OnInActive()
1558 {
1559     if (!isActive_) {
1560         return;
1561     }
1562 
1563     LOGI("web OnInActive called");
1564     CHECK_NULL_VOID(delegate_);
1565     delegate_->OnInactive();
1566     isActive_ = false;
1567 }
1568 
OnActive()1569 void WebPattern::OnActive()
1570 {
1571     if (isActive_) {
1572         return;
1573     }
1574 
1575     LOGI("web OnActive called");
1576     CHECK_NULL_VOID(delegate_);
1577     delegate_->OnActive();
1578     isActive_ = true;
1579 }
1580 
OnVisibleChange(bool isVisible)1581 void WebPattern::OnVisibleChange(bool isVisible)
1582 {
1583     if (!isVisible) {
1584         LOGI("web is not visible");
1585         CloseSelectOverlay();
1586     }
1587 }
1588 
UpdateBackgroundColorRightNow(int32_t color)1589 void WebPattern::UpdateBackgroundColorRightNow(int32_t color)
1590 {
1591     auto host = GetHost();
1592     CHECK_NULL_VOID(host);
1593     auto renderContext = host->GetRenderContext();
1594     CHECK_NULL_VOID(renderContext);
1595     renderContext->UpdateBackgroundColor(Color(static_cast<uint32_t>(color)));
1596 }
1597 } // namespace OHOS::Ace::NG
1598