WSWindowManager.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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, 1, "Open Terminal..."));
  85. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 2, "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() == 1) {
  97. if (fork() == 0) {
  98. execl("/bin/Terminal", "/bin/Terminal", nullptr);
  99. ASSERT_NOT_REACHED();
  100. }
  101. return;
  102. }
  103. if (item.identifier() == 2) {
  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. CElapsedTimer& WSWindowManager::DoubleClickInfo::click_clock(MouseButton button)
  584. {
  585. switch (button) {
  586. case MouseButton::Left: return m_left_click_clock;
  587. case MouseButton::Right: return m_right_click_clock;
  588. case MouseButton::Middle: return m_middle_click_clock;
  589. default:
  590. ASSERT_NOT_REACHED();
  591. }
  592. }
  593. // #define DOUBLECLICK_DEBUG
  594. void WSWindowManager::deliver_mouse_event(WSWindow& window, WSMouseEvent& event)
  595. {
  596. if (event.type() == WSEvent::MouseUp) {
  597. if (&window != m_double_click_info.m_clicked_window) {
  598. // we either haven't clicked anywhere, or we haven't clicked on this
  599. // window. set the current click window, and reset the timers.
  600. #if defined(DOUBLECLICK_DEBUG)
  601. dbgprintf("Initial mouseup on window %p (previous was %p)\n", &window, m_double_click_info.m_clicked_window);
  602. #endif
  603. m_double_click_info.m_clicked_window = window.make_weak_ptr();
  604. m_double_click_info.reset();
  605. }
  606. auto& clock = m_double_click_info.click_clock(event.button());
  607. // if the clock is invalid, we haven't clicked with this button on this
  608. // window yet, so there's nothing to do.
  609. if (!clock.is_valid()) {
  610. clock.start();
  611. } else {
  612. int elapsed_since_last_click = clock.elapsed();
  613. clock.start();
  614. // FIXME: It might be a sensible idea to also add a distance travel check.
  615. // If the pointer moves too far, it's not a double click.
  616. if (elapsed_since_last_click < 250) {
  617. #if defined(DOUBLECLICK_DEBUG)
  618. dbgprintf("Transforming MouseUp to MouseDoubleClick!\n");
  619. #endif
  620. event = WSMouseEvent(WSEvent::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  621. // invalidate this now we've delivered a doubleclick, otherwise
  622. // tripleclick will deliver two doubleclick events (incorrectly).
  623. clock = CElapsedTimer();
  624. } else {
  625. // too slow; try again
  626. clock.start();
  627. }
  628. }
  629. }
  630. window.event(event);
  631. }
  632. void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& hovered_window)
  633. {
  634. hovered_window = nullptr;
  635. if (process_ongoing_window_drag(event, hovered_window))
  636. return;
  637. if (process_ongoing_window_resize(event, hovered_window))
  638. return;
  639. if (m_cursor_tracking_button)
  640. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  641. // This is quite hackish, but it's how the WSButton hover effect is implemented.
  642. if (m_hovered_button && event.type() == WSEvent::MouseMove)
  643. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  644. HashTable<WSWindow*> windows_who_received_mouse_event_due_to_cursor_tracking;
  645. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  646. if (!window->global_cursor_tracking())
  647. continue;
  648. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  649. ASSERT(!window->is_minimized()); // Maybe this should also be supported? Idk.
  650. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  651. auto translated_event = event.translated(-window->position());
  652. deliver_mouse_event(*window, translated_event);
  653. }
  654. if (menubar_rect().contains(event.position())) {
  655. handle_menubar_mouse_event(event);
  656. return;
  657. }
  658. if (m_current_menu && m_current_menu->menu_window()) {
  659. auto& window = *m_current_menu->menu_window();
  660. bool event_is_inside_current_menu = window.rect().contains(event.position());
  661. if (!event_is_inside_current_menu) {
  662. if (m_current_menu->hovered_item())
  663. m_current_menu->clear_hovered_item();
  664. if (event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseUp)
  665. close_current_menu();
  666. } else {
  667. hovered_window = &window;
  668. auto translated_event = event.translated(-window.position());
  669. deliver_mouse_event(window, translated_event);
  670. }
  671. return;
  672. }
  673. WSWindow* event_window_with_frame = nullptr;
  674. for_each_visible_window_from_front_to_back([&] (WSWindow& window) {
  675. auto window_frame_rect = window.frame().rect();
  676. if (!window_frame_rect.contains(event.position()))
  677. return IterationDecision::Continue;
  678. if (&window != m_resize_candidate.ptr())
  679. clear_resize_candidate();
  680. // First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
  681. // In those cases, the event is swallowed by the window manager.
  682. if (window.type() == WSWindowType::Normal) {
  683. if (m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left) {
  684. hovered_window = &window;
  685. start_window_drag(window, event);
  686. return IterationDecision::Abort;
  687. }
  688. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  689. hovered_window = &window;
  690. start_window_resize(window, event);
  691. return IterationDecision::Abort;
  692. }
  693. }
  694. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  695. if (window.rect().contains(event.position())) {
  696. if (window.type() == WSWindowType::Normal && event.type() == WSEvent::MouseDown)
  697. move_to_front_and_make_active(window);
  698. hovered_window = &window;
  699. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window)) {
  700. auto translated_event = event.translated(-window.position());
  701. deliver_mouse_event(window, translated_event);
  702. }
  703. return IterationDecision::Abort;
  704. }
  705. // We are hitting the frame, pass the event along to WSWindowFrame.
  706. window.frame().on_mouse_event(event.translated(-window_frame_rect.location()));
  707. event_window_with_frame = &window;
  708. return IterationDecision::Abort;
  709. });
  710. if (event_window_with_frame != m_resize_candidate.ptr())
  711. clear_resize_candidate();
  712. }
  713. void WSWindowManager::clear_resize_candidate()
  714. {
  715. if (m_resize_candidate)
  716. invalidate_cursor();
  717. m_resize_candidate = nullptr;
  718. }
  719. bool WSWindowManager::any_opaque_window_contains_rect(const Rect& rect)
  720. {
  721. bool found_containing_window = false;
  722. for_each_window([&] (WSWindow& window) {
  723. if (!window.is_visible())
  724. return IterationDecision::Continue;
  725. if (window.is_minimized())
  726. return IterationDecision::Continue;
  727. if (window.opacity() < 1.0f)
  728. return IterationDecision::Continue;
  729. if (window.has_alpha_channel()) {
  730. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  731. // Maybe there's some way we could know this?
  732. return IterationDecision::Continue;
  733. }
  734. if (window.frame().rect().contains(rect)) {
  735. found_containing_window = true;
  736. return IterationDecision::Abort;
  737. }
  738. return IterationDecision::Continue;
  739. });
  740. return found_containing_window;
  741. };
  742. bool WSWindowManager::any_opaque_window_above_this_one_contains_rect(const WSWindow& a_window, const Rect& rect)
  743. {
  744. bool found_containing_window = false;
  745. bool checking = false;
  746. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  747. if (&window == &a_window) {
  748. checking = true;
  749. return IterationDecision::Continue;
  750. }
  751. if (!checking)
  752. return IterationDecision::Continue;
  753. if (!window.is_visible())
  754. return IterationDecision::Continue;
  755. if (window.is_minimized())
  756. return IterationDecision::Continue;
  757. if (window.opacity() < 1.0f)
  758. return IterationDecision::Continue;
  759. if (window.has_alpha_channel())
  760. return IterationDecision::Continue;
  761. if (window.frame().rect().contains(rect)) {
  762. found_containing_window = true;
  763. return IterationDecision::Abort;
  764. }
  765. return IterationDecision::Continue;
  766. });
  767. return found_containing_window;
  768. };
  769. void WSWindowManager::compose()
  770. {
  771. auto dirty_rects = move(m_dirty_rects);
  772. dirty_rects.add(Rect::intersection(m_last_geometry_label_rect, m_screen_rect));
  773. dirty_rects.add(Rect::intersection(m_last_cursor_rect, m_screen_rect));
  774. dirty_rects.add(Rect::intersection(current_cursor_rect(), m_screen_rect));
  775. #ifdef DEBUG_COUNTERS
  776. dbgprintf("[WM] compose #%u (%u rects)\n", ++m_compose_count, dirty_rects.rects().size());
  777. #endif
  778. auto any_dirty_rect_intersects_window = [&dirty_rects] (const WSWindow& window) {
  779. auto window_frame_rect = window.frame().rect();
  780. for (auto& dirty_rect : dirty_rects.rects()) {
  781. if (dirty_rect.intersects(window_frame_rect))
  782. return true;
  783. }
  784. return false;
  785. };
  786. for (auto& dirty_rect : dirty_rects.rects()) {
  787. if (any_opaque_window_contains_rect(dirty_rect))
  788. continue;
  789. if (!m_wallpaper)
  790. m_back_painter->fill_rect(dirty_rect, m_background_color);
  791. else
  792. m_back_painter->blit(dirty_rect.location(), *m_wallpaper, dirty_rect);
  793. }
  794. for_each_visible_window_from_back_to_front([&] (WSWindow& window) {
  795. if (!any_dirty_rect_intersects_window(window))
  796. return IterationDecision::Continue;
  797. PainterStateSaver saver(*m_back_painter);
  798. m_back_painter->add_clip_rect(window.frame().rect());
  799. RetainPtr<GraphicsBitmap> backing_store = window.backing_store();
  800. for (auto& dirty_rect : dirty_rects.rects()) {
  801. if (any_opaque_window_above_this_one_contains_rect(window, dirty_rect))
  802. continue;
  803. PainterStateSaver saver(*m_back_painter);
  804. m_back_painter->add_clip_rect(dirty_rect);
  805. window.frame().paint(*m_back_painter);
  806. if (!backing_store)
  807. continue;
  808. Rect dirty_rect_in_window_coordinates = Rect::intersection(dirty_rect, window.rect());
  809. if (dirty_rect_in_window_coordinates.is_empty())
  810. continue;
  811. dirty_rect_in_window_coordinates.move_by(-window.position());
  812. auto dst = window.position();
  813. dst.move_by(dirty_rect_in_window_coordinates.location());
  814. m_back_painter->blit(dst, *backing_store, dirty_rect_in_window_coordinates, window.opacity());
  815. if (backing_store->width() < window.width()) {
  816. Rect right_fill_rect { window.x() + backing_store->width(), window.y(), window.width() - backing_store->width(), window.height() };
  817. m_back_painter->fill_rect(right_fill_rect, window.background_color());
  818. }
  819. if (backing_store->height() < window.height()) {
  820. Rect bottom_fill_rect { window.x(), window.y() + backing_store->height(), window.width(), window.height() - backing_store->height() };
  821. m_back_painter->fill_rect(bottom_fill_rect, window.background_color());
  822. }
  823. }
  824. return IterationDecision::Continue;
  825. });
  826. draw_geometry_label();
  827. draw_menubar();
  828. draw_cursor();
  829. if (m_flash_flush) {
  830. for (auto& rect : dirty_rects.rects())
  831. m_front_painter->fill_rect(rect, Color::Yellow);
  832. }
  833. flip_buffers();
  834. for (auto& r : dirty_rects.rects())
  835. flush(r);
  836. }
  837. Rect WSWindowManager::current_cursor_rect() const
  838. {
  839. return { m_screen.cursor_location().translated(-active_cursor().hotspot()), active_cursor().size() };
  840. }
  841. void WSWindowManager::invalidate_cursor()
  842. {
  843. invalidate(current_cursor_rect());
  844. }
  845. Rect WSWindowManager::menubar_rect() const
  846. {
  847. return { 0, 0, m_screen_rect.width(), 18 };
  848. }
  849. void WSWindowManager::draw_menubar()
  850. {
  851. auto menubar_rect = this->menubar_rect();
  852. m_back_painter->fill_rect(menubar_rect, Color::LightGray);
  853. m_back_painter->draw_line({ 0, menubar_rect.bottom() }, { menubar_rect.right(), menubar_rect.bottom() }, Color::MidGray);
  854. int index = 0;
  855. for_each_active_menubar_menu([&] (WSMenu& menu) {
  856. Color text_color = Color::Black;
  857. if (&menu == current_menu()) {
  858. m_back_painter->fill_rect(menu.rect_in_menubar(), menu_selection_color());
  859. text_color = Color::White;
  860. }
  861. m_back_painter->draw_text(
  862. menu.text_rect_in_menubar(),
  863. menu.name(),
  864. index == 1 ? app_menu_font() : menu_font(),
  865. TextAlignment::CenterLeft,
  866. text_color
  867. );
  868. ++index;
  869. return true;
  870. });
  871. int username_width = Font::default_bold_font().width(m_username);
  872. Rect username_rect {
  873. menubar_rect.right() - menubar_menu_margin() / 2 - Font::default_bold_font().width(m_username),
  874. menubar_rect.y(),
  875. username_width,
  876. menubar_rect.height()
  877. };
  878. m_back_painter->draw_text(username_rect, m_username, Font::default_bold_font(), TextAlignment::CenterRight, Color::Black);
  879. time_t now = time(nullptr);
  880. auto* tm = localtime(&now);
  881. auto time_text = String::format("%4u-%02u-%02u %02u:%02u:%02u",
  882. tm->tm_year + 1900,
  883. tm->tm_mon + 1,
  884. tm->tm_mday,
  885. tm->tm_hour,
  886. tm->tm_min,
  887. tm->tm_sec);
  888. int time_width = font().width(time_text);
  889. Rect time_rect {
  890. username_rect.left() - menubar_menu_margin() / 2 - time_width,
  891. menubar_rect.y(),
  892. time_width,
  893. menubar_rect.height()
  894. };
  895. m_back_painter->draw_text(time_rect, time_text, font(), TextAlignment::CenterRight, Color::Black);
  896. 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 };
  897. m_cpu_monitor.paint(*m_back_painter, cpu_rect);
  898. }
  899. void WSWindowManager::draw_window_switcher()
  900. {
  901. if (m_switcher.is_visible())
  902. m_switcher.draw();
  903. }
  904. void WSWindowManager::draw_geometry_label()
  905. {
  906. auto* window_being_moved_or_resized = m_drag_window ? m_drag_window.ptr() : (m_resize_window ? m_resize_window.ptr() : nullptr);
  907. if (!window_being_moved_or_resized) {
  908. m_last_geometry_label_rect = { };
  909. return;
  910. }
  911. auto geometry_string = window_being_moved_or_resized->rect().to_string();
  912. if (!window_being_moved_or_resized->size_increment().is_null()) {
  913. 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();
  914. 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();
  915. geometry_string = String::format("%s (%dx%d)", geometry_string.characters(), width_steps, height_steps);
  916. }
  917. auto geometry_label_rect = Rect { 0, 0, font().width(geometry_string) + 16, font().glyph_height() + 10 };
  918. geometry_label_rect.center_within(window_being_moved_or_resized->rect());
  919. m_back_painter->fill_rect(geometry_label_rect, Color::LightGray);
  920. m_back_painter->draw_rect(geometry_label_rect, Color::DarkGray);
  921. m_back_painter->draw_text(geometry_label_rect, geometry_string, TextAlignment::Center);
  922. m_last_geometry_label_rect = geometry_label_rect;
  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::event(CEvent& event)
  935. {
  936. if (static_cast<WSEvent&>(event).is_mouse_event()) {
  937. WSWindow* hovered_window = nullptr;
  938. process_mouse_event(static_cast<WSMouseEvent&>(event), hovered_window);
  939. set_hovered_window(hovered_window);
  940. return;
  941. }
  942. if (static_cast<WSEvent&>(event).is_key_event()) {
  943. auto& key_event = static_cast<const WSKeyEvent&>(event);
  944. m_keyboard_modifiers = key_event.modifiers();
  945. if (key_event.type() == WSEvent::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->event(event);
  953. return;
  954. }
  955. if (event.type() == WSEvent::WM_DeferredCompose) {
  956. m_pending_compose_event = false;
  957. compose();
  958. return;
  959. }
  960. CObject::event(event);
  961. }
  962. void WSWindowManager::set_highlight_window(WSWindow* window)
  963. {
  964. if (window == m_highlight_window)
  965. return;
  966. if (auto* previous_highlight_window = m_highlight_window.ptr())
  967. invalidate(*previous_highlight_window);
  968. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  969. if (m_highlight_window)
  970. invalidate(*m_highlight_window);
  971. }
  972. void WSWindowManager::set_active_window(WSWindow* window)
  973. {
  974. if (window && window->is_blocked_by_modal_window())
  975. return;
  976. if (window->type() != WSWindowType::Normal)
  977. return;
  978. if (window == m_active_window)
  979. return;
  980. auto* previously_active_window = m_active_window.ptr();
  981. if (previously_active_window) {
  982. WSEventLoop::the().post_event(*previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
  983. invalidate(*previously_active_window);
  984. }
  985. m_active_window = window->make_weak_ptr();
  986. if (m_active_window) {
  987. WSEventLoop::the().post_event(*m_active_window, make<WSEvent>(WSEvent::WindowActivated));
  988. invalidate(*m_active_window);
  989. auto* client = window->client();
  990. ASSERT(client);
  991. set_current_menubar(client->app_menubar());
  992. if (previously_active_window)
  993. tell_wm_listeners_window_state_changed(*previously_active_window);
  994. tell_wm_listeners_window_state_changed(*m_active_window);
  995. }
  996. }
  997. void WSWindowManager::set_hovered_window(WSWindow* window)
  998. {
  999. if (m_hovered_window == window)
  1000. return;
  1001. if (m_hovered_window)
  1002. WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowLeft));
  1003. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  1004. if (m_hovered_window)
  1005. WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowEntered));
  1006. }
  1007. void WSWindowManager::invalidate()
  1008. {
  1009. m_dirty_rects.clear_with_capacity();
  1010. invalidate(m_screen_rect);
  1011. }
  1012. void WSWindowManager::recompose_immediately()
  1013. {
  1014. m_dirty_rects.clear_with_capacity();
  1015. invalidate(m_screen_rect, false);
  1016. }
  1017. void WSWindowManager::invalidate(const Rect& a_rect, bool should_schedule_compose_event)
  1018. {
  1019. auto rect = Rect::intersection(a_rect, m_screen_rect);
  1020. if (rect.is_empty())
  1021. return;
  1022. m_dirty_rects.add(rect);
  1023. if (should_schedule_compose_event && !m_pending_compose_event) {
  1024. WSEventLoop::the().post_event(*this, make<WSEvent>(WSEvent::WM_DeferredCompose));
  1025. m_pending_compose_event = true;
  1026. }
  1027. }
  1028. void WSWindowManager::invalidate(const WSWindow& window)
  1029. {
  1030. invalidate(window.frame().rect());
  1031. }
  1032. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  1033. {
  1034. if (rect.is_empty()) {
  1035. invalidate(window);
  1036. return;
  1037. }
  1038. auto outer_rect = window.frame().rect();
  1039. auto inner_rect = rect;
  1040. inner_rect.move_by(window.position());
  1041. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  1042. inner_rect.intersect(outer_rect);
  1043. invalidate(inner_rect);
  1044. }
  1045. void WSWindowManager::flush(const Rect& a_rect)
  1046. {
  1047. auto rect = Rect::intersection(a_rect, m_screen_rect);
  1048. #ifdef DEBUG_COUNTERS
  1049. dbgprintf("[WM] flush #%u (%d,%d %dx%d)\n", ++m_flush_count, rect.x(), rect.y(), rect.width(), rect.height());
  1050. #endif
  1051. const RGBA32* front_ptr = m_front_bitmap->scanline(rect.y()) + rect.x();
  1052. RGBA32* back_ptr = m_back_bitmap->scanline(rect.y()) + rect.x();
  1053. size_t pitch = m_back_bitmap->pitch();
  1054. for (int y = 0; y < rect.height(); ++y) {
  1055. fast_dword_copy(back_ptr, front_ptr, rect.width());
  1056. front_ptr = (const RGBA32*)((const byte*)front_ptr + pitch);
  1057. back_ptr = (RGBA32*)((byte*)back_ptr + pitch);
  1058. }
  1059. }
  1060. void WSWindowManager::close_menu(WSMenu& menu)
  1061. {
  1062. if (current_menu() == &menu)
  1063. close_current_menu();
  1064. }
  1065. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  1066. {
  1067. if (current_menubar() == &menubar)
  1068. set_current_menubar(nullptr);
  1069. }
  1070. const WSClientConnection* WSWindowManager::active_client() const
  1071. {
  1072. if (m_active_window)
  1073. return m_active_window->client();
  1074. return nullptr;
  1075. }
  1076. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  1077. {
  1078. if (active_client() == &client)
  1079. set_current_menubar(client.app_menubar());
  1080. invalidate(menubar_rect());
  1081. }
  1082. const WSCursor& WSWindowManager::active_cursor() const
  1083. {
  1084. if (m_drag_window)
  1085. return *m_move_cursor;
  1086. if (m_resize_window || m_resize_candidate) {
  1087. switch (m_resize_direction) {
  1088. case ResizeDirection::Up:
  1089. case ResizeDirection::Down:
  1090. return *m_resize_vertically_cursor;
  1091. case ResizeDirection::Left:
  1092. case ResizeDirection::Right:
  1093. return *m_resize_horizontally_cursor;
  1094. case ResizeDirection::UpLeft:
  1095. case ResizeDirection::DownRight:
  1096. return *m_resize_diagonally_tlbr_cursor;
  1097. case ResizeDirection::UpRight:
  1098. case ResizeDirection::DownLeft:
  1099. return *m_resize_diagonally_bltr_cursor;
  1100. case ResizeDirection::None:
  1101. break;
  1102. }
  1103. }
  1104. if (m_hovered_window && m_hovered_window->override_cursor())
  1105. return *m_hovered_window->override_cursor();
  1106. return *m_arrow_cursor;
  1107. }
  1108. void WSWindowManager::set_hovered_button(WSButton* button)
  1109. {
  1110. m_hovered_button = button ? button->make_weak_ptr() : nullptr;
  1111. }
  1112. void WSWindowManager::set_resize_candidate(WSWindow& window, ResizeDirection direction)
  1113. {
  1114. m_resize_candidate = window.make_weak_ptr();
  1115. m_resize_direction = direction;
  1116. }
  1117. Rect WSWindowManager::maximized_window_rect(const WSWindow& window) const
  1118. {
  1119. Rect rect = m_screen_rect;
  1120. // Subtract window title bar (leaving the border)
  1121. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  1122. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  1123. // Subtract menu bar
  1124. rect.set_y(rect.y() + menubar_rect().height());
  1125. rect.set_height(rect.height() - menubar_rect().height());
  1126. // Subtract taskbar window height if present
  1127. const_cast<WSWindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WSWindowType::Taskbar, [&rect] (WSWindow& taskbar_window) {
  1128. rect.set_height(rect.height() - taskbar_window.height());
  1129. return IterationDecision::Abort;
  1130. });
  1131. return rect;
  1132. }