Window.cpp 42 KB

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