WindowManager.cpp 86 KB

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