Window.cpp 45 KB

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