WindowManager.cpp 80 KB

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