WSWindowManager.cpp 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168
  1. #include "WSWindowManager.h"
  2. #include "WSWindow.h"
  3. #include "WSScreen.h"
  4. #include "WSMessageLoop.h"
  5. #include <SharedGraphics/Font.h>
  6. #include <SharedGraphics/Painter.h>
  7. #include <SharedGraphics/CharacterBitmap.h>
  8. #include <AK/StdLibExtras.h>
  9. #include <errno.h>
  10. #include "WSMenu.h"
  11. #include "WSMenuBar.h"
  12. #include "WSMenuItem.h"
  13. #include <WindowServer/WSClientConnection.h>
  14. #include <unistd.h>
  15. #include <stdio.h>
  16. #include <time.h>
  17. #include <SharedGraphics/StylePainter.h>
  18. #include <SharedGraphics/PNGLoader.h>
  19. #include <WindowServer/WSCursor.h>
  20. #include <WindowServer/WSButton.h>
  21. //#define DEBUG_COUNTERS
  22. //#define RESIZE_DEBUG
  23. static void get_cpu_usage(unsigned& busy, unsigned& idle);
  24. static WSWindowManager* s_the;
  25. WSWindowManager& WSWindowManager::the()
  26. {
  27. ASSERT(s_the);
  28. return *s_the;
  29. }
  30. void WSWindowManager::flip_buffers()
  31. {
  32. swap(m_front_bitmap, m_back_bitmap);
  33. swap(m_front_painter, m_back_painter);
  34. int new_y_offset = m_buffers_are_flipped ? 0 : m_screen_rect.height();
  35. WSScreen::the().set_y_offset(new_y_offset);
  36. m_buffers_are_flipped = !m_buffers_are_flipped;
  37. }
  38. WSWindowManager::WSWindowManager()
  39. : m_screen(WSScreen::the())
  40. , m_screen_rect(m_screen.rect())
  41. , m_flash_flush(false)
  42. {
  43. s_the = this;
  44. #ifndef DEBUG_COUNTERS
  45. (void)m_compose_count;
  46. (void)m_flush_count;
  47. #endif
  48. auto size = m_screen_rect.size();
  49. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(0));
  50. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, size, m_screen.scanline(size.height()));
  51. m_front_painter = make<Painter>(*m_front_bitmap);
  52. m_back_painter = make<Painter>(*m_back_bitmap);
  53. m_front_painter->set_font(font());
  54. m_back_painter->set_font(font());
  55. m_background_color = Color(50, 50, 50);
  56. m_active_window_border_color = Color(110, 34, 9);
  57. m_active_window_border_color2 = Color(244, 202, 158);
  58. m_active_window_title_color = Color::White;
  59. m_inactive_window_border_color = Color(128, 128, 128);
  60. m_inactive_window_border_color2 = Color(192, 192, 192);
  61. m_inactive_window_title_color = Color(213, 208, 199);
  62. m_dragging_window_border_color = Color(161, 50, 13);
  63. m_dragging_window_border_color2 = Color(250, 220, 187);
  64. m_dragging_window_title_color = Color::White;
  65. m_highlight_window_border_color = Color::from_rgb(0xa10d0d);
  66. m_highlight_window_border_color2 = Color::from_rgb(0xfabbbb);
  67. m_highlight_window_title_color = Color::White;
  68. m_arrow_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/arrow.png"), { 2, 2 });
  69. m_resize_horizontally_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/resize-horizontal.png"));
  70. m_resize_vertically_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/resize-vertical.png"));
  71. m_resize_diagonally_tlbr_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/resize-diagonal-tlbr.png"));
  72. m_resize_diagonally_bltr_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/resize-diagonal-bltr.png"));
  73. m_i_beam_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/i-beam.png"));
  74. m_disallowed_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/disallowed.png"));
  75. m_move_cursor = WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/move.png"));
  76. m_wallpaper_path = "/res/wallpapers/retro.rgb";
  77. m_wallpaper = GraphicsBitmap::load_from_file(GraphicsBitmap::Format::RGBA32, m_wallpaper_path, { 1024, 768 });
  78. m_username = getlogin();
  79. m_menu_selection_color = Color::from_rgb(0x84351a);
  80. {
  81. byte system_menu_name[] = { 0xf8, 0 };
  82. m_system_menu = make<WSMenu>(nullptr, -1, String((const char*)system_menu_name));
  83. m_system_menu->add_item(make<WSMenuItem>(0, "Open Terminal..."));
  84. m_system_menu->add_item(make<WSMenuItem>(1, "Open ProcessManager..."));
  85. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  86. m_system_menu->add_item(make<WSMenuItem>(100, "640x480"));
  87. m_system_menu->add_item(make<WSMenuItem>(101, "800x600"));
  88. m_system_menu->add_item(make<WSMenuItem>(102, "1024x768"));
  89. m_system_menu->add_item(make<WSMenuItem>(103, "1920x1080"));
  90. m_system_menu->add_item(make<WSMenuItem>(WSMenuItem::Separator));
  91. m_system_menu->add_item(make<WSMenuItem>(200, "About..."));
  92. m_system_menu->on_item_activation = [this] (WSMenuItem& item) {
  93. if (item.identifier() == 0) {
  94. if (fork() == 0) {
  95. execl("/bin/Terminal", "/bin/Terminal", nullptr);
  96. ASSERT_NOT_REACHED();
  97. }
  98. return;
  99. }
  100. if (item.identifier() == 1) {
  101. if (fork() == 0) {
  102. execl("/bin/ProcessManager", "/bin/ProcessManager", nullptr);
  103. ASSERT_NOT_REACHED();
  104. }
  105. return;
  106. }
  107. switch (item.identifier()) {
  108. case 100: set_resolution(640, 480); break;
  109. case 101: set_resolution(800, 600); break;
  110. case 102: set_resolution(1024, 768); break;
  111. case 103: set_resolution(1920, 1080); break;
  112. }
  113. if (item.identifier() == 200) {
  114. if (fork() == 0) {
  115. execl("/bin/About", "/bin/About", nullptr);
  116. ASSERT_NOT_REACHED();
  117. }
  118. return;
  119. }
  120. #ifdef DEBUG_MENUS
  121. dbgprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  122. #endif
  123. };
  124. }
  125. // NOTE: This ensures that the system menu has the correct dimensions.
  126. set_current_menubar(nullptr);
  127. create_thread([] (void* context) -> int {
  128. auto& wm = *(WSWindowManager*)context;
  129. for (;;) {
  130. static unsigned last_busy;
  131. static unsigned last_idle;
  132. unsigned busy;
  133. unsigned idle;
  134. get_cpu_usage(busy, idle);
  135. unsigned busy_diff = busy - last_busy;
  136. unsigned idle_diff = idle - last_idle;
  137. last_busy = busy;
  138. last_idle = idle;
  139. float cpu = (float)busy_diff / (float)(busy_diff + idle_diff);
  140. wm.m_cpu_history.enqueue(cpu);
  141. sleep(1);
  142. }
  143. }, this);
  144. WSMessageLoop::the().start_timer(300, [this] {
  145. static time_t last_update_time;
  146. static int last_cpu_history_size = 0;
  147. static int last_cpu_history_head_index = 0;
  148. time_t now = time(nullptr);
  149. if (now != last_update_time || m_cpu_history.size() != last_cpu_history_size || m_cpu_history.head_index() != last_cpu_history_head_index) {
  150. tick_clock();
  151. last_update_time = now;
  152. last_cpu_history_head_index = m_cpu_history.head_index();
  153. last_cpu_history_size = m_cpu_history.size();
  154. }
  155. });
  156. invalidate();
  157. compose();
  158. }
  159. WSWindowManager::~WSWindowManager()
  160. {
  161. }
  162. const Font& WSWindowManager::font() const
  163. {
  164. return Font::default_font();
  165. }
  166. const Font& WSWindowManager::window_title_font() const
  167. {
  168. return Font::default_bold_font();
  169. }
  170. const Font& WSWindowManager::menu_font() const
  171. {
  172. return Font::default_font();
  173. }
  174. const Font& WSWindowManager::app_menu_font() const
  175. {
  176. return Font::default_bold_font();
  177. }
  178. void get_cpu_usage(unsigned& busy, unsigned& idle)
  179. {
  180. busy = 0;
  181. idle = 0;
  182. FILE* fp = fopen("/proc/all", "r");
  183. if (!fp) {
  184. perror("failed to open /proc/all");
  185. ASSERT_NOT_REACHED();
  186. }
  187. for (;;) {
  188. char buf[BUFSIZ];
  189. char* ptr = fgets(buf, sizeof(buf), fp);
  190. if (!ptr)
  191. break;
  192. auto parts = String(buf, Chomp).split(',');
  193. if (parts.size() < 17)
  194. break;
  195. bool ok;
  196. pid_t pid = parts[0].to_uint(ok);
  197. ASSERT(ok);
  198. unsigned nsched = parts[1].to_uint(ok);
  199. ASSERT(ok);
  200. if (pid == 0)
  201. idle += nsched;
  202. else
  203. busy += nsched;
  204. }
  205. int rc = fclose(fp);
  206. ASSERT(rc == 0);
  207. }
  208. void WSWindowManager::tick_clock()
  209. {
  210. invalidate(menubar_rect());
  211. }
  212. bool WSWindowManager::set_wallpaper(const String& path)
  213. {
  214. auto bitmap = load_png(path);
  215. if (!bitmap)
  216. return false;
  217. m_wallpaper_path = path;
  218. m_wallpaper = move(bitmap);
  219. invalidate();
  220. return true;
  221. }
  222. void WSWindowManager::set_resolution(int width, int height)
  223. {
  224. if (m_screen_rect.width() == width && m_screen_rect.height() == height)
  225. return;
  226. m_wallpaper_path = { };
  227. m_wallpaper = nullptr;
  228. m_screen.set_resolution(width, height);
  229. m_screen_rect = m_screen.rect();
  230. m_front_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(0));
  231. m_back_bitmap = GraphicsBitmap::create_wrapper(GraphicsBitmap::Format::RGB32, { width, height }, m_screen.scanline(height));
  232. m_front_painter = make<Painter>(*m_front_bitmap);
  233. m_back_painter = make<Painter>(*m_back_bitmap);
  234. m_buffers_are_flipped = false;
  235. invalidate();
  236. compose();
  237. WSClientConnection::for_each_client([&] (WSClientConnection& client) {
  238. client.notify_about_new_screen_rect(m_screen_rect);
  239. });
  240. }
  241. template<typename Callback>
  242. void WSWindowManager::for_each_active_menubar_menu(Callback callback)
  243. {
  244. callback(*m_system_menu);
  245. if (m_current_menubar)
  246. m_current_menubar->for_each_menu(callback);
  247. }
  248. int WSWindowManager::menubar_menu_margin() const
  249. {
  250. return 16;
  251. }
  252. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  253. {
  254. if (menubar)
  255. m_current_menubar = menubar->make_weak_ptr();
  256. else
  257. m_current_menubar = nullptr;
  258. #ifdef DEBUG_MENUS
  259. dbgprintf("[WM] Current menubar is now %p\n", menubar);
  260. #endif
  261. Point next_menu_location { menubar_menu_margin() / 2, 0 };
  262. int index = 0;
  263. for_each_active_menubar_menu([&] (WSMenu& menu) {
  264. int text_width = index == 1 ? Font::default_bold_font().width(menu.name()) : font().width(menu.name());
  265. menu.set_rect_in_menubar({ next_menu_location.x() - menubar_menu_margin() / 2, 0, text_width + menubar_menu_margin(), menubar_rect().height() - 1 });
  266. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
  267. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  268. ++index;
  269. return true;
  270. });
  271. invalidate(menubar_rect());
  272. }
  273. void WSWindowManager::add_window(WSWindow& window)
  274. {
  275. m_windows.set(&window);
  276. m_windows_in_order.append(&window);
  277. set_active_window(&window);
  278. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  279. m_switcher.refresh();
  280. if (window.listens_to_wm_events()) {
  281. for_each_window([&] (WSWindow& other_window) {
  282. if (&window != &other_window)
  283. tell_wm_listener_about_window(window, other_window);
  284. return IterationDecision::Continue;
  285. });
  286. }
  287. tell_wm_listeners_window_state_changed(window);
  288. }
  289. void WSWindowManager::move_to_front_and_make_active(WSWindow& window)
  290. {
  291. if (window.is_blocked_by_modal_window())
  292. return;
  293. if (m_windows_in_order.tail() != &window)
  294. invalidate(window);
  295. m_windows_in_order.remove(&window);
  296. m_windows_in_order.append(&window);
  297. set_active_window(&window);
  298. }
  299. void WSWindowManager::remove_window(WSWindow& window)
  300. {
  301. if (!m_windows.contains(&window))
  302. return;
  303. invalidate(window);
  304. m_windows.remove(&window);
  305. m_windows_in_order.remove(&window);
  306. if (window.is_active())
  307. pick_new_active_window();
  308. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  309. m_switcher.refresh();
  310. for_each_window_listening_to_wm_events([&window] (WSWindow& listener) {
  311. if (window.client())
  312. WSMessageLoop::the().post_message(listener, make<WSWMWindowRemovedEvent>(window.client()->client_id(), window.window_id()));
  313. return IterationDecision::Continue;
  314. });
  315. }
  316. void WSWindowManager::tell_wm_listener_about_window(WSWindow& listener, WSWindow& window)
  317. {
  318. if (window.client())
  319. WSMessageLoop::the().post_message(listener, make<WSWMWindowStateChangedEvent>(window.client()->client_id(), window.window_id(), window.title(), window.rect(), window.is_active(), window.type(), window.is_minimized()));
  320. }
  321. void WSWindowManager::tell_wm_listeners_window_state_changed(WSWindow& window)
  322. {
  323. for_each_window_listening_to_wm_events([&] (WSWindow& listener) {
  324. tell_wm_listener_about_window(listener, window);
  325. return IterationDecision::Continue;
  326. });
  327. }
  328. void WSWindowManager::notify_title_changed(WSWindow& window)
  329. {
  330. dbgprintf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  331. invalidate(window.frame().rect());
  332. if (m_switcher.is_visible())
  333. m_switcher.refresh();
  334. tell_wm_listeners_window_state_changed(window);
  335. }
  336. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  337. {
  338. UNUSED_PARAM(old_rect);
  339. UNUSED_PARAM(new_rect);
  340. #ifdef RESIZE_DEBUG
  341. dbgprintf("[WM] WSWindow %p rect changed (%d,%d %dx%d) -> (%d,%d %dx%d)\n", &window, old_rect.x(), old_rect.y(), old_rect.width(), old_rect.height(), new_rect.x(), new_rect.y(), new_rect.width(), new_rect.height());
  342. #endif
  343. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  344. m_switcher.refresh();
  345. tell_wm_listeners_window_state_changed(window);
  346. }
  347. void WSWindowManager::notify_minimization_state_changed(WSWindow& window)
  348. {
  349. tell_wm_listeners_window_state_changed(window);
  350. if (window.is_active() && window.is_minimized())
  351. pick_new_active_window();
  352. }
  353. void WSWindowManager::pick_new_active_window()
  354. {
  355. for_each_visible_window_of_type_from_front_to_back(WSWindowType::Normal, [&] (WSWindow& candidate) {
  356. set_active_window(&candidate);
  357. return IterationDecision::Abort;
  358. });
  359. }
  360. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event)
  361. {
  362. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu;
  363. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  364. bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  365. if (should_open_menu) {
  366. if (current_menu() == &menu)
  367. return;
  368. close_current_menu();
  369. if (!menu.is_empty()) {
  370. auto& menu_window = menu.ensure_menu_window();
  371. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  372. menu_window.set_visible(true);
  373. }
  374. m_current_menu = menu.make_weak_ptr();
  375. return;
  376. }
  377. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  378. close_current_menu();
  379. return;
  380. }
  381. }
  382. void WSWindowManager::close_current_menu()
  383. {
  384. if (m_current_menu && m_current_menu->menu_window())
  385. m_current_menu->menu_window()->set_visible(false);
  386. m_current_menu = nullptr;
  387. }
  388. void WSWindowManager::handle_menubar_mouse_event(const WSMouseEvent& event)
  389. {
  390. for_each_active_menubar_menu([&] (WSMenu& menu) {
  391. if (menu.rect_in_menubar().contains(event.position())) {
  392. handle_menu_mouse_event(menu, event);
  393. return false;
  394. }
  395. return true;
  396. });
  397. }
  398. void WSWindowManager::start_window_drag(WSWindow& window, const WSMouseEvent& event)
  399. {
  400. #ifdef DRAG_DEBUG
  401. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  402. #endif
  403. move_to_front_and_make_active(window);
  404. m_drag_window = window.make_weak_ptr();;
  405. m_drag_origin = event.position();
  406. m_drag_window_origin = window.position();
  407. invalidate(window);
  408. }
  409. void WSWindowManager::start_window_resize(WSWindow& window, const WSMouseEvent& event)
  410. {
  411. move_to_front_and_make_active(window);
  412. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  413. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  414. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  415. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  416. };
  417. Rect outer_rect = window.frame().rect();
  418. ASSERT(outer_rect.contains(event.position()));
  419. int window_relative_x = event.x() - outer_rect.x();
  420. int window_relative_y = event.y() - outer_rect.y();
  421. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  422. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  423. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  424. if (m_resize_direction == ResizeDirection::None) {
  425. ASSERT(!m_resize_window);
  426. return;
  427. }
  428. #ifdef RESIZE_DEBUG
  429. printf("[WM] Begin resizing WSWindow{%p}\n", &window);
  430. #endif
  431. m_resizing_mouse_button = event.button();
  432. m_resize_window = window.make_weak_ptr();;
  433. m_resize_origin = event.position();
  434. m_resize_window_original_rect = window.rect();
  435. m_resize_window->set_has_painted_since_last_resize(true);
  436. invalidate(window);
  437. }
  438. bool WSWindowManager::process_ongoing_window_drag(const WSMouseEvent& event, WSWindow*&)
  439. {
  440. if (!m_drag_window)
  441. return false;
  442. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  443. #ifdef DRAG_DEBUG
  444. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  445. #endif
  446. invalidate(*m_drag_window);
  447. m_drag_window = nullptr;
  448. return true;
  449. }
  450. if (event.type() == WSMessage::MouseMove) {
  451. Point pos = m_drag_window_origin;
  452. #ifdef DRAG_DEBUG
  453. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  454. #endif
  455. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  456. m_drag_window->set_position_without_repaint(pos);
  457. return true;
  458. }
  459. return false;
  460. }
  461. bool WSWindowManager::process_ongoing_window_resize(const WSMouseEvent& event, WSWindow*&)
  462. {
  463. if (!m_resize_window)
  464. return false;
  465. if (event.type() == WSMessage::MouseUp && event.button() == m_resizing_mouse_button) {
  466. #ifdef RESIZE_DEBUG
  467. printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr());
  468. #endif
  469. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  470. invalidate(*m_resize_window);
  471. m_resize_window = nullptr;
  472. m_resizing_mouse_button = MouseButton::None;
  473. return true;
  474. }
  475. if (event.type() != WSMessage::MouseMove)
  476. return false;
  477. auto old_rect = m_resize_window->rect();
  478. int diff_x = event.x() - m_resize_origin.x();
  479. int diff_y = event.y() - m_resize_origin.y();
  480. int change_x = 0;
  481. int change_y = 0;
  482. int change_w = 0;
  483. int change_h = 0;
  484. switch (m_resize_direction) {
  485. case ResizeDirection::DownRight:
  486. change_w = diff_x;
  487. change_h = diff_y;
  488. break;
  489. case ResizeDirection::Right:
  490. change_w = diff_x;
  491. break;
  492. case ResizeDirection::UpRight:
  493. change_w = diff_x;
  494. change_y = diff_y;
  495. change_h = -diff_y;
  496. break;
  497. case ResizeDirection::Up:
  498. change_y = diff_y;
  499. change_h = -diff_y;
  500. break;
  501. case ResizeDirection::UpLeft:
  502. change_x = diff_x;
  503. change_w = -diff_x;
  504. change_y = diff_y;
  505. change_h = -diff_y;
  506. break;
  507. case ResizeDirection::Left:
  508. change_x = diff_x;
  509. change_w = -diff_x;
  510. break;
  511. case ResizeDirection::DownLeft:
  512. change_x = diff_x;
  513. change_w = -diff_x;
  514. change_h = diff_y;
  515. break;
  516. case ResizeDirection::Down:
  517. change_h = diff_y;
  518. break;
  519. default:
  520. ASSERT_NOT_REACHED();
  521. }
  522. auto new_rect = m_resize_window_original_rect;
  523. Size minimum_size { 50, 50 };
  524. new_rect.set_x(new_rect.x() + change_x);
  525. new_rect.set_y(new_rect.y() + change_y);
  526. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  527. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  528. if (!m_resize_window->size_increment().is_null()) {
  529. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  530. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  531. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  532. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  533. }
  534. if (m_resize_window->rect() == new_rect)
  535. return true;
  536. #ifdef RESIZE_DEBUG
  537. dbgprintf("[WM] Resizing [original: %s] now: %s\n",
  538. m_resize_window_original_rect.to_string().characters(),
  539. new_rect.to_string().characters());
  540. #endif
  541. m_resize_window->set_rect(new_rect);
  542. if (m_resize_window->has_painted_since_last_resize()) {
  543. m_resize_window->set_has_painted_since_last_resize(false);
  544. #ifdef RESIZE_DEBUG
  545. dbgprintf("[WM] I'm gonna wait for %s\n", new_rect.to_string().characters());
  546. #endif
  547. m_resize_window->set_last_lazy_resize_rect(new_rect);
  548. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
  549. }
  550. return true;
  551. }
  552. void WSWindowManager::set_cursor_tracking_button(WSButton* button)
  553. {
  554. m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
  555. }
  556. void WSWindowManager::process_mouse_event(const WSMouseEvent& event, WSWindow*& event_window)
  557. {
  558. event_window = nullptr;
  559. if (process_ongoing_window_drag(event, event_window))
  560. return;
  561. if (process_ongoing_window_resize(event, event_window))
  562. return;
  563. if (m_cursor_tracking_button)
  564. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  565. // This is quite hackish, but it's how the WSButton hover effect is implemented.
  566. if (m_hovered_button && event.type() == WSMessage::MouseMove)
  567. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  568. HashTable<WSWindow*> windows_who_received_mouse_event_due_to_cursor_tracking;
  569. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  570. if (!window->global_cursor_tracking())
  571. continue;
  572. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  573. ASSERT(!window->is_minimized()); // Maybe this should also be supported? Idk.
  574. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  575. window->on_message(event.translated(-window->position()));
  576. }
  577. if (menubar_rect().contains(event.position())) {
  578. handle_menubar_mouse_event(event);
  579. return;
  580. }
  581. if (m_current_menu && m_current_menu->menu_window()) {
  582. bool event_is_inside_current_menu = m_current_menu->menu_window()->rect().contains(event.position());
  583. if (!event_is_inside_current_menu) {
  584. if (m_current_menu->hovered_item())
  585. m_current_menu->clear_hovered_item();
  586. if (event.type() == WSMessage::MouseDown || event.type() == WSMessage::MouseUp)
  587. close_current_menu();
  588. }
  589. }
  590. WSWindow* event_window_with_frame = nullptr;
  591. for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
  592. auto window_frame_rect = window.frame().rect();
  593. if (!window_frame_rect.contains(event.position()))
  594. return IterationDecision::Continue;
  595. if (&window != m_resize_candidate.ptr())
  596. clear_resize_candidate();
  597. // First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
  598. // In those cases, the event is swallowed by the window manager.
  599. if (window.type() == WSWindowType::Normal) {
  600. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  601. start_window_drag(window, event);
  602. return IterationDecision::Abort;
  603. }
  604. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  605. start_window_resize(window, event);
  606. return IterationDecision::Abort;
  607. }
  608. }
  609. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  610. if (window.rect().contains(event.position())) {
  611. if (window.type() == WSWindowType::Normal && event.type() == WSMessage::MouseDown)
  612. move_to_front_and_make_active(window);
  613. event_window = &window;
  614. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window))
  615. window.on_message(event.translated(-window.position()));
  616. return IterationDecision::Abort;
  617. }
  618. // We are hitting the frame, pass the event along to WSWindowFrame.
  619. window.frame().on_mouse_event(event.translated(-window_frame_rect.location()));
  620. event_window_with_frame = &window;
  621. return IterationDecision::Abort;
  622. });
  623. if (event_window_with_frame != m_resize_candidate.ptr())
  624. clear_resize_candidate();
  625. }
  626. void WSWindowManager::clear_resize_candidate()
  627. {
  628. if (m_resize_candidate)
  629. invalidate_cursor();
  630. m_resize_candidate = nullptr;
  631. }
  632. void WSWindowManager::compose()
  633. {
  634. auto dirty_rects = move(m_dirty_rects);
  635. dirty_rects.add(Rect::intersection(m_last_geometry_label_rect, m_screen_rect));
  636. dirty_rects.add(Rect::intersection(m_last_cursor_rect, m_screen_rect));
  637. dirty_rects.add(Rect::intersection(current_cursor_rect(), m_screen_rect));
  638. #ifdef DEBUG_COUNTERS
  639. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  640. #endif
  641. auto any_opaque_window_contains_rect = [this] (const Rect& r) {
  642. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  643. if (!window->is_visible())
  644. continue;
  645. if (window->is_minimized())
  646. continue;
  647. if (window->opacity() < 1.0f)
  648. continue;
  649. if (window->has_alpha_channel()) {
  650. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  651. // Maybe there's some way we could know this?
  652. continue;
  653. }
  654. if (window->frame().rect().contains(r))
  655. return true;
  656. }
  657. return false;
  658. };
  659. auto any_opaque_window_above_this_one_contains_rect = [this] (const WSWindow& a_window, const Rect& rect) -> bool {
  660. bool found = false;
  661. bool checking = false;
  662. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  663. if (&window == &a_window) {
  664. checking = true;
  665. return IterationDecision::Continue;
  666. }
  667. if (!checking)
  668. return IterationDecision::Continue;
  669. if (!window.is_visible())
  670. return IterationDecision::Continue;
  671. if (window.is_minimized())
  672. return IterationDecision::Continue;
  673. if (window.opacity() < 1.0f)
  674. return IterationDecision::Continue;
  675. if (window.has_alpha_channel())
  676. return IterationDecision::Continue;
  677. if (window.frame().rect().contains(rect)) {
  678. found = true;
  679. return IterationDecision::Abort;
  680. }
  681. return IterationDecision::Continue;
  682. });
  683. return found;
  684. };
  685. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  686. auto window_frame_rect = window.frame().rect();
  687. for (auto& dirty_rect : dirty_rects.rects()) {
  688. if (dirty_rect.intersects(window_frame_rect))
  689. return true;
  690. }
  691. return false;
  692. };
  693. for (auto& dirty_rect : dirty_rects.rects()) {
  694. if (any_opaque_window_contains_rect(dirty_rect))
  695. continue;
  696. if (!m_wallpaper)
  697. m_back_painter->fill_rect(dirty_rect, m_background_color);
  698. else
  699. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  700. }
  701. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  702. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  703. if (!any_dirty_rect_intersects_window(window))
  704. return IterationDecision::Continue;
  705. PainterStateSaver saver(*m_back_painter);
  706. m_back_painter->add_clip_rect(window.frame().rect());
  707. for (auto& dirty_rect : dirty_rects.rects()) {
  708. if (any_opaque_window_above_this_one_contains_rect(window, dirty_rect))
  709. continue;
  710. PainterStateSaver saver(*m_back_painter);
  711. m_back_painter->add_clip_rect(dirty_rect);
  712. window.frame().paint(*m_back_painter);
  713. if (!backing_store)
  714. continue;
  715. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  716. if (dirty_rect_in_window_coordinates.is_empty())
  717. continue;
  718. dirty_rect_in_window_coordinates.move_by(-window.position());
  719. auto dst = window.position();
  720. dst.move_by(dirty_rect_in_window_coordinates.location());
  721. if (window.opacity() == 1.0f)
  722. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates);
  723. else
  724. m_back_painter->blit_with_opacity(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  725. }
  726. return IterationDecision::Continue;
  727. });
  728. if (auto* window_being_moved_or_resized = m_drag_window ? m_drag_window.ptr() : (m_resize_window ? m_resize_window.ptr() : nullptr)) {
  729. auto geometry_string = window_being_moved_or_resized->rect().to_string();
  730. if (!window_being_moved_or_resized->size_increment().is_null()) {
  731. int width_steps = (window_being_moved_or_resized->width() - window_being_moved_or_resized->base_size().width()) / window_being_moved_or_resized->size_increment().width();
  732. int height_steps = (window_being_moved_or_resized->height() - window_being_moved_or_resized->base_size().height()) / window_being_moved_or_resized->size_increment().height();
  733. geometry_string = String::format("%s (%dx%d)", geometry_string.characters(), width_steps, height_steps);
  734. }
  735. auto geometry_label_rect = Rect { 0, 0, font().width(geometry_string) + 16, font().glyph_height() + 10 };
  736. geometry_label_rect.center_within(window_being_moved_or_resized->rect());
  737. m_back_painter->fill_rect(geometry_label_rect, Color::LightGray);
  738. m_back_painter->draw_rect(geometry_label_rect, Color::DarkGray);
  739. m_back_painter->draw_text(geometry_label_rect, geometry_string, TextAlignment::Center);
  740. m_last_geometry_label_rect = geometry_label_rect;
  741. } else {
  742. m_last_geometry_label_rect = { };
  743. }
  744. draw_menubar();
  745. draw_cursor();
  746. if (m_flash_flush) {
  747. for (auto& rect : dirty_rects.rects())
  748. m_front_painter->fill_rect(rect, Color::Yellow);
  749. }
  750. flip_buffers();
  751. for (auto& r : dirty_rects.rects())
  752. flush(r);
  753. }
  754. Rect WSWindowManager::current_cursor_rect() const
  755. {
  756. return { m_screen.cursor_location().translated(-active_cursor().hotspot()), active_cursor().size() };
  757. }
  758. void WSWindowManager::invalidate_cursor()
  759. {
  760. invalidate(current_cursor_rect());
  761. }
  762. Rect WSWindowManager::menubar_rect() const
  763. {
  764. return { 0, 0, m_screen_rect.width(), 18 };
  765. }
  766. void WSWindowManager::draw_menubar()
  767. {
  768. auto menubar_rect = this->menubar_rect();
  769. m_back_painter->fill_rect(menubar_rect, Color::LightGray);
  770. m_back_painter->draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::White);
  771. int index = 0;
  772. for_each_active_menubar_menu([&] (WSMenu& menu) {
  773. Color text_color = Color::Black;
  774. if (&menu == current_menu()) {
  775. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  776. text_color = Color::White;
  777. }
  778. m_back_painter->draw_text(
  779. menu.text_rect_in_menubar(),
  780. menu.name(),
  781. index == 1 ? app_menu_font() : menu_font(),
  782. TextAlignment::CenterLeft,
  783. text_color
  784. );
  785. ++index;
  786. return true;
  787. });
  788. int username_width = Font::default_bold_font().width(m_username);
  789. Rect username_rect {
  790. menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  791. menubar_rect.y(),
  792. username_width,
  793. menubar_rect.height()
  794. };
  795. m_back_painter->draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  796. time_t now = time(nullptr);
  797. auto* tm = localtime(&now);
  798. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  799. tm->tm_year + 1900,
  800. tm->tm_mon + 1,
  801. tm->tm_mday,
  802. tm->tm_hour,
  803. tm->tm_min,
  804. tm->tm_sec);
  805. int time_width = font().width(time_text);
  806. Rect time_rect {
  807. username_rect.left() - menubar_menu_margin() / 2 - time_width,
  808. menubar_rect.y(),
  809. time_width,
  810. menubar_rect.height()
  811. };
  812. m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
  813. Rect cpu_rect { time_rect.right() - font().width(time_text) - (int)m_cpu_history.capacity() - 10, time_rect.y() + 1, (int)m_cpu_history.capacity(), time_rect.height() - 2 };
  814. m_back_painter->fill_rect(cpu_rect, Color::Black);
  815. int i = m_cpu_history.capacity() - m_cpu_history.size();
  816. for (auto cpu_usage : m_cpu_history) {
  817. m_back_painter->draw_line(
  818. { cpu_rect.x() + i, cpu_rect.bottom() },
  819. { cpu_rect.x() + i, (int)(cpu_rect.y() + (cpu_rect.height() - (cpu_usage * (float)cpu_rect.height()))) },
  820. Color::from_rgb(0xaa6d4b)
  821. );
  822. ++i;
  823. }
  824. }
  825. void WSWindowManager::draw_window_switcher()
  826. {
  827. if (m_switcher.is_visible())
  828. m_switcher.draw();
  829. }
  830. void WSWindowManager::draw_cursor()
  831. {
  832. Rect cursor_rect = current_cursor_rect();
  833. Color inner_color = Color::White;
  834. Color outer_color = Color::Black;
  835. if (m_screen.mouse_button_state() & (unsigned)MouseButton::Left)
  836. swap(inner_color, outer_color);
  837. m_back_painter->blit(cursor_rect.location(), active_cursor().bitmap(), active_cursor().rect());
  838. m_last_cursor_rect = cursor_rect;
  839. }
  840. void WSWindowManager::on_message(const WSMessage& message)
  841. {
  842. if (message.is_mouse_event()) {
  843. WSWindow* event_window = nullptr;
  844. process_mouse_event(static_cast<const WSMouseEvent&>(message), event_window);
  845. set_hovered_window(event_window);
  846. return;
  847. }
  848. if (message.is_key_event()) {
  849. auto& key_event = static_cast<const WSKeyEvent&>(message);
  850. m_keyboard_modifiers = key_event.modifiers();
  851. if (key_event.type() == WSMessage::KeyDown && key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab)
  852. m_switcher.show();
  853. if (m_switcher.is_visible()) {
  854. m_switcher.on_key_event(key_event);
  855. return;
  856. }
  857. if (m_active_window)
  858. return m_active_window->on_message(message);
  859. return;
  860. }
  861. if (message.type() == WSMessage::WM_DeferredCompose) {
  862. m_pending_compose_event = false;
  863. compose();
  864. return;
  865. }
  866. }
  867. void WSWindowManager::set_highlight_window(WSWindow* window)
  868. {
  869. if (window == m_highlight_window.ptr())
  870. return;
  871. if (auto* previous_highlight_window = m_highlight_window.ptr())
  872. invalidate(*previous_highlight_window);
  873. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  874. if (m_highlight_window)
  875. invalidate(*m_highlight_window);
  876. }
  877. void WSWindowManager::set_active_window(WSWindow* window)
  878. {
  879. if (window && window->is_blocked_by_modal_window())
  880. return;
  881. if (window->type() != WSWindowType::Normal) {
  882. dbgprintf("WSWindowManager: Attempted to make a non-normal window active.\n");
  883. return;
  884. }
  885. if (window == m_active_window.ptr())
  886. return;
  887. auto* previously_active_window = m_active_window.ptr();
  888. if (previously_active_window) {
  889. WSMessageLoop::the().post_message(*previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  890. invalidate(*previously_active_window);
  891. }
  892. m_active_window = window->make_weak_ptr();
  893. if (m_active_window) {
  894. WSMessageLoop::the().post_message(*m_active_window, make<WSMessage>(WSMessage::WindowActivated));
  895. invalidate(*m_active_window);
  896. auto* client = window->client();
  897. ASSERT(client);
  898. set_current_menubar(client->app_menubar());
  899. if (previously_active_window)
  900. tell_wm_listeners_window_state_changed(*previously_active_window);
  901. tell_wm_listeners_window_state_changed(*m_active_window);
  902. }
  903. }
  904. void WSWindowManager::set_hovered_window(WSWindow* window)
  905. {
  906. if (m_hovered_window.ptr() == window)
  907. return;
  908. if (m_hovered_window)
  909. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowLeft));
  910. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  911. if (m_hovered_window)
  912. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowEntered));
  913. }
  914. void WSWindowManager::invalidate()
  915. {
  916. m_dirty_rects.clear_with_capacity();
  917. invalidate(m_screen_rect);
  918. }
  919. void WSWindowManager::recompose_immediately()
  920. {
  921. m_dirty_rects.clear_with_capacity();
  922. invalidate(m_screen_rect, false);
  923. }
  924. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  925. {
  926. auto rect = Rect::intersection(a_rect, m_screen_rect);
  927. if (rect.is_empty())
  928. return;
  929. m_dirty_rects.add(rect);
  930. if (should_schedule_compose_event && !m_pending_compose_event) {
  931. WSMessageLoop::the().post_message(*this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  932. m_pending_compose_event = true;
  933. }
  934. }
  935. void WSWindowManager::invalidate(const WSWindow& window)
  936. {
  937. invalidate(window.frame().rect());
  938. }
  939. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  940. {
  941. if (rect.is_empty()) {
  942. invalidate(window);
  943. return;
  944. }
  945. auto outer_rect = window.frame().rect();
  946. auto inner_rect = rect;
  947. inner_rect.move_by(window.position());
  948. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  949. inner_rect.intersect(outer_rect);
  950. invalidate(inner_rect);
  951. }
  952. void WSWindowManager::flush(const Rect& a_rect)
  953. {
  954. auto rect = Rect::intersection(a_rect, m_screen_rect);
  955. #ifdef DEBUG_COUNTERS
  956. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  957. #endif
  958. const RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  959. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  960. size_t pitch = m_back_bitmap->pitch();
  961. for (int y = 0; y < rect.height(); ++y) {
  962. fast_dword_copy(back_ptr, front_ptr, rect.width());
  963. front_ptr = (const RGBA32*)((const byte*)front_ptr + pitch);
  964. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  965. }
  966. }
  967. void WSWindowManager::close_menu(WSMenu& menu)
  968. {
  969. if (current_menu() == &menu)
  970. close_current_menu();
  971. }
  972. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  973. {
  974. if (current_menubar() == &menubar)
  975. set_current_menubar(nullptr);
  976. }
  977. const WSClientConnection* WSWindowManager::active_client() const
  978. {
  979. if (m_active_window)
  980. return m_active_window->client();
  981. return nullptr;
  982. }
  983. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  984. {
  985. if (active_client() == &client)
  986. set_current_menubar(client.app_menubar());
  987. invalidate(menubar_rect());
  988. }
  989. const WSCursor& WSWindowManager::active_cursor() const
  990. {
  991. if (m_drag_window)
  992. return *m_move_cursor;
  993. if (m_resize_window || m_resize_candidate) {
  994. switch (m_resize_direction) {
  995. case ResizeDirection::Up:
  996. case ResizeDirection::Down:
  997. return *m_resize_vertically_cursor;
  998. case ResizeDirection::Left:
  999. case ResizeDirection::Right:
  1000. return *m_resize_horizontally_cursor;
  1001. case ResizeDirection::UpLeft:
  1002. case ResizeDirection::DownRight:
  1003. return *m_resize_diagonally_tlbr_cursor;
  1004. case ResizeDirection::UpRight:
  1005. case ResizeDirection::DownLeft:
  1006. return *m_resize_diagonally_bltr_cursor;
  1007. case ResizeDirection::None:
  1008. break;
  1009. }
  1010. }
  1011. if (m_hovered_window && m_hovered_window->override_cursor())
  1012. return *m_hovered_window->override_cursor();
  1013. return *m_arrow_cursor;
  1014. }
  1015. void WSWindowManager::set_hovered_button(WSButton* button)
  1016. {
  1017. m_hovered_button = button ? button->make_weak_ptr() : nullptr;
  1018. }
  1019. void WSWindowManager::set_resize_candidate(WSWindow& window, ResizeDirection direction)
  1020. {
  1021. m_resize_candidate = window.make_weak_ptr();
  1022. m_resize_direction = direction;
  1023. }