Window.cpp 28 KB

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