diff --git a/Userland/Libraries/LibGUI/Notification.cpp b/Userland/Libraries/LibGUI/Notification.cpp index b4ea45882f..d863309300 100644 --- a/Userland/Libraries/LibGUI/Notification.cpp +++ b/Userland/Libraries/LibGUI/Notification.cpp @@ -85,6 +85,52 @@ void Notification::close() m_showing = false; m_disposed = true; } + +bool Notification::update() +{ + VERIFY(m_showing); + VERIFY(!m_disposed); + if (!m_connection->is_connected()) { + m_showing = false; + m_disposed = true; + return false; + } + + bool is_checked = false; + + if (m_text_dirty || m_title_dirty) { + auto response = m_connection->send_sync(m_text, m_title); + m_text_dirty = false; + m_title_dirty = false; + + is_checked = true; + if (!response->still_showing()) { + m_showing = false; + m_disposed = true; + return false; + } + } + + if (m_icon_dirty) { + auto response = m_connection->send_sync(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap()); + m_icon_dirty = false; + + is_checked = true; + if (!response->still_showing()) { + m_showing = false; + m_disposed = true; + return false; + } + } + + if (!is_checked) { + auto response = m_connection->send_sync(); + m_showing = response->still_showing(); + if (!m_showing) { + m_disposed = true; + } + } + return m_showing; } } diff --git a/Userland/Libraries/LibGUI/Notification.h b/Userland/Libraries/LibGUI/Notification.h index 445b856411..1dda17d757 100644 --- a/Userland/Libraries/LibGUI/Notification.h +++ b/Userland/Libraries/LibGUI/Notification.h @@ -40,23 +40,39 @@ public: virtual ~Notification() override; const String& text() const { return m_text; } - void set_text(const String& text) { m_text = text; } + void set_text(const String& text) + { + m_text_dirty = true; + m_text = text; + } const String& title() const { return m_title; } - void set_title(const String& title) { m_title = title; } + void set_title(const String& title) + { + m_title_dirty = true; + m_title = title; + } const Gfx::Bitmap* icon() const { return m_icon; } - void set_icon(const Gfx::Bitmap* icon) { m_icon = icon; } + void set_icon(const Gfx::Bitmap* icon) + { + m_icon_dirty = true; + m_icon = icon; + } void show(); + bool update(); void close(); private: Notification(); String m_title; + bool m_title_dirty; String m_text; + bool m_text_dirty; RefPtr m_icon; + bool m_icon_dirty; NonnullRefPtr m_connection; bool m_showing { false }; diff --git a/Userland/Services/NotificationServer/ClientConnection.cpp b/Userland/Services/NotificationServer/ClientConnection.cpp index b2f52c31e3..c300fba730 100644 --- a/Userland/Services/NotificationServer/ClientConnection.cpp +++ b/Userland/Services/NotificationServer/ClientConnection.cpp @@ -69,4 +69,29 @@ OwnPtr ClientConnection return make(); } +OwnPtr ClientConnection::handle(const Messages::NotificationServer::UpdateNotificationIcon& message) +{ + auto window = NotificationWindow::get_window_by_id(client_id()); + if (window) { + window->set_image(message.icon()); + } + return make(window); +} + +OwnPtr ClientConnection::handle(const Messages::NotificationServer::UpdateNotificationText& message) +{ + auto window = NotificationWindow::get_window_by_id(client_id()); + if (window) { + window->set_text(message.text()); + window->set_title(message.title()); + } + return make(window); +} + +OwnPtr ClientConnection::handle(const Messages::NotificationServer::IsShowing&) +{ + auto window = NotificationWindow::get_window_by_id(client_id()); + return make(window); +} + } diff --git a/Userland/Services/NotificationServer/ClientConnection.h b/Userland/Services/NotificationServer/ClientConnection.h index c32bd415b6..82c5e1f3b7 100644 --- a/Userland/Services/NotificationServer/ClientConnection.h +++ b/Userland/Services/NotificationServer/ClientConnection.h @@ -46,6 +46,9 @@ private: virtual OwnPtr handle(const Messages::NotificationServer::Greet&) override; virtual OwnPtr handle(const Messages::NotificationServer::ShowNotification&) override; virtual OwnPtr handle(const Messages::NotificationServer::CloseNotification& message) override; + virtual OwnPtr handle(const Messages::NotificationServer::UpdateNotificationIcon& message) override; + virtual OwnPtr handle(const Messages::NotificationServer::UpdateNotificationText& message) override; + virtual OwnPtr handle(const Messages::NotificationServer::IsShowing& message) override; }; } diff --git a/Userland/Services/NotificationServer/NotificationServer.ipc b/Userland/Services/NotificationServer/NotificationServer.ipc index bfc02879fb..830e9d6312 100644 --- a/Userland/Services/NotificationServer/NotificationServer.ipc +++ b/Userland/Services/NotificationServer/NotificationServer.ipc @@ -5,5 +5,11 @@ endpoint NotificationServer = 95 ShowNotification([UTF8] String text, [UTF8] String title, Gfx::ShareableBitmap icon) => () + UpdateNotificationText([UTF8] String text, [UTF8] String title) => (bool still_showing) + + UpdateNotificationIcon(Gfx::ShareableBitmap icon) => (bool still_showing) + + IsShowing() => (bool still_showing) + CloseNotification() => () } diff --git a/Userland/Services/NotificationServer/NotificationWindow.cpp b/Userland/Services/NotificationServer/NotificationWindow.cpp index 348c22e847..f5df283cc4 100644 --- a/Userland/Services/NotificationServer/NotificationWindow.cpp +++ b/Userland/Services/NotificationServer/NotificationWindow.cpp @@ -28,20 +28,17 @@ #include #include #include -#include #include -#include #include #include #include -#include #include #include namespace NotificationServer { static HashMap> s_windows; -static const Gfx::Bitmap* default_image = GUI::Icon::default_icon("ladybug").bitmap_for_size(16); +static const Gfx::Bitmap* default_image = Gfx::Bitmap::load_from_file("/res/icons/32x32/ladybug.png"); void update_notification_window_locations() { @@ -133,4 +130,19 @@ RefPtr NotificationWindow::get_window_by_id(i32 id) return window.value_or(nullptr); } +void NotificationWindow::set_text(const String& value) +{ + m_text_label->set_text(value); +} + +void NotificationWindow::set_title(const String& value) +{ + m_title_label->set_text(value); +} + +void NotificationWindow::set_image(const Gfx::ShareableBitmap& image) +{ + m_image->set_bitmap(image.is_valid() ? image.bitmap() : default_image); +} + } diff --git a/Userland/Services/NotificationServer/NotificationWindow.h b/Userland/Services/NotificationServer/NotificationWindow.h index 206e57d085..a02894e2cb 100644 --- a/Userland/Services/NotificationServer/NotificationWindow.h +++ b/Userland/Services/NotificationServer/NotificationWindow.h @@ -40,6 +40,10 @@ public: virtual ~NotificationWindow() override; void set_original_rect(Gfx::IntRect original_rect) { m_original_rect = original_rect; }; + void set_text(const String&); + void set_title(const String&); + void set_image(const Gfx::ShareableBitmap&); + static RefPtr get_window_by_id(i32 id); private: