Window.cpp 34 KB

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