Window.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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 (previously_focused_widget) {
  566. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  567. previously_focused_widget->update();
  568. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  569. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  570. }
  571. if (m_focused_widget) {
  572. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  573. m_focused_widget->update();
  574. if (m_focused_widget && m_focused_widget->on_focus_change)
  575. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  576. }
  577. }
  578. void Window::set_global_cursor_tracking_widget(Widget* widget)
  579. {
  580. if (widget == m_global_cursor_tracking_widget)
  581. return;
  582. m_global_cursor_tracking_widget = widget;
  583. }
  584. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  585. {
  586. if (widget == m_automatic_cursor_tracking_widget)
  587. return;
  588. m_automatic_cursor_tracking_widget = widget;
  589. }
  590. void Window::set_has_alpha_channel(bool value)
  591. {
  592. if (m_has_alpha_channel == value)
  593. return;
  594. m_has_alpha_channel = value;
  595. if (!is_visible())
  596. return;
  597. m_pending_paint_event_rects.clear();
  598. m_back_store = nullptr;
  599. m_front_store = nullptr;
  600. WindowServerConnection::the().async_set_window_has_alpha_channel(m_window_id, value);
  601. update();
  602. }
  603. void Window::set_double_buffering_enabled(bool value)
  604. {
  605. VERIFY(!is_visible());
  606. m_double_buffering_enabled = value;
  607. }
  608. void Window::set_opacity(float opacity)
  609. {
  610. m_opacity_when_windowless = opacity;
  611. if (!is_visible())
  612. return;
  613. WindowServerConnection::the().async_set_window_opacity(m_window_id, opacity);
  614. }
  615. void Window::set_alpha_hit_threshold(float threshold)
  616. {
  617. if (threshold < 0.0f)
  618. threshold = 0.0f;
  619. else if (threshold > 1.0f)
  620. threshold = 1.0f;
  621. if (m_alpha_hit_threshold == threshold)
  622. return;
  623. m_alpha_hit_threshold = threshold;
  624. if (!is_visible())
  625. return;
  626. WindowServerConnection::the().async_set_window_alpha_hit_threshold(m_window_id, threshold);
  627. }
  628. void Window::set_hovered_widget(Widget* widget)
  629. {
  630. if (widget == m_hovered_widget)
  631. return;
  632. if (m_hovered_widget)
  633. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  634. m_hovered_widget = widget;
  635. if (m_hovered_widget)
  636. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  637. }
  638. void Window::set_current_backing_store(WindowBackingStore& backing_store, bool flush_immediately)
  639. {
  640. auto& bitmap = backing_store.bitmap();
  641. 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);
  642. }
  643. void Window::flip(const Vector<Gfx::IntRect, 32>& dirty_rects)
  644. {
  645. swap(m_front_store, m_back_store);
  646. set_current_backing_store(*m_front_store);
  647. if (!m_back_store || m_back_store->size() != m_front_store->size()) {
  648. m_back_store = create_backing_store(m_front_store->size());
  649. VERIFY(m_back_store);
  650. memcpy(m_back_store->bitmap().scanline(0), m_front_store->bitmap().scanline(0), m_front_store->bitmap().size_in_bytes());
  651. m_back_store->bitmap().set_volatile();
  652. return;
  653. }
  654. // Copy whatever was painted from the front to the back.
  655. Painter painter(m_back_store->bitmap());
  656. for (auto& dirty_rect : dirty_rects)
  657. painter.blit(dirty_rect.location(), m_front_store->bitmap(), dirty_rect, 1.0f, false);
  658. m_back_store->bitmap().set_volatile();
  659. }
  660. OwnPtr<WindowBackingStore> Window::create_backing_store(const Gfx::IntSize& size)
  661. {
  662. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  663. VERIFY(!size.is_empty());
  664. size_t pitch = Gfx::Bitmap::minimum_pitch(size.width(), format);
  665. size_t size_in_bytes = size.height() * pitch;
  666. auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes, PAGE_SIZE));
  667. if (!buffer.is_valid()) {
  668. perror("anon_create");
  669. return {};
  670. }
  671. // FIXME: Plumb scale factor here eventually.
  672. auto bitmap = Gfx::Bitmap::create_with_anonymous_buffer(format, buffer, size, 1, {});
  673. if (!bitmap)
  674. return {};
  675. return make<WindowBackingStore>(bitmap.release_nonnull());
  676. }
  677. void Window::set_modal(bool modal)
  678. {
  679. VERIFY(!is_visible());
  680. m_modal = modal;
  681. }
  682. void Window::wm_event(WMEvent&)
  683. {
  684. }
  685. void Window::screen_rect_change_event(ScreenRectChangeEvent&)
  686. {
  687. }
  688. void Window::set_icon(const Gfx::Bitmap* icon)
  689. {
  690. if (m_icon == icon)
  691. return;
  692. Gfx::IntSize icon_size = icon ? icon->size() : Gfx::IntSize(16, 16);
  693. m_icon = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, icon_size);
  694. VERIFY(m_icon);
  695. if (icon) {
  696. Painter painter(*m_icon);
  697. painter.blit({ 0, 0 }, *icon, icon->rect());
  698. }
  699. apply_icon();
  700. }
  701. void Window::apply_icon()
  702. {
  703. if (!m_icon)
  704. return;
  705. if (!is_visible())
  706. return;
  707. WindowServerConnection::the().async_set_window_icon_bitmap(m_window_id, m_icon->to_shareable_bitmap());
  708. }
  709. void Window::start_interactive_resize()
  710. {
  711. WindowServerConnection::the().async_start_window_resize(m_window_id);
  712. }
  713. Vector<Widget*> Window::focusable_widgets(FocusSource source) const
  714. {
  715. if (!m_main_widget)
  716. return {};
  717. HashTable<Widget*> seen_widgets;
  718. Vector<Widget*> collected_widgets;
  719. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  720. bool widget_accepts_focus = false;
  721. switch (source) {
  722. case FocusSource::Keyboard:
  723. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::TabFocus);
  724. break;
  725. case FocusSource::Mouse:
  726. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::ClickFocus);
  727. break;
  728. case FocusSource::Programmatic:
  729. widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
  730. break;
  731. }
  732. if (widget_accepts_focus) {
  733. auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
  734. if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
  735. collected_widgets.append(&effective_focus_widget);
  736. }
  737. widget.for_each_child_widget([&](auto& child) {
  738. if (!child.is_visible())
  739. return IterationDecision::Continue;
  740. if (!child.is_enabled())
  741. return IterationDecision::Continue;
  742. collect_focusable_widgets(child);
  743. return IterationDecision::Continue;
  744. });
  745. };
  746. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  747. return collected_widgets;
  748. }
  749. void Window::set_fullscreen(bool fullscreen)
  750. {
  751. if (m_fullscreen == fullscreen)
  752. return;
  753. m_fullscreen = fullscreen;
  754. if (!is_visible())
  755. return;
  756. WindowServerConnection::the().set_fullscreen(m_window_id, fullscreen);
  757. }
  758. void Window::set_frameless(bool frameless)
  759. {
  760. if (m_frameless == frameless)
  761. return;
  762. m_frameless = frameless;
  763. if (!is_visible())
  764. return;
  765. WindowServerConnection::the().set_frameless(m_window_id, frameless);
  766. if (!frameless)
  767. apply_icon();
  768. }
  769. bool Window::is_maximized() const
  770. {
  771. if (!is_visible())
  772. return false;
  773. return WindowServerConnection::the().is_maximized(m_window_id);
  774. }
  775. void Window::schedule_relayout()
  776. {
  777. if (m_layout_pending)
  778. return;
  779. m_layout_pending = true;
  780. deferred_invoke([this](auto&) {
  781. if (main_widget())
  782. main_widget()->do_layout();
  783. update();
  784. m_layout_pending = false;
  785. });
  786. }
  787. void Window::refresh_system_theme()
  788. {
  789. WindowServerConnection::the().async_refresh_system_theme();
  790. }
  791. void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
  792. {
  793. for (auto& e : *reified_windows) {
  794. VERIFY(e.value);
  795. callback(*e.value);
  796. }
  797. }
  798. void Window::update_all_windows(Badge<WindowServerConnection>)
  799. {
  800. for (auto& e : *reified_windows) {
  801. e.value->force_update();
  802. }
  803. }
  804. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  805. {
  806. m_visible_for_timer_purposes = !minimized && !occluded;
  807. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  808. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  809. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  810. if (!store)
  811. return;
  812. if (minimized || occluded) {
  813. store->bitmap().set_volatile();
  814. } else {
  815. if (!store->bitmap().set_nonvolatile()) {
  816. store = nullptr;
  817. update();
  818. }
  819. }
  820. }
  821. Action* Window::action_for_key_event(const KeyEvent& event)
  822. {
  823. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  824. Action* found_action = nullptr;
  825. for_each_child_of_type<Action>([&](auto& action) {
  826. if (action.shortcut() == shortcut) {
  827. found_action = &action;
  828. return IterationDecision::Break;
  829. }
  830. return IterationDecision::Continue;
  831. });
  832. return found_action;
  833. }
  834. void Window::set_base_size(const Gfx::IntSize& base_size)
  835. {
  836. if (m_base_size == base_size)
  837. return;
  838. m_base_size = base_size;
  839. if (is_visible())
  840. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  841. }
  842. void Window::set_size_increment(const Gfx::IntSize& size_increment)
  843. {
  844. if (m_size_increment == size_increment)
  845. return;
  846. m_size_increment = size_increment;
  847. if (is_visible())
  848. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  849. }
  850. void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio)
  851. {
  852. if (m_resize_aspect_ratio == ratio)
  853. return;
  854. m_resize_aspect_ratio = ratio;
  855. if (is_visible())
  856. WindowServerConnection::the().async_set_window_resize_aspect_ratio(m_window_id, m_resize_aspect_ratio);
  857. }
  858. void Window::did_add_widget(Badge<Widget>, Widget&)
  859. {
  860. if (!m_focused_widget)
  861. focus_a_widget_if_possible(FocusSource::Mouse);
  862. }
  863. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  864. {
  865. if (m_focused_widget == &widget)
  866. m_focused_widget = nullptr;
  867. if (m_hovered_widget == &widget)
  868. m_hovered_widget = nullptr;
  869. if (m_global_cursor_tracking_widget == &widget)
  870. m_global_cursor_tracking_widget = nullptr;
  871. if (m_automatic_cursor_tracking_widget == &widget)
  872. m_automatic_cursor_tracking_widget = nullptr;
  873. }
  874. void Window::set_progress(Optional<int> progress)
  875. {
  876. VERIFY(m_window_id);
  877. WindowServerConnection::the().async_set_window_progress(m_window_id, progress);
  878. }
  879. void Window::update_cursor()
  880. {
  881. Gfx::StandardCursor new_cursor;
  882. if (m_hovered_widget && m_hovered_widget->override_cursor() != Gfx::StandardCursor::None)
  883. new_cursor = m_hovered_widget->override_cursor();
  884. else
  885. new_cursor = m_cursor;
  886. if (m_effective_cursor == new_cursor)
  887. return;
  888. m_effective_cursor = new_cursor;
  889. if (m_custom_cursor)
  890. WindowServerConnection::the().async_set_window_custom_cursor(m_window_id, m_custom_cursor->to_shareable_bitmap());
  891. else
  892. WindowServerConnection::the().async_set_window_cursor(m_window_id, (u32)m_effective_cursor);
  893. }
  894. void Window::focus_a_widget_if_possible(FocusSource source)
  895. {
  896. auto focusable_widgets = this->focusable_widgets(source);
  897. if (!focusable_widgets.is_empty())
  898. set_focused_widget(focusable_widgets[0], source);
  899. }
  900. void Window::did_disable_focused_widget(Badge<Widget>)
  901. {
  902. focus_a_widget_if_possible(FocusSource::Mouse);
  903. }
  904. bool Window::is_active() const
  905. {
  906. VERIFY(Application::the());
  907. return this == Application::the()->active_window();
  908. }
  909. Gfx::Bitmap* Window::back_bitmap()
  910. {
  911. return m_back_store ? &m_back_store->bitmap() : nullptr;
  912. }
  913. void Window::set_menubar(RefPtr<Menubar> menubar)
  914. {
  915. if (m_menubar == menubar)
  916. return;
  917. if (m_menubar)
  918. m_menubar->notify_removed_from_window({});
  919. m_menubar = move(menubar);
  920. if (m_window_id && m_menubar) {
  921. m_menubar->notify_added_to_window({});
  922. WindowServerConnection::the().async_set_window_menubar(m_window_id, m_menubar->menubar_id());
  923. }
  924. }
  925. bool Window::is_modified() const
  926. {
  927. if (!m_window_id)
  928. return false;
  929. return WindowServerConnection::the().is_window_modified(m_window_id);
  930. }
  931. void Window::set_modified(bool modified)
  932. {
  933. if (!m_window_id)
  934. return;
  935. WindowServerConnection::the().async_set_window_modified(m_window_id, modified);
  936. }
  937. }