WindowManager.cpp 89 KB

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