Window.cpp 42 KB

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