GWindow.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  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::Drop) {
  138. auto& drop_event = static_cast<GDropEvent&>(event);
  139. if (!m_main_widget)
  140. return;
  141. auto result = m_main_widget->hit_test(drop_event.position());
  142. auto local_event = make<GDropEvent>(result.local_position, drop_event.text(), drop_event.data_type(), drop_event.data());
  143. ASSERT(result.widget);
  144. return result.widget->dispatch_event(*local_event, this);
  145. }
  146. if (event.type() == GEvent::MouseUp || event.type() == GEvent::MouseDown || event.type() == GEvent::MouseDoubleClick || event.type() == GEvent::MouseMove || event.type() == GEvent::MouseWheel) {
  147. auto& mouse_event = static_cast<GMouseEvent&>(event);
  148. if (m_global_cursor_tracking_widget) {
  149. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  150. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  151. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  152. m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
  153. return;
  154. }
  155. if (m_automatic_cursor_tracking_widget) {
  156. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  157. Point local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  158. auto local_event = make<GMouseEvent>((GEvent::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  159. m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
  160. if (mouse_event.buttons() == 0)
  161. m_automatic_cursor_tracking_widget = nullptr;
  162. return;
  163. }
  164. if (!m_main_widget)
  165. return;
  166. auto result = m_main_widget->hit_test(mouse_event.position());
  167. 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());
  168. ASSERT(result.widget);
  169. set_hovered_widget(result.widget);
  170. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  171. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  172. if (result.widget != m_global_cursor_tracking_widget.ptr())
  173. return result.widget->dispatch_event(*local_event, this);
  174. return;
  175. }
  176. if (event.type() == GEvent::MultiPaint) {
  177. if (!m_window_id)
  178. return;
  179. if (!m_main_widget)
  180. return;
  181. auto& paint_event = static_cast<GMultiPaintEvent&>(event);
  182. auto rects = paint_event.rects();
  183. ASSERT(!rects.is_empty());
  184. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  185. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  186. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  187. // effort on painting into an undersized bitmap that will be thrown away anyway.
  188. m_back_bitmap = nullptr;
  189. }
  190. bool created_new_backing_store = !m_back_bitmap;
  191. if (!m_back_bitmap) {
  192. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  193. } else if (m_double_buffering_enabled) {
  194. bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile();
  195. if (!still_has_pixels) {
  196. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  197. created_new_backing_store = true;
  198. }
  199. }
  200. auto rect = rects.first();
  201. if (rect.is_empty() || created_new_backing_store) {
  202. rects.clear();
  203. rects.append({ {}, paint_event.window_size() });
  204. }
  205. for (auto& rect : rects)
  206. m_main_widget->dispatch_event(*make<GPaintEvent>(rect), this);
  207. paint_keybinds();
  208. if (m_double_buffering_enabled)
  209. flip(rects);
  210. else if (created_new_backing_store)
  211. set_current_backing_bitmap(*m_back_bitmap, true);
  212. if (m_window_id) {
  213. Vector<Rect> rects_to_send;
  214. for (auto& r : rects)
  215. rects_to_send.append(r);
  216. GWindowServerConnection::the().post_message(WindowServer::DidFinishPainting(m_window_id, rects_to_send));
  217. }
  218. return;
  219. }
  220. if (event.type() == GEvent::KeyUp || event.type() == GEvent::KeyDown) {
  221. auto keyevent = static_cast<GKeyEvent&>(event);
  222. if (keyevent.logo() && event.type() == GEvent::KeyUp) {
  223. if (m_keybind_mode) {
  224. m_keybind_mode = false;
  225. } else {
  226. m_keybind_mode = true;
  227. collect_keyboard_activation_targets();
  228. m_entered_keybind = "";
  229. }
  230. update();
  231. return;
  232. }
  233. if (m_keybind_mode) {
  234. if (event.type() == GEvent::KeyUp) {
  235. StringBuilder builder;
  236. builder.append(m_entered_keybind);
  237. builder.append(keyevent.text());
  238. m_entered_keybind = builder.to_string();
  239. auto found_widget = m_keyboard_activation_targets.find(m_entered_keybind);
  240. if (found_widget != m_keyboard_activation_targets.end()) {
  241. m_keybind_mode = false;
  242. auto event = make<GMouseEvent>(GEvent::MouseDown, Point(), 0, GMouseButton::Left, 0, 0);
  243. found_widget->value->dispatch_event(*event, this);
  244. event = make<GMouseEvent>(GEvent::MouseUp, Point(), 0, GMouseButton::Left, 0, 0);
  245. found_widget->value->dispatch_event(*event, this);
  246. } else if (m_entered_keybind.length() >= m_max_keybind_length) {
  247. m_keybind_mode = false;
  248. }
  249. update();
  250. }
  251. } else {
  252. if (m_focused_widget)
  253. return m_focused_widget->dispatch_event(event, this);
  254. if (m_main_widget)
  255. return m_main_widget->dispatch_event(event, this);
  256. }
  257. return;
  258. }
  259. if (event.type() == GEvent::WindowBecameActive || event.type() == GEvent::WindowBecameInactive) {
  260. if (event.type() == GEvent::WindowBecameInactive && m_keybind_mode) {
  261. m_keybind_mode = false;
  262. update();
  263. }
  264. m_is_active = event.type() == GEvent::WindowBecameActive;
  265. if (m_main_widget)
  266. m_main_widget->dispatch_event(event, this);
  267. if (m_focused_widget)
  268. m_focused_widget->update();
  269. return;
  270. }
  271. if (event.type() == GEvent::WindowCloseRequest) {
  272. if (on_close_request) {
  273. if (on_close_request() == GWindow::CloseRequestDecision::StayOpen)
  274. return;
  275. }
  276. close();
  277. return;
  278. }
  279. if (event.type() == GEvent::WindowLeft) {
  280. set_hovered_widget(nullptr);
  281. return;
  282. }
  283. if (event.type() == GEvent::Resize) {
  284. auto new_size = static_cast<GResizeEvent&>(event).size();
  285. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  286. m_back_bitmap = nullptr;
  287. if (!m_pending_paint_event_rects.is_empty()) {
  288. m_pending_paint_event_rects.clear_with_capacity();
  289. m_pending_paint_event_rects.append({ {}, new_size });
  290. }
  291. m_rect_when_windowless = { {}, new_size };
  292. m_main_widget->set_relative_rect({ {}, new_size });
  293. return;
  294. }
  295. if (event.type() > GEvent::__Begin_WM_Events && event.type() < GEvent::__End_WM_Events)
  296. return wm_event(static_cast<GWMEvent&>(event));
  297. CObject::event(event);
  298. }
  299. void GWindow::paint_keybinds()
  300. {
  301. if (!m_keybind_mode)
  302. return;
  303. GPainter painter(*m_main_widget);
  304. for (auto& keypair : m_keyboard_activation_targets) {
  305. if (!keypair.value)
  306. continue;
  307. auto& widget = *keypair.value;
  308. bool could_be_keybind = true;
  309. for (size_t i = 0; i < m_entered_keybind.length(); ++i) {
  310. if (keypair.key.characters()[i] != m_entered_keybind.characters()[i]) {
  311. could_be_keybind = false;
  312. break;
  313. }
  314. }
  315. if (could_be_keybind) {
  316. Rect rect { widget.x() - 5, widget.y() - 5, 4 + Font::default_font().width(keypair.key), 16 };
  317. Rect highlight_rect { widget.x() - 3, widget.y() - 5, 0, 16 };
  318. painter.fill_rect(rect, Color::WarmGray);
  319. painter.draw_rect(rect, Color::Black);
  320. painter.draw_text(rect, keypair.key.characters(), TextAlignment::Center, Color::Black);
  321. painter.draw_text(highlight_rect, m_entered_keybind.characters(), TextAlignment::CenterLeft, Color::MidGray);
  322. }
  323. }
  324. }
  325. static void collect_keyboard_activation_targets_impl(GWidget& widget, Vector<GWidget*>& targets)
  326. {
  327. widget.for_each_child_widget([&](auto& child) {
  328. if (child.supports_keyboard_activation()) {
  329. targets.append(&child);
  330. collect_keyboard_activation_targets_impl(child, targets);
  331. }
  332. return IterationDecision::Continue;
  333. });
  334. }
  335. void GWindow::collect_keyboard_activation_targets()
  336. {
  337. m_keyboard_activation_targets.clear();
  338. if (!m_main_widget)
  339. return;
  340. Vector<GWidget*> targets;
  341. collect_keyboard_activation_targets_impl(*m_main_widget, targets);
  342. m_max_keybind_length = ceil_div(targets.size(), ('z' - 'a'));
  343. size_t buffer_length = m_max_keybind_length + 1;
  344. char keybind_buffer[buffer_length];
  345. for (size_t i = 0; i < buffer_length - 1; ++i)
  346. keybind_buffer[i] = 'a';
  347. keybind_buffer[buffer_length - 1] = '\0';
  348. for (auto& widget : targets) {
  349. m_keyboard_activation_targets.set(String(keybind_buffer), widget->make_weak_ptr());
  350. for (size_t i = 0; i < buffer_length - 1; ++i) {
  351. if (keybind_buffer[i] >= 'z') {
  352. keybind_buffer[i] = 'a';
  353. } else {
  354. ++keybind_buffer[i];
  355. break;
  356. }
  357. }
  358. }
  359. }
  360. bool GWindow::is_visible() const
  361. {
  362. return m_window_id != 0;
  363. }
  364. void GWindow::update(const Rect& a_rect)
  365. {
  366. if (!m_window_id)
  367. return;
  368. for (auto& pending_rect : m_pending_paint_event_rects) {
  369. if (pending_rect.contains(a_rect)) {
  370. #ifdef UPDATE_COALESCING_DEBUG
  371. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  372. #endif
  373. return;
  374. }
  375. }
  376. if (m_pending_paint_event_rects.is_empty()) {
  377. deferred_invoke([this](auto&) {
  378. auto rects = move(m_pending_paint_event_rects);
  379. if (rects.is_empty())
  380. return;
  381. Vector<Rect> rects_to_send;
  382. for (auto& r : rects)
  383. rects_to_send.append(r);
  384. GWindowServerConnection::the().post_message(WindowServer::InvalidateRect(m_window_id, rects_to_send));
  385. });
  386. }
  387. m_pending_paint_event_rects.append(a_rect);
  388. }
  389. void GWindow::set_main_widget(GWidget* widget)
  390. {
  391. if (m_main_widget == widget)
  392. return;
  393. if (m_main_widget)
  394. remove_child(*m_main_widget);
  395. m_main_widget = widget;
  396. if (m_main_widget) {
  397. add_child(*widget);
  398. auto new_window_rect = rect();
  399. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  400. new_window_rect.set_width(m_main_widget->preferred_size().width());
  401. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  402. new_window_rect.set_height(m_main_widget->preferred_size().height());
  403. set_rect(new_window_rect);
  404. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  405. m_main_widget->set_window(this);
  406. if (m_main_widget->accepts_focus())
  407. m_main_widget->set_focus(true);
  408. }
  409. update();
  410. }
  411. void GWindow::set_focused_widget(GWidget* widget)
  412. {
  413. if (m_focused_widget == widget)
  414. return;
  415. if (m_focused_widget) {
  416. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusOut));
  417. m_focused_widget->update();
  418. }
  419. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  420. if (m_focused_widget) {
  421. CEventLoop::current().post_event(*m_focused_widget, make<GEvent>(GEvent::FocusIn));
  422. m_focused_widget->update();
  423. }
  424. }
  425. void GWindow::set_global_cursor_tracking_widget(GWidget* widget)
  426. {
  427. if (widget == m_global_cursor_tracking_widget)
  428. return;
  429. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  430. }
  431. void GWindow::set_automatic_cursor_tracking_widget(GWidget* widget)
  432. {
  433. if (widget == m_automatic_cursor_tracking_widget)
  434. return;
  435. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  436. }
  437. void GWindow::set_has_alpha_channel(bool value)
  438. {
  439. if (m_has_alpha_channel == value)
  440. return;
  441. m_has_alpha_channel = value;
  442. if (!m_window_id)
  443. return;
  444. m_pending_paint_event_rects.clear();
  445. m_back_bitmap = nullptr;
  446. m_front_bitmap = nullptr;
  447. GWindowServerConnection::the().send_sync<WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  448. update();
  449. }
  450. void GWindow::set_double_buffering_enabled(bool value)
  451. {
  452. ASSERT(!m_window_id);
  453. m_double_buffering_enabled = value;
  454. }
  455. void GWindow::set_opacity(float opacity)
  456. {
  457. m_opacity_when_windowless = opacity;
  458. if (!m_window_id)
  459. return;
  460. GWindowServerConnection::the().send_sync<WindowServer::SetWindowOpacity>(m_window_id, opacity);
  461. }
  462. void GWindow::set_hovered_widget(GWidget* widget)
  463. {
  464. if (widget == m_hovered_widget)
  465. return;
  466. if (m_hovered_widget)
  467. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Leave));
  468. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  469. if (m_hovered_widget)
  470. CEventLoop::current().post_event(*m_hovered_widget, make<GEvent>(GEvent::Enter));
  471. }
  472. void GWindow::set_current_backing_bitmap(GraphicsBitmap& bitmap, bool flush_immediately)
  473. {
  474. GWindowServerConnection::the().send_sync<WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shared_buffer_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  475. }
  476. void GWindow::flip(const Vector<Rect, 32>& dirty_rects)
  477. {
  478. swap(m_front_bitmap, m_back_bitmap);
  479. set_current_backing_bitmap(*m_front_bitmap);
  480. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  481. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  482. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  483. m_back_bitmap->shared_buffer()->set_volatile();
  484. return;
  485. }
  486. // Copy whatever was painted from the front to the back.
  487. Painter painter(*m_back_bitmap);
  488. for (auto& dirty_rect : dirty_rects)
  489. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  490. m_back_bitmap->shared_buffer()->set_volatile();
  491. }
  492. NonnullRefPtr<GraphicsBitmap> GWindow::create_shared_bitmap(GraphicsBitmap::Format format, const Size& size)
  493. {
  494. ASSERT(GWindowServerConnection::the().server_pid());
  495. ASSERT(!size.is_empty());
  496. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(RGBA32), 16);
  497. size_t size_in_bytes = size.height() * pitch;
  498. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  499. ASSERT(shared_buffer);
  500. shared_buffer->share_with(GWindowServerConnection::the().server_pid());
  501. return GraphicsBitmap::create_with_shared_buffer(format, *shared_buffer, size);
  502. }
  503. NonnullRefPtr<GraphicsBitmap> GWindow::create_backing_bitmap(const Size& size)
  504. {
  505. auto format = m_has_alpha_channel ? GraphicsBitmap::Format::RGBA32 : GraphicsBitmap::Format::RGB32;
  506. return create_shared_bitmap(format, size);
  507. }
  508. void GWindow::set_modal(bool modal)
  509. {
  510. ASSERT(!m_window_id);
  511. m_modal = modal;
  512. }
  513. void GWindow::wm_event(GWMEvent&)
  514. {
  515. }
  516. void GWindow::set_icon(const GraphicsBitmap* icon)
  517. {
  518. if (m_icon == icon)
  519. return;
  520. m_icon = create_shared_bitmap(GraphicsBitmap::Format::RGBA32, icon->size());
  521. {
  522. GPainter painter(*m_icon);
  523. painter.blit({ 0, 0 }, *icon, icon->rect());
  524. }
  525. apply_icon();
  526. }
  527. void GWindow::apply_icon()
  528. {
  529. if (!m_icon)
  530. return;
  531. if (!m_window_id)
  532. return;
  533. int rc = seal_shared_buffer(m_icon->shared_buffer_id());
  534. ASSERT(rc == 0);
  535. rc = share_buffer_globally(m_icon->shared_buffer_id());
  536. ASSERT(rc == 0);
  537. static bool has_set_process_icon;
  538. if (!has_set_process_icon)
  539. set_process_icon(m_icon->shared_buffer_id());
  540. GWindowServerConnection::the().send_sync<WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->shared_buffer_id(), m_icon->size());
  541. }
  542. void GWindow::start_wm_resize()
  543. {
  544. GWindowServerConnection::the().post_message(WindowServer::WM_StartWindowResize(GWindowServerConnection::the().my_client_id(), m_window_id));
  545. }
  546. Vector<GWidget*> GWindow::focusable_widgets() const
  547. {
  548. if (!m_main_widget)
  549. return {};
  550. Vector<GWidget*> collected_widgets;
  551. Function<void(GWidget&)> collect_focusable_widgets = [&](GWidget& widget) {
  552. if (widget.accepts_focus())
  553. collected_widgets.append(&widget);
  554. widget.for_each_child_widget([&](auto& child) {
  555. if (!child.is_visible())
  556. return IterationDecision::Continue;
  557. if (!child.is_enabled())
  558. return IterationDecision::Continue;
  559. collect_focusable_widgets(child);
  560. return IterationDecision::Continue;
  561. });
  562. };
  563. collect_focusable_widgets(const_cast<GWidget&>(*m_main_widget));
  564. return collected_widgets;
  565. }
  566. void GWindow::save_to(AK::JsonObject& json)
  567. {
  568. json.set("title", title());
  569. json.set("visible", is_visible());
  570. json.set("active", is_active());
  571. json.set("resizable", is_resizable());
  572. json.set("fullscreen", is_fullscreen());
  573. json.set("rect", rect().to_string());
  574. json.set("base_size", base_size().to_string());
  575. json.set("size_increment", size_increment().to_string());
  576. CObject::save_to(json);
  577. }
  578. void GWindow::set_fullscreen(bool fullscreen)
  579. {
  580. if (m_fullscreen == fullscreen)
  581. return;
  582. m_fullscreen = fullscreen;
  583. if (!m_window_id)
  584. return;
  585. GWindowServerConnection::the().send_sync<WindowServer::SetFullscreen>(m_window_id, fullscreen);
  586. }
  587. void GWindow::schedule_relayout()
  588. {
  589. if (m_layout_pending)
  590. return;
  591. m_layout_pending = true;
  592. deferred_invoke([this](auto&) {
  593. if (main_widget())
  594. main_widget()->do_layout();
  595. update();
  596. m_layout_pending = false;
  597. });
  598. }
  599. void GWindow::update_all_windows(Badge<GWindowServerConnection>)
  600. {
  601. for (auto* window : all_windows) {
  602. window->update();
  603. }
  604. }