WindowManager.cpp 91 KB

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