WSWindowManager.cpp 41 KB

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