diff --git a/Libraries/LibGUI/Application.cpp b/Libraries/LibGUI/Application.cpp index 55cc9373c9d..56099a8e570 100644 --- a/Libraries/LibGUI/Application.cpp +++ b/Libraries/LibGUI/Application.cpp @@ -235,4 +235,16 @@ void Application::tooltip_hide_timer_did_fire() m_tooltip_window->hide(); } +void Application::window_did_become_active(Badge, Window& window) +{ + m_active_window = window.make_weak_ptr(); +} + +void Application::window_did_become_inactive(Badge, Window& window) +{ + if (m_active_window.ptr() != &window) + return; + m_active_window = nullptr; +} + } diff --git a/Libraries/LibGUI/Application.h b/Libraries/LibGUI/Application.h index 834120bf16b..bcb08e52480 100644 --- a/Libraries/LibGUI/Application.h +++ b/Libraries/LibGUI/Application.h @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -76,6 +77,12 @@ public: Core::EventLoop& event_loop() { return *m_event_loop; } + Window* active_window() { return m_active_window; } + const Window* active_window() const { return m_active_window; } + + void window_did_become_active(Badge, Window&); + void window_did_become_inactive(Badge, Window&); + private: Application(int argc, char** argv); @@ -92,6 +99,7 @@ private: RefPtr m_tooltip_hide_timer; RefPtr m_tooltip_window; RefPtr m_tooltip_source_widget; + WeakPtr m_active_window; bool m_quit_when_last_window_deleted { true }; bool m_focus_debugging_enabled { false }; String m_invoked_as; diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 8555bc76d09..9def24792a7 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -405,7 +405,10 @@ void Window::handle_input_entered_or_left_event(Core::Event& event) void Window::handle_became_active_or_inactive_event(Core::Event& event) { - m_is_active = event.type() == Event::WindowBecameActive; + if (event.type() == Event::WindowBecameActive) + Application::the()->window_did_become_active({}, *this); + else + Application::the()->window_did_become_inactive({}, *this); if (m_main_widget) m_main_widget->dispatch_event(event, this); if (m_focused_widget) @@ -941,4 +944,10 @@ void Window::did_disable_focused_widget(Badge) focus_a_widget_if_possible(FocusSource::Mouse); } +bool Window::is_active() const +{ + ASSERT(Application::the()); + return this == Application::the()->active_window(); +} + } diff --git a/Libraries/LibGUI/Window.h b/Libraries/LibGUI/Window.h index f132aa5eaf8..326fed14cf2 100644 --- a/Libraries/LibGUI/Window.h +++ b/Libraries/LibGUI/Window.h @@ -113,7 +113,7 @@ public: virtual void event(Core::Event&) override; bool is_visible() const; - bool is_active() const { return m_is_active; } + bool is_active() const; bool is_active_input() const { return m_is_active_input; } bool is_accessory() const { return m_accessory; } @@ -247,7 +247,6 @@ private: WindowType m_window_type { WindowType::Normal }; Gfx::StandardCursor m_cursor { Gfx::StandardCursor::None }; Gfx::StandardCursor m_effective_cursor { Gfx::StandardCursor::None }; - bool m_is_active { false }; bool m_is_active_input { false }; bool m_has_alpha_channel { false }; bool m_double_buffering_enabled { true };