Window.cpp 32 KB

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