Window.cpp 34 KB

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