From 2e370fa4d5d3cadd58182be18b9aeb59e32ef58e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 26 Jan 2019 21:58:43 +0100 Subject: [PATCH] LibGUI: Don't consider a GWidget focused if the window is inactive. --- LibGUI/GEventLoop.cpp | 12 +++++++++--- LibGUI/GEventLoop.h | 1 + LibGUI/GWidget.cpp | 2 ++ LibGUI/GWindow.cpp | 11 ++++++++++- LibGUI/GWindow.h | 3 ++- 5 files changed, 24 insertions(+), 5 deletions(-) diff --git a/LibGUI/GEventLoop.cpp b/LibGUI/GEventLoop.cpp index 96806f8559b..40e64cd969a 100644 --- a/LibGUI/GEventLoop.cpp +++ b/LibGUI/GEventLoop.cpp @@ -86,6 +86,14 @@ void GEventLoop::handle_paint_event(const GUI_Event& event, GWindow& window) post_event(&window, make(event.paint.rect)); } +void GEventLoop::handle_window_activation_event(const GUI_Event& event, GWindow& window) +{ +#ifdef GEVENTLOOP_DEBUG + dbgprintf("WID=%x WindowActivation\n", event.window_id); +#endif + post_event(&window, make(event.type == GUI_Event::Type::WindowActivated ? GEvent::WindowBecameActive : GEvent::WindowBecameInactive)); +} + void GEventLoop::handle_key_event(const GUI_Event& event, GWindow& window) { #ifdef GEVENTLOOP_DEBUG @@ -162,10 +170,8 @@ void GEventLoop::wait_for_event() handle_mouse_event(event, *window); break; case GUI_Event::Type::WindowActivated: - dbgprintf("WID=%x WindowActivated\n", event.window_id); - break; case GUI_Event::Type::WindowDeactivated: - dbgprintf("WID=%x WindowDeactivated\n", event.window_id); + handle_window_activation_event(event, *window); break; case GUI_Event::Type::KeyDown: case GUI_Event::Type::KeyUp: diff --git a/LibGUI/GEventLoop.h b/LibGUI/GEventLoop.h index b7150935c41..d040c369007 100644 --- a/LibGUI/GEventLoop.h +++ b/LibGUI/GEventLoop.h @@ -28,6 +28,7 @@ private: void handle_paint_event(const GUI_Event&, GWindow&); void handle_mouse_event(const GUI_Event&, GWindow&); void handle_key_event(const GUI_Event&, GWindow&); + void handle_window_activation_event(const GUI_Event&, GWindow&); struct QueuedEvent { GObject* receiver { nullptr }; diff --git a/LibGUI/GWidget.cpp b/LibGUI/GWidget.cpp index a650510ecad..e2b77e33ecb 100644 --- a/LibGUI/GWidget.cpp +++ b/LibGUI/GWidget.cpp @@ -145,6 +145,8 @@ bool GWidget::is_focused() const auto* win = window(); if (!win) return false; + if (!win->is_active()) + return false; return win->focused_widget() == this; } diff --git a/LibGUI/GWindow.cpp b/LibGUI/GWindow.cpp index 41f086b15d3..b6de4a75113 100644 --- a/LibGUI/GWindow.cpp +++ b/LibGUI/GWindow.cpp @@ -81,6 +81,7 @@ void GWindow::event(GEvent& event) ASSERT(result.widget); return result.widget->event(*local_event); } + return; } if (event.is_paint_event()) { @@ -94,6 +95,7 @@ void GWindow::event(GEvent& event) GUI_Rect gui_rect = rect; int rc = gui_notify_paint_finished(m_window_id, &gui_rect); ASSERT(rc == 0); + return; } if (event.is_key_event()) { @@ -102,7 +104,14 @@ void GWindow::event(GEvent& event) return m_focused_widget->event(event); } - return GObject::event(event); + if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) { + m_is_active = event.type() == GEvent::WindowBecameActive; + if (m_focused_widget) + m_focused_widget->update(); + return; + } + + GObject::event(event); } bool GWindow::is_visible() const diff --git a/LibGUI/GWindow.h b/LibGUI/GWindow.h index 12ade852b52..a958207f081 100644 --- a/LibGUI/GWindow.h +++ b/LibGUI/GWindow.h @@ -32,6 +32,7 @@ public: virtual void event(GEvent&) override; bool is_visible() const; + bool is_active() const { return m_is_active; } void close(); @@ -39,7 +40,6 @@ public: const GWidget* main_widget() const { return m_main_widget; } void set_main_widget(GWidget*); - GWidget* focused_widget() { return m_focused_widget; } const GWidget* focused_widget() const { return m_focused_widget; } void set_focused_widget(GWidget*); @@ -51,6 +51,7 @@ public: private: RetainPtr m_backing; int m_window_id { -1 }; + bool m_is_active { false }; GWidget* m_main_widget { nullptr }; GWidget* m_focused_widget { nullptr }; };