Serenity Operating System
at master 140 lines 5.1 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "TaskbarButton.h" 8#include "WindowList.h" 9#include <LibGUI/Action.h> 10#include <LibGUI/ConnectionToWindowManagerServer.h> 11#include <LibGUI/ConnectionToWindowServer.h> 12#include <LibGUI/Painter.h> 13#include <LibGfx/Font/Font.h> 14#include <LibGfx/Palette.h> 15#include <LibGfx/StylePainter.h> 16 17TaskbarButton::TaskbarButton(WindowIdentifier const& identifier) 18 : m_identifier(identifier) 19{ 20 set_checkable(true); 21} 22 23void TaskbarButton::context_menu_event(GUI::ContextMenuEvent&) 24{ 25 GUI::ConnectionToWindowManagerServer::the().async_popup_window_menu( 26 m_identifier.client_id(), 27 m_identifier.window_id(), 28 screen_relative_rect().location()); 29} 30 31void TaskbarButton::update_taskbar_rect() 32{ 33 GUI::ConnectionToWindowManagerServer::the().async_set_window_taskbar_rect( 34 m_identifier.client_id(), 35 m_identifier.window_id(), 36 screen_relative_rect()); 37} 38 39void TaskbarButton::clear_taskbar_rect() 40{ 41 GUI::ConnectionToWindowManagerServer::the().async_set_window_taskbar_rect( 42 m_identifier.client_id(), 43 m_identifier.window_id(), 44 {}); 45} 46 47void TaskbarButton::resize_event(GUI::ResizeEvent& event) 48{ 49 update_taskbar_rect(); 50 return GUI::Button::resize_event(event); 51} 52 53static void paint_custom_progressbar(GUI::Painter& painter, Gfx::IntRect const& rect, Gfx::IntRect const& text_rect, Palette const& palette, int min, int max, int value, StringView text, Gfx::Font const& font, Gfx::TextAlignment text_alignment) 54{ 55 float range_size = max - min; 56 float progress = (value - min) / range_size; 57 float progress_width = progress * rect.width(); 58 59 Gfx::IntRect progress_rect { rect.x(), rect.y(), (int)progress_width, rect.height() }; 60 61 { 62 Gfx::PainterStateSaver saver(painter); 63 painter.add_clip_rect(progress_rect); 64 65 Color start_color = palette.active_window_border1(); 66 Color end_color = palette.active_window_border2(); 67 painter.fill_rect_with_gradient(rect, start_color, end_color); 68 69 if (!text.is_null()) { 70 painter.draw_text(text_rect.translated(1, 1), text, font, text_alignment, palette.base_text(), Gfx::TextElision::Right); 71 painter.draw_text(text_rect, text, font, text_alignment, palette.base_text().inverted(), Gfx::TextElision::Right); 72 } 73 } 74 75 Gfx::IntRect hole_rect { (int)progress_width, 0, (int)(rect.width() - progress_width), rect.height() }; 76 hole_rect.translate_by(rect.location()); 77 hole_rect.set_right_without_resize(rect.right()); 78 Gfx::PainterStateSaver saver(painter); 79 painter.add_clip_rect(hole_rect); 80 if (!text.is_null()) 81 painter.draw_text(text_rect, text, font, text_alignment, palette.base_text(), Gfx::TextElision::Right); 82} 83 84void TaskbarButton::paint_event(GUI::PaintEvent& event) 85{ 86 VERIFY(icon()); 87 auto& icon = *this->icon(); 88 auto& font = is_checked() ? this->font().bold_variant() : this->font(); 89 auto& window = WindowList::the().ensure_window(m_identifier); 90 91 GUI::Painter painter(*this); 92 painter.add_clip_rect(event.rect()); 93 94 Gfx::StylePainter::paint_button(painter, rect(), palette(), button_style(), is_being_pressed(), is_hovered(), is_checked(), is_enabled()); 95 96 if (text().is_empty()) 97 return; 98 99 auto content_rect = rect().shrunken(8, 2); 100 auto icon_location = content_rect.center().translated(-(icon.width() / 2), -(icon.height() / 2)); 101 if (!text().is_empty()) 102 icon_location.set_x(content_rect.x()); 103 104 if (!text().is_empty()) { 105 content_rect.translate_by(icon.width() + 4, 0); 106 content_rect.set_width(content_rect.width() - icon.width() - 4); 107 } 108 109 Gfx::IntRect text_rect { 0, 0, static_cast<int>(ceilf(font.width(text()))), font.pixel_size_rounded_up() }; 110 if (text_rect.width() > content_rect.width()) 111 text_rect.set_width(content_rect.width()); 112 text_rect.align_within(content_rect, text_alignment()); 113 114 if (is_being_pressed() || is_checked()) { 115 text_rect.translate_by(1, 1); 116 icon_location.translate_by(1, 1); 117 } 118 119 if (window.progress().has_value()) { 120 auto adjusted_rect = rect().shrunken(4, 4); 121 if (!is_being_pressed() && !is_checked()) { 122 adjusted_rect.translate_by(-1, -1); 123 adjusted_rect.set_width(adjusted_rect.width() + 1); 124 adjusted_rect.set_height(adjusted_rect.height() + 1); 125 } 126 paint_custom_progressbar(painter, adjusted_rect, text_rect, palette(), 0, 100, window.progress().value(), text(), font, text_alignment()); 127 } 128 129 if (is_enabled()) { 130 if (is_hovered()) 131 painter.blit_brightened(icon_location, icon, icon.rect()); 132 else 133 painter.blit(icon_location, icon, icon.rect()); 134 } else { 135 painter.blit_disabled(icon_location, icon, icon.rect(), palette()); 136 } 137 138 if (!window.progress().has_value()) 139 paint_text(painter, text_rect, font, text_alignment()); 140}