Window.cpp 37 KB

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