Window.cpp 31 KB

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