GWindow.cpp 26 KB

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