WindowManager.cpp 60 KB

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