Notification.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibGUI/Notification.h>
  8. #include <LibIPC/ConnectionToServer.h>
  9. #include <NotificationServer/NotificationClientEndpoint.h>
  10. #include <NotificationServer/NotificationServerEndpoint.h>
  11. namespace GUI {
  12. class ConnectionToNotificationServer final
  13. : public IPC::ConnectionToServer<NotificationClientEndpoint, NotificationServerEndpoint>
  14. , public NotificationClientEndpoint {
  15. IPC_CLIENT_CONNECTION(ConnectionToNotificationServer, "/tmp/session/%sid/portal/notify"sv)
  16. friend class Notification;
  17. public:
  18. virtual void die() override
  19. {
  20. if (!m_notification->m_destroyed)
  21. m_notification->connection_closed();
  22. }
  23. private:
  24. explicit ConnectionToNotificationServer(NonnullOwnPtr<Core::LocalSocket> socket, Notification* notification)
  25. : IPC::ConnectionToServer<NotificationClientEndpoint, NotificationServerEndpoint>(*this, move(socket))
  26. , m_notification(notification)
  27. {
  28. }
  29. Notification* m_notification;
  30. };
  31. Notification::Notification() = default;
  32. Notification::~Notification() = default;
  33. void Notification::show()
  34. {
  35. VERIFY(!m_shown && !m_destroyed);
  36. auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
  37. m_connection = ConnectionToNotificationServer::try_create(this).release_value_but_fixme_should_propagate_errors();
  38. m_connection->show_notification(m_text, m_title, icon);
  39. m_shown = true;
  40. }
  41. void Notification::close()
  42. {
  43. VERIFY(m_shown);
  44. if (!m_destroyed) {
  45. m_connection->close_notification();
  46. connection_closed();
  47. return;
  48. }
  49. }
  50. bool Notification::update()
  51. {
  52. VERIFY(m_shown);
  53. if (m_destroyed) {
  54. return false;
  55. }
  56. if (m_text_dirty || m_title_dirty) {
  57. m_connection->update_notification_text(m_text, m_title);
  58. m_text_dirty = false;
  59. m_title_dirty = false;
  60. }
  61. if (m_icon_dirty) {
  62. m_connection->update_notification_icon(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
  63. m_icon_dirty = false;
  64. }
  65. return true;
  66. }
  67. void Notification::connection_closed()
  68. {
  69. m_connection.clear();
  70. m_destroyed = true;
  71. }
  72. }