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