Window.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/HashMap.h>
  27. #include <AK/JsonObject.h>
  28. #include <AK/NeverDestroyed.h>
  29. #include <AK/SharedBuffer.h>
  30. #include <LibCore/EventLoop.h>
  31. #include <LibCore/MimeData.h>
  32. #include <LibGUI/Action.h>
  33. #include <LibGUI/Application.h>
  34. #include <LibGUI/Event.h>
  35. #include <LibGUI/Painter.h>
  36. #include <LibGUI/Widget.h>
  37. #include <LibGUI/Window.h>
  38. #include <LibGUI/WindowServerConnection.h>
  39. #include <LibGfx/Bitmap.h>
  40. #include <serenity.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. //#define UPDATE_COALESCING_DEBUG
  45. namespace GUI {
  46. static NeverDestroyed<HashTable<Window*>> all_windows;
  47. static NeverDestroyed<HashMap<int, Window*>> reified_windows;
  48. Window* Window::from_window_id(int window_id)
  49. {
  50. auto it = reified_windows->find(window_id);
  51. if (it != reified_windows->end())
  52. return (*it).value;
  53. return nullptr;
  54. }
  55. Window::Window(Core::Object* parent)
  56. : Core::Object(parent)
  57. {
  58. all_windows->set(this);
  59. m_rect_when_windowless = { 100, 400, 140, 140 };
  60. m_title_when_windowless = "GUI::Window";
  61. }
  62. Window::~Window()
  63. {
  64. all_windows->remove(this);
  65. hide();
  66. }
  67. void Window::close()
  68. {
  69. hide();
  70. }
  71. void Window::move_to_front()
  72. {
  73. if (!is_visible())
  74. return;
  75. WindowServerConnection::the().send_sync<Messages::WindowServer::MoveWindowToFront>(m_window_id);
  76. }
  77. void Window::show()
  78. {
  79. if (is_visible())
  80. return;
  81. auto* parent_window = find_parent_window();
  82. m_override_cursor = StandardCursor::None;
  83. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateWindow>(
  84. m_rect_when_windowless,
  85. m_has_alpha_channel,
  86. m_modal,
  87. m_minimizable,
  88. m_resizable,
  89. m_fullscreen,
  90. m_frameless,
  91. m_opacity_when_windowless,
  92. m_base_size,
  93. m_size_increment,
  94. (i32)m_window_type,
  95. m_title_when_windowless,
  96. parent_window ? parent_window->window_id() : 0);
  97. m_window_id = response->window_id();
  98. m_visible = true;
  99. apply_icon();
  100. reified_windows->set(m_window_id, this);
  101. Application::the()->did_create_window({});
  102. update();
  103. }
  104. Window* Window::find_parent_window()
  105. {
  106. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  107. if (ancestor->is_window())
  108. return static_cast<Window*>(ancestor);
  109. }
  110. return nullptr;
  111. }
  112. void Window::server_did_destroy()
  113. {
  114. reified_windows->remove(m_window_id);
  115. m_window_id = 0;
  116. m_visible = false;
  117. m_pending_paint_event_rects.clear();
  118. m_back_bitmap = nullptr;
  119. m_front_bitmap = nullptr;
  120. m_override_cursor = StandardCursor::None;
  121. }
  122. void Window::hide()
  123. {
  124. if (!is_visible())
  125. return;
  126. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyWindow>(m_window_id);
  127. server_did_destroy();
  128. for (auto child_window_id : response->destroyed_window_ids()) {
  129. if (auto* window = Window::from_window_id(child_window_id)) {
  130. window->server_did_destroy();
  131. }
  132. }
  133. bool app_has_visible_windows = false;
  134. for (auto& window : *all_windows) {
  135. if (window->is_visible()) {
  136. app_has_visible_windows = true;
  137. break;
  138. }
  139. }
  140. if (!app_has_visible_windows)
  141. Application::the()->did_delete_last_window({});
  142. }
  143. void Window::set_title(const StringView& title)
  144. {
  145. m_title_when_windowless = title;
  146. if (!is_visible())
  147. return;
  148. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowTitle>(m_window_id, title);
  149. }
  150. String Window::title() const
  151. {
  152. if (!is_visible())
  153. return m_title_when_windowless;
  154. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowTitle>(m_window_id)->title();
  155. }
  156. Gfx::IntRect Window::rect() const
  157. {
  158. if (!is_visible())
  159. return m_rect_when_windowless;
  160. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowRect>(m_window_id)->rect();
  161. }
  162. void Window::set_rect(const Gfx::IntRect& a_rect)
  163. {
  164. m_rect_when_windowless = a_rect;
  165. if (!is_visible()) {
  166. if (m_main_widget)
  167. m_main_widget->resize(m_rect_when_windowless.size());
  168. return;
  169. }
  170. auto window_rect = WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowRect>(m_window_id, a_rect)->rect();
  171. if (m_back_bitmap && m_back_bitmap->size() != window_rect.size())
  172. m_back_bitmap = nullptr;
  173. if (m_front_bitmap && m_front_bitmap->size() != window_rect.size())
  174. m_front_bitmap = nullptr;
  175. if (m_main_widget)
  176. m_main_widget->resize(window_rect.size());
  177. }
  178. void Window::set_window_type(WindowType window_type)
  179. {
  180. m_window_type = window_type;
  181. }
  182. void Window::set_override_cursor(StandardCursor cursor)
  183. {
  184. if (!is_visible())
  185. return;
  186. if (!m_custom_cursor && m_override_cursor == cursor)
  187. return;
  188. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOverrideCursor>(m_window_id, (u32)cursor);
  189. m_override_cursor = cursor;
  190. m_custom_cursor = nullptr;
  191. }
  192. void Window::set_override_cursor(const Gfx::Bitmap& cursor)
  193. {
  194. if (!is_visible())
  195. return;
  196. if (&cursor == m_custom_cursor.ptr())
  197. return;
  198. m_custom_cursor = &cursor;
  199. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCustomOverrideCursor>(m_window_id, m_custom_cursor->to_shareable_bitmap(WindowServerConnection::the().server_pid()));
  200. }
  201. void Window::event(Core::Event& event)
  202. {
  203. if (event.type() == Event::Drop) {
  204. auto& drop_event = static_cast<DropEvent&>(event);
  205. if (!m_main_widget)
  206. return;
  207. auto result = m_main_widget->hit_test(drop_event.position());
  208. auto local_event = make<DropEvent>(result.local_position, drop_event.text(), drop_event.mime_data());
  209. ASSERT(result.widget);
  210. return result.widget->dispatch_event(*local_event, this);
  211. }
  212. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel) {
  213. auto& mouse_event = static_cast<MouseEvent&>(event);
  214. if (m_global_cursor_tracking_widget) {
  215. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  216. Gfx::IntPoint local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  217. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  218. m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
  219. return;
  220. }
  221. if (m_automatic_cursor_tracking_widget) {
  222. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  223. Gfx::IntPoint local_point { mouse_event.x() - window_relative_rect.x(), mouse_event.y() - window_relative_rect.y() };
  224. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  225. m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
  226. if (mouse_event.buttons() == 0)
  227. m_automatic_cursor_tracking_widget = nullptr;
  228. return;
  229. }
  230. if (!m_main_widget)
  231. return;
  232. auto result = m_main_widget->hit_test(mouse_event.position());
  233. auto local_event = make<MouseEvent>((Event::Type)event.type(), result.local_position, mouse_event.buttons(), mouse_event.button(), mouse_event.modifiers(), mouse_event.wheel_delta());
  234. ASSERT(result.widget);
  235. set_hovered_widget(result.widget);
  236. if (mouse_event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  237. m_automatic_cursor_tracking_widget = result.widget->make_weak_ptr();
  238. if (result.widget != m_global_cursor_tracking_widget.ptr())
  239. return result.widget->dispatch_event(*local_event, this);
  240. return;
  241. }
  242. if (event.type() == Event::MultiPaint) {
  243. if (!is_visible())
  244. return;
  245. if (!m_main_widget)
  246. return;
  247. auto& paint_event = static_cast<MultiPaintEvent&>(event);
  248. auto rects = paint_event.rects();
  249. ASSERT(!rects.is_empty());
  250. if (m_back_bitmap && m_back_bitmap->size() != paint_event.window_size()) {
  251. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  252. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  253. // effort on painting into an undersized bitmap that will be thrown away anyway.
  254. m_back_bitmap = nullptr;
  255. }
  256. bool created_new_backing_store = !m_back_bitmap;
  257. if (!m_back_bitmap) {
  258. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  259. ASSERT(m_back_bitmap);
  260. } else if (m_double_buffering_enabled) {
  261. bool still_has_pixels = m_back_bitmap->shared_buffer()->set_nonvolatile();
  262. if (!still_has_pixels) {
  263. m_back_bitmap = create_backing_bitmap(paint_event.window_size());
  264. ASSERT(m_back_bitmap);
  265. created_new_backing_store = true;
  266. }
  267. }
  268. auto rect = rects.first();
  269. if (rect.is_empty() || created_new_backing_store) {
  270. rects.clear();
  271. rects.append({ {}, paint_event.window_size() });
  272. }
  273. for (auto& rect : rects)
  274. m_main_widget->dispatch_event(*make<PaintEvent>(rect), this);
  275. if (m_double_buffering_enabled)
  276. flip(rects);
  277. else if (created_new_backing_store)
  278. set_current_backing_bitmap(*m_back_bitmap, true);
  279. if (is_visible()) {
  280. Vector<Gfx::IntRect> rects_to_send;
  281. for (auto& r : rects)
  282. rects_to_send.append(r);
  283. WindowServerConnection::the().post_message(Messages::WindowServer::DidFinishPainting(m_window_id, rects_to_send));
  284. }
  285. return;
  286. }
  287. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown) {
  288. if (m_focused_widget)
  289. return m_focused_widget->dispatch_event(event, this);
  290. if (m_main_widget)
  291. return m_main_widget->dispatch_event(event, this);
  292. return;
  293. }
  294. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive) {
  295. m_is_active = event.type() == Event::WindowBecameActive;
  296. if (on_activity_change)
  297. on_activity_change(m_is_active);
  298. if (m_main_widget)
  299. m_main_widget->dispatch_event(event, this);
  300. if (m_focused_widget)
  301. m_focused_widget->update();
  302. return;
  303. }
  304. if (event.type() == Event::WindowCloseRequest) {
  305. if (on_close_request) {
  306. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  307. return;
  308. }
  309. close();
  310. return;
  311. }
  312. if (event.type() == Event::WindowLeft) {
  313. set_hovered_widget(nullptr);
  314. return;
  315. }
  316. if (event.type() == Event::Resize) {
  317. auto new_size = static_cast<ResizeEvent&>(event).size();
  318. if (m_back_bitmap && m_back_bitmap->size() != new_size)
  319. m_back_bitmap = nullptr;
  320. if (!m_pending_paint_event_rects.is_empty()) {
  321. m_pending_paint_event_rects.clear_with_capacity();
  322. m_pending_paint_event_rects.append({ {}, new_size });
  323. }
  324. m_rect_when_windowless = { {}, new_size };
  325. m_main_widget->set_relative_rect({ {}, new_size });
  326. return;
  327. }
  328. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  329. return wm_event(static_cast<WMEvent&>(event));
  330. if (event.type() == Event::DragMove) {
  331. if (!m_main_widget)
  332. return;
  333. auto& drag_event = static_cast<DragEvent&>(event);
  334. auto result = m_main_widget->hit_test(drag_event.position());
  335. auto local_event = make<DragEvent>(static_cast<Event::Type>(drag_event.type()), result.local_position, drag_event.data_type());
  336. ASSERT(result.widget);
  337. return result.widget->dispatch_event(*local_event, this);
  338. }
  339. if (event.type() == Event::ThemeChange) {
  340. if (!m_main_widget)
  341. return;
  342. auto theme_event = static_cast<ThemeChangeEvent&>(event);
  343. auto dispatch_theme_change = [&](auto& widget, auto recursive) {
  344. widget.dispatch_event(theme_event, this);
  345. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  346. widget.dispatch_event(theme_event, this);
  347. recursive(widget, recursive);
  348. return IterationDecision::Continue;
  349. });
  350. };
  351. dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
  352. return;
  353. }
  354. Core::Object::event(event);
  355. }
  356. bool Window::is_visible() const
  357. {
  358. return m_visible;
  359. }
  360. void Window::update()
  361. {
  362. auto rect = this->rect();
  363. update({ 0, 0, rect.width(), rect.height() });
  364. }
  365. void Window::force_update()
  366. {
  367. if (!is_visible())
  368. return;
  369. auto rect = this->rect();
  370. WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true));
  371. }
  372. void Window::update(const Gfx::IntRect& a_rect)
  373. {
  374. if (!is_visible())
  375. return;
  376. for (auto& pending_rect : m_pending_paint_event_rects) {
  377. if (pending_rect.contains(a_rect)) {
  378. #ifdef UPDATE_COALESCING_DEBUG
  379. dbgprintf("Ignoring %s since it's contained by pending rect %s\n", a_rect.to_string().characters(), pending_rect.to_string().characters());
  380. #endif
  381. return;
  382. }
  383. }
  384. if (m_pending_paint_event_rects.is_empty()) {
  385. deferred_invoke([this](auto&) {
  386. auto rects = move(m_pending_paint_event_rects);
  387. if (rects.is_empty())
  388. return;
  389. Vector<Gfx::IntRect> rects_to_send;
  390. for (auto& r : rects)
  391. rects_to_send.append(r);
  392. WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, rects_to_send, false));
  393. });
  394. }
  395. m_pending_paint_event_rects.append(a_rect);
  396. }
  397. void Window::set_main_widget(Widget* widget)
  398. {
  399. if (m_main_widget == widget)
  400. return;
  401. if (m_main_widget)
  402. remove_child(*m_main_widget);
  403. m_main_widget = widget;
  404. if (m_main_widget) {
  405. add_child(*widget);
  406. auto new_window_rect = rect();
  407. if (m_main_widget->horizontal_size_policy() == SizePolicy::Fixed)
  408. new_window_rect.set_width(m_main_widget->preferred_size().width());
  409. if (m_main_widget->vertical_size_policy() == SizePolicy::Fixed)
  410. new_window_rect.set_height(m_main_widget->preferred_size().height());
  411. set_rect(new_window_rect);
  412. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  413. m_main_widget->set_window(this);
  414. if (m_main_widget->accepts_focus())
  415. m_main_widget->set_focus(true);
  416. }
  417. update();
  418. }
  419. void Window::set_focused_widget(Widget* widget)
  420. {
  421. if (m_focused_widget == widget)
  422. return;
  423. if (m_focused_widget) {
  424. Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusOut));
  425. m_focused_widget->update();
  426. }
  427. m_focused_widget = widget ? widget->make_weak_ptr() : nullptr;
  428. if (m_focused_widget) {
  429. Core::EventLoop::current().post_event(*m_focused_widget, make<Event>(Event::FocusIn));
  430. m_focused_widget->update();
  431. }
  432. }
  433. void Window::set_global_cursor_tracking_widget(Widget* widget)
  434. {
  435. if (widget == m_global_cursor_tracking_widget)
  436. return;
  437. m_global_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  438. }
  439. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  440. {
  441. if (widget == m_automatic_cursor_tracking_widget)
  442. return;
  443. m_automatic_cursor_tracking_widget = widget ? widget->make_weak_ptr() : nullptr;
  444. }
  445. void Window::set_has_alpha_channel(bool value)
  446. {
  447. if (m_has_alpha_channel == value)
  448. return;
  449. m_has_alpha_channel = value;
  450. if (!is_visible())
  451. return;
  452. m_pending_paint_event_rects.clear();
  453. m_back_bitmap = nullptr;
  454. m_front_bitmap = nullptr;
  455. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  456. update();
  457. }
  458. void Window::set_double_buffering_enabled(bool value)
  459. {
  460. ASSERT(!is_visible());
  461. m_double_buffering_enabled = value;
  462. }
  463. void Window::set_opacity(float opacity)
  464. {
  465. m_opacity_when_windowless = opacity;
  466. if (!is_visible())
  467. return;
  468. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOpacity>(m_window_id, opacity);
  469. }
  470. void Window::set_hovered_widget(Widget* widget)
  471. {
  472. if (widget == m_hovered_widget)
  473. return;
  474. if (m_hovered_widget)
  475. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  476. m_hovered_widget = widget ? widget->make_weak_ptr() : nullptr;
  477. if (m_hovered_widget)
  478. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  479. }
  480. void Window::set_current_backing_bitmap(Gfx::Bitmap& bitmap, bool flush_immediately)
  481. {
  482. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.shbuf_id(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  483. }
  484. void Window::flip(const Vector<Gfx::IntRect, 32>& dirty_rects)
  485. {
  486. swap(m_front_bitmap, m_back_bitmap);
  487. set_current_backing_bitmap(*m_front_bitmap);
  488. if (!m_back_bitmap || m_back_bitmap->size() != m_front_bitmap->size()) {
  489. m_back_bitmap = create_backing_bitmap(m_front_bitmap->size());
  490. ASSERT(m_back_bitmap);
  491. memcpy(m_back_bitmap->scanline(0), m_front_bitmap->scanline(0), m_front_bitmap->size_in_bytes());
  492. m_back_bitmap->shared_buffer()->set_volatile();
  493. return;
  494. }
  495. // Copy whatever was painted from the front to the back.
  496. Painter painter(*m_back_bitmap);
  497. for (auto& dirty_rect : dirty_rects)
  498. painter.blit(dirty_rect.location(), *m_front_bitmap, dirty_rect);
  499. m_back_bitmap->shared_buffer()->set_volatile();
  500. }
  501. RefPtr<Gfx::Bitmap> Window::create_shared_bitmap(Gfx::BitmapFormat format, const Gfx::IntSize& size)
  502. {
  503. ASSERT(WindowServerConnection::the().server_pid());
  504. ASSERT(!size.is_empty());
  505. size_t pitch = round_up_to_power_of_two(size.width() * sizeof(Gfx::RGBA32), 16);
  506. size_t size_in_bytes = size.height() * pitch;
  507. auto shared_buffer = SharedBuffer::create_with_size(size_in_bytes);
  508. ASSERT(shared_buffer);
  509. shared_buffer->share_with(WindowServerConnection::the().server_pid());
  510. return Gfx::Bitmap::create_with_shared_buffer(format, *shared_buffer, size);
  511. }
  512. RefPtr<Gfx::Bitmap> Window::create_backing_bitmap(const Gfx::IntSize& size)
  513. {
  514. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::RGBA32 : Gfx::BitmapFormat::RGB32;
  515. return create_shared_bitmap(format, size);
  516. }
  517. void Window::set_modal(bool modal)
  518. {
  519. ASSERT(!is_visible());
  520. m_modal = modal;
  521. }
  522. void Window::wm_event(WMEvent&)
  523. {
  524. }
  525. void Window::set_icon(const Gfx::Bitmap* icon)
  526. {
  527. if (m_icon == icon)
  528. return;
  529. m_icon = create_shared_bitmap(Gfx::BitmapFormat::RGBA32, icon->size());
  530. ASSERT(m_icon);
  531. {
  532. Painter painter(*m_icon);
  533. painter.blit({ 0, 0 }, *icon, icon->rect());
  534. }
  535. apply_icon();
  536. }
  537. void Window::apply_icon()
  538. {
  539. if (!m_icon)
  540. return;
  541. if (!is_visible())
  542. return;
  543. int rc = shbuf_seal(m_icon->shbuf_id());
  544. ASSERT(rc == 0);
  545. rc = shbuf_allow_all(m_icon->shbuf_id());
  546. ASSERT(rc == 0);
  547. static bool has_set_process_icon;
  548. if (!has_set_process_icon)
  549. set_process_icon(m_icon->shbuf_id());
  550. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->to_shareable_bitmap(WindowServerConnection::the().server_pid()));
  551. }
  552. void Window::start_wm_resize()
  553. {
  554. WindowServerConnection::the().post_message(Messages::WindowServer::WM_StartWindowResize(WindowServerConnection::the().my_client_id(), m_window_id));
  555. }
  556. Vector<Widget*> Window::focusable_widgets() const
  557. {
  558. if (!m_main_widget)
  559. return {};
  560. Vector<Widget*> collected_widgets;
  561. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  562. if (widget.accepts_focus())
  563. collected_widgets.append(&widget);
  564. widget.for_each_child_widget([&](auto& child) {
  565. if (!child.is_visible())
  566. return IterationDecision::Continue;
  567. if (!child.is_enabled())
  568. return IterationDecision::Continue;
  569. collect_focusable_widgets(child);
  570. return IterationDecision::Continue;
  571. });
  572. };
  573. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  574. return collected_widgets;
  575. }
  576. void Window::save_to(AK::JsonObject& json)
  577. {
  578. json.set("title", title());
  579. json.set("visible", is_visible());
  580. json.set("active", is_active());
  581. json.set("minimizable", is_minimizable());
  582. json.set("resizable", is_resizable());
  583. json.set("fullscreen", is_fullscreen());
  584. json.set("rect", rect().to_string());
  585. json.set("base_size", base_size().to_string());
  586. json.set("size_increment", size_increment().to_string());
  587. Core::Object::save_to(json);
  588. }
  589. void Window::set_fullscreen(bool fullscreen)
  590. {
  591. if (m_fullscreen == fullscreen)
  592. return;
  593. m_fullscreen = fullscreen;
  594. if (!is_visible())
  595. return;
  596. WindowServerConnection::the().send_sync<Messages::WindowServer::SetFullscreen>(m_window_id, fullscreen);
  597. }
  598. bool Window::is_maximized() const
  599. {
  600. if (!is_visible())
  601. return false;
  602. return WindowServerConnection::the().send_sync<Messages::WindowServer::IsMaximized>(m_window_id)->maximized();
  603. }
  604. void Window::schedule_relayout()
  605. {
  606. if (m_layout_pending)
  607. return;
  608. m_layout_pending = true;
  609. deferred_invoke([this](auto&) {
  610. if (main_widget())
  611. main_widget()->do_layout();
  612. update();
  613. m_layout_pending = false;
  614. });
  615. }
  616. void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
  617. {
  618. for (auto& e : *reified_windows) {
  619. ASSERT(e.value);
  620. callback(*e.value);
  621. }
  622. }
  623. void Window::update_all_windows(Badge<WindowServerConnection>)
  624. {
  625. for (auto& e : *reified_windows) {
  626. e.value->force_update();
  627. }
  628. }
  629. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  630. {
  631. m_visible_for_timer_purposes = !minimized && !occluded;
  632. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  633. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  634. RefPtr<Gfx::Bitmap>& bitmap = m_double_buffering_enabled ? m_front_bitmap : m_back_bitmap;
  635. if (!bitmap)
  636. return;
  637. if (minimized || occluded) {
  638. bitmap->shared_buffer()->set_volatile();
  639. } else {
  640. if (!bitmap->shared_buffer()->set_nonvolatile()) {
  641. bitmap = nullptr;
  642. update();
  643. }
  644. }
  645. }
  646. Action* Window::action_for_key_event(const KeyEvent& event)
  647. {
  648. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  649. Action* found_action = nullptr;
  650. for_each_child_of_type<Action>([&](auto& action) {
  651. if (action.shortcut() == shortcut) {
  652. found_action = &action;
  653. return IterationDecision::Break;
  654. }
  655. return IterationDecision::Continue;
  656. });
  657. return found_action;
  658. }
  659. void Window::set_base_size(const Gfx::IntSize& base_size)
  660. {
  661. if (m_base_size == base_size)
  662. return;
  663. m_base_size = base_size;
  664. if (is_visible())
  665. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
  666. }
  667. void Window::set_size_increment(const Gfx::IntSize& size_increment)
  668. {
  669. if (m_size_increment == size_increment)
  670. return;
  671. m_size_increment = size_increment;
  672. if (is_visible())
  673. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
  674. }
  675. void Window::did_add_widget(Badge<Widget>, Widget& widget)
  676. {
  677. if (!m_focused_widget && widget.accepts_focus())
  678. set_focused_widget(&widget);
  679. }
  680. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  681. {
  682. if (m_focused_widget == &widget)
  683. m_focused_widget = nullptr;
  684. if (m_hovered_widget == &widget)
  685. m_hovered_widget = nullptr;
  686. if (m_global_cursor_tracking_widget == &widget)
  687. m_global_cursor_tracking_widget = nullptr;
  688. if (m_automatic_cursor_tracking_widget == &widget)
  689. m_automatic_cursor_tracking_widget = nullptr;
  690. }
  691. void Window::set_progress(int progress)
  692. {
  693. ASSERT(m_window_id);
  694. WindowServerConnection::the().post_message(Messages::WindowServer::SetWindowProgress(m_window_id, progress));
  695. }
  696. }