Window.cpp 31 KB

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