Window.cpp 31 KB

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