diff --git a/libcef/browser/osr/render_widget_host_view_osr.cc b/libcef/browser/osr/render_widget_host_view_osr.cc index 7964a3bf0e21e6b12a95ae511d00881cfdf8714f..bd8232a606e49887a94f07d61fe4e71d48d4ab98 100644 --- a/libcef/browser/osr/render_widget_host_view_osr.cc +++ b/libcef/browser/osr/render_widget_host_view_osr.cc @@ -1043,7 +1043,9 @@ void CefRenderWidgetHostViewOSR::SelectionBoundsChanged( base::i18n::TextDirection focus_dir, const gfx::Rect& bounding_box, bool is_anchor_first) { - is_editable_node_ = true; + if (last_key_code_ != ui::VKEY_RETURN) { + is_editable_node_ = true; + } if (!browser_impl_) { LOG(ERROR) << "browser_impl_ is nullptr"; return; @@ -1452,6 +1454,9 @@ void CefRenderWidgetHostViewOSR::SendKeyEvent( const content::NativeWebKeyboardEvent& event) { TRACE_EVENT0("cef", "CefRenderWidgetHostViewOSR::SendKeyEvent"); content::RenderWidgetHostImpl* target_host = render_widget_host_; +#if BUILDFLAG(IS_OHOS) + last_key_code_ = event.windows_key_code; +#endif #if !BUILDFLAG(IS_OHOS) if (selection_controller_client_) { @@ -1631,7 +1636,6 @@ void CefRenderWidgetHostViewOSR::SendTouchEvent(const CefTouchEvent& event) { #if BUILDFLAG(IS_OHOS) if (event.type == CEF_TET_PRESSED) { - is_editable_node_ = false; auto compositor = CefRenderWidgetHostViewOSR::GetCompositor( browser_impl_->GetAcceleratedWidget()); if (compositor) { @@ -1749,20 +1753,27 @@ void CefRenderWidgetHostViewOSR::OnUpdateTextInputStateCalled( content::TextInputManager* text_input_manager, content::RenderWidgetHostViewBase* updated_view, bool did_update_state) { - const auto state = text_input_manager->GetTextInputState(); - if (state && state->type == ui::TEXT_INPUT_TYPE_TEXT_AREA && !is_need_show_keyboard_) { - is_need_show_keyboard_ = true; - LOG(INFO) << "In this type of area, there is no need to pull up the keyboard when pressing"; + if (pointer_state_.GetAction() == ui::MotionEvent::Action::DOWN) { + LOG(INFO) << "The keyboard status is not updated when pressed"; return; } - if (state && !state->show_ime_if_needed) { - LOG(INFO) << "Do not need show ime"; + const auto state = text_input_manager->GetTextInputState(); + if (state && state->type == ui::TEXT_INPUT_TYPE_TEXT_AREA && + !is_need_show_keyboard_) { + is_need_show_keyboard_ = true; + LOG(INFO) << "In this type of area, there is no need to pull up the " + "keyboard when pressing"; return; } + CefRefPtr handler = + browser_impl_->GetClient()->GetRenderHandler(); + CHECK(handler); + CefRenderHandler::TextInputMode mode = CEF_TEXT_INPUT_MODE_NONE; bool show_keyboard = false; + InitHideKeyboardFlag(); if (state && state->type != ui::TEXT_INPUT_TYPE_NONE) { static_assert( static_cast(CEF_TEXT_INPUT_MODE_MAX) == @@ -1770,35 +1781,66 @@ void CefRenderWidgetHostViewOSR::OnUpdateTextInputStateCalled( "Enum values in cef_text_input_mode_t must match ui::TextInputMode"); mode = static_cast(state->mode); show_keyboard = state->show_ime_if_needed; + is_need_hide_keyboard_ = false; + lastHideKeyboardTime_ = std::chrono::high_resolution_clock::now(); + } + if (state && !state->show_ime_if_needed && did_update_state) { + LOG(INFO) << "Autofocus requires a keyboard to be bound, " + "but there is no need to pull up the keyboard"; + handler->OnVirtualKeyboardRequested(browser_impl_->GetBrowser(), mode, + state->show_ime_if_needed); + return; } - CefRefPtr handler = - browser_impl_->GetClient()->GetRenderHandler(); - CHECK(handler); - #if BUILDFLAG(IS_OHOS) if (state && (state->type == ui::TEXT_INPUT_TYPE_NUMBER || state->type == ui::TEXT_INPUT_TYPE_TELEPHONE)) { mode = CEF_TEXT_INPUT_MODE_NUMERIC; - } else if (updated_view && text_input_manager->IsRegistered(updated_view) && - mode == CEF_TEXT_INPUT_MODE_NONE && is_editable_node_) { - LOG(INFO) << "add selection removeAllRange and addRange"; - mode = CEF_TEXT_INPUT_MODE_DEFAULT; - show_keyboard = true; + } + + if (!show_keyboard) { + last_key_code_ = -1; } #endif - handler->OnVirtualKeyboardRequested(browser_impl_->GetBrowser(), mode, - show_keyboard); - is_need_show_keyboard_ = false; + if (state && state->show_ime_if_needed) { + handler->OnVirtualKeyboardRequested(browser_impl_->GetBrowser(), mode, + show_keyboard); + is_need_show_keyboard_ = false; + } else if (!state || mode == CEF_TEXT_INPUT_MODE_NONE) { + CEF_POST_DELAYED_TASK( + CEF_UIT, + base::BindOnce( + &CefRenderWidgetHostViewOSR::HideVirtualKeyboardRequested, + weak_ptr_factory_.GetWeakPtr()), + 20); + } +} + +void CefRenderWidgetHostViewOSR::HideVirtualKeyboardRequested() { + if (is_need_hide_keyboard_) { + CefRefPtr handler = + browser_impl_->GetClient()->GetRenderHandler(); + CHECK(handler); + handler->OnVirtualKeyboardRequested(browser_impl_->GetBrowser(), CEF_TEXT_INPUT_MODE_NONE, + false); + } + is_need_hide_keyboard_ = true; +} + +void CefRenderWidgetHostViewOSR::InitHideKeyboardFlag() { + auto nowTime = std::chrono::high_resolution_clock::now(); + std::chrono::duration diff = + std::chrono::duration_cast( + nowTime - lastHideKeyboardTime_); + if (diff.count() >= 20) { + is_need_hide_keyboard_ = true; + } } void CefRenderWidgetHostViewOSR::FocusedNodeChanged( bool is_editable_node, const gfx::Rect& node_bounds_in_screen) { - CefRefPtr handler = - browser_impl_->GetClient()->GetRenderHandler(); - CHECK(handler); - + LOG(DEBUG) << "CefRenderWidgetHostViewOSR::FocusedNodeChanged"; is_editable_node_ = is_editable_node; } diff --git a/libcef/browser/osr/render_widget_host_view_osr.h b/libcef/browser/osr/render_widget_host_view_osr.h index 34347abd785c0fd3eff843400e16c998716b9251..900bab3a5e326e4adc16ec2430f870e23444f962 100644 --- a/libcef/browser/osr/render_widget_host_view_osr.h +++ b/libcef/browser/osr/render_widget_host_view_osr.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "include/cef_base.h" #include "include/cef_browser.h" @@ -413,6 +414,9 @@ class CefRenderWidgetHostViewOSR void OnScaleChanged(float old_page_scale_factor, float nwe_page_scale_factor); + void HideVirtualKeyboardRequested(); + void InitHideKeyboardFlag(); + // The last selection bounds reported to the view. gfx::SelectionBound selection_start_; gfx::SelectionBound selection_end_; @@ -479,8 +483,11 @@ class CefRenderWidgetHostViewOSR bool should_wait_ = false; bool is_select_text_ = false; bool is_editable_node_ = false; + int last_key_code_ = -1; gfx::Size viewport_size_in_pixels_; bool is_need_show_keyboard_ = false; + bool is_need_hide_keyboard_ = true; + std::chrono::high_resolution_clock::time_point lastHideKeyboardTime_; #endif CefRefPtr browser_impl_;