Window.cpp 40 KB

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