Window.cpp 23 KB

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