Window.cpp 43 KB

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