WSWindowManager.cpp 37 KB

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