Window.cpp 40 KB

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