Window.cpp 42 KB

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