Window.cpp 34 KB

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