WSWindowManager.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070
  1. #include "WSWindowManager.h"
  2. #include "WSCompositor.h"
  3. #include "WSEventLoop.h"
  4. #include "WSMenu.h"
  5. #include "WSMenuBar.h"
  6. #include "WSMenuItem.h"
  7. #include "WSScreen.h"
  8. #include "WSWindow.h"
  9. #include <AK/StdLibExtras.h>
  10. #include <AK/Vector.h>
  11. #include <LibCore/CTimer.h>
  12. #include <SharedGraphics/CharacterBitmap.h>
  13. #include <SharedGraphics/Font.h>
  14. #include <SharedGraphics/PNGLoader.h>
  15. #include <SharedGraphics/Painter.h>
  16. #include <SharedGraphics/StylePainter.h>
  17. #include <WindowServer/WSAPITypes.h>
  18. #include <WindowServer/WSButton.h>
  19. #include <WindowServer/WSClientConnection.h>
  20. #include <WindowServer/WSCursor.h>
  21. #include <errno.h>
  22. #include <stdio.h>
  23. #include <time.h>
  24. #include <unistd.h>
  25. //#define DEBUG_COUNTERS
  26. //#define RESIZE_DEBUG
  27. static WSWindowManager* s_the;
  28. WSWindowManager& WSWindowManager::the()
  29. {
  30. ASSERT(s_the);
  31. return *s_the;
  32. }
  33. WSWindowManager::WSWindowManager()
  34. {
  35. s_the = this;
  36. m_username = getlogin();
  37. reload_config(false);
  38. struct AppMenuItem {
  39. const char* binary_name;
  40. const char* description;
  41. };
  42. Vector<AppMenuItem> apps;
  43. apps.append({ "/bin/Terminal", "Open Terminal..." });
  44. apps.append({ "/bin/FileManager", "Open FileManager..." });
  45. apps.append({ "/bin/ProcessManager", "Open ProcessManager..." });
  46. {
  47. byte system_menu_name[] = { 0xf8, 0 };
  48. m_system_menu = make<WSMenu>(nullptr, -1, String((const char*)system_menu_name));
  49. int appIndex = 1;
  50. for (const auto& app : apps) {
  51. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, appIndex++, app.description));
  52. }
  53. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
  54. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 100, "Reload WM Config File"));
  55. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
  56. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 200, "About..."));
  57. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, WSMenuItem::Separator));
  58. m_system_menu->add_item(make<WSMenuItem>(*m_system_menu, 300, "Shutdown..."));
  59. m_system_menu->on_item_activation = [this, apps](WSMenuItem& item) {
  60. if (item.identifier() >= 1 && item.identifier() <= 1 + apps.size() - 1) {
  61. if (fork() == 0) {
  62. const auto& bin = apps[item.identifier() - 1].binary_name;
  63. execl(bin, bin, nullptr);
  64. ASSERT_NOT_REACHED();
  65. }
  66. }
  67. switch (item.identifier()) {
  68. case 100:
  69. reload_config(true);
  70. break;
  71. case 200:
  72. if (fork() == 0) {
  73. execl("/bin/About", "/bin/About", nullptr);
  74. ASSERT_NOT_REACHED();
  75. }
  76. return;
  77. case 300:
  78. if (fork() == 0) {
  79. execl("/bin/shutdown", "/bin/shutdown", "-n", nullptr);
  80. ASSERT_NOT_REACHED();
  81. }
  82. return;
  83. }
  84. #ifdef DEBUG_MENUS
  85. dbgprintf("WSMenu 1 item activated: '%s'\n", item.text().characters());
  86. #endif
  87. };
  88. }
  89. // NOTE: This ensures that the system menu has the correct dimensions.
  90. set_current_menubar(nullptr);
  91. new CTimer(300, [this] {
  92. static time_t last_update_time;
  93. time_t now = time(nullptr);
  94. if (now != last_update_time || m_cpu_monitor.is_dirty()) {
  95. tick_clock();
  96. last_update_time = now;
  97. m_cpu_monitor.set_dirty(false);
  98. }
  99. });
  100. invalidate();
  101. WSCompositor::the().compose();
  102. }
  103. WSWindowManager::~WSWindowManager()
  104. {
  105. }
  106. Retained<WSCursor> WSWindowManager::get_cursor(const String& name, const Point& hotspot)
  107. {
  108. auto path = m_wm_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  109. auto gb = GraphicsBitmap::load_from_file(path);
  110. if (gb)
  111. return WSCursor::create(*gb, hotspot);
  112. return WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/arrow.png"));
  113. }
  114. Retained<WSCursor> WSWindowManager::get_cursor(const String& name)
  115. {
  116. auto path = m_wm_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  117. auto gb = GraphicsBitmap::load_from_file(path);
  118. if (gb)
  119. return WSCursor::create(*gb);
  120. return WSCursor::create(*GraphicsBitmap::load_from_file("/res/cursors/arrow.png"));
  121. }
  122. void WSWindowManager::reload_config(bool set_screen)
  123. {
  124. m_wm_config = CConfigFile::get_for_app("WindowManager");
  125. m_double_click_speed = m_wm_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  126. if (set_screen)
  127. set_resolution(m_wm_config->read_num_entry("Screen", "Width", 1920),
  128. m_wm_config->read_num_entry("Screen", "Height", 1080));
  129. m_arrow_cursor = get_cursor("Arrow", { 2, 2 });
  130. m_resize_horizontally_cursor = get_cursor("ResizeH");
  131. m_resize_vertically_cursor = get_cursor("ResizeV");
  132. m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
  133. m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
  134. m_i_beam_cursor = get_cursor("IBeam");
  135. m_disallowed_cursor = get_cursor("Disallowed");
  136. m_move_cursor = get_cursor("Move");
  137. m_background_color = m_wm_config->read_color_entry("Colors", "Background", Color::Red);
  138. m_active_window_border_color = m_wm_config->read_color_entry("Colors", "ActiveWindowBorder", Color::Red);
  139. m_active_window_border_color2 = m_wm_config->read_color_entry("Colors", "ActiveWindowBorder2", Color::Red);
  140. m_active_window_title_color = m_wm_config->read_color_entry("Colors", "ActiveWindowTitle", Color::Red);
  141. m_inactive_window_border_color = m_wm_config->read_color_entry("Colors", "InactiveWindowBorder", Color::Red);
  142. m_inactive_window_border_color2 = m_wm_config->read_color_entry("Colors", "InactiveWindowBorder2", Color::Red);
  143. m_inactive_window_title_color = m_wm_config->read_color_entry("Colors", "InactiveWindowTitle", Color::Red);
  144. m_dragging_window_border_color = m_wm_config->read_color_entry("Colors", "DraggingWindowBorder", Color::Red);
  145. m_dragging_window_border_color2 = m_wm_config->read_color_entry("Colors", "DraggingWindowBorder2", Color::Red);
  146. m_dragging_window_title_color = m_wm_config->read_color_entry("Colors", "DraggingWindowTitle", Color::Red);
  147. m_highlight_window_border_color = m_wm_config->read_color_entry("Colors", "HighlightWindowBorder", Color::Red);
  148. m_highlight_window_border_color2 = m_wm_config->read_color_entry("Colors", "HighlightWindowBorder2", Color::Red);
  149. m_highlight_window_title_color = m_wm_config->read_color_entry("Colors", "HighlightWindowTitle", Color::Red);
  150. m_menu_selection_color = m_wm_config->read_color_entry("Colors", "MenuSelectionColor", Color::Red);
  151. }
  152. const Font& WSWindowManager::font() const
  153. {
  154. return Font::default_font();
  155. }
  156. const Font& WSWindowManager::window_title_font() const
  157. {
  158. return Font::default_bold_font();
  159. }
  160. const Font& WSWindowManager::menu_font() const
  161. {
  162. return Font::default_font();
  163. }
  164. const Font& WSWindowManager::app_menu_font() const
  165. {
  166. return Font::default_bold_font();
  167. }
  168. void WSWindowManager::tick_clock()
  169. {
  170. invalidate(menubar_rect());
  171. }
  172. void WSWindowManager::set_resolution(int width, int height)
  173. {
  174. WSCompositor::the().set_resolution(width, height);
  175. WSClientConnection::for_each_client([&](WSClientConnection& client) {
  176. client.notify_about_new_screen_rect(WSScreen::the().rect());
  177. });
  178. if (m_wm_config) {
  179. dbgprintf("Saving resolution: %dx%d to config file at %s.\n", width, height,
  180. m_wm_config->file_name().characters());
  181. m_wm_config->write_num_entry("Screen", "Width", width);
  182. m_wm_config->write_num_entry("Screen", "Height", height);
  183. m_wm_config->sync();
  184. }
  185. }
  186. int WSWindowManager::menubar_menu_margin() const
  187. {
  188. return 16;
  189. }
  190. void WSWindowManager::set_current_menu(WSMenu* menu)
  191. {
  192. if (m_current_menu == menu)
  193. return;
  194. if (m_current_menu)
  195. m_current_menu->close();
  196. if (menu)
  197. m_current_menu = menu->make_weak_ptr();
  198. }
  199. void WSWindowManager::set_current_menubar(WSMenuBar* menubar)
  200. {
  201. if (menubar)
  202. m_current_menubar = menubar->make_weak_ptr();
  203. else
  204. m_current_menubar = nullptr;
  205. #ifdef DEBUG_MENUS
  206. dbgprintf("[WM] Current menubar is now %p\n", menubar);
  207. #endif
  208. Point next_menu_location { menubar_menu_margin() / 2, 0 };
  209. int index = 0;
  210. for_each_active_menubar_menu([&](WSMenu& menu) {
  211. int text_width = index == 1 ? Font::default_bold_font().width(menu.name()) : font().width(menu.name());
  212. menu.set_rect_in_menubar({ next_menu_location.x() - menubar_menu_margin() / 2, 0, text_width + menubar_menu_margin(), menubar_rect().height() - 1 });
  213. menu.set_text_rect_in_menubar({ next_menu_location, { text_width, menubar_rect().height() } });
  214. next_menu_location.move_by(menu.rect_in_menubar().width(), 0);
  215. ++index;
  216. return true;
  217. });
  218. invalidate(menubar_rect());
  219. }
  220. void WSWindowManager::add_window(WSWindow& window)
  221. {
  222. m_windows_in_order.append(&window);
  223. if (window.is_fullscreen()) {
  224. WSEventLoop::the().post_event(window, make<WSResizeEvent>(window.rect(), WSScreen::the().rect()));
  225. window.set_rect(WSScreen::the().rect());
  226. }
  227. set_active_window(&window);
  228. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  229. m_switcher.refresh();
  230. if (window.listens_to_wm_events()) {
  231. for_each_window([&](WSWindow& other_window) {
  232. if (&window != &other_window) {
  233. tell_wm_listener_about_window(window, other_window);
  234. tell_wm_listener_about_window_icon(window, other_window);
  235. }
  236. return IterationDecision::Continue;
  237. });
  238. }
  239. tell_wm_listeners_window_state_changed(window);
  240. }
  241. void WSWindowManager::move_to_front_and_make_active(WSWindow& window)
  242. {
  243. if (window.is_blocked_by_modal_window())
  244. return;
  245. if (m_windows_in_order.tail() != &window)
  246. invalidate(window);
  247. m_windows_in_order.remove(&window);
  248. m_windows_in_order.append(&window);
  249. set_active_window(&window);
  250. }
  251. void WSWindowManager::remove_window(WSWindow& window)
  252. {
  253. invalidate(window);
  254. m_windows_in_order.remove(&window);
  255. if (window.is_active())
  256. pick_new_active_window();
  257. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  258. m_switcher.refresh();
  259. for_each_window_listening_to_wm_events([&window](WSWindow& listener) {
  260. if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowRemovals))
  261. return IterationDecision::Continue;
  262. if (window.client())
  263. WSEventLoop::the().post_event(listener, make<WSWMWindowRemovedEvent>(window.client()->client_id(), window.window_id()));
  264. return IterationDecision::Continue;
  265. });
  266. }
  267. void WSWindowManager::tell_wm_listener_about_window(WSWindow& listener, WSWindow& window)
  268. {
  269. if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowStateChanges))
  270. return;
  271. if (window.client())
  272. 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()));
  273. }
  274. void WSWindowManager::tell_wm_listener_about_window_rect(WSWindow& listener, WSWindow& window)
  275. {
  276. if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowRectChanges))
  277. return;
  278. if (window.client())
  279. WSEventLoop::the().post_event(listener, make<WSWMWindowRectChangedEvent>(window.client()->client_id(), window.window_id(), window.rect()));
  280. }
  281. void WSWindowManager::tell_wm_listener_about_window_icon(WSWindow& listener, WSWindow& window)
  282. {
  283. if (!(listener.wm_event_mask() & WSAPI_WMEventMask::WindowIconChanges))
  284. return;
  285. if (window.client())
  286. WSEventLoop::the().post_event(listener, make<WSWMWindowIconChangedEvent>(window.client()->client_id(), window.window_id(), window.icon_path()));
  287. }
  288. void WSWindowManager::tell_wm_listeners_window_state_changed(WSWindow& window)
  289. {
  290. for_each_window_listening_to_wm_events([&](WSWindow& listener) {
  291. tell_wm_listener_about_window(listener, window);
  292. return IterationDecision::Continue;
  293. });
  294. }
  295. void WSWindowManager::tell_wm_listeners_window_icon_changed(WSWindow& window)
  296. {
  297. for_each_window_listening_to_wm_events([&](WSWindow& listener) {
  298. tell_wm_listener_about_window_icon(listener, window);
  299. return IterationDecision::Continue;
  300. });
  301. }
  302. void WSWindowManager::tell_wm_listeners_window_rect_changed(WSWindow& window)
  303. {
  304. for_each_window_listening_to_wm_events([&](WSWindow& listener) {
  305. tell_wm_listener_about_window_rect(listener, window);
  306. return IterationDecision::Continue;
  307. });
  308. }
  309. void WSWindowManager::notify_title_changed(WSWindow& window)
  310. {
  311. if (window.type() != WSWindowType::Normal)
  312. return;
  313. dbgprintf("[WM] WSWindow{%p} title set to '%s'\n", &window, window.title().characters());
  314. invalidate(window.frame().rect());
  315. if (m_switcher.is_visible())
  316. m_switcher.refresh();
  317. tell_wm_listeners_window_state_changed(window);
  318. }
  319. void WSWindowManager::notify_rect_changed(WSWindow& window, const Rect& old_rect, const Rect& new_rect)
  320. {
  321. UNUSED_PARAM(old_rect);
  322. UNUSED_PARAM(new_rect);
  323. #ifdef RESIZE_DEBUG
  324. 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());
  325. #endif
  326. if (m_switcher.is_visible() && window.type() != WSWindowType::WindowSwitcher)
  327. m_switcher.refresh();
  328. tell_wm_listeners_window_rect_changed(window);
  329. }
  330. void WSWindowManager::notify_minimization_state_changed(WSWindow& window)
  331. {
  332. tell_wm_listeners_window_state_changed(window);
  333. if (window.is_active() && window.is_minimized())
  334. pick_new_active_window();
  335. }
  336. void WSWindowManager::pick_new_active_window()
  337. {
  338. for_each_visible_window_of_type_from_front_to_back(WSWindowType::Normal, [&](WSWindow& candidate) {
  339. set_active_window(&candidate);
  340. return IterationDecision::Break;
  341. });
  342. }
  343. void WSWindowManager::handle_menu_mouse_event(WSMenu& menu, const WSMouseEvent& event)
  344. {
  345. bool is_hover_with_any_menu_open = event.type() == WSMouseEvent::MouseMove && m_current_menu && (m_current_menu->menubar() || m_current_menu == m_system_menu);
  346. bool is_mousedown_with_left_button = event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left;
  347. bool should_open_menu = &menu != current_menu() && (is_hover_with_any_menu_open || is_mousedown_with_left_button);
  348. if (should_open_menu) {
  349. if (current_menu() == &menu)
  350. return;
  351. close_current_menu();
  352. if (!menu.is_empty()) {
  353. auto& menu_window = menu.ensure_menu_window();
  354. menu_window.move_to({ menu.rect_in_menubar().x(), menu.rect_in_menubar().bottom() + 2 });
  355. menu_window.set_visible(true);
  356. }
  357. m_current_menu = menu.make_weak_ptr();
  358. return;
  359. }
  360. if (event.type() == WSMouseEvent::MouseDown && event.button() == MouseButton::Left) {
  361. close_current_menu();
  362. return;
  363. }
  364. }
  365. void WSWindowManager::close_current_menu()
  366. {
  367. if (m_current_menu && m_current_menu->menu_window())
  368. m_current_menu->menu_window()->set_visible(false);
  369. m_current_menu = nullptr;
  370. }
  371. void WSWindowManager::handle_menubar_mouse_event(const WSMouseEvent& event)
  372. {
  373. for_each_active_menubar_menu([&](WSMenu& menu) {
  374. if (menu.rect_in_menubar().contains(event.position())) {
  375. handle_menu_mouse_event(menu, event);
  376. return false;
  377. }
  378. return true;
  379. });
  380. }
  381. void WSWindowManager::start_window_drag(WSWindow& window, const WSMouseEvent& event)
  382. {
  383. #ifdef DRAG_DEBUG
  384. printf("[WM] Begin dragging WSWindow{%p}\n", &window);
  385. #endif
  386. move_to_front_and_make_active(window);
  387. m_drag_window = window.make_weak_ptr();
  388. ;
  389. m_drag_origin = event.position();
  390. m_drag_window_origin = window.position();
  391. invalidate(window);
  392. }
  393. void WSWindowManager::start_window_resize(WSWindow& window, const Point& position, MouseButton button)
  394. {
  395. move_to_front_and_make_active(window);
  396. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  397. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  398. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  399. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  400. };
  401. Rect outer_rect = window.frame().rect();
  402. ASSERT(outer_rect.contains(position));
  403. int window_relative_x = position.x() - outer_rect.x();
  404. int window_relative_y = position.y() - outer_rect.y();
  405. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  406. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  407. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  408. if (m_resize_direction == ResizeDirection::None) {
  409. ASSERT(!m_resize_window);
  410. return;
  411. }
  412. #ifdef RESIZE_DEBUG
  413. printf("[WM] Begin resizing WSWindow{%p}\n", &window);
  414. #endif
  415. m_resizing_mouse_button = button;
  416. m_resize_window = window.make_weak_ptr();
  417. ;
  418. m_resize_origin = position;
  419. m_resize_window_original_rect = window.rect();
  420. invalidate(window);
  421. }
  422. void WSWindowManager::start_window_resize(WSWindow& window, const WSMouseEvent& event)
  423. {
  424. start_window_resize(window, event.position(), event.button());
  425. }
  426. bool WSWindowManager::process_ongoing_window_drag(WSMouseEvent& event, WSWindow*& hovered_window)
  427. {
  428. if (!m_drag_window)
  429. return false;
  430. if (event.type() == WSEvent::MouseUp && event.button() == MouseButton::Left) {
  431. #ifdef DRAG_DEBUG
  432. printf("[WM] Finish dragging WSWindow{%p}\n", m_drag_window.ptr());
  433. #endif
  434. invalidate(*m_drag_window);
  435. if (m_drag_window->rect().contains(event.position()))
  436. hovered_window = m_drag_window;
  437. if (m_drag_window->is_resizable()) {
  438. process_event_for_doubleclick(*m_drag_window, event);
  439. if (event.type() == WSEvent::MouseDoubleClick) {
  440. #if defined(DOUBLECLICK_DEBUG)
  441. dbgprintf("[WM] Click up became doubleclick!\n");
  442. #endif
  443. m_drag_window->set_maximized(!m_drag_window->is_maximized());
  444. }
  445. }
  446. m_drag_window = nullptr;
  447. return true;
  448. }
  449. if (event.type() == WSEvent::MouseMove) {
  450. #ifdef DRAG_DEBUG
  451. dbgprintf("[WM] Dragging [origin: %d,%d] now: %d,%d\n", m_drag_origin.x(), m_drag_origin.y(), event.x(), event.y());
  452. #endif
  453. Point pos = m_drag_window_origin.translated(event.position() - m_drag_origin);
  454. m_drag_window->set_position_without_repaint(pos);
  455. if (m_drag_window->rect().contains(event.position()))
  456. hovered_window = m_drag_window;
  457. return true;
  458. }
  459. return false;
  460. }
  461. bool WSWindowManager::process_ongoing_window_resize(const WSMouseEvent& event, WSWindow*& hovered_window)
  462. {
  463. if (!m_resize_window)
  464. return false;
  465. if (event.type() == WSEvent::MouseUp && event.button() == m_resizing_mouse_button) {
  466. #ifdef RESIZE_DEBUG
  467. printf("[WM] Finish resizing WSWindow{%p}\n", m_resize_window.ptr());
  468. #endif
  469. WSEventLoop::the().post_event(*m_resize_window, make<WSResizeEvent>(m_resize_window->rect(), m_resize_window->rect()));
  470. invalidate(*m_resize_window);
  471. if (m_resize_window->rect().contains(event.position()))
  472. hovered_window = m_resize_window;
  473. m_resize_window = nullptr;
  474. m_resizing_mouse_button = MouseButton::None;
  475. return true;
  476. }
  477. if (event.type() != WSEvent::MouseMove)
  478. return false;
  479. auto old_rect = m_resize_window->rect();
  480. int diff_x = event.x() - m_resize_origin.x();
  481. int diff_y = event.y() - m_resize_origin.y();
  482. int change_x = 0;
  483. int change_y = 0;
  484. int change_w = 0;
  485. int change_h = 0;
  486. switch (m_resize_direction) {
  487. case ResizeDirection::DownRight:
  488. change_w = diff_x;
  489. change_h = diff_y;
  490. break;
  491. case ResizeDirection::Right:
  492. change_w = diff_x;
  493. break;
  494. case ResizeDirection::UpRight:
  495. change_w = diff_x;
  496. change_y = diff_y;
  497. change_h = -diff_y;
  498. break;
  499. case ResizeDirection::Up:
  500. change_y = diff_y;
  501. change_h = -diff_y;
  502. break;
  503. case ResizeDirection::UpLeft:
  504. change_x = diff_x;
  505. change_w = -diff_x;
  506. change_y = diff_y;
  507. change_h = -diff_y;
  508. break;
  509. case ResizeDirection::Left:
  510. change_x = diff_x;
  511. change_w = -diff_x;
  512. break;
  513. case ResizeDirection::DownLeft:
  514. change_x = diff_x;
  515. change_w = -diff_x;
  516. change_h = diff_y;
  517. break;
  518. case ResizeDirection::Down:
  519. change_h = diff_y;
  520. break;
  521. default:
  522. ASSERT_NOT_REACHED();
  523. }
  524. auto new_rect = m_resize_window_original_rect;
  525. Size minimum_size { 50, 50 };
  526. new_rect.set_x(new_rect.x() + change_x);
  527. new_rect.set_y(new_rect.y() + change_y);
  528. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  529. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  530. if (!m_resize_window->size_increment().is_null()) {
  531. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  532. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  533. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  534. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  535. }
  536. if (new_rect.contains(event.position()))
  537. hovered_window = m_resize_window;
  538. if (m_resize_window->rect() == new_rect)
  539. return true;
  540. #ifdef RESIZE_DEBUG
  541. dbgprintf("[WM] Resizing [original: %s] now: %s\n",
  542. m_resize_window_original_rect.to_string().characters(),
  543. new_rect.to_string().characters());
  544. #endif
  545. m_resize_window->set_rect(new_rect);
  546. WSEventLoop::the().post_event(*m_resize_window, make<WSResizeEvent>(old_rect, new_rect));
  547. return true;
  548. }
  549. void WSWindowManager::set_cursor_tracking_button(WSButton* button)
  550. {
  551. m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
  552. }
  553. CElapsedTimer& WSWindowManager::DoubleClickInfo::click_clock(MouseButton button)
  554. {
  555. switch (button) {
  556. case MouseButton::Left:
  557. return m_left_click_clock;
  558. case MouseButton::Right:
  559. return m_right_click_clock;
  560. case MouseButton::Middle:
  561. return m_middle_click_clock;
  562. default:
  563. ASSERT_NOT_REACHED();
  564. }
  565. }
  566. // #define DOUBLECLICK_DEBUG
  567. void WSWindowManager::process_event_for_doubleclick(WSWindow& window, WSMouseEvent& event)
  568. {
  569. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  570. ASSERT(event.type() == WSEvent::MouseUp);
  571. if (&window != m_double_click_info.m_clicked_window) {
  572. // we either haven't clicked anywhere, or we haven't clicked on this
  573. // window. set the current click window, and reset the timers.
  574. #if defined(DOUBLECLICK_DEBUG)
  575. dbgprintf("Initial mouseup on window %p (previous was %p)\n", &window, m_double_click_info.m_clicked_window);
  576. #endif
  577. m_double_click_info.m_clicked_window = window.make_weak_ptr();
  578. m_double_click_info.reset();
  579. }
  580. auto& clock = m_double_click_info.click_clock(event.button());
  581. // if the clock is invalid, we haven't clicked with this button on this
  582. // window yet, so there's nothing to do.
  583. if (!clock.is_valid()) {
  584. clock.start();
  585. } else {
  586. int elapsed_since_last_click = clock.elapsed();
  587. clock.start();
  588. // FIXME: It might be a sensible idea to also add a distance travel check.
  589. // If the pointer moves too far, it's not a double click.
  590. if (elapsed_since_last_click < m_double_click_speed) {
  591. #if defined(DOUBLECLICK_DEBUG)
  592. dbgprintf("Transforming MouseUp to MouseDoubleClick!\n");
  593. #endif
  594. event = WSMouseEvent(WSEvent::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  595. // invalidate this now we've delivered a doubleclick, otherwise
  596. // tripleclick will deliver two doubleclick events (incorrectly).
  597. clock = CElapsedTimer();
  598. } else {
  599. // too slow; try again
  600. clock.start();
  601. }
  602. }
  603. }
  604. void WSWindowManager::deliver_mouse_event(WSWindow& window, WSMouseEvent& event)
  605. {
  606. window.event(event);
  607. if (event.type() == WSEvent::MouseUp) {
  608. process_event_for_doubleclick(window, event);
  609. if (event.type() == WSEvent::MouseDoubleClick)
  610. window.event(event);
  611. }
  612. }
  613. void WSWindowManager::process_mouse_event(WSMouseEvent& event, WSWindow*& hovered_window)
  614. {
  615. hovered_window = nullptr;
  616. if (process_ongoing_window_drag(event, hovered_window))
  617. return;
  618. if (process_ongoing_window_resize(event, hovered_window))
  619. return;
  620. if (m_cursor_tracking_button)
  621. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  622. // This is quite hackish, but it's how the WSButton hover effect is implemented.
  623. if (m_hovered_button && event.type() == WSEvent::MouseMove)
  624. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  625. HashTable<WSWindow*> windows_who_received_mouse_event_due_to_cursor_tracking;
  626. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  627. if (!window->global_cursor_tracking())
  628. continue;
  629. ASSERT(window->is_visible()); // Maybe this should be supported? Idk. Let's catch it and think about it later.
  630. ASSERT(!window->is_minimized()); // Maybe this should also be supported? Idk.
  631. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  632. auto translated_event = event.translated(-window->position());
  633. deliver_mouse_event(*window, translated_event);
  634. }
  635. if (menubar_rect().contains(event.position())) {
  636. handle_menubar_mouse_event(event);
  637. return;
  638. }
  639. if (m_current_menu && m_current_menu->menu_window()) {
  640. auto& window = *m_current_menu->menu_window();
  641. bool event_is_inside_current_menu = window.rect().contains(event.position());
  642. if (!event_is_inside_current_menu) {
  643. if (m_current_menu->hovered_item())
  644. m_current_menu->clear_hovered_item();
  645. if (event.type() == WSEvent::MouseDown || event.type() == WSEvent::MouseUp)
  646. close_current_menu();
  647. } else {
  648. hovered_window = &window;
  649. auto translated_event = event.translated(-window.position());
  650. deliver_mouse_event(window, translated_event);
  651. }
  652. return;
  653. }
  654. WSWindow* event_window_with_frame = nullptr;
  655. for_each_visible_window_from_front_to_back([&](WSWindow& window) {
  656. auto window_frame_rect = window.frame().rect();
  657. if (!window_frame_rect.contains(event.position()))
  658. return IterationDecision::Continue;
  659. if (&window != m_resize_candidate.ptr())
  660. clear_resize_candidate();
  661. // First check if we should initiate a drag or resize (Logo+LMB or Logo+RMB).
  662. // In those cases, the event is swallowed by the window manager.
  663. if (window.type() == WSWindowType::Normal) {
  664. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Left) {
  665. hovered_window = &window;
  666. start_window_drag(window, event);
  667. return IterationDecision::Break;
  668. }
  669. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == WSEvent::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  670. hovered_window = &window;
  671. start_window_resize(window, event);
  672. return IterationDecision::Break;
  673. }
  674. }
  675. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  676. if (window.rect().contains(event.position())) {
  677. if (window.type() == WSWindowType::Normal && event.type() == WSEvent::MouseDown)
  678. move_to_front_and_make_active(window);
  679. hovered_window = &window;
  680. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window)) {
  681. auto translated_event = event.translated(-window.position());
  682. deliver_mouse_event(window, translated_event);
  683. }
  684. return IterationDecision::Break;
  685. }
  686. // We are hitting the frame, pass the event along to WSWindowFrame.
  687. window.frame().on_mouse_event(event.translated(-window_frame_rect.location()));
  688. event_window_with_frame = &window;
  689. return IterationDecision::Break;
  690. });
  691. if (event_window_with_frame != m_resize_candidate.ptr())
  692. clear_resize_candidate();
  693. }
  694. void WSWindowManager::clear_resize_candidate()
  695. {
  696. if (m_resize_candidate)
  697. WSCompositor::the().invalidate_cursor();
  698. m_resize_candidate = nullptr;
  699. }
  700. bool WSWindowManager::any_opaque_window_contains_rect(const Rect& rect)
  701. {
  702. bool found_containing_window = false;
  703. for_each_window([&](WSWindow& window) {
  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. // FIXME: Just because the window has an alpha channel doesn't mean it's not opaque.
  712. // Maybe there's some way we could know this?
  713. return IterationDecision::Continue;
  714. }
  715. if (window.frame().rect().contains(rect)) {
  716. found_containing_window = true;
  717. return IterationDecision::Break;
  718. }
  719. return IterationDecision::Continue;
  720. });
  721. return found_containing_window;
  722. };
  723. bool WSWindowManager::any_opaque_window_above_this_one_contains_rect(const WSWindow& a_window, const Rect& rect)
  724. {
  725. bool found_containing_window = false;
  726. bool checking = false;
  727. for_each_visible_window_from_back_to_front([&](WSWindow& window) {
  728. if (&window == &a_window) {
  729. checking = true;
  730. return IterationDecision::Continue;
  731. }
  732. if (!checking)
  733. return IterationDecision::Continue;
  734. if (!window.is_visible())
  735. return IterationDecision::Continue;
  736. if (window.is_minimized())
  737. return IterationDecision::Continue;
  738. if (window.opacity() < 1.0f)
  739. return IterationDecision::Continue;
  740. if (window.has_alpha_channel())
  741. return IterationDecision::Continue;
  742. if (window.frame().rect().contains(rect)) {
  743. found_containing_window = true;
  744. return IterationDecision::Break;
  745. }
  746. return IterationDecision::Continue;
  747. });
  748. return found_containing_window;
  749. };
  750. Rect WSWindowManager::menubar_rect() const
  751. {
  752. if (active_fullscreen_window())
  753. return {};
  754. return { 0, 0, WSScreen::the().rect().width(), 18 };
  755. }
  756. void WSWindowManager::draw_window_switcher()
  757. {
  758. if (m_switcher.is_visible())
  759. m_switcher.draw();
  760. }
  761. void WSWindowManager::event(CEvent& event)
  762. {
  763. if (static_cast<WSEvent&>(event).is_mouse_event()) {
  764. WSWindow* hovered_window = nullptr;
  765. process_mouse_event(static_cast<WSMouseEvent&>(event), hovered_window);
  766. set_hovered_window(hovered_window);
  767. return;
  768. }
  769. if (static_cast<WSEvent&>(event).is_key_event()) {
  770. auto& key_event = static_cast<const WSKeyEvent&>(event);
  771. m_keyboard_modifiers = key_event.modifiers();
  772. if (key_event.type() == WSEvent::KeyDown && key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab)
  773. m_switcher.show();
  774. if (m_switcher.is_visible()) {
  775. m_switcher.on_key_event(key_event);
  776. return;
  777. }
  778. if (m_active_window)
  779. return m_active_window->event(event);
  780. return;
  781. }
  782. CObject::event(event);
  783. }
  784. void WSWindowManager::set_highlight_window(WSWindow* window)
  785. {
  786. if (window == m_highlight_window)
  787. return;
  788. if (auto* previous_highlight_window = m_highlight_window.ptr())
  789. invalidate(*previous_highlight_window);
  790. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  791. if (m_highlight_window)
  792. invalidate(*m_highlight_window);
  793. }
  794. void WSWindowManager::set_active_window(WSWindow* window)
  795. {
  796. if (window && window->is_blocked_by_modal_window())
  797. return;
  798. if (window->type() != WSWindowType::Normal)
  799. return;
  800. if (window == m_active_window)
  801. return;
  802. auto* previously_active_window = m_active_window.ptr();
  803. if (previously_active_window) {
  804. WSEventLoop::the().post_event(*previously_active_window, make<WSEvent>(WSEvent::WindowDeactivated));
  805. invalidate(*previously_active_window);
  806. }
  807. m_active_window = window->make_weak_ptr();
  808. if (m_active_window) {
  809. WSEventLoop::the().post_event(*m_active_window, make<WSEvent>(WSEvent::WindowActivated));
  810. invalidate(*m_active_window);
  811. auto* client = window->client();
  812. ASSERT(client);
  813. set_current_menubar(client->app_menubar());
  814. if (previously_active_window)
  815. tell_wm_listeners_window_state_changed(*previously_active_window);
  816. tell_wm_listeners_window_state_changed(*m_active_window);
  817. }
  818. }
  819. void WSWindowManager::set_hovered_window(WSWindow* window)
  820. {
  821. if (m_hovered_window == window)
  822. return;
  823. if (m_hovered_window)
  824. WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowLeft));
  825. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  826. if (m_hovered_window)
  827. WSEventLoop::the().post_event(*m_hovered_window, make<WSEvent>(WSEvent::WindowEntered));
  828. }
  829. void WSWindowManager::invalidate()
  830. {
  831. WSCompositor::the().invalidate();
  832. }
  833. void WSWindowManager::invalidate(const Rect& rect)
  834. {
  835. WSCompositor::the().invalidate(rect);
  836. }
  837. void WSWindowManager::invalidate(const WSWindow& window)
  838. {
  839. invalidate(window.frame().rect());
  840. }
  841. void WSWindowManager::invalidate(const WSWindow& window, const Rect& rect)
  842. {
  843. if (rect.is_empty()) {
  844. invalidate(window);
  845. return;
  846. }
  847. auto outer_rect = window.frame().rect();
  848. auto inner_rect = rect;
  849. inner_rect.move_by(window.position());
  850. // FIXME: This seems slightly wrong; the inner rect shouldn't intersect the border part of the outer rect.
  851. inner_rect.intersect(outer_rect);
  852. invalidate(inner_rect);
  853. }
  854. void WSWindowManager::close_menu(WSMenu& menu)
  855. {
  856. if (current_menu() == &menu)
  857. close_current_menu();
  858. }
  859. void WSWindowManager::close_menubar(WSMenuBar& menubar)
  860. {
  861. if (current_menubar() == &menubar)
  862. set_current_menubar(nullptr);
  863. }
  864. const WSClientConnection* WSWindowManager::active_client() const
  865. {
  866. if (m_active_window)
  867. return m_active_window->client();
  868. return nullptr;
  869. }
  870. void WSWindowManager::notify_client_changed_app_menubar(WSClientConnection& client)
  871. {
  872. if (active_client() == &client)
  873. set_current_menubar(client.app_menubar());
  874. invalidate(menubar_rect());
  875. }
  876. const WSCursor& WSWindowManager::active_cursor() const
  877. {
  878. if (m_drag_window)
  879. return *m_move_cursor;
  880. if (m_resize_window || m_resize_candidate) {
  881. switch (m_resize_direction) {
  882. case ResizeDirection::Up:
  883. case ResizeDirection::Down:
  884. return *m_resize_vertically_cursor;
  885. case ResizeDirection::Left:
  886. case ResizeDirection::Right:
  887. return *m_resize_horizontally_cursor;
  888. case ResizeDirection::UpLeft:
  889. case ResizeDirection::DownRight:
  890. return *m_resize_diagonally_tlbr_cursor;
  891. case ResizeDirection::UpRight:
  892. case ResizeDirection::DownLeft:
  893. return *m_resize_diagonally_bltr_cursor;
  894. case ResizeDirection::None:
  895. break;
  896. }
  897. }
  898. if (m_hovered_window && m_hovered_window->override_cursor())
  899. return *m_hovered_window->override_cursor();
  900. return *m_arrow_cursor;
  901. }
  902. void WSWindowManager::set_hovered_button(WSButton* button)
  903. {
  904. m_hovered_button = button ? button->make_weak_ptr() : nullptr;
  905. }
  906. void WSWindowManager::set_resize_candidate(WSWindow& window, ResizeDirection direction)
  907. {
  908. m_resize_candidate = window.make_weak_ptr();
  909. m_resize_direction = direction;
  910. }
  911. Rect WSWindowManager::maximized_window_rect(const WSWindow& window) const
  912. {
  913. Rect rect = WSScreen::the().rect();
  914. // Subtract window title bar (leaving the border)
  915. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  916. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  917. // Subtract menu bar
  918. rect.set_y(rect.y() + menubar_rect().height());
  919. rect.set_height(rect.height() - menubar_rect().height());
  920. // Subtract taskbar window height if present
  921. const_cast<WSWindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WSWindowType::Taskbar, [&rect](WSWindow& taskbar_window) {
  922. rect.set_height(rect.height() - taskbar_window.height());
  923. return IterationDecision::Break;
  924. });
  925. return rect;
  926. }