Skip to content

Commit 374449e

Browse files
committed
Fix for #43
1 parent 5aeb268 commit 374449e

2 files changed

Lines changed: 11 additions & 74 deletions

File tree

wrywebview/src/main/kotlin/io/github/kdroidfilter/webview/wry/WryWebViewPanel.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class WryWebViewPanel(
6060
// Request focus when clicked to capture keyboard events
6161
host.addMouseListener(object : MouseAdapter() {
6262
override fun mousePressed(e: MouseEvent?) {
63+
host.requestFocusInWindow()
6364
requestWebViewFocus()
6465
}
6566
})
@@ -342,7 +343,10 @@ class WryWebViewPanel(
342343
fun isReady(): Boolean = webviewId != null
343344

344345
fun requestWebViewFocus() {
345-
val action = { webviewId?.let { NativeBindings.focus(it) } }
346+
val action = {
347+
host.requestFocusInWindow()
348+
webviewId?.let { NativeBindings.focus(it) }
349+
}
346350
if (SwingUtilities.isEventDispatchThread()) {
347351
action()
348352
} else {

wrywebview/src/main/rust/lib.rs

Lines changed: 6 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -355,51 +355,20 @@ fn create_webview_inner(
355355
// On Linux, set up focus handling for the GTK widget
356356
#[cfg(target_os = "linux")]
357357
{
358-
use gdkx11::glib::translate::ToGlibPtr;
359-
use gdkx11::glib::Cast;
360-
use gdkx11::X11Display;
361358
use gtk::prelude::WidgetExt;
362359

363360
let gtk_widget = webview.webview();
364361
gtk_widget.set_can_focus(true);
365362

366-
// Connect to button-press-event to grab focus when clicked using X11
363+
// Connect to button-press-event to grab focus when clicked.
364+
// Avoid forcing raw X11 focus here because the host hierarchy may be
365+
// managed by AWT/Swing and direct XSetInputFocus can desynchronize focus.
367366
gtk_widget.connect_button_press_event(|widget, _event| {
368367
wry_log!("[wrywebview] button_press_event -> grab_focus");
369-
370-
// Use X11 focus directly for proper keyboard input
371-
if let Some(gdk_window) = widget.window() {
372-
if let Some(display) = gdk::Display::default() {
373-
if let Ok(x11_display) = display.downcast::<X11Display>() {
374-
unsafe {
375-
let gdk_window_ptr: *mut gdk::ffi::GdkWindow = gdk_window.to_glib_none().0;
376-
let xid = gdkx11::ffi::gdk_x11_window_get_xid(
377-
gdk_window_ptr as *mut gdkx11::ffi::GdkX11Window,
378-
);
379-
380-
if xid != 0 {
381-
let x11_display_ptr: *mut gdkx11::ffi::GdkX11Display = x11_display.to_glib_none().0;
382-
let x_display = gdkx11::ffi::gdk_x11_display_get_xdisplay(x11_display_ptr);
383-
384-
if !x_display.is_null() {
385-
x11::xlib::XSetInputFocus(
386-
x_display as *mut x11::xlib::Display,
387-
xid,
388-
x11::xlib::RevertToParent,
389-
x11::xlib::CurrentTime,
390-
);
391-
wry_log!("[wrywebview] button_press XSetInputFocus xid=0x{:x}", xid);
392-
}
393-
}
394-
}
395-
}
396-
}
397-
}
398-
399368
widget.grab_focus();
400369
gtk::glib::Propagation::Proceed
401370
});
402-
wry_log!("[wrywebview] gtk focus handling configured with X11 support");
371+
wry_log!("[wrywebview] gtk focus handling configured");
403372
}
404373

405374
let id = register(webview, state, web_context)?;
@@ -723,13 +692,10 @@ pub fn reload(id: u64) -> Result<(), WebViewError> {
723692
fn focus_inner(id: u64) -> Result<(), WebViewError> {
724693
wry_log!("[wrywebview] focus id={}", id);
725694
with_webview(id, |webview| {
726-
// On Linux, we need to use X11 focus directly since the GTK widget
727-
// is embedded in a foreign (AWT/Swing) window hierarchy
695+
// On Linux, keep focus changes within GTK/Wry to avoid desynchronizing
696+
// focus with the host AWT/Swing hierarchy.
728697
#[cfg(target_os = "linux")]
729698
{
730-
use gdkx11::glib::translate::ToGlibPtr;
731-
use gdkx11::glib::Cast;
732-
use gdkx11::X11Display;
733699
use gtk::prelude::WidgetExt;
734700

735701
let gtk_widget = webview.webview();
@@ -740,39 +706,6 @@ fn focus_inner(id: u64) -> Result<(), WebViewError> {
740706
gtk_widget.realize();
741707
}
742708

743-
// Get the GDK window and use X11 to set focus
744-
if let Some(gdk_window) = gtk_widget.window() {
745-
// Get the X11 display from GDK
746-
if let Some(display) = gdk::Display::default() {
747-
if let Ok(x11_display) = display.downcast::<X11Display>() {
748-
unsafe {
749-
// Get the X11 window ID (XID) from the GDK window
750-
let gdk_window_ptr: *mut gdk::ffi::GdkWindow = gdk_window.to_glib_none().0;
751-
let xid = gdkx11::ffi::gdk_x11_window_get_xid(
752-
gdk_window_ptr as *mut gdkx11::ffi::GdkX11Window,
753-
);
754-
755-
if xid != 0 {
756-
// Get the raw X11 display pointer
757-
let x11_display_ptr: *mut gdkx11::ffi::GdkX11Display = x11_display.to_glib_none().0;
758-
let x_display = gdkx11::ffi::gdk_x11_display_get_xdisplay(x11_display_ptr);
759-
760-
if !x_display.is_null() {
761-
// XSetInputFocus: RevertToParent = 2, CurrentTime = 0
762-
x11::xlib::XSetInputFocus(
763-
x_display as *mut x11::xlib::Display,
764-
xid,
765-
x11::xlib::RevertToParent,
766-
x11::xlib::CurrentTime,
767-
);
768-
wry_log!("[wrywebview] XSetInputFocus xid=0x{:x}", xid);
769-
}
770-
}
771-
}
772-
}
773-
}
774-
}
775-
776709
// Also call GTK grab_focus as a fallback
777710
gtk_widget.grab_focus();
778711
wry_log!("[wrywebview] gtk grab_focus called");

0 commit comments

Comments
 (0)