Window.cpp 38 KB

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