WindowManager.cpp 91 KB

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