WSWindowManager.cpp 42 KB

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