Window.cpp 29 KB

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