Window.cpp 34 KB

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