Window.cpp 42 KB

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