WSWindowManager.cpp 40 KB

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