WSWindowManager.cpp 41 KB

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