WindowManager.cpp 60 KB

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