Window.cpp 40 KB

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