WindowManager.cpp 61 KB

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