WindowManager.cpp 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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/StdLibExtras.h>
  36. #include <AK/Vector.h>
  37. #include <LibGfx/CharacterBitmap.h>
  38. #include <LibGfx/Font.h>
  39. #include <LibGfx/Painter.h>
  40. #include <LibGfx/StylePainter.h>
  41. #include <LibGfx/SystemTheme.h>
  42. #include <WindowServer/AppletManager.h>
  43. #include <WindowServer/Button.h>
  44. #include <WindowServer/ClientConnection.h>
  45. #include <WindowServer/Cursor.h>
  46. #include <WindowServer/WindowClientEndpoint.h>
  47. #include <errno.h>
  48. #include <stdio.h>
  49. #include <time.h>
  50. #include <unistd.h>
  51. //#define DEBUG_COUNTERS
  52. //#define RESIZE_DEBUG
  53. //#define MOVE_DEBUG
  54. //#define DOUBLECLICK_DEBUG
  55. namespace WindowServer {
  56. static WindowManager* s_the;
  57. WindowManager& WindowManager::the()
  58. {
  59. ASSERT(s_the);
  60. return *s_the;
  61. }
  62. WindowManager::WindowManager(const Gfx::PaletteImpl& palette)
  63. : m_palette(palette)
  64. {
  65. s_the = this;
  66. reload_config(false);
  67. invalidate();
  68. Compositor::the().compose();
  69. }
  70. WindowManager::~WindowManager()
  71. {
  72. }
  73. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name, const Gfx::Point& hotspot)
  74. {
  75. auto path = m_wm_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  76. auto gb = Gfx::Bitmap::load_from_file(path);
  77. if (gb)
  78. return Cursor::create(*gb, hotspot);
  79. return Cursor::create(*Gfx::Bitmap::load_from_file("/res/cursors/arrow.png"));
  80. }
  81. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name)
  82. {
  83. auto path = m_wm_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  84. auto gb = Gfx::Bitmap::load_from_file(path);
  85. if (gb)
  86. return Cursor::create(*gb);
  87. return Cursor::create(*Gfx::Bitmap::load_from_file("/res/cursors/arrow.png"));
  88. }
  89. void WindowManager::reload_config(bool set_screen)
  90. {
  91. m_wm_config = Core::ConfigFile::get_for_app("WindowManager");
  92. m_double_click_speed = m_wm_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  93. if (set_screen)
  94. set_resolution(m_wm_config->read_num_entry("Screen", "Width", 1920),
  95. m_wm_config->read_num_entry("Screen", "Height", 1080));
  96. m_arrow_cursor = get_cursor("Arrow", { 2, 2 });
  97. m_hand_cursor = get_cursor("Hand", { 8, 4 });
  98. m_resize_horizontally_cursor = get_cursor("ResizeH");
  99. m_resize_vertically_cursor = get_cursor("ResizeV");
  100. m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
  101. m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
  102. m_i_beam_cursor = get_cursor("IBeam");
  103. m_disallowed_cursor = get_cursor("Disallowed");
  104. m_move_cursor = get_cursor("Move");
  105. m_drag_cursor = get_cursor("Drag");
  106. }
  107. const Gfx::Font& WindowManager::font() const
  108. {
  109. return Gfx::Font::default_font();
  110. }
  111. const Gfx::Font& WindowManager::window_title_font() const
  112. {
  113. return Gfx::Font::default_bold_font();
  114. }
  115. void WindowManager::set_resolution(int width, int height)
  116. {
  117. 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 (m_wm_config) {
  123. dbg() << "Saving resolution: " << Gfx::Size(width, height) << " to config file at " << m_wm_config->file_name();
  124. m_wm_config->write_num_entry("Screen", "Width", width);
  125. m_wm_config->write_num_entry("Screen", "Height", height);
  126. m_wm_config->sync();
  127. }
  128. }
  129. void WindowManager::add_window(Window& window)
  130. {
  131. m_windows_in_order.append(&window);
  132. if (window.is_fullscreen()) {
  133. Core::EventLoop::current().post_event(window, make<ResizeEvent>(window.rect(), Screen::the().rect()));
  134. window.set_rect(Screen::the().rect());
  135. }
  136. set_active_window(&window);
  137. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  138. m_switcher.refresh();
  139. recompute_occlusions();
  140. if (window.listens_to_wm_events()) {
  141. for_each_window([&](Window& other_window) {
  142. if (&window != &other_window) {
  143. tell_wm_listener_about_window(window, other_window);
  144. tell_wm_listener_about_window_icon(window, other_window);
  145. }
  146. return IterationDecision::Continue;
  147. });
  148. }
  149. tell_wm_listeners_window_state_changed(window);
  150. }
  151. void WindowManager::move_to_front_and_make_active(Window& window)
  152. {
  153. if (window.is_blocked_by_modal_window())
  154. return;
  155. if (m_windows_in_order.tail() != &window)
  156. invalidate(window);
  157. m_windows_in_order.remove(&window);
  158. m_windows_in_order.append(&window);
  159. recompute_occlusions();
  160. set_active_window(&window);
  161. if (m_switcher.is_visible()) {
  162. m_switcher.refresh();
  163. m_switcher.select_window(window);
  164. set_highlight_window(&window);
  165. }
  166. }
  167. void WindowManager::remove_window(Window& window)
  168. {
  169. invalidate(window);
  170. m_windows_in_order.remove(&window);
  171. if (window.is_active())
  172. pick_new_active_window();
  173. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  174. m_switcher.refresh();
  175. recompute_occlusions();
  176. for_each_window_listening_to_wm_events([&window](Window& listener) {
  177. if (!(listener.wm_event_mask() & WMEventMask::WindowRemovals))
  178. return IterationDecision::Continue;
  179. if (!window.is_internal())
  180. listener.client()->post_message(Messages::WindowClient::WM_WindowRemoved(listener.window_id(), window.client_id(), window.window_id()));
  181. return IterationDecision::Continue;
  182. });
  183. }
  184. void WindowManager::tell_wm_listener_about_window(Window& listener, Window& window)
  185. {
  186. if (!(listener.wm_event_mask() & WMEventMask::WindowStateChanges))
  187. return;
  188. if (window.is_internal())
  189. return;
  190. listener.client()->post_message(Messages::WindowClient::WM_WindowStateChanged(listener.window_id(), window.client_id(), window.window_id(), window.is_active(), window.is_minimized(), (i32)window.type(), window.title(), window.rect()));
  191. }
  192. void WindowManager::tell_wm_listener_about_window_rect(Window& listener, Window& window)
  193. {
  194. if (!(listener.wm_event_mask() & WMEventMask::WindowRectChanges))
  195. return;
  196. if (window.is_internal())
  197. return;
  198. listener.client()->post_message(Messages::WindowClient::WM_WindowRectChanged(listener.window_id(), window.client_id(), window.window_id(), window.rect()));
  199. }
  200. void WindowManager::tell_wm_listener_about_window_icon(Window& listener, Window& window)
  201. {
  202. if (!(listener.wm_event_mask() & WMEventMask::WindowIconChanges))
  203. return;
  204. if (window.is_internal())
  205. return;
  206. if (window.icon().shared_buffer_id() == -1)
  207. return;
  208. dbg() << "WindowServer: Sharing icon buffer " << window.icon().shared_buffer_id() << " with PID " << listener.client()->client_pid();
  209. if (share_buffer_with(window.icon().shared_buffer_id(), listener.client()->client_pid()) < 0) {
  210. ASSERT_NOT_REACHED();
  211. }
  212. listener.client()->post_message(Messages::WindowClient::WM_WindowIconBitmapChanged(listener.window_id(), window.client_id(), window.window_id(), window.icon().shared_buffer_id(), window.icon().size()));
  213. }
  214. void WindowManager::tell_wm_listeners_window_state_changed(Window& window)
  215. {
  216. for_each_window_listening_to_wm_events([&](Window& listener) {
  217. tell_wm_listener_about_window(listener, window);
  218. return IterationDecision::Continue;
  219. });
  220. }
  221. void WindowManager::tell_wm_listeners_window_icon_changed(Window& window)
  222. {
  223. for_each_window_listening_to_wm_events([&](Window& listener) {
  224. tell_wm_listener_about_window_icon(listener, window);
  225. return IterationDecision::Continue;
  226. });
  227. }
  228. void WindowManager::tell_wm_listeners_window_rect_changed(Window& window)
  229. {
  230. for_each_window_listening_to_wm_events([&](Window& listener) {
  231. tell_wm_listener_about_window_rect(listener, window);
  232. return IterationDecision::Continue;
  233. });
  234. }
  235. void WindowManager::notify_title_changed(Window& window)
  236. {
  237. if (window.type() != WindowType::Normal)
  238. return;
  239. dbg() << "[WM] Window{" << &window << "} title set to \"" << window.title() << '"';
  240. invalidate(window.frame().rect());
  241. if (m_switcher.is_visible())
  242. m_switcher.refresh();
  243. tell_wm_listeners_window_state_changed(window);
  244. }
  245. void WindowManager::notify_rect_changed(Window& window, const Gfx::Rect& old_rect, const Gfx::Rect& new_rect)
  246. {
  247. UNUSED_PARAM(old_rect);
  248. UNUSED_PARAM(new_rect);
  249. #ifdef RESIZE_DEBUG
  250. dbg() << "[WM] Window " << &window << " rect changed " << old_rect << " -> " << new_rect;
  251. #endif
  252. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  253. m_switcher.refresh();
  254. recompute_occlusions();
  255. tell_wm_listeners_window_rect_changed(window);
  256. MenuManager::the().refresh();
  257. }
  258. void WindowManager::recompute_occlusions()
  259. {
  260. for_each_visible_window_from_back_to_front([&](Window& window) {
  261. if (m_switcher.is_visible()) {
  262. window.set_occluded(false);
  263. } else {
  264. if (any_opaque_window_above_this_one_contains_rect(window, window.frame().rect()))
  265. window.set_occluded(true);
  266. else
  267. window.set_occluded(false);
  268. }
  269. return IterationDecision::Continue;
  270. });
  271. }
  272. void WindowManager::notify_opacity_changed(Window&)
  273. {
  274. recompute_occlusions();
  275. }
  276. void WindowManager::notify_minimization_state_changed(Window& window)
  277. {
  278. tell_wm_listeners_window_state_changed(window);
  279. if (window.client())
  280. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  281. if (window.is_active() && window.is_minimized())
  282. pick_new_active_window();
  283. }
  284. void WindowManager::notify_occlusion_state_changed(Window& window)
  285. {
  286. if (window.client())
  287. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  288. }
  289. void WindowManager::pick_new_active_window()
  290. {
  291. bool new_window_picked = false;
  292. for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& candidate) {
  293. set_active_window(&candidate);
  294. new_window_picked = true;
  295. return IterationDecision::Break;
  296. });
  297. if (!new_window_picked)
  298. set_active_window(nullptr);
  299. }
  300. void WindowManager::start_window_move(Window& window, const MouseEvent& event)
  301. {
  302. #ifdef MOVE_DEBUG
  303. dbg() << "[WM] Begin moving Window{" << &window << "}";
  304. #endif
  305. move_to_front_and_make_active(window);
  306. m_move_window = window.make_weak_ptr();
  307. m_move_origin = event.position();
  308. m_move_window_origin = window.position();
  309. invalidate(window);
  310. }
  311. void WindowManager::start_window_resize(Window& window, const Gfx::Point& position, MouseButton button)
  312. {
  313. move_to_front_and_make_active(window);
  314. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  315. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  316. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  317. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  318. };
  319. Gfx::Rect outer_rect = window.frame().rect();
  320. ASSERT(outer_rect.contains(position));
  321. int window_relative_x = position.x() - outer_rect.x();
  322. int window_relative_y = position.y() - outer_rect.y();
  323. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  324. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  325. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  326. if (m_resize_direction == ResizeDirection::None) {
  327. ASSERT(!m_resize_window);
  328. return;
  329. }
  330. #ifdef RESIZE_DEBUG
  331. dbg() << "[WM] Begin resizing Window{" << &window << "}";
  332. #endif
  333. m_resizing_mouse_button = button;
  334. m_resize_window = window.make_weak_ptr();
  335. ;
  336. m_resize_origin = position;
  337. m_resize_window_original_rect = window.rect();
  338. invalidate(window);
  339. }
  340. void WindowManager::start_window_resize(Window& window, const MouseEvent& event)
  341. {
  342. start_window_resize(window, event.position(), event.button());
  343. }
  344. bool WindowManager::process_ongoing_window_move(MouseEvent& event, Window*& hovered_window)
  345. {
  346. if (!m_move_window)
  347. return false;
  348. if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  349. #ifdef MOVE_DEBUG
  350. dbg() << "[WM] Finish moving Window{" << m_move_window << "}";
  351. #endif
  352. invalidate(*m_move_window);
  353. if (m_move_window->rect().contains(event.position()))
  354. hovered_window = m_move_window;
  355. if (m_move_window->is_resizable()) {
  356. process_event_for_doubleclick(*m_move_window, event);
  357. if (event.type() == Event::MouseDoubleClick) {
  358. #if defined(DOUBLECLICK_DEBUG)
  359. dbg() << "[WM] Click up became doubleclick!";
  360. #endif
  361. m_move_window->set_maximized(!m_move_window->is_maximized());
  362. }
  363. }
  364. m_move_window = nullptr;
  365. return true;
  366. }
  367. if (event.type() == Event::MouseMove) {
  368. #ifdef MOVE_DEBUG
  369. dbg() << "[WM] Moving, origin: " << m_move_origin << ", now: " << event.position();
  370. if (m_move_window->is_maximized()) {
  371. dbg() << " [!] The window is still maximized. Not moving yet.";
  372. }
  373. #endif
  374. const int maximization_deadzone = 2;
  375. if (m_move_window->is_maximized()) {
  376. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  377. // dbg() << "[WM] " << pixels_moved_from_start << " moved since start of window move";
  378. if (pixels_moved_from_start > 5) {
  379. // dbg() << "[WM] de-maximizing window";
  380. m_move_origin = event.position();
  381. if (m_move_origin.y() <= maximization_deadzone)
  382. return true;
  383. auto width_before_resize = m_move_window->width();
  384. m_move_window->set_maximized(false);
  385. 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());
  386. m_move_window_origin = m_move_window->position();
  387. }
  388. } else {
  389. bool is_resizable = m_move_window->is_resizable();
  390. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  391. const int tiling_deadzone = 5;
  392. if (is_resizable && event.y() <= maximization_deadzone) {
  393. m_move_window->set_tiled(WindowTileType::None);
  394. m_move_window->set_maximized(true);
  395. return true;
  396. }
  397. if (is_resizable && event.x() <= tiling_deadzone) {
  398. m_move_window->set_tiled(WindowTileType::Left);
  399. } else if (is_resizable && event.x() >= Screen::the().width() - tiling_deadzone) {
  400. m_move_window->set_tiled(WindowTileType::Right);
  401. } else if (pixels_moved_from_start > 5 || m_move_window->tiled() == WindowTileType::None) {
  402. m_move_window->set_tiled(WindowTileType::None);
  403. Gfx::Point pos = m_move_window_origin.translated(event.position() - m_move_origin);
  404. m_move_window->set_position_without_repaint(pos);
  405. if (m_move_window->rect().contains(event.position()))
  406. hovered_window = m_move_window;
  407. }
  408. return true;
  409. }
  410. }
  411. return false;
  412. }
  413. bool WindowManager::process_ongoing_window_resize(const MouseEvent& event, Window*& hovered_window)
  414. {
  415. if (!m_resize_window)
  416. return false;
  417. if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
  418. #ifdef RESIZE_DEBUG
  419. dbg() << "[WM] Finish resizing Window{" << m_resize_window << "}";
  420. #endif
  421. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  422. invalidate(*m_resize_window);
  423. if (m_resize_window->rect().contains(event.position()))
  424. hovered_window = m_resize_window;
  425. m_resize_window = nullptr;
  426. m_resizing_mouse_button = MouseButton::None;
  427. return true;
  428. }
  429. if (event.type() != Event::MouseMove)
  430. return false;
  431. auto old_rect = m_resize_window->rect();
  432. int diff_x = event.x() - m_resize_origin.x();
  433. int diff_y = event.y() - m_resize_origin.y();
  434. int change_w = 0;
  435. int change_h = 0;
  436. switch (m_resize_direction) {
  437. case ResizeDirection::DownRight:
  438. change_w = diff_x;
  439. change_h = diff_y;
  440. break;
  441. case ResizeDirection::Right:
  442. change_w = diff_x;
  443. break;
  444. case ResizeDirection::UpRight:
  445. change_w = diff_x;
  446. change_h = -diff_y;
  447. break;
  448. case ResizeDirection::Up:
  449. change_h = -diff_y;
  450. break;
  451. case ResizeDirection::UpLeft:
  452. change_w = -diff_x;
  453. change_h = -diff_y;
  454. break;
  455. case ResizeDirection::Left:
  456. change_w = -diff_x;
  457. break;
  458. case ResizeDirection::DownLeft:
  459. change_w = -diff_x;
  460. change_h = diff_y;
  461. break;
  462. case ResizeDirection::Down:
  463. change_h = diff_y;
  464. break;
  465. default:
  466. ASSERT_NOT_REACHED();
  467. }
  468. auto new_rect = m_resize_window_original_rect;
  469. // First, size the new rect.
  470. Gfx::Size minimum_size { 50, 50 };
  471. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  472. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  473. if (!m_resize_window->size_increment().is_null()) {
  474. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  475. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  476. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  477. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  478. }
  479. // Second, set its position so that the sides of the window
  480. // that end up moving are the same ones as the user is dragging,
  481. // no matter which part of the logic above caused us to decide
  482. // to resize by this much.
  483. switch (m_resize_direction) {
  484. case ResizeDirection::DownRight:
  485. case ResizeDirection::Right:
  486. case ResizeDirection::Down:
  487. break;
  488. case ResizeDirection::Left:
  489. case ResizeDirection::Up:
  490. case ResizeDirection::UpLeft:
  491. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  492. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  493. break;
  494. case ResizeDirection::UpRight:
  495. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  496. break;
  497. case ResizeDirection::DownLeft:
  498. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  499. break;
  500. default:
  501. ASSERT_NOT_REACHED();
  502. }
  503. if (new_rect.contains(event.position()))
  504. hovered_window = m_resize_window;
  505. if (m_resize_window->rect() == new_rect)
  506. return true;
  507. #ifdef RESIZE_DEBUG
  508. dbg() << "[WM] Resizing, original: " << m_resize_window_original_rect << ", now: " << new_rect;
  509. #endif
  510. m_resize_window->set_rect(new_rect);
  511. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(old_rect, new_rect));
  512. return true;
  513. }
  514. bool WindowManager::process_ongoing_drag(MouseEvent& event, Window*& hovered_window)
  515. {
  516. if (!m_dnd_client)
  517. return false;
  518. if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Left))
  519. return true;
  520. hovered_window = nullptr;
  521. for_each_visible_window_from_front_to_back([&](auto& window) {
  522. if (window.frame().rect().contains(event.position())) {
  523. hovered_window = &window;
  524. return IterationDecision::Break;
  525. }
  526. return IterationDecision::Continue;
  527. });
  528. if (hovered_window) {
  529. m_dnd_client->post_message(Messages::WindowClient::DragAccepted());
  530. if (hovered_window->client()) {
  531. auto translated_event = event.translated(-hovered_window->position());
  532. hovered_window->client()->post_message(Messages::WindowClient::DragDropped(hovered_window->window_id(), translated_event.position(), m_dnd_text, m_dnd_data_type, m_dnd_data));
  533. }
  534. } else {
  535. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  536. }
  537. end_dnd_drag();
  538. return true;
  539. }
  540. void WindowManager::set_cursor_tracking_button(Button* button)
  541. {
  542. m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
  543. }
  544. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
  545. {
  546. switch (button) {
  547. case MouseButton::Left:
  548. return m_left;
  549. case MouseButton::Right:
  550. return m_right;
  551. case MouseButton::Middle:
  552. return m_middle;
  553. default:
  554. ASSERT_NOT_REACHED();
  555. }
  556. }
  557. // #define DOUBLECLICK_DEBUG
  558. void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
  559. {
  560. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  561. ASSERT(event.type() == Event::MouseUp);
  562. if (&window != m_double_click_info.m_clicked_window) {
  563. // we either haven't clicked anywhere, or we haven't clicked on this
  564. // window. set the current click window, and reset the timers.
  565. #if defined(DOUBLECLICK_DEBUG)
  566. dbg() << "Initial mouseup on window " << &window << " (previous was " << m_double_click_info.m_clicked_window << ')';
  567. #endif
  568. m_double_click_info.m_clicked_window = window.make_weak_ptr();
  569. m_double_click_info.reset();
  570. }
  571. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  572. // if the clock is invalid, we haven't clicked with this button on this
  573. // window yet, so there's nothing to do.
  574. if (!metadata.clock.is_valid()) {
  575. metadata.clock.start();
  576. } else {
  577. int elapsed_since_last_click = metadata.clock.elapsed();
  578. metadata.clock.start();
  579. if (elapsed_since_last_click < m_double_click_speed) {
  580. auto diff = event.position() - metadata.last_position;
  581. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  582. if (distance_travelled_squared > (m_max_distance_for_double_click * m_max_distance_for_double_click)) {
  583. // too far; try again
  584. metadata.clock.start();
  585. } else {
  586. #if defined(DOUBLECLICK_DEBUG)
  587. dbg() << "Transforming MouseUp to MouseDoubleClick (" << elapsed_since_last_click << " < " << m_double_click_speed << ")!";
  588. #endif
  589. event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  590. // invalidate this now we've delivered a doubleclick, otherwise
  591. // tripleclick will deliver two doubleclick events (incorrectly).
  592. metadata.clock = {};
  593. }
  594. } else {
  595. // too slow; try again
  596. metadata.clock.start();
  597. }
  598. }
  599. metadata.last_position = event.position();
  600. }
  601. void WindowManager::deliver_mouse_event(Window& window, MouseEvent& event)
  602. {
  603. window.dispatch_event(event);
  604. if (event.type() == Event::MouseUp) {
  605. process_event_for_doubleclick(window, event);
  606. if (event.type() == Event::MouseDoubleClick)
  607. window.dispatch_event(event);
  608. }
  609. }
  610. void WindowManager::process_mouse_event(MouseEvent& event, Window*& hovered_window)
  611. {
  612. hovered_window = nullptr;
  613. if (process_ongoing_drag(event, hovered_window))
  614. return;
  615. if (process_ongoing_window_move(event, hovered_window))
  616. return;
  617. if (process_ongoing_window_resize(event, hovered_window))
  618. return;
  619. if (m_cursor_tracking_button)
  620. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  621. // This is quite hackish, but it's how the Button hover effect is implemented.
  622. if (m_hovered_button && event.type() == Event::MouseMove)
  623. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  624. HashTable<Window*> windows_who_received_mouse_event_due_to_cursor_tracking;
  625. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  626. if (!window->global_cursor_tracking())
  627. continue;
  628. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  629. ASSERT(!window->is_minimized()); // Maybe this should also be supported? Idk.
  630. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  631. auto translated_event = event.translated(-window->position());
  632. deliver_mouse_event(*window, translated_event);
  633. }
  634. // FIXME: Now that the menubar has a dedicated window, is this special-casing really necessary?
  635. if (!active_window_is_modal() && menubar_rect().contains(event.position())) {
  636. MenuManager::the().dispatch_event(event);
  637. return;
  638. }
  639. if (!MenuManager::the().open_menu_stack().is_empty()) {
  640. auto* topmost_menu = MenuManager::the().open_menu_stack().last().ptr();
  641. ASSERT(topmost_menu);
  642. auto* window = topmost_menu->menu_window();
  643. ASSERT(window);
  644. bool event_is_inside_current_menu = window->rect().contains(event.position());
  645. if (event_is_inside_current_menu) {
  646. hovered_window = window;
  647. auto translated_event = event.translated(-window->position());
  648. deliver_mouse_event(*window, translated_event);
  649. return;
  650. }
  651. if (topmost_menu->hovered_item())
  652. topmost_menu->clear_hovered_item();
  653. if (event.type() == Event::MouseDown || event.type() == Event::MouseUp) {
  654. auto* window_menu_of = topmost_menu->window_menu_of();
  655. if (window_menu_of) {
  656. bool event_is_inside_taskbar_button = window_menu_of->taskbar_rect().contains(event.position());
  657. if (event_is_inside_taskbar_button && !topmost_menu->is_window_menu_open()) {
  658. topmost_menu->set_window_menu_open(true);
  659. return;
  660. }
  661. }
  662. if (event.type() == Event::MouseDown) {
  663. MenuManager::the().close_bar();
  664. topmost_menu->set_window_menu_open(false);
  665. }
  666. }
  667. if (event.type() == Event::MouseMove) {
  668. for (auto& menu : MenuManager::the().open_menu_stack()) {
  669. if (!menu)
  670. continue;
  671. if (!menu->menu_window()->rect().contains(event.position()))
  672. continue;
  673. hovered_window = menu->menu_window();
  674. auto translated_event = event.translated(-menu->menu_window()->position());
  675. deliver_mouse_event(*menu->menu_window(), translated_event);
  676. break;
  677. }
  678. }
  679. return;
  680. }
  681. Window* event_window_with_frame = nullptr;
  682. if (m_active_input_window) {
  683. // At this point, we have delivered the start of an input sequence to a
  684. // client application. We must keep delivering to that client
  685. // application until the input sequence is done.
  686. //
  687. // This prevents e.g. moving on one window out of the bounds starting
  688. // a move in that other unrelated window, and other silly shenanigans.
  689. if (!windows_who_received_mouse_event_due_to_cursor_tracking.contains(m_active_input_window)) {
  690. auto translated_event = event.translated(-m_active_input_window->position());
  691. deliver_mouse_event(*m_active_input_window, translated_event);
  692. windows_who_received_mouse_event_due_to_cursor_tracking.set(m_active_input_window.ptr());
  693. }
  694. if (event.type() == Event::MouseUp && event.buttons() == 0) {
  695. m_active_input_window = nullptr;
  696. }
  697. for_each_visible_window_from_front_to_back([&](auto& window) {
  698. if (window.frame().rect().contains(event.position())) {
  699. hovered_window = &window;
  700. return IterationDecision::Break;
  701. }
  702. return IterationDecision::Continue;
  703. });
  704. } else {
  705. for_each_visible_window_from_front_to_back([&](Window& window) {
  706. auto window_frame_rect = window.frame().rect();
  707. if (!window_frame_rect.contains(event.position()))
  708. return IterationDecision::Continue;
  709. if (&window != m_resize_candidate.ptr())
  710. clear_resize_candidate();
  711. // First check if we should initiate a move or resize (Logo+LMB or Logo+RMB).
  712. // In those cases, the event is swallowed by the window manager.
  713. if (window.is_movable()) {
  714. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
  715. hovered_window = &window;
  716. start_window_move(window, event);
  717. m_moved_or_resized_since_logo_keydown = true;
  718. return IterationDecision::Break;
  719. }
  720. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  721. hovered_window = &window;
  722. start_window_resize(window, event);
  723. m_moved_or_resized_since_logo_keydown = true;
  724. return IterationDecision::Break;
  725. }
  726. }
  727. if (m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseWheel) {
  728. float opacity_change = -event.wheel_delta() * 0.05f;
  729. float new_opacity = window.opacity() + opacity_change;
  730. if (new_opacity < 0.05f)
  731. new_opacity = 0.05f;
  732. if (new_opacity > 1.0f)
  733. new_opacity = 1.0f;
  734. window.set_opacity(new_opacity);
  735. window.invalidate();
  736. return IterationDecision::Break;
  737. }
  738. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  739. if (window.rect().contains(event.position())) {
  740. if (window.type() == WindowType::Normal && event.type() == Event::MouseDown)
  741. move_to_front_and_make_active(window);
  742. hovered_window = &window;
  743. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window)) {
  744. auto translated_event = event.translated(-window.position());
  745. deliver_mouse_event(window, translated_event);
  746. if (event.type() == Event::MouseDown) {
  747. m_active_input_window = window.make_weak_ptr();
  748. }
  749. }
  750. return IterationDecision::Break;
  751. }
  752. // We are hitting the frame, pass the event along to WindowFrame.
  753. window.frame().on_mouse_event(event.translated(-window_frame_rect.location()));
  754. event_window_with_frame = &window;
  755. return IterationDecision::Break;
  756. });
  757. // Clicked outside of any window
  758. if (!hovered_window && !event_window_with_frame && event.type() == Event::MouseDown)
  759. set_active_window(nullptr);
  760. }
  761. if (event_window_with_frame != m_resize_candidate.ptr())
  762. clear_resize_candidate();
  763. }
  764. void WindowManager::clear_resize_candidate()
  765. {
  766. if (m_resize_candidate)
  767. Compositor::the().invalidate_cursor();
  768. m_resize_candidate = nullptr;
  769. }
  770. bool WindowManager::any_opaque_window_contains_rect(const Gfx::Rect& rect)
  771. {
  772. bool found_containing_window = false;
  773. for_each_visible_window_from_back_to_front([&](Window& window) {
  774. if (window.is_minimized())
  775. return IterationDecision::Continue;
  776. if (window.opacity() < 1.0f)
  777. return IterationDecision::Continue;
  778. if (window.has_alpha_channel()) {
  779. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  780. // Maybe there's some way we could know this?
  781. return IterationDecision::Continue;
  782. }
  783. if (window.frame().rect().contains(rect)) {
  784. found_containing_window = true;
  785. return IterationDecision::Break;
  786. }
  787. return IterationDecision::Continue;
  788. });
  789. return found_containing_window;
  790. };
  791. bool WindowManager::any_opaque_window_above_this_one_contains_rect(const Window& a_window, const Gfx::Rect& rect)
  792. {
  793. bool found_containing_window = false;
  794. bool checking = false;
  795. for_each_visible_window_from_back_to_front([&](Window& window) {
  796. if (&window == &a_window) {
  797. checking = true;
  798. return IterationDecision::Continue;
  799. }
  800. if (!checking)
  801. return IterationDecision::Continue;
  802. if (!window.is_visible())
  803. return IterationDecision::Continue;
  804. if (window.is_minimized())
  805. return IterationDecision::Continue;
  806. if (window.opacity() < 1.0f)
  807. return IterationDecision::Continue;
  808. if (window.has_alpha_channel())
  809. return IterationDecision::Continue;
  810. if (window.frame().rect().contains(rect)) {
  811. found_containing_window = true;
  812. return IterationDecision::Break;
  813. }
  814. return IterationDecision::Continue;
  815. });
  816. return found_containing_window;
  817. };
  818. Gfx::Rect WindowManager::menubar_rect() const
  819. {
  820. if (active_fullscreen_window())
  821. return {};
  822. return MenuManager::the().menubar_rect();
  823. }
  824. void WindowManager::draw_window_switcher()
  825. {
  826. if (m_switcher.is_visible())
  827. m_switcher.draw();
  828. }
  829. void WindowManager::event(Core::Event& event)
  830. {
  831. if (static_cast<Event&>(event).is_mouse_event()) {
  832. Window* hovered_window = nullptr;
  833. process_mouse_event(static_cast<MouseEvent&>(event), hovered_window);
  834. set_hovered_window(hovered_window);
  835. return;
  836. }
  837. if (static_cast<Event&>(event).is_key_event()) {
  838. auto& key_event = static_cast<const KeyEvent&>(event);
  839. m_keyboard_modifiers = key_event.modifiers();
  840. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
  841. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  842. end_dnd_drag();
  843. return;
  844. }
  845. if (key_event.key() == Key_Logo) {
  846. if (key_event.type() == Event::KeyUp) {
  847. if (!m_moved_or_resized_since_logo_keydown && !m_switcher.is_visible() && !m_move_window && !m_resize_window) {
  848. MenuManager::the().toggle_menu(MenuManager::the().system_menu());
  849. return;
  850. }
  851. } else if (key_event.type() == Event::KeyDown) {
  852. m_moved_or_resized_since_logo_keydown = false;
  853. }
  854. }
  855. if (MenuManager::the().current_menu()) {
  856. MenuManager::the().dispatch_event(event);
  857. return;
  858. }
  859. 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)))
  860. m_switcher.show();
  861. if (m_switcher.is_visible()) {
  862. m_switcher.on_key_event(key_event);
  863. return;
  864. }
  865. if (m_active_window) {
  866. if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Logo) {
  867. if (key_event.key() == Key_Down) {
  868. m_moved_or_resized_since_logo_keydown = true;
  869. if (m_active_window->is_resizable() && m_active_window->is_maximized()) {
  870. m_active_window->set_maximized(false);
  871. return;
  872. }
  873. if (m_active_window->is_minimizable())
  874. m_active_window->set_minimized(true);
  875. return;
  876. }
  877. if (m_active_window->is_resizable()) {
  878. if (key_event.key() == Key_Up) {
  879. m_moved_or_resized_since_logo_keydown = true;
  880. m_active_window->set_maximized(!m_active_window->is_maximized());
  881. return;
  882. }
  883. if (key_event.key() == Key_Left) {
  884. m_moved_or_resized_since_logo_keydown = true;
  885. if (m_active_window->tiled() != WindowTileType::None) {
  886. m_active_window->set_tiled(WindowTileType::None);
  887. return;
  888. }
  889. if (m_active_window->is_maximized())
  890. m_active_window->set_maximized(false);
  891. m_active_window->set_tiled(WindowTileType::Left);
  892. return;
  893. }
  894. if (key_event.key() == Key_Right) {
  895. m_moved_or_resized_since_logo_keydown = true;
  896. if (m_active_window->tiled() != WindowTileType::None) {
  897. m_active_window->set_tiled(WindowTileType::None);
  898. return;
  899. }
  900. if (m_active_window->is_maximized())
  901. m_active_window->set_maximized(false);
  902. m_active_window->set_tiled(WindowTileType::Right);
  903. return;
  904. }
  905. }
  906. }
  907. m_active_window->dispatch_event(event);
  908. return;
  909. }
  910. }
  911. Core::Object::event(event);
  912. }
  913. void WindowManager::set_highlight_window(Window* window)
  914. {
  915. if (window == m_highlight_window)
  916. return;
  917. if (auto* previous_highlight_window = m_highlight_window.ptr())
  918. invalidate(*previous_highlight_window);
  919. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  920. if (m_highlight_window)
  921. invalidate(*m_highlight_window);
  922. }
  923. void WindowManager::set_active_window(Window* window)
  924. {
  925. if (window && window->is_blocked_by_modal_window())
  926. return;
  927. if (window && window->type() != WindowType::Normal)
  928. return;
  929. if (window == m_active_window)
  930. return;
  931. auto* previously_active_window = m_active_window.ptr();
  932. ClientConnection* previously_active_client = nullptr;
  933. ClientConnection* active_client = nullptr;
  934. if (previously_active_window) {
  935. previously_active_client = previously_active_window->client();
  936. Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
  937. invalidate(*previously_active_window);
  938. m_active_window = nullptr;
  939. tell_wm_listeners_window_state_changed(*previously_active_window);
  940. }
  941. if (window) {
  942. m_active_window = window->make_weak_ptr();
  943. active_client = m_active_window->client();
  944. Core::EventLoop::current().post_event(*m_active_window, make<Event>(Event::WindowActivated));
  945. invalidate(*m_active_window);
  946. auto* client = window->client();
  947. ASSERT(client);
  948. MenuManager::the().set_current_menubar(client->app_menubar());
  949. tell_wm_listeners_window_state_changed(*m_active_window);
  950. } else {
  951. MenuManager::the().set_current_menubar(nullptr);
  952. }
  953. if (active_client != previously_active_client) {
  954. if (previously_active_client)
  955. previously_active_client->deboost();
  956. if (active_client)
  957. active_client->boost();
  958. }
  959. }
  960. void WindowManager::set_hovered_window(Window* window)
  961. {
  962. if (m_hovered_window == window)
  963. return;
  964. if (m_hovered_window)
  965. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
  966. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  967. if (m_hovered_window)
  968. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
  969. }
  970. void WindowManager::invalidate()
  971. {
  972. Compositor::the().invalidate();
  973. }
  974. void WindowManager::invalidate(const Gfx::Rect& rect)
  975. {
  976. Compositor::the().invalidate(rect);
  977. }
  978. void WindowManager::invalidate(const Window& window)
  979. {
  980. invalidate(window.frame().rect());
  981. }
  982. void WindowManager::invalidate(const Window& window, const Gfx::Rect& rect)
  983. {
  984. if (window.type() == WindowType::MenuApplet) {
  985. AppletManager::the().invalidate_applet(window, rect);
  986. return;
  987. }
  988. if (rect.is_empty()) {
  989. invalidate(window);
  990. return;
  991. }
  992. auto outer_rect = window.frame().rect();
  993. auto inner_rect = rect;
  994. inner_rect.move_by(window.position());
  995. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  996. inner_rect.intersect(outer_rect);
  997. invalidate(inner_rect);
  998. }
  999. const ClientConnection* WindowManager::active_client() const
  1000. {
  1001. if (m_active_window)
  1002. return m_active_window->client();
  1003. return nullptr;
  1004. }
  1005. void WindowManager::notify_client_changed_app_menubar(ClientConnection& client)
  1006. {
  1007. if (active_client() == &client)
  1008. MenuManager::the().set_current_menubar(client.app_menubar());
  1009. }
  1010. const Cursor& WindowManager::active_cursor() const
  1011. {
  1012. if (m_dnd_client)
  1013. return *m_drag_cursor;
  1014. if (m_move_window)
  1015. return *m_move_cursor;
  1016. if (m_resize_window || m_resize_candidate) {
  1017. switch (m_resize_direction) {
  1018. case ResizeDirection::Up:
  1019. case ResizeDirection::Down:
  1020. return *m_resize_vertically_cursor;
  1021. case ResizeDirection::Left:
  1022. case ResizeDirection::Right:
  1023. return *m_resize_horizontally_cursor;
  1024. case ResizeDirection::UpLeft:
  1025. case ResizeDirection::DownRight:
  1026. return *m_resize_diagonally_tlbr_cursor;
  1027. case ResizeDirection::UpRight:
  1028. case ResizeDirection::DownLeft:
  1029. return *m_resize_diagonally_bltr_cursor;
  1030. case ResizeDirection::None:
  1031. break;
  1032. }
  1033. }
  1034. if (m_hovered_window && m_hovered_window->override_cursor())
  1035. return *m_hovered_window->override_cursor();
  1036. return *m_arrow_cursor;
  1037. }
  1038. void WindowManager::set_hovered_button(Button* button)
  1039. {
  1040. m_hovered_button = button ? button->make_weak_ptr() : nullptr;
  1041. }
  1042. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1043. {
  1044. m_resize_candidate = window.make_weak_ptr();
  1045. m_resize_direction = direction;
  1046. }
  1047. ResizeDirection WindowManager::resize_direction_of_window(const Window& window)
  1048. {
  1049. if (&window != m_resize_window)
  1050. return ResizeDirection::None;
  1051. return m_resize_direction;
  1052. }
  1053. Gfx::Rect WindowManager::maximized_window_rect(const Window& window) const
  1054. {
  1055. Gfx::Rect rect = Screen::the().rect();
  1056. // Subtract window title bar (leaving the border)
  1057. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  1058. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  1059. // Subtract menu bar
  1060. rect.set_y(rect.y() + menubar_rect().height());
  1061. rect.set_height(rect.height() - menubar_rect().height());
  1062. // Subtract taskbar window height if present
  1063. const_cast<WindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
  1064. rect.set_height(rect.height() - taskbar_window.height());
  1065. return IterationDecision::Break;
  1066. });
  1067. return rect;
  1068. }
  1069. void WindowManager::start_dnd_drag(ClientConnection& client, const String& text, Gfx::Bitmap* bitmap, const String& data_type, const String& data)
  1070. {
  1071. ASSERT(!m_dnd_client);
  1072. m_dnd_client = client.make_weak_ptr();
  1073. m_dnd_text = text;
  1074. m_dnd_bitmap = bitmap;
  1075. m_dnd_data_type = data_type;
  1076. m_dnd_data = data;
  1077. Compositor::the().invalidate_cursor();
  1078. m_active_input_window = nullptr;
  1079. }
  1080. void WindowManager::end_dnd_drag()
  1081. {
  1082. ASSERT(m_dnd_client);
  1083. Compositor::the().invalidate_cursor();
  1084. m_dnd_client = nullptr;
  1085. m_dnd_text = {};
  1086. m_dnd_bitmap = nullptr;
  1087. }
  1088. Gfx::Rect WindowManager::dnd_rect() const
  1089. {
  1090. int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
  1091. int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
  1092. int width = font().width(m_dnd_text) + bitmap_width;
  1093. int height = max((int)font().glyph_height(), bitmap_height);
  1094. auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
  1095. return Gfx::Rect(location, { width, height }).inflated(4, 4);
  1096. }
  1097. void WindowManager::update_theme(String theme_path, String theme_name)
  1098. {
  1099. auto new_theme = Gfx::load_system_theme(theme_path);
  1100. ASSERT(new_theme);
  1101. Gfx::set_system_theme(*new_theme);
  1102. m_palette = Gfx::PaletteImpl::create_with_shared_buffer(*new_theme);
  1103. HashTable<ClientConnection*> notified_clients;
  1104. for_each_window([&](Window& window) {
  1105. if (window.client()) {
  1106. if (!notified_clients.contains(window.client())) {
  1107. window.client()->post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  1108. notified_clients.set(window.client());
  1109. }
  1110. }
  1111. return IterationDecision::Continue;
  1112. });
  1113. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  1114. wm_config->write_entry("Theme", "Name", theme_name);
  1115. wm_config->sync();
  1116. invalidate();
  1117. }
  1118. }