WSWindowManager.cpp 45 KB

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