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