WindowManager.cpp 92 KB

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