WindowManager.cpp 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "WindowManager.h"
  7. #include "Compositor.h"
  8. #include "EventLoop.h"
  9. #include "Menu.h"
  10. #include "Screen.h"
  11. #include "Window.h"
  12. #include <AK/Debug.h>
  13. #include <AK/StdLibExtras.h>
  14. #include <AK/Vector.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/CharacterBitmap.h>
  17. #include <LibGfx/Font/Font.h>
  18. #include <LibGfx/StylePainter.h>
  19. #include <LibGfx/SystemTheme.h>
  20. #include <Services/Taskbar/TaskbarWindow.h>
  21. #include <WindowServer/AppletManager.h>
  22. #include <WindowServer/Button.h>
  23. #include <WindowServer/ConnectionFromClient.h>
  24. #include <WindowServer/Cursor.h>
  25. #include <WindowServer/WindowClientEndpoint.h>
  26. namespace WindowServer {
  27. static WindowManager* s_the;
  28. WindowManager& WindowManager::the()
  29. {
  30. VERIFY(s_the);
  31. return *s_the;
  32. }
  33. WindowManager::WindowManager(Gfx::PaletteImpl const& palette)
  34. : m_switcher(WindowSwitcher::construct())
  35. , m_keymap_switcher(KeymapSwitcher::construct())
  36. , m_palette(palette)
  37. {
  38. s_the = this;
  39. {
  40. // If we haven't created any window stacks, at least create the stationary/main window stack
  41. auto row = adopt_own(*new RemoveReference<decltype(m_window_stacks[0])>());
  42. auto main_window_stack = adopt_own(*new WindowStack(0, 0));
  43. main_window_stack->set_stationary_window_stack(*main_window_stack);
  44. m_current_window_stack = main_window_stack.ptr();
  45. row->append(move(main_window_stack));
  46. m_window_stacks.append(move(row));
  47. }
  48. reload_config();
  49. m_keymap_switcher->on_keymap_change = [&](DeprecatedString const& keymap) {
  50. for_each_window_manager([&keymap](WMConnectionFromClient& conn) {
  51. if (!(conn.event_mask() & WMEventMask::KeymapChanged))
  52. return IterationDecision::Continue;
  53. if (conn.window_id() < 0)
  54. return IterationDecision::Continue;
  55. conn.async_keymap_changed(conn.window_id(), keymap);
  56. return IterationDecision::Continue;
  57. });
  58. };
  59. Compositor::the().did_construct_window_manager({});
  60. }
  61. void WindowManager::reload_config()
  62. {
  63. m_config = Core::ConfigFile::open("/etc/WindowServer.ini", Core::ConfigFile::AllowWriting::Yes).release_value_but_fixme_should_propagate_errors();
  64. unsigned workspace_rows = (unsigned)m_config->read_num_entry("Workspaces", "Rows", default_window_stack_rows);
  65. unsigned workspace_columns = (unsigned)m_config->read_num_entry("Workspaces", "Columns", default_window_stack_columns);
  66. if (workspace_rows == 0 || workspace_columns == 0 || workspace_rows > max_window_stack_rows || workspace_columns > max_window_stack_columns) {
  67. workspace_rows = default_window_stack_rows;
  68. workspace_columns = default_window_stack_columns;
  69. }
  70. apply_workspace_settings(workspace_rows, workspace_columns, false);
  71. m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  72. m_mouse_buttons_switched = m_config->read_bool_entry("Mouse", "ButtonsSwitched", false);
  73. m_natural_scroll = m_config->read_bool_entry("Mouse", "NaturalScroll", false);
  74. m_cursor_highlight_radius = m_config->read_num_entry("Mouse", "CursorHighlightRadius", 25);
  75. Color default_highlight_color = Color::NamedColor::Red;
  76. default_highlight_color.set_alpha(110);
  77. m_cursor_highlight_color = Color::from_string(m_config->read_entry("Mouse", "CursorHighlightColor")).value_or(default_highlight_color);
  78. apply_cursor_theme(m_config->read_entry("Mouse", "CursorTheme", "Default"));
  79. auto reload_graphic = [&](RefPtr<MultiScaleBitmaps>& bitmap, DeprecatedString const& name) {
  80. if (bitmap) {
  81. if (!bitmap->load(name))
  82. bitmap = nullptr;
  83. } else {
  84. bitmap = MultiScaleBitmaps::create(name);
  85. }
  86. };
  87. reload_graphic(m_overlay_rect_shadow, m_config->read_entry("Graphics", "OverlayRectShadow"));
  88. Compositor::the().invalidate_after_theme_or_font_change();
  89. WindowFrame::reload_config();
  90. load_system_effects();
  91. }
  92. Gfx::Font const& WindowManager::font() const
  93. {
  94. return Gfx::FontDatabase::default_font();
  95. }
  96. Gfx::Font const& WindowManager::window_title_font() const
  97. {
  98. return Gfx::FontDatabase::window_title_font();
  99. }
  100. bool WindowManager::set_screen_layout(ScreenLayout&& screen_layout, bool save, DeprecatedString& error_msg)
  101. {
  102. if (!Screen::apply_layout(move(screen_layout), error_msg))
  103. return false;
  104. reload_icon_bitmaps_after_scale_change();
  105. tell_wms_screen_rects_changed();
  106. for_each_window_stack([&](auto& window_stack) {
  107. window_stack.for_each_window([](Window& window) {
  108. window.screens().clear_with_capacity();
  109. window.recalculate_rect();
  110. return IterationDecision::Continue;
  111. });
  112. return IterationDecision::Continue;
  113. });
  114. Compositor::the().screen_resolution_changed();
  115. if (save)
  116. Screen::layout().save_config(*m_config);
  117. return true;
  118. }
  119. ScreenLayout WindowManager::get_screen_layout() const
  120. {
  121. return Screen::layout();
  122. }
  123. bool WindowManager::save_screen_layout(DeprecatedString& error_msg)
  124. {
  125. if (!Screen::layout().save_config(*m_config)) {
  126. error_msg = "Could not save";
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool WindowManager::apply_workspace_settings(unsigned rows, unsigned columns, bool save)
  132. {
  133. VERIFY(rows != 0);
  134. VERIFY(rows <= max_window_stack_rows);
  135. VERIFY(columns != 0);
  136. VERIFY(columns <= max_window_stack_columns);
  137. auto current_rows = window_stack_rows();
  138. auto current_columns = window_stack_columns();
  139. if (rows != current_rows || columns != current_columns) {
  140. auto& current_window_stack = this->current_window_stack();
  141. auto current_stack_row = current_window_stack.row();
  142. auto current_stack_column = current_window_stack.column();
  143. bool need_rerender = false;
  144. bool removing_current_stack = current_stack_row > rows - 1 || current_stack_column > columns - 1;
  145. auto new_current_row = min(current_stack_row, rows - 1);
  146. auto new_current_column = min(current_stack_column, columns - 1);
  147. // Collect all windows that were moved. We can't tell the wms at this point because
  148. // the current window stack may not be valid anymore, until after the move is complete
  149. Vector<Window*, 32> windows_moved;
  150. auto merge_window_stack = [&](WindowStack& from_stack, WindowStack& into_stack) {
  151. auto move_to = WindowStack::MoveAllWindowsTo::Back;
  152. // TODO: Figure out a better algorithm. We basically always layer it on top, unless
  153. // it's either being moved to window stack we're viewing or that we will be viewing.
  154. // In that case we would want to make sure the window stack ends up on top (with no
  155. // change to the active window)
  156. bool moving_to_new_current_stack = into_stack.row() == new_current_row && into_stack.column() == new_current_column;
  157. if (moving_to_new_current_stack)
  158. move_to = WindowStack::MoveAllWindowsTo::Front;
  159. from_stack.move_all_windows(into_stack, windows_moved, move_to);
  160. };
  161. // While we have too many rows, merge each row too many into the new bottom row
  162. while (current_rows > rows) {
  163. auto& row = m_window_stacks[rows];
  164. for (size_t column_index = 0; column_index < row.size(); column_index++) {
  165. merge_window_stack(row[column_index], m_window_stacks[rows - 1][column_index]);
  166. if (rows - 1 == current_stack_row && column_index == current_stack_column)
  167. need_rerender = true;
  168. }
  169. m_window_stacks.remove(rows);
  170. current_rows--;
  171. }
  172. // While we have too many columns, merge each column too many into the new right most column
  173. while (current_columns > columns) {
  174. for (size_t row_index = 0; row_index < current_rows; row_index++) {
  175. auto& row = m_window_stacks[row_index];
  176. merge_window_stack(row[columns], row[columns - 1]);
  177. if (row_index == current_stack_row && columns - 1 == current_stack_column)
  178. need_rerender = true;
  179. row.remove(columns);
  180. }
  181. current_columns--;
  182. }
  183. // Add more rows if necessary
  184. while (rows > current_rows) {
  185. auto row = adopt_own(*new RemoveReference<decltype(m_window_stacks[0])>());
  186. for (size_t column_index = 0; column_index < columns; column_index++) {
  187. auto window_stack = adopt_own(*new WindowStack(current_rows, column_index));
  188. window_stack->set_stationary_window_stack(m_window_stacks[0][0]);
  189. row->append(move(window_stack));
  190. }
  191. m_window_stacks.append(move(row));
  192. current_rows++;
  193. }
  194. // Add more columns if necessary
  195. while (columns > current_columns) {
  196. for (size_t row_index = 0; row_index < current_rows; row_index++) {
  197. auto& row = m_window_stacks[row_index];
  198. while (row.size() < columns) {
  199. auto window_stack = adopt_own(*new WindowStack(row_index, row.size()));
  200. window_stack->set_stationary_window_stack(m_window_stacks[0][0]);
  201. row.append(move(window_stack));
  202. }
  203. }
  204. current_columns++;
  205. }
  206. if (removing_current_stack) {
  207. // If we're on a window stack that was removed, we need to move...
  208. m_current_window_stack = &m_window_stacks[new_current_row][new_current_column];
  209. Compositor::the().set_current_window_stack_no_transition(*m_current_window_stack);
  210. need_rerender = false; // The compositor already called invalidate_for_window_stack_merge_or_change for us
  211. }
  212. for (auto* window_moved : windows_moved)
  213. WindowManager::the().tell_wms_window_state_changed(*window_moved);
  214. tell_wms_screen_rects_changed(); // updates the available workspaces
  215. if (current_stack_row != new_current_row || current_stack_column != new_current_column)
  216. tell_wms_current_window_stack_changed();
  217. if (need_rerender)
  218. Compositor::the().invalidate_for_window_stack_merge_or_change();
  219. }
  220. if (save) {
  221. m_config->write_num_entry("Workspaces", "Rows", window_stack_rows());
  222. m_config->write_num_entry("Workspaces", "Columns", window_stack_columns());
  223. return !m_config->sync().is_error();
  224. }
  225. return true;
  226. }
  227. void WindowManager::set_acceleration_factor(double factor)
  228. {
  229. ScreenInput::the().set_acceleration_factor(factor);
  230. dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->filename());
  231. m_config->write_entry("Mouse", "AccelerationFactor", DeprecatedString::formatted("{}", factor));
  232. sync_config_to_disk();
  233. }
  234. void WindowManager::set_scroll_step_size(unsigned step_size)
  235. {
  236. ScreenInput::the().set_scroll_step_size(step_size);
  237. dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->filename());
  238. m_config->write_entry("Mouse", "ScrollStepSize", DeprecatedString::number(step_size));
  239. sync_config_to_disk();
  240. }
  241. void WindowManager::set_double_click_speed(int speed)
  242. {
  243. VERIFY(speed >= double_click_speed_min && speed <= double_click_speed_max);
  244. m_double_click_speed = speed;
  245. dbgln("Saving double-click speed {} to config file at {}", speed, m_config->filename());
  246. m_config->write_entry("Input", "DoubleClickSpeed", DeprecatedString::number(speed));
  247. sync_config_to_disk();
  248. }
  249. int WindowManager::double_click_speed() const
  250. {
  251. return m_double_click_speed;
  252. }
  253. void WindowManager::set_mouse_buttons_switched(bool switched)
  254. {
  255. m_mouse_buttons_switched = switched;
  256. dbgln("Saving mouse buttons switched state {} to config file at {}", switched, m_config->filename());
  257. m_config->write_bool_entry("Mouse", "ButtonsSwitched", switched);
  258. sync_config_to_disk();
  259. }
  260. bool WindowManager::are_mouse_buttons_switched() const
  261. {
  262. return m_mouse_buttons_switched;
  263. }
  264. void WindowManager::set_natural_scroll(bool inverted)
  265. {
  266. m_natural_scroll = inverted;
  267. dbgln("Saving scroll inverted state {} to config file at {}", inverted, m_config->filename());
  268. m_config->write_bool_entry("Mouse", "NaturalScroll", inverted);
  269. sync_config_to_disk();
  270. }
  271. bool WindowManager::is_natural_scroll() const
  272. {
  273. return m_natural_scroll;
  274. }
  275. WindowStack& WindowManager::window_stack_for_window(Window& window)
  276. {
  277. if (is_stationary_window_type(window.type()))
  278. return m_window_stacks[0][0];
  279. if (auto* parent = window.parent_window(); parent && !is_stationary_window_type(parent->type()))
  280. return parent->window_stack();
  281. return current_window_stack();
  282. }
  283. void WindowManager::add_window(Window& window)
  284. {
  285. auto& window_stack = window_stack_for_window(window);
  286. bool is_first_window = window_stack.is_empty();
  287. window_stack.add(window);
  288. if (window.is_fullscreen()) {
  289. auto& screen = Screen::main(); // TODO: support fullscreen windows on other screens!
  290. Core::EventLoop::current().post_event(window, make<ResizeEvent>(screen.rect()));
  291. window.set_rect(screen.rect());
  292. }
  293. if (window.type() != WindowType::Desktop || is_first_window)
  294. set_active_window(&window);
  295. if (window.type() == WindowType::Popup)
  296. notify_active_window_input_preempted();
  297. if (m_switcher->is_visible() && window.type() != WindowType::WindowSwitcher)
  298. m_switcher->refresh();
  299. Compositor::the().invalidate_occlusions();
  300. window.invalidate(true, true);
  301. tell_wms_window_state_changed(window);
  302. }
  303. void WindowManager::move_to_front_and_make_active(Window& window)
  304. {
  305. for_each_window_in_modal_chain(window, [&](auto& w) {
  306. w.set_minimized(false);
  307. w.window_stack().move_to_front(w);
  308. return IterationDecision::Continue;
  309. });
  310. if (auto* blocker = window.blocking_modal_window()) {
  311. blocker->window_stack().move_to_front(*blocker);
  312. set_active_window(blocker);
  313. } else {
  314. window.window_stack().move_to_front(window);
  315. set_active_window(&window);
  316. for (auto& child : window.child_windows()) {
  317. if (!child || !child->is_modal())
  318. continue;
  319. if (child->is_rendering_above())
  320. child->window_stack().move_to_front(*child);
  321. }
  322. }
  323. if (m_switcher->is_visible()) {
  324. m_switcher->refresh();
  325. if (!window.is_modal()) {
  326. m_switcher->select_window(window);
  327. set_highlight_window(&window);
  328. }
  329. }
  330. Compositor::the().invalidate_occlusions();
  331. }
  332. void WindowManager::remove_window(Window& window)
  333. {
  334. check_hide_geometry_overlay(window);
  335. auto* active = active_window();
  336. bool was_modal = window.is_modal(); // This requires the window to be on a window stack still!
  337. window.window_stack().remove(window);
  338. if (active == &window)
  339. pick_new_active_window(&window);
  340. if (window.type() == WindowType::Popup)
  341. notify_active_window_input_restored();
  342. window.invalidate_last_rendered_screen_rects_now();
  343. if (m_switcher->is_visible() && window.type() != WindowType::WindowSwitcher)
  344. m_switcher->refresh();
  345. Compositor::the().invalidate_occlusions();
  346. for_each_window_manager([&](WMConnectionFromClient& conn) {
  347. if (conn.window_id() < 0 || !(conn.event_mask() & WMEventMask::WindowRemovals))
  348. return IterationDecision::Continue;
  349. if (!window.is_internal() && !was_modal)
  350. conn.async_window_removed(conn.window_id(), window.client_id(), window.window_id());
  351. return IterationDecision::Continue;
  352. });
  353. }
  354. void WindowManager::greet_window_manager(WMConnectionFromClient& conn)
  355. {
  356. if (conn.window_id() < 0)
  357. return;
  358. tell_wm_about_current_window_stack(conn);
  359. for_each_window_stack([&](auto& window_stack) {
  360. window_stack.for_each_window([&](Window& other_window) {
  361. tell_wm_about_window(conn, other_window);
  362. tell_wm_about_window_icon(conn, other_window);
  363. return IterationDecision::Continue;
  364. });
  365. return IterationDecision::Continue;
  366. });
  367. if (auto* applet_area_window = AppletManager::the().window())
  368. tell_wms_applet_area_size_changed(applet_area_window->size());
  369. }
  370. void WindowManager::tell_wm_about_window(WMConnectionFromClient& conn, Window& window)
  371. {
  372. if (conn.window_id() < 0)
  373. return;
  374. if (!(conn.event_mask() & WMEventMask::WindowStateChanges))
  375. return;
  376. if (window.is_internal())
  377. return;
  378. if (window.blocking_modal_window())
  379. return;
  380. Window* modeless = window.modeless_ancestor();
  381. if (!modeless)
  382. return;
  383. bool is_blocked = modeless->blocking_modal_window();
  384. auto is_active = for_each_window_in_modal_chain(*modeless, [&](auto& w) {
  385. if (w.is_active())
  386. return IterationDecision::Break;
  387. return IterationDecision::Continue;
  388. });
  389. auto& window_stack = is_stationary_window_type(modeless->type()) ? current_window_stack() : modeless->window_stack();
  390. conn.async_window_state_changed(conn.window_id(), modeless->client_id(), modeless->window_id(), window_stack.row(), window_stack.column(), is_active, is_blocked, modeless->is_minimized(), modeless->is_frameless(), (i32)modeless->type(), modeless->computed_title(), modeless->rect(), modeless->progress());
  391. }
  392. void WindowManager::tell_wm_about_window_rect(WMConnectionFromClient& conn, Window& window)
  393. {
  394. if (conn.window_id() < 0)
  395. return;
  396. if (!(conn.event_mask() & WMEventMask::WindowRectChanges))
  397. return;
  398. if (window.is_internal())
  399. return;
  400. conn.async_window_rect_changed(conn.window_id(), window.client_id(), window.window_id(), window.rect());
  401. }
  402. void WindowManager::tell_wm_about_window_icon(WMConnectionFromClient& conn, Window& window)
  403. {
  404. if (conn.window_id() < 0)
  405. return;
  406. if (!(conn.event_mask() & WMEventMask::WindowIconChanges))
  407. return;
  408. if (window.is_internal())
  409. return;
  410. conn.async_window_icon_bitmap_changed(conn.window_id(), window.client_id(), window.window_id(), window.icon().to_shareable_bitmap());
  411. }
  412. void WindowManager::tell_wm_about_current_window_stack(WMConnectionFromClient& conn)
  413. {
  414. if (conn.window_id() < 0)
  415. return;
  416. if (!(conn.event_mask() & WMEventMask::WorkspaceChanges))
  417. return;
  418. auto& window_stack = current_window_stack();
  419. conn.async_workspace_changed(conn.window_id(), window_stack.row(), window_stack.column());
  420. }
  421. void WindowManager::tell_wms_window_state_changed(Window& window)
  422. {
  423. for_each_window_manager([&](WMConnectionFromClient& conn) {
  424. tell_wm_about_window(conn, window);
  425. return IterationDecision::Continue;
  426. });
  427. }
  428. void WindowManager::tell_wms_window_icon_changed(Window& window)
  429. {
  430. for_each_window_manager([&](WMConnectionFromClient& conn) {
  431. tell_wm_about_window_icon(conn, window);
  432. return IterationDecision::Continue;
  433. });
  434. }
  435. void WindowManager::tell_wms_window_rect_changed(Window& window)
  436. {
  437. for_each_window_manager([&](WMConnectionFromClient& conn) {
  438. tell_wm_about_window_rect(conn, window);
  439. return IterationDecision::Continue;
  440. });
  441. }
  442. void WindowManager::tell_wms_screen_rects_changed()
  443. {
  444. ConnectionFromClient::for_each_client([&](ConnectionFromClient& client) {
  445. client.notify_about_new_screen_rects();
  446. });
  447. }
  448. void WindowManager::tell_wms_applet_area_size_changed(Gfx::IntSize size)
  449. {
  450. for_each_window_manager([&](WMConnectionFromClient& conn) {
  451. if (conn.window_id() < 0)
  452. return IterationDecision::Continue;
  453. conn.async_applet_area_size_changed(conn.window_id(), size);
  454. return IterationDecision::Continue;
  455. });
  456. }
  457. void WindowManager::tell_wms_super_key_pressed()
  458. {
  459. for_each_window_manager([](WMConnectionFromClient& conn) {
  460. if (conn.window_id() < 0)
  461. return IterationDecision::Continue;
  462. conn.async_super_key_pressed(conn.window_id());
  463. return IterationDecision::Continue;
  464. });
  465. }
  466. void WindowManager::tell_wms_super_space_key_pressed()
  467. {
  468. for_each_window_manager([](WMConnectionFromClient& conn) {
  469. if (conn.window_id() < 0)
  470. return IterationDecision::Continue;
  471. conn.async_super_space_key_pressed(conn.window_id());
  472. return IterationDecision::Continue;
  473. });
  474. }
  475. void WindowManager::tell_wms_super_d_key_pressed()
  476. {
  477. for_each_window_manager([](WMConnectionFromClient& conn) {
  478. if (conn.window_id() < 0)
  479. return IterationDecision::Continue;
  480. conn.async_super_d_key_pressed(conn.window_id());
  481. return IterationDecision::Continue;
  482. });
  483. }
  484. void WindowManager::tell_wms_super_digit_key_pressed(u8 digit)
  485. {
  486. for_each_window_manager([digit](WMConnectionFromClient& conn) {
  487. if (conn.window_id() < 0)
  488. return IterationDecision::Continue;
  489. conn.async_super_digit_key_pressed(conn.window_id(), digit);
  490. return IterationDecision::Continue;
  491. });
  492. }
  493. void WindowManager::tell_wms_current_window_stack_changed()
  494. {
  495. for_each_window_manager([&](WMConnectionFromClient& conn) {
  496. tell_wm_about_current_window_stack(conn);
  497. return IterationDecision::Continue;
  498. });
  499. }
  500. static bool window_type_has_title(WindowType type)
  501. {
  502. return type == WindowType::Normal;
  503. }
  504. void WindowManager::notify_modified_changed(Window& window)
  505. {
  506. if (m_switcher->is_visible())
  507. m_switcher->refresh();
  508. tell_wms_window_state_changed(window);
  509. }
  510. void WindowManager::notify_title_changed(Window& window)
  511. {
  512. if (!window_type_has_title(window.type()))
  513. return;
  514. dbgln_if(WINDOWMANAGER_DEBUG, "[WM] Window({}) title set to '{}'", &window, window.title());
  515. if (m_switcher->is_visible())
  516. m_switcher->refresh();
  517. tell_wms_window_state_changed(window);
  518. }
  519. void WindowManager::notify_rect_changed(Window& window, Gfx::IntRect const& old_rect, Gfx::IntRect const& new_rect)
  520. {
  521. dbgln_if(RESIZE_DEBUG, "[WM] Window({}) rect changed {} -> {}", &window, old_rect, new_rect);
  522. if (m_switcher->is_visible() && window.type() != WindowType::WindowSwitcher)
  523. m_switcher->refresh();
  524. tell_wms_window_rect_changed(window);
  525. if (window.type() == WindowType::Applet)
  526. AppletManager::the().relayout();
  527. reevaluate_hover_state_for_window(&window);
  528. }
  529. void WindowManager::notify_opacity_changed(Window&)
  530. {
  531. Compositor::the().invalidate_occlusions();
  532. }
  533. void WindowManager::notify_minimization_state_changed(Window& window)
  534. {
  535. tell_wms_window_state_changed(window);
  536. if (window.client())
  537. window.client()->async_window_state_changed(window.window_id(), window.is_minimized(), window.is_maximized(), window.is_occluded());
  538. if (window.is_active() && window.is_minimized())
  539. pick_new_active_window(&window);
  540. }
  541. void WindowManager::notify_occlusion_state_changed(Window& window)
  542. {
  543. if (window.client())
  544. window.client()->async_window_state_changed(window.window_id(), window.is_minimized(), window.is_maximized(), window.is_occluded());
  545. }
  546. void WindowManager::notify_progress_changed(Window& window)
  547. {
  548. tell_wms_window_state_changed(window);
  549. }
  550. void WindowManager::pick_new_active_window(Window* previous_active)
  551. {
  552. Window* desktop = nullptr;
  553. auto result = for_each_visible_window_from_front_to_back([&](Window& candidate) {
  554. if (candidate.type() == WindowType::Desktop)
  555. desktop = &candidate;
  556. if (candidate.type() != WindowType::Normal)
  557. return IterationDecision::Continue;
  558. if (candidate.is_destroyed())
  559. return IterationDecision::Continue;
  560. if (!previous_active || previous_active != &candidate) {
  561. set_active_window(&candidate);
  562. return IterationDecision::Break;
  563. }
  564. return IterationDecision::Continue;
  565. });
  566. if (result != IterationDecision::Break)
  567. set_active_window(desktop);
  568. }
  569. void WindowManager::check_hide_geometry_overlay(Window& window)
  570. {
  571. if (&window == m_move_window.ptr() || &window == m_resize_window.ptr())
  572. m_geometry_overlay = nullptr;
  573. }
  574. void WindowManager::start_window_move(Window& window, Gfx::IntPoint origin)
  575. {
  576. MenuManager::the().close_everyone();
  577. dbgln_if(MOVE_DEBUG, "[WM] Begin moving Window({})", &window);
  578. move_to_front_and_make_active(window);
  579. m_move_window = window;
  580. m_move_window->set_default_positioned(false);
  581. m_move_origin = origin;
  582. m_move_window_origin = window.position();
  583. m_move_window_cursor_position = window.is_tiled() ? to_floating_cursor_position(m_mouse_down_origin) : m_mouse_down_origin;
  584. if (system_effects().geometry() == ShowGeometry::OnMoveAndResize || system_effects().geometry() == ShowGeometry::OnMoveOnly) {
  585. m_geometry_overlay = Compositor::the().create_overlay<WindowGeometryOverlay>(window);
  586. m_geometry_overlay->set_enabled(true);
  587. }
  588. window.invalidate(true, true);
  589. }
  590. void WindowManager::start_window_move(Window& window, MouseEvent const& event)
  591. {
  592. start_window_move(window, event.position());
  593. }
  594. void WindowManager::start_window_resize(Window& window, Gfx::IntPoint position, MouseButton button, ResizeDirection resize_direction)
  595. {
  596. MenuManager::the().close_everyone();
  597. move_to_front_and_make_active(window);
  598. m_resize_direction = resize_direction;
  599. if (m_resize_direction == ResizeDirection::None) {
  600. VERIFY(!m_resize_window);
  601. return;
  602. }
  603. dbgln_if(RESIZE_DEBUG, "[WM] Begin resizing Window({})", &window);
  604. m_resizing_mouse_button = button;
  605. m_resize_window = window;
  606. m_resize_origin = position;
  607. m_resize_window_original_rect = window.rect();
  608. if (system_effects().geometry() == ShowGeometry::OnMoveAndResize || system_effects().geometry() == ShowGeometry::OnResizeOnly) {
  609. m_geometry_overlay = Compositor::the().create_overlay<WindowGeometryOverlay>(window);
  610. m_geometry_overlay->set_enabled(true);
  611. }
  612. set_automatic_cursor_tracking_window(nullptr);
  613. window.invalidate(true, true);
  614. }
  615. void WindowManager::start_window_resize(Window& window, MouseEvent const& event, ResizeDirection resize_direction)
  616. {
  617. start_window_resize(window, event.position(), event.button(), resize_direction);
  618. }
  619. bool WindowManager::process_ongoing_window_move(MouseEvent& event)
  620. {
  621. if (!m_move_window)
  622. return false;
  623. if (event.type() == Event::MouseUp && event.button() == MouseButton::Primary) {
  624. dbgln_if(MOVE_DEBUG, "[WM] Finish moving Window({})", m_move_window);
  625. if (!m_move_window->is_tiled())
  626. m_move_window->set_floating_rect(m_move_window->rect());
  627. Core::EventLoop::current().post_event(*m_move_window, make<MoveEvent>(m_move_window->rect()));
  628. m_move_window->invalidate(true, true);
  629. if (m_move_window->is_resizable()) {
  630. process_event_for_doubleclick(*m_move_window, event);
  631. if (event.type() == Event::MouseDoubleClick) {
  632. dbgln_if(DOUBLECLICK_DEBUG, "[WM] Click up became doubleclick!");
  633. m_move_window->set_maximized(!m_move_window->is_maximized());
  634. }
  635. }
  636. m_move_window = nullptr;
  637. m_geometry_overlay = nullptr;
  638. return true;
  639. }
  640. if (event.type() == Event::MouseMove) {
  641. if constexpr (MOVE_DEBUG) {
  642. dbgln("[WM] Moving, origin: {}, now: {}", m_move_origin, event.position());
  643. if (m_move_window->is_maximized())
  644. dbgln(" [!] The window is still maximized. Not moving yet.");
  645. }
  646. int const tiling_deadzone = 10;
  647. int const secondary_deadzone = 2;
  648. auto& cursor_screen = Screen::closest_to_location(event.position());
  649. auto desktop = desktop_rect(cursor_screen);
  650. auto desktop_relative_to_screen = desktop.translated(-cursor_screen.rect().location());
  651. if (m_move_window->is_maximized()) {
  652. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  653. if (pixels_moved_from_start > 5) {
  654. dbgln_if(MOVE_DEBUG, "[WM] de-maximizing window");
  655. m_move_origin = event.position();
  656. if (m_move_origin.y() <= secondary_deadzone + desktop.top())
  657. return true;
  658. Gfx::IntPoint adjusted_position = event.position().translated(-m_move_window_cursor_position);
  659. m_move_window->set_maximized(false);
  660. m_move_window->move_to(adjusted_position);
  661. m_move_window_origin = m_move_window->position();
  662. }
  663. } else {
  664. bool is_resizable = m_move_window->is_resizable();
  665. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  666. auto event_location_relative_to_screen = event.position().translated(-cursor_screen.rect().location());
  667. if (is_resizable && event_location_relative_to_screen.x() <= tiling_deadzone) {
  668. if (event_location_relative_to_screen.y() <= tiling_deadzone + desktop_relative_to_screen.top())
  669. m_move_window->set_tiled(WindowTileType::TopLeft);
  670. else if (event_location_relative_to_screen.y() >= desktop_relative_to_screen.height() - tiling_deadzone)
  671. m_move_window->set_tiled(WindowTileType::BottomLeft);
  672. else
  673. m_move_window->set_tiled(WindowTileType::Left);
  674. } else if (is_resizable && event_location_relative_to_screen.x() >= cursor_screen.width() - tiling_deadzone) {
  675. if (event_location_relative_to_screen.y() <= tiling_deadzone + desktop.top())
  676. m_move_window->set_tiled(WindowTileType::TopRight);
  677. else if (event_location_relative_to_screen.y() >= desktop_relative_to_screen.height() - tiling_deadzone)
  678. m_move_window->set_tiled(WindowTileType::BottomRight);
  679. else
  680. m_move_window->set_tiled(WindowTileType::Right);
  681. } else if (is_resizable && event_location_relative_to_screen.y() <= secondary_deadzone + desktop_relative_to_screen.top()) {
  682. m_move_window->set_tiled(WindowTileType::Top);
  683. } else if (is_resizable && event_location_relative_to_screen.y() >= desktop_relative_to_screen.bottom() - secondary_deadzone) {
  684. m_move_window->set_tiled(WindowTileType::Bottom);
  685. } else if (!m_move_window->is_tiled()) {
  686. Gfx::IntPoint pos = m_move_window_origin.translated(event.position() - m_move_origin);
  687. m_move_window->set_position_without_repaint(pos);
  688. } else if (pixels_moved_from_start > 5) {
  689. Gfx::IntPoint adjusted_position = event.position().translated(-m_move_window_cursor_position);
  690. m_move_window->set_untiled();
  691. m_move_window->move_to(adjusted_position);
  692. m_move_origin = event.position();
  693. m_move_window_origin = m_move_window->position();
  694. }
  695. }
  696. if (system_effects().geometry() == ShowGeometry::OnMoveAndResize || system_effects().geometry() == ShowGeometry::OnMoveOnly) {
  697. m_geometry_overlay->window_rect_changed();
  698. }
  699. }
  700. Core::EventLoop::current().post_event(*m_move_window, make<MoveEvent>(m_move_window->rect()));
  701. return true;
  702. }
  703. Gfx::IntPoint WindowManager::to_floating_cursor_position(Gfx::IntPoint origin) const
  704. {
  705. VERIFY(m_move_window);
  706. Gfx::IntPoint new_position;
  707. auto dist_from_right = m_move_window->rect().width() - origin.x();
  708. auto dist_from_bottom = m_move_window->rect().height() - origin.y();
  709. auto floating_width = m_move_window->floating_rect().width();
  710. auto floating_height = m_move_window->floating_rect().height();
  711. if (origin.x() < dist_from_right && origin.x() < floating_width / 2)
  712. new_position.set_x(origin.x());
  713. else if (dist_from_right < origin.x() && dist_from_right < floating_width / 2)
  714. new_position.set_x(floating_width - dist_from_right);
  715. else
  716. new_position.set_x(floating_width / 2);
  717. if (origin.y() < dist_from_bottom && origin.y() < floating_height / 2)
  718. new_position.set_y(origin.y());
  719. else if (dist_from_bottom < origin.y() && dist_from_bottom < floating_height / 2)
  720. new_position.set_y(floating_height - dist_from_bottom);
  721. else
  722. new_position.set_y(floating_height / 2);
  723. return new_position;
  724. }
  725. bool WindowManager::process_ongoing_window_resize(MouseEvent const& event)
  726. {
  727. if (!m_resize_window)
  728. return false;
  729. if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
  730. dbgln_if(RESIZE_DEBUG, "[WM] Finish resizing Window({})", m_resize_window);
  731. if (!m_resize_window->is_tiled())
  732. m_resize_window->set_floating_rect(m_resize_window->rect());
  733. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect()));
  734. m_resize_window->invalidate(true, true);
  735. m_resize_window = nullptr;
  736. m_geometry_overlay = nullptr;
  737. m_resizing_mouse_button = MouseButton::None;
  738. return true;
  739. }
  740. if (event.type() != Event::MouseMove)
  741. return true;
  742. auto& cursor_screen = ScreenInput::the().cursor_location_screen();
  743. auto& closest_screen = Screen::closest_to_rect(m_resize_window->rect());
  744. if (&cursor_screen == &closest_screen) {
  745. constexpr auto hot_zone = 10;
  746. Gfx::IntRect tiling_rect = desktop_rect(cursor_screen).shrunken({ hot_zone, hot_zone });
  747. if ((m_resize_direction == ResizeDirection::Up || m_resize_direction == ResizeDirection::Down)
  748. && (event.y() >= tiling_rect.bottom() || event.y() <= tiling_rect.top())) {
  749. m_resize_window->set_tiled(WindowTileType::VerticallyMaximized);
  750. return true;
  751. }
  752. if ((m_resize_direction == ResizeDirection::Left || m_resize_direction == ResizeDirection::Right)
  753. && (event.x() >= tiling_rect.right() || event.x() <= tiling_rect.left())) {
  754. m_resize_window->set_tiled(WindowTileType::HorizontallyMaximized);
  755. return true;
  756. }
  757. }
  758. int diff_x = event.x() - m_resize_origin.x();
  759. int diff_y = event.y() - m_resize_origin.y();
  760. int change_w = 0;
  761. int change_h = 0;
  762. switch (m_resize_direction) {
  763. case ResizeDirection::DownRight:
  764. change_w = diff_x;
  765. change_h = diff_y;
  766. break;
  767. case ResizeDirection::Right:
  768. change_w = diff_x;
  769. break;
  770. case ResizeDirection::UpRight:
  771. change_w = diff_x;
  772. change_h = -diff_y;
  773. break;
  774. case ResizeDirection::Up:
  775. change_h = -diff_y;
  776. break;
  777. case ResizeDirection::UpLeft:
  778. change_w = -diff_x;
  779. change_h = -diff_y;
  780. break;
  781. case ResizeDirection::Left:
  782. change_w = -diff_x;
  783. break;
  784. case ResizeDirection::DownLeft:
  785. change_w = -diff_x;
  786. change_h = diff_y;
  787. break;
  788. case ResizeDirection::Down:
  789. change_h = diff_y;
  790. break;
  791. default:
  792. VERIFY_NOT_REACHED();
  793. }
  794. auto new_rect = m_resize_window_original_rect;
  795. // First, size the new rect.
  796. new_rect.set_width(new_rect.width() + change_w);
  797. new_rect.set_height(new_rect.height() + change_h);
  798. m_resize_window->apply_minimum_size(new_rect);
  799. if (!m_resize_window->size_increment().is_null()) {
  800. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  801. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  802. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  803. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  804. }
  805. if (m_resize_window->resize_aspect_ratio().has_value()) {
  806. auto& ratio = m_resize_window->resize_aspect_ratio().value();
  807. auto base_size = m_resize_window->base_size();
  808. if (abs(change_w) > abs(change_h)) {
  809. new_rect.set_height(base_size.height() + (new_rect.width() - base_size.width()) * ratio.height() / ratio.width());
  810. } else {
  811. new_rect.set_width(base_size.width() + (new_rect.height() - base_size.height()) * ratio.width() / ratio.height());
  812. }
  813. }
  814. // Second, set its position so that the sides of the window
  815. // that end up moving are the same ones as the user is dragging,
  816. // no matter which part of the logic above caused us to decide
  817. // to resize by this much.
  818. switch (m_resize_direction) {
  819. case ResizeDirection::DownRight:
  820. case ResizeDirection::Right:
  821. case ResizeDirection::Down:
  822. break;
  823. case ResizeDirection::Left:
  824. case ResizeDirection::Up:
  825. case ResizeDirection::UpLeft:
  826. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  827. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  828. break;
  829. case ResizeDirection::UpRight:
  830. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  831. break;
  832. case ResizeDirection::DownLeft:
  833. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  834. break;
  835. default:
  836. VERIFY_NOT_REACHED();
  837. }
  838. if (m_resize_window->rect() == new_rect)
  839. return true;
  840. if (m_resize_window->is_tiled()) {
  841. // Check if we should be un-tiling the window. This should happen when one side touching
  842. // the screen border changes. We need to un-tile because while it is tiled, rendering is
  843. // constrained to the screen where it's tiled on, and if one of these sides move we should
  844. // no longer constrain rendering to that screen. Changing the sides not touching a screen
  845. // border however is fine as long as the screen contains the entire window.
  846. m_resize_window->check_untile_due_to_resize(new_rect);
  847. }
  848. dbgln_if(RESIZE_DEBUG, "[WM] Resizing, original: {}, now: {}", m_resize_window_original_rect, new_rect);
  849. if (m_resize_window->rect().location() != m_resize_window_original_rect.location()) {
  850. m_resize_window->set_default_positioned(false);
  851. }
  852. m_resize_window->set_rect(new_rect);
  853. if (system_effects().geometry() == ShowGeometry::OnMoveAndResize || system_effects().geometry() == ShowGeometry::OnResizeOnly) {
  854. m_geometry_overlay->window_rect_changed();
  855. }
  856. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(new_rect));
  857. return true;
  858. }
  859. bool WindowManager::process_ongoing_drag(MouseEvent& event)
  860. {
  861. if (!m_dnd_client)
  862. return false;
  863. if (event.type() == Event::MouseMove) {
  864. m_dnd_overlay->cursor_moved();
  865. // We didn't let go of the drag yet, see if we should send some drag move events..
  866. if (auto* window = hovered_window()) {
  867. event.set_drag(true);
  868. event.set_mime_data(*m_dnd_mime_data);
  869. deliver_mouse_event(*window, event, false);
  870. } else {
  871. set_accepts_drag(false);
  872. }
  873. }
  874. if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Primary))
  875. return true;
  876. if (auto* window = hovered_window()) {
  877. m_dnd_client->async_drag_accepted();
  878. if (window->client()) {
  879. auto translated_event = event.translated(-window->position());
  880. window->client()->async_drag_dropped(window->window_id(), translated_event.position(), m_dnd_text, m_dnd_mime_data->all_data());
  881. }
  882. } else {
  883. m_dnd_client->async_drag_cancelled();
  884. }
  885. end_dnd_drag();
  886. return true;
  887. }
  888. void WindowManager::set_cursor_tracking_button(Button* button)
  889. {
  890. m_cursor_tracking_button = button;
  891. }
  892. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) const -> ClickMetadata const&
  893. {
  894. switch (button) {
  895. case MouseButton::Primary:
  896. return m_primary;
  897. case MouseButton::Secondary:
  898. return m_secondary;
  899. case MouseButton::Middle:
  900. return m_middle;
  901. case MouseButton::Backward:
  902. return m_backward;
  903. case MouseButton::Forward:
  904. return m_forward;
  905. default:
  906. VERIFY_NOT_REACHED();
  907. }
  908. }
  909. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
  910. {
  911. switch (button) {
  912. case MouseButton::Primary:
  913. return m_primary;
  914. case MouseButton::Secondary:
  915. return m_secondary;
  916. case MouseButton::Middle:
  917. return m_middle;
  918. case MouseButton::Backward:
  919. return m_backward;
  920. case MouseButton::Forward:
  921. return m_forward;
  922. default:
  923. VERIFY_NOT_REACHED();
  924. }
  925. }
  926. bool WindowManager::is_considered_doubleclick(MouseEvent const& event, DoubleClickInfo::ClickMetadata const& metadata) const
  927. {
  928. int elapsed_since_last_click = metadata.clock.elapsed();
  929. if (elapsed_since_last_click < m_double_click_speed) {
  930. auto diff = event.position() - metadata.last_position;
  931. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  932. if (distance_travelled_squared <= (m_max_distance_for_double_click * m_max_distance_for_double_click))
  933. return true;
  934. }
  935. return false;
  936. }
  937. void WindowManager::system_menu_doubleclick(Window& window, MouseEvent const& event)
  938. {
  939. // This is a special case. Basically, we're trying to determine whether
  940. // double clicking on the window menu icon happened. In this case, the
  941. // WindowFrame only receives a MouseDown event, and since the window
  942. // menu pops up, it does not see the MouseUp event. But, if they subsequently
  943. // click there again, the menu is closed and we receive a MouseUp event.
  944. // So, in order to be able to detect a double click when a menu is being
  945. // opened by the MouseDown event, we need to consider the MouseDown event
  946. // as a potential double-click trigger
  947. VERIFY(event.type() == Event::MouseDown);
  948. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  949. if (&window != m_double_click_info.m_clicked_window) {
  950. // we either haven't clicked anywhere, or we haven't clicked on this
  951. // window. set the current click window, and reset the timers.
  952. dbgln_if(DOUBLECLICK_DEBUG, "Initial mousedown on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  953. m_double_click_info.m_clicked_window = window;
  954. m_double_click_info.reset();
  955. }
  956. metadata.last_position = event.position();
  957. metadata.clock.start();
  958. }
  959. bool WindowManager::is_menu_doubleclick(Window& window, MouseEvent const& event) const
  960. {
  961. VERIFY(event.type() == Event::MouseUp);
  962. if (&window != m_double_click_info.m_clicked_window)
  963. return false;
  964. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  965. if (!metadata.clock.is_valid())
  966. return false;
  967. return is_considered_doubleclick(event, metadata);
  968. }
  969. void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
  970. {
  971. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  972. VERIFY(event.type() == Event::MouseUp);
  973. if (&window != m_double_click_info.m_clicked_window) {
  974. // we either haven't clicked anywhere, or we haven't clicked on this
  975. // window. set the current click window, and reset the timers.
  976. dbgln_if(DOUBLECLICK_DEBUG, "Initial mouseup on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  977. m_double_click_info.m_clicked_window = window;
  978. m_double_click_info.reset();
  979. }
  980. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  981. if (!metadata.clock.is_valid() || !is_considered_doubleclick(event, metadata)) {
  982. // either the clock is invalid because we haven't clicked on this
  983. // button on this window yet, so there's nothing to do, or this
  984. // isn't considered to be a double click. either way, restart the
  985. // clock
  986. metadata.clock.start();
  987. } else {
  988. dbgln_if(DOUBLECLICK_DEBUG, "Transforming MouseUp to MouseDoubleClick ({} < {})!", metadata.clock.elapsed(), m_double_click_speed);
  989. event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y());
  990. // invalidate this now we've delivered a doubleclick, otherwise
  991. // tripleclick will deliver two doubleclick events (incorrectly).
  992. metadata.clock = {};
  993. }
  994. metadata.last_position = event.position();
  995. }
  996. void WindowManager::deliver_mouse_event(Window& window, MouseEvent const& event, bool process_double_click)
  997. {
  998. auto translated_event = event.translated(-window.position());
  999. window.dispatch_event(translated_event);
  1000. if (process_double_click && translated_event.type() == Event::MouseUp) {
  1001. process_event_for_doubleclick(window, translated_event);
  1002. if (translated_event.type() == Event::MouseDoubleClick)
  1003. window.dispatch_event(translated_event);
  1004. }
  1005. }
  1006. bool WindowManager::process_ongoing_active_input_mouse_event(MouseEvent const& event)
  1007. {
  1008. auto* input_tracking_window = automatic_cursor_tracking_window();
  1009. if (!input_tracking_window)
  1010. return false;
  1011. // At this point, we have delivered the start of an input sequence to a
  1012. // client application. We must keep delivering to that client
  1013. // application until the input sequence is done.
  1014. //
  1015. // This prevents e.g. moving on one window out of the bounds starting
  1016. // a move in that other unrelated window, and other silly shenanigans.
  1017. deliver_mouse_event(*input_tracking_window, event, true);
  1018. if (event.type() == Event::MouseUp && event.buttons() == 0)
  1019. set_automatic_cursor_tracking_window(nullptr);
  1020. return true;
  1021. }
  1022. bool WindowManager::process_mouse_event_for_titlebar_buttons(MouseEvent const& event)
  1023. {
  1024. if (m_cursor_tracking_button) {
  1025. m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  1026. return true;
  1027. }
  1028. // This is quite hackish, but it's how the Button hover effect is implemented.
  1029. if (m_hovered_button && event.type() == Event::MouseMove)
  1030. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  1031. return false;
  1032. }
  1033. void WindowManager::process_mouse_event_for_window(HitTestResult& result, MouseEvent const& event)
  1034. {
  1035. auto& window = *result.window;
  1036. auto* blocking_modal_window = window.blocking_modal_window();
  1037. if (event.type() == Event::MouseDown) {
  1038. m_mouse_down_origin = result.is_frame_hit
  1039. ? event.position().translated(-window.position())
  1040. : result.window_relative_position;
  1041. }
  1042. // First check if we should initiate a move or resize (Super+LMB or Super+RMB).
  1043. // In those cases, the event is swallowed by the window manager.
  1044. if (!blocking_modal_window && window.is_movable()) {
  1045. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Super && event.type() == Event::MouseDown && event.button() == MouseButton::Primary) {
  1046. start_window_move(window, event);
  1047. return;
  1048. }
  1049. if (window.is_resizable() && m_keyboard_modifiers == Mod_Super && event.type() == Event::MouseDown && event.button() == MouseButton::Secondary && !window.blocking_modal_window()) {
  1050. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  1051. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  1052. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  1053. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  1054. };
  1055. Gfx::IntRect outer_rect = window.frame().rect();
  1056. if (!outer_rect.contains(event.position())) {
  1057. // FIXME: This used to be an VERIFY but crashing WindowServer over this seems silly.
  1058. dbgln("FIXME: !outer_rect.contains(position): outer_rect={}, position={}", outer_rect, event.position());
  1059. }
  1060. int window_relative_x = event.position().x() - outer_rect.x();
  1061. int window_relative_y = event.position().y() - outer_rect.y();
  1062. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  1063. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  1064. ResizeDirection resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  1065. start_window_resize(window, event, resize_direction);
  1066. return;
  1067. }
  1068. }
  1069. if (event.type() == Event::MouseDown) {
  1070. if (window.type() == WindowType::Normal)
  1071. move_to_front_and_make_active(window);
  1072. else if (window.type() == WindowType::Desktop)
  1073. set_active_window(&window);
  1074. }
  1075. if (blocking_modal_window && window.type() != WindowType::Popup) {
  1076. if (event.type() == Event::Type::MouseDown) {
  1077. // We're clicking on something that's blocked by a modal window.
  1078. // Flash the modal window to let the user know about it.
  1079. blocking_modal_window->frame().start_flash_animation();
  1080. }
  1081. // Don't send mouse events to windows blocked by a modal child.
  1082. return;
  1083. }
  1084. if (result.is_frame_hit) {
  1085. // We are hitting the frame, pass the event along to WindowFrame.
  1086. window.frame().handle_mouse_event(event.translated(-window.frame().rect().location()));
  1087. return;
  1088. }
  1089. if (!window.is_automatic_cursor_tracking()) {
  1090. deliver_mouse_event(window, event, true);
  1091. }
  1092. if (event.type() == Event::MouseDown)
  1093. set_automatic_cursor_tracking_window(&window);
  1094. }
  1095. void WindowManager::process_mouse_event(MouseEvent& event)
  1096. {
  1097. // 0. Forget the resize candidate (window that we could initiate a resize of from the current cursor position.)
  1098. // A new resize candidate may be determined if we hit an appropriate part of a window.
  1099. clear_resize_candidate();
  1100. // 1. Process ongoing drag events. This is done first to avoid clashing with global cursor tracking.
  1101. if (process_ongoing_drag(event))
  1102. return;
  1103. // 2. Send the mouse event to all clients with global cursor tracking enabled.
  1104. ConnectionFromClient::for_each_client([&](ConnectionFromClient& conn) {
  1105. if (conn.does_global_mouse_tracking()) {
  1106. conn.async_track_mouse_move(event.position());
  1107. }
  1108. });
  1109. // The automatic cursor tracking window is excluded here because we're sending the event to it
  1110. // in the next step.
  1111. for_each_visible_window_from_front_to_back([&](Window& window) {
  1112. if (window.is_automatic_cursor_tracking() && &window != automatic_cursor_tracking_window())
  1113. deliver_mouse_event(window, event, false);
  1114. return IterationDecision::Continue;
  1115. });
  1116. // 3. If there's an automatic cursor tracking window, all mouse events go there.
  1117. // Tracking ends after all mouse buttons have been released.
  1118. if (process_ongoing_active_input_mouse_event(event))
  1119. return;
  1120. // 4. If there's a window being moved around, take care of that.
  1121. if (process_ongoing_window_move(event))
  1122. return;
  1123. // 5. If there's a window being resized, take care of that.
  1124. if (process_ongoing_window_resize(event))
  1125. return;
  1126. // 6. If the event is inside a titlebar button, WindowServer implements all
  1127. // the behavior for those buttons internally.
  1128. if (process_mouse_event_for_titlebar_buttons(event))
  1129. return;
  1130. // 7. If there are menus open, deal with them now. (FIXME: This needs to be cleaned up & simplified!)
  1131. bool hitting_menu_in_window_with_active_menu = [&] {
  1132. if (!m_window_with_active_menu)
  1133. return false;
  1134. auto& frame = m_window_with_active_menu->frame();
  1135. return frame.menubar_rect().contains(event.position().translated(-frame.rect().location()));
  1136. }();
  1137. // FIXME: This is quite hackish, we clear the hovered menu before potentially setting the same menu
  1138. // as hovered again. This makes sure that the hovered state doesn't linger after moving the
  1139. // cursor away from a hovered menu.
  1140. MenuManager::the().set_hovered_menu(nullptr);
  1141. if (MenuManager::the().has_open_menu()
  1142. || hitting_menu_in_window_with_active_menu) {
  1143. if (!hitting_menu_in_window_with_active_menu) {
  1144. MenuManager::the().dispatch_event(event);
  1145. return;
  1146. }
  1147. }
  1148. // 8. Hit test the window stack to see what's under the cursor.
  1149. auto result = current_window_stack().hit_test(event.position());
  1150. if (!result.has_value())
  1151. return;
  1152. auto* event_window = result.value().window.ptr();
  1153. if (auto* popup_window = foremost_popup_window()) {
  1154. if (event_window == popup_window)
  1155. process_mouse_event_for_window(result.value(), event);
  1156. else if (event.buttons())
  1157. popup_window->request_close();
  1158. return;
  1159. }
  1160. process_mouse_event_for_window(result.value(), event);
  1161. }
  1162. void WindowManager::reevaluate_hover_state_for_window(Window* updated_window)
  1163. {
  1164. if (m_dnd_client || m_resize_window || m_move_window || m_cursor_tracking_button || MenuManager::the().has_open_menu())
  1165. return;
  1166. auto cursor_location = ScreenInput::the().cursor_location();
  1167. auto* currently_hovered = hovered_window();
  1168. if (updated_window) {
  1169. if (!(updated_window == currently_hovered || updated_window->frame().rect().contains(cursor_location) || (currently_hovered && currently_hovered->frame().rect().contains(cursor_location))))
  1170. return;
  1171. }
  1172. Window* hovered_window = nullptr;
  1173. if (auto* fullscreen_window = active_fullscreen_window()) {
  1174. if (fullscreen_window->hit_test(cursor_location).has_value())
  1175. hovered_window = fullscreen_window;
  1176. } else {
  1177. hovered_window = current_window_stack().window_at(cursor_location);
  1178. }
  1179. if (set_hovered_window(hovered_window)) {
  1180. if (currently_hovered && m_resize_candidate == currently_hovered)
  1181. clear_resize_candidate();
  1182. if (hovered_window) {
  1183. // Send a fake MouseMove event. This allows the new hovering window
  1184. // to determine which widget we're hovering, and also update the cursor
  1185. // accordingly. We do this because this re-evaluation of the currently
  1186. // hovered window wasn't triggered by a mouse move event, but rather
  1187. // e.g. a hit-test result change due to a transparent window repaint.
  1188. if (hovered_window->hit_test(cursor_location, false).has_value()) {
  1189. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->rect().location()), 0, MouseButton::None, 0);
  1190. hovered_window->dispatch_event(event);
  1191. } else if (!hovered_window->is_frameless()) {
  1192. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->frame().rect().location()), 0, MouseButton::None, 0);
  1193. hovered_window->frame().handle_mouse_event(event);
  1194. }
  1195. }
  1196. }
  1197. }
  1198. void WindowManager::clear_resize_candidate()
  1199. {
  1200. if (m_resize_candidate)
  1201. Compositor::the().invalidate_cursor();
  1202. m_resize_candidate = nullptr;
  1203. }
  1204. Gfx::IntRect WindowManager::desktop_rect(Screen& screen) const
  1205. {
  1206. if (active_fullscreen_window())
  1207. return Screen::main().rect(); // TODO: we should support fullscreen windows on any screen
  1208. auto screen_rect = screen.rect();
  1209. if (screen.is_main_screen())
  1210. screen_rect.set_height(screen.height() - TaskbarWindow::taskbar_height());
  1211. return screen_rect;
  1212. }
  1213. Gfx::IntRect WindowManager::arena_rect_for_type(Screen& screen, WindowType type) const
  1214. {
  1215. switch (type) {
  1216. case WindowType::Desktop:
  1217. return Screen::bounding_rect();
  1218. case WindowType::Normal:
  1219. return desktop_rect(screen);
  1220. case WindowType::Menu:
  1221. case WindowType::WindowSwitcher:
  1222. case WindowType::Taskbar:
  1223. case WindowType::Tooltip:
  1224. case WindowType::Applet:
  1225. case WindowType::Notification:
  1226. case WindowType::Popup:
  1227. case WindowType::Autocomplete:
  1228. return screen.rect();
  1229. default:
  1230. VERIFY_NOT_REACHED();
  1231. }
  1232. }
  1233. void WindowManager::event(Core::Event& event)
  1234. {
  1235. if (static_cast<Event&>(event).is_mouse_event()) {
  1236. auto& mouse_event = static_cast<MouseEvent&>(event);
  1237. if (mouse_event.type() != Event::MouseMove)
  1238. m_previous_event_was_super_keydown = false;
  1239. process_mouse_event(mouse_event);
  1240. m_last_processed_buttons = mouse_event.buttons();
  1241. auto include_window_frame = m_dnd_client ? WindowStack::IncludeWindowFrame::Yes : WindowStack::IncludeWindowFrame::No;
  1242. // TODO: handle transitioning between two stacks
  1243. set_hovered_window(current_window_stack().window_at(mouse_event.position(), include_window_frame));
  1244. return;
  1245. }
  1246. if (static_cast<Event&>(event).is_key_event()) {
  1247. process_key_event(static_cast<KeyEvent&>(event));
  1248. return;
  1249. }
  1250. Core::Object::event(event);
  1251. }
  1252. bool WindowManager::is_window_in_modal_chain(Window& chain_window, Window& other_window)
  1253. {
  1254. auto result = for_each_window_in_modal_chain(chain_window, [&](auto& window) {
  1255. if (&other_window == &window)
  1256. return IterationDecision::Break;
  1257. return IterationDecision::Continue;
  1258. });
  1259. return result;
  1260. }
  1261. void WindowManager::switch_to_window_stack(WindowStack& window_stack, Window* carry_window, bool show_overlay)
  1262. {
  1263. m_carry_window_to_new_stack.clear();
  1264. m_switching_to_window_stack = &window_stack;
  1265. if (carry_window && !is_stationary_window_type(carry_window->type()) && &carry_window->window_stack() != &window_stack) {
  1266. auto& from_stack = carry_window->window_stack();
  1267. for_each_visible_window_from_back_to_front([&](Window& window) {
  1268. if (is_stationary_window_type(window.type()))
  1269. return IterationDecision::Continue;
  1270. if (&window.window_stack() != &carry_window->window_stack())
  1271. return IterationDecision::Continue;
  1272. if (&window == carry_window || is_window_in_modal_chain(*carry_window, window))
  1273. m_carry_window_to_new_stack.append(window);
  1274. return IterationDecision::Continue;
  1275. },
  1276. &from_stack);
  1277. auto* from_active_window = from_stack.active_window();
  1278. bool did_carry_active_window = false;
  1279. for (auto& window : m_carry_window_to_new_stack) {
  1280. if (window == from_active_window)
  1281. did_carry_active_window = true;
  1282. window->set_moving_to_another_stack(true);
  1283. VERIFY(&window->window_stack() == &from_stack);
  1284. from_stack.remove(*window);
  1285. window_stack.add(*window);
  1286. }
  1287. // Before we change to the new stack, find a new active window on the stack we're switching from
  1288. if (did_carry_active_window)
  1289. pick_new_active_window(from_active_window);
  1290. // Now switch to the new stack
  1291. m_current_window_stack = &window_stack;
  1292. if (did_carry_active_window && from_active_window)
  1293. set_active_window(from_active_window);
  1294. // Because we moved windows between stacks we need to invalidate occlusions
  1295. Compositor::the().invalidate_occlusions();
  1296. } else {
  1297. m_current_window_stack = &window_stack;
  1298. }
  1299. Compositor::the().switch_to_window_stack(window_stack, show_overlay);
  1300. }
  1301. void WindowManager::did_switch_window_stack(Badge<Compositor>, WindowStack& previous_stack, WindowStack& new_stack)
  1302. {
  1303. VERIFY(&previous_stack != &new_stack);
  1304. // We are being notified by the compositor, it should not be switching right now!
  1305. VERIFY(!Compositor::the().is_switching_window_stacks());
  1306. if (m_switching_to_window_stack == &new_stack) {
  1307. m_switching_to_window_stack = nullptr;
  1308. if (!m_carry_window_to_new_stack.is_empty()) {
  1309. // switched_to_stack may be different from the stack where the windows were
  1310. // carried to when the user rapidly tries to switch stacks, so make sure to
  1311. // only reset the moving flag if we arrived at our final destination
  1312. for (auto& window_ref : m_carry_window_to_new_stack) {
  1313. if (auto* window = window_ref.ptr()) {
  1314. window->set_moving_to_another_stack(false);
  1315. tell_wms_window_state_changed(*window);
  1316. }
  1317. }
  1318. m_carry_window_to_new_stack.clear();
  1319. }
  1320. }
  1321. auto* previous_stack_active_window = previous_stack.active_window();
  1322. auto* new_stack_active_window = new_stack.active_window();
  1323. if (previous_stack_active_window != new_stack_active_window) {
  1324. if (previous_stack_active_window && is_stationary_window_type(previous_stack_active_window->type()))
  1325. notify_previous_active_window(*previous_stack_active_window);
  1326. if (new_stack_active_window && is_stationary_window_type(new_stack_active_window->type()))
  1327. notify_new_active_window(*new_stack_active_window);
  1328. }
  1329. if (!new_stack_active_window)
  1330. pick_new_active_window(nullptr);
  1331. reevaluate_hover_state_for_window();
  1332. tell_wms_current_window_stack_changed();
  1333. }
  1334. void WindowManager::process_key_event(KeyEvent& event)
  1335. {
  1336. m_keyboard_modifiers = event.modifiers();
  1337. // Escape key cancels an ongoing drag.
  1338. if (event.type() == Event::KeyDown && event.key() == Key_Escape && m_dnd_client) {
  1339. // Notify the drag-n-drop client that the drag was cancelled.
  1340. m_dnd_client->async_drag_cancelled();
  1341. // Also notify the currently hovered window (if any) that the ongoing drag was cancelled.
  1342. if (m_hovered_window && m_hovered_window->client() && m_hovered_window->client() != m_dnd_client)
  1343. m_hovered_window->client()->async_drag_cancelled();
  1344. end_dnd_drag();
  1345. return;
  1346. }
  1347. if (event.type() == Event::KeyDown && (event.modifiers() == (Mod_Ctrl | Mod_Super | Mod_Shift) && event.key() == Key_I)) {
  1348. reload_icon_bitmaps_after_scale_change();
  1349. Compositor::the().invalidate_screen();
  1350. return;
  1351. }
  1352. if (event.type() == Event::KeyDown && event.key() == Key_Super) {
  1353. m_previous_event_was_super_keydown = true;
  1354. } else if (m_previous_event_was_super_keydown) {
  1355. m_previous_event_was_super_keydown = false;
  1356. if (!m_dnd_client && !automatic_cursor_tracking_window() && event.type() == Event::KeyUp && event.key() == Key_Super) {
  1357. tell_wms_super_key_pressed();
  1358. return;
  1359. }
  1360. if (event.type() == Event::KeyDown && event.key() == Key_Space) {
  1361. tell_wms_super_space_key_pressed();
  1362. return;
  1363. }
  1364. if (event.type() == Event::KeyDown && event.key() == Key_D) {
  1365. tell_wms_super_d_key_pressed();
  1366. return;
  1367. }
  1368. if (event.type() == Event::KeyDown && event.key() >= Key_0 && event.key() <= Key_9) {
  1369. auto digit = event.key() - Key_0;
  1370. tell_wms_super_digit_key_pressed(digit);
  1371. return;
  1372. }
  1373. }
  1374. if (MenuManager::the().current_menu() && event.key() != Key_Super) {
  1375. MenuManager::the().dispatch_event(event);
  1376. return;
  1377. }
  1378. if (event.type() == Event::KeyDown) {
  1379. if ((event.modifiers() == Mod_Super && event.key() == Key_Tab) || (event.modifiers() == (Mod_Super | Mod_Shift) && event.key() == Key_Tab))
  1380. m_switcher->show(WindowSwitcher::Mode::ShowAllWindows);
  1381. else if ((event.modifiers() == Mod_Alt && event.key() == Key_Tab) || (event.modifiers() == (Mod_Alt | Mod_Shift) && event.key() == Key_Tab))
  1382. m_switcher->show(WindowSwitcher::Mode::ShowCurrentDesktop);
  1383. }
  1384. if (m_switcher->is_visible()) {
  1385. request_close_fragile_windows();
  1386. m_switcher->on_key_event(event);
  1387. return;
  1388. }
  1389. if (event.type() == Event::KeyDown && (event.modifiers() == (Mod_Alt | Mod_Shift) && (event.key() == Key_Shift || event.key() == Key_Alt))) {
  1390. m_keymap_switcher->next_keymap();
  1391. return;
  1392. }
  1393. if (event.type() == Event::KeyDown && (event.modifiers() == (Mod_Ctrl | Mod_Alt) || event.modifiers() == (Mod_Ctrl | Mod_Shift | Mod_Alt)) && (window_stack_columns() > 1 || window_stack_rows() > 1)) {
  1394. auto& current_stack = current_window_stack();
  1395. auto row = current_stack.row();
  1396. auto column = current_stack.column();
  1397. auto handle_window_stack_switch_key = [&]() {
  1398. switch (event.key()) {
  1399. case Key_Left:
  1400. if (column == 0)
  1401. return true;
  1402. column--;
  1403. return true;
  1404. case Key_Right:
  1405. if (column + 1 >= m_window_stacks[0].size())
  1406. return true;
  1407. column++;
  1408. return true;
  1409. case Key_Up:
  1410. if (row == 0)
  1411. return true;
  1412. row--;
  1413. return true;
  1414. case Key_Down:
  1415. if (row + 1 >= m_window_stacks.size())
  1416. return true;
  1417. row++;
  1418. return true;
  1419. default:
  1420. return false;
  1421. }
  1422. };
  1423. if (handle_window_stack_switch_key()) {
  1424. request_close_fragile_windows();
  1425. Window* carry_window = nullptr;
  1426. auto& new_window_stack = m_window_stacks[row][column];
  1427. if (&new_window_stack != &current_stack) {
  1428. if (event.modifiers() == (Mod_Ctrl | Mod_Shift | Mod_Alt))
  1429. carry_window = this->active_window();
  1430. }
  1431. // Call switch_to_window_stack even if we're not going to switch to another stack.
  1432. // We'll show the window stack switch overlay briefly!
  1433. switch_to_window_stack(new_window_stack, carry_window);
  1434. return;
  1435. }
  1436. }
  1437. auto* event_window = current_window_stack().active_window();
  1438. if (auto* popup_window = foremost_popup_window())
  1439. event_window = popup_window;
  1440. if (!event_window)
  1441. return;
  1442. if (event.type() == Event::KeyDown && event.modifiers() == Mod_Super) {
  1443. if (event.key() == Key_H) {
  1444. m_cursor_highlight_enabled = !m_cursor_highlight_enabled;
  1445. Compositor::the().invalidate_cursor();
  1446. return;
  1447. }
  1448. if (event_window->type() != WindowType::Desktop) {
  1449. if (event.key() == Key_Down) {
  1450. if (event_window->is_resizable() && event_window->is_maximized()) {
  1451. maximize_windows(*event_window, false);
  1452. return;
  1453. }
  1454. if (event_window->is_minimizable() && !event_window->is_modal())
  1455. minimize_windows(*event_window, true);
  1456. return;
  1457. }
  1458. if (event_window->is_resizable()) {
  1459. if (event.key() == Key_Up) {
  1460. maximize_windows(*event_window, !event_window->is_maximized());
  1461. return;
  1462. }
  1463. if (event.key() == Key_Left) {
  1464. if (event_window->tile_type() == WindowTileType::Left) {
  1465. event_window->set_untiled();
  1466. return;
  1467. }
  1468. if (event_window->is_maximized())
  1469. maximize_windows(*event_window, false);
  1470. event_window->set_tiled(WindowTileType::Left);
  1471. return;
  1472. }
  1473. if (event.key() == Key_Right) {
  1474. if (event_window->tile_type() == WindowTileType::Right) {
  1475. event_window->set_untiled();
  1476. return;
  1477. }
  1478. if (event_window->is_maximized())
  1479. maximize_windows(*event_window, false);
  1480. event_window->set_tiled(WindowTileType::Right);
  1481. return;
  1482. }
  1483. }
  1484. }
  1485. }
  1486. if (event.type() == Event::KeyDown && event.modifiers() == (Mod_Super | Mod_Alt) && event_window->type() != WindowType::Desktop) {
  1487. if (event_window->is_resizable()) {
  1488. if (event.key() == Key_Right || event.key() == Key_Left) {
  1489. if (event_window->tile_type() == WindowTileType::HorizontallyMaximized) {
  1490. event_window->set_untiled();
  1491. return;
  1492. }
  1493. if (event_window->is_maximized())
  1494. maximize_windows(*event_window, false);
  1495. event_window->set_tiled(WindowTileType::HorizontallyMaximized);
  1496. return;
  1497. }
  1498. if (event.key() == Key_Up || event.key() == Key_Down) {
  1499. if (event_window->tile_type() == WindowTileType::VerticallyMaximized) {
  1500. event_window->set_untiled();
  1501. return;
  1502. }
  1503. if (event_window->is_maximized())
  1504. maximize_windows(*event_window, false);
  1505. event_window->set_tiled(WindowTileType::VerticallyMaximized);
  1506. return;
  1507. }
  1508. }
  1509. }
  1510. event_window->dispatch_event(event);
  1511. }
  1512. void WindowManager::set_highlight_window(Window* new_highlight_window)
  1513. {
  1514. // NOTE: The highlight window is global across all stacks. That's because we
  1515. // can only have one and we want to be able to highlight it during transitions
  1516. auto* previous_highlight_window = highlight_window();
  1517. if (new_highlight_window == previous_highlight_window)
  1518. return;
  1519. if (!new_highlight_window)
  1520. m_highlight_window = nullptr;
  1521. else
  1522. m_highlight_window = new_highlight_window->make_weak_ptr<Window>();
  1523. if (previous_highlight_window) {
  1524. reevaluate_hover_state_for_window(previous_highlight_window);
  1525. previous_highlight_window->invalidate(true, true);
  1526. Compositor::the().invalidate_screen(previous_highlight_window->frame().render_rect());
  1527. }
  1528. if (new_highlight_window) {
  1529. reevaluate_hover_state_for_window(new_highlight_window);
  1530. new_highlight_window->invalidate(true, true);
  1531. Compositor::the().invalidate_screen(new_highlight_window->frame().render_rect());
  1532. }
  1533. // Invalidate occlusions in case the state change changes geometry
  1534. Compositor::the().invalidate_occlusions();
  1535. }
  1536. static bool window_type_can_become_active(WindowType type)
  1537. {
  1538. return type == WindowType::Normal || type == WindowType::Desktop;
  1539. }
  1540. void WindowManager::set_active_window(Window* new_active_window)
  1541. {
  1542. if (new_active_window) {
  1543. if (auto* blocker = new_active_window->blocking_modal_window()) {
  1544. VERIFY(blocker->is_modal());
  1545. VERIFY(blocker != new_active_window);
  1546. new_active_window = blocker;
  1547. }
  1548. if (!window_type_can_become_active(new_active_window->type()))
  1549. return;
  1550. }
  1551. auto& window_stack = current_window_stack();
  1552. if (new_active_window == window_stack.active_window())
  1553. return;
  1554. if (auto* previously_active_window = window_stack.active_window()) {
  1555. window_stack.set_active_window(nullptr);
  1556. set_automatic_cursor_tracking_window(nullptr);
  1557. notify_previous_active_window(*previously_active_window);
  1558. }
  1559. if (new_active_window) {
  1560. request_close_fragile_windows();
  1561. window_stack.set_active_window(new_active_window);
  1562. notify_new_active_window(*new_active_window);
  1563. reevaluate_hover_state_for_window(new_active_window);
  1564. }
  1565. // Window shapes may have changed (e.g. shadows for inactive/active windows)
  1566. Compositor::the().invalidate_occlusions();
  1567. }
  1568. void WindowManager::notify_new_active_window(Window& new_active_window)
  1569. {
  1570. Core::EventLoop::current().post_event(new_active_window, make<Event>(Event::WindowActivated));
  1571. new_active_window.invalidate(true, true);
  1572. tell_wms_window_state_changed(new_active_window);
  1573. }
  1574. void WindowManager::notify_previous_active_window(Window& previously_active_window)
  1575. {
  1576. for (auto& child_window : previously_active_window.child_windows()) {
  1577. if (child_window && child_window->type() == WindowType::Tooltip)
  1578. child_window->request_close();
  1579. }
  1580. Core::EventLoop::current().post_event(previously_active_window, make<Event>(Event::WindowDeactivated));
  1581. previously_active_window.invalidate(true, true);
  1582. tell_wms_window_state_changed(previously_active_window);
  1583. }
  1584. void WindowManager::notify_active_window_input_preempted()
  1585. {
  1586. if (active_window())
  1587. Core::EventLoop::current().post_event(*active_window(), make<Event>(Event::WindowInputPreempted));
  1588. }
  1589. void WindowManager::notify_active_window_input_restored()
  1590. {
  1591. if (active_window())
  1592. Core::EventLoop::current().post_event(*active_window(), make<Event>(Event::WindowInputRestored));
  1593. }
  1594. bool WindowManager::set_hovered_window(Window* window)
  1595. {
  1596. if (m_hovered_window == window)
  1597. return false;
  1598. if (m_hovered_window)
  1599. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
  1600. m_hovered_window = window;
  1601. if (m_hovered_window)
  1602. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
  1603. return true;
  1604. }
  1605. ConnectionFromClient const* WindowManager::active_client() const
  1606. {
  1607. if (auto* window = const_cast<WindowManager*>(this)->current_window_stack().active_window())
  1608. return window->client();
  1609. return nullptr;
  1610. }
  1611. Cursor const& WindowManager::active_cursor() const
  1612. {
  1613. if (m_dnd_client) {
  1614. if (m_dnd_accepts_drag)
  1615. return *m_drag_copy_cursor;
  1616. return *m_drag_cursor;
  1617. }
  1618. if (m_move_window)
  1619. return *m_move_cursor;
  1620. if (m_resize_window || m_resize_candidate) {
  1621. switch (m_resize_direction) {
  1622. case ResizeDirection::Up:
  1623. case ResizeDirection::Down:
  1624. return *m_resize_vertically_cursor;
  1625. case ResizeDirection::Left:
  1626. case ResizeDirection::Right:
  1627. return *m_resize_horizontally_cursor;
  1628. case ResizeDirection::UpLeft:
  1629. case ResizeDirection::DownRight:
  1630. return *m_resize_diagonally_tlbr_cursor;
  1631. case ResizeDirection::UpRight:
  1632. case ResizeDirection::DownLeft:
  1633. return *m_resize_diagonally_bltr_cursor;
  1634. case ResizeDirection::None:
  1635. break;
  1636. default:
  1637. VERIFY_NOT_REACHED();
  1638. break;
  1639. }
  1640. }
  1641. if (m_automatic_cursor_tracking_window) {
  1642. if (m_automatic_cursor_tracking_window->cursor())
  1643. return *m_automatic_cursor_tracking_window->cursor();
  1644. } else if (m_hovered_window) {
  1645. if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
  1646. if (modal_window->cursor())
  1647. return *modal_window->cursor();
  1648. } else if (m_hovered_window->cursor()) {
  1649. return *m_hovered_window->cursor();
  1650. }
  1651. }
  1652. return *m_arrow_cursor;
  1653. }
  1654. void WindowManager::set_hovered_button(Button* button)
  1655. {
  1656. m_hovered_button = button;
  1657. }
  1658. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1659. {
  1660. m_resize_candidate = window;
  1661. m_resize_direction = direction;
  1662. }
  1663. ResizeDirection WindowManager::resize_direction_of_window(Window const& window)
  1664. {
  1665. if (&window != m_resize_window)
  1666. return ResizeDirection::None;
  1667. return m_resize_direction;
  1668. }
  1669. Gfx::IntRect WindowManager::tiled_window_rect(Window const& window, WindowTileType tile_type, bool relative_to_window_screen) const
  1670. {
  1671. VERIFY(tile_type != WindowTileType::None);
  1672. auto& screen = Screen::closest_to_rect(window.frame().rect());
  1673. Gfx::IntRect rect = screen.rect();
  1674. if (screen.is_main_screen())
  1675. rect.set_height(rect.height() - TaskbarWindow::taskbar_height());
  1676. if (tile_type == WindowTileType::Maximized) {
  1677. auto border_thickness = palette().window_border_thickness();
  1678. rect.inflate(border_thickness * 2, border_thickness * 2);
  1679. }
  1680. if (tile_type == WindowTileType::Left
  1681. || tile_type == WindowTileType::TopLeft
  1682. || tile_type == WindowTileType::BottomLeft) {
  1683. rect.set_width(rect.width() / 2);
  1684. }
  1685. if (tile_type == WindowTileType::Right
  1686. || tile_type == WindowTileType::TopRight
  1687. || tile_type == WindowTileType::BottomRight) {
  1688. rect.set_width(rect.width() / 2);
  1689. rect.set_x(rect.width());
  1690. }
  1691. if (tile_type == WindowTileType::Top
  1692. || tile_type == WindowTileType::TopLeft
  1693. || tile_type == WindowTileType::TopRight) {
  1694. rect.set_height(rect.height() / 2);
  1695. }
  1696. if (tile_type == WindowTileType::Bottom
  1697. || tile_type == WindowTileType::BottomLeft
  1698. || tile_type == WindowTileType::BottomRight) {
  1699. auto half_screen_remainder = rect.height() % 2;
  1700. rect.set_height(rect.height() / 2 + half_screen_remainder);
  1701. rect.set_y(rect.height() - half_screen_remainder);
  1702. }
  1703. Gfx::IntRect window_rect = window.rect();
  1704. Gfx::IntRect window_frame_rect = window.frame().rect();
  1705. if (tile_type == WindowTileType::VerticallyMaximized) {
  1706. rect.set_x(window_rect.x());
  1707. rect.set_width(window_rect.width());
  1708. } else {
  1709. rect.set_x(rect.x() + window_rect.x() - window_frame_rect.x());
  1710. rect.set_width(rect.width() - window_frame_rect.width() + window_rect.width());
  1711. }
  1712. if (tile_type == WindowTileType::HorizontallyMaximized) {
  1713. rect.set_y(window_rect.y());
  1714. rect.set_height(window_rect.height());
  1715. } else {
  1716. rect.set_y(rect.y() + window_rect.y() - window_frame_rect.y());
  1717. rect.set_height(rect.height() - window_frame_rect.height() + window_rect.height());
  1718. }
  1719. if (relative_to_window_screen)
  1720. rect.translate_by(-screen.rect().location());
  1721. return rect;
  1722. }
  1723. void WindowManager::start_dnd_drag(ConnectionFromClient& client, DeprecatedString const& text, Gfx::Bitmap const* bitmap, Core::MimeData const& mime_data)
  1724. {
  1725. VERIFY(!m_dnd_client);
  1726. m_dnd_client = client;
  1727. m_dnd_text = text;
  1728. Compositor::the().invalidate_cursor(true);
  1729. m_dnd_overlay = Compositor::the().create_overlay<DndOverlay>(text, bitmap);
  1730. m_dnd_overlay->set_enabled(true);
  1731. m_dnd_mime_data = mime_data;
  1732. set_automatic_cursor_tracking_window(nullptr);
  1733. }
  1734. void WindowManager::end_dnd_drag()
  1735. {
  1736. VERIFY(m_dnd_client);
  1737. Compositor::the().invalidate_cursor();
  1738. m_dnd_client = nullptr;
  1739. m_dnd_text = {};
  1740. m_dnd_overlay = nullptr;
  1741. m_dnd_accepts_drag = false;
  1742. }
  1743. void WindowManager::set_accepts_drag(bool accepts)
  1744. {
  1745. VERIFY(m_dnd_client);
  1746. m_dnd_accepts_drag = accepts;
  1747. Compositor::the().invalidate_cursor();
  1748. }
  1749. void WindowManager::invalidate_after_theme_or_font_change()
  1750. {
  1751. Compositor::the().set_background_color(m_config->read_entry("Background", "Color", palette().desktop_background().to_deprecated_string()));
  1752. WindowFrame::reload_config();
  1753. for_each_window_stack([&](auto& window_stack) {
  1754. window_stack.for_each_window([&](Window& window) {
  1755. window.frame().theme_changed();
  1756. window.menubar().font_changed(window.rect());
  1757. return IterationDecision::Continue;
  1758. });
  1759. return IterationDecision::Continue;
  1760. });
  1761. ConnectionFromClient::for_each_client([&](ConnectionFromClient& client) {
  1762. client.notify_about_theme_change();
  1763. });
  1764. MenuManager::the().did_change_theme();
  1765. AppletManager::the().did_change_theme();
  1766. Compositor::the().invalidate_after_theme_or_font_change();
  1767. }
  1768. bool WindowManager::update_theme(DeprecatedString theme_path, DeprecatedString theme_name, bool keep_desktop_background)
  1769. {
  1770. auto error_or_new_theme = Gfx::load_system_theme(theme_path);
  1771. if (error_or_new_theme.is_error()) {
  1772. dbgln("WindowManager: Updating theme failed, error {}", error_or_new_theme.error());
  1773. return false;
  1774. }
  1775. auto new_theme = error_or_new_theme.release_value();
  1776. m_theme_overridden = false;
  1777. Gfx::set_system_theme(new_theme);
  1778. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
  1779. m_config->write_entry("Theme", "Name", theme_name);
  1780. if (!keep_desktop_background)
  1781. m_config->remove_entry("Background", "Color");
  1782. if (!sync_config_to_disk())
  1783. return false;
  1784. invalidate_after_theme_or_font_change();
  1785. return true;
  1786. }
  1787. bool WindowManager::set_theme_override(Core::AnonymousBuffer const& theme_override)
  1788. {
  1789. if (!theme_override.is_valid())
  1790. return false;
  1791. m_theme_overridden = true;
  1792. Gfx::set_system_theme(theme_override);
  1793. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(theme_override);
  1794. invalidate_after_theme_or_font_change();
  1795. return true;
  1796. }
  1797. Optional<Core::AnonymousBuffer> WindowManager::get_theme_override() const
  1798. {
  1799. if (!m_theme_overridden)
  1800. return {};
  1801. return Gfx::current_system_theme_buffer();
  1802. }
  1803. void WindowManager::clear_theme_override()
  1804. {
  1805. m_theme_overridden = false;
  1806. auto previous_theme_name = m_config->read_entry("Theme", "Name");
  1807. auto previous_theme = MUST(Gfx::load_system_theme(DeprecatedString::formatted("/res/themes/{}.ini", previous_theme_name)));
  1808. Gfx::set_system_theme(previous_theme);
  1809. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(previous_theme);
  1810. invalidate_after_theme_or_font_change();
  1811. }
  1812. void WindowManager::did_popup_a_menu(Badge<Menu>)
  1813. {
  1814. // Clear any ongoing input gesture
  1815. auto* window = automatic_cursor_tracking_window();
  1816. if (!window)
  1817. return;
  1818. window->set_automatic_cursor_tracking_enabled(false);
  1819. set_automatic_cursor_tracking_window(nullptr);
  1820. }
  1821. void WindowManager::minimize_windows(Window& window, bool minimized)
  1822. {
  1823. for_each_window_in_modal_chain(window, [&](auto& w) {
  1824. w.set_minimized(minimized);
  1825. return IterationDecision::Continue;
  1826. });
  1827. }
  1828. void WindowManager::hide_windows(Window& window, bool hidden)
  1829. {
  1830. for_each_window_in_modal_chain(window, [&](auto& w) {
  1831. w.set_hidden(hidden);
  1832. if (!hidden)
  1833. pick_new_active_window(&window);
  1834. return IterationDecision::Continue;
  1835. });
  1836. }
  1837. void WindowManager::maximize_windows(Window& window, bool maximized)
  1838. {
  1839. for_each_window_in_modal_chain(window, [&](auto& w) {
  1840. if (&window == &w) {
  1841. window.set_maximized(maximized);
  1842. return IterationDecision::Continue;
  1843. }
  1844. if (w.is_minimized())
  1845. w.set_minimized(false);
  1846. return IterationDecision::Continue;
  1847. });
  1848. }
  1849. void WindowManager::set_always_on_top(Window& window, bool always_on_top)
  1850. {
  1851. for_each_window_in_modal_chain(window, [&](auto& w) {
  1852. w.set_always_on_top(always_on_top);
  1853. return IterationDecision::Continue;
  1854. });
  1855. }
  1856. Gfx::IntPoint WindowManager::get_recommended_window_position(Gfx::IntPoint desired)
  1857. {
  1858. // FIXME: Find a better source for the width and height to shift by.
  1859. Gfx::IntPoint shift(8, Gfx::WindowTheme::current().titlebar_height(Gfx::WindowTheme::WindowType::Normal, Gfx::WindowTheme::WindowMode::Other, palette()) + 10);
  1860. Window const* overlap_window = nullptr;
  1861. current_window_stack().for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& window) {
  1862. if (window.is_default_positioned() && (!overlap_window || overlap_window->window_id() < window.window_id())) {
  1863. overlap_window = &window;
  1864. }
  1865. return IterationDecision::Continue;
  1866. });
  1867. Gfx::IntPoint point;
  1868. if (overlap_window) {
  1869. auto& screen = Screen::closest_to_location(desired);
  1870. point = overlap_window->position() + shift;
  1871. point = { point.x() % screen.width(),
  1872. (point.y() >= (screen.height() - (screen.is_main_screen() ? TaskbarWindow::taskbar_height() : 0)))
  1873. ? Gfx::WindowTheme::current().titlebar_height(Gfx::WindowTheme::WindowType::Normal, Gfx::WindowTheme::WindowMode::Other, palette())
  1874. : point.y() };
  1875. } else {
  1876. point = desired;
  1877. }
  1878. return point;
  1879. }
  1880. void WindowManager::reload_icon_bitmaps_after_scale_change()
  1881. {
  1882. reload_config();
  1883. for_each_window_stack([&](auto& window_stack) {
  1884. window_stack.for_each_window([&](Window& window) {
  1885. auto& window_frame = window.frame();
  1886. window_frame.theme_changed();
  1887. return IterationDecision::Continue;
  1888. });
  1889. return IterationDecision::Continue;
  1890. });
  1891. }
  1892. void WindowManager::set_window_with_active_menu(Window* window)
  1893. {
  1894. if (m_window_with_active_menu == window)
  1895. return;
  1896. if (window)
  1897. m_window_with_active_menu = window->make_weak_ptr<Window>();
  1898. else
  1899. m_window_with_active_menu = nullptr;
  1900. }
  1901. WindowStack& WindowManager::get_rendering_window_stacks(WindowStack*& transitioning_window_stack)
  1902. {
  1903. return Compositor::the().get_rendering_window_stacks(transitioning_window_stack);
  1904. }
  1905. void WindowManager::apply_cursor_theme(DeprecatedString const& theme_name)
  1906. {
  1907. auto theme_path = DeprecatedString::formatted("/res/cursor-themes/{}/{}", theme_name, "Config.ini");
  1908. auto cursor_theme_config_or_error = Core::ConfigFile::open(theme_path);
  1909. if (cursor_theme_config_or_error.is_error()) {
  1910. dbgln("Unable to open cursor theme '{}': {}", theme_path, cursor_theme_config_or_error.error());
  1911. return;
  1912. }
  1913. auto cursor_theme_config = cursor_theme_config_or_error.release_value();
  1914. auto* current_cursor = Compositor::the().current_cursor();
  1915. auto reload_cursor = [&](RefPtr<Cursor>& cursor, DeprecatedString const& name) {
  1916. bool is_current_cursor = current_cursor && current_cursor == cursor.ptr();
  1917. static auto const s_default_cursor_path = "/res/cursor-themes/Default/arrow.x2y2.png"sv;
  1918. cursor = Cursor::create(DeprecatedString::formatted("/res/cursor-themes/{}/{}", theme_name, cursor_theme_config->read_entry("Cursor", name)), s_default_cursor_path);
  1919. if (is_current_cursor) {
  1920. Compositor::the().current_cursor_was_reloaded(cursor.ptr());
  1921. if (m_hovered_window) {
  1922. if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
  1923. modal_window->set_cursor(cursor);
  1924. } else if (m_hovered_window->cursor()) {
  1925. m_hovered_window->set_cursor(cursor);
  1926. }
  1927. }
  1928. }
  1929. };
  1930. reload_cursor(m_hidden_cursor, "Hidden");
  1931. reload_cursor(m_arrow_cursor, "Arrow");
  1932. reload_cursor(m_hand_cursor, "Hand");
  1933. reload_cursor(m_help_cursor, "Help");
  1934. reload_cursor(m_resize_horizontally_cursor, "ResizeH");
  1935. reload_cursor(m_resize_vertically_cursor, "ResizeV");
  1936. reload_cursor(m_resize_diagonally_tlbr_cursor, "ResizeDTLBR");
  1937. reload_cursor(m_resize_diagonally_bltr_cursor, "ResizeDBLTR");
  1938. reload_cursor(m_resize_column_cursor, "ResizeColumn");
  1939. reload_cursor(m_resize_row_cursor, "ResizeRow");
  1940. reload_cursor(m_i_beam_cursor, "IBeam");
  1941. reload_cursor(m_disallowed_cursor, "Disallowed");
  1942. reload_cursor(m_move_cursor, "Move");
  1943. reload_cursor(m_drag_cursor, "Drag");
  1944. reload_cursor(m_drag_copy_cursor, "DragCopy");
  1945. reload_cursor(m_wait_cursor, "Wait");
  1946. reload_cursor(m_crosshair_cursor, "Crosshair");
  1947. reload_cursor(m_eyedropper_cursor, "Eyedropper");
  1948. reload_cursor(m_zoom_cursor, "Zoom");
  1949. Compositor::the().invalidate_cursor();
  1950. m_config->write_entry("Mouse", "CursorTheme", theme_name);
  1951. sync_config_to_disk();
  1952. }
  1953. void WindowManager::set_cursor_highlight_radius(int radius)
  1954. {
  1955. // TODO: Validate radius
  1956. m_cursor_highlight_radius = radius;
  1957. Compositor::the().invalidate_cursor();
  1958. m_config->write_num_entry("Mouse", "CursorHighlightRadius", radius);
  1959. sync_config_to_disk();
  1960. }
  1961. void WindowManager::set_cursor_highlight_color(Gfx::Color color)
  1962. {
  1963. m_cursor_highlight_color = color;
  1964. Compositor::the().invalidate_cursor();
  1965. m_config->write_entry("Mouse", "CursorHighlightColor", color.to_deprecated_string());
  1966. sync_config_to_disk();
  1967. }
  1968. void WindowManager::apply_system_effects(Vector<bool> effects, ShowGeometry geometry)
  1969. {
  1970. if (m_system_effects == SystemEffects { effects, geometry })
  1971. return;
  1972. m_system_effects = { effects, geometry };
  1973. m_config->write_bool_entry("Effects", "AnimateMenus", m_system_effects.animate_menus());
  1974. m_config->write_bool_entry("Effects", "FlashMenus", m_system_effects.flash_menus());
  1975. m_config->write_bool_entry("Effects", "AnimateWindows", m_system_effects.animate_windows());
  1976. m_config->write_bool_entry("Effects", "SmoothScrolling", m_system_effects.smooth_scrolling());
  1977. m_config->write_bool_entry("Effects", "TabAccents", m_system_effects.tab_accents());
  1978. m_config->write_bool_entry("Effects", "SplitterKnurls", m_system_effects.splitter_knurls());
  1979. m_config->write_bool_entry("Effects", "Tooltips", m_system_effects.tooltips());
  1980. m_config->write_bool_entry("Effects", "MenuShadow", m_system_effects.menu_shadow());
  1981. m_config->write_bool_entry("Effects", "WindowShadow", m_system_effects.window_shadow());
  1982. m_config->write_bool_entry("Effects", "TooltipShadow", m_system_effects.tooltip_shadow());
  1983. m_config->write_entry("Effects", "ShowGeometry", ShowGeometryTools::enum_to_string(geometry));
  1984. sync_config_to_disk();
  1985. }
  1986. void WindowManager::load_system_effects()
  1987. {
  1988. Vector<bool> effects = {
  1989. m_config->read_bool_entry("Effects", "AnimateMenus", true),
  1990. m_config->read_bool_entry("Effects", "FlashMenus", true),
  1991. m_config->read_bool_entry("Effects", "AnimateWindows", true),
  1992. m_config->read_bool_entry("Effects", "SmoothScrolling", true),
  1993. m_config->read_bool_entry("Effects", "TabAccents", true),
  1994. m_config->read_bool_entry("Effects", "SplitterKnurls", true),
  1995. m_config->read_bool_entry("Effects", "Tooltips", true),
  1996. m_config->read_bool_entry("Effects", "MenuShadow", true),
  1997. m_config->read_bool_entry("Effects", "WindowShadow", true),
  1998. m_config->read_bool_entry("Effects", "TooltipShadow", true)
  1999. };
  2000. ShowGeometry geometry = ShowGeometryTools::string_to_enum(m_config->read_entry("Effects", "ShowGeometry", "OnMoveAndResize"));
  2001. m_system_effects = { effects, geometry };
  2002. ConnectionFromClient::for_each_client([&](auto& client) {
  2003. client.async_update_system_effects(effects);
  2004. });
  2005. }
  2006. bool WindowManager::sync_config_to_disk()
  2007. {
  2008. if (auto result = m_config->sync(); result.is_error()) {
  2009. dbgln("Failed to save config file: {}", result.error());
  2010. return false;
  2011. }
  2012. return true;
  2013. }
  2014. Window* WindowManager::foremost_popup_window(WindowStack& stack)
  2015. {
  2016. Window* popup_window = nullptr;
  2017. for_each_visible_window_from_front_to_back([&](Window& window) {
  2018. if (window.type() == WindowType::Popup) {
  2019. popup_window = &window;
  2020. return IterationDecision::Break;
  2021. }
  2022. return IterationDecision::Continue;
  2023. },
  2024. &stack);
  2025. return popup_window;
  2026. }
  2027. void WindowManager::request_close_fragile_windows(WindowStack& stack)
  2028. {
  2029. for_each_visible_window_from_back_to_front([&](Window& window) {
  2030. if (is_fragile_window_type(window.type()))
  2031. window.request_close();
  2032. return IterationDecision::Continue;
  2033. },
  2034. &stack);
  2035. }
  2036. }