WindowManager.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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/SharedBuffer.h>
  36. #include <AK/StdLibExtras.h>
  37. #include <AK/Vector.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. //#define WINDOWMANAGER_DEBUG
  54. //#define RESIZE_DEBUG
  55. //#define MOVE_DEBUG
  56. //#define DOUBLECLICK_DEBUG
  57. namespace WindowServer {
  58. static WindowManager* s_the;
  59. WindowManager& WindowManager::the()
  60. {
  61. ASSERT(s_the);
  62. return *s_the;
  63. }
  64. WindowManager::WindowManager(const Gfx::PaletteImpl& palette)
  65. : m_palette(palette)
  66. {
  67. s_the = this;
  68. reload_config(false);
  69. invalidate();
  70. Compositor::the().compose();
  71. }
  72. WindowManager::~WindowManager()
  73. {
  74. }
  75. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name, const Gfx::IntPoint& hotspot)
  76. {
  77. auto path = m_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  78. auto gb = Gfx::Bitmap::load_from_file(path);
  79. if (gb)
  80. return Cursor::create(*gb, hotspot);
  81. return Cursor::create(*Gfx::Bitmap::load_from_file("/res/cursors/arrow.png"));
  82. }
  83. NonnullRefPtr<Cursor> WindowManager::get_cursor(const String& name)
  84. {
  85. auto path = m_config->read_entry("Cursor", name, "/res/cursors/arrow.png");
  86. auto gb = Gfx::Bitmap::load_from_file(path);
  87. if (gb)
  88. return Cursor::create(*gb);
  89. return Cursor::create(*Gfx::Bitmap::load_from_file("/res/cursors/arrow.png"));
  90. }
  91. void WindowManager::reload_config(bool set_screen)
  92. {
  93. m_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  94. m_double_click_speed = m_config->read_num_entry("Input", "DoubleClickSpeed", 250);
  95. if (set_screen) {
  96. set_resolution(m_config->read_num_entry("Screen", "Width", 1920), m_config->read_num_entry("Screen", "Height", 1080));
  97. }
  98. m_arrow_cursor = get_cursor("Arrow", { 2, 2 });
  99. m_hand_cursor = get_cursor("Hand", { 8, 4 });
  100. m_help_cursor = get_cursor("Help", { 1, 1 });
  101. m_resize_horizontally_cursor = get_cursor("ResizeH");
  102. m_resize_vertically_cursor = get_cursor("ResizeV");
  103. m_resize_diagonally_tlbr_cursor = get_cursor("ResizeDTLBR");
  104. m_resize_diagonally_bltr_cursor = get_cursor("ResizeDBLTR");
  105. m_resize_column_cursor = get_cursor("ResizeColumn");
  106. m_resize_row_cursor = get_cursor("ResizeRow");
  107. m_i_beam_cursor = get_cursor("IBeam");
  108. m_disallowed_cursor = get_cursor("Disallowed");
  109. m_move_cursor = get_cursor("Move");
  110. m_drag_cursor = get_cursor("Drag");
  111. m_wait_cursor = get_cursor("Wait");
  112. }
  113. const Gfx::Font& WindowManager::font() const
  114. {
  115. return Gfx::Font::default_font();
  116. }
  117. const Gfx::Font& WindowManager::window_title_font() const
  118. {
  119. return Gfx::Font::default_bold_font();
  120. }
  121. bool WindowManager::set_resolution(int width, int height)
  122. {
  123. bool success = Compositor::the().set_resolution(width, height);
  124. MenuManager::the().set_needs_window_resize();
  125. ClientConnection::for_each_client([&](ClientConnection& client) {
  126. client.notify_about_new_screen_rect(Screen::the().rect());
  127. });
  128. if (success) {
  129. for_each_window([](Window& window) {
  130. window.recalculate_rect();
  131. return IterationDecision::Continue;
  132. });
  133. }
  134. if (m_config) {
  135. if (success) {
  136. dbg() << "Saving resolution: " << Gfx::IntSize(width, height) << " to config file at " << m_config->file_name();
  137. m_config->write_num_entry("Screen", "Width", width);
  138. m_config->write_num_entry("Screen", "Height", height);
  139. m_config->sync();
  140. } else {
  141. dbg() << "Saving fallback resolution: " << resolution() << " to config file at " << m_config->file_name();
  142. m_config->write_num_entry("Screen", "Width", resolution().width());
  143. m_config->write_num_entry("Screen", "Height", resolution().height());
  144. m_config->sync();
  145. }
  146. }
  147. return success;
  148. }
  149. Gfx::IntSize WindowManager::resolution() const
  150. {
  151. return Screen::the().size();
  152. }
  153. void WindowManager::add_window(Window& window)
  154. {
  155. bool is_first_window = m_windows_in_order.is_empty();
  156. m_windows_in_order.append(&window);
  157. if (window.is_fullscreen()) {
  158. Core::EventLoop::current().post_event(window, make<ResizeEvent>(window.rect(), Screen::the().rect()));
  159. window.set_rect(Screen::the().rect());
  160. }
  161. if (window.type() != WindowType::Desktop || is_first_window)
  162. set_active_window(&window);
  163. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  164. m_switcher.refresh();
  165. Compositor::the().recompute_occlusions();
  166. if (window.listens_to_wm_events()) {
  167. for_each_window([&](Window& other_window) {
  168. if (&window != &other_window) {
  169. tell_wm_listener_about_window(window, other_window);
  170. tell_wm_listener_about_window_icon(window, other_window);
  171. }
  172. return IterationDecision::Continue;
  173. });
  174. }
  175. tell_wm_listeners_window_state_changed(window);
  176. }
  177. void WindowManager::move_to_front_and_make_active(Window& window)
  178. {
  179. auto move_window_to_front = [&](Window& wnd, bool make_active, bool make_input) {
  180. if (wnd.is_accessory()) {
  181. auto* parent = wnd.parent_window();
  182. do_move_to_front(*parent, true, false);
  183. make_active = false;
  184. for (auto& accessory_window : parent->accessory_windows()) {
  185. if (accessory_window && accessory_window.ptr() != &wnd)
  186. do_move_to_front(*accessory_window, false, false);
  187. }
  188. }
  189. do_move_to_front(wnd, make_active, make_input);
  190. };
  191. // If a window that is currently blocked by a modal child is being
  192. // brought to the front, bring the entire stack of modal windows
  193. // to the front and activate the modal window. Also set the
  194. // active input window to that same window (which would pull
  195. // active input from any accessory window)
  196. for_each_window_in_modal_stack(window, [&](auto& w, bool is_stack_top) {
  197. move_window_to_front(w, is_stack_top, is_stack_top);
  198. });
  199. }
  200. void WindowManager::do_move_to_front(Window& window, bool make_active, bool make_input)
  201. {
  202. if (m_windows_in_order.tail() != &window)
  203. window.invalidate();
  204. m_windows_in_order.remove(&window);
  205. m_windows_in_order.append(&window);
  206. Compositor::the().recompute_occlusions();
  207. if (make_active)
  208. set_active_window(&window, make_input);
  209. if (m_switcher.is_visible()) {
  210. m_switcher.refresh();
  211. if (!window.is_accessory()) {
  212. m_switcher.select_window(window);
  213. set_highlight_window(&window);
  214. }
  215. }
  216. for (auto& child_window : window.child_windows()) {
  217. if (child_window)
  218. do_move_to_front(*child_window, make_active, make_input);
  219. }
  220. }
  221. void WindowManager::remove_window(Window& window)
  222. {
  223. window.invalidate();
  224. m_windows_in_order.remove(&window);
  225. auto* active = active_window();
  226. auto* active_input = active_input_window();
  227. if (active == &window || active_input == &window || (active && window.is_descendant_of(*active)) || (active_input && active_input != active && window.is_descendant_of(*active_input)))
  228. pick_new_active_window(&window);
  229. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  230. m_switcher.refresh();
  231. Compositor::the().recompute_occlusions();
  232. for_each_window_listening_to_wm_events([&window](Window& listener) {
  233. if (!(listener.wm_event_mask() & WMEventMask::WindowRemovals))
  234. return IterationDecision::Continue;
  235. if (!window.is_internal() && !window.is_modal())
  236. listener.client()->post_message(Messages::WindowClient::WM_WindowRemoved(listener.window_id(), window.client_id(), window.window_id()));
  237. return IterationDecision::Continue;
  238. });
  239. }
  240. void WindowManager::tell_wm_listener_about_window(Window& listener, Window& window)
  241. {
  242. if (!(listener.wm_event_mask() & WMEventMask::WindowStateChanges))
  243. return;
  244. if (window.is_internal())
  245. return;
  246. auto* parent = window.parent_window();
  247. 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()));
  248. }
  249. void WindowManager::tell_wm_listener_about_window_rect(Window& listener, Window& window)
  250. {
  251. if (!(listener.wm_event_mask() & WMEventMask::WindowRectChanges))
  252. return;
  253. if (window.is_internal())
  254. return;
  255. listener.client()->post_message(Messages::WindowClient::WM_WindowRectChanged(listener.window_id(), window.client_id(), window.window_id(), window.rect()));
  256. }
  257. void WindowManager::tell_wm_listener_about_window_icon(Window& listener, Window& window)
  258. {
  259. if (!(listener.wm_event_mask() & WMEventMask::WindowIconChanges))
  260. return;
  261. if (window.is_internal())
  262. return;
  263. if (window.icon().shbuf_id() == -1)
  264. return;
  265. #ifdef WINDOWMANAGER_DEBUG
  266. dbg() << "WindowServer: Sharing icon buffer " << window.icon().shbuf_id() << " with PID " << listener.client()->client_pid();
  267. #endif
  268. if (shbuf_allow_pid(window.icon().shbuf_id(), listener.client()->client_pid()) < 0) {
  269. ASSERT_NOT_REACHED();
  270. }
  271. listener.client()->post_message(Messages::WindowClient::WM_WindowIconBitmapChanged(listener.window_id(), window.client_id(), window.window_id(), window.icon().shbuf_id(), window.icon().size()));
  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. #ifdef WINDOWMANAGER_DEBUG
  299. dbg() << "[WM] Window{" << &window << "} title set to \"" << window.title() << '"';
  300. #endif
  301. invalidate(window.frame().rect());
  302. if (m_switcher.is_visible())
  303. m_switcher.refresh();
  304. tell_wm_listeners_window_state_changed(window);
  305. }
  306. void WindowManager::notify_modal_unparented(Window& window)
  307. {
  308. if (window.type() != WindowType::Normal)
  309. return;
  310. #ifdef WINDOWMANAGER_DEBUG
  311. dbg() << "[WM] Modal Window{" << &window << "} was unparented";
  312. #endif
  313. if (m_switcher.is_visible())
  314. m_switcher.refresh();
  315. tell_wm_listeners_window_state_changed(window);
  316. }
  317. void WindowManager::notify_rect_changed(Window& window, const Gfx::IntRect& old_rect, const Gfx::IntRect& new_rect)
  318. {
  319. UNUSED_PARAM(old_rect);
  320. UNUSED_PARAM(new_rect);
  321. #ifdef RESIZE_DEBUG
  322. dbg() << "[WM] Window " << &window << " rect changed " << old_rect << " -> " << new_rect;
  323. #endif
  324. if (m_switcher.is_visible() && window.type() != WindowType::WindowSwitcher)
  325. m_switcher.refresh();
  326. Compositor::the().recompute_occlusions();
  327. tell_wm_listeners_window_rect_changed(window);
  328. if (window.type() == WindowType::MenuApplet)
  329. AppletManager::the().calculate_applet_rects(MenuManager::the().window());
  330. MenuManager::the().refresh();
  331. }
  332. void WindowManager::notify_opacity_changed(Window&)
  333. {
  334. Compositor::the().recompute_occlusions();
  335. }
  336. void WindowManager::notify_minimization_state_changed(Window& window)
  337. {
  338. tell_wm_listeners_window_state_changed(window);
  339. if (window.client())
  340. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  341. if (window.is_active() && window.is_minimized())
  342. pick_new_active_window(&window);
  343. }
  344. void WindowManager::notify_occlusion_state_changed(Window& window)
  345. {
  346. if (window.client())
  347. window.client()->post_message(Messages::WindowClient::WindowStateChanged(window.window_id(), window.is_minimized(), window.is_occluded()));
  348. }
  349. void WindowManager::notify_progress_changed(Window& window)
  350. {
  351. tell_wm_listeners_window_state_changed(window);
  352. }
  353. bool WindowManager::pick_new_active_window(Window* previous_active)
  354. {
  355. bool new_window_picked = false;
  356. Window* first_candidate = nullptr;
  357. for_each_visible_window_of_type_from_front_to_back(WindowType::Normal, [&](Window& candidate) {
  358. if (candidate.is_destroyed())
  359. return IterationDecision::Continue;
  360. if (previous_active != first_candidate)
  361. first_candidate = &candidate;
  362. if ((!previous_active && !candidate.is_accessory()) || (previous_active && !candidate.is_accessory_of(*previous_active))) {
  363. set_active_window(&candidate);
  364. new_window_picked = true;
  365. return IterationDecision::Break;
  366. }
  367. return IterationDecision::Continue;
  368. });
  369. if (!new_window_picked) {
  370. set_active_window(first_candidate);
  371. new_window_picked = first_candidate != nullptr;
  372. }
  373. return new_window_picked;
  374. }
  375. void WindowManager::start_window_move(Window& window, const MouseEvent& event)
  376. {
  377. #ifdef MOVE_DEBUG
  378. dbg() << "[WM] Begin moving Window{" << &window << "}";
  379. #endif
  380. move_to_front_and_make_active(window);
  381. m_move_window = window.make_weak_ptr();
  382. m_move_origin = event.position();
  383. m_move_window_origin = window.position();
  384. window.invalidate();
  385. }
  386. void WindowManager::start_window_resize(Window& window, const Gfx::IntPoint& position, MouseButton button)
  387. {
  388. move_to_front_and_make_active(window);
  389. constexpr ResizeDirection direction_for_hot_area[3][3] = {
  390. { ResizeDirection::UpLeft, ResizeDirection::Up, ResizeDirection::UpRight },
  391. { ResizeDirection::Left, ResizeDirection::None, ResizeDirection::Right },
  392. { ResizeDirection::DownLeft, ResizeDirection::Down, ResizeDirection::DownRight },
  393. };
  394. Gfx::IntRect outer_rect = window.frame().rect();
  395. if (!outer_rect.contains(position)) {
  396. // FIXME: This used to be an ASSERT but crashing WindowServer over this seems silly.
  397. dbg() << "FIXME: !outer_rect.contains(position): outer_rect=" << outer_rect << ", position=" << position;
  398. }
  399. int window_relative_x = position.x() - outer_rect.x();
  400. int window_relative_y = position.y() - outer_rect.y();
  401. int hot_area_row = min(2, window_relative_y / (outer_rect.height() / 3));
  402. int hot_area_column = min(2, window_relative_x / (outer_rect.width() / 3));
  403. m_resize_direction = direction_for_hot_area[hot_area_row][hot_area_column];
  404. if (m_resize_direction == ResizeDirection::None) {
  405. ASSERT(!m_resize_window);
  406. return;
  407. }
  408. #ifdef RESIZE_DEBUG
  409. dbg() << "[WM] Begin resizing Window{" << &window << "}";
  410. #endif
  411. m_resizing_mouse_button = button;
  412. m_resize_window = window.make_weak_ptr();
  413. m_resize_origin = position;
  414. m_resize_window_original_rect = window.rect();
  415. window.invalidate();
  416. }
  417. void WindowManager::start_window_resize(Window& window, const MouseEvent& event)
  418. {
  419. start_window_resize(window, event.position(), event.button());
  420. }
  421. bool WindowManager::process_ongoing_window_move(MouseEvent& event, Window*& hovered_window)
  422. {
  423. if (!m_move_window)
  424. return false;
  425. if (event.type() == Event::MouseUp && event.button() == MouseButton::Left) {
  426. #ifdef MOVE_DEBUG
  427. dbg() << "[WM] Finish moving Window{" << m_move_window << "}";
  428. #endif
  429. m_move_window->invalidate();
  430. if (m_move_window->rect().contains(event.position()))
  431. hovered_window = m_move_window;
  432. if (m_move_window->is_resizable()) {
  433. process_event_for_doubleclick(*m_move_window, event);
  434. if (event.type() == Event::MouseDoubleClick) {
  435. #if defined(DOUBLECLICK_DEBUG)
  436. dbg() << "[WM] Click up became doubleclick!";
  437. #endif
  438. m_move_window->set_maximized(!m_move_window->is_maximized());
  439. }
  440. }
  441. m_move_window = nullptr;
  442. return true;
  443. }
  444. if (event.type() == Event::MouseMove) {
  445. #ifdef MOVE_DEBUG
  446. dbg() << "[WM] Moving, origin: " << m_move_origin << ", now: " << event.position();
  447. if (m_move_window->is_maximized()) {
  448. dbg() << " [!] The window is still maximized. Not moving yet.";
  449. }
  450. #endif
  451. const int maximization_deadzone = 2;
  452. if (m_move_window->is_maximized()) {
  453. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  454. // dbg() << "[WM] " << pixels_moved_from_start << " moved since start of window move";
  455. if (pixels_moved_from_start > 5) {
  456. // dbg() << "[WM] de-maximizing window";
  457. m_move_origin = event.position();
  458. if (m_move_origin.y() <= maximization_deadzone)
  459. return true;
  460. auto width_before_resize = m_move_window->width();
  461. m_move_window->set_maximized(false);
  462. m_move_window->move_to(m_move_origin.x() - (m_move_window->width() * ((float)m_move_origin.x() / width_before_resize)), m_move_origin.y());
  463. m_move_window_origin = m_move_window->position();
  464. }
  465. } else {
  466. bool is_resizable = m_move_window->is_resizable();
  467. auto pixels_moved_from_start = event.position().pixels_moved(m_move_origin);
  468. const int tiling_deadzone = 5;
  469. if (is_resizable && event.y() <= maximization_deadzone) {
  470. m_move_window->set_tiled(WindowTileType::None);
  471. m_move_window->set_maximized(true);
  472. return true;
  473. }
  474. if (is_resizable && event.x() <= tiling_deadzone) {
  475. m_move_window->set_tiled(WindowTileType::Left);
  476. } else if (is_resizable && event.x() >= Screen::the().width() - tiling_deadzone) {
  477. m_move_window->set_tiled(WindowTileType::Right);
  478. } else if (pixels_moved_from_start > 5 || m_move_window->tiled() == WindowTileType::None) {
  479. m_move_window->set_tiled(WindowTileType::None);
  480. Gfx::IntPoint pos = m_move_window_origin.translated(event.position() - m_move_origin);
  481. m_move_window->set_position_without_repaint(pos);
  482. if (m_move_window->rect().contains(event.position()))
  483. hovered_window = m_move_window;
  484. }
  485. return true;
  486. }
  487. }
  488. return false;
  489. }
  490. bool WindowManager::process_ongoing_window_resize(const MouseEvent& event, Window*& hovered_window)
  491. {
  492. if (!m_resize_window)
  493. return false;
  494. if (event.type() == Event::MouseUp && event.button() == m_resizing_mouse_button) {
  495. #ifdef RESIZE_DEBUG
  496. dbg() << "[WM] Finish resizing Window{" << m_resize_window << "}";
  497. #endif
  498. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(m_resize_window->rect(), 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. auto old_rect = m_resize_window->rect();
  509. int diff_x = event.x() - m_resize_origin.x();
  510. int diff_y = event.y() - m_resize_origin.y();
  511. int change_w = 0;
  512. int change_h = 0;
  513. switch (m_resize_direction) {
  514. case ResizeDirection::DownRight:
  515. change_w = diff_x;
  516. change_h = diff_y;
  517. break;
  518. case ResizeDirection::Right:
  519. change_w = diff_x;
  520. break;
  521. case ResizeDirection::UpRight:
  522. change_w = diff_x;
  523. change_h = -diff_y;
  524. break;
  525. case ResizeDirection::Up:
  526. change_h = -diff_y;
  527. break;
  528. case ResizeDirection::UpLeft:
  529. change_w = -diff_x;
  530. change_h = -diff_y;
  531. break;
  532. case ResizeDirection::Left:
  533. change_w = -diff_x;
  534. break;
  535. case ResizeDirection::DownLeft:
  536. change_w = -diff_x;
  537. change_h = diff_y;
  538. break;
  539. case ResizeDirection::Down:
  540. change_h = diff_y;
  541. break;
  542. default:
  543. ASSERT_NOT_REACHED();
  544. }
  545. auto new_rect = m_resize_window_original_rect;
  546. // First, size the new rect.
  547. Gfx::IntSize minimum_size { 50, 50 };
  548. new_rect.set_width(max(minimum_size.width(), new_rect.width() + change_w));
  549. new_rect.set_height(max(minimum_size.height(), new_rect.height() + change_h));
  550. if (!m_resize_window->size_increment().is_null()) {
  551. int horizontal_incs = (new_rect.width() - m_resize_window->base_size().width()) / m_resize_window->size_increment().width();
  552. new_rect.set_width(m_resize_window->base_size().width() + horizontal_incs * m_resize_window->size_increment().width());
  553. int vertical_incs = (new_rect.height() - m_resize_window->base_size().height()) / m_resize_window->size_increment().height();
  554. new_rect.set_height(m_resize_window->base_size().height() + vertical_incs * m_resize_window->size_increment().height());
  555. }
  556. // Second, set its position so that the sides of the window
  557. // that end up moving are the same ones as the user is dragging,
  558. // no matter which part of the logic above caused us to decide
  559. // to resize by this much.
  560. switch (m_resize_direction) {
  561. case ResizeDirection::DownRight:
  562. case ResizeDirection::Right:
  563. case ResizeDirection::Down:
  564. break;
  565. case ResizeDirection::Left:
  566. case ResizeDirection::Up:
  567. case ResizeDirection::UpLeft:
  568. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  569. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  570. break;
  571. case ResizeDirection::UpRight:
  572. new_rect.set_bottom_without_resize(m_resize_window_original_rect.bottom());
  573. break;
  574. case ResizeDirection::DownLeft:
  575. new_rect.set_right_without_resize(m_resize_window_original_rect.right());
  576. break;
  577. default:
  578. ASSERT_NOT_REACHED();
  579. }
  580. if (new_rect.contains(event.position()))
  581. hovered_window = m_resize_window;
  582. if (m_resize_window->rect() == new_rect)
  583. return true;
  584. #ifdef RESIZE_DEBUG
  585. dbg() << "[WM] Resizing, original: " << m_resize_window_original_rect << ", now: " << new_rect;
  586. #endif
  587. m_resize_window->set_rect(new_rect);
  588. Core::EventLoop::current().post_event(*m_resize_window, make<ResizeEvent>(old_rect, new_rect));
  589. return true;
  590. }
  591. bool WindowManager::process_ongoing_drag(MouseEvent& event, Window*& hovered_window)
  592. {
  593. if (!m_dnd_client)
  594. return false;
  595. if (event.type() == Event::MouseMove) {
  596. // We didn't let go of the drag yet, see if we should send some drag move events..
  597. for_each_visible_window_from_front_to_back([&](Window& window) {
  598. if (!window.rect().contains(event.position()))
  599. return IterationDecision::Continue;
  600. hovered_window = &window;
  601. auto translated_event = event.translated(-window.position());
  602. translated_event.set_drag(true);
  603. translated_event.set_drag_data_type(m_dnd_data_type);
  604. deliver_mouse_event(window, translated_event);
  605. return IterationDecision::Break;
  606. });
  607. }
  608. if (!(event.type() == Event::MouseUp && event.button() == MouseButton::Left))
  609. return true;
  610. hovered_window = nullptr;
  611. for_each_visible_window_from_front_to_back([&](auto& window) {
  612. if (window.frame().rect().contains(event.position())) {
  613. hovered_window = &window;
  614. return IterationDecision::Break;
  615. }
  616. return IterationDecision::Continue;
  617. });
  618. if (hovered_window) {
  619. m_dnd_client->post_message(Messages::WindowClient::DragAccepted());
  620. if (hovered_window->client()) {
  621. auto translated_event = event.translated(-hovered_window->position());
  622. hovered_window->client()->post_message(Messages::WindowClient::DragDropped(hovered_window->window_id(), translated_event.position(), m_dnd_text, m_dnd_data_type, m_dnd_data));
  623. }
  624. } else {
  625. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  626. }
  627. end_dnd_drag();
  628. return true;
  629. }
  630. void WindowManager::set_cursor_tracking_button(Button* button)
  631. {
  632. m_cursor_tracking_button = button ? button->make_weak_ptr() : nullptr;
  633. }
  634. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) const -> const ClickMetadata&
  635. {
  636. switch (button) {
  637. case MouseButton::Left:
  638. return m_left;
  639. case MouseButton::Right:
  640. return m_right;
  641. case MouseButton::Middle:
  642. return m_middle;
  643. case MouseButton::Back:
  644. return m_back;
  645. case MouseButton::Forward:
  646. return m_forward;
  647. default:
  648. ASSERT_NOT_REACHED();
  649. }
  650. }
  651. auto WindowManager::DoubleClickInfo::metadata_for_button(MouseButton button) -> ClickMetadata&
  652. {
  653. switch (button) {
  654. case MouseButton::Left:
  655. return m_left;
  656. case MouseButton::Right:
  657. return m_right;
  658. case MouseButton::Middle:
  659. return m_middle;
  660. case MouseButton::Back:
  661. return m_back;
  662. case MouseButton::Forward:
  663. return m_forward;
  664. default:
  665. ASSERT_NOT_REACHED();
  666. }
  667. }
  668. // #define DOUBLECLICK_DEBUG
  669. bool WindowManager::is_considered_doubleclick(const MouseEvent& event, const DoubleClickInfo::ClickMetadata& metadata) const
  670. {
  671. int elapsed_since_last_click = metadata.clock.elapsed();
  672. if (elapsed_since_last_click < m_double_click_speed) {
  673. auto diff = event.position() - metadata.last_position;
  674. auto distance_travelled_squared = diff.x() * diff.x() + diff.y() * diff.y();
  675. if (distance_travelled_squared <= (m_max_distance_for_double_click * m_max_distance_for_double_click))
  676. return true;
  677. }
  678. return false;
  679. }
  680. void WindowManager::start_menu_doubleclick(Window& window, const MouseEvent& event)
  681. {
  682. // This is a special case. Basically, we're trying to determine whether
  683. // double clicking on the window menu icon happened. In this case, the
  684. // WindowFrame only receives a MouseDown event, and since the window
  685. // menu popus up, it does not see the MouseUp event. But, if they subsequently
  686. // click there again, the menu is closed and we receive a MouseUp event.
  687. // So, in order to be able to detect a double click when a menu is being
  688. // opened by the MouseDown event, we need to consider the MouseDown event
  689. // as a potential double-click trigger
  690. ASSERT(event.type() == Event::MouseDown);
  691. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  692. if (&window != m_double_click_info.m_clicked_window) {
  693. // we either haven't clicked anywhere, or we haven't clicked on this
  694. // window. set the current click window, and reset the timers.
  695. #if defined(DOUBLECLICK_DEBUG)
  696. dbg() << "Initial mousedown on window " << &window << " for menu (previous was " << m_double_click_info.m_clicked_window << ')';
  697. #endif
  698. m_double_click_info.m_clicked_window = window.make_weak_ptr();
  699. m_double_click_info.reset();
  700. }
  701. metadata.last_position = event.position();
  702. metadata.clock.start();
  703. }
  704. bool WindowManager::is_menu_doubleclick(Window& window, const MouseEvent& event) const
  705. {
  706. ASSERT(event.type() == Event::MouseUp);
  707. if (&window != m_double_click_info.m_clicked_window)
  708. return false;
  709. auto& metadata = m_double_click_info.metadata_for_button(event.button());
  710. if (!metadata.clock.is_valid())
  711. return false;
  712. return is_considered_doubleclick(event, metadata);
  713. }
  714. void WindowManager::process_event_for_doubleclick(Window& window, MouseEvent& event)
  715. {
  716. // We only care about button presses (because otherwise it's not a doubleclick, duh!)
  717. ASSERT(event.type() == Event::MouseUp);
  718. if (&window != m_double_click_info.m_clicked_window) {
  719. // we either haven't clicked anywhere, or we haven't clicked on this
  720. // window. set the current click window, and reset the timers.
  721. #if defined(DOUBLECLICK_DEBUG)
  722. dbg() << "Initial mouseup on window " << &window << " (previous was " << m_double_click_info.m_clicked_window << ')';
  723. #endif
  724. m_double_click_info.m_clicked_window = window.make_weak_ptr();
  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. #if defined(DOUBLECLICK_DEBUG)
  736. dbg() << "Transforming MouseUp to MouseDoubleClick (" << elapsed_since_last_click << " < " << m_double_click_speed << ")!";
  737. #endif
  738. event = MouseEvent(Event::MouseDoubleClick, event.position(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  739. // invalidate this now we've delivered a doubleclick, otherwise
  740. // tripleclick will deliver two doubleclick events (incorrectly).
  741. metadata.clock = {};
  742. }
  743. metadata.last_position = event.position();
  744. }
  745. void WindowManager::deliver_mouse_event(Window& window, MouseEvent& event)
  746. {
  747. window.dispatch_event(event);
  748. if (event.type() == Event::MouseUp) {
  749. process_event_for_doubleclick(window, event);
  750. if (event.type() == Event::MouseDoubleClick)
  751. window.dispatch_event(event);
  752. }
  753. }
  754. void WindowManager::process_mouse_event(MouseEvent& event, Window*& hovered_window)
  755. {
  756. hovered_window = nullptr;
  757. if (process_ongoing_drag(event, hovered_window))
  758. return;
  759. if (process_ongoing_window_move(event, hovered_window))
  760. return;
  761. if (process_ongoing_window_resize(event, hovered_window))
  762. return;
  763. if (m_cursor_tracking_button)
  764. return m_cursor_tracking_button->on_mouse_event(event.translated(-m_cursor_tracking_button->screen_rect().location()));
  765. // This is quite hackish, but it's how the Button hover effect is implemented.
  766. if (m_hovered_button && event.type() == Event::MouseMove)
  767. m_hovered_button->on_mouse_event(event.translated(-m_hovered_button->screen_rect().location()));
  768. HashTable<Window*> windows_who_received_mouse_event_due_to_cursor_tracking;
  769. for (auto* window = m_windows_in_order.tail(); window; window = window->prev()) {
  770. if (!window->global_cursor_tracking() || !window->is_visible() || window->is_minimized() || window->is_blocked_by_modal_window())
  771. continue;
  772. windows_who_received_mouse_event_due_to_cursor_tracking.set(window);
  773. auto translated_event = event.translated(-window->position());
  774. deliver_mouse_event(*window, translated_event);
  775. }
  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. clear_resize_candidate();
  779. MenuManager::the().dispatch_event(event);
  780. return;
  781. }
  782. Window* event_window_with_frame = nullptr;
  783. if (m_active_input_tracking_window) {
  784. // At this point, we have delivered the start of an input sequence to a
  785. // client application. We must keep delivering to that client
  786. // application until the input sequence is done.
  787. //
  788. // This prevents e.g. moving on one window out of the bounds starting
  789. // a move in that other unrelated window, and other silly shenanigans.
  790. if (!windows_who_received_mouse_event_due_to_cursor_tracking.contains(m_active_input_tracking_window)) {
  791. auto translated_event = event.translated(-m_active_input_tracking_window->position());
  792. deliver_mouse_event(*m_active_input_tracking_window, translated_event);
  793. windows_who_received_mouse_event_due_to_cursor_tracking.set(m_active_input_tracking_window.ptr());
  794. }
  795. if (event.type() == Event::MouseUp && event.buttons() == 0) {
  796. m_active_input_tracking_window = nullptr;
  797. }
  798. for_each_visible_window_from_front_to_back([&](auto& window) {
  799. if (window.frame().rect().contains(event.position())) {
  800. hovered_window = &window;
  801. return IterationDecision::Break;
  802. }
  803. return IterationDecision::Continue;
  804. });
  805. } else {
  806. auto process_mouse_event_for_window = [&](Window& window) {
  807. if (&window != m_resize_candidate.ptr())
  808. clear_resize_candidate();
  809. // First check if we should initiate a move or resize (Logo+LMB or Logo+RMB).
  810. // In those cases, the event is swallowed by the window manager.
  811. if (window.is_movable()) {
  812. if (!window.is_fullscreen() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Left) {
  813. hovered_window = &window;
  814. start_window_move(window, event);
  815. return;
  816. }
  817. if (window.is_resizable() && m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseDown && event.button() == MouseButton::Right && !window.is_blocked_by_modal_window()) {
  818. hovered_window = &window;
  819. start_window_resize(window, event);
  820. return;
  821. }
  822. }
  823. if (m_keyboard_modifiers == Mod_Logo && event.type() == Event::MouseWheel) {
  824. float opacity_change = -event.wheel_delta() * 0.05f;
  825. float new_opacity = window.opacity() + opacity_change;
  826. if (new_opacity < 0.05f)
  827. new_opacity = 0.05f;
  828. if (new_opacity > 1.0f)
  829. new_opacity = 1.0f;
  830. window.set_opacity(new_opacity);
  831. window.invalidate();
  832. return;
  833. }
  834. // Well okay, let's see if we're hitting the frame or the window inside the frame.
  835. if (window.rect().contains(event.position())) {
  836. if (event.type() == Event::MouseDown) {
  837. if (window.type() == WindowType::Normal)
  838. move_to_front_and_make_active(window);
  839. else if (window.type() == WindowType::Desktop)
  840. set_active_window(&window);
  841. }
  842. hovered_window = &window;
  843. if (!window.global_cursor_tracking() && !windows_who_received_mouse_event_due_to_cursor_tracking.contains(&window) && !window.is_blocked_by_modal_window()) {
  844. auto translated_event = event.translated(-window.position());
  845. deliver_mouse_event(window, translated_event);
  846. if (event.type() == Event::MouseDown) {
  847. m_active_input_tracking_window = window.make_weak_ptr();
  848. }
  849. }
  850. return;
  851. }
  852. // We are hitting the frame, pass the event along to WindowFrame.
  853. window.frame().on_mouse_event(event.translated(-window.frame().rect().location()));
  854. event_window_with_frame = &window;
  855. };
  856. if (auto* fullscreen_window = active_fullscreen_window()) {
  857. process_mouse_event_for_window(*fullscreen_window);
  858. } else {
  859. for_each_visible_window_from_front_to_back([&](Window& window) {
  860. auto window_frame_rect = window.frame().rect();
  861. if (!window_frame_rect.contains(event.position()))
  862. return IterationDecision::Continue;
  863. process_mouse_event_for_window(window);
  864. return IterationDecision::Break;
  865. });
  866. }
  867. // Clicked outside of any window
  868. if (!hovered_window && !event_window_with_frame && event.type() == Event::MouseDown)
  869. set_active_window(nullptr);
  870. }
  871. if (event_window_with_frame != m_resize_candidate.ptr())
  872. clear_resize_candidate();
  873. }
  874. void WindowManager::clear_resize_candidate()
  875. {
  876. if (m_resize_candidate)
  877. Compositor::the().invalidate_cursor();
  878. m_resize_candidate = nullptr;
  879. }
  880. Gfx::IntRect WindowManager::menubar_rect() const
  881. {
  882. if (active_fullscreen_window())
  883. return {};
  884. return MenuManager::the().menubar_rect();
  885. }
  886. Gfx::IntRect WindowManager::desktop_rect() const
  887. {
  888. if (active_fullscreen_window())
  889. return {};
  890. return {
  891. 0,
  892. menubar_rect().bottom() + 1,
  893. Screen::the().width(),
  894. Screen::the().height() - menubar_rect().height() - 28
  895. };
  896. }
  897. void WindowManager::event(Core::Event& event)
  898. {
  899. if (static_cast<Event&>(event).is_mouse_event()) {
  900. Window* hovered_window = nullptr;
  901. process_mouse_event(static_cast<MouseEvent&>(event), hovered_window);
  902. set_hovered_window(hovered_window);
  903. return;
  904. }
  905. if (static_cast<Event&>(event).is_key_event()) {
  906. auto& key_event = static_cast<const KeyEvent&>(event);
  907. m_keyboard_modifiers = key_event.modifiers();
  908. if (key_event.type() == Event::KeyDown && key_event.key() == Key_Escape && m_dnd_client) {
  909. m_dnd_client->post_message(Messages::WindowClient::DragCancelled());
  910. end_dnd_drag();
  911. return;
  912. }
  913. if (MenuManager::the().current_menu()) {
  914. MenuManager::the().dispatch_event(event);
  915. return;
  916. }
  917. 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)))
  918. m_switcher.show();
  919. if (m_switcher.is_visible()) {
  920. m_switcher.on_key_event(key_event);
  921. return;
  922. }
  923. if (m_active_input_window) {
  924. if (key_event.type() == Event::KeyDown && key_event.modifiers() == Mod_Logo) {
  925. if (key_event.key() == Key_Down) {
  926. if (m_active_input_window->is_resizable() && m_active_input_window->is_maximized()) {
  927. m_active_input_window->set_maximized(false);
  928. return;
  929. }
  930. if (m_active_input_window->is_minimizable())
  931. m_active_input_window->set_minimized(true);
  932. return;
  933. }
  934. if (m_active_input_window->is_resizable()) {
  935. if (key_event.key() == Key_Up) {
  936. m_active_input_window->set_maximized(!m_active_input_window->is_maximized());
  937. return;
  938. }
  939. if (key_event.key() == Key_Left) {
  940. if (m_active_input_window->tiled() != WindowTileType::None) {
  941. m_active_input_window->set_tiled(WindowTileType::None);
  942. return;
  943. }
  944. if (m_active_input_window->is_maximized())
  945. m_active_input_window->set_maximized(false);
  946. m_active_input_window->set_tiled(WindowTileType::Left);
  947. return;
  948. }
  949. if (key_event.key() == Key_Right) {
  950. if (m_active_input_window->tiled() != WindowTileType::None) {
  951. m_active_input_window->set_tiled(WindowTileType::None);
  952. return;
  953. }
  954. if (m_active_input_window->is_maximized())
  955. m_active_input_window->set_maximized(false);
  956. m_active_input_window->set_tiled(WindowTileType::Right);
  957. return;
  958. }
  959. }
  960. }
  961. m_active_input_window->dispatch_event(event);
  962. return;
  963. }
  964. }
  965. Core::Object::event(event);
  966. }
  967. void WindowManager::set_highlight_window(Window* window)
  968. {
  969. if (window == m_highlight_window)
  970. return;
  971. if (auto* previous_highlight_window = m_highlight_window.ptr())
  972. previous_highlight_window->invalidate();
  973. m_highlight_window = window ? window->make_weak_ptr() : nullptr;
  974. if (m_highlight_window)
  975. m_highlight_window->invalidate();
  976. }
  977. bool WindowManager::is_active_window_or_accessory(Window& window) const
  978. {
  979. if (m_active_window == &window)
  980. return true;
  981. if (!window.is_accessory())
  982. return false;
  983. return m_active_window == window.parent_window();
  984. }
  985. static bool window_type_can_become_active(WindowType type)
  986. {
  987. return type == WindowType::Normal || type == WindowType::Desktop;
  988. }
  989. void WindowManager::restore_active_input_window(Window* window)
  990. {
  991. // If the previous active input window is gone, fall back to the
  992. // current active window
  993. if (!window)
  994. window = active_window();
  995. // If the current active window is also gone, pick some other window
  996. if (!window && pick_new_active_window(nullptr))
  997. return;
  998. set_active_input_window(window);
  999. }
  1000. Window* WindowManager::set_active_input_window(Window* window)
  1001. {
  1002. if (window == m_active_input_window)
  1003. return window;
  1004. Window* previous_input_window = m_active_input_window;
  1005. if (previous_input_window)
  1006. Core::EventLoop::current().post_event(*previous_input_window, make<Event>(Event::WindowInputLeft));
  1007. if (window) {
  1008. m_active_input_window = window->make_weak_ptr();
  1009. Core::EventLoop::current().post_event(*window, make<Event>(Event::WindowInputEntered));
  1010. } else {
  1011. m_active_input_window = nullptr;
  1012. }
  1013. return previous_input_window;
  1014. }
  1015. void WindowManager::set_active_window(Window* window, bool make_input)
  1016. {
  1017. if (window) {
  1018. if (auto* modal_window = window->is_blocked_by_modal_window()) {
  1019. ASSERT(modal_window->is_modal());
  1020. ASSERT(modal_window != window);
  1021. window = modal_window;
  1022. make_input = true;
  1023. }
  1024. if (!window_type_can_become_active(window->type()))
  1025. return;
  1026. }
  1027. auto* new_active_input_window = window;
  1028. if (window && window->is_accessory()) {
  1029. // The parent of an accessory window is always the active
  1030. // window, but input is routed to the accessory window
  1031. window = window->parent_window();
  1032. }
  1033. if (make_input)
  1034. set_active_input_window(new_active_input_window);
  1035. if (window == m_active_window)
  1036. return;
  1037. auto* previously_active_window = m_active_window.ptr();
  1038. ClientConnection* previously_active_client = nullptr;
  1039. ClientConnection* active_client = nullptr;
  1040. if (previously_active_window) {
  1041. previously_active_client = previously_active_window->client();
  1042. Core::EventLoop::current().post_event(*previously_active_window, make<Event>(Event::WindowDeactivated));
  1043. previously_active_window->invalidate();
  1044. m_active_window = nullptr;
  1045. m_active_input_tracking_window = nullptr;
  1046. tell_wm_listeners_window_state_changed(*previously_active_window);
  1047. }
  1048. if (window) {
  1049. m_active_window = window->make_weak_ptr();
  1050. active_client = m_active_window->client();
  1051. Core::EventLoop::current().post_event(*m_active_window, make<Event>(Event::WindowActivated));
  1052. m_active_window->invalidate();
  1053. if (auto* client = window->client()) {
  1054. MenuManager::the().set_current_menubar(client->app_menubar());
  1055. } else {
  1056. MenuManager::the().set_current_menubar(nullptr);
  1057. }
  1058. tell_wm_listeners_window_state_changed(*m_active_window);
  1059. } else {
  1060. MenuManager::the().set_current_menubar(nullptr);
  1061. }
  1062. if (active_client != previously_active_client) {
  1063. if (previously_active_client)
  1064. previously_active_client->deboost();
  1065. if (active_client)
  1066. active_client->boost();
  1067. }
  1068. }
  1069. void WindowManager::set_hovered_window(Window* window)
  1070. {
  1071. if (m_hovered_window == window)
  1072. return;
  1073. if (m_hovered_window)
  1074. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowLeft));
  1075. m_hovered_window = window ? window->make_weak_ptr() : nullptr;
  1076. if (m_hovered_window)
  1077. Core::EventLoop::current().post_event(*m_hovered_window, make<Event>(Event::WindowEntered));
  1078. }
  1079. void WindowManager::invalidate()
  1080. {
  1081. Compositor::the().invalidate();
  1082. }
  1083. void WindowManager::invalidate(const Gfx::IntRect& rect)
  1084. {
  1085. Compositor::the().invalidate(rect);
  1086. }
  1087. const ClientConnection* WindowManager::active_client() const
  1088. {
  1089. if (m_active_window)
  1090. return m_active_window->client();
  1091. return nullptr;
  1092. }
  1093. void WindowManager::notify_client_changed_app_menubar(ClientConnection& client)
  1094. {
  1095. if (active_client() == &client)
  1096. MenuManager::the().set_current_menubar(client.app_menubar());
  1097. }
  1098. const Cursor& WindowManager::active_cursor() const
  1099. {
  1100. if (m_dnd_client)
  1101. return *m_drag_cursor;
  1102. if (m_move_window)
  1103. return *m_move_cursor;
  1104. if (m_resize_window || m_resize_candidate) {
  1105. switch (m_resize_direction) {
  1106. case ResizeDirection::Up:
  1107. case ResizeDirection::Down:
  1108. return *m_resize_vertically_cursor;
  1109. case ResizeDirection::Left:
  1110. case ResizeDirection::Right:
  1111. return *m_resize_horizontally_cursor;
  1112. case ResizeDirection::UpLeft:
  1113. case ResizeDirection::DownRight:
  1114. return *m_resize_diagonally_tlbr_cursor;
  1115. case ResizeDirection::UpRight:
  1116. case ResizeDirection::DownLeft:
  1117. return *m_resize_diagonally_bltr_cursor;
  1118. case ResizeDirection::None:
  1119. break;
  1120. }
  1121. }
  1122. if (m_hovered_window && m_hovered_window->override_cursor())
  1123. return *m_hovered_window->override_cursor();
  1124. return *m_arrow_cursor;
  1125. }
  1126. void WindowManager::set_hovered_button(Button* button)
  1127. {
  1128. m_hovered_button = button ? button->make_weak_ptr() : nullptr;
  1129. }
  1130. void WindowManager::set_resize_candidate(Window& window, ResizeDirection direction)
  1131. {
  1132. m_resize_candidate = window.make_weak_ptr();
  1133. m_resize_direction = direction;
  1134. }
  1135. ResizeDirection WindowManager::resize_direction_of_window(const Window& window)
  1136. {
  1137. if (&window != m_resize_window)
  1138. return ResizeDirection::None;
  1139. return m_resize_direction;
  1140. }
  1141. Gfx::IntRect WindowManager::maximized_window_rect(const Window& window) const
  1142. {
  1143. Gfx::IntRect rect = Screen::the().rect();
  1144. // Subtract window title bar (leaving the border)
  1145. rect.set_y(rect.y() + window.frame().title_bar_rect().height());
  1146. rect.set_height(rect.height() - window.frame().title_bar_rect().height());
  1147. // Subtract menu bar
  1148. rect.set_y(rect.y() + menubar_rect().height());
  1149. rect.set_height(rect.height() - menubar_rect().height());
  1150. // Subtract taskbar window height if present
  1151. const_cast<WindowManager*>(this)->for_each_visible_window_of_type_from_back_to_front(WindowType::Taskbar, [&rect](Window& taskbar_window) {
  1152. rect.set_height(rect.height() - taskbar_window.height());
  1153. return IterationDecision::Break;
  1154. });
  1155. constexpr int tasteful_space_above_maximized_window = 2;
  1156. rect.set_y(rect.y() + tasteful_space_above_maximized_window);
  1157. rect.set_height(rect.height() - tasteful_space_above_maximized_window);
  1158. return rect;
  1159. }
  1160. void WindowManager::start_dnd_drag(ClientConnection& client, const String& text, Gfx::Bitmap* bitmap, const String& data_type, const String& data)
  1161. {
  1162. ASSERT(!m_dnd_client);
  1163. m_dnd_client = client.make_weak_ptr();
  1164. m_dnd_text = text;
  1165. m_dnd_bitmap = bitmap;
  1166. m_dnd_data_type = data_type;
  1167. m_dnd_data = data;
  1168. Compositor::the().invalidate_cursor();
  1169. m_active_input_tracking_window = nullptr;
  1170. }
  1171. void WindowManager::end_dnd_drag()
  1172. {
  1173. ASSERT(m_dnd_client);
  1174. Compositor::the().invalidate_cursor();
  1175. m_dnd_client = nullptr;
  1176. m_dnd_text = {};
  1177. m_dnd_bitmap = nullptr;
  1178. }
  1179. Gfx::IntRect WindowManager::dnd_rect() const
  1180. {
  1181. int bitmap_width = m_dnd_bitmap ? m_dnd_bitmap->width() : 0;
  1182. int bitmap_height = m_dnd_bitmap ? m_dnd_bitmap->height() : 0;
  1183. int width = font().width(m_dnd_text) + bitmap_width;
  1184. int height = max((int)font().glyph_height(), bitmap_height);
  1185. auto location = Compositor::the().current_cursor_rect().center().translated(8, 8);
  1186. return Gfx::IntRect(location, { width, height }).inflated(4, 4);
  1187. }
  1188. bool WindowManager::update_theme(String theme_path, String theme_name)
  1189. {
  1190. auto new_theme = Gfx::load_system_theme(theme_path);
  1191. if (!new_theme)
  1192. return false;
  1193. ASSERT(new_theme);
  1194. Gfx::set_system_theme(*new_theme);
  1195. m_palette = Gfx::PaletteImpl::create_with_shared_buffer(*new_theme);
  1196. Compositor::the().set_background_color(palette().desktop_background().to_string());
  1197. HashTable<ClientConnection*> notified_clients;
  1198. for_each_window([&](Window& window) {
  1199. if (window.client()) {
  1200. if (!notified_clients.contains(window.client())) {
  1201. window.client()->post_message(Messages::WindowClient::UpdateSystemTheme(Gfx::current_system_theme_buffer_id()));
  1202. notified_clients.set(window.client());
  1203. }
  1204. }
  1205. window.frame().layout_buttons();
  1206. return IterationDecision::Continue;
  1207. });
  1208. MenuManager::the().did_change_theme();
  1209. auto wm_config = Core::ConfigFile::open("/etc/WindowServer/WindowServer.ini");
  1210. wm_config->write_entry("Theme", "Name", theme_name);
  1211. wm_config->sync();
  1212. invalidate();
  1213. return true;
  1214. }
  1215. void WindowManager::did_popup_a_menu(Badge<Menu>)
  1216. {
  1217. // Clear any ongoing input gesture
  1218. if (!m_active_input_tracking_window)
  1219. return;
  1220. m_active_input_tracking_window->set_automatic_cursor_tracking_enabled(false);
  1221. m_active_input_tracking_window = nullptr;
  1222. }
  1223. void WindowManager::minimize_windows(Window& window, bool minimized)
  1224. {
  1225. for_each_window_in_modal_stack(window, [&](auto& w, bool) {
  1226. w.set_minimized(minimized);
  1227. });
  1228. }
  1229. void WindowManager::maximize_windows(Window& window, bool maximized)
  1230. {
  1231. for_each_window_in_modal_stack(window, [&](auto& w, bool) {
  1232. w.set_maximized(maximized);
  1233. if (w.is_minimized())
  1234. w.set_minimized(false);
  1235. });
  1236. }
  1237. }