WindowManager.cpp 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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)
  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->is_resizable()) {
  450. process_event_for_doubleclick(*m_move_window, event);
  451. if (event.type() == Event::MouseDoubleClick) {
  452. dbgln_if(DOUBLECLICK_DEBUG, "[WM] Click up became doubleclick!");
  453. m_move_window->set_maximized(!m_move_window->is_maximized());
  454. }
  455. }
  456. m_move_window = nullptr;
  457. return true;
  458. }
  459. if (event.type() == Event::MouseMove) {
  460. if constexpr (MOVE_DEBUG) {
  461. dbgln("[WM] Moving, origin: {}, now: {}", m_move_origin, event.position());
  462. if (m_move_window->is_maximized())
  463. dbgln(" [!] The window is still maximized. Not moving yet.");
  464. }
  465. const int tiling_deadzone = 10;
  466. const int secondary_deadzone = 2;
  467. auto desktop = desktop_rect();
  468. if (m_move_window->is_maximized()) {
  469. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  470. if (pixels_moved_from_start > 5) {
  471. // dbgln("[WM] de-maximizing window");
  472. m_move_origin = event.position();
  473. if (m_move_origin.y() <= secondary_deadzone + desktop.top())
  474. return true;
  475. m_move_window->set_maximized(false, event.position());
  476. m_move_window_origin = m_move_window->position();
  477. }
  478. } else {
  479. bool is_resizable = m_move_window->is_resizable();
  480. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  481. if (is_resizable && event.x() <= tiling_deadzone) {
  482. if (event.y() <= tiling_deadzone + desktop.top())
  483. m_move_window->set_tiled(WindowTileType::TopLeft);
  484. else if (event.y() >= desktop.height() - tiling_deadzone)
  485. m_move_window->set_tiled(WindowTileType::BottomLeft);
  486. else
  487. m_move_window->set_tiled(WindowTileType::Left);
  488. } else if (is_resizable && event.x() >= Screen::the().width() - tiling_deadzone) {
  489. if (event.y() <= tiling_deadzone + desktop.top())
  490. m_move_window->set_tiled(WindowTileType::TopRight);
  491. else if (event.y() >= desktop.height() - tiling_deadzone)
  492. m_move_window->set_tiled(WindowTileType::BottomRight);
  493. else
  494. m_move_window->set_tiled(WindowTileType::Right);
  495. } else if (is_resizable && event.y() <= secondary_deadzone + desktop.top()) {
  496. m_move_window->set_tiled(WindowTileType::Top);
  497. } else if (is_resizable && event.y() >= desktop.bottom() - secondary_deadzone) {
  498. m_move_window->set_tiled(WindowTileType::Bottom);
  499. } else if (m_move_window->tiled() == WindowTileType::None) {
  500. Gfx::IntPoint pos = m_move_window_origin.translated(event.position() - m_move_origin);
  501. m_move_window->set_position_without_repaint(pos);
  502. // "Bounce back" the window if it would end up too far outside the screen.
  503. // 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.
  504. bool force_titlebar_visible = !(m_keyboard_modifiers & Mod_Super);
  505. m_move_window->nudge_into_desktop(force_titlebar_visible);
  506. } else if (pixels_moved_from_start > 5) {
  507. m_move_window->set_untiled(event.position());
  508. m_move_origin = event.position();
  509. m_move_window_origin = m_move_window->position();
  510. }
  511. }
  512. }
  513. return true;
  514. }
  515. bool WindowManager::process_ongoing_window_resize(MouseEvent const& event)
  516. {
  517. if (!m_resize_window)
  518. return false;
  519. if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
  520. dbgln_if(RESIZE_DEBUG, "[WM] Finish resizing Window({})", m_resize_window);
  521. auto max_rect = maximized_window_rect(*m_resize_window);
  522. if (event.y() > max_rect.bottom()) {
  523. dbgln_if(RESIZE_DEBUG, "Should Maximize vertically");
  524. m_resize_window->set_vertically_maximized();
  525. m_resize_window = nullptr;
  526. m_resizing_mouse_button = MouseButton::None;
  527. return true;
  528. }
  529. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect()));
  530. m_resize_window->invalidate(true, true);
  531. m_resize_window = nullptr;
  532. m_resizing_mouse_button = MouseButton::None;
  533. return true;
  534. }
  535. if (event.type() != Event::MouseMove)
  536. return true;
  537. int diff_x = event.x() - m_resize_origin.x();
  538. int diff_y = event.y() - m_resize_origin.y();
  539. int change_w = 0;
  540. int change_h = 0;
  541. switch (m_resize_direction) {
  542. case ResizeDirection::DownRight:
  543. change_w = diff_x;
  544. change_h = diff_y;
  545. break;
  546. case ResizeDirection::Right:
  547. change_w = diff_x;
  548. break;
  549. case ResizeDirection::UpRight:
  550. change_w = diff_x;
  551. change_h = -diff_y;
  552. break;
  553. case ResizeDirection::Up:
  554. change_h = -diff_y;
  555. break;
  556. case ResizeDirection::UpLeft:
  557. change_w = -diff_x;
  558. change_h = -diff_y;
  559. break;
  560. case ResizeDirection::Left:
  561. change_w = -diff_x;
  562. break;
  563. case ResizeDirection::DownLeft:
  564. change_w = -diff_x;
  565. change_h = diff_y;
  566. break;
  567. case ResizeDirection::Down:
  568. change_h = diff_y;
  569. break;
  570. default:
  571. VERIFY_NOT_REACHED();
  572. }
  573. auto new_rect = m_resize_window_original_rect;
  574. // First, size the new rect.
  575. new_rect.set_width(new_rect.width() + change_w);
  576. new_rect.set_height(new_rect.height() + change_h);
  577. m_resize_window->apply_minimum_size(new_rect);
  578. if (!m_resize_window->size_increment().is_null()) {
  579. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  580. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  581. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  582. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  583. }
  584. if (m_resize_window->resize_aspect_ratio().has_value()) {
  585. auto& ratio = m_resize_window->resize_aspect_ratio().value();
  586. auto base_size = m_resize_window->base_size();
  587. if (abs(change_w) > abs(change_h)) {
  588. new_rect.set_height(base_size.height() + (new_rect.width() - base_size.width()) * ratio.height() / ratio.width());
  589. } else {
  590. new_rect.set_width(base_size.width() + (new_rect.height() - base_size.height()) * ratio.width() / ratio.height());
  591. }
  592. }
  593. // Second, set its position so that the sides of the window
  594. // that end up moving are the same ones as the user is dragging,
  595. // no matter which part of the logic above caused us to decide
  596. // to resize by this much.
  597. switch (m_resize_direction) {
  598. case ResizeDirection::DownRight:
  599. case ResizeDirection::Right:
  600. case ResizeDirection::Down:
  601. break;
  602. case ResizeDirection::Left:
  603. case ResizeDirection::Up:
  604. case ResizeDirection::UpLeft:
  605. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  606. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  607. break;
  608. case ResizeDirection::UpRight:
  609. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  610. break;
  611. case ResizeDirection::DownLeft:
  612. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  613. break;
  614. default:
  615. VERIFY_NOT_REACHED();
  616. }
  617. if (m_resize_window->rect() == new_rect)
  618. return true;
  619. dbgln_if(RESIZE_DEBUG, "[WM] Resizing, original: {}, now: {}", m_resize_window_original_rect, new_rect);
  620. m_resize_window->set_rect(new_rect);
  621. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(new_rect));
  622. return true;
  623. }
  624. bool WindowManager::process_ongoing_drag(MouseEvent& event)
  625. {
  626. if (!m_dnd_client)
  627. return false;
  628. if (event.type() == Event::MouseMove) {
  629. // We didn't let go of the drag yet, see if we should send some drag move events..
  630. m_window_stack.for_each_visible_window_from_front_to_back([&](Window& window) {
  631. if (!window.rect().contains(event.position()))
  632. return IterationDecision::Continue;
  633. event.set_drag(true);
  634. event.set_mime_data(*m_dnd_mime_data);
  635. deliver_mouse_event(window, event, false);
  636. return IterationDecision::Break;
  637. });
  638. }
  639. if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Left))
  640. return true;
  641. if (auto* window = m_window_stack.window_at(event.position())) {
  642. m_dnd_client->async_drag_accepted();
  643. if (window->client()) {
  644. auto translated_event = event.translated(-window->position());
  645. window->client()->async_drag_dropped(window->window_id(), translated_event.position(), m_dnd_text, m_dnd_mime_data->all_data());
  646. }
  647. } else {
  648. m_dnd_client->async_drag_cancelled();
  649. }
  650. end_dnd_drag();
  651. return true;
  652. }
  653. void WindowManager::set_cursor_tracking_button(Button* button)
  654. {
  655. m_cursor_tracking_button = button;
  656. }
  657. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) const -> ClickMetadata const&
  658. {
  659. switch (button) {
  660. case MouseButton::Left:
  661. return m_left;
  662. case MouseButton::Right:
  663. return m_right;
  664. case MouseButton::Middle:
  665. return m_middle;
  666. case MouseButton::Back:
  667. return m_back;
  668. case MouseButton::Forward:
  669. return m_forward;
  670. default:
  671. VERIFY_NOT_REACHED();
  672. }
  673. }
  674. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
  675. {
  676. switch (button) {
  677. case MouseButton::Left:
  678. return m_left;
  679. case MouseButton::Right:
  680. return m_right;
  681. case MouseButton::Middle:
  682. return m_middle;
  683. case MouseButton::Back:
  684. return m_back;
  685. case MouseButton::Forward:
  686. return m_forward;
  687. default:
  688. VERIFY_NOT_REACHED();
  689. }
  690. }
  691. bool WindowManager::is_considered_doubleclick(MouseEvent const& event, DoubleClickInfo::ClickMetadata const& metadata) const
  692. {
  693. int elapsed_since_last_click = metadata.clock.elapsed();
  694. if (elapsed_since_last_click < m_double_click_speed) {
  695. auto diff = event.position() - metadata.last_position;
  696. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  697. if (distance_travelled_squared <= (m_max_distance_for_double_click * m_max_distance_for_double_click))
  698. return true;
  699. }
  700. return false;
  701. }
  702. void WindowManager::start_menu_doubleclick(Window& window, MouseEvent const& event)
  703. {
  704. // This is a special case. Basically, we're trying to determine whether
  705. // double clicking on the window menu icon happened. In this case, the
  706. // WindowFrame only receives a MouseDown event, and since the window
  707. // menu popus up, it does not see the MouseUp event. But, if they subsequently
  708. // click there again, the menu is closed and we receive a MouseUp event.
  709. // So, in order to be able to detect a double click when a menu is being
  710. // opened by the MouseDown event, we need to consider the MouseDown event
  711. // as a potential double-click trigger
  712. VERIFY(event.type() == Event::MouseDown);
  713. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  714. if (&window != m_double_click_info.m_clicked_window) {
  715. // we either haven't clicked anywhere, or we haven't clicked on this
  716. // window. set the current click window, and reset the timers.
  717. dbgln_if(DOUBLECLICK_DEBUG, "Initial mousedown on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  718. m_double_click_info.m_clicked_window = window;
  719. m_double_click_info.reset();
  720. }
  721. metadata.last_position = event.position();
  722. metadata.clock.start();
  723. }
  724. bool WindowManager::is_menu_doubleclick(Window& window, MouseEvent const& event) const
  725. {
  726. VERIFY(event.type() == Event::MouseUp);
  727. if (&window != m_double_click_info.m_clicked_window)
  728. return false;
  729. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  730. if (!metadata.clock.is_valid())
  731. return false;
  732. return is_considered_doubleclick(event, metadata);
  733. }
  734. void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
  735. {
  736. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  737. VERIFY(event.type() == Event::MouseUp);
  738. if (&window != m_double_click_info.m_clicked_window) {
  739. // we either haven't clicked anywhere, or we haven't clicked on this
  740. // window. set the current click window, and reset the timers.
  741. dbgln_if(DOUBLECLICK_DEBUG, "Initial mouseup on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  742. m_double_click_info.m_clicked_window = window;
  743. m_double_click_info.reset();
  744. }
  745. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  746. if (!metadata.clock.is_valid() || !is_considered_doubleclick(event, metadata)) {
  747. // either the clock is invalid because we haven't clicked on this
  748. // button on this window yet, so there's nothing to do, or this
  749. // isn't considered to be a double click. either way, restart the
  750. // clock
  751. metadata.clock.start();
  752. } else {
  753. dbgln_if(DOUBLECLICK_DEBUG, "Transforming MouseUp to MouseDoubleClick ({} < {})!", metadata.clock.elapsed(), m_double_click_speed);
  754. event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  755. // invalidate this now we've delivered a doubleclick, otherwise
  756. // tripleclick will deliver two doubleclick events (incorrectly).
  757. metadata.clock = {};
  758. }
  759. metadata.last_position = event.position();
  760. }
  761. void WindowManager::deliver_mouse_event(Window& window, MouseEvent& event, bool process_double_click)
  762. {
  763. auto translated_event = event.translated(-window.position());
  764. window.dispatch_event(translated_event);
  765. if (process_double_click && translated_event.type() == Event::MouseUp) {
  766. process_event_for_doubleclick(window, translated_event);
  767. if (translated_event.type() == Event::MouseDoubleClick)
  768. window.dispatch_event(translated_event);
  769. }
  770. }
  771. bool WindowManager::process_ongoing_active_input_mouse_event(MouseEvent& event)
  772. {
  773. if (!m_active_input_tracking_window)
  774. return false;
  775. // At this point, we have delivered the start of an input sequence to a
  776. // client application. We must keep delivering to that client
  777. // application until the input sequence is done.
  778. //
  779. // This prevents e.g. moving on one window out of the bounds starting
  780. // a move in that other unrelated window, and other silly shenanigans.
  781. deliver_mouse_event(*m_active_input_tracking_window, event, true);
  782. if (event.type() == Event::MouseUp && event.buttons() == 0) {
  783. m_active_input_tracking_window = nullptr;
  784. }
  785. return true;
  786. }
  787. bool WindowManager::process_mouse_event_for_titlebar_buttons(MouseEvent& event)
  788. {
  789. if (m_cursor_tracking_button) {
  790. m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  791. return true;
  792. }
  793. // This is quite hackish, but it's how the Button hover effect is implemented.
  794. if (m_hovered_button && event.type() == Event::MouseMove)
  795. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  796. return false;
  797. }
  798. void WindowManager::process_mouse_event_for_window(HitTestResult& result, MouseEvent& event)
  799. {
  800. auto& window = *result.window;
  801. if (auto* blocking_modal_window = window.blocking_modal_window()) {
  802. if (event.type() == Event::Type::MouseDown) {
  803. // We're clicking on something that's blocked by a modal window.
  804. // Flash the modal window to let the user know about it.
  805. blocking_modal_window->frame().start_flash_animation();
  806. }
  807. // Don't send mouse events to windows blocked by a modal child.
  808. return;
  809. }
  810. // First check if we should initiate a move or resize (Super+LMB or Super+RMB).
  811. // In those cases, the event is swallowed by the window manager.
  812. if (window.is_movable()) {
  813. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Super && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
  814. start_window_move(window, event);
  815. return;
  816. }
  817. if (window.is_resizable() && m_keyboard_modifiers == Mod_Super && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.blocking_modal_window()) {
  818. start_window_resize(window, event);
  819. return;
  820. }
  821. }
  822. if (event.type() == Event::MouseDown) {
  823. if (window.type() == WindowType::Normal || window.type() == WindowType::ToolWindow)
  824. move_to_front_and_make_active(window);
  825. else if (window.type() == WindowType::Desktop)
  826. set_active_window(&window);
  827. }
  828. if (result.is_frame_hit) {
  829. // We are hitting the frame, pass the event along to WindowFrame.
  830. window.frame().handle_mouse_event(event.translated(-window.frame().rect().location()));
  831. return;
  832. }
  833. if (!window.global_cursor_tracking()) {
  834. deliver_mouse_event(window, event, true);
  835. }
  836. if (event.type() == Event::MouseDown)
  837. m_active_input_tracking_window = window;
  838. }
  839. void WindowManager::process_mouse_event(MouseEvent& event)
  840. {
  841. // 0. Forget the resize candidate (window that we could initiate a resize of from the current cursor position.)
  842. // A new resize candidate may be determined if we hit an appropriate part of a window.
  843. clear_resize_candidate();
  844. // 1. Process ongoing drag events. This is done first to avoid clashing with global cursor tracking.
  845. if (process_ongoing_drag(event))
  846. return;
  847. // 2. Send the mouse event to all windows with global cursor tracking enabled.
  848. m_window_stack.for_each_visible_window_from_front_to_back([&](Window& window) {
  849. if (window.global_cursor_tracking())
  850. deliver_mouse_event(window, event, false);
  851. return IterationDecision::Continue;
  852. });
  853. // 3. If there's an active input tracking window, all mouse events go there.
  854. // Tracking ends after all mouse buttons have been released.
  855. if (process_ongoing_active_input_mouse_event(event))
  856. return;
  857. // 4. If there's a window being moved around, take care of that.
  858. if (process_ongoing_window_move(event))
  859. return;
  860. // 5. If there's a window being resized, take care of that.
  861. if (process_ongoing_window_resize(event))
  862. return;
  863. // 6. If the event is inside a titlebar button, WindowServer implements all
  864. // the behavior for those buttons internally.
  865. if (process_mouse_event_for_titlebar_buttons(event))
  866. return;
  867. // 7. If there are menus open, deal with them now. (FIXME: This needs to be cleaned up & simplified!)
  868. bool hitting_menu_in_window_with_active_menu = [&] {
  869. if (!m_window_with_active_menu)
  870. return false;
  871. auto& frame = m_window_with_active_menu->frame();
  872. return frame.menubar_rect().contains(event.position().translated(-frame.rect().location()));
  873. }();
  874. // FIXME: This is quite hackish, we clear the hovered menu before potentially setting the same menu
  875. // as hovered again. This makes sure that the hovered state doesn't linger after moving the
  876. // cursor away from a hovered menu.
  877. MenuManager::the().set_hovered_menu(nullptr);
  878. if (MenuManager::the().has_open_menu()
  879. || hitting_menu_in_window_with_active_menu) {
  880. if (!hitting_menu_in_window_with_active_menu) {
  881. MenuManager::the().dispatch_event(event);
  882. return;
  883. }
  884. }
  885. // 8. Hit test the window stack to see what's under the cursor.
  886. auto result = m_window_stack.hit_test(event.position());
  887. if (!result.has_value()) {
  888. // No window is under the cursor.
  889. if (event.type() == Event::MouseDown) {
  890. // Clicked outside of any window -> no active window.
  891. // FIXME: Is this actually necessary? The desktop window should catch everything anyway.
  892. set_active_window(nullptr);
  893. }
  894. return;
  895. }
  896. process_mouse_event_for_window(result.value(), event);
  897. }
  898. void WindowManager::reevaluate_hovered_window(Window* updated_window)
  899. {
  900. if (m_dnd_client || m_resize_window || m_move_window || m_cursor_tracking_button || MenuManager::the().has_open_menu())
  901. return;
  902. auto cursor_location = Screen::the().cursor_location();
  903. auto* currently_hovered = hovered_window();
  904. if (updated_window) {
  905. if (!(updated_window == currently_hovered || updated_window->frame().rect().contains(cursor_location) || (currently_hovered && currently_hovered->frame().rect().contains(cursor_location))))
  906. return;
  907. }
  908. Window* hovered_window = nullptr;
  909. if (auto* fullscreen_window = active_fullscreen_window()) {
  910. if (fullscreen_window->hit_test(cursor_location).has_value())
  911. hovered_window = fullscreen_window;
  912. } else {
  913. hovered_window = m_window_stack.window_at(cursor_location);
  914. }
  915. if (set_hovered_window(hovered_window)) {
  916. if (currently_hovered && m_resize_candidate == currently_hovered)
  917. clear_resize_candidate();
  918. if (hovered_window) {
  919. // Send a fake MouseMove event. This allows the new hovering window
  920. // to determine which widget we're hovering, and also update the cursor
  921. // accordingly. We do this because this re-evaluation of the currently
  922. // hovered window wasn't triggered by a mouse move event, but rather
  923. // e.g. a hit-test result change due to a transparent window repaint.
  924. if (hovered_window->hit_test(cursor_location, false).has_value()) {
  925. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->rect().location()), 0, MouseButton::None, 0);
  926. hovered_window->dispatch_event(event);
  927. } else if (!hovered_window->is_frameless()) {
  928. MouseEvent event(Event::MouseMove, cursor_location.translated(-hovered_window->frame().rect().location()), 0, MouseButton::None, 0);
  929. hovered_window->frame().handle_mouse_event(event);
  930. }
  931. }
  932. }
  933. }
  934. void WindowManager::clear_resize_candidate()
  935. {
  936. if (m_resize_candidate)
  937. Compositor::the().invalidate_cursor();
  938. m_resize_candidate = nullptr;
  939. }
  940. Gfx::IntRect WindowManager::desktop_rect() const
  941. {
  942. if (active_fullscreen_window())
  943. return Screen::the().rect();
  944. return {
  945. 0,
  946. 0,
  947. Screen::the().width(),
  948. Screen::the().height() - 28
  949. };
  950. }
  951. Gfx::IntRect WindowManager::arena_rect_for_type(WindowType type) const
  952. {
  953. switch (type) {
  954. case WindowType::Desktop:
  955. case WindowType::Normal:
  956. case WindowType::ToolWindow:
  957. return desktop_rect();
  958. case WindowType::Menu:
  959. case WindowType::WindowSwitcher:
  960. case WindowType::Taskbar:
  961. case WindowType::Tooltip:
  962. case WindowType::Applet:
  963. case WindowType::Notification:
  964. return Screen::the().rect();
  965. default:
  966. VERIFY_NOT_REACHED();
  967. }
  968. }
  969. void WindowManager::event(Core::Event& event)
  970. {
  971. if (static_cast<Event&>(event).is_mouse_event()) {
  972. auto& mouse_event = static_cast<MouseEvent&>(event);
  973. if (mouse_event.type() != Event::MouseMove)
  974. m_previous_event_was_super_keydown = false;
  975. process_mouse_event(mouse_event);
  976. set_hovered_window(m_window_stack.window_at(mouse_event.position()));
  977. return;
  978. }
  979. if (static_cast<Event&>(event).is_key_event()) {
  980. auto& key_event = static_cast<KeyEvent const&>(event);
  981. m_keyboard_modifiers = key_event.modifiers();
  982. // Escape key cancels an ongoing drag.
  983. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
  984. // Notify the drag-n-drop client that the drag was cancelled.
  985. m_dnd_client->async_drag_cancelled();
  986. // Also notify the currently hovered window (if any) that the ongoing drag was cancelled.
  987. if (m_hovered_window && m_hovered_window->client() && m_hovered_window->client() != m_dnd_client)
  988. m_hovered_window->client()->async_drag_cancelled();
  989. end_dnd_drag();
  990. return;
  991. }
  992. if (key_event.type() == Event::KeyDown && (key_event.modifiers() == (Mod_Ctrl | Mod_Super | Mod_Shift) && key_event.key() == Key_I)) {
  993. reload_icon_bitmaps_after_scale_change(!m_allow_hidpi_icons);
  994. Compositor::the().invalidate_screen();
  995. return;
  996. }
  997. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Super) {
  998. m_previous_event_was_super_keydown = true;
  999. } else if (m_previous_event_was_super_keydown) {
  1000. m_previous_event_was_super_keydown = false;
  1001. if (!m_dnd_client && !m_active_input_tracking_window && key_event.type() == Event::KeyUp && key_event.key() == Key_Super) {
  1002. tell_wms_super_key_pressed();
  1003. return;
  1004. }
  1005. }
  1006. if (MenuManager::the().current_menu() && key_event.key() != Key_Super) {
  1007. MenuManager::the().dispatch_event(event);
  1008. return;
  1009. }
  1010. 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)))
  1011. m_switcher.show();
  1012. if (m_switcher.is_visible()) {
  1013. m_switcher.on_key_event(key_event);
  1014. return;
  1015. }
  1016. if (m_active_input_window) {
  1017. if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Super && m_active_input_window->type() != WindowType::Desktop) {
  1018. if (key_event.key() == Key_Down) {
  1019. if (m_active_input_window->is_resizable() && m_active_input_window->is_maximized()) {
  1020. maximize_windows(*m_active_input_window, false);
  1021. return;
  1022. }
  1023. if (m_active_input_window->is_minimizable())
  1024. minimize_windows(*m_active_input_window, true);
  1025. return;
  1026. }
  1027. if (m_active_input_window->is_resizable()) {
  1028. if (key_event.key() == Key_Up) {
  1029. maximize_windows(*m_active_input_window, !m_active_input_window->is_maximized());
  1030. return;
  1031. }
  1032. if (key_event.key() == Key_Left) {
  1033. if (m_active_input_window->tiled() == WindowTileType::Left)
  1034. return;
  1035. if (m_active_input_window->tiled() != WindowTileType::None) {
  1036. m_active_input_window->set_untiled();
  1037. return;
  1038. }
  1039. if (m_active_input_window->is_maximized())
  1040. maximize_windows(*m_active_input_window, false);
  1041. m_active_input_window->set_tiled(WindowTileType::Left);
  1042. return;
  1043. }
  1044. if (key_event.key() == Key_Right) {
  1045. if (m_active_input_window->tiled() == WindowTileType::Right)
  1046. return;
  1047. if (m_active_input_window->tiled() != WindowTileType::None) {
  1048. m_active_input_window->set_untiled();
  1049. return;
  1050. }
  1051. if (m_active_input_window->is_maximized())
  1052. maximize_windows(*m_active_input_window, false);
  1053. m_active_input_window->set_tiled(WindowTileType::Right);
  1054. return;
  1055. }
  1056. }
  1057. }
  1058. m_active_input_window->dispatch_event(event);
  1059. return;
  1060. }
  1061. }
  1062. Core::Object::event(event);
  1063. }
  1064. void WindowManager::set_highlight_window(Window* new_highlight_window)
  1065. {
  1066. if (new_highlight_window == m_window_stack.highlight_window())
  1067. return;
  1068. auto* previous_highlight_window = m_window_stack.highlight_window();
  1069. m_window_stack.set_highlight_window(new_highlight_window);
  1070. if (previous_highlight_window) {
  1071. previous_highlight_window->invalidate(true, true);
  1072. Compositor::the().invalidate_screen(previous_highlight_window->frame().render_rect());
  1073. }
  1074. if (new_highlight_window) {
  1075. new_highlight_window->invalidate(true, true);
  1076. Compositor::the().invalidate_screen(new_highlight_window->frame().render_rect());
  1077. }
  1078. // Invalidate occlusions in case the state change changes geometry
  1079. Compositor::the().invalidate_occlusions();
  1080. }
  1081. bool WindowManager::is_active_window_or_accessory(Window& window) const
  1082. {
  1083. if (window.is_accessory())
  1084. return window.parent_window()->is_active();
  1085. return window.is_active();
  1086. }
  1087. static bool window_type_can_become_active(WindowType type)
  1088. {
  1089. return type == WindowType::Normal || type == WindowType::ToolWindow || type == WindowType::Desktop;
  1090. }
  1091. void WindowManager::restore_active_input_window(Window* window)
  1092. {
  1093. // If the previous active input window is gone, fall back to the
  1094. // current active window
  1095. if (!window)
  1096. window = active_window();
  1097. // If the current active window is also gone, pick some other window
  1098. if (!window && pick_new_active_window(nullptr))
  1099. return;
  1100. if (window && !window->is_minimized() && window->is_visible())
  1101. set_active_input_window(window);
  1102. else
  1103. set_active_input_window(nullptr);
  1104. }
  1105. Window* WindowManager::set_active_input_window(Window* window)
  1106. {
  1107. if (window == m_active_input_window)
  1108. return window;
  1109. Window* previous_input_window = m_active_input_window;
  1110. if (previous_input_window)
  1111. Core::EventLoop::current().post_event(*previous_input_window, make<Event>(Event::WindowInputLeft));
  1112. if (window) {
  1113. m_active_input_window = *window;
  1114. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  1115. } else {
  1116. m_active_input_window = nullptr;
  1117. }
  1118. return previous_input_window;
  1119. }
  1120. void WindowManager::set_active_window(Window* new_active_window, bool make_input)
  1121. {
  1122. if (new_active_window) {
  1123. if (auto* modal_window = new_active_window->blocking_modal_window()) {
  1124. VERIFY(modal_window->is_modal());
  1125. VERIFY(modal_window != new_active_window);
  1126. new_active_window = modal_window;
  1127. make_input = true;
  1128. }
  1129. if (!window_type_can_become_active(new_active_window->type()))
  1130. return;
  1131. }
  1132. auto* new_active_input_window = new_active_window;
  1133. if (new_active_window && new_active_window->is_accessory()) {
  1134. // The parent of an accessory window is always the active
  1135. // window, but input is routed to the accessory window
  1136. new_active_window = new_active_window->parent_window();
  1137. }
  1138. if (make_input)
  1139. set_active_input_window(new_active_input_window);
  1140. if (new_active_window == m_window_stack.active_window())
  1141. return;
  1142. if (auto* previously_active_window = m_window_stack.active_window()) {
  1143. for (auto& child_window : previously_active_window->child_windows()) {
  1144. if (child_window && child_window->type() == WindowType::Tooltip)
  1145. child_window->request_close();
  1146. }
  1147. Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
  1148. previously_active_window->invalidate(true, true);
  1149. m_window_stack.set_active_window(nullptr);
  1150. m_active_input_tracking_window = nullptr;
  1151. tell_wms_window_state_changed(*previously_active_window);
  1152. }
  1153. if (new_active_window) {
  1154. m_window_stack.set_active_window(new_active_window);
  1155. Core::EventLoop::current().post_event(*new_active_window, make<Event>(Event::WindowActivated));
  1156. new_active_window->invalidate(true, true);
  1157. tell_wms_window_state_changed(*new_active_window);
  1158. }
  1159. // Window shapes may have changed (e.g. shadows for inactive/active windows)
  1160. Compositor::the().invalidate_occlusions();
  1161. }
  1162. bool WindowManager::set_hovered_window(Window* window)
  1163. {
  1164. if (m_hovered_window == window)
  1165. return false;
  1166. if (m_hovered_window)
  1167. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
  1168. m_hovered_window = window;
  1169. if (m_hovered_window)
  1170. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
  1171. return true;
  1172. }
  1173. ClientConnection const* WindowManager::active_client() const
  1174. {
  1175. if (auto* window = m_window_stack.active_window())
  1176. return window->client();
  1177. return nullptr;
  1178. }
  1179. Cursor const& WindowManager::active_cursor() const
  1180. {
  1181. if (m_dnd_client)
  1182. return *m_drag_cursor;
  1183. if (m_move_window)
  1184. return *m_move_cursor;
  1185. if (m_resize_window || m_resize_candidate) {
  1186. switch (m_resize_direction) {
  1187. case ResizeDirection::Up:
  1188. case ResizeDirection::Down:
  1189. return *m_resize_vertically_cursor;
  1190. case ResizeDirection::Left:
  1191. case ResizeDirection::Right:
  1192. return *m_resize_horizontally_cursor;
  1193. case ResizeDirection::UpLeft:
  1194. case ResizeDirection::DownRight:
  1195. return *m_resize_diagonally_tlbr_cursor;
  1196. case ResizeDirection::UpRight:
  1197. case ResizeDirection::DownLeft:
  1198. return *m_resize_diagonally_bltr_cursor;
  1199. case ResizeDirection::None:
  1200. break;
  1201. }
  1202. }
  1203. if (m_hovered_window) {
  1204. if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
  1205. if (modal_window->cursor())
  1206. return *modal_window->cursor();
  1207. } else if (m_hovered_window->cursor()) {
  1208. return *m_hovered_window->cursor();
  1209. }
  1210. }
  1211. return *m_arrow_cursor;
  1212. }
  1213. void WindowManager::set_hovered_button(Button* button)
  1214. {
  1215. m_hovered_button = button;
  1216. }
  1217. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1218. {
  1219. m_resize_candidate = window;
  1220. m_resize_direction = direction;
  1221. }
  1222. ResizeDirection WindowManager::resize_direction_of_window(Window const& window)
  1223. {
  1224. if (&window != m_resize_window)
  1225. return ResizeDirection::None;
  1226. return m_resize_direction;
  1227. }
  1228. Gfx::IntRect WindowManager::maximized_window_rect(Window const& window) const
  1229. {
  1230. Gfx::IntRect rect = Screen::the().rect();
  1231. // Subtract window title bar (leaving the border)
  1232. rect.set_y(rect.y() + window.frame().titlebar_rect().height() + window.frame().menubar_rect().height());
  1233. rect.set_height(rect.height() - window.frame().titlebar_rect().height() - window.frame().menubar_rect().height());
  1234. // Subtract taskbar window height if present
  1235. const_cast<WindowManager*>(this)->m_window_stack.for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
  1236. rect.set_height(rect.height() - taskbar_window.height());
  1237. return IterationDecision::Break;
  1238. });
  1239. constexpr int tasteful_space_above_maximized_window = 1;
  1240. rect.set_y(rect.y() + tasteful_space_above_maximized_window);
  1241. rect.set_height(rect.height() - tasteful_space_above_maximized_window);
  1242. return rect;
  1243. }
  1244. void WindowManager::start_dnd_drag(ClientConnection& client, String const& text, Gfx::Bitmap const* bitmap, Core::MimeData const& mime_data)
  1245. {
  1246. VERIFY(!m_dnd_client);
  1247. m_dnd_client = client;
  1248. m_dnd_text = text;
  1249. m_dnd_bitmap = bitmap;
  1250. m_dnd_mime_data = mime_data;
  1251. Compositor::the().invalidate_cursor();
  1252. m_active_input_tracking_window = nullptr;
  1253. }
  1254. void WindowManager::end_dnd_drag()
  1255. {
  1256. VERIFY(m_dnd_client);
  1257. Compositor::the().invalidate_cursor();
  1258. m_dnd_client = nullptr;
  1259. m_dnd_text = {};
  1260. m_dnd_bitmap = nullptr;
  1261. }
  1262. Gfx::IntRect WindowManager::dnd_rect() const
  1263. {
  1264. int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
  1265. int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
  1266. int width = font().width(m_dnd_text) + bitmap_width;
  1267. int height = max((int)font().glyph_height(), bitmap_height);
  1268. auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
  1269. return Gfx::IntRect(location, { width, height }).inflated(16, 8);
  1270. }
  1271. void WindowManager::invalidate_after_theme_or_font_change()
  1272. {
  1273. Compositor::the().set_background_color(m_config->read_entry("Background", "Color", palette().desktop_background().to_string()));
  1274. WindowFrame::reload_config();
  1275. m_window_stack.for_each_window([&](Window& window) {
  1276. window.frame().theme_changed();
  1277. return IterationDecision::Continue;
  1278. });
  1279. ClientConnection::for_each_client([&](ClientConnection& client) {
  1280. client.async_update_system_theme(Gfx::current_system_theme_buffer());
  1281. });
  1282. MenuManager::the().did_change_theme();
  1283. AppletManager::the().did_change_theme();
  1284. Compositor::the().invalidate_occlusions();
  1285. Compositor::the().invalidate_screen();
  1286. }
  1287. bool WindowManager::update_theme(String theme_path, String theme_name)
  1288. {
  1289. auto new_theme = Gfx::load_system_theme(theme_path);
  1290. if (!new_theme.is_valid())
  1291. return false;
  1292. Gfx::set_system_theme(new_theme);
  1293. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
  1294. auto wm_config = Core::ConfigFile::open("/etc/WindowServer.ini");
  1295. wm_config->write_entry("Theme", "Name", theme_name);
  1296. wm_config->remove_entry("Background", "Color");
  1297. wm_config->sync();
  1298. invalidate_after_theme_or_font_change();
  1299. return true;
  1300. }
  1301. void WindowManager::did_popup_a_menu(Badge<Menu>)
  1302. {
  1303. // Clear any ongoing input gesture
  1304. if (!m_active_input_tracking_window)
  1305. return;
  1306. m_active_input_tracking_window->set_automatic_cursor_tracking_enabled(false);
  1307. m_active_input_tracking_window = nullptr;
  1308. }
  1309. void WindowManager::minimize_windows(Window& window, bool minimized)
  1310. {
  1311. for_each_window_in_modal_stack(window, [&](auto& w, bool) {
  1312. w.set_minimized(minimized);
  1313. return IterationDecision::Continue;
  1314. });
  1315. }
  1316. void WindowManager::maximize_windows(Window& window, bool maximized)
  1317. {
  1318. for_each_window_in_modal_stack(window, [&](auto& w, bool stack_top) {
  1319. if (stack_top)
  1320. w.set_maximized(maximized);
  1321. if (w.is_minimized())
  1322. w.set_minimized(false);
  1323. return IterationDecision::Continue;
  1324. });
  1325. }
  1326. Gfx::IntPoint WindowManager::get_recommended_window_position(Gfx::IntPoint const& desired)
  1327. {
  1328. // FIXME: Find a better source for the width and height to shift by.
  1329. Gfx::IntPoint shift(8, Gfx::WindowTheme::current().titlebar_height(Gfx::WindowTheme::WindowType::Normal, palette()) + 10);
  1330. // FIXME: Find a better source for this.
  1331. int taskbar_height = 28;
  1332. Window const* overlap_window = nullptr;
  1333. m_window_stack.for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& window) {
  1334. if (window.default_positioned() && (!overlap_window || overlap_window->window_id() < window.window_id())) {
  1335. overlap_window = &window;
  1336. }
  1337. return IterationDecision::Continue;
  1338. });
  1339. Gfx::IntPoint point;
  1340. if (overlap_window) {
  1341. point = overlap_window->position() + shift;
  1342. point = { point.x() % Screen::the().width(),
  1343. (point.y() >= (Screen::the().height() - taskbar_height))
  1344. ? Gfx::WindowTheme::current().titlebar_height(Gfx::WindowTheme::WindowType::Normal, palette())
  1345. : point.y() };
  1346. } else {
  1347. point = desired;
  1348. }
  1349. return point;
  1350. }
  1351. int WindowManager::compositor_icon_scale() const
  1352. {
  1353. if (!m_allow_hidpi_icons)
  1354. return 1;
  1355. return scale_factor();
  1356. }
  1357. void WindowManager::reload_icon_bitmaps_after_scale_change(bool allow_hidpi_icons)
  1358. {
  1359. m_allow_hidpi_icons = allow_hidpi_icons;
  1360. reload_config();
  1361. m_window_stack.for_each_window([&](Window& window) {
  1362. auto& window_frame = window.frame();
  1363. window_frame.theme_changed();
  1364. return IterationDecision::Continue;
  1365. });
  1366. }
  1367. void WindowManager::set_window_with_active_menu(Window* window)
  1368. {
  1369. if (m_window_with_active_menu == window)
  1370. return;
  1371. if (window)
  1372. m_window_with_active_menu = window->make_weak_ptr<Window>();
  1373. else
  1374. m_window_with_active_menu = nullptr;
  1375. }
  1376. }