Skip to content

Commit 99c5f3c

Browse files
committed
Fix Window::IsFocused() on Linux
IsFocused() asked the seat's keyboard for the window at its position, the same call 522df16 removed from WindowManager::GetCurrent(): it is about pointer position and GDK rejects keyboard devices, so it logged a Gdk-CRITICAL assertion on every call and always answered false (libnativeapi/nativeapi#6). Ask the toplevel instead — gtk_window_is_active(), or GDK_WINDOW_STATE_FOCUSED for a bare GdkWindow — which works on both X11 and Wayland.
1 parent 26963d8 commit 99c5f3c

1 file changed

Lines changed: 8 additions & 11 deletions

File tree

‎src/platform/linux/window_linux.cpp‎

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,16 @@ void Window::Blur() {
191191
}
192192

193193
bool Window::IsFocused() const {
194+
// Asking the seat's keyboard for the window at its position is not an option: that
195+
// call is about pointer position and GDK rejects keyboard devices outright, so it
196+
// only logs an assertion failure and never finds a window. The toplevel's own
197+
// state works on both X11 and Wayland.
198+
if (pimpl_->widget_ && GTK_IS_WINDOW(pimpl_->widget_)) {
199+
return gtk_window_is_active(GTK_WINDOW(pimpl_->widget_));
200+
}
194201
if (!pimpl_->gdk_window_)
195202
return false;
196-
// Check if this window is the focus window of its display
197-
GdkDisplay* display = gdk_window_get_display(pimpl_->gdk_window_);
198-
GdkSeat* seat = gdk_display_get_default_seat(display);
199-
if (seat) {
200-
GdkDevice* keyboard = gdk_seat_get_keyboard(seat);
201-
if (keyboard) {
202-
GdkWindow* focus_window = gdk_device_get_window_at_position(keyboard, nullptr, nullptr);
203-
return focus_window == pimpl_->gdk_window_;
204-
}
205-
}
206-
return false;
203+
return gdk_window_get_state(pimpl_->gdk_window_) & GDK_WINDOW_STATE_FOCUSED;
207204
}
208205

209206
void Window::Show() {

0 commit comments

Comments
 (0)