Window.cpp 38 KB

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