WSWindowManager.cpp 43 KB

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