LibGUI: Add support for changing window title

This commit is contained in:
Bananymous 2025-05-05 01:10:05 +03:00
parent ccb81de85d
commit 76d4e6bd18
7 changed files with 50 additions and 1 deletions

View File

@ -287,6 +287,15 @@ namespace LibGUI
return on_socket_error(__FUNCTION__);
}
void Window::set_title(BAN::StringView title)
{
WindowPacket::WindowSetTitle packet;
MUST(packet.title.append(title));
if (auto ret = packet.send_serialized(m_server_fd); ret.is_error())
return on_socket_error(__FUNCTION__);
}
void Window::set_position(int32_t x, int32_t y)
{
WindowPacket::WindowSetPosition packet;

View File

@ -165,6 +165,7 @@ namespace LibGUI
WindowSetMouseCapture,
WindowSetSize,
WindowSetFullscreen,
WindowSetTitle,
DestroyWindowEvent,
CloseWindowEvent,
@ -230,6 +231,11 @@ namespace LibGUI
bool, fullscreen
);
DEFINE_PACKET(
WindowSetTitle,
BAN::String, title
);
}
namespace EventPacket

View File

@ -68,6 +68,7 @@ namespace LibGUI
void set_mouse_capture(bool captured);
void set_fullscreen(bool fullscreen);
void set_title(BAN::StringView title);
void set_position(int32_t x, int32_t y);

View File

@ -52,6 +52,8 @@ public:
LibGUI::Window::Attributes get_attributes() const { return m_attributes; };
void set_attributes(LibGUI::Window::Attributes attributes) { m_attributes = attributes; };
BAN::ErrorOr<void> set_title(BAN::StringView title) { m_title.clear(); TRY(m_title.append(title)); TRY(prepare_title_bar()); return {}; }
const uint32_t* framebuffer() const { return m_fb_addr; }
uint32_t title_bar_pixel(int32_t abs_x, int32_t abs_y, Position cursor) const

View File

@ -307,7 +307,7 @@ void WindowServer::on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::
if (!target_window)
{
dwarnln("client tried to set window size while not owning a window");
dwarnln("client tried to set window fullscreen while not owning a window");
return;
}
@ -316,6 +316,32 @@ void WindowServer::on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::
invalidate(m_framebuffer.area());
}
void WindowServer::on_window_set_title(int fd, const LibGUI::WindowPacket::WindowSetTitle& packet)
{
BAN::RefPtr<Window> target_window;
for (auto& window : m_client_windows)
{
if (window->client_fd() != fd)
continue;
target_window = window;
break;
}
if (!target_window)
{
dwarnln("client tried to set window title while not owning a window");
return;
}
if (auto ret = target_window->set_title(packet.title); ret.is_error())
{
dwarnln("failed to set window title: {}", ret.error());
return;
}
invalidate(target_window->title_bar_area());
}
void WindowServer::on_key_event(LibInput::KeyEvent event)
{
// Mod key is not passed to clients

View File

@ -38,6 +38,7 @@ public:
void on_window_set_mouse_capture(int fd, const LibGUI::WindowPacket::WindowSetMouseCapture&);
void on_window_set_size(int fd, const LibGUI::WindowPacket::WindowSetSize&);
void on_window_set_fullscreen(int fd, const LibGUI::WindowPacket::WindowSetFullscreen&);
void on_window_set_title(int fd, const LibGUI::WindowPacket::WindowSetTitle&);
void on_key_event(LibInput::KeyEvent event);
void on_mouse_button(LibInput::MouseButtonEvent event);

View File

@ -379,6 +379,10 @@ int main()
if (auto ret = LibGUI::WindowPacket::WindowSetFullscreen::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_fullscreen(fd, ret.release_value());
break;
case LibGUI::PacketType::WindowSetTitle:
if (auto ret = LibGUI::WindowPacket::WindowSetTitle::deserialize(client_data.packet_buffer.span()); !ret.is_error())
window_server.on_window_set_title(fd, ret.release_value());
break;
default:
dprintln("unhandled packet type: {}", *reinterpret_cast<uint32_t*>(client_data.packet_buffer.data()));
}