Notification.cpp 2.0 KB

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