Window.cpp 25 KB

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