Window.cpp 44 KB

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