GWindow.cpp 22 KB

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