Window.cpp 25 KB

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