WindowManager.cpp 91 KB

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