WindowManager.cpp 80 KB

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