Window.cpp 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. , m_menubar(Menubar::construct())
  59. {
  60. all_windows->set(this);
  61. m_rect_when_windowless = { -5000, -5000, 140, 140 };
  62. m_title_when_windowless = "GUI::Window";
  63. register_property(
  64. "title",
  65. [this] { return title(); },
  66. [this](auto& value) {
  67. set_title(value.to_string());
  68. return true;
  69. });
  70. register_property("visible", [this] { return is_visible(); });
  71. register_property("active", [this] { return is_active(); });
  72. REGISTER_BOOL_PROPERTY("minimizable", is_minimizable, set_minimizable);
  73. REGISTER_BOOL_PROPERTY("resizable", is_resizable, set_resizable);
  74. REGISTER_BOOL_PROPERTY("fullscreen", is_fullscreen, set_fullscreen);
  75. REGISTER_RECT_PROPERTY("rect", rect, set_rect);
  76. REGISTER_SIZE_PROPERTY("base_size", base_size, set_base_size);
  77. REGISTER_SIZE_PROPERTY("size_increment", size_increment, set_size_increment);
  78. }
  79. Window::~Window()
  80. {
  81. all_windows->remove(this);
  82. hide();
  83. }
  84. void Window::close()
  85. {
  86. hide();
  87. if (on_close)
  88. on_close();
  89. }
  90. void Window::move_to_front()
  91. {
  92. if (!is_visible())
  93. return;
  94. WindowServerConnection::the().async_move_window_to_front(m_window_id);
  95. }
  96. void Window::show()
  97. {
  98. if (is_visible())
  99. return;
  100. auto* parent_window = find_parent_window();
  101. m_window_id = s_window_id_allocator.allocate();
  102. Gfx::IntRect launch_origin_rect;
  103. if (auto* launch_origin_rect_string = getenv("__libgui_launch_origin_rect")) {
  104. auto parts = StringView(launch_origin_rect_string).split_view(',');
  105. if (parts.size() == 4) {
  106. launch_origin_rect = Gfx::IntRect {
  107. parts[0].to_int().value_or(0),
  108. parts[1].to_int().value_or(0),
  109. parts[2].to_int().value_or(0),
  110. parts[3].to_int().value_or(0),
  111. };
  112. }
  113. unsetenv("__libgui_launch_origin_rect");
  114. }
  115. WindowServerConnection::the().async_create_window(
  116. m_window_id,
  117. m_rect_when_windowless,
  118. !m_moved_by_client,
  119. m_has_alpha_channel,
  120. m_modal,
  121. m_minimizable,
  122. m_resizable,
  123. m_fullscreen,
  124. m_frameless,
  125. m_forced_shadow,
  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. m_menubar->for_each_menu([&](Menu& menu) {
  140. menu.realize_menu_if_needed();
  141. WindowServerConnection::the().async_add_menu(m_window_id, menu.menu_id());
  142. return IterationDecision::Continue;
  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. set_rect(rect().centered_within(Desktop::the().rect()));
  248. }
  249. void Window::center_within(const Window& other)
  250. {
  251. if (this == &other)
  252. return;
  253. set_rect(rect().centered_within(other.rect()));
  254. }
  255. void Window::set_window_type(WindowType window_type)
  256. {
  257. m_window_type = window_type;
  258. if (!m_minimum_size_modified) {
  259. // Apply minimum size defaults.
  260. if (m_window_type == WindowType::Normal || m_window_type == WindowType::ToolWindow)
  261. m_minimum_size_when_windowless = { 50, 50 };
  262. else
  263. m_minimum_size_when_windowless = { 1, 1 };
  264. }
  265. }
  266. void Window::make_window_manager(unsigned event_mask)
  267. {
  268. GUI::WindowManagerServerConnection::the().async_set_event_mask(event_mask);
  269. GUI::WindowManagerServerConnection::the().async_set_manager_window(m_window_id);
  270. }
  271. void Window::set_cursor(Gfx::StandardCursor cursor)
  272. {
  273. if (m_cursor == cursor)
  274. return;
  275. m_cursor = cursor;
  276. m_custom_cursor = nullptr;
  277. update_cursor();
  278. }
  279. void Window::set_cursor(const Gfx::Bitmap& cursor)
  280. {
  281. if (m_custom_cursor == &cursor)
  282. return;
  283. m_cursor = Gfx::StandardCursor::None;
  284. m_custom_cursor = &cursor;
  285. update_cursor();
  286. }
  287. void Window::handle_drop_event(DropEvent& event)
  288. {
  289. if (!m_main_widget)
  290. return;
  291. auto result = m_main_widget->hit_test(event.position());
  292. auto local_event = make<DropEvent>(result.local_position, event.text(), event.mime_data());
  293. VERIFY(result.widget);
  294. result.widget->dispatch_event(*local_event, this);
  295. Application::the()->set_drag_hovered_widget({}, nullptr);
  296. }
  297. void Window::handle_mouse_event(MouseEvent& event)
  298. {
  299. if (m_global_cursor_tracking_widget) {
  300. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  301. Gfx::IntPoint local_point { event.x() - window_relative_rect.x(), event.y() - window_relative_rect.y() };
  302. auto local_event = MouseEvent((Event::Type)event.type(), local_point, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  303. m_global_cursor_tracking_widget->dispatch_event(local_event, this);
  304. return;
  305. }
  306. if (m_automatic_cursor_tracking_widget) {
  307. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  308. Gfx::IntPoint local_point { event.x() - window_relative_rect.x(), event.y() - window_relative_rect.y() };
  309. auto local_event = MouseEvent((Event::Type)event.type(), local_point, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  310. m_automatic_cursor_tracking_widget->dispatch_event(local_event, this);
  311. if (event.buttons() == 0)
  312. m_automatic_cursor_tracking_widget = nullptr;
  313. return;
  314. }
  315. if (!m_main_widget)
  316. return;
  317. auto result = m_main_widget->hit_test(event.position());
  318. auto local_event = MouseEvent((Event::Type)event.type(), result.local_position, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  319. VERIFY(result.widget);
  320. set_hovered_widget(result.widget);
  321. if (event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  322. m_automatic_cursor_tracking_widget = *result.widget;
  323. if (result.widget != m_global_cursor_tracking_widget.ptr())
  324. result.widget->dispatch_event(local_event, this);
  325. if (!m_pending_paint_event_rects.is_empty()) {
  326. MultiPaintEvent paint_event(move(m_pending_paint_event_rects), size());
  327. handle_multi_paint_event(paint_event);
  328. }
  329. }
  330. void Window::handle_multi_paint_event(MultiPaintEvent& event)
  331. {
  332. if (!is_visible())
  333. return;
  334. if (!m_main_widget)
  335. return;
  336. auto rects = event.rects();
  337. if (!m_pending_paint_event_rects.is_empty()) {
  338. // It's possible that there had been some calls to update() that
  339. // haven't been flushed. We can handle these right now, avoiding
  340. // another round trip.
  341. rects.extend(move(m_pending_paint_event_rects));
  342. }
  343. VERIFY(!rects.is_empty());
  344. if (m_back_store && m_back_store->size() != event.window_size()) {
  345. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  346. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  347. // effort on painting into an undersized bitmap that will be thrown away anyway.
  348. m_back_store = nullptr;
  349. }
  350. bool created_new_backing_store = !m_back_store;
  351. if (!m_back_store) {
  352. m_back_store = create_backing_store(event.window_size());
  353. VERIFY(m_back_store);
  354. } else if (m_double_buffering_enabled) {
  355. bool was_purged = false;
  356. bool bitmap_has_memory = m_back_store->bitmap().set_nonvolatile(was_purged);
  357. if (!bitmap_has_memory) {
  358. // We didn't have enough memory to make the bitmap non-volatile!
  359. // Fall back to single-buffered mode for this window.
  360. // FIXME: Once we have a way to listen for system memory pressure notifications,
  361. // it would be cool to transition back into double-buffered mode once
  362. // the coast is clear.
  363. dbgln("Not enough memory to make backing store non-volatile. Falling back to single-buffered mode.");
  364. m_double_buffering_enabled = false;
  365. m_back_store = move(m_front_store);
  366. created_new_backing_store = true;
  367. } else if (was_purged) {
  368. // The backing store bitmap was cleared, but it does have memory.
  369. // Act as if it's a new backing store so the entire window gets repainted.
  370. created_new_backing_store = true;
  371. }
  372. }
  373. auto rect = rects.first();
  374. if (rect.is_empty() || created_new_backing_store) {
  375. rects.clear();
  376. rects.append({ {}, event.window_size() });
  377. }
  378. for (auto& rect : rects) {
  379. PaintEvent paint_event(rect);
  380. m_main_widget->dispatch_event(paint_event, this);
  381. }
  382. if (m_double_buffering_enabled)
  383. flip(rects);
  384. else if (created_new_backing_store)
  385. set_current_backing_store(*m_back_store, true);
  386. if (is_visible())
  387. WindowServerConnection::the().async_did_finish_painting(m_window_id, rects);
  388. }
  389. void Window::handle_key_event(KeyEvent& event)
  390. {
  391. if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
  392. focus_a_widget_if_possible(FocusSource::Keyboard);
  393. }
  394. if (m_focused_widget)
  395. return m_focused_widget->dispatch_event(event, this);
  396. if (m_main_widget)
  397. return m_main_widget->dispatch_event(event, this);
  398. }
  399. void Window::handle_resize_event(ResizeEvent& event)
  400. {
  401. auto new_size = event.size();
  402. if (m_back_store && m_back_store->size() != new_size)
  403. m_back_store = nullptr;
  404. if (!m_pending_paint_event_rects.is_empty()) {
  405. m_pending_paint_event_rects.clear_with_capacity();
  406. m_pending_paint_event_rects.append({ {}, new_size });
  407. }
  408. m_rect_when_windowless = { {}, new_size };
  409. if (m_main_widget)
  410. m_main_widget->set_relative_rect({ {}, new_size });
  411. }
  412. void Window::handle_input_entered_or_left_event(Core::Event& event)
  413. {
  414. m_is_active_input = event.type() == Event::WindowInputEntered;
  415. if (on_active_input_change)
  416. on_active_input_change(m_is_active_input);
  417. if (m_main_widget)
  418. m_main_widget->dispatch_event(event, this);
  419. if (m_focused_widget)
  420. m_focused_widget->update();
  421. }
  422. void Window::handle_became_active_or_inactive_event(Core::Event& event)
  423. {
  424. if (event.type() == Event::WindowBecameActive)
  425. Application::the()->window_did_become_active({}, *this);
  426. else
  427. Application::the()->window_did_become_inactive({}, *this);
  428. if (on_active_window_change)
  429. on_active_window_change(event.type() == Event::WindowBecameActive);
  430. if (m_main_widget)
  431. m_main_widget->dispatch_event(event, this);
  432. if (m_focused_widget)
  433. m_focused_widget->update();
  434. }
  435. void Window::handle_close_request()
  436. {
  437. if (on_close_request) {
  438. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  439. return;
  440. }
  441. close();
  442. }
  443. void Window::handle_theme_change_event(ThemeChangeEvent& event)
  444. {
  445. if (!m_main_widget)
  446. return;
  447. auto dispatch_theme_change = [&](auto& widget, auto recursive) {
  448. widget.dispatch_event(event, this);
  449. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  450. widget.dispatch_event(event, this);
  451. recursive(widget, recursive);
  452. return IterationDecision::Continue;
  453. });
  454. };
  455. dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
  456. }
  457. void Window::handle_fonts_change_event(FontsChangeEvent& event)
  458. {
  459. if (!m_main_widget)
  460. return;
  461. auto dispatch_fonts_change = [&](auto& widget, auto recursive) {
  462. widget.dispatch_event(event, this);
  463. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  464. widget.dispatch_event(event, this);
  465. recursive(widget, recursive);
  466. return IterationDecision::Continue;
  467. });
  468. };
  469. dispatch_fonts_change(*m_main_widget.ptr(), dispatch_fonts_change);
  470. }
  471. void Window::handle_screen_rects_change_event(ScreenRectsChangeEvent& event)
  472. {
  473. if (!m_main_widget)
  474. return;
  475. auto dispatch_screen_rects_change = [&](auto& widget, auto recursive) {
  476. widget.dispatch_event(event, this);
  477. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  478. widget.dispatch_event(event, this);
  479. recursive(widget, recursive);
  480. return IterationDecision::Continue;
  481. });
  482. };
  483. dispatch_screen_rects_change(*m_main_widget.ptr(), dispatch_screen_rects_change);
  484. screen_rects_change_event(event);
  485. }
  486. void Window::handle_drag_move_event(DragEvent& event)
  487. {
  488. if (!m_main_widget)
  489. return;
  490. auto result = m_main_widget->hit_test(event.position());
  491. VERIFY(result.widget);
  492. Application::the()->set_drag_hovered_widget({}, result.widget, result.local_position, event.mime_types());
  493. // NOTE: Setting the drag hovered widget may have executed arbitrary code, so re-check that the widget is still there.
  494. if (!result.widget)
  495. return;
  496. if (result.widget->has_pending_drop()) {
  497. DragEvent drag_move_event(static_cast<Event::Type>(event.type()), result.local_position, event.mime_types());
  498. result.widget->dispatch_event(drag_move_event, this);
  499. }
  500. }
  501. void Window::enter_event(Core::Event&)
  502. {
  503. }
  504. void Window::leave_event(Core::Event&)
  505. {
  506. }
  507. void Window::handle_entered_event(Core::Event& event)
  508. {
  509. enter_event(event);
  510. }
  511. void Window::handle_left_event(Core::Event& event)
  512. {
  513. set_hovered_widget(nullptr);
  514. Application::the()->set_drag_hovered_widget({}, nullptr);
  515. leave_event(event);
  516. }
  517. void Window::event(Core::Event& event)
  518. {
  519. ScopeGuard guard([&] {
  520. // Accept the event so it doesn't bubble up to parent windows!
  521. event.accept();
  522. });
  523. if (event.type() == Event::Drop)
  524. return handle_drop_event(static_cast<DropEvent&>(event));
  525. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel)
  526. return handle_mouse_event(static_cast<MouseEvent&>(event));
  527. if (event.type() == Event::MultiPaint)
  528. return handle_multi_paint_event(static_cast<MultiPaintEvent&>(event));
  529. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown)
  530. return handle_key_event(static_cast<KeyEvent&>(event));
  531. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
  532. return handle_became_active_or_inactive_event(event);
  533. if (event.type() == Event::WindowInputEntered || event.type() == Event::WindowInputLeft)
  534. return handle_input_entered_or_left_event(event);
  535. if (event.type() == Event::WindowCloseRequest)
  536. return handle_close_request();
  537. if (event.type() == Event::WindowEntered)
  538. return handle_entered_event(event);
  539. if (event.type() == Event::WindowLeft)
  540. return handle_left_event(event);
  541. if (event.type() == Event::Resize)
  542. return handle_resize_event(static_cast<ResizeEvent&>(event));
  543. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  544. return wm_event(static_cast<WMEvent&>(event));
  545. if (event.type() == Event::DragMove)
  546. return handle_drag_move_event(static_cast<DragEvent&>(event));
  547. if (event.type() == Event::ThemeChange)
  548. return handle_theme_change_event(static_cast<ThemeChangeEvent&>(event));
  549. if (event.type() == Event::FontsChange)
  550. return handle_fonts_change_event(static_cast<FontsChangeEvent&>(event));
  551. if (event.type() == Event::ScreenRectsChange)
  552. return handle_screen_rects_change_event(static_cast<ScreenRectsChangeEvent&>(event));
  553. Core::Object::event(event);
  554. }
  555. bool Window::is_visible() const
  556. {
  557. return m_visible;
  558. }
  559. void Window::update()
  560. {
  561. auto rect = this->rect();
  562. update({ 0, 0, rect.width(), rect.height() });
  563. }
  564. void Window::force_update()
  565. {
  566. if (!is_visible())
  567. return;
  568. auto rect = this->rect();
  569. WindowServerConnection::the().async_invalidate_rect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true);
  570. }
  571. void Window::update(const Gfx::IntRect& a_rect)
  572. {
  573. if (!is_visible())
  574. return;
  575. for (auto& pending_rect : m_pending_paint_event_rects) {
  576. if (pending_rect.contains(a_rect)) {
  577. dbgln_if(UPDATE_COALESCING_DEBUG, "Ignoring {} since it's contained by pending rect {}", a_rect, pending_rect);
  578. return;
  579. }
  580. }
  581. if (m_pending_paint_event_rects.is_empty()) {
  582. deferred_invoke([this](auto&) {
  583. auto rects = move(m_pending_paint_event_rects);
  584. if (rects.is_empty())
  585. return;
  586. WindowServerConnection::the().async_invalidate_rect(m_window_id, rects, false);
  587. });
  588. }
  589. m_pending_paint_event_rects.append(a_rect);
  590. }
  591. void Window::set_main_widget(Widget* widget)
  592. {
  593. if (m_main_widget == widget)
  594. return;
  595. if (m_main_widget) {
  596. m_main_widget->set_window(nullptr);
  597. remove_child(*m_main_widget);
  598. }
  599. m_main_widget = widget;
  600. if (m_main_widget) {
  601. add_child(*widget);
  602. auto new_window_rect = rect();
  603. if (m_main_widget->min_width() >= 0)
  604. new_window_rect.set_width(max(new_window_rect.width(), m_main_widget->min_width()));
  605. if (m_main_widget->min_height() >= 0)
  606. new_window_rect.set_height(max(new_window_rect.height(), m_main_widget->min_height()));
  607. set_rect(new_window_rect);
  608. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  609. m_main_widget->set_window(this);
  610. if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
  611. m_main_widget->set_focus(true);
  612. }
  613. update();
  614. }
  615. void Window::set_focused_widget(Widget* widget, FocusSource source)
  616. {
  617. if (m_focused_widget == widget)
  618. return;
  619. WeakPtr<Widget> previously_focused_widget = m_focused_widget;
  620. m_focused_widget = widget;
  621. if (!m_focused_widget && m_previously_focused_widget)
  622. m_focused_widget = m_previously_focused_widget;
  623. if (previously_focused_widget) {
  624. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  625. previously_focused_widget->update();
  626. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  627. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  628. m_previously_focused_widget = previously_focused_widget;
  629. }
  630. if (m_focused_widget) {
  631. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  632. m_focused_widget->update();
  633. if (m_focused_widget && m_focused_widget->on_focus_change)
  634. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  635. }
  636. }
  637. void Window::set_global_cursor_tracking_widget(Widget* widget)
  638. {
  639. if (widget == m_global_cursor_tracking_widget)
  640. return;
  641. m_global_cursor_tracking_widget = widget;
  642. }
  643. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  644. {
  645. if (widget == m_automatic_cursor_tracking_widget)
  646. return;
  647. m_automatic_cursor_tracking_widget = widget;
  648. }
  649. void Window::set_has_alpha_channel(bool value)
  650. {
  651. if (m_has_alpha_channel == value)
  652. return;
  653. m_has_alpha_channel = value;
  654. if (!is_visible())
  655. return;
  656. m_pending_paint_event_rects.clear();
  657. m_back_store = nullptr;
  658. m_front_store = nullptr;
  659. WindowServerConnection::the().async_set_window_has_alpha_channel(m_window_id, value);
  660. update();
  661. }
  662. void Window::set_double_buffering_enabled(bool value)
  663. {
  664. VERIFY(!is_visible());
  665. m_double_buffering_enabled = value;
  666. }
  667. void Window::set_opacity(float opacity)
  668. {
  669. m_opacity_when_windowless = opacity;
  670. if (!is_visible())
  671. return;
  672. WindowServerConnection::the().async_set_window_opacity(m_window_id, opacity);
  673. }
  674. void Window::set_alpha_hit_threshold(float threshold)
  675. {
  676. if (threshold < 0.0f)
  677. threshold = 0.0f;
  678. else if (threshold > 1.0f)
  679. threshold = 1.0f;
  680. if (m_alpha_hit_threshold == threshold)
  681. return;
  682. m_alpha_hit_threshold = threshold;
  683. if (!is_visible())
  684. return;
  685. WindowServerConnection::the().async_set_window_alpha_hit_threshold(m_window_id, threshold);
  686. }
  687. void Window::set_hovered_widget(Widget* widget)
  688. {
  689. if (widget == m_hovered_widget)
  690. return;
  691. if (m_hovered_widget)
  692. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  693. m_hovered_widget = widget;
  694. if (m_hovered_widget)
  695. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  696. auto* app = Application::the();
  697. if (app && app->hover_debugging_enabled())
  698. update();
  699. }
  700. void Window::set_current_backing_store(WindowBackingStore& backing_store, bool flush_immediately)
  701. {
  702. auto& bitmap = backing_store.bitmap();
  703. 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);
  704. }
  705. void Window::flip(const Vector<Gfx::IntRect, 32>& dirty_rects)
  706. {
  707. swap(m_front_store, m_back_store);
  708. set_current_backing_store(*m_front_store);
  709. if (!m_back_store || m_back_store->size() != m_front_store->size()) {
  710. m_back_store = create_backing_store(m_front_store->size());
  711. VERIFY(m_back_store);
  712. memcpy(m_back_store->bitmap().scanline(0), m_front_store->bitmap().scanline(0), m_front_store->bitmap().size_in_bytes());
  713. m_back_store->bitmap().set_volatile();
  714. return;
  715. }
  716. // Copy whatever was painted from the front to the back.
  717. Painter painter(m_back_store->bitmap());
  718. for (auto& dirty_rect : dirty_rects)
  719. painter.blit(dirty_rect.location(), m_front_store->bitmap(), dirty_rect, 1.0f, false);
  720. m_back_store->bitmap().set_volatile();
  721. }
  722. OwnPtr<WindowBackingStore> Window::create_backing_store(const Gfx::IntSize& size)
  723. {
  724. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  725. VERIFY(!size.is_empty());
  726. size_t pitch = Gfx::Bitmap::minimum_pitch(size.width(), format);
  727. size_t size_in_bytes = size.height() * pitch;
  728. auto buffer = Core::AnonymousBuffer::create_with_size(round_up_to_power_of_two(size_in_bytes, PAGE_SIZE));
  729. if (!buffer.is_valid()) {
  730. perror("anon_create");
  731. return {};
  732. }
  733. // FIXME: Plumb scale factor here eventually.
  734. auto bitmap = Gfx::Bitmap::try_create_with_anonymous_buffer(format, move(buffer), size, 1, {});
  735. if (!bitmap) {
  736. VERIFY(size.width() <= INT16_MAX);
  737. VERIFY(size.height() <= INT16_MAX);
  738. return {};
  739. }
  740. return make<WindowBackingStore>(bitmap.release_nonnull());
  741. }
  742. void Window::set_modal(bool modal)
  743. {
  744. VERIFY(!is_visible());
  745. m_modal = modal;
  746. }
  747. void Window::wm_event(WMEvent&)
  748. {
  749. }
  750. void Window::screen_rects_change_event(ScreenRectsChangeEvent&)
  751. {
  752. }
  753. void Window::set_icon(const Gfx::Bitmap* icon)
  754. {
  755. if (m_icon == icon)
  756. return;
  757. Gfx::IntSize icon_size = icon ? icon->size() : Gfx::IntSize(16, 16);
  758. m_icon = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, icon_size);
  759. VERIFY(m_icon);
  760. if (icon) {
  761. Painter painter(*m_icon);
  762. painter.blit({ 0, 0 }, *icon, icon->rect());
  763. }
  764. apply_icon();
  765. }
  766. void Window::apply_icon()
  767. {
  768. if (!m_icon)
  769. return;
  770. if (!is_visible())
  771. return;
  772. WindowServerConnection::the().async_set_window_icon_bitmap(m_window_id, m_icon->to_shareable_bitmap());
  773. }
  774. void Window::start_interactive_resize()
  775. {
  776. WindowServerConnection::the().async_start_window_resize(m_window_id);
  777. }
  778. Vector<Widget&> Window::focusable_widgets(FocusSource source) const
  779. {
  780. if (!m_main_widget)
  781. return {};
  782. HashTable<Widget*> seen_widgets;
  783. Vector<Widget&> collected_widgets;
  784. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  785. bool widget_accepts_focus = false;
  786. switch (source) {
  787. case FocusSource::Keyboard:
  788. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::TabFocus);
  789. break;
  790. case FocusSource::Mouse:
  791. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::ClickFocus);
  792. break;
  793. case FocusSource::Programmatic:
  794. widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
  795. break;
  796. }
  797. if (widget_accepts_focus) {
  798. auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
  799. if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
  800. collected_widgets.append(effective_focus_widget);
  801. }
  802. widget.for_each_child_widget([&](auto& child) {
  803. if (!child.is_visible())
  804. return IterationDecision::Continue;
  805. if (!child.is_enabled())
  806. return IterationDecision::Continue;
  807. if (!child.is_auto_focusable())
  808. return IterationDecision::Continue;
  809. collect_focusable_widgets(child);
  810. return IterationDecision::Continue;
  811. });
  812. };
  813. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  814. return collected_widgets;
  815. }
  816. void Window::set_fullscreen(bool fullscreen)
  817. {
  818. if (m_fullscreen == fullscreen)
  819. return;
  820. m_fullscreen = fullscreen;
  821. if (!is_visible())
  822. return;
  823. WindowServerConnection::the().async_set_fullscreen(m_window_id, fullscreen);
  824. }
  825. void Window::set_frameless(bool frameless)
  826. {
  827. if (m_frameless == frameless)
  828. return;
  829. m_frameless = frameless;
  830. if (!is_visible())
  831. return;
  832. WindowServerConnection::the().async_set_frameless(m_window_id, frameless);
  833. if (!frameless)
  834. apply_icon();
  835. }
  836. void Window::set_forced_shadow(bool shadow)
  837. {
  838. if (m_forced_shadow == shadow)
  839. return;
  840. m_forced_shadow = shadow;
  841. if (!is_visible())
  842. return;
  843. WindowServerConnection::the().async_set_forced_shadow(m_window_id, shadow);
  844. }
  845. bool Window::is_maximized() const
  846. {
  847. if (!is_visible())
  848. return false;
  849. return WindowServerConnection::the().is_maximized(m_window_id);
  850. }
  851. void Window::set_maximized(bool maximized)
  852. {
  853. VERIFY(m_window_id != 0);
  854. WindowServerConnection::the().async_set_maximized(m_window_id, maximized);
  855. }
  856. void Window::schedule_relayout()
  857. {
  858. if (m_layout_pending)
  859. return;
  860. m_layout_pending = true;
  861. deferred_invoke([this](auto&) {
  862. if (main_widget())
  863. main_widget()->do_layout();
  864. update();
  865. m_layout_pending = false;
  866. });
  867. }
  868. void Window::refresh_system_theme()
  869. {
  870. WindowServerConnection::the().async_refresh_system_theme();
  871. }
  872. void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
  873. {
  874. for (auto& e : *reified_windows) {
  875. VERIFY(e.value);
  876. callback(*e.value);
  877. }
  878. }
  879. void Window::update_all_windows(Badge<WindowServerConnection>)
  880. {
  881. for (auto& e : *reified_windows) {
  882. e.value->force_update();
  883. }
  884. }
  885. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  886. {
  887. m_visible_for_timer_purposes = !minimized && !occluded;
  888. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  889. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  890. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  891. if (!store)
  892. return;
  893. if (minimized || occluded) {
  894. store->bitmap().set_volatile();
  895. } else {
  896. bool was_purged = false;
  897. bool bitmap_has_memory = store->bitmap().set_nonvolatile(was_purged);
  898. if (!bitmap_has_memory) {
  899. // Not enough memory to make the bitmap non-volatile. Lose the bitmap and schedule an update.
  900. // Let the paint system figure out what to do.
  901. store = nullptr;
  902. update();
  903. } else if (was_purged) {
  904. // The bitmap memory was purged by the kernel, but we have all-new zero-filled pages.
  905. // Schedule an update to regenerate the bitmap.
  906. update();
  907. }
  908. }
  909. }
  910. Action* Window::action_for_key_event(const KeyEvent& event)
  911. {
  912. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  913. Action* found_action = nullptr;
  914. for_each_child_of_type<Action>([&](auto& action) {
  915. if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) {
  916. found_action = &action;
  917. return IterationDecision::Break;
  918. }
  919. return IterationDecision::Continue;
  920. });
  921. return found_action;
  922. }
  923. void Window::set_base_size(const Gfx::IntSize& base_size)
  924. {
  925. if (m_base_size == base_size)
  926. return;
  927. m_base_size = base_size;
  928. if (is_visible())
  929. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  930. }
  931. void Window::set_size_increment(const Gfx::IntSize& size_increment)
  932. {
  933. if (m_size_increment == size_increment)
  934. return;
  935. m_size_increment = size_increment;
  936. if (is_visible())
  937. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  938. }
  939. void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio)
  940. {
  941. if (m_resize_aspect_ratio == ratio)
  942. return;
  943. m_resize_aspect_ratio = ratio;
  944. if (is_visible())
  945. WindowServerConnection::the().async_set_window_resize_aspect_ratio(m_window_id, m_resize_aspect_ratio);
  946. }
  947. void Window::did_add_widget(Badge<Widget>, Widget&)
  948. {
  949. if (!m_focused_widget)
  950. focus_a_widget_if_possible(FocusSource::Mouse);
  951. }
  952. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  953. {
  954. if (m_focused_widget == &widget)
  955. m_focused_widget = nullptr;
  956. if (m_hovered_widget == &widget)
  957. m_hovered_widget = nullptr;
  958. if (m_global_cursor_tracking_widget == &widget)
  959. m_global_cursor_tracking_widget = nullptr;
  960. if (m_automatic_cursor_tracking_widget == &widget)
  961. m_automatic_cursor_tracking_widget = nullptr;
  962. }
  963. void Window::set_progress(Optional<int> progress)
  964. {
  965. VERIFY(m_window_id);
  966. WindowServerConnection::the().async_set_window_progress(m_window_id, progress);
  967. }
  968. void Window::update_cursor()
  969. {
  970. Gfx::StandardCursor new_cursor;
  971. if (m_hovered_widget && m_hovered_widget->override_cursor() != Gfx::StandardCursor::None)
  972. new_cursor = m_hovered_widget->override_cursor();
  973. else
  974. new_cursor = m_cursor;
  975. if (m_effective_cursor == new_cursor)
  976. return;
  977. m_effective_cursor = new_cursor;
  978. if (m_custom_cursor)
  979. WindowServerConnection::the().async_set_window_custom_cursor(m_window_id, m_custom_cursor->to_shareable_bitmap());
  980. else
  981. WindowServerConnection::the().async_set_window_cursor(m_window_id, (u32)m_effective_cursor);
  982. }
  983. void Window::focus_a_widget_if_possible(FocusSource source)
  984. {
  985. auto focusable_widgets = this->focusable_widgets(source);
  986. if (!focusable_widgets.is_empty())
  987. set_focused_widget(&focusable_widgets[0], source);
  988. }
  989. void Window::did_disable_focused_widget(Badge<Widget>)
  990. {
  991. focus_a_widget_if_possible(FocusSource::Mouse);
  992. }
  993. bool Window::is_active() const
  994. {
  995. VERIFY(Application::the());
  996. return this == Application::the()->active_window();
  997. }
  998. Gfx::Bitmap* Window::back_bitmap()
  999. {
  1000. return m_back_store ? &m_back_store->bitmap() : nullptr;
  1001. }
  1002. Menu& Window::add_menu(String name)
  1003. {
  1004. Menu& menu = m_menubar->add_menu({}, move(name));
  1005. if (m_window_id) {
  1006. menu.realize_menu_if_needed();
  1007. WindowServerConnection::the().async_add_menu(m_window_id, menu.menu_id());
  1008. }
  1009. return menu;
  1010. }
  1011. bool Window::is_modified() const
  1012. {
  1013. if (!m_window_id)
  1014. return false;
  1015. return WindowServerConnection::the().is_window_modified(m_window_id);
  1016. }
  1017. void Window::set_modified(bool modified)
  1018. {
  1019. if (!m_window_id)
  1020. return;
  1021. WindowServerConnection::the().async_set_window_modified(m_window_id, modified);
  1022. }
  1023. }