Window.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashMap.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/NeverDestroyed.h>
  29. #include <AK/SharedBuffer.h>
  30. #include <LibCore/MimeData.h>
  31. #include <LibGUI/Action.h>
  32. #include <LibGUI/Application.h>
  33. #include <LibGUI/Event.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/Widget.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGUI/WindowServerConnection.h>
  38. #include <LibGfx/Bitmap.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <unistd.h>
  42. //#define UPDATE_COALESCING_DEBUG
  43. namespace GUI {
  44. static NeverDestroyed<HashTable<Window*>> all_windows;
  45. static NeverDestroyed<HashMap<int, Window*>> reified_windows;
  46. Window* Window::from_window_id(int window_id)
  47. {
  48. auto it = reified_windows->find(window_id);
  49. if (it != reified_windows->end())
  50. return (*it).value;
  51. return nullptr;
  52. }
  53. Window::Window(Core::Object* parent)
  54. : Core::Object(parent)
  55. {
  56. all_windows->set(this);
  57. m_rect_when_windowless = { 100, 400, 140, 140 };
  58. m_title_when_windowless = "GWindow";
  59. }
  60. Window::~Window()
  61. {
  62. all_windows->remove(this);
  63. hide();
  64. }
  65. void Window::close()
  66. {
  67. hide();
  68. }
  69. void Window::move_to_front()
  70. {
  71. if (!m_window_id)
  72. return;
  73. WindowServerConnection::the().send_sync<Messages::WindowServer::MoveWindowToFront>(m_window_id);
  74. }
  75. void Window::show()
  76. {
  77. if (m_window_id)
  78. return;
  79. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateWindow>(
  80. m_rect_when_windowless,
  81. m_has_alpha_channel,
  82. m_modal,
  83. m_minimizable,
  84. m_resizable,
  85. m_fullscreen,
  86. m_show_titlebar,
  87. m_opacity_when_windowless,
  88. m_base_size,
  89. m_size_increment,
  90. (i32)m_window_type,
  91. m_title_when_windowless);
  92. m_window_id = response->window_id();
  93. apply_icon();
  94. reified_windows->set(m_window_id, this);
  95. Application::the().did_create_window({});
  96. update();
  97. }
  98. void Window::hide()
  99. {
  100. if (!m_window_id)
  101. return;
  102. reified_windows->remove(m_window_id);
  103. WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyWindow>(m_window_id);
  104. m_window_id = 0;
  105. m_pending_paint_event_rects.clear();
  106. m_back_bitmap = nullptr;
  107. m_front_bitmap = nullptr;
  108. bool app_has_visible_windows = false;
  109. for (auto& window : *all_windows) {
  110. if (window->is_visible()) {
  111. app_has_visible_windows = true;
  112. break;
  113. }
  114. }
  115. if (!app_has_visible_windows)
  116. Application::the().did_delete_last_window({});
  117. }
  118. void Window::set_title(const StringView& title)
  119. {
  120. m_title_when_windowless = title;
  121. if (!m_window_id)
  122. return;
  123. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowTitle>(m_window_id, title);
  124. }
  125. String Window::title() const
  126. {
  127. if (!m_window_id)
  128. return m_title_when_windowless;
  129. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowTitle>(m_window_id)->title();
  130. }
  131. Gfx::Rect Window::rect() const
  132. {
  133. if (!m_window_id)
  134. return m_rect_when_windowless;
  135. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowRect>(m_window_id)->rect();
  136. }
  137. void Window::set_rect(const Gfx::Rect& a_rect)
  138. {
  139. m_rect_when_windowless = a_rect;
  140. if (!m_window_id) {
  141. if (m_main_widget)
  142. m_main_widget->resize(m_rect_when_windowless.size());
  143. return;
  144. }
  145. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowRect>(m_window_id, a_rect);
  146. if (m_back_bitmap && m_back_bitmap->size() != a_rect.size())
  147. m_back_bitmap = nullptr;
  148. if (m_front_bitmap && m_front_bitmap->size() != a_rect.size())
  149. m_front_bitmap = nullptr;
  150. if (m_main_widget)
  151. m_main_widget->resize(a_rect.size());
  152. }
  153. void Window::set_window_type(WindowType window_type)
  154. {
  155. m_window_type = window_type;
  156. }
  157. void Window::set_override_cursor(StandardCursor cursor)
  158. {
  159. if (!m_window_id)
  160. return;
  161. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor);
  162. }
  163. void Window::event(Core::Event& event)
  164. {
  165. if (event.type() == Event::Drop) {
  166. auto& drop_event = static_cast<DropEvent&>(event);
  167. if (!m_main_widget)
  168. return;
  169. auto result = m_main_widget->hit_test(drop_event.position());
  170. auto local_event = make<DropEvent>(result.local_position, drop_event.text(), drop_event.mime_data());
  171. ASSERT(result.widget);
  172. return result.widget->dispatch_event(*local_event, this);
  173. }
  174. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel) {
  175. auto& mouse_event = static_cast<MouseEvent&>(event);
  176. if (m_global_cursor_tracking_widget) {
  177. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  178. Gfx::Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  179. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  180. m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
  181. return;
  182. }
  183. if (m_automatic_cursor_tracking_widget) {
  184. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  185. Gfx::Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  186. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  187. m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
  188. if (mouse_event.buttons() == 0)
  189. m_automatic_cursor_tracking_widget = nullptr;
  190. return;
  191. }
  192. if (!m_main_widget)
  193. return;
  194. auto result = m_main_widget->hit_test(mouse_event.position());
  195. auto local_event = make<MouseEvent>((Event::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  196. ASSERT(result.widget);
  197. set_hovered_widget(result.widget);
  198. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  199. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  200. if (result.widget != m_global_cursor_tracking_widget.ptr())
  201. return result.widget->dispatch_event(*local_event, this);
  202. return;
  203. }
  204. if (event.type() == Event::MultiPaint) {
  205. if (!m_window_id)
  206. return;
  207. if (!m_main_widget)
  208. return;
  209. auto& paint_event = static_cast<MultiPaintEvent&>(event);
  210. auto rects = paint_event.rects();
  211. ASSERT(!rects.is_empty());
  212. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  213. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  214. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  215. // effort on painting into an undersized bitmap that will be thrown away anyway.
  216. m_back_bitmap = nullptr;
  217. }
  218. bool created_new_backing_store = !m_back_bitmap;
  219. if (!m_back_bitmap) {
  220. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  221. } else if (m_double_buffering_enabled) {
  222. bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile();
  223. if (!still_has_pixels) {
  224. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  225. created_new_backing_store = true;
  226. }
  227. }
  228. auto rect = rects.first();
  229. if (rect.is_empty() || created_new_backing_store) {
  230. rects.clear();
  231. rects.append({ {}, paint_event.window_size() });
  232. }
  233. for (auto& rect : rects)
  234. m_main_widget->dispatch_event(*make<PaintEvent>(rect), this);
  235. if (m_double_buffering_enabled)
  236. flip(rects);
  237. else if (created_new_backing_store)
  238. set_current_backing_bitmap(*m_back_bitmap, true);
  239. if (m_window_id) {
  240. Vector<Gfx::Rect> rects_to_send;
  241. for (auto& r : rects)
  242. rects_to_send.append(r);
  243. WindowServerConnection::the().post_message(Messages::WindowServer::DidFinishPainting(m_window_id, rects_to_send));
  244. }
  245. return;
  246. }
  247. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
  248. if (m_focused_widget)
  249. return m_focused_widget->dispatch_event(event, this);
  250. if (m_main_widget)
  251. return m_main_widget->dispatch_event(event, this);
  252. return;
  253. }
  254. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive) {
  255. m_is_active = event.type() == Event::WindowBecameActive;
  256. if (m_main_widget)
  257. m_main_widget->dispatch_event(event, this);
  258. if (m_focused_widget)
  259. m_focused_widget->update();
  260. return;
  261. }
  262. if (event.type() == Event::WindowCloseRequest) {
  263. if (on_close_request) {
  264. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  265. return;
  266. }
  267. close();
  268. return;
  269. }
  270. if (event.type() == Event::WindowLeft) {
  271. set_hovered_widget(nullptr);
  272. return;
  273. }
  274. if (event.type() == Event::Resize) {
  275. auto new_size = static_cast<ResizeEvent&>(event).size();
  276. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  277. m_back_bitmap = nullptr;
  278. if (!m_pending_paint_event_rects.is_empty()) {
  279. m_pending_paint_event_rects.clear_with_capacity();
  280. m_pending_paint_event_rects.append({ {}, new_size });
  281. }
  282. m_rect_when_windowless = { {}, new_size };
  283. m_main_widget->set_relative_rect({ {}, new_size });
  284. return;
  285. }
  286. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  287. return wm_event(static_cast<WMEvent&>(event));
  288. if (event.type() == Event::DragMove) {
  289. if (!m_main_widget)
  290. return;
  291. auto& drag_event = static_cast<DragEvent&>(event);
  292. auto result = m_main_widget->hit_test(drag_event.position());
  293. auto local_event = make<DragEvent>(static_cast<Event::Type>(drag_event.type()), result.local_position, drag_event.data_type());
  294. ASSERT(result.widget);
  295. return result.widget->dispatch_event(*local_event, this);
  296. }
  297. Core::Object::event(event);
  298. }
  299. bool Window::is_visible() const
  300. {
  301. return m_window_id != 0;
  302. }
  303. void Window::update()
  304. {
  305. update({ 0, 0, width(), height() });
  306. }
  307. void Window::update(const Gfx::Rect& a_rect)
  308. {
  309. if (!m_window_id)
  310. return;
  311. for (auto& pending_rect : m_pending_paint_event_rects) {
  312. if (pending_rect.contains(a_rect)) {
  313. #ifdef UPDATE_COALESCING_DEBUG
  314. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  315. #endif
  316. return;
  317. }
  318. }
  319. if (m_pending_paint_event_rects.is_empty()) {
  320. deferred_invoke([this](auto&) {
  321. auto rects = move(m_pending_paint_event_rects);
  322. if (rects.is_empty())
  323. return;
  324. Vector<Gfx::Rect> rects_to_send;
  325. for (auto& r : rects)
  326. rects_to_send.append(r);
  327. WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, rects_to_send));
  328. });
  329. }
  330. m_pending_paint_event_rects.append(a_rect);
  331. }
  332. void Window::set_main_widget(Widget* widget)
  333. {
  334. if (m_main_widget == widget)
  335. return;
  336. if (m_main_widget)
  337. remove_child(*m_main_widget);
  338. m_main_widget = widget;
  339. if (m_main_widget) {
  340. add_child(*widget);
  341. auto new_window_rect = rect();
  342. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  343. new_window_rect.set_width(m_main_widget->preferred_size().width());
  344. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  345. new_window_rect.set_height(m_main_widget->preferred_size().height());
  346. set_rect(new_window_rect);
  347. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  348. m_main_widget->set_window(this);
  349. if (m_main_widget->accepts_focus())
  350. m_main_widget->set_focus(true);
  351. }
  352. update();
  353. }
  354. void Window::set_focused_widget(Widget* widget)
  355. {
  356. if (m_focused_widget == widget)
  357. return;
  358. if (m_focused_widget) {
  359. Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusOut));
  360. m_focused_widget->update();
  361. }
  362. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  363. if (m_focused_widget) {
  364. Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusIn));
  365. m_focused_widget->update();
  366. }
  367. }
  368. void Window::set_global_cursor_tracking_widget(Widget* widget)
  369. {
  370. if (widget == m_global_cursor_tracking_widget)
  371. return;
  372. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  373. }
  374. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  375. {
  376. if (widget == m_automatic_cursor_tracking_widget)
  377. return;
  378. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  379. }
  380. void Window::set_has_alpha_channel(bool value)
  381. {
  382. if (m_has_alpha_channel == value)
  383. return;
  384. m_has_alpha_channel = value;
  385. if (!m_window_id)
  386. return;
  387. m_pending_paint_event_rects.clear();
  388. m_back_bitmap = nullptr;
  389. m_front_bitmap = nullptr;
  390. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  391. update();
  392. }
  393. void Window::set_double_buffering_enabled(bool value)
  394. {
  395. ASSERT(!m_window_id);
  396. m_double_buffering_enabled = value;
  397. }
  398. void Window::set_opacity(float opacity)
  399. {
  400. m_opacity_when_windowless = opacity;
  401. if (!m_window_id)
  402. return;
  403. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOpacity>(m_window_id, opacity);
  404. }
  405. void Window::set_hovered_widget(Widget* widget)
  406. {
  407. if (widget == m_hovered_widget)
  408. return;
  409. if (m_hovered_widget)
  410. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  411. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  412. if (m_hovered_widget)
  413. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  414. }
  415. void Window::set_current_backing_bitmap(Gfx::Bitmap& bitmap, bool flush_immediately)
  416. {
  417. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shared_buffer_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  418. }
  419. void Window::flip(const Vector<Gfx::Rect, 32>& dirty_rects)
  420. {
  421. swap(m_front_bitmap, m_back_bitmap);
  422. set_current_backing_bitmap(*m_front_bitmap);
  423. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  424. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  425. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  426. m_back_bitmap->shared_buffer()->set_volatile();
  427. return;
  428. }
  429. // Copy whatever was painted from the front to the back.
  430. Painter painter(*m_back_bitmap);
  431. for (auto& dirty_rect : dirty_rects)
  432. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  433. m_back_bitmap->shared_buffer()->set_volatile();
  434. }
  435. NonnullRefPtr<Gfx::Bitmap> Window::create_shared_bitmap(Gfx::Bitmap::Format format, const Gfx::Size& size)
  436. {
  437. ASSERT(WindowServerConnection::the().server_pid());
  438. ASSERT(!size.is_empty());
  439. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(Gfx::RGBA32), 16);
  440. size_t size_in_bytes = size.height() * pitch;
  441. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  442. ASSERT(shared_buffer);
  443. shared_buffer->share_with(WindowServerConnection::the().server_pid());
  444. return Gfx::Bitmap::create_with_shared_buffer(format, *shared_buffer, size);
  445. }
  446. NonnullRefPtr<Gfx::Bitmap> Window::create_backing_bitmap(const Gfx::Size& size)
  447. {
  448. auto format = m_has_alpha_channel ? Gfx::Bitmap::Format::RGBA32 : Gfx::Bitmap::Format::RGB32;
  449. return create_shared_bitmap(format, size);
  450. }
  451. void Window::set_modal(bool modal)
  452. {
  453. ASSERT(!m_window_id);
  454. m_modal = modal;
  455. }
  456. void Window::wm_event(WMEvent&)
  457. {
  458. }
  459. void Window::set_icon(const Gfx::Bitmap* icon)
  460. {
  461. if (m_icon == icon)
  462. return;
  463. m_icon = create_shared_bitmap(Gfx::Bitmap::Format::RGBA32, icon->size());
  464. {
  465. Painter painter(*m_icon);
  466. painter.blit({ 0, 0 }, *icon, icon->rect());
  467. }
  468. apply_icon();
  469. }
  470. void Window::apply_icon()
  471. {
  472. if (!m_icon)
  473. return;
  474. if (!m_window_id)
  475. return;
  476. int rc = seal_shared_buffer(m_icon->shared_buffer_id());
  477. ASSERT(rc == 0);
  478. rc = share_buffer_globally(m_icon->shared_buffer_id());
  479. ASSERT(rc == 0);
  480. static bool has_set_process_icon;
  481. if (!has_set_process_icon)
  482. set_process_icon(m_icon->shared_buffer_id());
  483. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shared_buffer_id(), m_icon->size());
  484. }
  485. void Window::start_wm_resize()
  486. {
  487. WindowServerConnection::the().post_message(Messages::WindowServer::WM_StartWindowResize(WindowServerConnection::the().my_client_id(), m_window_id));
  488. }
  489. Vector<Widget*> Window::focusable_widgets() const
  490. {
  491. if (!m_main_widget)
  492. return {};
  493. Vector<Widget*> collected_widgets;
  494. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  495. if (widget.accepts_focus())
  496. collected_widgets.append(&widget);
  497. widget.for_each_child_widget([&](auto& child) {
  498. if (!child.is_visible())
  499. return IterationDecision::Continue;
  500. if (!child.is_enabled())
  501. return IterationDecision::Continue;
  502. collect_focusable_widgets(child);
  503. return IterationDecision::Continue;
  504. });
  505. };
  506. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  507. return collected_widgets;
  508. }
  509. void Window::save_to(AK::JsonObject& json)
  510. {
  511. json.set("title", title());
  512. json.set("visible", is_visible());
  513. json.set("active", is_active());
  514. json.set("minimizable", is_minimizable());
  515. json.set("resizable", is_resizable());
  516. json.set("fullscreen", is_fullscreen());
  517. json.set("rect", rect().to_string());
  518. json.set("base_size", base_size().to_string());
  519. json.set("size_increment", size_increment().to_string());
  520. Core::Object::save_to(json);
  521. }
  522. void Window::set_fullscreen(bool fullscreen)
  523. {
  524. if (m_fullscreen == fullscreen)
  525. return;
  526. m_fullscreen = fullscreen;
  527. if (!m_window_id)
  528. return;
  529. WindowServerConnection::the().send_sync<Messages::WindowServer::SetFullscreen>(m_window_id, fullscreen);
  530. }
  531. void Window::schedule_relayout()
  532. {
  533. if (m_layout_pending)
  534. return;
  535. m_layout_pending = true;
  536. deferred_invoke([this](auto&) {
  537. if (main_widget())
  538. main_widget()->do_layout();
  539. update();
  540. m_layout_pending = false;
  541. });
  542. }
  543. void Window::update_all_windows(Badge<WindowServerConnection>)
  544. {
  545. for (auto* window : *all_windows) {
  546. window->update();
  547. }
  548. }
  549. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  550. {
  551. m_visible_for_timer_purposes = !minimized && !occluded;
  552. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  553. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  554. RefPtr<Gfx::Bitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap;
  555. if (!bitmap)
  556. return;
  557. if (minimized || occluded) {
  558. bitmap->shared_buffer()->set_volatile();
  559. } else {
  560. if (!bitmap->shared_buffer()->set_nonvolatile()) {
  561. bitmap = nullptr;
  562. update();
  563. }
  564. }
  565. }
  566. Action* Window::action_for_key_event(const KeyEvent& event)
  567. {
  568. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  569. Action* found_action = nullptr;
  570. for_each_child_of_type<Action>([&](auto& action) {
  571. if (action.shortcut() == shortcut) {
  572. found_action = &action;
  573. return IterationDecision::Break;
  574. }
  575. return IterationDecision::Continue;
  576. });
  577. return found_action;
  578. }
  579. }