Serenity Operating System
at hosted 411 lines 15 kB view raw
1/* 2 * Copyright (c) 2018-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 <AK/Badge.h> 28#include <LibGfx/CharacterBitmap.h> 29#include <LibGfx/Font.h> 30#include <LibGfx/Painter.h> 31#include <LibGfx/StylePainter.h> 32#include <WindowServer/Button.h> 33#include <WindowServer/Compositor.h> 34#include <WindowServer/Event.h> 35#include <WindowServer/Window.h> 36#include <WindowServer/WindowFrame.h> 37#include <WindowServer/WindowManager.h> 38 39namespace WindowServer { 40 41static const int window_titlebar_height = 19; 42 43static const char* s_close_button_bitmap_data = { 44 "## ##" 45 "### ###" 46 " ###### " 47 " #### " 48 " #### " 49 " ###### " 50 "### ###" 51 "## ##" 52 " " 53}; 54 55static Gfx::CharacterBitmap* s_close_button_bitmap; 56static const int s_close_button_bitmap_width = 8; 57static const int s_close_button_bitmap_height = 9; 58 59static const char* s_minimize_button_bitmap_data = { 60 " " 61 " " 62 " " 63 " ###### " 64 " #### " 65 " ## " 66 " " 67 " " 68 " " 69}; 70 71static Gfx::CharacterBitmap* s_minimize_button_bitmap; 72static const int s_minimize_button_bitmap_width = 8; 73static const int s_minimize_button_bitmap_height = 9; 74 75static const char* s_maximize_button_bitmap_data = { 76 " " 77 " " 78 " " 79 " ## " 80 " #### " 81 " ###### " 82 " " 83 " " 84 " " 85}; 86 87static Gfx::CharacterBitmap* s_maximize_button_bitmap; 88static const int s_maximize_button_bitmap_width = 8; 89static const int s_maximize_button_bitmap_height = 9; 90 91static const char* s_unmaximize_button_bitmap_data = { 92 " " 93 " ## " 94 " #### " 95 " ###### " 96 " " 97 " ###### " 98 " #### " 99 " ## " 100 " " 101}; 102 103static Gfx::CharacterBitmap* s_unmaximize_button_bitmap; 104static const int s_unmaximize_button_bitmap_width = 8; 105static const int s_unmaximize_button_bitmap_height = 9; 106 107WindowFrame::WindowFrame(Window& window) 108 : m_window(window) 109{ 110 if (!s_close_button_bitmap) 111 s_close_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref(); 112 113 if (!s_minimize_button_bitmap) 114 s_minimize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_minimize_button_bitmap_data, s_minimize_button_bitmap_width, s_minimize_button_bitmap_height).leak_ref(); 115 116 if (!s_maximize_button_bitmap) 117 s_maximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_maximize_button_bitmap_data, s_maximize_button_bitmap_width, s_maximize_button_bitmap_height).leak_ref(); 118 119 if (!s_unmaximize_button_bitmap) 120 s_unmaximize_button_bitmap = &Gfx::CharacterBitmap::create_from_ascii(s_unmaximize_button_bitmap_data, s_unmaximize_button_bitmap_width, s_unmaximize_button_bitmap_height).leak_ref(); 121 122 m_buttons.append(make<Button>(*this, *s_close_button_bitmap, [this](auto&) { 123 m_window.request_close(); 124 })); 125 126 if (window.is_resizable()) { 127 auto button = make<Button>(*this, *s_maximize_button_bitmap, [this](auto&) { 128 m_window.set_maximized(!m_window.is_maximized()); 129 }); 130 m_maximize_button = button.ptr(); 131 m_buttons.append(move(button)); 132 } 133 134 if (window.is_minimizable()) { 135 auto button = make<Button>(*this, *s_minimize_button_bitmap, [this](auto&) { 136 m_window.set_minimized(true); 137 }); 138 m_minimize_button = button.ptr(); 139 m_buttons.append(move(button)); 140 } 141} 142 143WindowFrame::~WindowFrame() 144{ 145} 146 147void WindowFrame::did_set_maximized(Badge<Window>, bool maximized) 148{ 149 ASSERT(m_maximize_button); 150 m_maximize_button->set_bitmap(maximized ? *s_unmaximize_button_bitmap : *s_maximize_button_bitmap); 151} 152 153Gfx::Rect WindowFrame::title_bar_rect() const 154{ 155 if (m_window.type() == WindowType::Notification) 156 return { m_window.width() + 3, 3, window_titlebar_height, m_window.height() }; 157 return { 3, 3, m_window.width(), window_titlebar_height }; 158} 159 160Gfx::Rect WindowFrame::title_bar_icon_rect() const 161{ 162 auto titlebar_rect = title_bar_rect(); 163 return { 164 titlebar_rect.x() + 1, 165 titlebar_rect.y() + 2, 166 16, 167 titlebar_rect.height(), 168 }; 169} 170 171Gfx::Rect WindowFrame::title_bar_text_rect() const 172{ 173 auto titlebar_rect = title_bar_rect(); 174 auto titlebar_icon_rect = title_bar_icon_rect(); 175 return { 176 titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2, 177 titlebar_rect.y(), 178 titlebar_rect.width() - 4 - titlebar_icon_rect.width() - 2, 179 titlebar_rect.height() 180 }; 181} 182 183WindowFrame::FrameColors WindowFrame::compute_frame_colors() const 184{ 185 auto& wm = WindowManager::the(); 186 auto palette = wm.palette(); 187 if (&m_window == wm.m_highlight_window) 188 return { palette.highlight_window_title(), palette.highlight_window_border1(), palette.highlight_window_border2() }; 189 if (&m_window == wm.m_move_window) 190 return { palette.moving_window_title(), palette.moving_window_border1(), palette.moving_window_border2() }; 191 if (&m_window == wm.m_active_window) 192 return { palette.active_window_title(), palette.active_window_border1(), palette.active_window_border2() }; 193 return { palette.inactive_window_title(), palette.inactive_window_border1(), palette.inactive_window_border2() }; 194} 195 196void WindowFrame::paint_notification_frame(Gfx::Painter& painter) 197{ 198 auto palette = WindowManager::the().palette(); 199 Gfx::Rect outer_rect = { {}, rect().size() }; 200 201 Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette); 202 203 auto titlebar_rect = title_bar_rect(); 204 painter.fill_rect_with_gradient(Gfx::Orientation::Vertical, titlebar_rect, palette.active_window_border1(), palette.active_window_border2()); 205 206 int stripe_top = m_buttons.last().relative_rect().bottom() + 4; 207 int stripe_bottom = m_window.height() - 3; 208 if (stripe_top && stripe_bottom && stripe_top < stripe_bottom) { 209 for (int i = 2; i <= window_titlebar_height - 2; i += 2) { 210 painter.draw_line({ titlebar_rect.x() + i, stripe_top }, { titlebar_rect.x() + i, stripe_bottom }, palette.active_window_border1()); 211 } 212 } 213} 214 215void WindowFrame::paint_normal_frame(Gfx::Painter& painter) 216{ 217 auto palette = WindowManager::the().palette(); 218 auto& window = m_window; 219 Gfx::Rect outer_rect = { {}, rect().size() }; 220 221 Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette); 222 223 if (!window.show_titlebar()) 224 return; 225 226 auto titlebar_rect = title_bar_rect(); 227 auto titlebar_icon_rect = title_bar_icon_rect(); 228 auto titlebar_inner_rect = title_bar_text_rect(); 229 auto titlebar_title_rect = titlebar_inner_rect; 230 titlebar_title_rect.set_width(Gfx::Font::default_bold_font().width(window.title())); 231 232 auto [title_color, border_color, border_color2] = compute_frame_colors(); 233 234 auto& wm = WindowManager::the(); 235 painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button()); 236 237 auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::Rect() : m_buttons.last().relative_rect(); 238 239 painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2); 240 241 int stripe_left = titlebar_title_rect.right() + 4; 242 int stripe_right = leftmost_button_rect.left() - 3; 243 if (stripe_left && stripe_right && stripe_left < stripe_right) { 244 for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) { 245 painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, border_color); 246 } 247 } 248 249 auto clipped_title_rect = titlebar_title_rect; 250 clipped_title_rect.set_width(stripe_right - clipped_title_rect.x()); 251 if (!clipped_title_rect.is_empty()) { 252 painter.draw_text(clipped_title_rect.translated(1, 2), window.title(), wm.window_title_font(), Gfx::TextAlignment::CenterLeft, border_color.darkened(0.4), Gfx::TextElision::Right); 253 // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline. 254 painter.draw_text(clipped_title_rect.translated(0, 1), window.title(), wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right); 255 } 256 257 painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect()); 258} 259 260void WindowFrame::paint(Gfx::Painter& painter) 261{ 262 Gfx::PainterStateSaver saver(painter); 263 painter.translate(rect().location()); 264 265 if (m_window.type() == WindowType::Notification) 266 paint_notification_frame(painter); 267 else if (m_window.type() == WindowType::Normal) 268 paint_normal_frame(painter); 269 else 270 return; 271 272 for (auto& button : m_buttons) { 273 button.paint(painter); 274 } 275} 276 277static Gfx::Rect frame_rect_for_window(Window& window, const Gfx::Rect& rect) 278{ 279 auto type = window.type(); 280 auto offset = !window.show_titlebar() ? (window_titlebar_height + 1) : 0; 281 282 switch (type) { 283 case WindowType::Normal: 284 return { 285 rect.x() - 3, 286 rect.y() - window_titlebar_height - 4 + offset, 287 rect.width() + 6, 288 rect.height() + 7 + window_titlebar_height - offset 289 }; 290 case WindowType::Notification: 291 return { 292 rect.x() - 3, 293 rect.y() - 3, 294 rect.width() + 6 + window_titlebar_height, 295 rect.height() + 6 296 }; 297 default: 298 return rect; 299 } 300} 301 302static Gfx::Rect frame_rect_for_window(Window& window) 303{ 304 return frame_rect_for_window(window, window.rect()); 305} 306 307Gfx::Rect WindowFrame::rect() const 308{ 309 return frame_rect_for_window(m_window); 310} 311 312void WindowFrame::invalidate_title_bar() 313{ 314 WindowManager::the().invalidate(title_bar_rect().translated(rect().location())); 315} 316 317void WindowFrame::notify_window_rect_changed(const Gfx::Rect& old_rect, const Gfx::Rect& new_rect) 318{ 319 int window_button_width = 15; 320 int window_button_height = 15; 321 int pos; 322 if (m_window.type() == WindowType::Notification) 323 pos = title_bar_rect().top() + 2; 324 else 325 pos = title_bar_text_rect().right() + 1; 326 327 for (auto& button : m_buttons) { 328 if (m_window.type() == WindowType::Notification) { 329 Gfx::Rect rect { 0, pos, window_button_width, window_button_height }; 330 rect.center_horizontally_within(title_bar_rect()); 331 button.set_relative_rect(rect); 332 pos += window_button_width; 333 } else { 334 pos -= window_button_width; 335 Gfx::Rect rect { pos, 0, window_button_width, window_button_height }; 336 rect.center_vertically_within(title_bar_text_rect()); 337 button.set_relative_rect(rect); 338 } 339 } 340 341 auto& wm = WindowManager::the(); 342 wm.invalidate(frame_rect_for_window(m_window, old_rect)); 343 wm.invalidate(frame_rect_for_window(m_window, new_rect)); 344 wm.notify_rect_changed(m_window, old_rect, new_rect); 345} 346 347void WindowFrame::on_mouse_event(const MouseEvent& event) 348{ 349 ASSERT(!m_window.is_fullscreen()); 350 351 if (m_window.is_blocked_by_modal_window()) 352 return; 353 354 auto& wm = WindowManager::the(); 355 if (m_window.type() != WindowType::Normal && m_window.type() != WindowType::Notification) 356 return; 357 358 if (m_window.type() == WindowType::Normal && event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right) && title_bar_icon_rect().contains(event.position())) { 359 wm.move_to_front_and_make_active(m_window); 360 m_window.popup_window_menu(event.position().translated(rect().location())); 361 return; 362 } 363 364 // This is slightly hackish, but expand the title bar rect by one pixel downwards, 365 // so that mouse events between the title bar and window contents don't act like 366 // mouse events on the border. 367 auto adjusted_title_bar_rect = title_bar_rect(); 368 adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 1); 369 370 if (adjusted_title_bar_rect.contains(event.position())) { 371 wm.clear_resize_candidate(); 372 373 if (event.type() == Event::MouseDown) 374 wm.move_to_front_and_make_active(m_window); 375 376 for (auto& button : m_buttons) { 377 if (button.relative_rect().contains(event.position())) 378 return button.on_mouse_event(event.translated(-button.relative_rect().location())); 379 } 380 if (event.type() == Event::MouseDown) { 381 if (m_window.type() == WindowType::Normal && event.button() == MouseButton::Right) { 382 m_window.popup_window_menu(event.position().translated(rect().location())); 383 return; 384 } 385 if (m_window.is_movable() && event.button() == MouseButton::Left) 386 wm.start_window_move(m_window, event.translated(rect().location())); 387 } 388 return; 389 } 390 391 if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) { 392 constexpr ResizeDirection direction_for_hot_area[3][3] = { 393 { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight }, 394 { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right }, 395 { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight }, 396 }; 397 Gfx::Rect outer_rect = { {}, rect().size() }; 398 ASSERT(outer_rect.contains(event.position())); 399 int window_relative_x = event.x() - outer_rect.x(); 400 int window_relative_y = event.y() - outer_rect.y(); 401 int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3)); 402 int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3)); 403 wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]); 404 Compositor::the().invalidate_cursor(); 405 return; 406 } 407 408 if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left) 409 wm.start_window_resize(m_window, event.translated(rect().location())); 410} 411}