WindowManager.cpp 60 KB

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