WindowServer/LibGUI: Add window focus events

This commit is contained in:
Bananymous 2025-08-21 03:01:25 +03:00
parent 2f3fd6867d
commit f11bb082e4
4 changed files with 41 additions and 1 deletions

View File

@ -335,6 +335,10 @@ namespace LibGUI
if (m_window_shown_event_callback)
m_window_shown_event_callback(TRY_OR_BREAK(EventPacket::WindowShownEvent::deserialize(packet_data.span())).event);
break;
case PacketType::WindowFocusEvent:
if (m_window_focus_event_callback)
m_window_focus_event_callback(TRY_OR_BREAK(EventPacket::WindowFocusEvent::deserialize(packet_data.span())).event);
break;
case PacketType::KeyEvent:
if (m_key_event_callback)
m_key_event_callback(TRY_OR_BREAK(EventPacket::KeyEvent::deserialize(packet_data.span())).event);

View File

@ -217,6 +217,7 @@ namespace LibGUI
CloseWindowEvent,
ResizeWindowEvent,
WindowShownEvent,
WindowFocusEvent,
KeyEvent,
MouseButtonEvent,
MouseMoveEvent,
@ -333,6 +334,14 @@ namespace LibGUI
event_t, event
);
DEFINE_PACKET_EXTRA(
WindowFocusEvent,
struct event_t {
bool focused;
},
event_t, event
);
DEFINE_PACKET_EXTRA(
KeyEvent,
using event_t = LibInput::KeyEvent,

View File

@ -76,6 +76,7 @@ namespace LibGUI
void set_mouse_move_event_callback(BAN::Function<void(EventPacket::MouseMoveEvent::event_t)> callback) { m_mouse_move_event_callback = callback; }
void set_mouse_scroll_event_callback(BAN::Function<void(EventPacket::MouseScrollEvent::event_t)> callback) { m_mouse_scroll_event_callback = callback; }
void set_window_shown_event_callback(BAN::Function<void(EventPacket::WindowShownEvent::event_t)> callback) { m_window_shown_event_callback = callback; }
void set_window_focus_event_callback(BAN::Function<void(EventPacket::WindowFocusEvent::event_t)> callback) { m_window_focus_event_callback = callback; }
int server_fd() const { return m_server_fd; }
@ -108,6 +109,7 @@ namespace LibGUI
BAN::Function<void()> m_close_window_event_callback;
BAN::Function<void()> m_resize_window_event_callback;
BAN::Function<void(EventPacket::WindowShownEvent::event_t)> m_window_shown_event_callback;
BAN::Function<void(EventPacket::WindowFocusEvent::event_t)> m_window_focus_event_callback;
BAN::Function<void(EventPacket::KeyEvent::event_t)> m_key_event_callback;
BAN::Function<void(EventPacket::MouseButtonEvent::event_t)> m_mouse_button_event_callback;
BAN::Function<void(EventPacket::MouseMoveEvent::event_t)> m_mouse_move_event_callback;

View File

@ -90,7 +90,7 @@ void WindowServer::on_window_create(int fd, const LibGUI::WindowPacket::WindowCr
window_popper.disable();
if (packet.attributes.focusable)
if (packet.attributes.shown && packet.attributes.focusable)
set_focused_window(window);
else if (m_client_windows.size() > 1)
BAN::swap(m_client_windows[m_client_windows.size() - 1], m_client_windows[m_client_windows.size() - 2]);
@ -197,6 +197,9 @@ void WindowServer::on_window_set_attributes(int fd, const LibGUI::WindowPacket::
};
if (auto ret = event_packet.send_serialized(target_window->client_fd()); ret.is_error())
dwarnln("could not send window shown event: {}", ret.error());
if (packet.attributes.focusable && packet.attributes.shown)
set_focused_window(target_window);
}
void WindowServer::on_window_set_mouse_relative(int fd, const LibGUI::WindowPacket::WindowSetMouseRelative& packet)
@ -781,6 +784,17 @@ void WindowServer::set_focused_window(BAN::RefPtr<Window> window)
invalidate(cursor_area());
}
if (m_focused_window)
{
LibGUI::EventPacket::WindowFocusEvent packet;
packet.event.focused = false;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send window focus event: {}", ret.error());
return;
}
}
for (size_t i = m_client_windows.size(); i > 0; i--)
{
if (m_client_windows[i - 1] == window)
@ -792,6 +806,17 @@ void WindowServer::set_focused_window(BAN::RefPtr<Window> window)
break;
}
}
if (m_focused_window)
{
LibGUI::EventPacket::WindowFocusEvent packet;
packet.event.focused = true;
if (auto ret = packet.send_serialized(m_focused_window->client_fd()); ret.is_error())
{
dwarnln("could not send window focus event: {}", ret.error());
return;
}
}
}
static uint32_t alpha_blend(uint32_t color_a, uint32_t color_b)