WindowManager.cpp 81 KB

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