WSWindowManager.cpp 43 KB

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