Window.cpp 25 KB

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