Serenity Operating System
at master 1094 lines 39 kB view raw
1/* 2 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include "Window.h" 8#include "Animation.h" 9#include "AppletManager.h" 10#include "Compositor.h" 11#include "ConnectionFromClient.h" 12#include "Event.h" 13#include "EventLoop.h" 14#include "Screen.h" 15#include "WindowManager.h" 16#include <AK/Badge.h> 17#include <AK/CharacterTypes.h> 18#include <AK/Debug.h> 19#include <LibCore/Account.h> 20#include <LibCore/ProcessStatisticsReader.h> 21#include <LibCore/SessionManagement.h> 22 23namespace WindowServer { 24 25static DeprecatedString default_window_icon_path() 26{ 27 return "/res/icons/16x16/window.png"; 28} 29 30static Gfx::Bitmap& default_window_icon() 31{ 32 static RefPtr<Gfx::Bitmap> s_icon; 33 if (!s_icon) 34 s_icon = Gfx::Bitmap::load_from_file(default_window_icon_path()).release_value_but_fixme_should_propagate_errors(); 35 return *s_icon; 36} 37 38static Gfx::Bitmap& minimize_icon() 39{ 40 static RefPtr<Gfx::Bitmap> s_icon; 41 if (!s_icon) 42 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/downward-triangle.png"sv).release_value_but_fixme_should_propagate_errors(); 43 return *s_icon; 44} 45 46static Gfx::Bitmap& maximize_icon() 47{ 48 static RefPtr<Gfx::Bitmap> s_icon; 49 if (!s_icon) 50 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/upward-triangle.png"sv).release_value_but_fixme_should_propagate_errors(); 51 return *s_icon; 52} 53 54static Gfx::Bitmap& restore_icon() 55{ 56 static RefPtr<Gfx::Bitmap> s_icon; 57 if (!s_icon) 58 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-restore.png"sv).release_value_but_fixme_should_propagate_errors(); 59 return *s_icon; 60} 61 62static Gfx::Bitmap& close_icon() 63{ 64 static RefPtr<Gfx::Bitmap> s_icon; 65 if (!s_icon) 66 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-close.png"sv).release_value_but_fixme_should_propagate_errors(); 67 return *s_icon; 68} 69 70static Gfx::Bitmap& pin_icon() 71{ 72 static RefPtr<Gfx::Bitmap> s_icon; 73 if (!s_icon) 74 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/window-pin.png"sv).release_value_but_fixme_should_propagate_errors(); 75 return *s_icon; 76} 77 78static Gfx::Bitmap& move_icon() 79{ 80 static RefPtr<Gfx::Bitmap> s_icon; 81 if (!s_icon) 82 s_icon = Gfx::Bitmap::load_from_file("/res/icons/16x16/move.png"sv).release_value_but_fixme_should_propagate_errors(); 83 return *s_icon; 84} 85 86Window::Window(Core::Object& parent, WindowType type) 87 : Core::Object(&parent) 88 , m_type(type) 89 , m_icon(default_window_icon()) 90 , m_frame(*this) 91{ 92 WindowManager::the().add_window(*this); 93 frame().window_was_constructed({}); 94} 95 96Window::Window(ConnectionFromClient& client, WindowType window_type, WindowMode window_mode, int window_id, bool minimizable, bool closeable, bool frameless, bool resizable, bool fullscreen, Window* parent_window) 97 : Core::Object(&client) 98 , m_client(&client) 99 , m_type(window_type) 100 , m_mode(window_mode) 101 , m_minimizable(minimizable) 102 , m_closeable(closeable) 103 , m_frameless(frameless) 104 , m_resizable(resizable) 105 , m_fullscreen(fullscreen) 106 , m_window_id(window_id) 107 , m_client_id(client.client_id()) 108 , m_icon(default_window_icon()) 109 , m_frame(*this) 110{ 111 if (parent_window) 112 set_parent_window(*parent_window); 113 if (!is_frameless() && type() == WindowType::Normal) { 114 if (auto title_username_maybe = compute_title_username(&client); !title_username_maybe.is_error()) 115 m_title_username = title_username_maybe.release_value(); 116 } 117 WindowManager::the().add_window(*this); 118 frame().window_was_constructed({}); 119} 120 121Window::~Window() 122{ 123 // Detach from client at the start of teardown since we don't want 124 // to confuse things by trying to send messages to it. 125 m_client = nullptr; 126 127 WindowManager::the().remove_window(*this); 128} 129 130void Window::destroy() 131{ 132 m_destroyed = true; 133 set_visible(false); 134} 135 136void Window::set_title(DeprecatedString const& title) 137{ 138 if (m_title == title) 139 return; 140 m_title = title; 141 frame().invalidate_titlebar(); 142 WindowManager::the().notify_title_changed(*this); 143} 144 145void Window::set_rect(Gfx::IntRect const& rect) 146{ 147 if (m_rect == rect) 148 return; 149 auto old_rect = m_rect; 150 m_rect = rect; 151 if (rect.is_empty()) { 152 m_backing_store = nullptr; 153 } else if (is_internal() && (!m_backing_store || old_rect.size() != rect.size())) { 154 auto format = has_alpha_channel() ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888; 155 m_backing_store = Gfx::Bitmap::create(format, m_rect.size()).release_value_but_fixme_should_propagate_errors(); 156 m_backing_store_visible_size = m_rect.size(); 157 } 158 159 if (m_floating_rect.is_empty()) 160 m_floating_rect = rect; 161 162 invalidate(true, old_rect.size() != rect.size()); 163 m_frame.window_rect_changed(old_rect, rect); 164 invalidate_last_rendered_screen_rects(); 165} 166 167void Window::set_rect_without_repaint(Gfx::IntRect const& rect) 168{ 169 VERIFY(rect.width() >= 0 && rect.height() >= 0); 170 if (m_rect == rect) 171 return; 172 auto old_rect = m_rect; 173 m_rect = rect; 174 175 invalidate(true, old_rect.size() != rect.size()); 176 m_frame.window_rect_changed(old_rect, rect); 177 invalidate_last_rendered_screen_rects(); 178} 179 180bool Window::apply_minimum_size(Gfx::IntRect& rect) 181{ 182 int new_width = max(m_minimum_size.width(), rect.width()); 183 int new_height = max(m_minimum_size.height(), rect.height()); 184 bool did_size_clamp = new_width != rect.width() || new_height != rect.height(); 185 186 rect.set_width(new_width); 187 rect.set_height(new_height); 188 189 return did_size_clamp; 190} 191 192void Window::set_minimum_size(Gfx::IntSize size) 193{ 194 VERIFY(size.width() >= 0 && size.height() >= 0); 195 if (m_minimum_size == size) 196 return; 197 m_minimum_size = size; 198} 199 200void Window::handle_mouse_event(MouseEvent const& event) 201{ 202 set_automatic_cursor_tracking_enabled(event.buttons() != 0); 203 204 switch (event.type()) { 205 case Event::MouseMove: 206 m_client->async_mouse_move(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y(), event.is_drag(), event.mime_types()); 207 break; 208 case Event::MouseDown: 209 m_client->async_mouse_down(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y()); 210 break; 211 case Event::MouseDoubleClick: 212 m_client->async_mouse_double_click(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y()); 213 break; 214 case Event::MouseUp: 215 m_client->async_mouse_up(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y()); 216 break; 217 case Event::MouseWheel: 218 m_client->async_mouse_wheel(m_window_id, event.position(), (u32)event.button(), event.buttons(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y()); 219 break; 220 default: 221 VERIFY_NOT_REACHED(); 222 } 223} 224 225void Window::update_window_menu_items() 226{ 227 if (!m_window_menu) 228 return; 229 230 m_window_menu_minimize_item->set_text(m_minimized_state != WindowMinimizedState::None ? "&Unminimize" : "Mi&nimize"); 231 m_window_menu_minimize_item->set_enabled(m_minimizable && !is_modal()); 232 233 m_window_menu_maximize_item->set_text(is_maximized() ? "&Restore" : "Ma&ximize"); 234 m_window_menu_maximize_item->set_enabled(m_resizable); 235 236 m_window_menu_close_item->set_enabled(m_closeable); 237 238 m_window_menu_move_item->set_enabled(m_minimized_state == WindowMinimizedState::None && !is_maximized() && !m_fullscreen); 239 240 if (m_window_menu_always_on_top_item) 241 m_window_menu_always_on_top_item->set_checked(m_always_on_top); 242 243 m_window_menu->update_alt_shortcuts_for_items(); 244} 245 246void Window::set_minimized(bool minimized) 247{ 248 if ((m_minimized_state != WindowMinimizedState::None) == minimized) 249 return; 250 if (minimized && !m_minimizable) 251 return; 252 m_minimized_state = minimized ? WindowMinimizedState::Minimized : WindowMinimizedState::None; 253 update_window_menu_items(); 254 255 if (!blocking_modal_window()) 256 start_minimize_animation(); 257 if (!minimized) 258 request_update({ {}, size() }); 259 260 // Since a minimized window won't be visible we need to invalidate the last rendered 261 // rectangles before the next occlusion calculation 262 invalidate_last_rendered_screen_rects_now(); 263 264 WindowManager::the().notify_minimization_state_changed(*this); 265} 266 267void Window::set_hidden(bool hidden) 268{ 269 if ((m_minimized_state != WindowMinimizedState::None) == hidden) 270 return; 271 if (hidden && !m_minimizable) 272 return; 273 m_minimized_state = hidden ? WindowMinimizedState::Hidden : WindowMinimizedState::None; 274 update_window_menu_items(); 275 Compositor::the().invalidate_occlusions(); 276 Compositor::the().invalidate_screen(frame().render_rect()); 277 if (!blocking_modal_window()) 278 start_minimize_animation(); 279 if (!hidden) 280 request_update({ {}, size() }); 281 WindowManager::the().notify_minimization_state_changed(*this); 282} 283 284void Window::set_minimizable(bool minimizable) 285{ 286 if (m_minimizable == minimizable) 287 return; 288 m_minimizable = minimizable; 289 update_window_menu_items(); 290 // TODO: Hide/show (or alternatively change enabled state of) window minimize button dynamically depending on value of m_minimizable 291} 292 293void Window::set_closeable(bool closeable) 294{ 295 if (m_closeable == closeable) 296 return; 297 m_closeable = closeable; 298 update_window_menu_items(); 299} 300 301void Window::set_taskbar_rect(Gfx::IntRect const& rect) 302{ 303 if (m_taskbar_rect == rect) 304 return; 305 m_taskbar_rect = rect; 306} 307 308static Gfx::IntRect interpolate_rect(Gfx::IntRect const& from_rect, Gfx::IntRect const& to_rect, float progress) 309{ 310 auto dx = to_rect.x() - from_rect.x(); 311 auto dy = to_rect.y() - from_rect.y(); 312 auto dw = to_rect.width() - from_rect.width(); 313 auto dh = to_rect.height() - from_rect.height(); 314 315 return Gfx::IntRect { 316 from_rect.x() + ((float)dx * progress), 317 from_rect.y() + ((float)dy * progress), 318 from_rect.width() + ((float)dw * progress), 319 from_rect.height() + ((float)dh * progress), 320 }; 321} 322 323void Window::start_minimize_animation() 324{ 325 if (&window_stack() != &WindowManager::the().current_window_stack()) 326 return; 327 if (!WindowManager::the().system_effects().animate_windows()) 328 return; 329 if (is_modal()) { 330 if (auto modeless = modeless_ancestor(); modeless) { 331 auto rect = modeless->taskbar_rect(); 332 VERIFY(!rect.is_empty()); 333 m_taskbar_rect = rect; 334 } 335 } 336 m_animation = Animation::create(); 337 m_animation->set_duration(150); 338 m_animation->on_update = [this](float progress, Gfx::Painter& painter, Screen& screen, Gfx::DisjointIntRectSet& flush_rects) { 339 Gfx::PainterStateSaver saver(painter); 340 painter.set_draw_op(Gfx::Painter::DrawOp::Invert); 341 342 auto from_rect = is_minimized() ? frame().rect() : taskbar_rect(); 343 auto to_rect = is_minimized() ? taskbar_rect() : frame().rect(); 344 345 auto rect = interpolate_rect(from_rect, to_rect, progress); 346 347 painter.draw_rect(rect, Color::Transparent); // Color doesn't matter, we draw inverted 348 flush_rects.add(rect.intersected(screen.rect())); 349 Compositor::the().invalidate_screen(rect); 350 }; 351 m_animation->on_stop = [this] { 352 m_animation = nullptr; 353 }; 354 m_animation->start(); 355} 356 357void Window::start_launch_animation(Gfx::IntRect const& launch_origin_rect) 358{ 359 if (&window_stack() != &WindowManager::the().current_window_stack()) 360 return; 361 if (!WindowManager::the().system_effects().animate_windows()) 362 return; 363 364 m_animation = Animation::create(); 365 m_animation->set_duration(150); 366 m_animation->on_update = [this, launch_origin_rect](float progress, Gfx::Painter& painter, Screen& screen, Gfx::DisjointIntRectSet& flush_rects) { 367 Gfx::PainterStateSaver saver(painter); 368 painter.set_draw_op(Gfx::Painter::DrawOp::Invert); 369 370 auto rect = interpolate_rect(launch_origin_rect, frame().rect(), progress); 371 372 painter.draw_rect(rect, Color::Transparent); // Color doesn't matter, we draw inverted 373 flush_rects.add(rect.intersected(screen.rect())); 374 Compositor::the().invalidate_screen(rect); 375 }; 376 m_animation->on_stop = [this] { 377 m_animation = nullptr; 378 }; 379 m_animation->start(); 380} 381 382void Window::set_opacity(float opacity) 383{ 384 if (m_opacity == opacity) 385 return; 386 bool was_opaque = is_opaque(); 387 m_opacity = opacity; 388 if (was_opaque != is_opaque()) 389 Compositor::the().invalidate_occlusions(); 390 invalidate(false); 391 WindowManager::the().notify_opacity_changed(*this); 392} 393 394void Window::set_has_alpha_channel(bool value) 395{ 396 if (m_has_alpha_channel == value) 397 return; 398 m_has_alpha_channel = value; 399 Compositor::the().invalidate_occlusions(); 400} 401 402void Window::set_occluded(bool occluded) 403{ 404 if (m_occluded == occluded) 405 return; 406 m_occluded = occluded; 407 WindowManager::the().notify_occlusion_state_changed(*this); 408} 409 410void Window::set_maximized(bool maximized) 411{ 412 if (is_maximized() == maximized) 413 return; 414 if (maximized && (!is_resizable() || resize_aspect_ratio().has_value())) 415 return; 416 m_tile_type = maximized ? WindowTileType::Maximized : WindowTileType::None; 417 update_window_menu_items(); 418 if (maximized) 419 set_rect(WindowManager::the().tiled_window_rect(*this)); 420 else 421 set_rect(m_floating_rect); 422 m_frame.did_set_maximized({}, maximized); 423 Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect)); 424 Core::EventLoop::current().post_event(*this, make<MoveEvent>(m_rect)); 425 set_default_positioned(false); 426 427 WindowManager::the().notify_minimization_state_changed(*this); 428} 429 430void Window::set_always_on_top(bool always_on_top) 431{ 432 if (m_always_on_top == always_on_top) 433 return; 434 435 m_always_on_top = always_on_top; 436 update_window_menu_items(); 437 438 window_stack().move_always_on_top_windows_to_front(); 439 Compositor::the().invalidate_occlusions(); 440} 441 442void Window::set_resizable(bool resizable) 443{ 444 if (m_resizable == resizable) 445 return; 446 m_resizable = resizable; 447 update_window_menu_items(); 448 // TODO: Hide/show (or alternatively change enabled state of) window maximize button dynamically depending on value of is_resizable() 449} 450 451void Window::event(Core::Event& event) 452{ 453 if (is_internal()) { 454 VERIFY(parent()); 455 event.ignore(); 456 return; 457 } 458 459 if (blocking_modal_window() && type() != WindowType::Popup) { 460 // Allow windows to process their inactivity after being blocked 461 if (event.type() != Event::WindowDeactivated && event.type() != Event::WindowInputPreempted) 462 return; 463 } 464 465 if (static_cast<Event&>(event).is_mouse_event()) 466 return handle_mouse_event(static_cast<MouseEvent const&>(event)); 467 468 switch (event.type()) { 469 case Event::WindowEntered: 470 m_client->async_window_entered(m_window_id); 471 break; 472 case Event::WindowLeft: 473 m_client->async_window_left(m_window_id); 474 break; 475 case Event::KeyDown: 476 handle_keydown_event(static_cast<KeyEvent const&>(event)); 477 break; 478 case Event::KeyUp: 479 m_client->async_key_up(m_window_id, 480 (u32) static_cast<KeyEvent const&>(event).code_point(), 481 (u32) static_cast<KeyEvent const&>(event).key(), 482 static_cast<KeyEvent const&>(event).modifiers(), 483 (u32) static_cast<KeyEvent const&>(event).scancode()); 484 break; 485 case Event::WindowActivated: 486 m_client->async_window_activated(m_window_id); 487 break; 488 case Event::WindowDeactivated: 489 m_client->async_window_deactivated(m_window_id); 490 break; 491 case Event::WindowInputPreempted: 492 m_client->async_window_input_preempted(m_window_id); 493 break; 494 case Event::WindowInputRestored: 495 m_client->async_window_input_restored(m_window_id); 496 break; 497 case Event::WindowCloseRequest: 498 m_client->async_window_close_request(m_window_id); 499 break; 500 case Event::WindowResized: 501 m_client->async_window_resized(m_window_id, static_cast<ResizeEvent const&>(event).rect()); 502 break; 503 case Event::WindowMoved: 504 m_client->async_window_moved(m_window_id, static_cast<MoveEvent const&>(event).rect()); 505 break; 506 default: 507 break; 508 } 509} 510 511void Window::handle_keydown_event(KeyEvent const& event) 512{ 513 if (event.modifiers() == Mod_Alt && event.key() == Key_Space && type() == WindowType::Normal && !is_frameless()) { 514 auto position = frame().titlebar_rect().bottom_left().translated(frame().rect().location()); 515 popup_window_menu(position, WindowMenuDefaultAction::Close); 516 return; 517 } 518 if (event.modifiers() == Mod_Alt && event.code_point() && m_menubar.has_menus()) { 519 Menu* menu_to_open = nullptr; 520 m_menubar.for_each_menu([&](Menu& menu) { 521 if (to_ascii_lowercase(menu.alt_shortcut_character()) == to_ascii_lowercase(event.code_point())) { 522 menu_to_open = &menu; 523 return IterationDecision::Break; 524 } 525 return IterationDecision::Continue; 526 }); 527 528 if (menu_to_open) { 529 frame().open_menubar_menu(*menu_to_open); 530 if (!menu_to_open->is_empty()) 531 menu_to_open->set_hovered_index(0); 532 return; 533 } 534 } 535 m_client->async_key_down(m_window_id, (u32)event.code_point(), (u32)event.key(), event.modifiers(), (u32)event.scancode()); 536} 537 538void Window::set_visible(bool b) 539{ 540 if (m_visible == b) 541 return; 542 m_visible = b; 543 544 WindowManager::the().reevaluate_hover_state_for_window(this); 545 546 if (!m_visible) 547 WindowManager::the().check_hide_geometry_overlay(*this); 548 Compositor::the().invalidate_occlusions(); 549 if (m_visible) { 550 invalidate(true); 551 } else { 552 // Since the window won't be visible we need to invalidate the last rendered 553 // rectangles before the next occlusion calculation 554 invalidate_last_rendered_screen_rects_now(); 555 } 556} 557 558void Window::set_frameless(bool frameless) 559{ 560 if (m_frameless == frameless) 561 return; 562 m_frameless = frameless; 563 if (m_visible) { 564 Compositor::the().invalidate_occlusions(); 565 invalidate(true, true); 566 invalidate_last_rendered_screen_rects(); 567 } 568} 569 570void Window::invalidate(bool invalidate_frame, bool re_render_frame) 571{ 572 m_invalidated = true; 573 m_invalidated_all = true; 574 if (invalidate_frame && !m_invalidated_frame) { 575 m_invalidated_frame = true; 576 } 577 if (re_render_frame) 578 frame().set_dirty(true); 579 m_dirty_rects.clear(); 580 Compositor::the().invalidate_window(); 581} 582 583void Window::invalidate(Gfx::IntRect const& rect, bool invalidate_frame) 584{ 585 if (type() == WindowType::Applet) { 586 AppletManager::the().invalidate_applet(*this, rect); 587 return; 588 } 589 590 if (invalidate_no_notify(rect, invalidate_frame)) 591 Compositor::the().invalidate_window(); 592} 593 594bool Window::invalidate_no_notify(Gfx::IntRect const& rect, bool invalidate_frame) 595{ 596 if (rect.is_empty()) 597 return false; 598 if (m_invalidated_all) { 599 if (invalidate_frame) 600 m_invalidated_frame |= true; 601 return false; 602 } 603 604 auto outer_rect = frame().render_rect(); 605 auto inner_rect = rect; 606 inner_rect.translate_by(position()); 607 // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect. 608 inner_rect.intersect(outer_rect); 609 if (inner_rect.is_empty()) 610 return false; 611 612 m_invalidated = true; 613 if (invalidate_frame) 614 m_invalidated_frame |= true; 615 m_dirty_rects.add(inner_rect.translated(-outer_rect.location())); 616 return true; 617} 618 619void Window::invalidate_last_rendered_screen_rects() 620{ 621 m_invalidate_last_render_rects = true; 622 Compositor::the().invalidate_occlusions(); 623} 624 625void Window::invalidate_last_rendered_screen_rects_now() 626{ 627 // We can't wait for the next occlusion computation because the window will either no longer 628 // be around or won't be visible anymore. So we need to invalidate the last rendered rects now. 629 if (!m_opaque_rects.is_empty()) 630 Compositor::the().invalidate_screen(m_opaque_rects); 631 if (!m_transparency_rects.is_empty()) 632 Compositor::the().invalidate_screen(m_transparency_rects); 633 m_invalidate_last_render_rects = false; 634 Compositor::the().invalidate_occlusions(); 635} 636 637void Window::refresh_client_size() 638{ 639 client()->async_window_resized(m_window_id, m_rect); 640} 641 642void Window::prepare_dirty_rects() 643{ 644 if (m_invalidated_all) { 645 if (m_invalidated_frame) 646 m_dirty_rects = frame().render_rect(); 647 else 648 m_dirty_rects = rect(); 649 } else { 650 m_dirty_rects.move_by(frame().render_rect().location()); 651 if (m_invalidated_frame) { 652 if (m_invalidated) { 653 m_dirty_rects.add(frame().render_rect()); 654 } else { 655 for (auto& rects : frame().render_rect().shatter(rect())) 656 m_dirty_rects.add(rects); 657 } 658 } 659 } 660} 661 662void Window::clear_dirty_rects() 663{ 664 m_invalidated_all = false; 665 m_invalidated_frame = false; 666 m_invalidated = false; 667 m_dirty_rects.clear_with_capacity(); 668} 669 670bool Window::is_active() const 671{ 672 VERIFY(m_window_stack); 673 return m_window_stack->active_window() == this; 674} 675 676Window* Window::blocking_modal_window() 677{ 678 auto maybe_blocker = WindowManager::the().for_each_window_in_modal_chain(*this, [&](auto& window) { 679 if (is_descendant_of(window)) 680 return IterationDecision::Continue; 681 if (window.is_blocking() && this != &window) 682 return IterationDecision::Break; 683 return IterationDecision::Continue; 684 }); 685 return maybe_blocker; 686} 687 688void Window::set_default_icon() 689{ 690 m_icon = default_window_icon(); 691} 692 693void Window::request_update(Gfx::IntRect const& rect, bool ignore_occlusion) 694{ 695 if (rect.is_empty()) 696 return; 697 if (m_pending_paint_rects.is_empty()) { 698 deferred_invoke([this, ignore_occlusion] { 699 client()->post_paint_message(*this, ignore_occlusion); 700 }); 701 } 702 m_pending_paint_rects.add(rect); 703} 704 705void Window::ensure_window_menu() 706{ 707 if (!m_window_menu) { 708 m_window_menu = Menu::construct(nullptr, -1, "(Window Menu)"); 709 m_window_menu->set_window_menu_of(*this); 710 711 auto minimize_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::MinimizeOrUnminimize, ""); 712 m_window_menu_minimize_item = minimize_item.ptr(); 713 m_window_menu->add_item(move(minimize_item)); 714 715 auto maximize_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::MaximizeOrRestore, ""); 716 m_window_menu_maximize_item = maximize_item.ptr(); 717 m_window_menu->add_item(move(maximize_item)); 718 719 auto move_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::Move, "&Move"); 720 m_window_menu_move_item = move_item.ptr(); 721 m_window_menu_move_item->set_icon(&move_icon()); 722 m_window_menu->add_item(move(move_item)); 723 724 m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator)); 725 726 auto menubar_visibility_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::ToggleMenubarVisibility, "Menu &Bar"); 727 m_window_menu_menubar_visibility_item = menubar_visibility_item.ptr(); 728 menubar_visibility_item->set_checkable(true); 729 m_window_menu->add_item(move(menubar_visibility_item)); 730 731 m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator)); 732 733 if (!is_modal()) { 734 auto pin_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::ToggleAlwaysOnTop, "Always on &Top"); 735 m_window_menu_always_on_top_item = pin_item.ptr(); 736 m_window_menu_always_on_top_item->set_icon(&pin_icon()); 737 m_window_menu_always_on_top_item->set_checkable(true); 738 m_window_menu->add_item(move(pin_item)); 739 m_window_menu->add_item(make<MenuItem>(*m_window_menu, MenuItem::Type::Separator)); 740 } 741 742 auto close_item = make<MenuItem>(*m_window_menu, (unsigned)WindowMenuAction::Close, "&Close"); 743 m_window_menu_close_item = close_item.ptr(); 744 m_window_menu_close_item->set_icon(&close_icon()); 745 m_window_menu_close_item->set_default(true); 746 m_window_menu->add_item(move(close_item)); 747 748 m_window_menu->on_item_activation = [&](auto& item) { 749 handle_window_menu_action(static_cast<WindowMenuAction>(item.identifier())); 750 }; 751 752 update_window_menu_items(); 753 } 754} 755 756void Window::handle_window_menu_action(WindowMenuAction action) 757{ 758 switch (action) { 759 case WindowMenuAction::MinimizeOrUnminimize: 760 WindowManager::the().minimize_windows(*this, m_minimized_state == WindowMinimizedState::None); 761 if (m_minimized_state == WindowMinimizedState::None) 762 WindowManager::the().move_to_front_and_make_active(*this); 763 break; 764 case WindowMenuAction::MaximizeOrRestore: 765 WindowManager::the().maximize_windows(*this, !is_maximized()); 766 WindowManager::the().move_to_front_and_make_active(*this); 767 break; 768 case WindowMenuAction::Move: 769 WindowManager::the().start_window_move(*this, ScreenInput::the().cursor_location()); 770 break; 771 case WindowMenuAction::Close: 772 request_close(); 773 break; 774 case WindowMenuAction::ToggleMenubarVisibility: { 775 auto& item = *m_window_menu->item_by_identifier((unsigned)action); 776 frame().invalidate(); 777 item.set_checked(!item.is_checked()); 778 m_should_show_menubar = item.is_checked(); 779 frame().invalidate(); 780 recalculate_rect(); 781 invalidate_last_rendered_screen_rects(); 782 break; 783 } 784 case WindowMenuAction::ToggleAlwaysOnTop: { 785 auto& item = *m_window_menu->item_by_identifier((unsigned)action); 786 auto new_is_checked = !item.is_checked(); 787 item.set_checked(new_is_checked); 788 WindowManager::the().set_always_on_top(*this, new_is_checked); 789 break; 790 } 791 } 792} 793 794void Window::popup_window_menu(Gfx::IntPoint position, WindowMenuDefaultAction default_action) 795{ 796 ensure_window_menu(); 797 if (default_action == WindowMenuDefaultAction::BasedOnWindowState) { 798 // When clicked on the task bar, determine the default action 799 if (!is_active() && !is_minimized()) 800 default_action = WindowMenuDefaultAction::None; 801 else if (is_minimized()) 802 default_action = WindowMenuDefaultAction::Unminimize; 803 else 804 default_action = WindowMenuDefaultAction::Minimize; 805 } 806 m_window_menu_minimize_item->set_default(default_action == WindowMenuDefaultAction::Minimize || default_action == WindowMenuDefaultAction::Unminimize); 807 m_window_menu_minimize_item->set_icon(m_minimized_state != WindowMinimizedState::None ? nullptr : &minimize_icon()); 808 m_window_menu_maximize_item->set_default(default_action == WindowMenuDefaultAction::Maximize || default_action == WindowMenuDefaultAction::Restore); 809 m_window_menu_maximize_item->set_icon(is_maximized() ? &restore_icon() : &maximize_icon()); 810 m_window_menu_close_item->set_default(default_action == WindowMenuDefaultAction::Close); 811 m_window_menu_menubar_visibility_item->set_enabled(m_menubar.has_menus()); 812 m_window_menu_menubar_visibility_item->set_checked(m_menubar.has_menus() && m_should_show_menubar); 813 814 m_window_menu->popup(position); 815} 816 817void Window::window_menu_activate_default() 818{ 819 ensure_window_menu(); 820 m_window_menu->activate_default(); 821} 822 823void Window::request_close() 824{ 825 if (is_destroyed()) 826 return; 827 828 Event close_request(Event::WindowCloseRequest); 829 event(close_request); 830} 831 832void Window::set_fullscreen(bool fullscreen) 833{ 834 if (m_fullscreen == fullscreen) 835 return; 836 m_fullscreen = fullscreen; 837 Gfx::IntRect new_window_rect = m_rect; 838 if (m_fullscreen) { 839 m_saved_nonfullscreen_rect = m_rect; 840 new_window_rect = Screen::main().rect(); // TODO: We should support fullscreen on any screen 841 } else if (!m_saved_nonfullscreen_rect.is_empty()) { 842 new_window_rect = m_saved_nonfullscreen_rect; 843 } 844 845 Core::EventLoop::current().post_event(*this, make<ResizeEvent>(new_window_rect)); 846 Core::EventLoop::current().post_event(*this, make<MoveEvent>(new_window_rect)); 847 set_rect(new_window_rect); 848} 849 850WindowTileType Window::tile_type_based_on_rect(Gfx::IntRect const& rect) const 851{ 852 auto& window_screen = Screen::closest_to_rect(this->rect()); // based on currently used rect 853 auto tile_type = WindowTileType::None; 854 if (window_screen.rect().contains(rect)) { 855 auto current_tile_type = this->tile_type(); 856 bool tiling_to_top = current_tile_type == WindowTileType::Top || current_tile_type == WindowTileType::TopLeft || current_tile_type == WindowTileType::TopRight; 857 bool tiling_to_bottom = current_tile_type == WindowTileType::Bottom || current_tile_type == WindowTileType::BottomLeft || current_tile_type == WindowTileType::BottomRight; 858 bool tiling_to_left = current_tile_type == WindowTileType::Left || current_tile_type == WindowTileType::TopLeft || current_tile_type == WindowTileType::BottomLeft; 859 bool tiling_to_right = current_tile_type == WindowTileType::Right || current_tile_type == WindowTileType::TopRight || current_tile_type == WindowTileType::BottomRight; 860 861 auto ideal_tiled_rect = WindowManager::the().tiled_window_rect(*this, current_tile_type); 862 bool same_top = ideal_tiled_rect.top() == rect.top(); 863 bool same_left = ideal_tiled_rect.left() == rect.left(); 864 bool same_right = ideal_tiled_rect.right() == rect.right(); 865 bool same_bottom = ideal_tiled_rect.bottom() == rect.bottom(); 866 867 // Try to find the most suitable tile type. For example, if a window is currently tiled to the BottomRight and 868 // the window is resized upwards as to where it perfectly touches the screen's top border, then the more suitable 869 // tile type would be Right, as three sides are lined up perfectly. 870 if (tiling_to_top && same_top && same_left && same_right) 871 return WindowTileType::Top; 872 else if ((tiling_to_top || tiling_to_left) && same_top && same_left) 873 return rect.bottom() == WindowManager::the().tiled_window_rect(*this, WindowTileType::Bottom).bottom() ? WindowTileType::Left : WindowTileType::TopLeft; 874 else if ((tiling_to_top || tiling_to_right) && same_top && same_right) 875 return rect.bottom() == WindowManager::the().tiled_window_rect(*this, WindowTileType::Bottom).bottom() ? WindowTileType::Right : WindowTileType::TopRight; 876 else if (tiling_to_left && same_left && same_top && same_bottom) 877 return WindowTileType::Left; 878 else if (tiling_to_right && same_right && same_top && same_bottom) 879 return WindowTileType::Right; 880 else if (tiling_to_bottom && same_bottom && same_left && same_right) 881 return WindowTileType::Bottom; 882 else if ((tiling_to_bottom || tiling_to_left) && same_bottom && same_left) 883 return rect.top() == WindowManager::the().tiled_window_rect(*this, WindowTileType::Left).top() ? WindowTileType::Left : WindowTileType::BottomLeft; 884 else if ((tiling_to_bottom || tiling_to_right) && same_bottom && same_right) 885 return rect.top() == WindowManager::the().tiled_window_rect(*this, WindowTileType::Right).top() ? WindowTileType::Right : WindowTileType::BottomRight; 886 } 887 return tile_type; 888} 889 890void Window::check_untile_due_to_resize(Gfx::IntRect const& new_rect) 891{ 892 auto new_tile_type = tile_type_based_on_rect(new_rect); 893 if constexpr (RESIZE_DEBUG) { 894 if (new_tile_type == WindowTileType::None) { 895 auto current_rect = rect(); 896 auto& window_screen = Screen::closest_to_rect(current_rect); 897 if (!(window_screen.rect().contains(new_rect))) 898 dbgln("Untiling because new rect {} does not fit into screen #{} rect {}", new_rect, window_screen.index(), window_screen.rect()); 899 else 900 dbgln("Untiling because new rect {} does not touch screen #{} rect {}", new_rect, window_screen.index(), window_screen.rect()); 901 } else if (new_tile_type != m_tile_type) 902 dbgln("Changing tile type from {} to {}", (int)m_tile_type, (int)new_tile_type); 903 } 904 m_tile_type = new_tile_type; 905} 906 907bool Window::set_untiled() 908{ 909 if (m_tile_type == WindowTileType::None) 910 return false; 911 VERIFY(!resize_aspect_ratio().has_value()); 912 913 m_tile_type = WindowTileType::None; 914 set_rect(m_floating_rect); 915 916 Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect)); 917 Core::EventLoop::current().post_event(*this, make<MoveEvent>(m_rect)); 918 919 return true; 920} 921 922void Window::set_tiled(WindowTileType tile_type) 923{ 924 VERIFY(tile_type != WindowTileType::None); 925 926 if (m_tile_type == tile_type) 927 return; 928 929 if (resize_aspect_ratio().has_value()) 930 return; 931 932 if (is_maximized()) 933 set_maximized(false); 934 935 m_tile_type = tile_type; 936 937 set_rect(WindowManager::the().tiled_window_rect(*this, tile_type)); 938 Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect)); 939 Core::EventLoop::current().post_event(*this, make<MoveEvent>(m_rect)); 940} 941 942void Window::detach_client(Badge<ConnectionFromClient>) 943{ 944 m_client = nullptr; 945} 946 947void Window::recalculate_rect() 948{ 949 if (!is_resizable()) 950 return; 951 952 bool send_event = true; 953 if (is_tiled()) { 954 set_rect(WindowManager::the().tiled_window_rect(*this, m_tile_type)); 955 } else if (type() == WindowType::Desktop) { 956 set_rect(WindowManager::the().arena_rect_for_type(Screen::main(), WindowType::Desktop)); 957 } else { 958 send_event = false; 959 } 960 961 if (send_event) { 962 Core::EventLoop::current().post_event(*this, make<ResizeEvent>(m_rect)); 963 } 964} 965 966void Window::add_child_window(Window& child_window) 967{ 968 m_child_windows.append(child_window); 969} 970 971void Window::set_parent_window(Window& parent_window) 972{ 973 VERIFY(!m_parent_window); 974 m_parent_window = parent_window; 975 parent_window.add_child_window(*this); 976} 977 978Window* Window::modeless_ancestor() 979{ 980 if (!is_modal()) 981 return this; 982 for (auto parent = m_parent_window; parent; parent = parent->parent_window()) { 983 if (!parent->is_modal()) 984 return parent; 985 } 986 return nullptr; 987} 988 989void Window::set_progress(Optional<int> progress) 990{ 991 if (m_progress == progress) 992 return; 993 994 m_progress = progress; 995 WindowManager::the().notify_progress_changed(*this); 996} 997 998bool Window::is_descendant_of(Window& window) const 999{ 1000 for (auto* parent = parent_window(); parent; parent = parent->parent_window()) 1001 if (parent == &window) 1002 return true; 1003 return false; 1004} 1005 1006Optional<HitTestResult> Window::hit_test(Gfx::IntPoint position, bool include_frame) 1007{ 1008 if (!m_hit_testing_enabled) 1009 return {}; 1010 // We need to check the (possibly constrained) render rect to make sure 1011 // we don't hit-test on a window that is constrained to a screen, but somehow 1012 // (partially) moved into another screen where it's not rendered 1013 if (!frame().rect().intersected(frame().render_rect()).contains(position)) 1014 return {}; 1015 if (!rect().contains(position)) { 1016 if (include_frame) 1017 return frame().hit_test(position); 1018 return {}; 1019 } 1020 bool hit = false; 1021 u8 threshold = alpha_hit_threshold() * 255; 1022 if (threshold == 0 || !m_backing_store || !m_backing_store->has_alpha_channel()) { 1023 hit = true; 1024 } else { 1025 auto relative_point = position.translated(-rect().location()) * m_backing_store->scale(); 1026 u8 alpha = 0xff; 1027 if (m_backing_store->rect().contains(relative_point)) 1028 alpha = m_backing_store->get_pixel(relative_point).alpha(); 1029 hit = alpha >= threshold; 1030 } 1031 if (!hit) 1032 return {}; 1033 return HitTestResult { 1034 .window = *this, 1035 .screen_position = position, 1036 .window_relative_position = position.translated(-rect().location()), 1037 .is_frame_hit = false, 1038 }; 1039} 1040 1041void Window::add_menu(Menu& menu) 1042{ 1043 m_menubar.add_menu(menu, rect()); 1044 Compositor::the().invalidate_occlusions(); 1045 frame().invalidate(); 1046} 1047 1048void Window::invalidate_menubar() 1049{ 1050 if (!m_should_show_menubar || !m_menubar.has_menus()) 1051 return; 1052 frame().invalidate_menubar(); 1053} 1054 1055void Window::set_modified(bool modified) 1056{ 1057 if (m_modified == modified) 1058 return; 1059 1060 m_modified = modified; 1061 WindowManager::the().notify_modified_changed(*this); 1062 frame().set_button_icons(); 1063 frame().invalidate_titlebar(); 1064} 1065 1066DeprecatedString Window::computed_title() const 1067{ 1068 DeprecatedString title = m_title.replace("[*]"sv, is_modified() ? " (*)"sv : ""sv, ReplaceMode::FirstOnly); 1069 if (m_title_username.has_value()) 1070 title = DeprecatedString::formatted("{} [{}]", title, m_title_username.value()); 1071 if (client() && client()->is_unresponsive()) 1072 return DeprecatedString::formatted("{} (Not responding)", title); 1073 return title; 1074} 1075 1076ErrorOr<Optional<DeprecatedString>> Window::compute_title_username(ConnectionFromClient* client) 1077{ 1078 if (!client) 1079 return Error::from_string_literal("Tried to compute title username without a client"); 1080 auto stats = TRY(Core::ProcessStatisticsReader::get_all(true)); 1081 pid_t client_pid = TRY(client->socket().peer_pid()); 1082 auto client_stat = stats.processes.first_matching([&](auto& stat) { return stat.pid == client_pid; }); 1083 if (!client_stat.has_value()) 1084 return Error::from_string_literal("Failed to find client process stat"); 1085 pid_t login_session_pid = TRY(Core::SessionManagement::root_session_id(client_pid)); 1086 auto login_session_stat = stats.processes.first_matching([&](auto& stat) { return stat.pid == login_session_pid; }); 1087 if (!login_session_stat.has_value()) 1088 return Error::from_string_literal("Failed to find login process stat"); 1089 if (login_session_stat.value().uid == client_stat.value().uid) 1090 return Optional<DeprecatedString> {}; 1091 return client_stat.value().username; 1092} 1093 1094}