WindowManager.cpp 61 KB

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