Window.cpp 40 KB

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