Window.cpp 27 KB

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