Selaa lähdekoodia

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.
Andreas Kling 5 vuotta sitten
vanhempi
commit
faedb763ca

+ 1 - 1
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));
 }
 
 }

+ 4 - 0
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;
 };
 
 }

+ 1 - 1
Servers/NotificationServer/ClientConnection.cpp

@@ -55,7 +55,7 @@ OwnPtr<Messages::NotificationServer::GreetResponse> 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();
 }
 

+ 1 - 1
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) =|
 }

+ 13 - 5
Servers/NotificationServer/NotificationWindow.cpp

@@ -31,13 +31,14 @@
 #include <LibGUI/Desktop.h>
 #include <LibGUI/Label.h>
 #include <LibGUI/Widget.h>
+#include <LibGfx/Bitmap.h>
 #include <LibGfx/Font.h>
 
 namespace NotificationServer {
 
 static HashTable<RefPtr<NotificationWindow>> 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<GUI::HorizontalBoxLayout>();
-    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<GUI::Label>();
+        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<GUI::Widget>();
     left_container.set_layout<GUI::VerticalBoxLayout>();
@@ -79,7 +87,7 @@ NotificationWindow::NotificationWindow(const String& text, const String& title)
 
     auto& right_container = widget.add<GUI::Widget>();
     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<GUI::HorizontalBoxLayout>();
 
     auto& button = right_container.add<GUI::Button>("Okay");

+ 1 - 1
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;
 };

+ 3 - 0
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;