Window.cpp 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474
  1. /*
  2. * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/HashMap.h>
  8. #include <AK/IDAllocator.h>
  9. #include <AK/JsonObject.h>
  10. #include <AK/NeverDestroyed.h>
  11. #include <AK/ScopeGuard.h>
  12. #include <LibConfig/Client.h>
  13. #include <LibCore/EventLoop.h>
  14. #include <LibCore/MimeData.h>
  15. #include <LibGUI/Action.h>
  16. #include <LibGUI/Application.h>
  17. #include <LibGUI/ConnectionToWindowManagerServer.h>
  18. #include <LibGUI/ConnectionToWindowServer.h>
  19. #include <LibGUI/Desktop.h>
  20. #include <LibGUI/Event.h>
  21. #include <LibGUI/MenuItem.h>
  22. #include <LibGUI/Menubar.h>
  23. #include <LibGUI/Painter.h>
  24. #include <LibGUI/Widget.h>
  25. #include <LibGUI/Window.h>
  26. #include <LibGfx/Bitmap.h>
  27. #include <LibGfx/Palette.h>
  28. #include <fcntl.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <unistd.h>
  32. namespace GUI {
  33. static i32 s_next_backing_store_serial;
  34. static IDAllocator s_window_id_allocator;
  35. class WindowBackingStore {
  36. public:
  37. explicit WindowBackingStore(NonnullRefPtr<Gfx::Bitmap> bitmap)
  38. : m_bitmap(move(bitmap))
  39. , m_serial(++s_next_backing_store_serial)
  40. , m_visible_size(m_bitmap->size())
  41. {
  42. }
  43. Gfx::Bitmap& bitmap() { return *m_bitmap; }
  44. Gfx::Bitmap const& bitmap() const { return *m_bitmap; }
  45. Gfx::IntSize size() const { return m_bitmap->size(); }
  46. i32 serial() const { return m_serial; }
  47. Gfx::IntSize visible_size() const { return m_visible_size; }
  48. void set_visible_size(Gfx::IntSize visible_size) { m_visible_size = visible_size; }
  49. [[nodiscard]] bool is_volatile() const { return m_volatile; }
  50. void set_volatile()
  51. {
  52. if (m_volatile)
  53. return;
  54. #ifdef AK_OS_SERENITY
  55. int rc = madvise(m_bitmap->scanline_u8(0), m_bitmap->data_size(), MADV_SET_VOLATILE);
  56. if (rc < 0) {
  57. perror("madvise(MADV_SET_VOLATILE)");
  58. VERIFY_NOT_REACHED();
  59. }
  60. #endif
  61. m_volatile = true;
  62. }
  63. // Returns true if making the bitmap non-volatile succeeded. `was_purged` indicates status of contents.
  64. // Returns false if there was not enough memory.
  65. [[nodiscard]] bool set_nonvolatile(bool& was_purged)
  66. {
  67. if (!m_volatile) {
  68. was_purged = false;
  69. return true;
  70. }
  71. #ifdef AK_OS_SERENITY
  72. int rc = madvise(m_bitmap->scanline_u8(0), m_bitmap->data_size(), MADV_SET_NONVOLATILE);
  73. if (rc < 0) {
  74. if (errno == ENOMEM) {
  75. was_purged = true;
  76. return false;
  77. }
  78. perror("madvise(MADV_SET_NONVOLATILE)");
  79. VERIFY_NOT_REACHED();
  80. }
  81. was_purged = rc != 0;
  82. #endif
  83. m_volatile = false;
  84. return true;
  85. }
  86. private:
  87. NonnullRefPtr<Gfx::Bitmap> m_bitmap;
  88. i32 const m_serial;
  89. Gfx::IntSize m_visible_size;
  90. bool m_volatile { false };
  91. };
  92. static NeverDestroyed<HashTable<Window*>> all_windows;
  93. static NeverDestroyed<HashMap<int, Window*>> reified_windows;
  94. Window* Window::from_window_id(int window_id)
  95. {
  96. auto it = reified_windows->find(window_id);
  97. if (it != reified_windows->end())
  98. return (*it).value;
  99. return nullptr;
  100. }
  101. Window::Window(Core::EventReceiver* parent)
  102. : GUI::Object(parent)
  103. , m_menubar(Menubar::construct())
  104. , m_pid(getpid())
  105. {
  106. if (parent)
  107. set_window_mode(WindowMode::Passive);
  108. all_windows->set(this);
  109. m_rect_when_windowless = { -5000, -5000, 0, 0 };
  110. m_floating_rect = { -5000, -5000, 0, 0 };
  111. m_title_when_windowless = "GUI::Window";
  112. REGISTER_DEPRECATED_STRING_PROPERTY("title", title, set_title)
  113. register_property(
  114. "visible"sv, [this] { return is_visible(); }, nullptr, nullptr);
  115. register_property(
  116. "active"sv, [this] { return is_active(); }, nullptr, nullptr);
  117. REGISTER_BOOL_PROPERTY("minimizable", is_minimizable, set_minimizable);
  118. REGISTER_BOOL_PROPERTY("resizable", is_resizable, set_resizable);
  119. REGISTER_BOOL_PROPERTY("fullscreen", is_fullscreen, set_fullscreen);
  120. REGISTER_RECT_PROPERTY("rect", rect, set_rect);
  121. REGISTER_SIZE_PROPERTY("base_size", base_size, set_base_size);
  122. REGISTER_SIZE_PROPERTY("size_increment", size_increment, set_size_increment);
  123. REGISTER_BOOL_PROPERTY("obey_widget_min_size", is_obeying_widget_min_size, set_obey_widget_min_size);
  124. }
  125. Window::~Window()
  126. {
  127. all_windows->remove(this);
  128. hide();
  129. }
  130. void Window::close()
  131. {
  132. hide();
  133. if (m_save_size_and_position_on_close)
  134. save_size_and_position(m_save_domain, m_save_group);
  135. if (on_close)
  136. on_close();
  137. }
  138. void Window::move_to_front()
  139. {
  140. if (!is_visible())
  141. return;
  142. ConnectionToWindowServer::the().async_move_window_to_front(m_window_id);
  143. }
  144. void Window::show()
  145. {
  146. if (is_visible())
  147. return;
  148. auto* parent_window = find_parent_window();
  149. m_window_id = s_window_id_allocator.allocate();
  150. Gfx::IntRect launch_origin_rect;
  151. if (auto* launch_origin_rect_string = getenv("__libgui_launch_origin_rect")) {
  152. auto parts = StringView { launch_origin_rect_string, strlen(launch_origin_rect_string) }.split_view(',');
  153. if (parts.size() == 4) {
  154. launch_origin_rect = Gfx::IntRect {
  155. parts[0].to_number<int>().value_or(0),
  156. parts[1].to_number<int>().value_or(0),
  157. parts[2].to_number<int>().value_or(0),
  158. parts[3].to_number<int>().value_or(0),
  159. };
  160. }
  161. unsetenv("__libgui_launch_origin_rect");
  162. }
  163. update_min_size();
  164. ConnectionToWindowServer::the().async_create_window(
  165. m_window_id,
  166. m_pid,
  167. m_rect_when_windowless,
  168. !m_moved_by_client,
  169. m_has_alpha_channel,
  170. m_minimizable,
  171. m_closeable,
  172. m_resizable,
  173. m_fullscreen,
  174. m_frameless,
  175. m_forced_shadow,
  176. m_alpha_hit_threshold,
  177. m_base_size,
  178. m_size_increment,
  179. m_minimum_size_when_windowless,
  180. m_resize_aspect_ratio,
  181. (i32)m_window_type,
  182. (i32)m_window_mode,
  183. m_title_when_windowless,
  184. parent_window ? parent_window->window_id() : 0,
  185. launch_origin_rect);
  186. m_visible = true;
  187. m_visible_for_timer_purposes = true;
  188. apply_icon();
  189. m_menubar->for_each_menu([&](Menu& menu) {
  190. menu.realize_menu_if_needed();
  191. ConnectionToWindowServer::the().async_add_menu(m_window_id, menu.menu_id());
  192. return IterationDecision::Continue;
  193. });
  194. set_maximized(m_maximized);
  195. reified_windows->set(m_window_id, this);
  196. Application::the()->did_create_window({});
  197. update();
  198. }
  199. Window* Window::find_parent_window()
  200. {
  201. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  202. if (is<Window>(ancestor))
  203. return static_cast<Window*>(ancestor);
  204. }
  205. return nullptr;
  206. }
  207. void Window::server_did_destroy()
  208. {
  209. reified_windows->remove(m_window_id);
  210. m_window_id = 0;
  211. m_visible = false;
  212. m_pending_paint_event_rects.clear();
  213. m_back_store = nullptr;
  214. m_front_store = nullptr;
  215. m_cursor = Gfx::StandardCursor::None;
  216. }
  217. void Window::hide()
  218. {
  219. if (!is_visible())
  220. return;
  221. // NOTE: Don't bother asking WindowServer to destroy windows during application teardown.
  222. // All our windows will be automatically garbage-collected by WindowServer anyway.
  223. if (GUI::Application::in_teardown())
  224. return;
  225. m_rect_when_windowless = rect();
  226. m_floating_rect = floating_rect();
  227. auto destroyed_window_ids = ConnectionToWindowServer::the().destroy_window(m_window_id);
  228. server_did_destroy();
  229. for (auto child_window_id : destroyed_window_ids) {
  230. if (auto* window = Window::from_window_id(child_window_id)) {
  231. window->server_did_destroy();
  232. }
  233. }
  234. if (auto* app = Application::the()) {
  235. bool app_has_visible_windows = false;
  236. for (auto& window : *all_windows) {
  237. if (window->is_visible()) {
  238. app_has_visible_windows = true;
  239. break;
  240. }
  241. }
  242. if (!app_has_visible_windows)
  243. app->did_delete_last_window({});
  244. }
  245. }
  246. void Window::set_title(ByteString title)
  247. {
  248. m_title_when_windowless = move(title);
  249. if (!is_visible())
  250. return;
  251. ConnectionToWindowServer::the().async_set_window_title(m_window_id, m_title_when_windowless);
  252. }
  253. ByteString Window::title() const
  254. {
  255. if (!is_visible())
  256. return m_title_when_windowless;
  257. return ConnectionToWindowServer::the().get_window_title(m_window_id);
  258. }
  259. Gfx::IntRect Window::applet_rect_on_screen() const
  260. {
  261. VERIFY(m_window_type == WindowType::Applet);
  262. return ConnectionToWindowServer::the().get_applet_rect_on_screen(m_window_id);
  263. }
  264. Gfx::IntRect Window::rect() const
  265. {
  266. if (!is_visible())
  267. return m_rect_when_windowless;
  268. return ConnectionToWindowServer::the().get_window_rect(m_window_id);
  269. }
  270. Gfx::IntRect Window::floating_rect() const
  271. {
  272. if (!is_visible())
  273. return m_floating_rect;
  274. return ConnectionToWindowServer::the().get_window_floating_rect(m_window_id);
  275. }
  276. void Window::set_rect(Gfx::IntRect const& a_rect)
  277. {
  278. if (a_rect.location() != m_rect_when_windowless.location()) {
  279. m_moved_by_client = true;
  280. }
  281. m_rect_when_windowless = a_rect;
  282. m_floating_rect = a_rect;
  283. if (!is_visible()) {
  284. if (m_main_widget)
  285. m_main_widget->resize(m_rect_when_windowless.size());
  286. return;
  287. }
  288. auto window_rect = ConnectionToWindowServer::the().set_window_rect(m_window_id, a_rect);
  289. if (m_back_store && m_back_store->size() != window_rect.size())
  290. m_back_store = nullptr;
  291. if (m_front_store && m_front_store->size() != window_rect.size())
  292. m_front_store = nullptr;
  293. if (m_main_widget)
  294. m_main_widget->resize(window_rect.size());
  295. }
  296. Gfx::IntSize Window::minimum_size() const
  297. {
  298. if (!is_visible())
  299. return m_minimum_size_when_windowless;
  300. return ConnectionToWindowServer::the().get_window_minimum_size(m_window_id);
  301. }
  302. void Window::set_minimum_size(Gfx::IntSize size)
  303. {
  304. VERIFY(size.width() >= 0 && size.height() >= 0);
  305. VERIFY(!is_obeying_widget_min_size());
  306. m_minimum_size_when_windowless = size;
  307. if (is_visible())
  308. ConnectionToWindowServer::the().async_set_window_minimum_size(m_window_id, size);
  309. }
  310. void Window::center_on_screen()
  311. {
  312. set_rect(rect().centered_within(Desktop::the().rect()));
  313. }
  314. void Window::center_within(Window const& other)
  315. {
  316. if (this == &other)
  317. return;
  318. set_rect(rect().centered_within(other.rect()));
  319. }
  320. void Window::center_within(Gfx::IntRect const& other)
  321. {
  322. set_rect(rect().centered_within(other));
  323. }
  324. void Window::constrain_to_desktop()
  325. {
  326. auto desktop_rect = Desktop::the().rect().shrunken(0, 0, Desktop::the().taskbar_height(), 0);
  327. auto titlebar = Application::the()->palette().window_title_height();
  328. auto border = Application::the()->palette().window_border_thickness();
  329. auto constexpr margin = 1;
  330. auto framed_rect = rect().inflated(border + titlebar + margin, border, border, border);
  331. if (desktop_rect.contains(framed_rect))
  332. return;
  333. auto constrained = framed_rect.constrained_to(desktop_rect);
  334. constrained.shrink(border + titlebar + margin, border, border, border);
  335. set_rect(constrained.x(), constrained.y(), rect().width(), rect().height());
  336. }
  337. void Window::set_window_type(WindowType window_type)
  338. {
  339. m_window_type = window_type;
  340. }
  341. void Window::set_window_mode(WindowMode mode)
  342. {
  343. VERIFY(!is_visible());
  344. m_window_mode = mode;
  345. }
  346. void Window::make_window_manager(unsigned event_mask)
  347. {
  348. GUI::ConnectionToWindowManagerServer::the().async_set_event_mask(event_mask);
  349. GUI::ConnectionToWindowManagerServer::the().async_set_manager_window(m_window_id);
  350. }
  351. void Window::set_cursor(Gfx::StandardCursor cursor)
  352. {
  353. if (m_cursor == cursor)
  354. return;
  355. m_cursor = cursor;
  356. update_cursor();
  357. }
  358. void Window::set_cursor(NonnullRefPtr<Gfx::Bitmap const> cursor)
  359. {
  360. if (m_cursor == cursor)
  361. return;
  362. m_cursor = cursor;
  363. update_cursor();
  364. }
  365. void Window::handle_drop_event(DropEvent& event)
  366. {
  367. if (!m_main_widget)
  368. return;
  369. auto result = m_main_widget->hit_test(event.position());
  370. auto local_event = make<DropEvent>(result.local_position, event.text(), event.mime_data());
  371. VERIFY(result.widget);
  372. result.widget->dispatch_event(*local_event, this);
  373. Application::the()->set_drag_hovered_widget({}, nullptr);
  374. }
  375. void Window::handle_mouse_event(MouseEvent& event)
  376. {
  377. if (!m_main_widget)
  378. return;
  379. auto result = m_main_widget->hit_test(event.position());
  380. VERIFY(result.widget);
  381. if (m_automatic_cursor_tracking_widget) {
  382. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  383. Gfx::IntPoint local_point { event.x() - window_relative_rect.x(), event.y() - window_relative_rect.y() };
  384. auto local_event = MouseEvent((Event::Type)event.type(), local_point, event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y());
  385. m_automatic_cursor_tracking_widget->dispatch_event(local_event, this);
  386. if (event.buttons() == 0) {
  387. m_automatic_cursor_tracking_widget = nullptr;
  388. } else {
  389. auto is_hovered = m_automatic_cursor_tracking_widget.ptr() == result.widget.ptr();
  390. set_hovered_widget(is_hovered ? m_automatic_cursor_tracking_widget.ptr() : nullptr);
  391. }
  392. return;
  393. }
  394. set_hovered_widget(result.widget);
  395. if (event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  396. m_automatic_cursor_tracking_widget = *result.widget;
  397. auto local_event = MouseEvent((Event::Type)event.type(), result.local_position, event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y(), event.wheel_raw_delta_x(), event.wheel_raw_delta_y());
  398. result.widget->dispatch_event(local_event, this);
  399. }
  400. Gfx::IntSize Window::backing_store_size(Gfx::IntSize window_size) const
  401. {
  402. if (!m_resizing)
  403. return window_size;
  404. int const backing_margin_during_resize = 64;
  405. return { window_size.width() + backing_margin_during_resize, window_size.height() + backing_margin_during_resize };
  406. }
  407. void Window::handle_multi_paint_event(MultiPaintEvent& event)
  408. {
  409. if (!is_visible())
  410. return;
  411. if (!m_main_widget)
  412. return;
  413. auto rects = event.rects();
  414. if (!m_pending_paint_event_rects.is_empty()) {
  415. // It's possible that there had been some calls to update() that
  416. // haven't been flushed. We can handle these right now, avoiding
  417. // another round trip.
  418. rects.extend(move(m_pending_paint_event_rects));
  419. }
  420. VERIFY(!rects.is_empty());
  421. // Throw away our backing store if its size is different, and we've stopped resizing or double buffering is disabled.
  422. // This ensures that we shrink the backing store after a resize, and that we do not get flickering artifacts when
  423. // directly painting into a shared active backing store.
  424. if (m_back_store && (!m_resizing || !m_double_buffering_enabled) && m_back_store->size() != event.window_size())
  425. m_back_store = nullptr;
  426. // Discard our backing store if it's unable to contain the new window size. Smaller is fine though, that prevents
  427. // lots of backing store allocations during a resize.
  428. if (m_back_store && !m_back_store->size().contains(event.window_size()))
  429. m_back_store = nullptr;
  430. bool created_new_backing_store = false;
  431. if (!m_back_store) {
  432. m_back_store = create_backing_store(backing_store_size(event.window_size())).release_value_but_fixme_should_propagate_errors();
  433. created_new_backing_store = true;
  434. } else if (m_double_buffering_enabled) {
  435. bool was_purged = false;
  436. bool bitmap_has_memory = m_back_store->set_nonvolatile(was_purged);
  437. if (!bitmap_has_memory) {
  438. // We didn't have enough memory to make the bitmap non-volatile!
  439. // Fall back to single-buffered mode for this window.
  440. // FIXME: Once we have a way to listen for system memory pressure notifications,
  441. // it would be cool to transition back into double-buffered mode once
  442. // the coast is clear.
  443. dbgln("Not enough memory to make backing store non-volatile. Falling back to single-buffered mode.");
  444. m_double_buffering_enabled = false;
  445. m_back_store = move(m_front_store);
  446. created_new_backing_store = true;
  447. } else if (was_purged) {
  448. // The backing store bitmap was cleared, but it does have memory.
  449. // Act as if it's a new backing store so the entire window gets repainted.
  450. created_new_backing_store = true;
  451. }
  452. }
  453. if (created_new_backing_store) {
  454. rects.clear();
  455. rects.append({ {}, event.window_size() });
  456. }
  457. for (auto& rect : rects) {
  458. PaintEvent paint_event(rect);
  459. m_main_widget->dispatch_event(paint_event, this);
  460. }
  461. m_back_store->set_visible_size(event.window_size());
  462. if (m_double_buffering_enabled)
  463. flip(rects);
  464. else if (created_new_backing_store)
  465. set_current_backing_store(*m_back_store, true);
  466. if (is_visible())
  467. ConnectionToWindowServer::the().async_did_finish_painting(m_window_id, rects);
  468. }
  469. void Window::propagate_shortcuts(KeyEvent& event, Widget* widget, ShortcutPropagationBoundary boundary)
  470. {
  471. VERIFY(event.type() == Event::KeyDown);
  472. auto shortcut = Shortcut(event.modifiers(), event.key());
  473. Action* action = nullptr;
  474. if (widget) {
  475. VERIFY(widget->window() == this);
  476. do {
  477. action = widget->action_for_shortcut(shortcut);
  478. if (action)
  479. break;
  480. widget = widget->parent_widget();
  481. } while (widget);
  482. }
  483. if (!action && boundary >= ShortcutPropagationBoundary::Window)
  484. action = action_for_shortcut(shortcut);
  485. if (!action && boundary >= ShortcutPropagationBoundary::Application)
  486. action = Application::the()->action_for_shortcut(shortcut);
  487. if (action) {
  488. action->process_event(*this, event);
  489. return;
  490. }
  491. event.ignore();
  492. }
  493. void Window::restore_size_and_position(StringView domain, StringView group, Optional<Gfx::IntSize> fallback_size, Optional<Gfx::IntPoint> fallback_position)
  494. {
  495. int x = Config::read_i32(domain, group, "X"sv, INT_MIN);
  496. int y = Config::read_i32(domain, group, "Y"sv, INT_MIN);
  497. if (x != INT_MIN && y != INT_MIN) {
  498. move_to(x, y);
  499. } else if (fallback_position.has_value()) {
  500. move_to(fallback_position.release_value());
  501. }
  502. int width = Config::read_i32(domain, group, "Width"sv, INT_MIN);
  503. int height = Config::read_i32(domain, group, "Height"sv, INT_MIN);
  504. if (width != INT_MIN && height != INT_MIN) {
  505. resize(width, height);
  506. } else if (fallback_size.has_value()) {
  507. resize(fallback_size.release_value());
  508. }
  509. set_maximized(Config::read_bool(domain, group, "Maximized"sv, false));
  510. }
  511. void Window::save_size_and_position(StringView domain, StringView group) const
  512. {
  513. auto rect_to_save = floating_rect();
  514. Config::write_i32(domain, group, "X"sv, rect_to_save.x());
  515. Config::write_i32(domain, group, "Y"sv, rect_to_save.y());
  516. Config::write_i32(domain, group, "Width"sv, rect_to_save.width());
  517. Config::write_i32(domain, group, "Height"sv, rect_to_save.height());
  518. Config::write_bool(domain, group, "Maximized"sv, is_maximized());
  519. }
  520. void Window::save_size_and_position_on_close(StringView domain, StringView group)
  521. {
  522. m_save_size_and_position_on_close = true;
  523. m_save_domain = domain;
  524. m_save_group = group;
  525. }
  526. void Window::handle_key_event(KeyEvent& event)
  527. {
  528. if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
  529. focus_a_widget_if_possible(FocusSource::Keyboard);
  530. }
  531. if (m_default_return_key_widget && event.key() == Key_Return)
  532. if (!m_focused_widget || !is<Button>(m_focused_widget.ptr()))
  533. return default_return_key_widget()->dispatch_event(event, this);
  534. if (m_focused_widget)
  535. m_focused_widget->dispatch_event(event, this);
  536. else if (m_main_widget)
  537. m_main_widget->dispatch_event(event, this);
  538. if (event.is_accepted())
  539. return;
  540. // Only process shortcuts if this is a keydown event.
  541. if (event.type() == Event::KeyDown) {
  542. auto const boundary = (is_blocking() || is_popup()) ? ShortcutPropagationBoundary::Window : ShortcutPropagationBoundary::Application;
  543. propagate_shortcuts(event, nullptr, boundary);
  544. }
  545. }
  546. void Window::handle_resize_event(ResizeEvent& event)
  547. {
  548. auto new_size = event.size();
  549. // When the user is done resizing, we receive a last resize event with our actual size.
  550. m_resizing = new_size != m_rect_when_windowless.size();
  551. if (!m_pending_paint_event_rects.is_empty()) {
  552. m_pending_paint_event_rects.clear_with_capacity();
  553. m_pending_paint_event_rects.append({ {}, new_size });
  554. }
  555. m_rect_when_windowless.set_size(new_size);
  556. if (m_main_widget)
  557. m_main_widget->set_relative_rect({ {}, new_size });
  558. }
  559. void Window::handle_input_preemption_event(Core::Event& event)
  560. {
  561. if (on_input_preemption_change)
  562. on_input_preemption_change(event.type() == Event::WindowInputPreempted);
  563. if (!m_focused_widget)
  564. return;
  565. m_focused_widget->set_focus_preempted(event.type() == Event::WindowInputPreempted);
  566. m_focused_widget->update();
  567. }
  568. void Window::handle_became_active_or_inactive_event(Core::Event& event)
  569. {
  570. if (event.type() == Event::WindowBecameActive)
  571. Application::the()->window_did_become_active({}, *this);
  572. else
  573. Application::the()->window_did_become_inactive({}, *this);
  574. if (on_active_window_change)
  575. on_active_window_change(event.type() == Event::WindowBecameActive);
  576. if (m_main_widget)
  577. m_main_widget->dispatch_event(event, this);
  578. if (m_focused_widget) {
  579. if (event.type() == Event::WindowBecameActive)
  580. m_focused_widget->set_focus_preempted(false);
  581. m_focused_widget->update();
  582. }
  583. }
  584. void Window::handle_close_request()
  585. {
  586. if (on_close_request) {
  587. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  588. return;
  589. }
  590. close();
  591. }
  592. void Window::handle_theme_change_event(ThemeChangeEvent& event)
  593. {
  594. if (!m_main_widget)
  595. return;
  596. auto dispatch_theme_change = [&](auto& widget, auto recursive) {
  597. widget.dispatch_event(event, this);
  598. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  599. widget.dispatch_event(event, this);
  600. recursive(widget, recursive);
  601. return IterationDecision::Continue;
  602. });
  603. };
  604. dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
  605. }
  606. void Window::handle_fonts_change_event(FontsChangeEvent& event)
  607. {
  608. if (!m_main_widget)
  609. return;
  610. auto dispatch_fonts_change = [&](auto& widget, auto recursive) {
  611. widget.dispatch_event(event, this);
  612. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  613. widget.dispatch_event(event, this);
  614. recursive(widget, recursive);
  615. return IterationDecision::Continue;
  616. });
  617. };
  618. dispatch_fonts_change(*m_main_widget.ptr(), dispatch_fonts_change);
  619. if (is_auto_shrinking())
  620. schedule_relayout();
  621. if (on_font_change)
  622. on_font_change();
  623. }
  624. void Window::handle_screen_rects_change_event(ScreenRectsChangeEvent& event)
  625. {
  626. if (!m_main_widget)
  627. return;
  628. auto dispatch_screen_rects_change = [&](auto& widget, auto recursive) {
  629. widget.dispatch_event(event, this);
  630. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  631. widget.dispatch_event(event, this);
  632. recursive(widget, recursive);
  633. return IterationDecision::Continue;
  634. });
  635. };
  636. dispatch_screen_rects_change(*m_main_widget.ptr(), dispatch_screen_rects_change);
  637. screen_rects_change_event(event);
  638. }
  639. void Window::handle_applet_area_rect_change_event(AppletAreaRectChangeEvent& event)
  640. {
  641. if (!m_main_widget)
  642. return;
  643. auto dispatch_applet_area_rect_change = [&](auto& widget, auto recursive) {
  644. widget.dispatch_event(event, this);
  645. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  646. widget.dispatch_event(event, this);
  647. recursive(widget, recursive);
  648. return IterationDecision::Continue;
  649. });
  650. };
  651. dispatch_applet_area_rect_change(*m_main_widget.ptr(), dispatch_applet_area_rect_change);
  652. applet_area_rect_change_event(event);
  653. }
  654. void Window::handle_drag_move_event(DragEvent& event)
  655. {
  656. if (!m_main_widget)
  657. return;
  658. auto result = m_main_widget->hit_test(event.position());
  659. VERIFY(result.widget);
  660. Application::the()->set_drag_hovered_widget({}, result.widget, result.local_position, event.mime_types());
  661. // NOTE: Setting the drag hovered widget may have executed arbitrary code, so re-check that the widget is still there.
  662. if (!result.widget)
  663. return;
  664. if (result.widget->has_pending_drop()) {
  665. DragEvent drag_move_event(static_cast<Event::Type>(event.type()), result.local_position, event.mime_types());
  666. result.widget->dispatch_event(drag_move_event, this);
  667. }
  668. }
  669. void Window::enter_event(Core::Event&)
  670. {
  671. }
  672. void Window::leave_event(Core::Event&)
  673. {
  674. }
  675. void Window::handle_entered_event(Core::Event& event)
  676. {
  677. enter_event(event);
  678. }
  679. void Window::handle_left_event(Core::Event& event)
  680. {
  681. set_hovered_widget(nullptr);
  682. Application::the()->set_drag_hovered_widget({}, nullptr);
  683. leave_event(event);
  684. }
  685. void Window::event(Core::Event& event)
  686. {
  687. ScopeGuard guard([&] {
  688. // Accept the event so it doesn't bubble up to parent windows!
  689. event.accept();
  690. });
  691. if (event.type() == Event::Drop)
  692. return handle_drop_event(static_cast<DropEvent&>(event));
  693. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel)
  694. return handle_mouse_event(static_cast<MouseEvent&>(event));
  695. if (event.type() == Event::MultiPaint)
  696. return handle_multi_paint_event(static_cast<MultiPaintEvent&>(event));
  697. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown)
  698. return handle_key_event(static_cast<KeyEvent&>(event));
  699. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
  700. return handle_became_active_or_inactive_event(event);
  701. if (event.type() == Event::WindowInputPreempted || event.type() == Event::WindowInputRestored)
  702. return handle_input_preemption_event(event);
  703. if (event.type() == Event::WindowCloseRequest)
  704. return handle_close_request();
  705. if (event.type() == Event::WindowEntered)
  706. return handle_entered_event(event);
  707. if (event.type() == Event::WindowLeft)
  708. return handle_left_event(event);
  709. if (event.type() == Event::Resize)
  710. return handle_resize_event(static_cast<ResizeEvent&>(event));
  711. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  712. return wm_event(static_cast<WMEvent&>(event));
  713. if (event.type() == Event::DragMove)
  714. return handle_drag_move_event(static_cast<DragEvent&>(event));
  715. if (event.type() == Event::ThemeChange)
  716. return handle_theme_change_event(static_cast<ThemeChangeEvent&>(event));
  717. if (event.type() == Event::FontsChange)
  718. return handle_fonts_change_event(static_cast<FontsChangeEvent&>(event));
  719. if (event.type() == Event::ScreenRectsChange)
  720. return handle_screen_rects_change_event(static_cast<ScreenRectsChangeEvent&>(event));
  721. if (event.type() == Event::AppletAreaRectChange)
  722. return handle_applet_area_rect_change_event(static_cast<AppletAreaRectChangeEvent&>(event));
  723. Core::EventReceiver::event(event);
  724. }
  725. bool Window::is_visible() const
  726. {
  727. return m_visible;
  728. }
  729. void Window::update()
  730. {
  731. auto rect = this->rect();
  732. update({ 0, 0, rect.width(), rect.height() });
  733. }
  734. void Window::force_update()
  735. {
  736. if (!is_visible())
  737. return;
  738. auto rect = this->rect();
  739. ConnectionToWindowServer::the().async_invalidate_rect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true);
  740. }
  741. void Window::update(Gfx::IntRect const& a_rect)
  742. {
  743. if (!is_visible())
  744. return;
  745. for (auto& pending_rect : m_pending_paint_event_rects) {
  746. if (pending_rect.contains(a_rect)) {
  747. dbgln_if(UPDATE_COALESCING_DEBUG, "Ignoring {} since it's contained by pending rect {}", a_rect, pending_rect);
  748. return;
  749. }
  750. }
  751. if (m_pending_paint_event_rects.is_empty()) {
  752. deferred_invoke([this] {
  753. auto rects = move(m_pending_paint_event_rects);
  754. if (rects.is_empty())
  755. return;
  756. ConnectionToWindowServer::the().async_invalidate_rect(m_window_id, rects, false);
  757. });
  758. }
  759. m_pending_paint_event_rects.append(a_rect);
  760. }
  761. void Window::set_main_widget(Widget* widget)
  762. {
  763. if (m_main_widget == widget)
  764. return;
  765. if (m_main_widget) {
  766. m_main_widget->set_window(nullptr);
  767. remove_child(*m_main_widget);
  768. }
  769. m_main_widget = widget;
  770. if (m_main_widget) {
  771. add_child(*widget);
  772. auto new_window_rect = rect();
  773. auto new_widget_min_size = m_main_widget->effective_min_size();
  774. new_window_rect.set_width(max(new_window_rect.width(), MUST(new_widget_min_size.width().shrink_value())));
  775. new_window_rect.set_height(max(new_window_rect.height(), MUST(new_widget_min_size.height().shrink_value())));
  776. set_rect(new_window_rect);
  777. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  778. m_main_widget->set_window(this);
  779. if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
  780. m_main_widget->set_focus(true);
  781. }
  782. update();
  783. }
  784. void Window::set_default_return_key_widget(Widget* widget)
  785. {
  786. if (m_default_return_key_widget == widget)
  787. return;
  788. m_default_return_key_widget = widget;
  789. }
  790. void Window::set_focused_widget(Widget* widget, FocusSource source)
  791. {
  792. if (m_focused_widget == widget)
  793. return;
  794. WeakPtr<Widget> previously_focused_widget = m_focused_widget;
  795. m_focused_widget = widget;
  796. if (!m_focused_widget && m_previously_focused_widget)
  797. m_focused_widget = m_previously_focused_widget;
  798. if (m_default_return_key_widget && m_default_return_key_widget->on_focus_change)
  799. m_default_return_key_widget->on_focus_change(m_default_return_key_widget->is_focused(), source);
  800. if (previously_focused_widget) {
  801. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  802. previously_focused_widget->update();
  803. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  804. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  805. m_previously_focused_widget = previously_focused_widget;
  806. }
  807. if (m_focused_widget) {
  808. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  809. m_focused_widget->update();
  810. if (m_focused_widget && m_focused_widget->on_focus_change)
  811. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  812. }
  813. }
  814. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  815. {
  816. if (widget == m_automatic_cursor_tracking_widget)
  817. return;
  818. m_automatic_cursor_tracking_widget = widget;
  819. }
  820. void Window::set_has_alpha_channel(bool value)
  821. {
  822. if (m_has_alpha_channel == value)
  823. return;
  824. m_has_alpha_channel = value;
  825. if (!is_visible())
  826. return;
  827. m_pending_paint_event_rects.clear();
  828. m_back_store = nullptr;
  829. m_front_store = nullptr;
  830. ConnectionToWindowServer::the().async_set_window_has_alpha_channel(m_window_id, value);
  831. update();
  832. }
  833. void Window::set_double_buffering_enabled(bool value)
  834. {
  835. VERIFY(!is_visible());
  836. m_double_buffering_enabled = value;
  837. }
  838. void Window::set_alpha_hit_threshold(float threshold)
  839. {
  840. if (threshold < 0.0f)
  841. threshold = 0.0f;
  842. else if (threshold > 1.0f)
  843. threshold = 1.0f;
  844. if (m_alpha_hit_threshold == threshold)
  845. return;
  846. m_alpha_hit_threshold = threshold;
  847. if (!is_visible())
  848. return;
  849. ConnectionToWindowServer::the().async_set_window_alpha_hit_threshold(m_window_id, threshold);
  850. }
  851. void Window::set_hovered_widget(Widget* widget)
  852. {
  853. if (widget == m_hovered_widget)
  854. return;
  855. if (m_hovered_widget)
  856. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  857. m_hovered_widget = widget;
  858. if (m_hovered_widget)
  859. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  860. auto* app = Application::the();
  861. if (app && app->hover_debugging_enabled())
  862. update();
  863. }
  864. void Window::set_current_backing_store(WindowBackingStore& backing_store, bool flush_immediately) const
  865. {
  866. auto& bitmap = backing_store.bitmap();
  867. ConnectionToWindowServer::the().set_window_backing_store(
  868. m_window_id,
  869. 32,
  870. bitmap.pitch(),
  871. MUST(IPC::File::clone_fd(bitmap.anonymous_buffer().fd())),
  872. backing_store.serial(),
  873. bitmap.has_alpha_channel(),
  874. bitmap.size(),
  875. backing_store.visible_size(),
  876. flush_immediately);
  877. }
  878. void Window::flip(Vector<Gfx::IntRect, 32> const& dirty_rects)
  879. {
  880. swap(m_front_store, m_back_store);
  881. set_current_backing_store(*m_front_store);
  882. if (!m_back_store || m_back_store->size() != m_front_store->size()) {
  883. m_back_store = create_backing_store(m_front_store->size()).release_value_but_fixme_should_propagate_errors();
  884. memcpy(m_back_store->bitmap().scanline(0), m_front_store->bitmap().scanline(0), m_front_store->bitmap().size_in_bytes());
  885. m_back_store->set_volatile();
  886. return;
  887. }
  888. // Copy whatever was painted from the front to the back.
  889. Painter painter(m_back_store->bitmap());
  890. for (auto& dirty_rect : dirty_rects)
  891. painter.blit(dirty_rect.location(), m_front_store->bitmap(), dirty_rect, 1.0f, false);
  892. m_back_store->set_volatile();
  893. }
  894. ErrorOr<NonnullOwnPtr<WindowBackingStore>> Window::create_backing_store(Gfx::IntSize size)
  895. {
  896. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  897. VERIFY(!size.is_empty());
  898. size_t pitch = Gfx::Bitmap::minimum_pitch(size.width(), format);
  899. size_t size_in_bytes = size.height() * pitch;
  900. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes, PAGE_SIZE)));
  901. // FIXME: Plumb scale factor here eventually.
  902. auto bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(format, buffer, size, 1));
  903. return make<WindowBackingStore>(bitmap);
  904. }
  905. void Window::wm_event(WMEvent&)
  906. {
  907. }
  908. void Window::screen_rects_change_event(ScreenRectsChangeEvent&)
  909. {
  910. }
  911. void Window::applet_area_rect_change_event(AppletAreaRectChangeEvent&)
  912. {
  913. }
  914. void Window::set_icon(Gfx::Bitmap const* icon)
  915. {
  916. if (m_icon == icon)
  917. return;
  918. Gfx::IntSize icon_size = icon ? icon->size() : Gfx::IntSize(16, 16);
  919. auto new_icon = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, icon_size).release_value_but_fixme_should_propagate_errors();
  920. if (icon) {
  921. Painter painter(*new_icon);
  922. painter.blit({ 0, 0 }, *icon, icon->rect());
  923. }
  924. m_icon = move(new_icon);
  925. apply_icon();
  926. }
  927. void Window::apply_icon()
  928. {
  929. if (!m_icon)
  930. return;
  931. if (!is_visible())
  932. return;
  933. ConnectionToWindowServer::the().async_set_window_icon_bitmap(m_window_id, m_icon->to_shareable_bitmap());
  934. }
  935. void Window::start_interactive_resize(ResizeDirection resize_direction)
  936. {
  937. ConnectionToWindowServer::the().async_start_window_resize(m_window_id, (i32)resize_direction);
  938. }
  939. Vector<Widget&> Window::focusable_widgets(FocusSource source) const
  940. {
  941. if (!m_main_widget)
  942. return {};
  943. HashTable<Widget*> seen_widgets;
  944. Vector<Widget&> collected_widgets;
  945. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  946. bool widget_accepts_focus = false;
  947. switch (source) {
  948. case FocusSource::Keyboard:
  949. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::TabFocus);
  950. break;
  951. case FocusSource::Mouse:
  952. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::ClickFocus);
  953. break;
  954. case FocusSource::Programmatic:
  955. widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
  956. break;
  957. }
  958. if (widget_accepts_focus) {
  959. auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
  960. if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
  961. collected_widgets.append(effective_focus_widget);
  962. }
  963. widget.for_each_child_widget([&](auto& child) {
  964. if (!child.is_visible())
  965. return IterationDecision::Continue;
  966. if (!child.is_enabled())
  967. return IterationDecision::Continue;
  968. if (!child.is_auto_focusable())
  969. return IterationDecision::Continue;
  970. collect_focusable_widgets(child);
  971. return IterationDecision::Continue;
  972. });
  973. };
  974. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  975. return collected_widgets;
  976. }
  977. void Window::set_fullscreen(bool fullscreen)
  978. {
  979. if (m_fullscreen == fullscreen)
  980. return;
  981. m_fullscreen = fullscreen;
  982. if (!is_visible())
  983. return;
  984. ConnectionToWindowServer::the().async_set_fullscreen(m_window_id, fullscreen);
  985. }
  986. void Window::set_frameless(bool frameless)
  987. {
  988. if (m_frameless == frameless)
  989. return;
  990. m_frameless = frameless;
  991. if (!is_visible())
  992. return;
  993. ConnectionToWindowServer::the().async_set_frameless(m_window_id, frameless);
  994. if (!frameless)
  995. apply_icon();
  996. }
  997. void Window::set_forced_shadow(bool shadow)
  998. {
  999. if (m_forced_shadow == shadow)
  1000. return;
  1001. m_forced_shadow = shadow;
  1002. if (!is_visible())
  1003. return;
  1004. ConnectionToWindowServer::the().async_set_forced_shadow(m_window_id, shadow);
  1005. }
  1006. void Window::set_obey_widget_min_size(bool obey_widget_min_size)
  1007. {
  1008. if (m_obey_widget_min_size != obey_widget_min_size) {
  1009. m_obey_widget_min_size = obey_widget_min_size;
  1010. schedule_relayout();
  1011. }
  1012. }
  1013. void Window::set_auto_shrink(bool shrink)
  1014. {
  1015. if (m_auto_shrink == shrink)
  1016. return;
  1017. m_auto_shrink = shrink;
  1018. schedule_relayout();
  1019. }
  1020. void Window::set_maximized(bool maximized)
  1021. {
  1022. m_maximized = maximized;
  1023. if (!is_visible())
  1024. return;
  1025. ConnectionToWindowServer::the().async_set_maximized(m_window_id, maximized);
  1026. }
  1027. void Window::set_minimized(bool minimized)
  1028. {
  1029. if (!is_minimizable())
  1030. return;
  1031. m_minimized = minimized;
  1032. if (!is_visible())
  1033. return;
  1034. ConnectionToWindowServer::the().async_set_minimized(m_window_id, minimized);
  1035. }
  1036. void Window::update_min_size()
  1037. {
  1038. if (!main_widget())
  1039. return;
  1040. main_widget()->do_layout();
  1041. auto min_size = main_widget()->effective_min_size();
  1042. Gfx::IntSize size = { MUST(min_size.width().shrink_value()), MUST(min_size.height().shrink_value()) };
  1043. if (is_obeying_widget_min_size()) {
  1044. m_minimum_size_when_windowless = size;
  1045. if (is_visible())
  1046. ConnectionToWindowServer::the().async_set_window_minimum_size(m_window_id, size);
  1047. }
  1048. if (is_auto_shrinking())
  1049. resize(size);
  1050. }
  1051. void Window::schedule_relayout()
  1052. {
  1053. if (m_layout_pending || !is_visible())
  1054. return;
  1055. m_layout_pending = true;
  1056. deferred_invoke([this] {
  1057. update_min_size();
  1058. update();
  1059. m_layout_pending = false;
  1060. });
  1061. }
  1062. void Window::refresh_system_theme()
  1063. {
  1064. ConnectionToWindowServer::the().async_refresh_system_theme();
  1065. }
  1066. void Window::for_each_window(Badge<ConnectionToWindowServer>, Function<void(Window&)> callback)
  1067. {
  1068. for (auto& e : *reified_windows) {
  1069. VERIFY(e.value);
  1070. callback(*e.value);
  1071. }
  1072. }
  1073. void Window::update_all_windows(Badge<ConnectionToWindowServer>)
  1074. {
  1075. for (auto& e : *reified_windows) {
  1076. e.value->force_update();
  1077. }
  1078. }
  1079. void Window::notify_state_changed(Badge<ConnectionToWindowServer>, bool minimized, bool maximized, bool occluded)
  1080. {
  1081. m_visible_for_timer_purposes = !minimized && !occluded;
  1082. m_maximized = maximized;
  1083. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  1084. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  1085. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  1086. if (!store)
  1087. return;
  1088. if (minimized || occluded) {
  1089. store->set_volatile();
  1090. } else {
  1091. bool was_purged = false;
  1092. bool bitmap_has_memory = store->set_nonvolatile(was_purged);
  1093. if (!bitmap_has_memory) {
  1094. // Not enough memory to make the bitmap non-volatile. Lose the bitmap and schedule an update.
  1095. // Let the paint system figure out what to do.
  1096. store = nullptr;
  1097. update();
  1098. } else if (was_purged) {
  1099. // The bitmap memory was purged by the kernel, but we have all-new zero-filled pages.
  1100. // Schedule an update to regenerate the bitmap.
  1101. update();
  1102. }
  1103. }
  1104. }
  1105. Action* Window::action_for_shortcut(Shortcut const& shortcut)
  1106. {
  1107. return Action::find_action_for_shortcut(*this, shortcut);
  1108. }
  1109. void Window::set_base_size(Gfx::IntSize base_size)
  1110. {
  1111. if (m_base_size == base_size)
  1112. return;
  1113. m_base_size = base_size;
  1114. if (is_visible())
  1115. ConnectionToWindowServer::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  1116. }
  1117. void Window::set_size_increment(Gfx::IntSize size_increment)
  1118. {
  1119. if (m_size_increment == size_increment)
  1120. return;
  1121. m_size_increment = size_increment;
  1122. if (is_visible())
  1123. ConnectionToWindowServer::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  1124. }
  1125. void Window::set_resize_aspect_ratio(Optional<Gfx::IntSize> const& ratio)
  1126. {
  1127. if (m_resize_aspect_ratio == ratio)
  1128. return;
  1129. m_resize_aspect_ratio = ratio;
  1130. if (is_visible())
  1131. ConnectionToWindowServer::the().async_set_window_resize_aspect_ratio(m_window_id, m_resize_aspect_ratio);
  1132. }
  1133. void Window::did_add_widget(Badge<Widget>, Widget&)
  1134. {
  1135. if (!m_focused_widget)
  1136. focus_a_widget_if_possible(FocusSource::Mouse);
  1137. }
  1138. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  1139. {
  1140. if (m_focused_widget == &widget)
  1141. m_focused_widget = nullptr;
  1142. if (m_hovered_widget == &widget)
  1143. m_hovered_widget = nullptr;
  1144. if (m_automatic_cursor_tracking_widget == &widget)
  1145. m_automatic_cursor_tracking_widget = nullptr;
  1146. }
  1147. void Window::set_progress(Optional<int> progress)
  1148. {
  1149. VERIFY(m_window_id);
  1150. ConnectionToWindowServer::the().async_set_window_progress(m_window_id, progress);
  1151. }
  1152. void Window::update_cursor()
  1153. {
  1154. auto new_cursor = m_cursor;
  1155. auto is_usable_cursor = [](auto& cursor) {
  1156. return cursor.template has<NonnullRefPtr<Gfx::Bitmap const>>() || cursor.template get<Gfx::StandardCursor>() != Gfx::StandardCursor::None;
  1157. };
  1158. // NOTE: If there's an automatic cursor tracking widget, we retain its cursor until tracking stops.
  1159. if (auto widget = m_automatic_cursor_tracking_widget) {
  1160. if (is_usable_cursor(widget->override_cursor()))
  1161. new_cursor = widget->override_cursor();
  1162. } else if (auto widget = m_hovered_widget) {
  1163. if (is_usable_cursor(widget->override_cursor()))
  1164. new_cursor = widget->override_cursor();
  1165. }
  1166. if (m_effective_cursor == new_cursor)
  1167. return;
  1168. m_effective_cursor = new_cursor;
  1169. if (new_cursor.has<NonnullRefPtr<Gfx::Bitmap const>>())
  1170. ConnectionToWindowServer::the().async_set_window_custom_cursor(m_window_id, new_cursor.get<NonnullRefPtr<Gfx::Bitmap const>>()->to_shareable_bitmap());
  1171. else
  1172. ConnectionToWindowServer::the().async_set_window_cursor(m_window_id, (u32)new_cursor.get<Gfx::StandardCursor>());
  1173. }
  1174. void Window::focus_a_widget_if_possible(FocusSource source)
  1175. {
  1176. auto focusable_widgets = this->focusable_widgets(source);
  1177. if (!focusable_widgets.is_empty())
  1178. set_focused_widget(&focusable_widgets[0], source);
  1179. }
  1180. void Window::did_disable_focused_widget(Badge<Widget>)
  1181. {
  1182. focus_a_widget_if_possible(FocusSource::Mouse);
  1183. }
  1184. bool Window::is_active() const
  1185. {
  1186. VERIFY(Application::the());
  1187. return this == Application::the()->active_window();
  1188. }
  1189. Gfx::Bitmap* Window::back_bitmap()
  1190. {
  1191. return m_back_store ? &m_back_store->bitmap() : nullptr;
  1192. }
  1193. void Window::add_menu(NonnullRefPtr<Menu> menu)
  1194. {
  1195. m_menubar->add_menu({}, move(menu));
  1196. if (m_window_id) {
  1197. menu->realize_menu_if_needed();
  1198. ConnectionToWindowServer::the().async_add_menu(m_window_id, menu->menu_id());
  1199. }
  1200. }
  1201. NonnullRefPtr<Menu> Window::add_menu(String name)
  1202. {
  1203. auto menu = m_menubar->add_menu({}, move(name));
  1204. if (m_window_id) {
  1205. menu->realize_menu_if_needed();
  1206. ConnectionToWindowServer::the().async_add_menu(m_window_id, menu->menu_id());
  1207. }
  1208. return menu;
  1209. }
  1210. void Window::flash_menubar_menu_for(MenuItem const& menu_item)
  1211. {
  1212. if (!Desktop::the().system_effects().flash_menus())
  1213. return;
  1214. auto menu_id = menu_item.menu_id();
  1215. if (menu_id < 0)
  1216. return;
  1217. ConnectionToWindowServer::the().async_flash_menubar_menu(m_window_id, menu_id);
  1218. }
  1219. bool Window::is_modified() const
  1220. {
  1221. if (!m_window_id)
  1222. return false;
  1223. return ConnectionToWindowServer::the().is_window_modified(m_window_id);
  1224. }
  1225. void Window::set_modified(bool modified)
  1226. {
  1227. if (!m_window_id)
  1228. return;
  1229. ConnectionToWindowServer::the().async_set_window_modified(m_window_id, modified);
  1230. }
  1231. void Window::flush_pending_paints_immediately()
  1232. {
  1233. if (!m_window_id)
  1234. return;
  1235. if (m_pending_paint_event_rects.is_empty())
  1236. return;
  1237. MultiPaintEvent paint_event(move(m_pending_paint_event_rects), size());
  1238. handle_multi_paint_event(paint_event);
  1239. }
  1240. void Window::set_always_on_top(bool always_on_top)
  1241. {
  1242. if (!m_window_id)
  1243. return;
  1244. ConnectionToWindowServer::the().set_always_on_top(m_window_id, always_on_top);
  1245. }
  1246. }