Notification.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. virtual void dummy() override { }
  28. Notification* m_notification;
  29. };
  30. Notification::Notification()
  31. {
  32. }
  33. Notification::~Notification()
  34. {
  35. }
  36. void Notification::show()
  37. {
  38. VERIFY(!m_shown && !m_destroyed);
  39. auto icon = m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap();
  40. m_connection = NotificationServerConnection::construct(this);
  41. m_connection->show_notification(m_text, m_title, icon);
  42. m_shown = true;
  43. }
  44. void Notification::close()
  45. {
  46. VERIFY(m_shown);
  47. if (!m_destroyed) {
  48. m_connection->close_notification();
  49. connection_closed();
  50. return;
  51. }
  52. }
  53. bool Notification::update()
  54. {
  55. VERIFY(m_shown);
  56. if (m_destroyed) {
  57. return false;
  58. }
  59. if (m_text_dirty || m_title_dirty) {
  60. m_connection->update_notification_text(m_text, m_title);
  61. m_text_dirty = false;
  62. m_title_dirty = false;
  63. }
  64. if (m_icon_dirty) {
  65. m_connection->update_notification_icon(m_icon ? m_icon->to_shareable_bitmap() : Gfx::ShareableBitmap());
  66. m_icon_dirty = false;
  67. }
  68. return true;
  69. }
  70. void Notification::connection_closed()
  71. {
  72. m_connection.clear();
  73. m_destroyed = true;
  74. }
  75. }