From faedb763cae94c440e05991e1523da04282dd502 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 26 Mar 2020 20:38:28 +0100 Subject: [PATCH] NotificationServer: Allow showing an icon in notifications We currently use icon paths for this because I didn't want to deal with implementing icon bitmap sharing right now. In the future it would be better to post a bitmap somehow instead of a path. --- Libraries/LibGUI/Notification.cpp | 2 +- Libraries/LibGUI/Notification.h | 4 ++++ .../NotificationServer/ClientConnection.cpp | 2 +- .../NotificationServer/NotificationServer.ipc | 2 +- .../NotificationServer/NotificationWindow.cpp | 18 +++++++++++++----- .../NotificationServer/NotificationWindow.h | 2 +- Userland/notify.cpp | 3 +++ 7 files changed, 24 insertions(+), 9 deletions(-) diff --git a/Libraries/LibGUI/Notification.cpp b/Libraries/LibGUI/Notification.cpp index f07f056a72..af8d794d83 100644 --- a/Libraries/LibGUI/Notification.cpp +++ b/Libraries/LibGUI/Notification.cpp @@ -35,7 +35,7 @@ Notification::~Notification() void Notification::show() { auto connection = NotificationServerConnection::construct(); - connection->post_message(Messages::NotificationServer::ShowNotification(m_text, m_title)); + connection->post_message(Messages::NotificationServer::ShowNotification(m_text, m_title, m_icon_path)); } } diff --git a/Libraries/LibGUI/Notification.h b/Libraries/LibGUI/Notification.h index 218dfa6c7e..da13ca8be2 100644 --- a/Libraries/LibGUI/Notification.h +++ b/Libraries/LibGUI/Notification.h @@ -16,6 +16,9 @@ public: const String& title() const { return m_title; } void set_title(const String& title) { m_title = title; } + const String& icon_path() const { return m_icon_path; } + void set_icon_path(const String& icon_path) { m_icon_path = icon_path; } + void show(); private: @@ -23,6 +26,7 @@ private: String m_title; String m_text; + String m_icon_path; }; } diff --git a/Servers/NotificationServer/ClientConnection.cpp b/Servers/NotificationServer/ClientConnection.cpp index 7ee8243caf..c61d1cc028 100644 --- a/Servers/NotificationServer/ClientConnection.cpp +++ b/Servers/NotificationServer/ClientConnection.cpp @@ -55,7 +55,7 @@ OwnPtr ClientConnection::handle(con void ClientConnection::handle(const Messages::NotificationServer::ShowNotification& message) { - auto window = NotificationWindow::construct(message.text(), message.title()); + auto window = NotificationWindow::construct(message.text(), message.title(), message.icon_path()); window->show(); } diff --git a/Servers/NotificationServer/NotificationServer.ipc b/Servers/NotificationServer/NotificationServer.ipc index e8040e1f52..75bf5e0f89 100644 --- a/Servers/NotificationServer/NotificationServer.ipc +++ b/Servers/NotificationServer/NotificationServer.ipc @@ -3,5 +3,5 @@ endpoint NotificationServer = 95 // Basic protocol Greet() => (i32 client_id) - ShowNotification(String text, String title) =| + ShowNotification(String text, String title, String icon_path) =| } diff --git a/Servers/NotificationServer/NotificationWindow.cpp b/Servers/NotificationServer/NotificationWindow.cpp index 9016ad69d8..0c249f6846 100644 --- a/Servers/NotificationServer/NotificationWindow.cpp +++ b/Servers/NotificationServer/NotificationWindow.cpp @@ -31,13 +31,14 @@ #include #include #include +#include #include namespace NotificationServer { static HashTable> s_windows; -NotificationWindow::NotificationWindow(const String& text, const String& title) +NotificationWindow::NotificationWindow(const String& text, const String& title, const String& icon_path) { s_windows.set(this); @@ -50,7 +51,7 @@ NotificationWindow::NotificationWindow(const String& text, const String& title) } Gfx::Rect rect; - rect.set_width(200); + rect.set_width(240); rect.set_height(40); rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 8, 26)); @@ -65,8 +66,15 @@ NotificationWindow::NotificationWindow(const String& text, const String& title) widget.set_fill_with_background_color(true); widget.set_layout(); - widget.layout()->set_margins({ 4, 4, 4, 4 }); - widget.layout()->set_spacing(4); + widget.layout()->set_margins({ 8, 8, 8, 8 }); + widget.layout()->set_spacing(6); + + if (auto icon = Gfx::Bitmap::load_from_file(icon_path)) { + auto& icon_label = widget.add(); + icon_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed); + icon_label.set_preferred_size(32, 32); + icon_label.set_icon(icon); + } auto& left_container = widget.add(); left_container.set_layout(); @@ -79,7 +87,7 @@ NotificationWindow::NotificationWindow(const String& text, const String& title) auto& right_container = widget.add(); right_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); - right_container.set_preferred_size(40, 0); + right_container.set_preferred_size(36, 0); right_container.set_layout(); auto& button = right_container.add("Okay"); diff --git a/Servers/NotificationServer/NotificationWindow.h b/Servers/NotificationServer/NotificationWindow.h index dd13a65f86..589c70848d 100644 --- a/Servers/NotificationServer/NotificationWindow.h +++ b/Servers/NotificationServer/NotificationWindow.h @@ -37,7 +37,7 @@ public: virtual ~NotificationWindow() override; private: - NotificationWindow(const String& text, const String& title); + NotificationWindow(const String& text, const String& title, const String& icon_path); Gfx::Rect m_original_rect; }; diff --git a/Userland/notify.cpp b/Userland/notify.cpp index a1e0176dba..4dded943f5 100644 --- a/Userland/notify.cpp +++ b/Userland/notify.cpp @@ -36,13 +36,16 @@ int main(int argc, char** argv) Core::ArgsParser args_parser; const char* title = nullptr; const char* message = nullptr; + const char* icon_path = nullptr; args_parser.add_positional_argument(title, "Title of the notification", "title"); args_parser.add_positional_argument(message, "Message to display in the notification", "message"); + args_parser.add_positional_argument(icon_path, "Path of icon to display in the notification", "icon-path", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); auto notification = GUI::Notification::construct(); notification->set_text(message); notification->set_title(title); + notification->set_icon_path(icon_path); notification->show(); return 0;