NotificationWindow.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "NotificationWindow.h"
  7. #include <AK/HashMap.h>
  8. #include <AK/Optional.h>
  9. #include <AK/Vector.h>
  10. #include <LibGUI/BoxLayout.h>
  11. #include <LibGUI/Desktop.h>
  12. #include <LibGUI/Label.h>
  13. #include <LibGUI/Widget.h>
  14. #include <LibGfx/Bitmap.h>
  15. #include <LibGfx/Font/FontDatabase.h>
  16. #include <LibGfx/ShareableBitmap.h>
  17. namespace NotificationServer {
  18. static HashMap<u32, RefPtr<NotificationWindow>> s_windows;
  19. static void update_notification_window_locations(Gfx::IntRect const& screen_rect)
  20. {
  21. Optional<Gfx::IntRect> last_window_rect;
  22. for (auto& window_entry : s_windows) {
  23. auto& window = window_entry.value;
  24. Gfx::IntPoint new_window_location;
  25. if (last_window_rect.has_value())
  26. new_window_location = last_window_rect.value().bottom_left().translated(0, 10);
  27. else
  28. new_window_location = screen_rect.top_right().translated(-window->rect().width() - 24, 7);
  29. if (window->rect().location() != new_window_location) {
  30. window->move_to(new_window_location);
  31. window->set_original_rect(window->rect());
  32. }
  33. last_window_rect = window->rect();
  34. }
  35. }
  36. NotificationWindow::NotificationWindow(i32 client_id, DeprecatedString const& text, DeprecatedString const& title, Gfx::ShareableBitmap const& icon)
  37. {
  38. m_id = client_id;
  39. s_windows.set(m_id, this);
  40. set_window_type(GUI::WindowType::Notification);
  41. set_resizable(false);
  42. set_minimizable(false);
  43. Optional<Gfx::IntRect> lowest_notification_rect_on_screen;
  44. for (auto& window_entry : s_windows) {
  45. auto& window = window_entry.value;
  46. if (!lowest_notification_rect_on_screen.has_value()
  47. || (window->m_original_rect.y() > lowest_notification_rect_on_screen.value().y()))
  48. lowest_notification_rect_on_screen = window->m_original_rect;
  49. }
  50. Gfx::IntRect rect;
  51. rect.set_width(220);
  52. rect.set_height(40);
  53. rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 24, 7));
  54. if (lowest_notification_rect_on_screen.has_value())
  55. rect.set_location(lowest_notification_rect_on_screen.value().bottom_left().translated(0, 10));
  56. set_rect(rect);
  57. m_original_rect = rect;
  58. auto widget = set_main_widget<GUI::Widget>().release_value_but_fixme_should_propagate_errors();
  59. widget->set_fill_with_background_color(true);
  60. widget->set_layout<GUI::HorizontalBoxLayout>(8, 6);
  61. m_image = &widget->add<GUI::ImageWidget>();
  62. m_image->set_visible(icon.is_valid());
  63. if (icon.is_valid()) {
  64. m_image->set_bitmap(icon.bitmap());
  65. }
  66. auto& left_container = widget->add<GUI::Widget>();
  67. left_container.set_layout<GUI::VerticalBoxLayout>();
  68. m_title_label = &left_container.add<GUI::Label>(title);
  69. m_title_label->set_font(Gfx::FontDatabase::default_font().bold_variant());
  70. m_title_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  71. m_text_label = &left_container.add<GUI::Label>(text);
  72. m_text_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  73. // FIXME: There used to be code for setting the tooltip here, but since we
  74. // expand the notification now we no longer set the tooltip. Should there be
  75. // a limit to the lines shown in an expanded notification, at which point a
  76. // tooltip should be set?
  77. on_close = [this] {
  78. s_windows.remove(m_id);
  79. update_notification_window_locations(GUI::Desktop::the().rect());
  80. };
  81. }
  82. RefPtr<NotificationWindow> NotificationWindow::get_window_by_id(i32 id)
  83. {
  84. auto window = s_windows.get(id);
  85. return window.value_or(nullptr);
  86. }
  87. void NotificationWindow::resize_to_fit_text()
  88. {
  89. auto line_height = m_text_label->font().glyph_height();
  90. auto total_height = m_text_label->text_calculated_preferred_height();
  91. m_text_label->set_fixed_height(total_height);
  92. set_height(40 - line_height + total_height);
  93. }
  94. void NotificationWindow::enter_event(Core::Event&)
  95. {
  96. m_hovering = true;
  97. resize_to_fit_text();
  98. move_to_front();
  99. update_notification_window_locations(GUI::Desktop::the().rect());
  100. }
  101. void NotificationWindow::leave_event(Core::Event&)
  102. {
  103. m_hovering = false;
  104. m_text_label->set_preferred_height(GUI::SpecialDimension::Grow);
  105. set_height(40);
  106. update_notification_window_locations(GUI::Desktop::the().rect());
  107. }
  108. void NotificationWindow::set_text(DeprecatedString const& value)
  109. {
  110. m_text_label->set_text(value);
  111. if (m_hovering)
  112. resize_to_fit_text();
  113. }
  114. void NotificationWindow::set_title(DeprecatedString const& value)
  115. {
  116. m_title_label->set_text(value);
  117. }
  118. void NotificationWindow::set_image(Gfx::ShareableBitmap const& image)
  119. {
  120. m_image->set_visible(image.is_valid());
  121. if (image.is_valid()) {
  122. m_image->set_bitmap(image.bitmap());
  123. }
  124. }
  125. void NotificationWindow::set_height(int height)
  126. {
  127. auto rect = this->rect();
  128. rect.set_height(height);
  129. set_rect(rect);
  130. }
  131. void NotificationWindow::screen_rects_change_event(GUI::ScreenRectsChangeEvent& event)
  132. {
  133. update_notification_window_locations(event.rects()[event.main_screen_index()]);
  134. }
  135. }