Window.cpp 43 KB

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