WindowManager.cpp 61 KB

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