WindowManager.cpp 61 KB

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