GWindow.cpp 21 KB

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