Window.cpp 34 KB

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