GWindow.cpp 21 KB

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