WindowManager.cpp 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  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/LogStream.h>
  35. #include <AK/StdLibExtras.h>
  36. #include <AK/Vector.h>
  37. #include <LibGfx/Bitmap.h>
  38. #include <LibGfx/CharacterBitmap.h>
  39. #include <LibGfx/Font.h>
  40. #include <LibGfx/Painter.h>
  41. #include <LibGfx/StylePainter.h>
  42. #include <LibGfx/SystemTheme.h>
  43. #include <WindowServer/AppletManager.h>
  44. #include <WindowServer/Button.h>
  45. #include <WindowServer/ClientConnection.h>
  46. #include <WindowServer/Cursor.h>
  47. #include <WindowServer/WindowClientEndpoint.h>
  48. #include <errno.h>
  49. #include <serenity.h>
  50. #include <stdio.h>
  51. #include <time.h>
  52. #include <unistd.h>
  53. namespace WindowServer {
  54. static WindowManager* s_the;
  55. WindowManager& WindowManager::the()
  56. {
  57. ASSERT(s_the);
  58. return *s_the;
  59. }
  60. WindowManager::WindowManager(const Gfx::PaletteImpl& palette)
  61. : m_palette(palette)
  62. {
  63. s_the = this;
  64. reload_config();
  65. Compositor::the().did_construct_window_manager({});
  66. }
  67. WindowManager::~WindowManager()
  68. {
  69. }
  70. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name)
  71. {
  72. static const auto s_default_cursor_path = "/res/cursors/arrow.x2y2.png";
  73. auto path = m_config->read_entry("Cursor", name, s_default_cursor_path);
  74. auto gb = Gfx::Bitmap::load_from_file(path, compositor_icon_scale());
  75. if (gb)
  76. return Cursor::create(*gb, path);
  77. return Cursor::create(*Gfx::Bitmap::load_from_file(s_default_cursor_path), s_default_cursor_path);
  78. }
  79. void WindowManager::reload_config()
  80. {
  81. m_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  82. m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  83. m_hidden_cursor = get_cursor("Hidden");
  84. m_arrow_cursor = get_cursor("Arrow");
  85. m_hand_cursor = get_cursor("Hand");
  86. m_help_cursor = get_cursor("Help");
  87. m_resize_horizontally_cursor = get_cursor("ResizeH");
  88. m_resize_vertically_cursor = get_cursor("ResizeV");
  89. m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
  90. m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
  91. m_resize_column_cursor = get_cursor("ResizeColumn");
  92. m_resize_row_cursor = get_cursor("ResizeRow");
  93. m_i_beam_cursor = get_cursor("IBeam");
  94. m_disallowed_cursor = get_cursor("Disallowed");
  95. m_move_cursor = get_cursor("Move");
  96. m_drag_cursor = get_cursor("Drag");
  97. m_wait_cursor = get_cursor("Wait");
  98. m_crosshair_cursor = get_cursor("Crosshair");
  99. WindowFrame::reload_config();
  100. }
  101. const Gfx::Font& WindowManager::font() const
  102. {
  103. return Gfx::FontDatabase::default_font();
  104. }
  105. const Gfx::Font& WindowManager::window_title_font() const
  106. {
  107. return Gfx::FontDatabase::default_bold_font();
  108. }
  109. bool WindowManager::set_resolution(int width, int height, int scale)
  110. {
  111. bool success = Compositor::the().set_resolution(width, height, scale);
  112. MenuManager::the().set_needs_window_resize();
  113. ClientConnection::for_each_client([&](ClientConnection& client) {
  114. client.notify_about_new_screen_rect(Screen::the().rect());
  115. });
  116. if (success) {
  117. for_each_window([](Window& window) {
  118. window.recalculate_rect();
  119. return IterationDecision::Continue;
  120. });
  121. }
  122. if (m_config) {
  123. if (success) {
  124. dbgln("Saving resolution: {} @ {}x to config file at {}", Gfx::IntSize(width, height), scale, m_config->file_name());
  125. m_config->write_num_entry("Screen", "Width", width);
  126. m_config->write_num_entry("Screen", "Height", height);
  127. m_config->write_num_entry("Screen", "ScaleFactor", scale);
  128. m_config->sync();
  129. } else {
  130. dbgln("Saving fallback resolution: {} @1x to config file at {}", resolution(), m_config->file_name());
  131. m_config->write_num_entry("Screen", "Width", resolution().width());
  132. m_config->write_num_entry("Screen", "Height", resolution().height());
  133. m_config->write_num_entry("Screen", "ScaleFactor", 1);
  134. m_config->sync();
  135. }
  136. }
  137. return success;
  138. }
  139. Gfx::IntSize WindowManager::resolution() const
  140. {
  141. return Screen::the().size();
  142. }
  143. void WindowManager::set_acceleration_factor(double factor)
  144. {
  145. Screen::the().set_acceleration_factor(factor);
  146. dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->file_name());
  147. m_config->write_entry("Mouse", "AccelerationFactor", String::formatted("{}", factor));
  148. m_config->sync();
  149. }
  150. void WindowManager::set_scroll_step_size(unsigned step_size)
  151. {
  152. Screen::the().set_scroll_step_size(step_size);
  153. dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->file_name());
  154. m_config->write_entry("Mouse", "ScrollStepSize", String::number(step_size));
  155. m_config->sync();
  156. }
  157. int WindowManager::scale_factor() const
  158. {
  159. return Screen::the().scale_factor();
  160. }
  161. void WindowManager::add_window(Window& window)
  162. {
  163. bool is_first_window = m_windows_in_order.is_empty();
  164. m_windows_in_order.append(&window);
  165. if (window.is_fullscreen()) {
  166. Core::EventLoop::current().post_event(window, make<ResizeEvent>(Screen::the().rect()));
  167. window.set_rect(Screen::the().rect());
  168. }
  169. if (window.type() != WindowType::Desktop || is_first_window)
  170. set_active_window(&window);
  171. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  172. m_switcher.refresh();
  173. Compositor::the().invalidate_occlusions();
  174. if (window.listens_to_wm_events()) {
  175. for_each_window([&](Window& other_window) {
  176. if (&window != &other_window) {
  177. tell_wm_listener_about_window(window, other_window);
  178. tell_wm_listener_about_window_icon(window, other_window);
  179. }
  180. return IterationDecision::Continue;
  181. });
  182. }
  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 ASSERT 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. ASSERT(!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_Logo, 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_Logo);
  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<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. ASSERT_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. ASSERT_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. ASSERT_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. ASSERT_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. ASSERT(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. ASSERT(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. ASSERT(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. // FIXME: Now that the menubar has a dedicated window, is this special-casing really necessary?
  784. if (MenuManager::the().has_open_menu() || menubar_rect().contains(event.position())) {
  785. for_each_visible_window_of_type_from_front_to_back(WindowType::MenuApplet, [&](auto& window) {
  786. if (!window.rect_in_menubar().contains(event.position()))
  787. return IterationDecision::Continue;
  788. hovered_window = &window;
  789. return IterationDecision::Break;
  790. });
  791. clear_resize_candidate();
  792. MenuManager::the().dispatch_event(event);
  793. return;
  794. }
  795. Window* event_window_with_frame = nullptr;
  796. if (m_active_input_tracking_window) {
  797. // At this point, we have delivered the start of an input sequence to a
  798. // client application. We must keep delivering to that client
  799. // application until the input sequence is done.
  800. //
  801. // This prevents e.g. moving on one window out of the bounds starting
  802. // a move in that other unrelated window, and other silly shenanigans.
  803. auto translated_event = event.translated(-m_active_input_tracking_window->position());
  804. deliver_mouse_event(*m_active_input_tracking_window, translated_event, true);
  805. received_mouse_event = m_active_input_tracking_window.ptr();
  806. if (event.type() == Event::MouseUp && event.buttons() == 0) {
  807. m_active_input_tracking_window = nullptr;
  808. }
  809. for_each_visible_window_from_front_to_back([&](auto& window) {
  810. if (window.hit_test(event.position())) {
  811. hovered_window = &window;
  812. return IterationDecision::Break;
  813. }
  814. return IterationDecision::Continue;
  815. });
  816. } else {
  817. auto process_mouse_event_for_window = [&](Window& window) {
  818. if (&window != m_resize_candidate.ptr())
  819. clear_resize_candidate();
  820. // First check if we should initiate a move or resize (Logo+LMB or Logo+RMB).
  821. // In those cases, the event is swallowed by the window manager.
  822. if (window.is_movable()) {
  823. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
  824. hovered_window = &window;
  825. start_window_move(window, event);
  826. return;
  827. }
  828. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.blocking_modal_window()) {
  829. hovered_window = &window;
  830. start_window_resize(window, event);
  831. return;
  832. }
  833. }
  834. if (m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseWheel) {
  835. float opacity_change = -event.wheel_delta() * 0.05f;
  836. float new_opacity = window.opacity() + opacity_change;
  837. if (new_opacity < 0.05f)
  838. new_opacity = 0.05f;
  839. if (new_opacity > 1.0f)
  840. new_opacity = 1.0f;
  841. window.set_opacity(new_opacity);
  842. return;
  843. }
  844. ASSERT(window.hit_test(event.position()));
  845. if (event.type() == Event::MouseDown) {
  846. // We're clicking on something that's blocked by a modal window.
  847. // Flash the modal window to let the user know about it.
  848. if (auto* blocking_modal_window = window.blocking_modal_window())
  849. blocking_modal_window->frame().start_flash_animation();
  850. if (window.type() == WindowType::Normal || window.type() == WindowType::ToolWindow)
  851. move_to_front_and_make_active(window);
  852. else if (window.type() == WindowType::Desktop)
  853. set_active_window(&window);
  854. }
  855. if (window.frame().hit_test(event.position())) {
  856. // We are hitting the frame, pass the event along to WindowFrame.
  857. window.frame().on_mouse_event(event.translated(-window.frame().rect().location()));
  858. event_window_with_frame = &window;
  859. } else if (window.hit_test(event.position(), false)) {
  860. // We are hitting the window content
  861. hovered_window = &window;
  862. if (!window.global_cursor_tracking() && !window.blocking_modal_window()) {
  863. auto translated_event = event.translated(-window.position());
  864. deliver_mouse_event(window, translated_event, true);
  865. received_mouse_event = &window;
  866. if (event.type() == Event::MouseDown) {
  867. m_active_input_tracking_window = window;
  868. }
  869. }
  870. }
  871. };
  872. if (auto* fullscreen_window = active_fullscreen_window()) {
  873. process_mouse_event_for_window(*fullscreen_window);
  874. } else {
  875. for_each_visible_window_from_front_to_back([&](Window& window) {
  876. if (!window.hit_test(event.position()))
  877. return IterationDecision::Continue;
  878. process_mouse_event_for_window(window);
  879. return IterationDecision::Break;
  880. });
  881. }
  882. // Clicked outside of any window
  883. if (!hovered_window && !event_window_with_frame && event.type() == Event::MouseDown)
  884. set_active_window(nullptr);
  885. }
  886. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  887. if (received_mouse_event == window)
  888. continue;
  889. if (!window->global_cursor_tracking() || !window->is_visible() || window->is_minimized() || window->blocking_modal_window())
  890. continue;
  891. auto translated_event = event.translated(-window->position());
  892. deliver_mouse_event(*window, translated_event, false);
  893. }
  894. if (event_window_with_frame != m_resize_candidate.ptr())
  895. clear_resize_candidate();
  896. }
  897. void WindowManager::reevaluate_hovered_window(Window* updated_window)
  898. {
  899. if (m_dnd_client || m_resize_window || m_move_window || m_cursor_tracking_button || MenuManager::the().has_open_menu())
  900. return;
  901. auto cursor_location = Screen::the().cursor_location();
  902. auto* currently_hovered = hovered_window();
  903. if (updated_window) {
  904. if (!(updated_window == currently_hovered || updated_window->frame().rect().contains(cursor_location) || (currently_hovered && currently_hovered->frame().rect().contains(cursor_location))))
  905. return;
  906. }
  907. Window* hovered_window = nullptr;
  908. if (auto* fullscreen_window = active_fullscreen_window()) {
  909. if (fullscreen_window->hit_test(cursor_location))
  910. hovered_window = fullscreen_window;
  911. } else {
  912. for_each_visible_window_from_front_to_back([&](Window& window) {
  913. if (!window.hit_test(cursor_location))
  914. return IterationDecision::Continue;
  915. hovered_window = &window;
  916. return IterationDecision::Break;
  917. });
  918. }
  919. if (set_hovered_window(hovered_window)) {
  920. if (currently_hovered && m_resize_candidate == currently_hovered)
  921. clear_resize_candidate();
  922. if (hovered_window) {
  923. // Send a fake MouseMove event. This allows the new hovering window
  924. // to determine which widget we're hovering, and also update the cursor
  925. // accordingly. We do this because this re-evaluation of the currently
  926. // hovered window wasn't triggered by a mouse move event, but rather
  927. // e.g. a hit-test result change due to a transparent window repaint.
  928. if (hovered_window->hit_test(cursor_location, false)) {
  929. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->rect().location()), 0, MouseButton::None, 0);
  930. hovered_window->event(event);
  931. } else if (!hovered_window->is_frameless()) {
  932. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->frame().rect().location()), 0, MouseButton::None, 0);
  933. hovered_window->frame().on_mouse_event(event);
  934. }
  935. }
  936. }
  937. }
  938. void WindowManager::clear_resize_candidate()
  939. {
  940. if (m_resize_candidate)
  941. Compositor::the().invalidate_cursor();
  942. m_resize_candidate = nullptr;
  943. }
  944. Gfx::IntRect WindowManager::menubar_rect() const
  945. {
  946. if (active_fullscreen_window())
  947. return {};
  948. return MenuManager::the().menubar_rect();
  949. }
  950. Gfx::IntRect WindowManager::desktop_rect() const
  951. {
  952. if (active_fullscreen_window())
  953. return Screen::the().rect();
  954. return {
  955. 0,
  956. menubar_rect().bottom() + 1,
  957. Screen::the().width(),
  958. Screen::the().height() - menubar_rect().height() - 28
  959. };
  960. }
  961. Gfx::IntRect WindowManager::arena_rect_for_type(WindowType type) const
  962. {
  963. switch (type) {
  964. case WindowType::Desktop:
  965. case WindowType::Normal:
  966. case WindowType::ToolWindow:
  967. return desktop_rect();
  968. case WindowType::Menu:
  969. case WindowType::WindowSwitcher:
  970. case WindowType::Taskbar:
  971. case WindowType::Tooltip:
  972. case WindowType::Menubar:
  973. case WindowType::MenuApplet:
  974. case WindowType::Notification:
  975. return Screen::the().rect();
  976. default:
  977. ASSERT_NOT_REACHED();
  978. }
  979. }
  980. void WindowManager::event(Core::Event& event)
  981. {
  982. if (static_cast<Event&>(event).is_mouse_event()) {
  983. if (event.type() != Event::MouseMove)
  984. m_previous_event_is_key_down_logo = false;
  985. Window* hovered_window = nullptr;
  986. process_mouse_event(static_cast<MouseEvent&>(event), hovered_window);
  987. set_hovered_window(hovered_window);
  988. return;
  989. }
  990. if (static_cast<Event&>(event).is_key_event()) {
  991. auto& key_event = static_cast<const KeyEvent&>(event);
  992. m_keyboard_modifiers = key_event.modifiers();
  993. // Escape key cancels an ongoing drag.
  994. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
  995. // Notify the drag-n-drop client that the drag was cancelled.
  996. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  997. // Also notify the currently hovered window (if any) that the ongoing drag was cancelled.
  998. if (m_hovered_window && m_hovered_window->client() && m_hovered_window->client() != m_dnd_client)
  999. m_hovered_window->client()->post_message(Messages::WindowClient::DragCancelled());
  1000. end_dnd_drag();
  1001. return;
  1002. }
  1003. if (key_event.type() == Event::KeyDown && (key_event.modifiers() == (Mod_Ctrl | Mod_Logo | Mod_Shift) && key_event.key() == Key_I)) {
  1004. reload_icon_bitmaps_after_scale_change(!m_allow_hidpi_icons);
  1005. Compositor::the().invalidate_screen();
  1006. return;
  1007. }
  1008. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Logo) {
  1009. m_previous_event_is_key_down_logo = true;
  1010. } else if (m_previous_event_is_key_down_logo) {
  1011. m_previous_event_is_key_down_logo = false;
  1012. if (key_event.type() == Event::KeyUp && key_event.key() == Key_Logo) {
  1013. if (MenuManager::the().has_open_menu()) {
  1014. MenuManager::the().close_everyone();
  1015. } else {
  1016. MenuManager::the().open_menu(*MenuManager::the().system_menu());
  1017. }
  1018. return;
  1019. }
  1020. }
  1021. if (MenuManager::the().current_menu()) {
  1022. MenuManager::the().dispatch_event(event);
  1023. return;
  1024. }
  1025. if (key_event.type() == Event::KeyDown && ((key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab) || (key_event.modifiers() == (Mod_Logo | Mod_Shift) && key_event.key() == Key_Tab)))
  1026. m_switcher.show();
  1027. if (m_switcher.is_visible()) {
  1028. m_switcher.on_key_event(key_event);
  1029. return;
  1030. }
  1031. if (m_active_input_window) {
  1032. if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Logo) {
  1033. if (key_event.key() == Key_Down) {
  1034. if (m_active_input_window->is_resizable() && m_active_input_window->is_maximized()) {
  1035. maximize_windows(*m_active_input_window, false);
  1036. return;
  1037. }
  1038. if (m_active_input_window->is_minimizable())
  1039. minimize_windows(*m_active_input_window, true);
  1040. return;
  1041. }
  1042. if (m_active_input_window->is_resizable()) {
  1043. if (key_event.key() == Key_Up) {
  1044. maximize_windows(*m_active_input_window, !m_active_input_window->is_maximized());
  1045. return;
  1046. }
  1047. if (key_event.key() == Key_Left) {
  1048. if (m_active_input_window->tiled() == WindowTileType::Left)
  1049. return;
  1050. if (m_active_input_window->tiled() != WindowTileType::None) {
  1051. m_active_input_window->set_untiled();
  1052. return;
  1053. }
  1054. if (m_active_input_window->is_maximized())
  1055. maximize_windows(*m_active_input_window, false);
  1056. m_active_input_window->set_tiled(WindowTileType::Left);
  1057. return;
  1058. }
  1059. if (key_event.key() == Key_Right) {
  1060. if (m_active_input_window->tiled() == WindowTileType::Right)
  1061. return;
  1062. if (m_active_input_window->tiled() != WindowTileType::None) {
  1063. m_active_input_window->set_untiled();
  1064. return;
  1065. }
  1066. if (m_active_input_window->is_maximized())
  1067. maximize_windows(*m_active_input_window, false);
  1068. m_active_input_window->set_tiled(WindowTileType::Right);
  1069. return;
  1070. }
  1071. }
  1072. }
  1073. m_active_input_window->dispatch_event(event);
  1074. return;
  1075. }
  1076. }
  1077. Core::Object::event(event);
  1078. }
  1079. void WindowManager::set_highlight_window(Window* window)
  1080. {
  1081. if (window == m_highlight_window)
  1082. return;
  1083. auto* previous_highlight_window = m_highlight_window.ptr();
  1084. m_highlight_window = window;
  1085. if (previous_highlight_window)
  1086. previous_highlight_window->invalidate(true, true);
  1087. if (m_highlight_window)
  1088. m_highlight_window->invalidate(true, true);
  1089. }
  1090. bool WindowManager::is_active_window_or_accessory(Window& window) const
  1091. {
  1092. if (m_active_window == &window)
  1093. return true;
  1094. if (!window.is_accessory())
  1095. return false;
  1096. return m_active_window == window.parent_window();
  1097. }
  1098. static bool window_type_can_become_active(WindowType type)
  1099. {
  1100. return type == WindowType::Normal || type == WindowType::ToolWindow || type == WindowType::Desktop;
  1101. }
  1102. void WindowManager::restore_active_input_window(Window* window)
  1103. {
  1104. // If the previous active input window is gone, fall back to the
  1105. // current active window
  1106. if (!window)
  1107. window = active_window();
  1108. // If the current active window is also gone, pick some other window
  1109. if (!window && pick_new_active_window(nullptr))
  1110. return;
  1111. set_active_input_window(window);
  1112. }
  1113. Window* WindowManager::set_active_input_window(Window* window)
  1114. {
  1115. if (window == m_active_input_window)
  1116. return window;
  1117. Window* previous_input_window = m_active_input_window;
  1118. if (previous_input_window)
  1119. Core::EventLoop::current().post_event(*previous_input_window, make<Event>(Event::WindowInputLeft));
  1120. if (window) {
  1121. m_active_input_window = *window;
  1122. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  1123. } else {
  1124. m_active_input_window = nullptr;
  1125. }
  1126. return previous_input_window;
  1127. }
  1128. void WindowManager::set_active_window(Window* window, bool make_input)
  1129. {
  1130. if (window) {
  1131. if (auto* modal_window = window->blocking_modal_window()) {
  1132. ASSERT(modal_window->is_modal());
  1133. ASSERT(modal_window != window);
  1134. window = modal_window;
  1135. make_input = true;
  1136. }
  1137. if (!window_type_can_become_active(window->type()))
  1138. return;
  1139. }
  1140. auto* new_active_input_window = window;
  1141. if (window && window->is_accessory()) {
  1142. // The parent of an accessory window is always the active
  1143. // window, but input is routed to the accessory window
  1144. window = window->parent_window();
  1145. }
  1146. if (make_input)
  1147. set_active_input_window(new_active_input_window);
  1148. if (window == m_active_window)
  1149. return;
  1150. auto* previously_active_window = m_active_window.ptr();
  1151. if (previously_active_window) {
  1152. Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
  1153. previously_active_window->invalidate(true, true);
  1154. m_active_window = nullptr;
  1155. m_active_input_tracking_window = nullptr;
  1156. tell_wm_listeners_window_state_changed(*previously_active_window);
  1157. }
  1158. if (window) {
  1159. m_active_window = *window;
  1160. Core::EventLoop::current().post_event(*m_active_window, make<Event>(Event::WindowActivated));
  1161. m_active_window->invalidate(true, true);
  1162. if (auto* client = window->client()) {
  1163. MenuManager::the().set_current_menubar(client->app_menubar());
  1164. } else {
  1165. MenuManager::the().set_current_menubar(nullptr);
  1166. }
  1167. tell_wm_listeners_window_state_changed(*m_active_window);
  1168. } else {
  1169. MenuManager::the().set_current_menubar(nullptr);
  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. const ClientConnection* WindowManager::active_client() const
  1186. {
  1187. if (m_active_window)
  1188. return m_active_window->client();
  1189. return nullptr;
  1190. }
  1191. void WindowManager::notify_client_changed_app_menubar(ClientConnection& client)
  1192. {
  1193. if (active_client() == &client)
  1194. MenuManager::the().set_current_menubar(client.app_menubar());
  1195. }
  1196. const Cursor& WindowManager::active_cursor() const
  1197. {
  1198. if (m_dnd_client)
  1199. return *m_drag_cursor;
  1200. if (m_move_window)
  1201. return *m_move_cursor;
  1202. if (m_resize_window || m_resize_candidate) {
  1203. switch (m_resize_direction) {
  1204. case ResizeDirection::Up:
  1205. case ResizeDirection::Down:
  1206. return *m_resize_vertically_cursor;
  1207. case ResizeDirection::Left:
  1208. case ResizeDirection::Right:
  1209. return *m_resize_horizontally_cursor;
  1210. case ResizeDirection::UpLeft:
  1211. case ResizeDirection::DownRight:
  1212. return *m_resize_diagonally_tlbr_cursor;
  1213. case ResizeDirection::UpRight:
  1214. case ResizeDirection::DownLeft:
  1215. return *m_resize_diagonally_bltr_cursor;
  1216. case ResizeDirection::None:
  1217. break;
  1218. }
  1219. }
  1220. if (m_hovered_window) {
  1221. if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
  1222. if (modal_window->cursor())
  1223. return *modal_window->cursor();
  1224. } else if (m_hovered_window->cursor()) {
  1225. return *m_hovered_window->cursor();
  1226. }
  1227. }
  1228. return *m_arrow_cursor;
  1229. }
  1230. void WindowManager::set_hovered_button(Button* button)
  1231. {
  1232. m_hovered_button = button;
  1233. }
  1234. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1235. {
  1236. m_resize_candidate = window;
  1237. m_resize_direction = direction;
  1238. }
  1239. ResizeDirection WindowManager::resize_direction_of_window(const Window& window)
  1240. {
  1241. if (&window != m_resize_window)
  1242. return ResizeDirection::None;
  1243. return m_resize_direction;
  1244. }
  1245. Gfx::IntRect WindowManager::maximized_window_rect(const Window& window) const
  1246. {
  1247. Gfx::IntRect rect = Screen::the().rect();
  1248. // Subtract window title bar (leaving the border)
  1249. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  1250. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  1251. // Subtract menu bar
  1252. rect.set_y(rect.y() + menubar_rect().height());
  1253. rect.set_height(rect.height() - menubar_rect().height());
  1254. // Subtract taskbar window height if present
  1255. const_cast<WindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
  1256. rect.set_height(rect.height() - taskbar_window.height());
  1257. return IterationDecision::Break;
  1258. });
  1259. constexpr int tasteful_space_above_maximized_window = 2;
  1260. rect.set_y(rect.y() + tasteful_space_above_maximized_window);
  1261. rect.set_height(rect.height() - tasteful_space_above_maximized_window);
  1262. return rect;
  1263. }
  1264. void WindowManager::start_dnd_drag(ClientConnection& client, const String& text, const Gfx::Bitmap* bitmap, const Core::MimeData& mime_data)
  1265. {
  1266. ASSERT(!m_dnd_client);
  1267. m_dnd_client = client;
  1268. m_dnd_text = text;
  1269. m_dnd_bitmap = bitmap;
  1270. m_dnd_mime_data = mime_data;
  1271. Compositor::the().invalidate_cursor();
  1272. m_active_input_tracking_window = nullptr;
  1273. }
  1274. void WindowManager::end_dnd_drag()
  1275. {
  1276. ASSERT(m_dnd_client);
  1277. Compositor::the().invalidate_cursor();
  1278. m_dnd_client = nullptr;
  1279. m_dnd_text = {};
  1280. m_dnd_bitmap = nullptr;
  1281. }
  1282. Gfx::IntRect WindowManager::dnd_rect() const
  1283. {
  1284. int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
  1285. int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
  1286. int width = font().width(m_dnd_text) + bitmap_width;
  1287. int height = max((int)font().glyph_height(), bitmap_height);
  1288. auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
  1289. return Gfx::IntRect(location, { width, height }).inflated(16, 8);
  1290. }
  1291. bool WindowManager::update_theme(String theme_path, String theme_name)
  1292. {
  1293. auto new_theme = Gfx::load_system_theme(theme_path);
  1294. if (!new_theme.is_valid())
  1295. return false;
  1296. Gfx::set_system_theme(new_theme);
  1297. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
  1298. Compositor::the().set_background_color(palette().desktop_background().to_string());
  1299. HashTable<ClientConnection*> notified_clients;
  1300. WindowFrame::reload_config();
  1301. for_each_window([&](Window& window) {
  1302. if (window.client()) {
  1303. if (!notified_clients.contains(window.client())) {
  1304. window.client()->post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  1305. notified_clients.set(window.client());
  1306. }
  1307. }
  1308. window.frame().theme_changed();
  1309. return IterationDecision::Continue;
  1310. });
  1311. MenuManager::the().did_change_theme();
  1312. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  1313. wm_config->write_entry("Theme", "Name", theme_name);
  1314. wm_config->sync();
  1315. Compositor::the().invalidate_occlusions();
  1316. Compositor::the().invalidate_screen();
  1317. return true;
  1318. }
  1319. void WindowManager::did_popup_a_menu(Badge<Menu>)
  1320. {
  1321. // Clear any ongoing input gesture
  1322. if (!m_active_input_tracking_window)
  1323. return;
  1324. m_active_input_tracking_window->set_automatic_cursor_tracking_enabled(false);
  1325. m_active_input_tracking_window = nullptr;
  1326. }
  1327. void WindowManager::minimize_windows(Window& window, bool minimized)
  1328. {
  1329. for_each_window_in_modal_stack(window, [&](auto& w, bool) {
  1330. w.set_minimized(minimized);
  1331. return IterationDecision::Continue;
  1332. });
  1333. }
  1334. void WindowManager::maximize_windows(Window& window, bool maximized)
  1335. {
  1336. for_each_window_in_modal_stack(window, [&](auto& w, bool stack_top) {
  1337. if (stack_top)
  1338. w.set_maximized(maximized);
  1339. if (w.is_minimized())
  1340. w.set_minimized(false);
  1341. return IterationDecision::Continue;
  1342. });
  1343. }
  1344. Gfx::IntPoint WindowManager::get_recommended_window_position(const Gfx::IntPoint& desired)
  1345. {
  1346. // FIXME: Find a better source for the width and height to shift by.
  1347. Gfx::IntPoint shift(8, Gfx::WindowTheme::current().title_bar_height(Gfx::WindowTheme::WindowType::Normal, palette()) + 10);
  1348. // FIXME: Find a better source for this.
  1349. int taskbar_height = 28;
  1350. int menubar_height = MenuManager::the().menubar_rect().height();
  1351. const Window* overlap_window = nullptr;
  1352. for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& window) {
  1353. if (window.default_positioned() && (!overlap_window || overlap_window->window_id() < window.window_id())) {
  1354. overlap_window = &window;
  1355. }
  1356. return IterationDecision::Continue;
  1357. });
  1358. Gfx::IntPoint point;
  1359. if (overlap_window) {
  1360. point = overlap_window->position() + shift;
  1361. point = { point.x() % Screen::the().width(),
  1362. (point.y() >= (Screen::the().height() - taskbar_height))
  1363. ? menubar_height + Gfx::WindowTheme::current().title_bar_height(Gfx::WindowTheme::WindowType::Normal, palette())
  1364. : point.y() };
  1365. } else {
  1366. point = desired;
  1367. }
  1368. return point;
  1369. }
  1370. int WindowManager::compositor_icon_scale() const
  1371. {
  1372. if (!m_allow_hidpi_icons)
  1373. return 1;
  1374. return scale_factor();
  1375. }
  1376. void WindowManager::reload_icon_bitmaps_after_scale_change(bool allow_hidpi_icons)
  1377. {
  1378. m_allow_hidpi_icons = allow_hidpi_icons;
  1379. reload_config();
  1380. for_each_window([&](Window& window) {
  1381. auto& window_frame = window.frame();
  1382. window_frame.theme_changed();
  1383. return IterationDecision::Continue;
  1384. });
  1385. }
  1386. }