WSWindowManager.cpp 43 KB

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