Window.cpp 42 KB

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