Window.cpp 43 KB

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