Window.cpp 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382
  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. }
  532. void Window::handle_screen_rects_change_event(ScreenRectsChangeEvent& event)
  533. {
  534. if (!m_main_widget)
  535. return;
  536. auto dispatch_screen_rects_change = [&](auto& widget, auto recursive) {
  537. widget.dispatch_event(event, this);
  538. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  539. widget.dispatch_event(event, this);
  540. recursive(widget, recursive);
  541. return IterationDecision::Continue;
  542. });
  543. };
  544. dispatch_screen_rects_change(*m_main_widget.ptr(), dispatch_screen_rects_change);
  545. screen_rects_change_event(event);
  546. }
  547. void Window::handle_applet_area_rect_change_event(AppletAreaRectChangeEvent& event)
  548. {
  549. if (!m_main_widget)
  550. return;
  551. auto dispatch_applet_area_rect_change = [&](auto& widget, auto recursive) {
  552. widget.dispatch_event(event, this);
  553. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  554. widget.dispatch_event(event, this);
  555. recursive(widget, recursive);
  556. return IterationDecision::Continue;
  557. });
  558. };
  559. dispatch_applet_area_rect_change(*m_main_widget.ptr(), dispatch_applet_area_rect_change);
  560. applet_area_rect_change_event(event);
  561. }
  562. void Window::handle_drag_move_event(DragEvent& event)
  563. {
  564. if (!m_main_widget)
  565. return;
  566. auto result = m_main_widget->hit_test(event.position());
  567. VERIFY(result.widget);
  568. Application::the()->set_drag_hovered_widget({}, result.widget, result.local_position, event.mime_types());
  569. // NOTE: Setting the drag hovered widget may have executed arbitrary code, so re-check that the widget is still there.
  570. if (!result.widget)
  571. return;
  572. if (result.widget->has_pending_drop()) {
  573. DragEvent drag_move_event(static_cast<Event::Type>(event.type()), result.local_position, event.mime_types());
  574. result.widget->dispatch_event(drag_move_event, this);
  575. }
  576. }
  577. void Window::enter_event(Core::Event&)
  578. {
  579. }
  580. void Window::leave_event(Core::Event&)
  581. {
  582. }
  583. void Window::handle_entered_event(Core::Event& event)
  584. {
  585. enter_event(event);
  586. }
  587. void Window::handle_left_event(Core::Event& event)
  588. {
  589. set_hovered_widget(nullptr);
  590. Application::the()->set_drag_hovered_widget({}, nullptr);
  591. leave_event(event);
  592. }
  593. void Window::event(Core::Event& event)
  594. {
  595. ScopeGuard guard([&] {
  596. // Accept the event so it doesn't bubble up to parent windows!
  597. event.accept();
  598. });
  599. if (event.type() == Event::Drop)
  600. return handle_drop_event(static_cast<DropEvent&>(event));
  601. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel)
  602. return handle_mouse_event(static_cast<MouseEvent&>(event));
  603. if (event.type() == Event::MultiPaint)
  604. return handle_multi_paint_event(static_cast<MultiPaintEvent&>(event));
  605. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown)
  606. return handle_key_event(static_cast<KeyEvent&>(event));
  607. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
  608. return handle_became_active_or_inactive_event(event);
  609. if (event.type() == Event::WindowInputPreempted || event.type() == Event::WindowInputRestored)
  610. return handle_input_preemption_event(event);
  611. if (event.type() == Event::WindowCloseRequest)
  612. return handle_close_request();
  613. if (event.type() == Event::WindowEntered)
  614. return handle_entered_event(event);
  615. if (event.type() == Event::WindowLeft)
  616. return handle_left_event(event);
  617. if (event.type() == Event::Resize)
  618. return handle_resize_event(static_cast<ResizeEvent&>(event));
  619. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  620. return wm_event(static_cast<WMEvent&>(event));
  621. if (event.type() == Event::DragMove)
  622. return handle_drag_move_event(static_cast<DragEvent&>(event));
  623. if (event.type() == Event::ThemeChange)
  624. return handle_theme_change_event(static_cast<ThemeChangeEvent&>(event));
  625. if (event.type() == Event::FontsChange)
  626. return handle_fonts_change_event(static_cast<FontsChangeEvent&>(event));
  627. if (event.type() == Event::ScreenRectsChange)
  628. return handle_screen_rects_change_event(static_cast<ScreenRectsChangeEvent&>(event));
  629. if (event.type() == Event::AppletAreaRectChange)
  630. return handle_applet_area_rect_change_event(static_cast<AppletAreaRectChangeEvent&>(event));
  631. Core::Object::event(event);
  632. }
  633. bool Window::is_visible() const
  634. {
  635. return m_visible;
  636. }
  637. void Window::update()
  638. {
  639. auto rect = this->rect();
  640. update({ 0, 0, rect.width(), rect.height() });
  641. }
  642. void Window::force_update()
  643. {
  644. if (!is_visible())
  645. return;
  646. auto rect = this->rect();
  647. ConnectionToWindowServer::the().async_invalidate_rect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true);
  648. }
  649. void Window::update(Gfx::IntRect const& a_rect)
  650. {
  651. if (!is_visible())
  652. return;
  653. for (auto& pending_rect : m_pending_paint_event_rects) {
  654. if (pending_rect.contains(a_rect)) {
  655. dbgln_if(UPDATE_COALESCING_DEBUG, "Ignoring {} since it's contained by pending rect {}", a_rect, pending_rect);
  656. return;
  657. }
  658. }
  659. if (m_pending_paint_event_rects.is_empty()) {
  660. deferred_invoke([this] {
  661. auto rects = move(m_pending_paint_event_rects);
  662. if (rects.is_empty())
  663. return;
  664. ConnectionToWindowServer::the().async_invalidate_rect(m_window_id, rects, false);
  665. });
  666. }
  667. m_pending_paint_event_rects.append(a_rect);
  668. }
  669. void Window::set_main_widget(Widget* widget)
  670. {
  671. if (m_main_widget == widget)
  672. return;
  673. if (m_main_widget) {
  674. m_main_widget->set_window(nullptr);
  675. remove_child(*m_main_widget);
  676. }
  677. m_main_widget = widget;
  678. if (m_main_widget) {
  679. add_child(*widget);
  680. auto new_window_rect = rect();
  681. auto new_widget_min_size = m_main_widget->effective_min_size();
  682. new_window_rect.set_width(max(new_window_rect.width(), MUST(new_widget_min_size.width().shrink_value())));
  683. new_window_rect.set_height(max(new_window_rect.height(), MUST(new_widget_min_size.height().shrink_value())));
  684. set_rect(new_window_rect);
  685. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  686. m_main_widget->set_window(this);
  687. if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
  688. m_main_widget->set_focus(true);
  689. }
  690. update();
  691. }
  692. void Window::set_default_return_key_widget(Widget* widget)
  693. {
  694. if (m_default_return_key_widget == widget)
  695. return;
  696. m_default_return_key_widget = widget;
  697. }
  698. void Window::set_focused_widget(Widget* widget, FocusSource source)
  699. {
  700. if (m_focused_widget == widget)
  701. return;
  702. WeakPtr<Widget> previously_focused_widget = m_focused_widget;
  703. m_focused_widget = widget;
  704. if (!m_focused_widget && m_previously_focused_widget)
  705. m_focused_widget = m_previously_focused_widget;
  706. if (m_default_return_key_widget && m_default_return_key_widget->on_focus_change)
  707. m_default_return_key_widget->on_focus_change(m_default_return_key_widget->is_focused(), source);
  708. if (previously_focused_widget) {
  709. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  710. previously_focused_widget->update();
  711. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  712. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  713. m_previously_focused_widget = previously_focused_widget;
  714. }
  715. if (m_focused_widget) {
  716. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  717. m_focused_widget->update();
  718. if (m_focused_widget && m_focused_widget->on_focus_change)
  719. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  720. }
  721. }
  722. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  723. {
  724. if (widget == m_automatic_cursor_tracking_widget)
  725. return;
  726. m_automatic_cursor_tracking_widget = widget;
  727. }
  728. void Window::set_has_alpha_channel(bool value)
  729. {
  730. if (m_has_alpha_channel == value)
  731. return;
  732. m_has_alpha_channel = value;
  733. if (!is_visible())
  734. return;
  735. m_pending_paint_event_rects.clear();
  736. m_back_store = nullptr;
  737. m_front_store = nullptr;
  738. ConnectionToWindowServer::the().async_set_window_has_alpha_channel(m_window_id, value);
  739. update();
  740. }
  741. void Window::set_double_buffering_enabled(bool value)
  742. {
  743. VERIFY(!is_visible());
  744. m_double_buffering_enabled = value;
  745. }
  746. void Window::set_opacity(float opacity)
  747. {
  748. m_opacity_when_windowless = opacity;
  749. if (!is_visible())
  750. return;
  751. ConnectionToWindowServer::the().async_set_window_opacity(m_window_id, opacity);
  752. }
  753. void Window::set_alpha_hit_threshold(float threshold)
  754. {
  755. if (threshold < 0.0f)
  756. threshold = 0.0f;
  757. else if (threshold > 1.0f)
  758. threshold = 1.0f;
  759. if (m_alpha_hit_threshold == threshold)
  760. return;
  761. m_alpha_hit_threshold = threshold;
  762. if (!is_visible())
  763. return;
  764. ConnectionToWindowServer::the().async_set_window_alpha_hit_threshold(m_window_id, threshold);
  765. }
  766. void Window::set_hovered_widget(Widget* widget)
  767. {
  768. if (widget == m_hovered_widget)
  769. return;
  770. if (m_hovered_widget)
  771. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  772. m_hovered_widget = widget;
  773. if (m_hovered_widget)
  774. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  775. auto* app = Application::the();
  776. if (app && app->hover_debugging_enabled())
  777. update();
  778. }
  779. void Window::set_current_backing_store(WindowBackingStore& backing_store, bool flush_immediately) const
  780. {
  781. auto& bitmap = backing_store.bitmap();
  782. ConnectionToWindowServer::the().set_window_backing_store(
  783. m_window_id,
  784. 32,
  785. bitmap.pitch(),
  786. bitmap.anonymous_buffer().fd(),
  787. backing_store.serial(),
  788. bitmap.has_alpha_channel(),
  789. bitmap.size(),
  790. backing_store.visible_size(),
  791. flush_immediately);
  792. }
  793. void Window::flip(Vector<Gfx::IntRect, 32> const& dirty_rects)
  794. {
  795. swap(m_front_store, m_back_store);
  796. set_current_backing_store(*m_front_store);
  797. if (!m_back_store || m_back_store->size() != m_front_store->size()) {
  798. m_back_store = create_backing_store(m_front_store->size()).release_value_but_fixme_should_propagate_errors();
  799. memcpy(m_back_store->bitmap().scanline(0), m_front_store->bitmap().scanline(0), m_front_store->bitmap().size_in_bytes());
  800. m_back_store->bitmap().set_volatile();
  801. return;
  802. }
  803. // Copy whatever was painted from the front to the back.
  804. Painter painter(m_back_store->bitmap());
  805. for (auto& dirty_rect : dirty_rects)
  806. painter.blit(dirty_rect.location(), m_front_store->bitmap(), dirty_rect, 1.0f, false);
  807. m_back_store->bitmap().set_volatile();
  808. }
  809. ErrorOr<NonnullOwnPtr<WindowBackingStore>> Window::create_backing_store(Gfx::IntSize size)
  810. {
  811. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  812. VERIFY(!size.is_empty());
  813. size_t pitch = Gfx::Bitmap::minimum_pitch(size.width(), format);
  814. size_t size_in_bytes = size.height() * pitch;
  815. auto buffer = TRY(Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes, PAGE_SIZE)));
  816. // FIXME: Plumb scale factor here eventually.
  817. auto bitmap = TRY(Gfx::Bitmap::create_with_anonymous_buffer(format, buffer, size, 1, {}));
  818. return make<WindowBackingStore>(bitmap);
  819. }
  820. void Window::wm_event(WMEvent&)
  821. {
  822. }
  823. void Window::screen_rects_change_event(ScreenRectsChangeEvent&)
  824. {
  825. }
  826. void Window::applet_area_rect_change_event(AppletAreaRectChangeEvent&)
  827. {
  828. }
  829. void Window::set_icon(Gfx::Bitmap const* icon)
  830. {
  831. if (m_icon == icon)
  832. return;
  833. Gfx::IntSize icon_size = icon ? icon->size() : Gfx::IntSize(16, 16);
  834. auto new_icon = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, icon_size).release_value_but_fixme_should_propagate_errors();
  835. if (icon) {
  836. Painter painter(*new_icon);
  837. painter.blit({ 0, 0 }, *icon, icon->rect());
  838. }
  839. m_icon = move(new_icon);
  840. apply_icon();
  841. }
  842. void Window::apply_icon()
  843. {
  844. if (!m_icon)
  845. return;
  846. if (!is_visible())
  847. return;
  848. ConnectionToWindowServer::the().async_set_window_icon_bitmap(m_window_id, m_icon->to_shareable_bitmap());
  849. }
  850. void Window::start_interactive_resize(ResizeDirection resize_direction)
  851. {
  852. ConnectionToWindowServer::the().async_start_window_resize(m_window_id, (i32)resize_direction);
  853. }
  854. Vector<Widget&> Window::focusable_widgets(FocusSource source) const
  855. {
  856. if (!m_main_widget)
  857. return {};
  858. HashTable<Widget*> seen_widgets;
  859. Vector<Widget&> collected_widgets;
  860. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  861. bool widget_accepts_focus = false;
  862. switch (source) {
  863. case FocusSource::Keyboard:
  864. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::TabFocus);
  865. break;
  866. case FocusSource::Mouse:
  867. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::ClickFocus);
  868. break;
  869. case FocusSource::Programmatic:
  870. widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
  871. break;
  872. }
  873. if (widget_accepts_focus) {
  874. auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
  875. if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
  876. collected_widgets.append(effective_focus_widget);
  877. }
  878. widget.for_each_child_widget([&](auto& child) {
  879. if (!child.is_visible())
  880. return IterationDecision::Continue;
  881. if (!child.is_enabled())
  882. return IterationDecision::Continue;
  883. if (!child.is_auto_focusable())
  884. return IterationDecision::Continue;
  885. collect_focusable_widgets(child);
  886. return IterationDecision::Continue;
  887. });
  888. };
  889. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  890. return collected_widgets;
  891. }
  892. void Window::set_fullscreen(bool fullscreen)
  893. {
  894. if (m_fullscreen == fullscreen)
  895. return;
  896. m_fullscreen = fullscreen;
  897. if (!is_visible())
  898. return;
  899. ConnectionToWindowServer::the().async_set_fullscreen(m_window_id, fullscreen);
  900. }
  901. void Window::set_frameless(bool frameless)
  902. {
  903. if (m_frameless == frameless)
  904. return;
  905. m_frameless = frameless;
  906. if (!is_visible())
  907. return;
  908. ConnectionToWindowServer::the().async_set_frameless(m_window_id, frameless);
  909. if (!frameless)
  910. apply_icon();
  911. }
  912. void Window::set_forced_shadow(bool shadow)
  913. {
  914. if (m_forced_shadow == shadow)
  915. return;
  916. m_forced_shadow = shadow;
  917. if (!is_visible())
  918. return;
  919. ConnectionToWindowServer::the().async_set_forced_shadow(m_window_id, shadow);
  920. }
  921. void Window::set_obey_widget_min_size(bool obey_widget_min_size)
  922. {
  923. if (m_obey_widget_min_size != obey_widget_min_size) {
  924. m_obey_widget_min_size = obey_widget_min_size;
  925. schedule_relayout();
  926. }
  927. }
  928. void Window::set_auto_shrink(bool shrink)
  929. {
  930. if (m_auto_shrink == shrink)
  931. return;
  932. m_auto_shrink = shrink;
  933. schedule_relayout();
  934. }
  935. void Window::set_maximized(bool maximized)
  936. {
  937. m_maximized = maximized;
  938. if (!is_visible())
  939. return;
  940. ConnectionToWindowServer::the().async_set_maximized(m_window_id, maximized);
  941. }
  942. void Window::set_minimized(bool minimized)
  943. {
  944. if (!is_minimizable())
  945. return;
  946. m_minimized = minimized;
  947. if (!is_visible())
  948. return;
  949. ConnectionToWindowServer::the().async_set_minimized(m_window_id, minimized);
  950. }
  951. void Window::update_min_size()
  952. {
  953. if (!main_widget())
  954. return;
  955. main_widget()->do_layout();
  956. auto min_size = main_widget()->effective_min_size();
  957. Gfx::IntSize size = { MUST(min_size.width().shrink_value()), MUST(min_size.height().shrink_value()) };
  958. if (is_obeying_widget_min_size()) {
  959. m_minimum_size_when_windowless = size;
  960. if (is_visible())
  961. ConnectionToWindowServer::the().async_set_window_minimum_size(m_window_id, size);
  962. }
  963. if (is_auto_shrinking())
  964. resize(size);
  965. }
  966. void Window::schedule_relayout()
  967. {
  968. if (m_layout_pending || !is_visible())
  969. return;
  970. m_layout_pending = true;
  971. deferred_invoke([this] {
  972. update_min_size();
  973. update();
  974. m_layout_pending = false;
  975. });
  976. }
  977. void Window::refresh_system_theme()
  978. {
  979. ConnectionToWindowServer::the().async_refresh_system_theme();
  980. }
  981. void Window::for_each_window(Badge<ConnectionToWindowServer>, Function<void(Window&)> callback)
  982. {
  983. for (auto& e : *reified_windows) {
  984. VERIFY(e.value);
  985. callback(*e.value);
  986. }
  987. }
  988. void Window::update_all_windows(Badge<ConnectionToWindowServer>)
  989. {
  990. for (auto& e : *reified_windows) {
  991. e.value->force_update();
  992. }
  993. }
  994. void Window::notify_state_changed(Badge<ConnectionToWindowServer>, bool minimized, bool maximized, bool occluded)
  995. {
  996. m_visible_for_timer_purposes = !minimized && !occluded;
  997. m_maximized = maximized;
  998. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  999. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  1000. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  1001. if (!store)
  1002. return;
  1003. if (minimized || occluded) {
  1004. store->bitmap().set_volatile();
  1005. } else {
  1006. bool was_purged = false;
  1007. bool bitmap_has_memory = store->bitmap().set_nonvolatile(was_purged);
  1008. if (!bitmap_has_memory) {
  1009. // Not enough memory to make the bitmap non-volatile. Lose the bitmap and schedule an update.
  1010. // Let the paint system figure out what to do.
  1011. store = nullptr;
  1012. update();
  1013. } else if (was_purged) {
  1014. // The bitmap memory was purged by the kernel, but we have all-new zero-filled pages.
  1015. // Schedule an update to regenerate the bitmap.
  1016. update();
  1017. }
  1018. }
  1019. }
  1020. Action* Window::action_for_shortcut(Shortcut const& shortcut)
  1021. {
  1022. return Action::find_action_for_shortcut(*this, shortcut);
  1023. }
  1024. void Window::set_base_size(Gfx::IntSize base_size)
  1025. {
  1026. if (m_base_size == base_size)
  1027. return;
  1028. m_base_size = base_size;
  1029. if (is_visible())
  1030. ConnectionToWindowServer::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  1031. }
  1032. void Window::set_size_increment(Gfx::IntSize size_increment)
  1033. {
  1034. if (m_size_increment == size_increment)
  1035. return;
  1036. m_size_increment = size_increment;
  1037. if (is_visible())
  1038. ConnectionToWindowServer::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  1039. }
  1040. void Window::set_resize_aspect_ratio(Optional<Gfx::IntSize> const& ratio)
  1041. {
  1042. if (m_resize_aspect_ratio == ratio)
  1043. return;
  1044. m_resize_aspect_ratio = ratio;
  1045. if (is_visible())
  1046. ConnectionToWindowServer::the().async_set_window_resize_aspect_ratio(m_window_id, m_resize_aspect_ratio);
  1047. }
  1048. void Window::did_add_widget(Badge<Widget>, Widget&)
  1049. {
  1050. if (!m_focused_widget)
  1051. focus_a_widget_if_possible(FocusSource::Mouse);
  1052. }
  1053. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  1054. {
  1055. if (m_focused_widget == &widget)
  1056. m_focused_widget = nullptr;
  1057. if (m_hovered_widget == &widget)
  1058. m_hovered_widget = nullptr;
  1059. if (m_automatic_cursor_tracking_widget == &widget)
  1060. m_automatic_cursor_tracking_widget = nullptr;
  1061. }
  1062. void Window::set_progress(Optional<int> progress)
  1063. {
  1064. VERIFY(m_window_id);
  1065. ConnectionToWindowServer::the().async_set_window_progress(m_window_id, progress);
  1066. }
  1067. void Window::update_cursor()
  1068. {
  1069. auto new_cursor = m_cursor;
  1070. auto is_usable_cursor = [](auto& cursor) {
  1071. return cursor.template has<NonnullRefPtr<Gfx::Bitmap const>>() || cursor.template get<Gfx::StandardCursor>() != Gfx::StandardCursor::None;
  1072. };
  1073. // NOTE: If there's an automatic cursor tracking widget, we retain its cursor until tracking stops.
  1074. if (auto widget = m_automatic_cursor_tracking_widget) {
  1075. if (is_usable_cursor(widget->override_cursor()))
  1076. new_cursor = widget->override_cursor();
  1077. } else if (auto widget = m_hovered_widget) {
  1078. if (is_usable_cursor(widget->override_cursor()))
  1079. new_cursor = widget->override_cursor();
  1080. }
  1081. if (are_cursors_the_same(m_effective_cursor, new_cursor))
  1082. return;
  1083. m_effective_cursor = new_cursor;
  1084. if (new_cursor.has<NonnullRefPtr<Gfx::Bitmap const>>())
  1085. ConnectionToWindowServer::the().async_set_window_custom_cursor(m_window_id, new_cursor.get<NonnullRefPtr<Gfx::Bitmap const>>()->to_shareable_bitmap());
  1086. else
  1087. ConnectionToWindowServer::the().async_set_window_cursor(m_window_id, (u32)new_cursor.get<Gfx::StandardCursor>());
  1088. }
  1089. void Window::focus_a_widget_if_possible(FocusSource source)
  1090. {
  1091. auto focusable_widgets = this->focusable_widgets(source);
  1092. if (!focusable_widgets.is_empty())
  1093. set_focused_widget(&focusable_widgets[0], source);
  1094. }
  1095. void Window::did_disable_focused_widget(Badge<Widget>)
  1096. {
  1097. focus_a_widget_if_possible(FocusSource::Mouse);
  1098. }
  1099. bool Window::is_active() const
  1100. {
  1101. VERIFY(Application::the());
  1102. return this == Application::the()->active_window();
  1103. }
  1104. Gfx::Bitmap* Window::back_bitmap()
  1105. {
  1106. return m_back_store ? &m_back_store->bitmap() : nullptr;
  1107. }
  1108. ErrorOr<void> Window::try_add_menu(NonnullRefPtr<Menu> menu)
  1109. {
  1110. TRY(m_menubar->try_add_menu({}, move(menu)));
  1111. if (m_window_id) {
  1112. menu->realize_menu_if_needed();
  1113. ConnectionToWindowServer::the().async_add_menu(m_window_id, menu->menu_id());
  1114. }
  1115. return {};
  1116. }
  1117. ErrorOr<NonnullRefPtr<Menu>> Window::try_add_menu(DeprecatedString name)
  1118. {
  1119. auto menu = TRY(m_menubar->try_add_menu({}, move(name)));
  1120. if (m_window_id) {
  1121. menu->realize_menu_if_needed();
  1122. ConnectionToWindowServer::the().async_add_menu(m_window_id, menu->menu_id());
  1123. }
  1124. return menu;
  1125. }
  1126. Menu& Window::add_menu(DeprecatedString name)
  1127. {
  1128. auto menu = MUST(try_add_menu(move(name)));
  1129. return *menu;
  1130. }
  1131. void Window::flash_menubar_menu_for(MenuItem const& menu_item)
  1132. {
  1133. if (!Desktop::the().system_effects().flash_menus())
  1134. return;
  1135. auto menu_id = menu_item.menu_id();
  1136. if (menu_id < 0)
  1137. return;
  1138. ConnectionToWindowServer::the().async_flash_menubar_menu(m_window_id, menu_id);
  1139. }
  1140. bool Window::is_modified() const
  1141. {
  1142. if (!m_window_id)
  1143. return false;
  1144. return ConnectionToWindowServer::the().is_window_modified(m_window_id);
  1145. }
  1146. void Window::set_modified(bool modified)
  1147. {
  1148. if (!m_window_id)
  1149. return;
  1150. ConnectionToWindowServer::the().async_set_window_modified(m_window_id, modified);
  1151. }
  1152. void Window::flush_pending_paints_immediately()
  1153. {
  1154. if (!m_window_id)
  1155. return;
  1156. if (m_pending_paint_event_rects.is_empty())
  1157. return;
  1158. MultiPaintEvent paint_event(move(m_pending_paint_event_rects), size());
  1159. handle_multi_paint_event(paint_event);
  1160. }
  1161. void Window::set_always_on_top(bool always_on_top)
  1162. {
  1163. if (!m_window_id)
  1164. return;
  1165. ConnectionToWindowServer::the().set_always_on_top(m_window_id, always_on_top);
  1166. }
  1167. }