WSWindowManager.cpp 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243
  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(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. }
  469. void WSWindowManager::remove_window(WSWindow& window)
  470. {
  471. if (!m_windows.contains(&window))
  472. return;
  473. invalidate(window);
  474. m_windows.remove(&window);
  475. m_windows_in_order.remove(&window);
  476. if (!active_window() && !m_windows.is_empty())
  477. set_active_window(*m_windows.begin());
  478. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  479. m_switcher.refresh();
  480. }
  481. void WSWindowManager::notify_title_changed(WSWindow& window)
  482. {
  483. dbgprintf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  484. invalidate(outer_window_rect(window));
  485. if (m_switcher.is_visible())
  486. m_switcher.refresh();
  487. }
  488. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  489. {
  490. 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());
  491. invalidate(outer_window_rect(old_rect));
  492. invalidate(outer_window_rect(new_rect));
  493. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  494. m_switcher.refresh();
  495. }
  496. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, WSMouseEvent& event)
  497. {
  498. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu;
  499. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  500. bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  501. if (should_open_menu) {
  502. if (current_menu() == &menu)
  503. return;
  504. close_current_menu();
  505. if (!menu.is_empty()) {
  506. auto& menu_window = menu.ensure_menu_window();
  507. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() });
  508. menu_window.set_visible(true);
  509. }
  510. m_current_menu = menu.make_weak_ptr();
  511. return;
  512. }
  513. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  514. close_current_menu();
  515. return;
  516. }
  517. }
  518. void WSWindowManager::close_current_menu()
  519. {
  520. if (m_current_menu && m_current_menu->menu_window())
  521. m_current_menu->menu_window()->set_visible(false);
  522. m_current_menu = nullptr;
  523. }
  524. void WSWindowManager::handle_menubar_mouse_event(WSMouseEvent& event)
  525. {
  526. for_each_active_menubar_menu([&] (WSMenu& menu) {
  527. if (menu.rect_in_menubar().contains(event.position())) {
  528. handle_menu_mouse_event(menu, event);
  529. return false;
  530. }
  531. return true;
  532. });
  533. }
  534. void WSWindowManager::handle_close_button_mouse_event(WSWindow& window, WSMouseEvent& event)
  535. {
  536. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  537. WSMessage message(WSMessage::WindowCloseRequest);
  538. window.on_message(message);
  539. return;
  540. }
  541. }
  542. void WSWindowManager::start_window_drag(WSWindow& window, WSMouseEvent& event)
  543. {
  544. #ifdef DRAG_DEBUG
  545. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  546. #endif
  547. m_drag_window = window.make_weak_ptr();;
  548. m_drag_origin = event.position();
  549. m_drag_window_origin = window.position();
  550. invalidate(window);
  551. }
  552. void WSWindowManager::start_window_resize(WSWindow& window, WSMouseEvent& event)
  553. {
  554. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  555. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  556. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  557. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  558. };
  559. Rect outer_rect = outer_window_rect(window);
  560. ASSERT(outer_rect.contains(event.position()));
  561. int window_relative_x = event.x() - outer_rect.x();
  562. int window_relative_y = event.y() - outer_rect.y();
  563. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  564. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  565. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  566. if (m_resize_direction == ResizeDirection::None) {
  567. ASSERT(!m_resize_window);
  568. return;
  569. }
  570. #ifdef RESIZE_DEBUG
  571. printf("[WM] Begin resizing WSWindow{%p}\n", &window);
  572. #endif
  573. m_resize_window = window.make_weak_ptr();;
  574. m_resize_origin = event.position();
  575. m_resize_window_original_rect = window.rect();
  576. m_resize_window->set_has_painted_since_last_resize(true);
  577. invalidate(window);
  578. }
  579. void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& event_window)
  580. {
  581. event_window = nullptr;
  582. if (m_drag_window) {
  583. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Left) {
  584. #ifdef DRAG_DEBUG
  585. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  586. #endif
  587. invalidate(*m_drag_window);
  588. m_drag_window = nullptr;
  589. return;
  590. }
  591. if (event.type() == WSMessage::MouseMove) {
  592. auto old_window_rect = m_drag_window->rect();
  593. Point pos = m_drag_window_origin;
  594. #ifdef DRAG_DEBUG
  595. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  596. #endif
  597. pos.move_by(event.x() - m_drag_origin.x(), event.y() - m_drag_origin.y());
  598. m_drag_window->set_position_without_repaint(pos);
  599. invalidate(outer_window_rect(old_window_rect));
  600. invalidate(outer_window_rect(m_drag_window->rect()));
  601. return;
  602. }
  603. }
  604. if (m_resize_window) {
  605. if (event.type() == WSMessage::MouseUp && event.button() == MouseButton::Right) {
  606. #ifdef RESIZE_DEBUG
  607. printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr());
  608. #endif
  609. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  610. invalidate(*m_resize_window);
  611. m_resize_window = nullptr;
  612. return;
  613. }
  614. if (event.type() == WSMessage::MouseMove) {
  615. auto old_rect = m_resize_window->rect();
  616. int diff_x = event.x() - m_resize_origin.x();
  617. int diff_y = event.y() - m_resize_origin.y();
  618. int change_x = 0;
  619. int change_y = 0;
  620. int change_w = 0;
  621. int change_h = 0;
  622. switch (m_resize_direction) {
  623. case ResizeDirection::DownRight:
  624. change_w = diff_x;
  625. change_h = diff_y;
  626. break;
  627. case ResizeDirection::Right:
  628. change_w = diff_x;
  629. break;
  630. case ResizeDirection::UpRight:
  631. change_w = diff_x;
  632. change_y = diff_y;
  633. change_h = -diff_y;
  634. break;
  635. case ResizeDirection::Up:
  636. change_y = diff_y;
  637. change_h = -diff_y;
  638. break;
  639. case ResizeDirection::UpLeft:
  640. change_x = diff_x;
  641. change_w = -diff_x;
  642. change_y = diff_y;
  643. change_h = -diff_y;
  644. break;
  645. case ResizeDirection::Left:
  646. change_x = diff_x;
  647. change_w = -diff_x;
  648. break;
  649. case ResizeDirection::DownLeft:
  650. change_x = diff_x;
  651. change_w = -diff_x;
  652. change_h = diff_y;
  653. break;
  654. case ResizeDirection::Down:
  655. change_h = diff_y;
  656. break;
  657. default:
  658. ASSERT_NOT_REACHED();
  659. }
  660. auto new_rect = m_resize_window_original_rect;
  661. Size minimum_size { 50, 50 };
  662. new_rect.set_x(new_rect.x() + change_x);
  663. new_rect.set_y(new_rect.y() + change_y);
  664. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  665. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  666. if (!m_resize_window->size_increment().is_null()) {
  667. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  668. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  669. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  670. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  671. }
  672. if (m_resize_window->rect() == new_rect)
  673. return;
  674. #ifdef RESIZE_DEBUG
  675. dbgprintf("[WM] Resizing [original: %s] now: %s\n",
  676. m_resize_window_original_rect.to_string().characters(),
  677. new_rect.to_string().characters());
  678. #endif
  679. m_resize_window->set_rect(new_rect);
  680. if (m_resize_window->has_painted_since_last_resize()) {
  681. m_resize_window->set_has_painted_since_last_resize(false);
  682. #ifdef RESIZE_DEBUG
  683. dbgprintf("[WM] I'm gonna wait for %s\n", new_rect.to_string().characters());
  684. #endif
  685. m_resize_window->set_last_lazy_resize_rect(new_rect);
  686. WSMessageLoop::the().post_message(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
  687. }
  688. return;
  689. }
  690. }
  691. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  692. if (!window->global_cursor_tracking())
  693. continue;
  694. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  695. Point position { event.x() - window->rect().x(), event.y() - window->rect().y() };
  696. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button(), event.modifiers());
  697. window->on_message(*local_event);
  698. }
  699. if (menubar_rect().contains(event.position())) {
  700. handle_menubar_mouse_event(event);
  701. return;
  702. }
  703. if (m_current_menu && m_current_menu->menu_window()) {
  704. bool event_is_inside_current_menu = m_current_menu->menu_window()->rect().contains(event.position());
  705. if (!event_is_inside_current_menu) {
  706. if (m_current_menu->hovered_item())
  707. m_current_menu->clear_hovered_item();
  708. if (event.type() == WSMessage::MouseDown || event.type() == WSMessage::MouseUp)
  709. close_current_menu();
  710. }
  711. }
  712. for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
  713. if (window.type() == WSWindowType::Normal && outer_window_rect(window).contains(event.position())) {
  714. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left) {
  715. move_to_front(window);
  716. set_active_window(&window);
  717. start_window_drag(window, event);
  718. return IterationDecision::Abort;
  719. }
  720. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSMessage::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  721. move_to_front(window);
  722. set_active_window(&window);
  723. start_window_resize(window, event);
  724. return IterationDecision::Abort;
  725. }
  726. }
  727. if (window.type() == WSWindowType::Normal && title_bar_rect(window.rect()).contains(event.position())) {
  728. if (event.type() == WSMessage::MouseDown) {
  729. move_to_front(window);
  730. set_active_window(&window);
  731. }
  732. if (close_button_rect_for_window(window.rect()).contains(event.position())) {
  733. handle_close_button_mouse_event(window, event);
  734. return IterationDecision::Abort;
  735. }
  736. if (event.type() == WSMessage::MouseDown && event.button() == MouseButton::Left)
  737. start_window_drag(window, event);
  738. return IterationDecision::Abort;
  739. }
  740. if (window.rect().contains(event.position())) {
  741. if (window.type() == WSWindowType::Normal && event.type() == WSMessage::MouseDown) {
  742. move_to_front(window);
  743. set_active_window(&window);
  744. }
  745. event_window = &window;
  746. if (!window.global_cursor_tracking()) {
  747. // FIXME: Should we just alter the coordinates of the existing MouseEvent and pass it through?
  748. Point position { event.x() - window.rect().x(), event.y() - window.rect().y() };
  749. auto local_event = make<WSMouseEvent>(event.type(), position, event.buttons(), event.button(), event.modifiers());
  750. window.on_message(*local_event);
  751. }
  752. return IterationDecision::Abort;
  753. }
  754. return IterationDecision::Continue;
  755. });
  756. }
  757. void WSWindowManager::compose()
  758. {
  759. auto dirty_rects = move(m_dirty_rects);
  760. auto cursor_location = m_screen.cursor_location();
  761. dirty_rects.add(m_last_cursor_rect);
  762. dirty_rects.add({ cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() });
  763. #ifdef DEBUG_COUNTERS
  764. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  765. #endif
  766. auto any_opaque_window_contains_rect = [this] (const Rect& r) {
  767. for (auto* window = m_windows_in_order.head(); window; window = window->next()) {
  768. if (!window->is_visible())
  769. continue;
  770. if (window->opacity() < 1.0f)
  771. continue;
  772. if (window->has_alpha_channel()) {
  773. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  774. // Maybe there's some way we could know this?
  775. continue;
  776. }
  777. if (outer_window_rect(*window).contains(r))
  778. return true;
  779. }
  780. return false;
  781. };
  782. auto any_opaque_window_above_this_one_contains_rect = [this] (const WSWindow& a_window, const Rect& rect) -> bool {
  783. bool found = false;
  784. bool checking = false;
  785. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  786. if (&window == &a_window) {
  787. checking = true;
  788. return IterationDecision::Continue;
  789. }
  790. if (!checking)
  791. return IterationDecision::Continue;
  792. if (!window.is_visible())
  793. return IterationDecision::Continue;;
  794. if (window.opacity() < 1.0f)
  795. return IterationDecision::Continue;;
  796. if (window.has_alpha_channel())
  797. return IterationDecision::Continue;;
  798. if (outer_window_rect(window).contains(rect)) {
  799. found = true;
  800. return IterationDecision::Abort;
  801. }
  802. return IterationDecision::Continue;
  803. });
  804. return found;
  805. };
  806. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  807. auto window_rect = outer_window_rect(window);
  808. for (auto& dirty_rect : dirty_rects.rects()) {
  809. if (dirty_rect.intersects(window_rect))
  810. return true;
  811. }
  812. return false;
  813. };
  814. for (auto& dirty_rect : dirty_rects.rects()) {
  815. if (any_opaque_window_contains_rect(dirty_rect))
  816. continue;
  817. if (!m_wallpaper)
  818. m_back_painter->fill_rect(dirty_rect, m_background_color);
  819. else
  820. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  821. }
  822. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  823. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  824. if (!any_dirty_rect_intersects_window(window))
  825. return IterationDecision::Continue;
  826. PainterStateSaver saver(*m_back_painter);
  827. m_back_painter->set_clip_rect(outer_window_rect(window));
  828. for (auto& dirty_rect : dirty_rects.rects()) {
  829. if (any_opaque_window_above_this_one_contains_rect(window, dirty_rect))
  830. continue;
  831. PainterStateSaver saver(*m_back_painter);
  832. m_back_painter->set_clip_rect(dirty_rect);
  833. paint_window_frame(window);
  834. if (!backing_store)
  835. continue;
  836. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  837. if (dirty_rect_in_window_coordinates.is_empty())
  838. continue;
  839. dirty_rect_in_window_coordinates.move_by(-window.position());
  840. auto dst = window.position();
  841. dst.move_by(dirty_rect_in_window_coordinates.location());
  842. if (window.opacity() == 1.0f)
  843. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates);
  844. else
  845. m_back_painter->blit_with_opacity(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  846. }
  847. return IterationDecision::Continue;
  848. });
  849. draw_menubar();
  850. draw_cursor();
  851. if (m_flash_flush) {
  852. for (auto& rect : dirty_rects.rects())
  853. m_front_painter->fill_rect(rect, Color::Yellow);
  854. }
  855. flip_buffers();
  856. for (auto& r : dirty_rects.rects())
  857. flush(r);
  858. }
  859. void WSWindowManager::invalidate_cursor()
  860. {
  861. auto cursor_location = m_screen.cursor_location();
  862. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  863. invalidate(cursor_rect);
  864. }
  865. Rect WSWindowManager::menubar_rect() const
  866. {
  867. return { 0, 0, m_screen_rect.width(), 18 };
  868. }
  869. void WSWindowManager::draw_menubar()
  870. {
  871. auto menubar_rect = this->menubar_rect();
  872. m_back_painter->fill_rect(menubar_rect, Color::LightGray);
  873. m_back_painter->draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::White);
  874. int index = 0;
  875. for_each_active_menubar_menu([&] (WSMenu& menu) {
  876. Color text_color = Color::Black;
  877. if (&menu == current_menu()) {
  878. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  879. text_color = Color::White;
  880. }
  881. m_back_painter->draw_text(
  882. menu.text_rect_in_menubar(),
  883. menu.name(),
  884. index == 1 ? app_menu_font() : menu_font(),
  885. TextAlignment::CenterLeft,
  886. text_color
  887. );
  888. ++index;
  889. return true;
  890. });
  891. int username_width = Font::default_bold_font().width(m_username);
  892. Rect username_rect {
  893. menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  894. menubar_rect.y(),
  895. username_width,
  896. menubar_rect.height()
  897. };
  898. m_back_painter->draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  899. time_t now = time(nullptr);
  900. auto* tm = localtime(&now);
  901. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  902. tm->tm_year + 1900,
  903. tm->tm_mon + 1,
  904. tm->tm_mday,
  905. tm->tm_hour,
  906. tm->tm_min,
  907. tm->tm_sec);
  908. int time_width = font().width(time_text);
  909. Rect time_rect {
  910. username_rect.left() - menubar_menu_margin() / 2 - time_width,
  911. menubar_rect.y(),
  912. time_width,
  913. menubar_rect.height()
  914. };
  915. m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
  916. 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 };
  917. m_back_painter->fill_rect(cpu_rect, Color::Black);
  918. int i = m_cpu_history.capacity() - m_cpu_history.size();
  919. for (auto cpu_usage : m_cpu_history) {
  920. m_back_painter->draw_line(
  921. { cpu_rect.x() + i, cpu_rect.bottom() },
  922. { cpu_rect.x() + i, (int)(cpu_rect.y() + (cpu_rect.height() - (cpu_usage * (float)cpu_rect.height()))) },
  923. Color::from_rgb(0xaa6d4b)
  924. );
  925. ++i;
  926. }
  927. }
  928. void WSWindowManager::draw_window_switcher()
  929. {
  930. if (m_switcher.is_visible())
  931. m_switcher.draw();
  932. }
  933. void WSWindowManager::draw_cursor()
  934. {
  935. auto cursor_location = m_screen.cursor_location();
  936. Rect cursor_rect { cursor_location.x(), cursor_location.y(), (int)m_cursor_bitmap_inner->width(), (int)m_cursor_bitmap_inner->height() };
  937. Color inner_color = Color::White;
  938. Color outer_color = Color::Black;
  939. if (m_screen.mouse_button_state() & (unsigned)MouseButton::Left)
  940. swap(inner_color, outer_color);
  941. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_inner, inner_color);
  942. m_back_painter->draw_bitmap(cursor_location, *m_cursor_bitmap_outer, outer_color);
  943. m_last_cursor_rect = cursor_rect;
  944. }
  945. void WSWindowManager::on_message(WSMessage& message)
  946. {
  947. if (message.is_mouse_event()) {
  948. WSWindow* event_window = nullptr;
  949. process_mouse_event(static_cast<WSMouseEvent&>(message), event_window);
  950. set_hovered_window(event_window);
  951. return;
  952. }
  953. if (message.is_key_event()) {
  954. auto& key_event = static_cast<WSKeyEvent&>(message);
  955. m_keyboard_modifiers = key_event.modifiers();
  956. if (key_event.type() == WSMessage::KeyDown && key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab)
  957. m_switcher.show();
  958. if (m_switcher.is_visible()) {
  959. m_switcher.on_key_event(key_event);
  960. return;
  961. }
  962. if (m_active_window)
  963. return m_active_window->on_message(message);
  964. return;
  965. }
  966. if (message.type() == WSMessage::WM_DeferredCompose) {
  967. m_pending_compose_event = false;
  968. compose();
  969. return;
  970. }
  971. }
  972. void WSWindowManager::set_highlight_window(WSWindow* window)
  973. {
  974. if (window == m_highlight_window.ptr())
  975. return;
  976. if (auto* previous_highlight_window = m_highlight_window.ptr())
  977. invalidate(*previous_highlight_window);
  978. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  979. if (m_highlight_window)
  980. invalidate(*m_highlight_window);
  981. }
  982. void WSWindowManager::set_active_window(WSWindow* window)
  983. {
  984. if (window && window->is_blocked_by_modal_window())
  985. return;
  986. if (window->type() != WSWindowType::Normal) {
  987. dbgprintf("WSWindowManager: Attempted to make a non-normal window active.\n");
  988. return;
  989. }
  990. if (window == m_active_window.ptr())
  991. return;
  992. if (auto* previously_active_window = m_active_window.ptr()) {
  993. WSMessageLoop::the().post_message(*previously_active_window, make<WSMessage>(WSMessage::WindowDeactivated));
  994. invalidate(*previously_active_window);
  995. }
  996. m_active_window = window->make_weak_ptr();
  997. if (m_active_window) {
  998. WSMessageLoop::the().post_message(*m_active_window, make<WSMessage>(WSMessage::WindowActivated));
  999. invalidate(*m_active_window);
  1000. auto* client = window->client();
  1001. ASSERT(client);
  1002. set_current_menubar(client->app_menubar());
  1003. }
  1004. }
  1005. void WSWindowManager::set_hovered_window(WSWindow* window)
  1006. {
  1007. if (m_hovered_window.ptr() == window)
  1008. return;
  1009. if (m_hovered_window)
  1010. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowLeft));
  1011. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  1012. if (m_hovered_window)
  1013. WSMessageLoop::the().post_message(*m_hovered_window, make<WSMessage>(WSMessage::WindowEntered));
  1014. }
  1015. void WSWindowManager::invalidate()
  1016. {
  1017. m_dirty_rects.clear_with_capacity();
  1018. invalidate(m_screen_rect);
  1019. }
  1020. void WSWindowManager::recompose_immediately()
  1021. {
  1022. m_dirty_rects.clear_with_capacity();
  1023. invalidate(m_screen_rect, false);
  1024. }
  1025. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  1026. {
  1027. auto rect = Rect::intersection(a_rect, m_screen_rect);
  1028. if (rect.is_empty())
  1029. return;
  1030. m_dirty_rects.add(rect);
  1031. if (should_schedule_compose_event && !m_pending_compose_event) {
  1032. WSMessageLoop::the().post_message(*this, make<WSMessage>(WSMessage::WM_DeferredCompose));
  1033. m_pending_compose_event = true;
  1034. }
  1035. }
  1036. void WSWindowManager::invalidate(const WSWindow& window)
  1037. {
  1038. if (window.type() == WSWindowType::Menu) {
  1039. invalidate(menu_window_rect(window.rect()));
  1040. return;
  1041. }
  1042. if (window.type() == WSWindowType::Normal) {
  1043. invalidate(outer_window_rect(window));
  1044. return;
  1045. }
  1046. if (window.type() == WSWindowType::WindowSwitcher) {
  1047. invalidate(window.rect());
  1048. return;
  1049. }
  1050. ASSERT_NOT_REACHED();
  1051. }
  1052. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  1053. {
  1054. if (rect.is_empty()) {
  1055. invalidate(window);
  1056. return;
  1057. }
  1058. auto outer_rect = outer_window_rect(window);
  1059. auto inner_rect = rect;
  1060. inner_rect.move_by(window.position());
  1061. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  1062. inner_rect.intersect(outer_rect);
  1063. invalidate(inner_rect);
  1064. }
  1065. void WSWindowManager::flush(const Rect& a_rect)
  1066. {
  1067. auto rect = Rect::intersection(a_rect, m_screen_rect);
  1068. #ifdef DEBUG_COUNTERS
  1069. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  1070. #endif
  1071. const RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  1072. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  1073. size_t pitch = m_back_bitmap->pitch();
  1074. for (int y = 0; y < rect.height(); ++y) {
  1075. fast_dword_copy(back_ptr, front_ptr, rect.width());
  1076. front_ptr = (const RGBA32*)((const byte*)front_ptr + pitch);
  1077. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  1078. }
  1079. }
  1080. void WSWindowManager::close_menu(WSMenu& menu)
  1081. {
  1082. if (current_menu() == &menu)
  1083. close_current_menu();
  1084. }
  1085. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  1086. {
  1087. if (current_menubar() == &menubar)
  1088. set_current_menubar(nullptr);
  1089. }
  1090. const WSClientConnection* WSWindowManager::active_client() const
  1091. {
  1092. if (m_active_window)
  1093. return m_active_window->client();
  1094. return nullptr;
  1095. }
  1096. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  1097. {
  1098. if (active_client() == &client)
  1099. set_current_menubar(client.app_menubar());
  1100. invalidate(menubar_rect());
  1101. }