Window.cpp 40 KB

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