Window.cpp 33 KB

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