Window.cpp 39 KB

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