GWindow.cpp 25 KB

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