Window.cpp 43 KB

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