Serenity Operating System
at portability 373 lines 14 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 return { 3, 3, m_window.width(), window_titlebar_height }; 156} 157 158Gfx::Rect WindowFrame::title_bar_icon_rect() const 159{ 160 auto titlebar_rect = title_bar_rect(); 161 return { 162 titlebar_rect.x() + 1, 163 titlebar_rect.y() + 2, 164 16, 165 titlebar_rect.height(), 166 }; 167} 168 169Gfx::Rect WindowFrame::title_bar_text_rect() const 170{ 171 auto titlebar_rect = title_bar_rect(); 172 auto titlebar_icon_rect = title_bar_icon_rect(); 173 return { 174 titlebar_rect.x() + 2 + titlebar_icon_rect.width() + 2, 175 titlebar_rect.y(), 176 titlebar_rect.width() - 4 - titlebar_icon_rect.width() - 2, 177 titlebar_rect.height() 178 }; 179} 180 181void WindowFrame::paint(Gfx::Painter& painter) 182{ 183 Gfx::PainterStateSaver saver(painter); 184 painter.translate(rect().location()); 185 186 if (m_window.type() != WindowType::Normal) 187 return; 188 189 auto palette = WindowManager::the().palette(); 190 auto& window = m_window; 191 192 auto titlebar_rect = title_bar_rect(); 193 auto titlebar_icon_rect = title_bar_icon_rect(); 194 auto titlebar_inner_rect = title_bar_text_rect(); 195 Gfx::Rect outer_rect = { {}, rect().size() }; 196 197 auto titlebar_title_rect = titlebar_inner_rect; 198 titlebar_title_rect.set_width(Gfx::Font::default_bold_font().width(window.title())); 199 200 Color title_color; 201 Color border_color; 202 Color border_color2; 203 204 auto& wm = WindowManager::the(); 205 206 if (&window == wm.m_highlight_window) { 207 border_color = palette.highlight_window_border1(); 208 border_color2 = palette.highlight_window_border2(); 209 title_color = palette.highlight_window_title(); 210 } else if (&window == wm.m_move_window) { 211 border_color = palette.moving_window_border1(); 212 border_color2 = palette.moving_window_border2(); 213 title_color = palette.moving_window_title(); 214 } else if (&window == wm.m_active_window) { 215 border_color = palette.active_window_border1(); 216 border_color2 = palette.active_window_border2(); 217 title_color = palette.active_window_title(); 218 } else { 219 border_color = palette.inactive_window_border1(); 220 border_color2 = palette.inactive_window_border2(); 221 title_color = palette.inactive_window_title(); 222 } 223 224 Gfx::StylePainter::paint_window_frame(painter, outer_rect, palette); 225 226 if (!window.show_titlebar()) 227 return; 228 229 painter.draw_line(titlebar_rect.bottom_left().translated(0, 1), titlebar_rect.bottom_right().translated(0, 1), palette.button()); 230 231 auto leftmost_button_rect = m_buttons.is_empty() ? Gfx::Rect() : m_buttons.last().relative_rect(); 232 233 painter.fill_rect_with_gradient(titlebar_rect, border_color, border_color2); 234 235 int stripe_left = titlebar_title_rect.right() + 4; 236 int stripe_right = leftmost_button_rect.left() - 3; 237 if (stripe_left && stripe_right && stripe_left < stripe_right) { 238 for (int i = 2; i <= titlebar_inner_rect.height() - 2; i += 2) { 239 painter.draw_line({ stripe_left, titlebar_inner_rect.y() + i }, { stripe_right, titlebar_inner_rect.y() + i }, border_color); 240 } 241 } 242 243 auto clipped_title_rect = titlebar_title_rect; 244 clipped_title_rect.set_width(stripe_right - clipped_title_rect.x()); 245 if (!clipped_title_rect.is_empty()) { 246 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); 247 // FIXME: The translated(0, 1) wouldn't be necessary if we could center text based on its baseline. 248 painter.draw_text(clipped_title_rect.translated(0, 1), window.title(), wm.window_title_font(), Gfx::TextAlignment::CenterLeft, title_color, Gfx::TextElision::Right); 249 } 250 251 painter.blit(titlebar_icon_rect.location(), window.icon(), window.icon().rect()); 252 253 for (auto& button : m_buttons) { 254 button.paint(painter); 255 } 256} 257 258static Gfx::Rect frame_rect_for_window(Window& window, const Gfx::Rect& rect) 259{ 260 auto type = window.type(); 261 auto offset = !window.show_titlebar() ? (window_titlebar_height + 1) : 0; 262 263 switch (type) { 264 case WindowType::Normal: 265 return { rect.x() - 3, 266 rect.y() - window_titlebar_height - 4 + offset, 267 rect.width() + 6, 268 rect.height() + 7 + window_titlebar_height - offset }; 269 default: 270 return rect; 271 } 272} 273 274static Gfx::Rect frame_rect_for_window(Window& window) 275{ 276 return frame_rect_for_window(window, window.rect()); 277} 278 279Gfx::Rect WindowFrame::rect() const 280{ 281 return frame_rect_for_window(m_window); 282} 283 284void WindowFrame::invalidate_title_bar() 285{ 286 WindowManager::the().invalidate(title_bar_rect().translated(rect().location())); 287} 288 289void WindowFrame::notify_window_rect_changed(const Gfx::Rect& old_rect, const Gfx::Rect& new_rect) 290{ 291 int window_button_width = 15; 292 int window_button_height = 15; 293 int x = title_bar_text_rect().right() + 1; 294 295 for (auto& button : m_buttons) { 296 x -= window_button_width; 297 Gfx::Rect rect { x, 0, window_button_width, window_button_height }; 298 rect.center_vertically_within(title_bar_text_rect()); 299 button.set_relative_rect(rect); 300 } 301 302 auto& wm = WindowManager::the(); 303 wm.invalidate(frame_rect_for_window(m_window, old_rect)); 304 wm.invalidate(frame_rect_for_window(m_window, new_rect)); 305 wm.notify_rect_changed(m_window, old_rect, new_rect); 306} 307 308void WindowFrame::on_mouse_event(const MouseEvent& event) 309{ 310 ASSERT(!m_window.is_fullscreen()); 311 312 if (m_window.is_blocked_by_modal_window()) 313 return; 314 315 auto& wm = WindowManager::the(); 316 if (m_window.type() != WindowType::Normal) 317 return; 318 319 if (event.type() == Event::MouseDown && (event.button() == MouseButton::Left || event.button() == MouseButton::Right) && title_bar_icon_rect().contains(event.position())) { 320 wm.move_to_front_and_make_active(m_window); 321 m_window.popup_window_menu(event.position().translated(rect().location())); 322 return; 323 } 324 325 // This is slightly hackish, but expand the title bar rect by one pixel downwards, 326 // so that mouse events between the title bar and window contents don't act like 327 // mouse events on the border. 328 auto adjusted_title_bar_rect = title_bar_rect(); 329 adjusted_title_bar_rect.set_height(adjusted_title_bar_rect.height() + 1); 330 331 if (adjusted_title_bar_rect.contains(event.position())) { 332 wm.clear_resize_candidate(); 333 334 if (event.type() == Event::MouseDown) 335 wm.move_to_front_and_make_active(m_window); 336 337 for (auto& button : m_buttons) { 338 if (button.relative_rect().contains(event.position())) 339 return button.on_mouse_event(event.translated(-button.relative_rect().location())); 340 } 341 if (event.type() == Event::MouseDown) { 342 if (event.button() == MouseButton::Right) { 343 m_window.popup_window_menu(event.position().translated(rect().location())); 344 return; 345 } 346 if (event.button() == MouseButton::Left) 347 wm.start_window_move(m_window, event.translated(rect().location())); 348 } 349 return; 350 } 351 352 if (m_window.is_resizable() && event.type() == Event::MouseMove && event.buttons() == 0) { 353 constexpr ResizeDirection direction_for_hot_area[3][3] = { 354 { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight }, 355 { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right }, 356 { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight }, 357 }; 358 Gfx::Rect outer_rect = { {}, rect().size() }; 359 ASSERT(outer_rect.contains(event.position())); 360 int window_relative_x = event.x() - outer_rect.x(); 361 int window_relative_y = event.y() - outer_rect.y(); 362 int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3)); 363 int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3)); 364 wm.set_resize_candidate(m_window, direction_for_hot_area[hot_area_row][hot_area_column]); 365 Compositor::the().invalidate_cursor(); 366 return; 367 } 368 369 if (m_window.is_resizable() && event.type() == Event::MouseDown && event.button() == MouseButton::Left) 370 wm.start_window_resize(m_window, event.translated(rect().location())); 371} 372 373}