WSWindowManager.cpp 45 KB

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