Window.cpp 27 KB

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