NotificationWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "NotificationWindow.h"
  27. #include <AK/Vector.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Desktop.h>
  31. #include <LibGUI/ImageWidget.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/Widget.h>
  34. #include <LibGfx/Bitmap.h>
  35. #include <LibGfx/Font.h>
  36. #include <LibGfx/FontDatabase.h>
  37. #include <LibGfx/ShareableBitmap.h>
  38. namespace NotificationServer {
  39. static Vector<RefPtr<NotificationWindow>> s_windows;
  40. void update_notification_window_locations()
  41. {
  42. Gfx::IntRect last_window_rect;
  43. for (auto& window : s_windows) {
  44. Gfx::IntPoint new_window_location;
  45. if (last_window_rect.is_null())
  46. new_window_location = GUI::Desktop::the().rect().top_right().translated(-window->rect().width() - 24, 26);
  47. else
  48. new_window_location = last_window_rect.bottom_left().translated(0, 10);
  49. if (window->rect().location() != new_window_location) {
  50. window->move_to(new_window_location);
  51. window->set_original_rect(window->rect());
  52. }
  53. last_window_rect = window->rect();
  54. }
  55. }
  56. NotificationWindow::NotificationWindow(const String& text, const String& title, const Gfx::ShareableBitmap& icon)
  57. {
  58. s_windows.append(this);
  59. set_window_type(GUI::WindowType::Notification);
  60. set_resizable(false);
  61. set_minimizable(false);
  62. Gfx::IntRect lowest_notification_rect_on_screen;
  63. for (auto& window : s_windows) {
  64. if (window->m_original_rect.y() > lowest_notification_rect_on_screen.y())
  65. lowest_notification_rect_on_screen = window->m_original_rect;
  66. }
  67. Gfx::IntRect rect;
  68. rect.set_width(220);
  69. rect.set_height(40);
  70. rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 24, 26));
  71. if (!lowest_notification_rect_on_screen.is_null())
  72. rect.set_location(lowest_notification_rect_on_screen.bottom_left().translated(0, 10));
  73. set_rect(rect);
  74. m_original_rect = rect;
  75. auto& widget = set_main_widget<GUI::Widget>();
  76. widget.set_fill_with_background_color(true);
  77. widget.set_layout<GUI::HorizontalBoxLayout>();
  78. widget.layout()->set_margins({ 8, 8, 8, 8 });
  79. widget.layout()->set_spacing(6);
  80. if (icon.is_valid()) {
  81. auto& image = widget.add<GUI::ImageWidget>();
  82. image.set_bitmap(icon.bitmap());
  83. }
  84. auto& left_container = widget.add<GUI::Widget>();
  85. left_container.set_layout<GUI::VerticalBoxLayout>();
  86. auto& title_label = left_container.add<GUI::Label>(title);
  87. title_label.set_font(Gfx::FontDatabase::default_bold_font());
  88. title_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  89. auto& text_label = left_container.add<GUI::Label>(text);
  90. text_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  91. auto& right_container = widget.add<GUI::Widget>();
  92. right_container.set_fixed_width(36);
  93. right_container.set_layout<GUI::HorizontalBoxLayout>();
  94. on_close_request = [this] {
  95. s_windows.remove_first_matching([this](auto& entry) { return entry == this; });
  96. update_notification_window_locations();
  97. return CloseRequestDecision::Close;
  98. };
  99. }
  100. NotificationWindow::~NotificationWindow()
  101. {
  102. }
  103. }