WSWindowManager.cpp 48 KB

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