1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 16:47:44 +00:00

LibGUI+WindowServer: Replace WindowInput{Enter,Leave} Events

with WindowInput{Preempted,Restored} Events and allow Widgets to save
the state of their focus preemption. As of now, only Popups will
preempt input and trigger these events.
This commit is contained in:
thankyouverycool 2022-11-17 08:39:20 -05:00 committed by Andreas Kling
parent 901878bad9
commit 5d567565a4
11 changed files with 50 additions and 45 deletions

View file

@ -120,16 +120,16 @@ void ConnectionToWindowServer::window_deactivated(i32 window_id)
Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowBecameInactive));
}
void ConnectionToWindowServer::window_input_entered(i32 window_id)
void ConnectionToWindowServer::window_input_preempted(i32 window_id)
{
if (auto* window = Window::from_window_id(window_id))
Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputPreempted));
}
void ConnectionToWindowServer::window_input_left(i32 window_id)
void ConnectionToWindowServer::window_input_restored(i32 window_id)
{
if (auto* window = Window::from_window_id(window_id))
Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputLeft));
Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputRestored));
}
void ConnectionToWindowServer::window_close_request(i32 window_id)

View file

@ -37,8 +37,8 @@ private:
virtual void key_up(i32, u32, u32, u32, u32) override;
virtual void window_activated(i32) override;
virtual void window_deactivated(i32) override;
virtual void window_input_entered(i32) override;
virtual void window_input_left(i32) override;
virtual void window_input_preempted(i32) override;
virtual void window_input_restored(i32) override;
virtual void window_close_request(i32) override;
virtual void window_resized(i32, Gfx::IntRect const&) override;
virtual void window_moved(i32, Gfx::IntRect const&) override;

View file

@ -42,8 +42,8 @@ public:
WindowLeft,
WindowBecameInactive,
WindowBecameActive,
WindowInputEntered,
WindowInputLeft,
WindowInputPreempted,
WindowInputRestored,
FocusIn,
FocusOut,
WindowCloseRequest,

View file

@ -214,6 +214,9 @@ public:
bool is_focused() const;
void set_focus(bool, FocusSource = FocusSource::Programmatic);
bool focus_preempted() const { return m_focus_preempted; }
void set_focus_preempted(bool b) { m_focus_preempted = b; }
Function<void(bool const, const FocusSource)> on_focus_change;
// Returns true if this widget or one of its descendants is focused.
@ -440,6 +443,7 @@ private:
bool m_visible { true };
bool m_greedy_for_hits { false };
bool m_auto_focusable { true };
bool m_focus_preempted { false };
bool m_enabled { true };
bool m_updates_enabled { true };
bool m_accepts_command_palette { true };

View file

@ -522,15 +522,14 @@ void Window::handle_resize_event(ResizeEvent& event)
m_main_widget->set_relative_rect({ {}, new_size });
}
void Window::handle_input_entered_or_left_event(Core::Event& event)
void Window::handle_input_preemption_event(Core::Event& event)
{
m_is_active_input = event.type() == Event::WindowInputEntered;
if (on_active_input_change)
on_active_input_change(m_is_active_input);
if (m_main_widget)
m_main_widget->dispatch_event(event, this);
if (m_focused_widget)
m_focused_widget->update();
if (on_input_preemption_change)
on_input_preemption_change(event.type() == Event::WindowInputPreempted);
if (!m_focused_widget)
return;
m_focused_widget->set_focus_preempted(event.type() == Event::WindowInputPreempted);
m_focused_widget->update();
}
void Window::handle_became_active_or_inactive_event(Core::Event& event)
@ -543,8 +542,11 @@ void Window::handle_became_active_or_inactive_event(Core::Event& event)
on_active_window_change(event.type() == Event::WindowBecameActive);
if (m_main_widget)
m_main_widget->dispatch_event(event, this);
if (m_focused_widget)
if (m_focused_widget) {
if (event.type() == Event::WindowBecameActive)
m_focused_widget->set_focus_preempted(false);
m_focused_widget->update();
}
}
void Window::handle_close_request()
@ -678,8 +680,8 @@ void Window::event(Core::Event& event)
if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
return handle_became_active_or_inactive_event(event);
if (event.type() == Event::WindowInputEntered || event.type() == Event::WindowInputLeft)
return handle_input_entered_or_left_event(event);
if (event.type() == Event::WindowInputPreempted || event.type() == Event::WindowInputRestored)
return handle_input_preemption_event(event);
if (event.type() == Event::WindowCloseRequest)
return handle_close_request();

View file

@ -97,6 +97,7 @@ public:
Function<void()> on_close;
Function<CloseRequestDecision()> on_close_request;
Function<void(bool is_active_input)> on_active_input_change;
Function<void(bool is_preempted)> on_input_preemption_change;
Function<void(bool is_active_window)> on_active_window_change;
Function<void(InputPreemptor)> on_input_preemption;
@ -259,7 +260,7 @@ private:
void handle_multi_paint_event(MultiPaintEvent&);
void handle_key_event(KeyEvent&);
void handle_resize_event(ResizeEvent&);
void handle_input_entered_or_left_event(Core::Event&);
void handle_input_preemption_event(Core::Event&);
void handle_became_active_or_inactive_event(Core::Event&);
void handle_close_request();
void handle_theme_change_event(ThemeChangeEvent&);