Window.cpp 41 KB

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