WSWindowManager.cpp 43 KB

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