Window.cpp 33 KB

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