Serenity Operating System
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
27#include "NotificationWindow.h"
28#include <AK/Vector.h>
29#include <LibGUI/BoxLayout.h>
30#include <LibGUI/Button.h>
31#include <LibGUI/Desktop.h>
32#include <LibGUI/Label.h>
33#include <LibGUI/Widget.h>
34#include <LibGfx/Bitmap.h>
35#include <LibGfx/Font.h>
36#include <LibGfx/ShareableBitmap.h>
37
38namespace NotificationServer {
39
40static Vector<RefPtr<NotificationWindow>> s_windows;
41
42void update_notification_window_locations()
43{
44 Gfx::Rect last_window_rect;
45 for (auto& window : s_windows) {
46 Gfx::Point new_window_location;
47 if (last_window_rect.is_null())
48 new_window_location = GUI::Desktop::the().rect().top_right().translated(-window->rect().width() - 24, 26);
49 else
50 new_window_location = last_window_rect.bottom_left().translated(0, 10);
51 if (window->rect().location() != new_window_location) {
52 window->move_to(new_window_location);
53 window->set_original_rect(window->rect());
54 }
55 last_window_rect = window->rect();
56 }
57}
58
59NotificationWindow::NotificationWindow(const String& text, const String& title, const Gfx::ShareableBitmap& icon)
60{
61 s_windows.append(this);
62
63 set_window_type(GUI::WindowType::Notification);
64 set_resizable(false);
65 set_minimizable(false);
66
67 Gfx::Rect lowest_notification_rect_on_screen;
68 for (auto& window : s_windows) {
69 if (window->m_original_rect.y() > lowest_notification_rect_on_screen.y())
70 lowest_notification_rect_on_screen = window->m_original_rect;
71 }
72
73 Gfx::Rect rect;
74 rect.set_width(220);
75 rect.set_height(40);
76 rect.set_location(GUI::Desktop::the().rect().top_right().translated(-rect.width() - 24, 26));
77
78 if (!lowest_notification_rect_on_screen.is_null())
79 rect.set_location(lowest_notification_rect_on_screen.bottom_left().translated(0, 10));
80
81 set_rect(rect);
82
83 m_original_rect = rect;
84
85 auto& widget = set_main_widget<GUI::Widget>();
86 widget.set_fill_with_background_color(true);
87
88 widget.set_layout<GUI::HorizontalBoxLayout>();
89 widget.layout()->set_margins({ 8, 8, 8, 8 });
90 widget.layout()->set_spacing(6);
91
92 if (icon.is_valid()) {
93 auto& icon_label = widget.add<GUI::Label>();
94 icon_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
95 icon_label.set_preferred_size(32, 32);
96 icon_label.set_icon(icon.bitmap());
97 }
98
99 auto& left_container = widget.add<GUI::Widget>();
100 left_container.set_layout<GUI::VerticalBoxLayout>();
101
102 auto& title_label = left_container.add<GUI::Label>(title);
103 title_label.set_font(Gfx::Font::default_bold_font());
104 title_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
105 auto& text_label = left_container.add<GUI::Label>(text);
106 text_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
107
108 auto& right_container = widget.add<GUI::Widget>();
109 right_container.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
110 right_container.set_preferred_size(36, 0);
111 right_container.set_layout<GUI::HorizontalBoxLayout>();
112
113 on_close_request = [this] {
114 s_windows.remove_first_matching([this](auto& entry) { return entry == this; });
115 update_notification_window_locations();
116 return CloseRequestDecision::Close;
117 };
118}
119
120NotificationWindow::~NotificationWindow()
121{
122}
123
124}