WindowManager.cpp 56 KB

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