Window.cpp 45 KB

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