NotificationWindow.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/HashTable.h>
  28. #include <LibGUI/BoxLayout.h>
  29. #include <LibGUI/Button.h>
  30. #include <LibGUI/Desktop.h>
  31. #include <LibGUI/Label.h>
  32. #include <LibGUI/Widget.h>
  33. #include <LibGfx/Font.h>
  34. namespace NotificationServer {
  35. static HashTable<RefPtr<NotificationWindow>> s_windows;
  36. NotificationWindow::NotificationWindow(const String& text, const String& title)
  37. {
  38. s_windows.set(this);
  39. set_window_type(GUI::WindowType::Tooltip);
  40. Gfx::Rect lowest_notification_rect_on_screen;
  41. for (auto& window : s_windows) {
  42. if (window->m_original_rect.y() > lowest_notification_rect_on_screen.y())
  43. lowest_notification_rect_on_screen = window->m_original_rect;
  44. }
  45. Gfx::Rect rect;
  46. rect.set_width(200);
  47. rect.set_height(40);
  48. rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 8, 26));
  49. if (!lowest_notification_rect_on_screen.is_null())
  50. rect.set_location(lowest_notification_rect_on_screen.bottom_left().translated(0, 8));
  51. set_rect(rect);
  52. m_original_rect = rect;
  53. auto& widget = set_main_widget<GUI::Widget>();
  54. widget.set_fill_with_background_color(true);
  55. widget.set_layout<GUI::HorizontalBoxLayout>();
  56. widget.layout()->set_margins({ 4, 4, 4, 4 });
  57. widget.layout()->set_spacing(4);
  58. auto left_container = widget.add<GUI::Widget>();
  59. left_container->set_layout<GUI::VerticalBoxLayout>();
  60. auto title_label = left_container->add<GUI::Label>(title);
  61. title_label->set_font(Gfx::Font::default_bold_font());
  62. title_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  63. auto text_label = left_container->add<GUI::Label>(text);
  64. text_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  65. auto right_container = widget.add<GUI::Widget>();
  66. right_container->set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  67. right_container->set_preferred_size(40, 0);
  68. right_container->set_layout<GUI::HorizontalBoxLayout>();
  69. auto button = right_container->add<GUI::Button>("Okay");
  70. button->on_click = [this] {
  71. s_windows.remove(this);
  72. close();
  73. };
  74. }
  75. NotificationWindow::~NotificationWindow()
  76. {
  77. }
  78. }