WindowManager.cpp 59 KB

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