Window.cpp 43 KB

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