WindowManager.cpp 54 KB

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