1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-24 03:55:06 +00:00

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.
This commit is contained in:
Andreas Kling 2020-03-26 20:38:28 +01:00
parent 3c29818048
commit faedb763ca
7 changed files with 24 additions and 9 deletions

View file

@ -35,7 +35,7 @@ Notification::~Notification()
void Notification::show() void Notification::show()
{ {
auto connection = NotificationServerConnection::construct(); 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));
} }
} }

View file

@ -16,6 +16,9 @@ public:
const String& title() const { return m_title; } const String& title() const { return m_title; }
void set_title(const String& title) { m_title = 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(); void show();
private: private:
@ -23,6 +26,7 @@ private:
String m_title; String m_title;
String m_text; String m_text;
String m_icon_path;
}; };
} }

View file

@ -55,7 +55,7 @@ OwnPtr<Messages::NotificationServer::GreetResponse> ClientConnection::handle(con
void ClientConnection::handle(const Messages::NotificationServer::ShowNotification& message) 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(); window->show();
} }

View file

@ -3,5 +3,5 @@ endpoint NotificationServer = 95
// Basic protocol // Basic protocol
Greet() => (i32 client_id) Greet() => (i32 client_id)
ShowNotification(String text, String title) =| ShowNotification(String text, String title, String icon_path) =|
} }

View file

@ -31,13 +31,14 @@
#include <LibGUI/Desktop.h> #include <LibGUI/Desktop.h>
#include <LibGUI/Label.h> #include <LibGUI/Label.h>
#include <LibGUI/Widget.h> #include <LibGUI/Widget.h>
#include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h> #include <LibGfx/Font.h>
namespace NotificationServer { namespace NotificationServer {
static HashTable<RefPtr<NotificationWindow>> s_windows; 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); s_windows.set(this);
@ -50,7 +51,7 @@ NotificationWindow::NotificationWindow(const String& text, const String& title)
} }
Gfx::Rect rect; Gfx::Rect rect;
rect.set_width(200); rect.set_width(240);
rect.set_height(40); rect.set_height(40);
rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 8, 26)); 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_fill_with_background_color(true);
widget.set_layout<GUI::HorizontalBoxLayout>(); widget.set_layout<GUI::HorizontalBoxLayout>();
widget.layout()->set_margins({ 4, 4, 4, 4 }); widget.layout()->set_margins({ 8, 8, 8, 8 });
widget.layout()->set_spacing(4); 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>(); auto& left_container = widget.add<GUI::Widget>();
left_container.set_layout<GUI::VerticalBoxLayout>(); 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>(); auto& right_container = widget.add<GUI::Widget>();
right_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill); 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>(); right_container.set_layout<GUI::HorizontalBoxLayout>();
auto& button = right_container.add<GUI::Button>("Okay"); auto& button = right_container.add<GUI::Button>("Okay");

View file

@ -37,7 +37,7 @@ public:
virtual ~NotificationWindow() override; virtual ~NotificationWindow() override;
private: private:
NotificationWindow(const String& text, const String& title); NotificationWindow(const String& text, const String& title, const String& icon_path);
Gfx::Rect m_original_rect; Gfx::Rect m_original_rect;
}; };

View file

@ -36,13 +36,16 @@ int main(int argc, char** argv)
Core::ArgsParser args_parser; Core::ArgsParser args_parser;
const char* title = nullptr; const char* title = nullptr;
const char* message = 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(title, "Title of the notification", "title");
args_parser.add_positional_argument(message, "Message to display in the notification", "message"); 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); args_parser.parse(argc, argv);
auto notification = GUI::Notification::construct(); auto notification = GUI::Notification::construct();
notification->set_text(message); notification->set_text(message);
notification->set_title(title); notification->set_title(title);
notification->set_icon_path(icon_path);
notification->show(); notification->show();
return 0; return 0;