WindowManager.cpp 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "WindowManager.h"
  27. #include "Compositor.h"
  28. #include "EventLoop.h"
  29. #include "Menu.h"
  30. #include "MenuBar.h"
  31. #include "MenuItem.h"
  32. #include "Screen.h"
  33. #include "Window.h"
  34. #include <AK/LogStream.h>
  35. #include <AK/StdLibExtras.h>
  36. #include <AK/Vector.h>
  37. #include <LibGfx/Bitmap.h>
  38. #include <LibGfx/CharacterBitmap.h>
  39. #include <LibGfx/Font.h>
  40. #include <LibGfx/Painter.h>
  41. #include <LibGfx/StylePainter.h>
  42. #include <LibGfx/SystemTheme.h>
  43. #include <WindowServer/AppletManager.h>
  44. #include <WindowServer/Button.h>
  45. #include <WindowServer/ClientConnection.h>
  46. #include <WindowServer/Cursor.h>
  47. #include <WindowServer/WindowClientEndpoint.h>
  48. #include <errno.h>
  49. #include <serenity.h>
  50. #include <stdio.h>
  51. #include <time.h>
  52. #include <unistd.h>
  53. namespace WindowServer {
  54. static WindowManager* s_the;
  55. WindowManager& WindowManager::the()
  56. {
  57. ASSERT(s_the);
  58. return *s_the;
  59. }
  60. WindowManager::WindowManager(const Gfx::PaletteImpl& palette)
  61. : m_palette(palette)
  62. {
  63. s_the = this;
  64. reload_config();
  65. Compositor::the().did_construct_window_manager({});
  66. }
  67. WindowManager::~WindowManager()
  68. {
  69. }
  70. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name)
  71. {
  72. static const auto s_default_cursor_path = "/res/cursors/arrow.x2y2.png";
  73. auto path = m_config->read_entry("Cursor", name, s_default_cursor_path);
  74. auto gb = Gfx::Bitmap::load_from_file(path, compositor_icon_scale());
  75. if (gb)
  76. return Cursor::create(*gb, path);
  77. return Cursor::create(*Gfx::Bitmap::load_from_file(s_default_cursor_path), s_default_cursor_path);
  78. }
  79. void WindowManager::reload_config()
  80. {
  81. m_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  82. m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  83. m_hidden_cursor = get_cursor("Hidden");
  84. m_arrow_cursor = get_cursor("Arrow");
  85. m_hand_cursor = get_cursor("Hand");
  86. m_help_cursor = get_cursor("Help");
  87. m_resize_horizontally_cursor = get_cursor("ResizeH");
  88. m_resize_vertically_cursor = get_cursor("ResizeV");
  89. m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
  90. m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
  91. m_resize_column_cursor = get_cursor("ResizeColumn");
  92. m_resize_row_cursor = get_cursor("ResizeRow");
  93. m_i_beam_cursor = get_cursor("IBeam");
  94. m_disallowed_cursor = get_cursor("Disallowed");
  95. m_move_cursor = get_cursor("Move");
  96. m_drag_cursor = get_cursor("Drag");
  97. m_wait_cursor = get_cursor("Wait");
  98. m_crosshair_cursor = get_cursor("Crosshair");
  99. }
  100. const Gfx::Font& WindowManager::font() const
  101. {
  102. return Gfx::FontDatabase::default_font();
  103. }
  104. const Gfx::Font& WindowManager::window_title_font() const
  105. {
  106. return Gfx::FontDatabase::default_bold_font();
  107. }
  108. bool WindowManager::set_resolution(int width, int height, int scale)
  109. {
  110. bool success = Compositor::the().set_resolution(width, height, scale);
  111. MenuManager::the().set_needs_window_resize();
  112. ClientConnection::for_each_client([&](ClientConnection& client) {
  113. client.notify_about_new_screen_rect(Screen::the().rect());
  114. });
  115. if (success) {
  116. for_each_window([](Window& window) {
  117. window.recalculate_rect();
  118. return IterationDecision::Continue;
  119. });
  120. }
  121. if (m_config) {
  122. if (success) {
  123. dbgln("Saving resolution: {} @ {}x to config file at {}", Gfx::IntSize(width, height), scale, m_config->file_name());
  124. m_config->write_num_entry("Screen", "Width", width);
  125. m_config->write_num_entry("Screen", "Height", height);
  126. m_config->write_num_entry("Screen", "ScaleFactor", scale);
  127. m_config->sync();
  128. } else {
  129. dbgln("Saving fallback resolution: {} @1x to config file at {}", resolution(), m_config->file_name());
  130. m_config->write_num_entry("Screen", "Width", resolution().width());
  131. m_config->write_num_entry("Screen", "Height", resolution().height());
  132. m_config->write_num_entry("Screen", "ScaleFactor", 1);
  133. m_config->sync();
  134. }
  135. }
  136. return success;
  137. }
  138. Gfx::IntSize WindowManager::resolution() const
  139. {
  140. return Screen::the().size();
  141. }
  142. void WindowManager::set_acceleration_factor(double factor)
  143. {
  144. Screen::the().set_acceleration_factor(factor);
  145. dbgln("Saving acceleration factor {} to config file at {}", factor, m_config->file_name());
  146. m_config->write_entry("Mouse", "AccelerationFactor", String::formatted("{}", factor));
  147. m_config->sync();
  148. }
  149. void WindowManager::set_scroll_step_size(unsigned step_size)
  150. {
  151. Screen::the().set_scroll_step_size(step_size);
  152. dbgln("Saving scroll step size {} to config file at {}", step_size, m_config->file_name());
  153. m_config->write_entry("Mouse", "ScrollStepSize", String::number(step_size));
  154. m_config->sync();
  155. }
  156. int WindowManager::scale_factor() const
  157. {
  158. return Screen::the().scale_factor();
  159. }
  160. void WindowManager::add_window(Window& window)
  161. {
  162. bool is_first_window = m_windows_in_order.is_empty();
  163. m_windows_in_order.append(&window);
  164. if (window.is_fullscreen()) {
  165. Core::EventLoop::current().post_event(window, make<ResizeEvent>(Screen::the().rect()));
  166. window.set_rect(Screen::the().rect());
  167. }
  168. if (window.type() != WindowType::Desktop || is_first_window)
  169. set_active_window(&window);
  170. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  171. m_switcher.refresh();
  172. Compositor::the().invalidate_occlusions();
  173. if (window.listens_to_wm_events()) {
  174. for_each_window([&](Window& other_window) {
  175. if (&window != &other_window) {
  176. tell_wm_listener_about_window(window, other_window);
  177. tell_wm_listener_about_window_icon(window, other_window);
  178. }
  179. return IterationDecision::Continue;
  180. });
  181. }
  182. tell_wm_listeners_window_state_changed(window);
  183. }
  184. void WindowManager::move_to_front_and_make_active(Window& window)
  185. {
  186. auto move_window_to_front = [&](Window& wnd, bool make_active, bool make_input) {
  187. if (wnd.is_accessory()) {
  188. auto* parent = wnd.parent_window();
  189. do_move_to_front(*parent, true, false);
  190. make_active = false;
  191. for (auto& accessory_window : parent->accessory_windows()) {
  192. if (accessory_window && accessory_window.ptr() != &wnd)
  193. do_move_to_front(*accessory_window, false, false);
  194. }
  195. }
  196. do_move_to_front(wnd, make_active, make_input);
  197. };
  198. // If a window that is currently blocked by a modal child is being
  199. // brought to the front, bring the entire stack of modal windows
  200. // to the front and activate the modal window. Also set the
  201. // active input window to that same window (which would pull
  202. // active input from any accessory window)
  203. for_each_window_in_modal_stack(window, [&](auto& w, bool is_stack_top) {
  204. move_window_to_front(w, is_stack_top, is_stack_top);
  205. return IterationDecision::Continue;
  206. });
  207. Compositor::the().invalidate_occlusions();
  208. }
  209. void WindowManager::do_move_to_front(Window& window, bool make_active, bool make_input)
  210. {
  211. if (m_windows_in_order.tail() != &window)
  212. window.invalidate();
  213. m_windows_in_order.remove(&window);
  214. m_windows_in_order.append(&window);
  215. if (make_active)
  216. set_active_window(&window, make_input);
  217. if (m_switcher.is_visible()) {
  218. m_switcher.refresh();
  219. if (!window.is_accessory()) {
  220. m_switcher.select_window(window);
  221. set_highlight_window(&window);
  222. }
  223. }
  224. for (auto& child_window : window.child_windows()) {
  225. if (child_window)
  226. do_move_to_front(*child_window, make_active, make_input);
  227. }
  228. }
  229. void WindowManager::remove_window(Window& window)
  230. {
  231. m_windows_in_order.remove(&window);
  232. auto* active = active_window();
  233. auto* active_input = active_input_window();
  234. if (active == &window || active_input == &window || (active && window.is_descendant_of(*active)) || (active_input && active_input != active && window.is_descendant_of(*active_input)))
  235. pick_new_active_window(&window);
  236. Compositor::the().invalidate_screen(window.frame().render_rect());
  237. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  238. m_switcher.refresh();
  239. Compositor::the().invalidate_occlusions();
  240. for_each_window_listening_to_wm_events([&window](Window& listener) {
  241. if (!(listener.wm_event_mask() & WMEventMask::WindowRemovals))
  242. return IterationDecision::Continue;
  243. if (!window.is_internal() && !window.is_modal())
  244. listener.client()->post_message(Messages::WindowClient::WM_WindowRemoved(listener.window_id(), window.client_id(), window.window_id()));
  245. return IterationDecision::Continue;
  246. });
  247. }
  248. void WindowManager::tell_wm_listener_about_window(Window& listener, Window& window)
  249. {
  250. if (!(listener.wm_event_mask() & WMEventMask::WindowStateChanges))
  251. return;
  252. if (window.is_internal())
  253. return;
  254. auto* parent = window.parent_window();
  255. listener.client()->post_message(Messages::WindowClient::WM_WindowStateChanged(listener.window_id(), window.client_id(), window.window_id(), parent ? parent->client_id() : -1, parent ? parent->window_id() : -1, window.is_active(), window.is_minimized(), window.is_modal_dont_unparent(), window.is_frameless(), (i32)window.type(), window.title(), window.rect(), window.progress()));
  256. }
  257. void WindowManager::tell_wm_listener_about_window_rect(Window& listener, Window& window)
  258. {
  259. if (!(listener.wm_event_mask() & WMEventMask::WindowRectChanges))
  260. return;
  261. if (window.is_internal())
  262. return;
  263. listener.client()->post_message(Messages::WindowClient::WM_WindowRectChanged(listener.window_id(), window.client_id(), window.window_id(), window.rect()));
  264. }
  265. void WindowManager::tell_wm_listener_about_window_icon(Window& listener, Window& window)
  266. {
  267. if (!(listener.wm_event_mask() & WMEventMask::WindowIconChanges))
  268. return;
  269. if (window.is_internal())
  270. return;
  271. listener.client()->post_message(Messages::WindowClient::WM_WindowIconBitmapChanged(listener.window_id(), window.client_id(), window.window_id(), window.icon().to_shareable_bitmap()));
  272. }
  273. void WindowManager::tell_wm_listeners_window_state_changed(Window& window)
  274. {
  275. for_each_window_listening_to_wm_events([&](Window& listener) {
  276. tell_wm_listener_about_window(listener, window);
  277. return IterationDecision::Continue;
  278. });
  279. }
  280. void WindowManager::tell_wm_listeners_window_icon_changed(Window& window)
  281. {
  282. for_each_window_listening_to_wm_events([&](Window& listener) {
  283. tell_wm_listener_about_window_icon(listener, window);
  284. return IterationDecision::Continue;
  285. });
  286. }
  287. void WindowManager::tell_wm_listeners_window_rect_changed(Window& window)
  288. {
  289. for_each_window_listening_to_wm_events([&](Window& listener) {
  290. tell_wm_listener_about_window_rect(listener, window);
  291. return IterationDecision::Continue;
  292. });
  293. }
  294. void WindowManager::notify_title_changed(Window& window)
  295. {
  296. if (window.type() != WindowType::Normal)
  297. return;
  298. dbgln_if(WINDOWMANAGER_DEBUG, "[WM] Window({}) title set to '{}'", &window, window.title());
  299. if (m_switcher.is_visible())
  300. m_switcher.refresh();
  301. tell_wm_listeners_window_state_changed(window);
  302. }
  303. void WindowManager::notify_modal_unparented(Window& window)
  304. {
  305. if (window.type() != WindowType::Normal)
  306. return;
  307. dbgln_if(WINDOWMANAGER_DEBUG, "[WM] Window({}) was unparented", &window);
  308. if (m_switcher.is_visible())
  309. m_switcher.refresh();
  310. tell_wm_listeners_window_state_changed(window);
  311. }
  312. void WindowManager::notify_rect_changed(Window& window, const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  313. {
  314. dbgln_if(RESIZE_DEBUG, "[WM] Window({}) rect changed {} -> {}", &window, old_rect, new_rect);
  315. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  316. m_switcher.refresh();
  317. tell_wm_listeners_window_rect_changed(window);
  318. if (window.type() == WindowType::MenuApplet)
  319. AppletManager::the().calculate_applet_rects(MenuManager::the().window());
  320. MenuManager::the().refresh();
  321. }
  322. void WindowManager::notify_opacity_changed(Window&)
  323. {
  324. Compositor::the().invalidate_occlusions();
  325. }
  326. void WindowManager::notify_minimization_state_changed(Window& window)
  327. {
  328. tell_wm_listeners_window_state_changed(window);
  329. if (window.client())
  330. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  331. if (window.is_active() && window.is_minimized())
  332. pick_new_active_window(&window);
  333. }
  334. void WindowManager::notify_occlusion_state_changed(Window& window)
  335. {
  336. if (window.client())
  337. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  338. }
  339. void WindowManager::notify_progress_changed(Window& window)
  340. {
  341. tell_wm_listeners_window_state_changed(window);
  342. }
  343. bool WindowManager::pick_new_active_window(Window* previous_active)
  344. {
  345. bool new_window_picked = false;
  346. Window* first_candidate = nullptr;
  347. for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& candidate) {
  348. if (candidate.is_destroyed())
  349. return IterationDecision::Continue;
  350. if (previous_active != first_candidate)
  351. first_candidate = &candidate;
  352. if ((!previous_active && !candidate.is_accessory()) || (previous_active && !candidate.is_accessory_of(*previous_active))) {
  353. set_active_window(&candidate);
  354. new_window_picked = true;
  355. return IterationDecision::Break;
  356. }
  357. return IterationDecision::Continue;
  358. });
  359. if (!new_window_picked) {
  360. set_active_window(first_candidate);
  361. new_window_picked = first_candidate != nullptr;
  362. }
  363. return new_window_picked;
  364. }
  365. void WindowManager::start_window_move(Window& window, const MouseEvent& event)
  366. {
  367. dbgln_if(MOVE_DEBUG, "[WM] Begin moving Window({})", &window);
  368. move_to_front_and_make_active(window);
  369. m_move_window = window;
  370. m_move_window->set_default_positioned(false);
  371. m_move_origin = event.position();
  372. m_move_window_origin = window.position();
  373. window.invalidate();
  374. }
  375. void WindowManager::start_window_resize(Window& window, const Gfx::IntPoint& position, MouseButton button)
  376. {
  377. move_to_front_and_make_active(window);
  378. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  379. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  380. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  381. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  382. };
  383. Gfx::IntRect outer_rect = window.frame().rect();
  384. if (!outer_rect.contains(position)) {
  385. // FIXME: This used to be an ASSERT but crashing WindowServer over this seems silly.
  386. dbgln("FIXME: !outer_rect.contains(position): outer_rect={}, position={}", outer_rect, position);
  387. }
  388. int window_relative_x = position.x() - outer_rect.x();
  389. int window_relative_y = position.y() - outer_rect.y();
  390. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  391. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  392. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  393. if (m_resize_direction == ResizeDirection::None) {
  394. ASSERT(!m_resize_window);
  395. return;
  396. }
  397. dbgln_if(RESIZE_DEBUG, "[WM] Begin resizing Window({})", &window);
  398. m_resizing_mouse_button = button;
  399. m_resize_window = window;
  400. m_resize_origin = position;
  401. m_resize_window_original_rect = window.rect();
  402. m_active_input_tracking_window = nullptr;
  403. window.invalidate();
  404. if (hot_area_row == 0 || hot_area_column == 0) {
  405. m_resize_window->set_default_positioned(false);
  406. }
  407. }
  408. void WindowManager::start_window_resize(Window& window, const MouseEvent& event)
  409. {
  410. start_window_resize(window, event.position(), event.button());
  411. }
  412. bool WindowManager::process_ongoing_window_move(MouseEvent& event, Window*& hovered_window)
  413. {
  414. if (!m_move_window)
  415. return false;
  416. if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  417. dbgln_if(MOVE_DEBUG, "[WM] Finish moving Window({})", m_move_window);
  418. m_move_window->invalidate();
  419. if (m_move_window->rect().contains(event.position()))
  420. hovered_window = m_move_window;
  421. if (m_move_window->is_resizable()) {
  422. process_event_for_doubleclick(*m_move_window, event);
  423. if (event.type() == Event::MouseDoubleClick) {
  424. #if defined(DOUBLECLICK_DEBUG)
  425. dbgln("[WM] Click up became doubleclick!");
  426. #endif
  427. m_move_window->set_maximized(!m_move_window->is_maximized());
  428. }
  429. }
  430. m_move_window = nullptr;
  431. return true;
  432. }
  433. if (event.type() == Event::MouseMove) {
  434. if constexpr (MOVE_DEBUG) {
  435. dbgln("[WM] Moving, origin: {}, now: {}", m_move_origin, event.position());
  436. if (m_move_window->is_maximized())
  437. dbgln(" [!] The window is still maximized. Not moving yet.");
  438. }
  439. const int tiling_deadzone = 10;
  440. const int secondary_deadzone = 2;
  441. auto desktop = desktop_rect();
  442. if (m_move_window->is_maximized()) {
  443. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  444. if (pixels_moved_from_start > 5) {
  445. // dbgln("[WM] de-maximizing window");
  446. m_move_origin = event.position();
  447. if (m_move_origin.y() <= secondary_deadzone + desktop.top())
  448. return true;
  449. m_move_window->set_maximized(false, event.position());
  450. m_move_window_origin = m_move_window->position();
  451. }
  452. } else {
  453. bool is_resizable = m_move_window->is_resizable();
  454. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  455. if (is_resizable && event.x() <= tiling_deadzone) {
  456. if (event.y() <= tiling_deadzone + desktop.top())
  457. m_move_window->set_tiled(WindowTileType::TopLeft);
  458. else if (event.y() >= desktop.height() - tiling_deadzone)
  459. m_move_window->set_tiled(WindowTileType::BottomLeft);
  460. else
  461. m_move_window->set_tiled(WindowTileType::Left);
  462. } else if (is_resizable && event.x() >= Screen::the().width() - tiling_deadzone) {
  463. if (event.y() <= tiling_deadzone + desktop.top())
  464. m_move_window->set_tiled(WindowTileType::TopRight);
  465. else if (event.y() >= desktop.height() - tiling_deadzone)
  466. m_move_window->set_tiled(WindowTileType::BottomRight);
  467. else
  468. m_move_window->set_tiled(WindowTileType::Right);
  469. } else if (is_resizable && event.y() <= secondary_deadzone + desktop.top()) {
  470. m_move_window->set_tiled(WindowTileType::Top);
  471. } else if (is_resizable && event.y() >= desktop.bottom() - secondary_deadzone) {
  472. m_move_window->set_tiled(WindowTileType::Bottom);
  473. } else if (m_move_window->tiled() == WindowTileType::None) {
  474. Gfx::IntPoint pos = m_move_window_origin.translated(event.position() - m_move_origin);
  475. m_move_window->set_position_without_repaint(pos);
  476. // "Bounce back" the window if it would end up too far outside the screen.
  477. // If the user has let go of Mod_Logo, maybe they didn't intentionally press it to begin with. Therefore, refuse to go into a state where knowledge about super-drags is necessary.
  478. bool force_titlebar_visible = !(m_keyboard_modifiers & Mod_Logo);
  479. m_move_window->nudge_into_desktop(force_titlebar_visible);
  480. } else if (pixels_moved_from_start > 5) {
  481. m_move_window->set_untiled(event.position());
  482. m_move_origin = event.position();
  483. m_move_window_origin = m_move_window->position();
  484. }
  485. }
  486. if (m_move_window->rect().contains(event.position()))
  487. hovered_window = m_move_window;
  488. return true;
  489. }
  490. return false;
  491. }
  492. bool WindowManager::process_ongoing_window_resize(const MouseEvent& event, Window*& hovered_window)
  493. {
  494. if (!m_resize_window)
  495. return false;
  496. if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
  497. dbgln_if(RESIZE_DEBUG, "[WM] Finish resizing Window({})", m_resize_window);
  498. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect()));
  499. m_resize_window->invalidate();
  500. if (m_resize_window->rect().contains(event.position()))
  501. hovered_window = m_resize_window;
  502. m_resize_window = nullptr;
  503. m_resizing_mouse_button = MouseButton::None;
  504. return true;
  505. }
  506. if (event.type() != Event::MouseMove)
  507. return false;
  508. int diff_x = event.x() - m_resize_origin.x();
  509. int diff_y = event.y() - m_resize_origin.y();
  510. int change_w = 0;
  511. int change_h = 0;
  512. switch (m_resize_direction) {
  513. case ResizeDirection::DownRight:
  514. change_w = diff_x;
  515. change_h = diff_y;
  516. break;
  517. case ResizeDirection::Right:
  518. change_w = diff_x;
  519. break;
  520. case ResizeDirection::UpRight:
  521. change_w = diff_x;
  522. change_h = -diff_y;
  523. break;
  524. case ResizeDirection::Up:
  525. change_h = -diff_y;
  526. break;
  527. case ResizeDirection::UpLeft:
  528. change_w = -diff_x;
  529. change_h = -diff_y;
  530. break;
  531. case ResizeDirection::Left:
  532. change_w = -diff_x;
  533. break;
  534. case ResizeDirection::DownLeft:
  535. change_w = -diff_x;
  536. change_h = diff_y;
  537. break;
  538. case ResizeDirection::Down:
  539. change_h = diff_y;
  540. break;
  541. default:
  542. ASSERT_NOT_REACHED();
  543. }
  544. auto new_rect = m_resize_window_original_rect;
  545. // First, size the new rect.
  546. new_rect.set_width(new_rect.width() + change_w);
  547. new_rect.set_height(new_rect.height() + change_h);
  548. m_resize_window->apply_minimum_size(new_rect);
  549. if (!m_resize_window->size_increment().is_null()) {
  550. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  551. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  552. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  553. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  554. }
  555. if (m_resize_window->resize_aspect_ratio().has_value()) {
  556. auto& ratio = m_resize_window->resize_aspect_ratio().value();
  557. if (abs(change_w) > abs(change_h)) {
  558. new_rect.set_height(new_rect.width() * ratio.height() / ratio.width());
  559. } else {
  560. new_rect.set_width(new_rect.height() * ratio.width() / ratio.height());
  561. }
  562. }
  563. // Second, set its position so that the sides of the window
  564. // that end up moving are the same ones as the user is dragging,
  565. // no matter which part of the logic above caused us to decide
  566. // to resize by this much.
  567. switch (m_resize_direction) {
  568. case ResizeDirection::DownRight:
  569. case ResizeDirection::Right:
  570. case ResizeDirection::Down:
  571. break;
  572. case ResizeDirection::Left:
  573. case ResizeDirection::Up:
  574. case ResizeDirection::UpLeft:
  575. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  576. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  577. break;
  578. case ResizeDirection::UpRight:
  579. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  580. break;
  581. case ResizeDirection::DownLeft:
  582. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  583. break;
  584. default:
  585. ASSERT_NOT_REACHED();
  586. }
  587. if (new_rect.contains(event.position()))
  588. hovered_window = m_resize_window;
  589. if (m_resize_window->rect() == new_rect)
  590. return true;
  591. dbgln_if(RESIZE_DEBUG, "[WM] Resizing, original: {}, now: {}", m_resize_window_original_rect, new_rect);
  592. m_resize_window->set_rect(new_rect);
  593. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(new_rect));
  594. return true;
  595. }
  596. bool WindowManager::process_ongoing_drag(MouseEvent& event, Window*& hovered_window)
  597. {
  598. if (!m_dnd_client)
  599. return false;
  600. if (event.type() == Event::MouseMove) {
  601. // We didn't let go of the drag yet, see if we should send some drag move events..
  602. for_each_visible_window_from_front_to_back([&](Window& window) {
  603. if (!window.rect().contains(event.position()))
  604. return IterationDecision::Continue;
  605. hovered_window = &window;
  606. auto translated_event = event.translated(-window.position());
  607. translated_event.set_drag(true);
  608. translated_event.set_mime_data(*m_dnd_mime_data);
  609. deliver_mouse_event(window, translated_event);
  610. return IterationDecision::Break;
  611. });
  612. }
  613. if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Left))
  614. return true;
  615. hovered_window = nullptr;
  616. for_each_visible_window_from_front_to_back([&](auto& window) {
  617. if (window.frame().rect().contains(event.position())) {
  618. hovered_window = &window;
  619. return IterationDecision::Break;
  620. }
  621. return IterationDecision::Continue;
  622. });
  623. if (hovered_window) {
  624. m_dnd_client->post_message(Messages::WindowClient::DragAccepted());
  625. if (hovered_window->client()) {
  626. auto translated_event = event.translated(-hovered_window->position());
  627. hovered_window->client()->post_message(Messages::WindowClient::DragDropped(hovered_window->window_id(), translated_event.position(), m_dnd_text, m_dnd_mime_data->all_data()));
  628. }
  629. } else {
  630. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  631. }
  632. end_dnd_drag();
  633. return true;
  634. }
  635. void WindowManager::set_cursor_tracking_button(Button* button)
  636. {
  637. m_cursor_tracking_button = button;
  638. }
  639. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) const -> const ClickMetadata&
  640. {
  641. switch (button) {
  642. case MouseButton::Left:
  643. return m_left;
  644. case MouseButton::Right:
  645. return m_right;
  646. case MouseButton::Middle:
  647. return m_middle;
  648. case MouseButton::Back:
  649. return m_back;
  650. case MouseButton::Forward:
  651. return m_forward;
  652. default:
  653. ASSERT_NOT_REACHED();
  654. }
  655. }
  656. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
  657. {
  658. switch (button) {
  659. case MouseButton::Left:
  660. return m_left;
  661. case MouseButton::Right:
  662. return m_right;
  663. case MouseButton::Middle:
  664. return m_middle;
  665. case MouseButton::Back:
  666. return m_back;
  667. case MouseButton::Forward:
  668. return m_forward;
  669. default:
  670. ASSERT_NOT_REACHED();
  671. }
  672. }
  673. bool WindowManager::is_considered_doubleclick(const MouseEvent& event, const DoubleClickInfo::ClickMetadata& metadata) const
  674. {
  675. int elapsed_since_last_click = metadata.clock.elapsed();
  676. if (elapsed_since_last_click < m_double_click_speed) {
  677. auto diff = event.position() - metadata.last_position;
  678. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  679. if (distance_travelled_squared <= (m_max_distance_for_double_click * m_max_distance_for_double_click))
  680. return true;
  681. }
  682. return false;
  683. }
  684. void WindowManager::start_menu_doubleclick(Window& window, const MouseEvent& event)
  685. {
  686. // This is a special case. Basically, we're trying to determine whether
  687. // double clicking on the window menu icon happened. In this case, the
  688. // WindowFrame only receives a MouseDown event, and since the window
  689. // menu popus up, it does not see the MouseUp event. But, if they subsequently
  690. // click there again, the menu is closed and we receive a MouseUp event.
  691. // So, in order to be able to detect a double click when a menu is being
  692. // opened by the MouseDown event, we need to consider the MouseDown event
  693. // as a potential double-click trigger
  694. ASSERT(event.type() == Event::MouseDown);
  695. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  696. if (&window != m_double_click_info.m_clicked_window) {
  697. // we either haven't clicked anywhere, or we haven't clicked on this
  698. // window. set the current click window, and reset the timers.
  699. dbgln_if(DOUBLECLICK_DEBUG, "Initial mousedown on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  700. m_double_click_info.m_clicked_window = window;
  701. m_double_click_info.reset();
  702. }
  703. metadata.last_position = event.position();
  704. metadata.clock.start();
  705. }
  706. bool WindowManager::is_menu_doubleclick(Window& window, const MouseEvent& event) const
  707. {
  708. ASSERT(event.type() == Event::MouseUp);
  709. if (&window != m_double_click_info.m_clicked_window)
  710. return false;
  711. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  712. if (!metadata.clock.is_valid())
  713. return false;
  714. return is_considered_doubleclick(event, metadata);
  715. }
  716. void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
  717. {
  718. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  719. ASSERT(event.type() == Event::MouseUp);
  720. if (&window != m_double_click_info.m_clicked_window) {
  721. // we either haven't clicked anywhere, or we haven't clicked on this
  722. // window. set the current click window, and reset the timers.
  723. dbgln_if(DOUBLECLICK_DEBUG, "Initial mouseup on Window({}) for menus (previous was {})", &window, m_double_click_info.m_clicked_window);
  724. m_double_click_info.m_clicked_window = window;
  725. m_double_click_info.reset();
  726. }
  727. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  728. if (!metadata.clock.is_valid() || !is_considered_doubleclick(event, metadata)) {
  729. // either the clock is invalid because we haven't clicked on this
  730. // button on this window yet, so there's nothing to do, or this
  731. // isn't considered to be a double click. either way, restart the
  732. // clock
  733. metadata.clock.start();
  734. } else {
  735. dbgln_if(DOUBLECLICK_DEBUG, "Transforming MouseUp to MouseDoubleClick ({} < {})!", metadata.clock.elapsed(), m_double_click_speed);
  736. event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  737. // invalidate this now we've delivered a doubleclick, otherwise
  738. // tripleclick will deliver two doubleclick events (incorrectly).
  739. metadata.clock = {};
  740. }
  741. metadata.last_position = event.position();
  742. }
  743. void WindowManager::deliver_mouse_event(Window& window, MouseEvent& event)
  744. {
  745. window.dispatch_event(event);
  746. if (event.type() == Event::MouseUp) {
  747. process_event_for_doubleclick(window, event);
  748. if (event.type() == Event::MouseDoubleClick)
  749. window.dispatch_event(event);
  750. }
  751. }
  752. void WindowManager::process_mouse_event(MouseEvent& event, Window*& hovered_window)
  753. {
  754. HashTable<Window*> windows_who_received_mouse_event_due_to_cursor_tracking;
  755. // We need to process ongoing drag events first. Otherwise, global tracking
  756. // and dnd collides, leading to duplicate GUI::DragOperation instances
  757. if (process_ongoing_drag(event, hovered_window))
  758. return;
  759. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  760. if (!window->global_cursor_tracking() || !window->is_visible() || window->is_minimized() || window->blocking_modal_window())
  761. continue;
  762. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  763. auto translated_event = event.translated(-window->position());
  764. deliver_mouse_event(*window, translated_event);
  765. }
  766. hovered_window = nullptr;
  767. if (process_ongoing_window_move(event, hovered_window))
  768. return;
  769. if (process_ongoing_window_resize(event, hovered_window))
  770. return;
  771. if (m_cursor_tracking_button)
  772. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  773. // This is quite hackish, but it's how the Button hover effect is implemented.
  774. if (m_hovered_button && event.type() == Event::MouseMove)
  775. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  776. // FIXME: Now that the menubar has a dedicated window, is this special-casing really necessary?
  777. if (MenuManager::the().has_open_menu() || menubar_rect().contains(event.position())) {
  778. for_each_visible_window_of_type_from_front_to_back(WindowType::MenuApplet, [&](auto& window) {
  779. if (!window.rect_in_menubar().contains(event.position()))
  780. return IterationDecision::Continue;
  781. hovered_window = &window;
  782. return IterationDecision::Break;
  783. });
  784. clear_resize_candidate();
  785. MenuManager::the().dispatch_event(event);
  786. return;
  787. }
  788. Window* event_window_with_frame = nullptr;
  789. if (m_active_input_tracking_window) {
  790. // At this point, we have delivered the start of an input sequence to a
  791. // client application. We must keep delivering to that client
  792. // application until the input sequence is done.
  793. //
  794. // This prevents e.g. moving on one window out of the bounds starting
  795. // a move in that other unrelated window, and other silly shenanigans.
  796. if (!windows_who_received_mouse_event_due_to_cursor_tracking.contains(m_active_input_tracking_window)) {
  797. auto translated_event = event.translated(-m_active_input_tracking_window->position());
  798. deliver_mouse_event(*m_active_input_tracking_window, translated_event);
  799. windows_who_received_mouse_event_due_to_cursor_tracking.set(m_active_input_tracking_window.ptr());
  800. }
  801. if (event.type() == Event::MouseUp && event.buttons() == 0) {
  802. m_active_input_tracking_window = nullptr;
  803. }
  804. for_each_visible_window_from_front_to_back([&](auto& window) {
  805. if (window.frame().rect().contains(event.position())) {
  806. hovered_window = &window;
  807. return IterationDecision::Break;
  808. }
  809. return IterationDecision::Continue;
  810. });
  811. } else {
  812. auto process_mouse_event_for_window = [&](Window& window) {
  813. if (&window != m_resize_candidate.ptr())
  814. clear_resize_candidate();
  815. // First check if we should initiate a move or resize (Logo+LMB or Logo+RMB).
  816. // In those cases, the event is swallowed by the window manager.
  817. if (window.is_movable()) {
  818. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
  819. hovered_window = &window;
  820. start_window_move(window, event);
  821. return;
  822. }
  823. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.blocking_modal_window()) {
  824. hovered_window = &window;
  825. start_window_resize(window, event);
  826. return;
  827. }
  828. }
  829. if (m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseWheel) {
  830. float opacity_change = -event.wheel_delta() * 0.05f;
  831. float new_opacity = window.opacity() + opacity_change;
  832. if (new_opacity < 0.05f)
  833. new_opacity = 0.05f;
  834. if (new_opacity > 1.0f)
  835. new_opacity = 1.0f;
  836. window.set_opacity(new_opacity);
  837. return;
  838. }
  839. ASSERT(window.frame().rect().contains(event.position()));
  840. if (event.type() == Event::MouseDown) {
  841. // We're clicking on something that's blocked by a modal window.
  842. // Flash the modal window to let the user know about it.
  843. if (auto* blocking_modal_window = window.blocking_modal_window())
  844. blocking_modal_window->frame().start_flash_animation();
  845. if (window.type() == WindowType::Normal)
  846. move_to_front_and_make_active(window);
  847. else if (window.type() == WindowType::Desktop)
  848. set_active_window(&window);
  849. }
  850. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  851. if (window.rect().contains(event.position())) {
  852. hovered_window = &window;
  853. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window) && !window.blocking_modal_window()) {
  854. auto translated_event = event.translated(-window.position());
  855. deliver_mouse_event(window, translated_event);
  856. if (event.type() == Event::MouseDown) {
  857. m_active_input_tracking_window = window;
  858. }
  859. }
  860. return;
  861. }
  862. // We are hitting the frame, pass the event along to WindowFrame.
  863. window.frame().on_mouse_event(event.translated(-window.frame().rect().location()));
  864. event_window_with_frame = &window;
  865. };
  866. if (auto* fullscreen_window = active_fullscreen_window()) {
  867. process_mouse_event_for_window(*fullscreen_window);
  868. } else {
  869. for_each_visible_window_from_front_to_back([&](Window& window) {
  870. auto window_frame_rect = window.frame().rect();
  871. if (!window_frame_rect.contains(event.position()))
  872. return IterationDecision::Continue;
  873. process_mouse_event_for_window(window);
  874. return IterationDecision::Break;
  875. });
  876. }
  877. // Clicked outside of any window
  878. if (!hovered_window && !event_window_with_frame && event.type() == Event::MouseDown)
  879. set_active_window(nullptr);
  880. }
  881. if (event_window_with_frame != m_resize_candidate.ptr())
  882. clear_resize_candidate();
  883. }
  884. void WindowManager::clear_resize_candidate()
  885. {
  886. if (m_resize_candidate)
  887. Compositor::the().invalidate_cursor();
  888. m_resize_candidate = nullptr;
  889. }
  890. Gfx::IntRect WindowManager::menubar_rect() const
  891. {
  892. if (active_fullscreen_window())
  893. return {};
  894. return MenuManager::the().menubar_rect();
  895. }
  896. Gfx::IntRect WindowManager::desktop_rect() const
  897. {
  898. if (active_fullscreen_window())
  899. return Screen::the().rect();
  900. return {
  901. 0,
  902. menubar_rect().bottom() + 1,
  903. Screen::the().width(),
  904. Screen::the().height() - menubar_rect().height() - 28
  905. };
  906. }
  907. Gfx::IntRect WindowManager::arena_rect_for_type(WindowType type) const
  908. {
  909. switch (type) {
  910. case WindowType::Desktop:
  911. case WindowType::Normal:
  912. return desktop_rect();
  913. case WindowType::Menu:
  914. case WindowType::WindowSwitcher:
  915. case WindowType::Taskbar:
  916. case WindowType::Tooltip:
  917. case WindowType::Menubar:
  918. case WindowType::MenuApplet:
  919. case WindowType::Notification:
  920. return Screen::the().rect();
  921. default:
  922. ASSERT_NOT_REACHED();
  923. }
  924. }
  925. void WindowManager::event(Core::Event& event)
  926. {
  927. if (static_cast<Event&>(event).is_mouse_event()) {
  928. Window* hovered_window = nullptr;
  929. process_mouse_event(static_cast<MouseEvent&>(event), hovered_window);
  930. set_hovered_window(hovered_window);
  931. return;
  932. }
  933. if (static_cast<Event&>(event).is_key_event()) {
  934. auto& key_event = static_cast<const KeyEvent&>(event);
  935. m_keyboard_modifiers = key_event.modifiers();
  936. // Escape key cancels an ongoing drag.
  937. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
  938. // Notify the drag-n-drop client that the drag was cancelled.
  939. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  940. // Also notify the currently hovered window (if any) that the ongoing drag was cancelled.
  941. if (m_hovered_window && m_hovered_window->client() && m_hovered_window->client() != m_dnd_client)
  942. m_hovered_window->client()->post_message(Messages::WindowClient::DragCancelled());
  943. end_dnd_drag();
  944. return;
  945. }
  946. if (key_event.type() == Event::KeyDown && (key_event.modifiers() == (Mod_Ctrl | Mod_Logo | Mod_Shift) && key_event.key() == Key_I)) {
  947. reload_icon_bitmaps_after_scale_change(!m_allow_hidpi_icons);
  948. Compositor::the().invalidate_screen();
  949. return;
  950. }
  951. if (MenuManager::the().current_menu()) {
  952. MenuManager::the().dispatch_event(event);
  953. return;
  954. }
  955. if (key_event.type() == Event::KeyDown && ((key_event.modifiers() == Mod_Logo && key_event.key() == Key_Tab) || (key_event.modifiers() == (Mod_Logo | Mod_Shift) && key_event.key() == Key_Tab)))
  956. m_switcher.show();
  957. if (m_switcher.is_visible()) {
  958. m_switcher.on_key_event(key_event);
  959. return;
  960. }
  961. if (m_active_input_window) {
  962. if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Logo) {
  963. if (key_event.key() == Key_Down) {
  964. if (m_active_input_window->is_resizable() && m_active_input_window->is_maximized()) {
  965. maximize_windows(*m_active_input_window, false);
  966. return;
  967. }
  968. if (m_active_input_window->is_minimizable())
  969. minimize_windows(*m_active_input_window, true);
  970. return;
  971. }
  972. if (m_active_input_window->is_resizable()) {
  973. if (key_event.key() == Key_Up) {
  974. maximize_windows(*m_active_input_window, !m_active_input_window->is_maximized());
  975. return;
  976. }
  977. if (key_event.key() == Key_Left) {
  978. if (m_active_input_window->tiled() == WindowTileType::Left)
  979. return;
  980. if (m_active_input_window->tiled() != WindowTileType::None) {
  981. m_active_input_window->set_untiled();
  982. return;
  983. }
  984. if (m_active_input_window->is_maximized())
  985. maximize_windows(*m_active_input_window, false);
  986. m_active_input_window->set_tiled(WindowTileType::Left);
  987. return;
  988. }
  989. if (key_event.key() == Key_Right) {
  990. if (m_active_input_window->tiled() == WindowTileType::Right)
  991. return;
  992. if (m_active_input_window->tiled() != WindowTileType::None) {
  993. m_active_input_window->set_untiled();
  994. return;
  995. }
  996. if (m_active_input_window->is_maximized())
  997. maximize_windows(*m_active_input_window, false);
  998. m_active_input_window->set_tiled(WindowTileType::Right);
  999. return;
  1000. }
  1001. }
  1002. }
  1003. m_active_input_window->dispatch_event(event);
  1004. return;
  1005. }
  1006. }
  1007. Core::Object::event(event);
  1008. }
  1009. void WindowManager::set_highlight_window(Window* window)
  1010. {
  1011. if (window == m_highlight_window)
  1012. return;
  1013. if (auto* previous_highlight_window = m_highlight_window.ptr())
  1014. previous_highlight_window->invalidate();
  1015. m_highlight_window = window;
  1016. if (m_highlight_window)
  1017. m_highlight_window->invalidate();
  1018. }
  1019. bool WindowManager::is_active_window_or_accessory(Window& window) const
  1020. {
  1021. if (m_active_window == &window)
  1022. return true;
  1023. if (!window.is_accessory())
  1024. return false;
  1025. return m_active_window == window.parent_window();
  1026. }
  1027. static bool window_type_can_become_active(WindowType type)
  1028. {
  1029. return type == WindowType::Normal || type == WindowType::Desktop;
  1030. }
  1031. void WindowManager::restore_active_input_window(Window* window)
  1032. {
  1033. // If the previous active input window is gone, fall back to the
  1034. // current active window
  1035. if (!window)
  1036. window = active_window();
  1037. // If the current active window is also gone, pick some other window
  1038. if (!window && pick_new_active_window(nullptr))
  1039. return;
  1040. set_active_input_window(window);
  1041. }
  1042. Window* WindowManager::set_active_input_window(Window* window)
  1043. {
  1044. if (window == m_active_input_window)
  1045. return window;
  1046. Window* previous_input_window = m_active_input_window;
  1047. if (previous_input_window)
  1048. Core::EventLoop::current().post_event(*previous_input_window, make<Event>(Event::WindowInputLeft));
  1049. if (window) {
  1050. m_active_input_window = *window;
  1051. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  1052. } else {
  1053. m_active_input_window = nullptr;
  1054. }
  1055. return previous_input_window;
  1056. }
  1057. void WindowManager::set_active_window(Window* window, bool make_input)
  1058. {
  1059. if (window) {
  1060. if (auto* modal_window = window->blocking_modal_window()) {
  1061. ASSERT(modal_window->is_modal());
  1062. ASSERT(modal_window != window);
  1063. window = modal_window;
  1064. make_input = true;
  1065. }
  1066. if (!window_type_can_become_active(window->type()))
  1067. return;
  1068. }
  1069. auto* new_active_input_window = window;
  1070. if (window && window->is_accessory()) {
  1071. // The parent of an accessory window is always the active
  1072. // window, but input is routed to the accessory window
  1073. window = window->parent_window();
  1074. }
  1075. if (make_input)
  1076. set_active_input_window(new_active_input_window);
  1077. if (window == m_active_window)
  1078. return;
  1079. auto* previously_active_window = m_active_window.ptr();
  1080. if (previously_active_window) {
  1081. Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
  1082. previously_active_window->invalidate();
  1083. m_active_window = nullptr;
  1084. m_active_input_tracking_window = nullptr;
  1085. tell_wm_listeners_window_state_changed(*previously_active_window);
  1086. }
  1087. if (window) {
  1088. m_active_window = *window;
  1089. Core::EventLoop::current().post_event(*m_active_window, make<Event>(Event::WindowActivated));
  1090. m_active_window->invalidate();
  1091. if (auto* client = window->client()) {
  1092. MenuManager::the().set_current_menubar(client->app_menubar());
  1093. } else {
  1094. MenuManager::the().set_current_menubar(nullptr);
  1095. }
  1096. tell_wm_listeners_window_state_changed(*m_active_window);
  1097. } else {
  1098. MenuManager::the().set_current_menubar(nullptr);
  1099. }
  1100. }
  1101. void WindowManager::set_hovered_window(Window* window)
  1102. {
  1103. if (m_hovered_window == window)
  1104. return;
  1105. if (m_hovered_window)
  1106. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
  1107. m_hovered_window = window;
  1108. if (m_hovered_window)
  1109. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
  1110. }
  1111. const ClientConnection* WindowManager::active_client() const
  1112. {
  1113. if (m_active_window)
  1114. return m_active_window->client();
  1115. return nullptr;
  1116. }
  1117. void WindowManager::notify_client_changed_app_menubar(ClientConnection& client)
  1118. {
  1119. if (active_client() == &client)
  1120. MenuManager::the().set_current_menubar(client.app_menubar());
  1121. }
  1122. const Cursor& WindowManager::active_cursor() const
  1123. {
  1124. if (m_dnd_client)
  1125. return *m_drag_cursor;
  1126. if (m_move_window)
  1127. return *m_move_cursor;
  1128. if (m_resize_window || m_resize_candidate) {
  1129. switch (m_resize_direction) {
  1130. case ResizeDirection::Up:
  1131. case ResizeDirection::Down:
  1132. return *m_resize_vertically_cursor;
  1133. case ResizeDirection::Left:
  1134. case ResizeDirection::Right:
  1135. return *m_resize_horizontally_cursor;
  1136. case ResizeDirection::UpLeft:
  1137. case ResizeDirection::DownRight:
  1138. return *m_resize_diagonally_tlbr_cursor;
  1139. case ResizeDirection::UpRight:
  1140. case ResizeDirection::DownLeft:
  1141. return *m_resize_diagonally_bltr_cursor;
  1142. case ResizeDirection::None:
  1143. break;
  1144. }
  1145. }
  1146. if (m_hovered_window) {
  1147. if (auto* modal_window = const_cast<Window&>(*m_hovered_window).blocking_modal_window()) {
  1148. if (modal_window->cursor())
  1149. return *modal_window->cursor();
  1150. } else if (m_hovered_window->cursor()) {
  1151. return *m_hovered_window->cursor();
  1152. }
  1153. }
  1154. return *m_arrow_cursor;
  1155. }
  1156. void WindowManager::set_hovered_button(Button* button)
  1157. {
  1158. m_hovered_button = button;
  1159. }
  1160. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1161. {
  1162. m_resize_candidate = window;
  1163. m_resize_direction = direction;
  1164. }
  1165. ResizeDirection WindowManager::resize_direction_of_window(const Window& window)
  1166. {
  1167. if (&window != m_resize_window)
  1168. return ResizeDirection::None;
  1169. return m_resize_direction;
  1170. }
  1171. Gfx::IntRect WindowManager::maximized_window_rect(const Window& window) const
  1172. {
  1173. Gfx::IntRect rect = Screen::the().rect();
  1174. // Subtract window title bar (leaving the border)
  1175. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  1176. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  1177. // Subtract menu bar
  1178. rect.set_y(rect.y() + menubar_rect().height());
  1179. rect.set_height(rect.height() - menubar_rect().height());
  1180. // Subtract taskbar window height if present
  1181. const_cast<WindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
  1182. rect.set_height(rect.height() - taskbar_window.height());
  1183. return IterationDecision::Break;
  1184. });
  1185. constexpr int tasteful_space_above_maximized_window = 2;
  1186. rect.set_y(rect.y() + tasteful_space_above_maximized_window);
  1187. rect.set_height(rect.height() - tasteful_space_above_maximized_window);
  1188. return rect;
  1189. }
  1190. void WindowManager::start_dnd_drag(ClientConnection& client, const String& text, const Gfx::Bitmap* bitmap, const Core::MimeData& mime_data)
  1191. {
  1192. ASSERT(!m_dnd_client);
  1193. m_dnd_client = client;
  1194. m_dnd_text = text;
  1195. m_dnd_bitmap = bitmap;
  1196. m_dnd_mime_data = mime_data;
  1197. Compositor::the().invalidate_cursor();
  1198. m_active_input_tracking_window = nullptr;
  1199. }
  1200. void WindowManager::end_dnd_drag()
  1201. {
  1202. ASSERT(m_dnd_client);
  1203. Compositor::the().invalidate_cursor();
  1204. m_dnd_client = nullptr;
  1205. m_dnd_text = {};
  1206. m_dnd_bitmap = nullptr;
  1207. }
  1208. Gfx::IntRect WindowManager::dnd_rect() const
  1209. {
  1210. int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
  1211. int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
  1212. int width = font().width(m_dnd_text) + bitmap_width;
  1213. int height = max((int)font().glyph_height(), bitmap_height);
  1214. auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
  1215. return Gfx::IntRect(location, { width, height }).inflated(16, 8);
  1216. }
  1217. bool WindowManager::update_theme(String theme_path, String theme_name)
  1218. {
  1219. auto new_theme = Gfx::load_system_theme(theme_path);
  1220. if (!new_theme.is_valid())
  1221. return false;
  1222. Gfx::set_system_theme(new_theme);
  1223. m_palette = Gfx::PaletteImpl::create_with_anonymous_buffer(new_theme);
  1224. Compositor::the().set_background_color(palette().desktop_background().to_string());
  1225. HashTable<ClientConnection*> notified_clients;
  1226. for_each_window([&](Window& window) {
  1227. if (window.client()) {
  1228. if (!notified_clients.contains(window.client())) {
  1229. window.client()->post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer()));
  1230. notified_clients.set(window.client());
  1231. }
  1232. }
  1233. window.frame().layout_buttons();
  1234. window.frame().set_button_icons();
  1235. return IterationDecision::Continue;
  1236. });
  1237. MenuManager::the().did_change_theme();
  1238. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  1239. wm_config->write_entry("Theme", "Name", theme_name);
  1240. wm_config->sync();
  1241. Compositor::the().invalidate_screen();
  1242. return true;
  1243. }
  1244. void WindowManager::did_popup_a_menu(Badge<Menu>)
  1245. {
  1246. // Clear any ongoing input gesture
  1247. if (!m_active_input_tracking_window)
  1248. return;
  1249. m_active_input_tracking_window->set_automatic_cursor_tracking_enabled(false);
  1250. m_active_input_tracking_window = nullptr;
  1251. }
  1252. void WindowManager::minimize_windows(Window& window, bool minimized)
  1253. {
  1254. for_each_window_in_modal_stack(window, [&](auto& w, bool) {
  1255. w.set_minimized(minimized);
  1256. return IterationDecision::Continue;
  1257. });
  1258. }
  1259. void WindowManager::maximize_windows(Window& window, bool maximized)
  1260. {
  1261. for_each_window_in_modal_stack(window, [&](auto& w, bool stack_top) {
  1262. if (stack_top)
  1263. w.set_maximized(maximized);
  1264. if (w.is_minimized())
  1265. w.set_minimized(false);
  1266. return IterationDecision::Continue;
  1267. });
  1268. }
  1269. Gfx::IntPoint WindowManager::get_recommended_window_position(const Gfx::IntPoint& desired)
  1270. {
  1271. // FIXME: Find a better source for the width and height to shift by.
  1272. Gfx::IntPoint shift(8, Gfx::WindowTheme::current().title_bar_height(palette()) + 10);
  1273. // FIXME: Find a better source for this.
  1274. int taskbar_height = 28;
  1275. int menubar_height = MenuManager::the().menubar_rect().height();
  1276. const Window* overlap_window = nullptr;
  1277. for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& window) {
  1278. if (window.default_positioned() && (!overlap_window || overlap_window->window_id() < window.window_id())) {
  1279. overlap_window = &window;
  1280. }
  1281. return IterationDecision::Continue;
  1282. });
  1283. Gfx::IntPoint point;
  1284. if (overlap_window) {
  1285. point = overlap_window->position() + shift;
  1286. point = { point.x() % Screen::the().width(),
  1287. (point.y() >= (Screen::the().height() - taskbar_height))
  1288. ? menubar_height + Gfx::WindowTheme::current().title_bar_height(palette())
  1289. : point.y() };
  1290. } else {
  1291. point = desired;
  1292. }
  1293. return point;
  1294. }
  1295. int WindowManager::compositor_icon_scale() const
  1296. {
  1297. if (!m_allow_hidpi_icons)
  1298. return 1;
  1299. return scale_factor();
  1300. }
  1301. void WindowManager::reload_icon_bitmaps_after_scale_change(bool allow_hidpi_icons)
  1302. {
  1303. m_allow_hidpi_icons = allow_hidpi_icons;
  1304. reload_config();
  1305. for_each_window([&](Window& window) {
  1306. auto& window_frame = window.frame();
  1307. window_frame.set_button_icons();
  1308. return IterationDecision::Continue;
  1309. });
  1310. }
  1311. }