WSWindowManager.cpp 45 KB

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