Window.cpp 23 KB

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