GWindow.cpp 25 KB

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