Notification.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/portal/notify")
  16. friend class Notification;
  17. public:
  18. virtual void die() override
  19. {
  20. m_notification->connection_closed();
  21. }
  22. private:
  23. explicit ConnectionToNotificationServer(NonnullOwnPtr<Core::Stream::LocalSocket> socket, Notification* notification)
  24. : IPC::ConnectionToServer<NotificationClientEndpoint, NotificationServerEndpoint>(*this, move(socket))
  25. , m_notification(notification)
  26. {
  27. }
  28. Notification* m_notification;
  29. };
  30. Notification::Notification() = default;
  31. Notification::~Notification() = default;
  32. void Notification::show()
  33. {
  34. VERIFY(!m_shown && !m_destroyed);
  35. auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
  36. m_connection = ConnectionToNotificationServer::try_create(this).release_value_but_fixme_should_propagate_errors();
  37. m_connection->show_notification(m_text, m_title, icon);
  38. m_shown = true;
  39. }
  40. void Notification::close()
  41. {
  42. VERIFY(m_shown);
  43. if (!m_destroyed) {
  44. m_connection->close_notification();
  45. connection_closed();
  46. return;
  47. }
  48. }
  49. bool Notification::update()
  50. {
  51. VERIFY(m_shown);
  52. if (m_destroyed) {
  53. return false;
  54. }
  55. if (m_text_dirty || m_title_dirty) {
  56. m_connection->update_notification_text(m_text, m_title);
  57. m_text_dirty = false;
  58. m_title_dirty = false;
  59. }
  60. if (m_icon_dirty) {
  61. m_connection->update_notification_icon(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
  62. m_icon_dirty = false;
  63. }
  64. return true;
  65. }
  66. void Notification::connection_closed()
  67. {
  68. m_connection.clear();
  69. m_destroyed = true;
  70. }
  71. }