WindowManager.cpp 60 KB

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