LibGUI: Add GUI::Application::active_window()

Instead of each window having a bool flag that says whether that window
is currently active, have a pointer to the active window on the app
object instead.
This commit is contained in:
Andreas Kling 2021-01-05 15:43:59 +01:00
parent 8b90e8d08b
commit 1c8eaf28cd
Notes: sideshowbarker 2024-07-19 00:05:39 +09:00
4 changed files with 31 additions and 3 deletions

View file

@ -235,4 +235,16 @@ void Application::tooltip_hide_timer_did_fire()
m_tooltip_window->hide();
}
void Application::window_did_become_active(Badge<Window>, Window& window)
{
m_active_window = window.make_weak_ptr<Window>();
}
void Application::window_did_become_inactive(Badge<Window>, Window& window)
{
if (m_active_window.ptr() != &window)
return;
m_active_window = nullptr;
}
}

View file

@ -29,6 +29,7 @@
#include <AK/HashMap.h>
#include <AK/OwnPtr.h>
#include <AK/String.h>
#include <AK/WeakPtr.h>
#include <LibCore/Object.h>
#include <LibGUI/Forward.h>
#include <LibGUI/Shortcut.h>
@ -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>, Window&);
void window_did_become_inactive(Badge<Window>, Window&);
private:
Application(int argc, char** argv);
@ -92,6 +99,7 @@ private:
RefPtr<Core::Timer> m_tooltip_hide_timer;
RefPtr<TooltipWindow> m_tooltip_window;
RefPtr<Widget> m_tooltip_source_widget;
WeakPtr<Window> m_active_window;
bool m_quit_when_last_window_deleted { true };
bool m_focus_debugging_enabled { false };
String m_invoked_as;

View file

@ -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<Widget>)
focus_a_widget_if_possible(FocusSource::Mouse);
}
bool Window::is_active() const
{
ASSERT(Application::the());
return this == Application::the()->active_window();
}
}

View file

@ -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 };