Window.cpp 45 KB

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