Window.cpp 27 KB

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