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