diff --git a/Userland/Libraries/LibGUI/Notification.cpp b/Userland/Libraries/LibGUI/Notification.cpp index f38141044c..c7be8802d0 100644 --- a/Userland/Libraries/LibGUI/Notification.cpp +++ b/Userland/Libraries/LibGUI/Notification.cpp @@ -34,23 +34,28 @@ namespace GUI { class NotificationServerConnection : public IPC::ServerConnection , public NotificationClientEndpoint { C_OBJECT(NotificationServerConnection) + + friend class Notification; + public: virtual void handshake() override { send_sync(); } - virtual void die() override { m_connected = false; } - - bool is_connected() const { return m_connected; } + virtual void die() override + { + m_notification->connection_closed(); + } private: - NotificationServerConnection() + explicit NotificationServerConnection(Notification* notification) : IPC::ServerConnection(*this, "/tmp/portal/notify") + , m_notification(notification) { } virtual void handle(const Messages::NotificationClient::Dummy&) override { } - bool m_connected { true }; + Notification* m_notification; }; Notification::Notification() @@ -63,25 +68,27 @@ Notification::~Notification() void Notification::show() { - VERIFY(!m_connection); + VERIFY(!m_shown && !m_destroyed); auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap(); - m_connection = NotificationServerConnection::construct(); + m_connection = NotificationServerConnection::construct(this); m_connection->send_sync(m_text, m_title, icon); + m_shown = true; } void Notification::close() { - VERIFY(m_connection); - if (!m_connection->is_connected()) { + VERIFY(m_shown); + if (!m_destroyed) { + m_connection->send_sync(); + connection_closed(); return; } - m_connection->send_sync(); } bool Notification::update() { - VERIFY(m_connection); - if (!m_connection->is_connected()) { + VERIFY(m_shown); + if (m_destroyed) { return false; } @@ -99,4 +106,10 @@ bool Notification::update() return true; } +void Notification::connection_closed() +{ + m_connection.clear(); + m_destroyed = true; +} + } diff --git a/Userland/Libraries/LibGUI/Notification.h b/Userland/Libraries/LibGUI/Notification.h index e949d7fc24..86c3aeaf3e 100644 --- a/Userland/Libraries/LibGUI/Notification.h +++ b/Userland/Libraries/LibGUI/Notification.h @@ -36,6 +36,8 @@ class NotificationServerConnection; class Notification : public Core::Object { C_OBJECT(Notification); + friend class NotificationServerConnection; + public: virtual ~Notification() override; @@ -64,9 +66,13 @@ public: bool update(); void close(); + bool is_showing() const { return m_shown && !m_destroyed; } + private: Notification(); + void connection_closed(); + String m_title; bool m_title_dirty; String m_text; @@ -74,6 +80,8 @@ private: RefPtr m_icon; bool m_icon_dirty; + bool m_destroyed { false }; + bool m_shown { false }; RefPtr m_connection; };