Window.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 was_purged = false;
  361. bool bitmap_has_memory = m_back_store->bitmap().set_nonvolatile(was_purged);
  362. if (!bitmap_has_memory) {
  363. // We didn't have enough memory to make the bitmap non-volatile!
  364. // Fall back to single-buffered mode for this window.
  365. // FIXME: Once we have a way to listen for system memory pressure notifications,
  366. // it would be cool to transition back into double-buffered mode once
  367. // the coast is clear.
  368. dbgln("Not enough memory to make backing store non-volatile. Falling back to single-buffered mode.");
  369. m_double_buffering_enabled = false;
  370. m_back_store = move(m_front_store);
  371. created_new_backing_store = true;
  372. } else if (was_purged) {
  373. // The backing store bitmap was cleared, but it does have memory.
  374. // Act as if it's a new backing store so the entire window gets repainted.
  375. created_new_backing_store = true;
  376. }
  377. }
  378. auto rect = rects.first();
  379. if (rect.is_empty() || created_new_backing_store) {
  380. rects.clear();
  381. rects.append({ {}, event.window_size() });
  382. }
  383. for (auto& rect : rects) {
  384. PaintEvent paint_event(rect);
  385. m_main_widget->dispatch_event(paint_event, this);
  386. }
  387. if (m_double_buffering_enabled)
  388. flip(rects);
  389. else if (created_new_backing_store)
  390. set_current_backing_store(*m_back_store, true);
  391. if (is_visible())
  392. WindowServerConnection::the().async_did_finish_painting(m_window_id, rects);
  393. }
  394. void Window::handle_key_event(KeyEvent& event)
  395. {
  396. if (!m_focused_widget && event.type() == Event::KeyDown && event.key() == Key_Tab && !event.ctrl() && !event.alt() && !event.super()) {
  397. focus_a_widget_if_possible(FocusSource::Keyboard);
  398. }
  399. if (m_focused_widget)
  400. return m_focused_widget->dispatch_event(event, this);
  401. if (m_main_widget)
  402. return m_main_widget->dispatch_event(event, this);
  403. }
  404. void Window::handle_resize_event(ResizeEvent& event)
  405. {
  406. auto new_size = event.size();
  407. if (m_back_store && m_back_store->size() != new_size)
  408. m_back_store = nullptr;
  409. if (!m_pending_paint_event_rects.is_empty()) {
  410. m_pending_paint_event_rects.clear_with_capacity();
  411. m_pending_paint_event_rects.append({ {}, new_size });
  412. }
  413. m_rect_when_windowless = { {}, new_size };
  414. if (m_main_widget)
  415. m_main_widget->set_relative_rect({ {}, new_size });
  416. }
  417. void Window::handle_input_entered_or_left_event(Core::Event& event)
  418. {
  419. m_is_active_input = event.type() == Event::WindowInputEntered;
  420. if (on_active_input_change)
  421. on_active_input_change(m_is_active_input);
  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_became_active_or_inactive_event(Core::Event& event)
  428. {
  429. if (event.type() == Event::WindowBecameActive)
  430. Application::the()->window_did_become_active({}, *this);
  431. else
  432. Application::the()->window_did_become_inactive({}, *this);
  433. if (m_main_widget)
  434. m_main_widget->dispatch_event(event, this);
  435. if (m_focused_widget)
  436. m_focused_widget->update();
  437. }
  438. void Window::handle_close_request()
  439. {
  440. if (on_close_request) {
  441. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  442. return;
  443. }
  444. close();
  445. }
  446. void Window::handle_theme_change_event(ThemeChangeEvent& event)
  447. {
  448. if (!m_main_widget)
  449. return;
  450. auto dispatch_theme_change = [&](auto& widget, auto recursive) {
  451. widget.dispatch_event(event, this);
  452. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  453. widget.dispatch_event(event, this);
  454. recursive(widget, recursive);
  455. return IterationDecision::Continue;
  456. });
  457. };
  458. dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
  459. }
  460. void Window::handle_fonts_change_event(FontsChangeEvent& event)
  461. {
  462. if (!m_main_widget)
  463. return;
  464. auto dispatch_fonts_change = [&](auto& widget, auto recursive) {
  465. widget.dispatch_event(event, this);
  466. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  467. widget.dispatch_event(event, this);
  468. recursive(widget, recursive);
  469. return IterationDecision::Continue;
  470. });
  471. };
  472. dispatch_fonts_change(*m_main_widget.ptr(), dispatch_fonts_change);
  473. }
  474. void Window::handle_screen_rects_change_event(ScreenRectsChangeEvent& event)
  475. {
  476. if (!m_main_widget)
  477. return;
  478. auto dispatch_screen_rects_change = [&](auto& widget, auto recursive) {
  479. widget.dispatch_event(event, this);
  480. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  481. widget.dispatch_event(event, this);
  482. recursive(widget, recursive);
  483. return IterationDecision::Continue;
  484. });
  485. };
  486. dispatch_screen_rects_change(*m_main_widget.ptr(), dispatch_screen_rects_change);
  487. screen_rects_change_event(event);
  488. }
  489. void Window::handle_drag_move_event(DragEvent& event)
  490. {
  491. if (!m_main_widget)
  492. return;
  493. auto result = m_main_widget->hit_test(event.position());
  494. VERIFY(result.widget);
  495. Application::the()->set_drag_hovered_widget({}, result.widget, result.local_position, event.mime_types());
  496. // NOTE: Setting the drag hovered widget may have executed arbitrary code, so re-check that the widget is still there.
  497. if (!result.widget)
  498. return;
  499. if (result.widget->has_pending_drop()) {
  500. DragEvent drag_move_event(static_cast<Event::Type>(event.type()), result.local_position, event.mime_types());
  501. result.widget->dispatch_event(drag_move_event, this);
  502. }
  503. }
  504. void Window::enter_event(Core::Event&)
  505. {
  506. }
  507. void Window::leave_event(Core::Event&)
  508. {
  509. }
  510. void Window::handle_entered_event(Core::Event& event)
  511. {
  512. enter_event(event);
  513. }
  514. void Window::handle_left_event(Core::Event& event)
  515. {
  516. set_hovered_widget(nullptr);
  517. Application::the()->set_drag_hovered_widget({}, nullptr);
  518. leave_event(event);
  519. }
  520. void Window::event(Core::Event& event)
  521. {
  522. ScopeGuard guard([&] {
  523. // Accept the event so it doesn't bubble up to parent windows!
  524. event.accept();
  525. });
  526. if (event.type() == Event::Drop)
  527. return handle_drop_event(static_cast<DropEvent&>(event));
  528. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel)
  529. return handle_mouse_event(static_cast<MouseEvent&>(event));
  530. if (event.type() == Event::MultiPaint)
  531. return handle_multi_paint_event(static_cast<MultiPaintEvent&>(event));
  532. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown)
  533. return handle_key_event(static_cast<KeyEvent&>(event));
  534. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
  535. return handle_became_active_or_inactive_event(event);
  536. if (event.type() == Event::WindowInputEntered || event.type() == Event::WindowInputLeft)
  537. return handle_input_entered_or_left_event(event);
  538. if (event.type() == Event::WindowCloseRequest)
  539. return handle_close_request();
  540. if (event.type() == Event::WindowEntered)
  541. return handle_entered_event(event);
  542. if (event.type() == Event::WindowLeft)
  543. return handle_left_event(event);
  544. if (event.type() == Event::Resize)
  545. return handle_resize_event(static_cast<ResizeEvent&>(event));
  546. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  547. return wm_event(static_cast<WMEvent&>(event));
  548. if (event.type() == Event::DragMove)
  549. return handle_drag_move_event(static_cast<DragEvent&>(event));
  550. if (event.type() == Event::ThemeChange)
  551. return handle_theme_change_event(static_cast<ThemeChangeEvent&>(event));
  552. if (event.type() == Event::FontsChange)
  553. return handle_fonts_change_event(static_cast<FontsChangeEvent&>(event));
  554. if (event.type() == Event::ScreenRectsChange)
  555. return handle_screen_rects_change_event(static_cast<ScreenRectsChangeEvent&>(event));
  556. Core::Object::event(event);
  557. }
  558. bool Window::is_visible() const
  559. {
  560. return m_visible;
  561. }
  562. void Window::update()
  563. {
  564. auto rect = this->rect();
  565. update({ 0, 0, rect.width(), rect.height() });
  566. }
  567. void Window::force_update()
  568. {
  569. if (!is_visible())
  570. return;
  571. auto rect = this->rect();
  572. WindowServerConnection::the().async_invalidate_rect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true);
  573. }
  574. void Window::update(const Gfx::IntRect& a_rect)
  575. {
  576. if (!is_visible())
  577. return;
  578. for (auto& pending_rect : m_pending_paint_event_rects) {
  579. if (pending_rect.contains(a_rect)) {
  580. dbgln_if(UPDATE_COALESCING_DEBUG, "Ignoring {} since it's contained by pending rect {}", a_rect, pending_rect);
  581. return;
  582. }
  583. }
  584. if (m_pending_paint_event_rects.is_empty()) {
  585. deferred_invoke([this](auto&) {
  586. auto rects = move(m_pending_paint_event_rects);
  587. if (rects.is_empty())
  588. return;
  589. WindowServerConnection::the().async_invalidate_rect(m_window_id, rects, false);
  590. });
  591. }
  592. m_pending_paint_event_rects.append(a_rect);
  593. }
  594. void Window::set_main_widget(Widget* widget)
  595. {
  596. if (m_main_widget == widget)
  597. return;
  598. if (m_main_widget) {
  599. m_main_widget->set_window(nullptr);
  600. remove_child(*m_main_widget);
  601. }
  602. m_main_widget = widget;
  603. if (m_main_widget) {
  604. add_child(*widget);
  605. auto new_window_rect = rect();
  606. if (m_main_widget->min_width() >= 0)
  607. new_window_rect.set_width(max(new_window_rect.width(), m_main_widget->min_width()));
  608. if (m_main_widget->min_height() >= 0)
  609. new_window_rect.set_height(max(new_window_rect.height(), m_main_widget->min_height()));
  610. set_rect(new_window_rect);
  611. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  612. m_main_widget->set_window(this);
  613. if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
  614. m_main_widget->set_focus(true);
  615. }
  616. update();
  617. }
  618. void Window::set_focused_widget(Widget* widget, FocusSource source)
  619. {
  620. if (m_focused_widget == widget)
  621. return;
  622. WeakPtr<Widget> previously_focused_widget = m_focused_widget;
  623. m_focused_widget = widget;
  624. if (!m_focused_widget && m_previously_focused_widget)
  625. m_focused_widget = m_previously_focused_widget;
  626. if (previously_focused_widget) {
  627. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  628. previously_focused_widget->update();
  629. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  630. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  631. m_previously_focused_widget = previously_focused_widget;
  632. }
  633. if (m_focused_widget) {
  634. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  635. m_focused_widget->update();
  636. if (m_focused_widget && m_focused_widget->on_focus_change)
  637. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  638. }
  639. }
  640. void Window::set_global_cursor_tracking_widget(Widget* widget)
  641. {
  642. if (widget == m_global_cursor_tracking_widget)
  643. return;
  644. m_global_cursor_tracking_widget = widget;
  645. }
  646. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  647. {
  648. if (widget == m_automatic_cursor_tracking_widget)
  649. return;
  650. m_automatic_cursor_tracking_widget = widget;
  651. }
  652. void Window::set_has_alpha_channel(bool value)
  653. {
  654. if (m_has_alpha_channel == value)
  655. return;
  656. m_has_alpha_channel = value;
  657. if (!is_visible())
  658. return;
  659. m_pending_paint_event_rects.clear();
  660. m_back_store = nullptr;
  661. m_front_store = nullptr;
  662. WindowServerConnection::the().async_set_window_has_alpha_channel(m_window_id, value);
  663. update();
  664. }
  665. void Window::set_double_buffering_enabled(bool value)
  666. {
  667. VERIFY(!is_visible());
  668. m_double_buffering_enabled = value;
  669. }
  670. void Window::set_opacity(float opacity)
  671. {
  672. m_opacity_when_windowless = opacity;
  673. if (!is_visible())
  674. return;
  675. WindowServerConnection::the().async_set_window_opacity(m_window_id, opacity);
  676. }
  677. void Window::set_alpha_hit_threshold(float threshold)
  678. {
  679. if (threshold < 0.0f)
  680. threshold = 0.0f;
  681. else if (threshold > 1.0f)
  682. threshold = 1.0f;
  683. if (m_alpha_hit_threshold == threshold)
  684. return;
  685. m_alpha_hit_threshold = threshold;
  686. if (!is_visible())
  687. return;
  688. WindowServerConnection::the().async_set_window_alpha_hit_threshold(m_window_id, threshold);
  689. }
  690. void Window::set_hovered_widget(Widget* widget)
  691. {
  692. if (widget == m_hovered_widget)
  693. return;
  694. if (m_hovered_widget && !m_hovered_widget->is_parent_of(widget))
  695. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  696. m_hovered_widget = widget;
  697. if (m_hovered_widget)
  698. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  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. collect_focusable_widgets(child);
  808. return IterationDecision::Continue;
  809. });
  810. };
  811. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  812. return collected_widgets;
  813. }
  814. void Window::set_fullscreen(bool fullscreen)
  815. {
  816. if (m_fullscreen == fullscreen)
  817. return;
  818. m_fullscreen = fullscreen;
  819. if (!is_visible())
  820. return;
  821. WindowServerConnection::the().async_set_fullscreen(m_window_id, fullscreen);
  822. }
  823. void Window::set_frameless(bool frameless)
  824. {
  825. if (m_frameless == frameless)
  826. return;
  827. m_frameless = frameless;
  828. if (!is_visible())
  829. return;
  830. WindowServerConnection::the().async_set_frameless(m_window_id, frameless);
  831. if (!frameless)
  832. apply_icon();
  833. }
  834. void Window::set_forced_shadow(bool shadow)
  835. {
  836. if (m_forced_shadow == shadow)
  837. return;
  838. m_forced_shadow = shadow;
  839. if (!is_visible())
  840. return;
  841. WindowServerConnection::the().async_set_forced_shadow(m_window_id, shadow);
  842. }
  843. bool Window::is_maximized() const
  844. {
  845. if (!is_visible())
  846. return false;
  847. return WindowServerConnection::the().is_maximized(m_window_id);
  848. }
  849. void Window::set_maximized(bool maximized)
  850. {
  851. VERIFY(m_window_id != 0);
  852. WindowServerConnection::the().async_set_maximized(m_window_id, maximized);
  853. }
  854. void Window::schedule_relayout()
  855. {
  856. if (m_layout_pending)
  857. return;
  858. m_layout_pending = true;
  859. deferred_invoke([this](auto&) {
  860. if (main_widget())
  861. main_widget()->do_layout();
  862. update();
  863. m_layout_pending = false;
  864. });
  865. }
  866. void Window::refresh_system_theme()
  867. {
  868. WindowServerConnection::the().async_refresh_system_theme();
  869. }
  870. void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
  871. {
  872. for (auto& e : *reified_windows) {
  873. VERIFY(e.value);
  874. callback(*e.value);
  875. }
  876. }
  877. void Window::update_all_windows(Badge<WindowServerConnection>)
  878. {
  879. for (auto& e : *reified_windows) {
  880. e.value->force_update();
  881. }
  882. }
  883. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  884. {
  885. m_visible_for_timer_purposes = !minimized && !occluded;
  886. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  887. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  888. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  889. if (!store)
  890. return;
  891. if (minimized || occluded) {
  892. store->bitmap().set_volatile();
  893. } else {
  894. bool was_purged = false;
  895. bool bitmap_has_memory = store->bitmap().set_nonvolatile(was_purged);
  896. if (!bitmap_has_memory) {
  897. // Not enough memory to make the bitmap non-volatile. Lose the bitmap and schedule an update.
  898. // Let the paint system figure out what to do.
  899. store = nullptr;
  900. update();
  901. } else if (was_purged) {
  902. // The bitmap memory was purged by the kernel, but we have all-new zero-filled pages.
  903. // Schedule an update to regenerate the bitmap.
  904. update();
  905. }
  906. }
  907. }
  908. Action* Window::action_for_key_event(const KeyEvent& event)
  909. {
  910. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  911. Action* found_action = nullptr;
  912. for_each_child_of_type<Action>([&](auto& action) {
  913. if (action.shortcut() == shortcut || action.alternate_shortcut() == shortcut) {
  914. found_action = &action;
  915. return IterationDecision::Break;
  916. }
  917. return IterationDecision::Continue;
  918. });
  919. return found_action;
  920. }
  921. void Window::set_base_size(const Gfx::IntSize& base_size)
  922. {
  923. if (m_base_size == base_size)
  924. return;
  925. m_base_size = base_size;
  926. if (is_visible())
  927. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  928. }
  929. void Window::set_size_increment(const Gfx::IntSize& size_increment)
  930. {
  931. if (m_size_increment == size_increment)
  932. return;
  933. m_size_increment = size_increment;
  934. if (is_visible())
  935. WindowServerConnection::the().async_set_window_base_size_and_size_increment(m_window_id, m_base_size, m_size_increment);
  936. }
  937. void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio)
  938. {
  939. if (m_resize_aspect_ratio == ratio)
  940. return;
  941. m_resize_aspect_ratio = ratio;
  942. if (is_visible())
  943. WindowServerConnection::the().async_set_window_resize_aspect_ratio(m_window_id, m_resize_aspect_ratio);
  944. }
  945. void Window::did_add_widget(Badge<Widget>, Widget&)
  946. {
  947. if (!m_focused_widget)
  948. focus_a_widget_if_possible(FocusSource::Mouse);
  949. }
  950. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  951. {
  952. if (m_focused_widget == &widget)
  953. m_focused_widget = nullptr;
  954. if (m_hovered_widget == &widget)
  955. m_hovered_widget = nullptr;
  956. if (m_global_cursor_tracking_widget == &widget)
  957. m_global_cursor_tracking_widget = nullptr;
  958. if (m_automatic_cursor_tracking_widget == &widget)
  959. m_automatic_cursor_tracking_widget = nullptr;
  960. }
  961. void Window::set_progress(Optional<int> progress)
  962. {
  963. VERIFY(m_window_id);
  964. WindowServerConnection::the().async_set_window_progress(m_window_id, progress);
  965. }
  966. void Window::update_cursor()
  967. {
  968. Gfx::StandardCursor new_cursor;
  969. if (m_hovered_widget && m_hovered_widget->override_cursor() != Gfx::StandardCursor::None)
  970. new_cursor = m_hovered_widget->override_cursor();
  971. else
  972. new_cursor = m_cursor;
  973. if (m_effective_cursor == new_cursor)
  974. return;
  975. m_effective_cursor = new_cursor;
  976. if (m_custom_cursor)
  977. WindowServerConnection::the().async_set_window_custom_cursor(m_window_id, m_custom_cursor->to_shareable_bitmap());
  978. else
  979. WindowServerConnection::the().async_set_window_cursor(m_window_id, (u32)m_effective_cursor);
  980. }
  981. void Window::focus_a_widget_if_possible(FocusSource source)
  982. {
  983. auto focusable_widgets = this->focusable_widgets(source);
  984. if (!focusable_widgets.is_empty())
  985. set_focused_widget(&focusable_widgets[0], source);
  986. }
  987. void Window::did_disable_focused_widget(Badge<Widget>)
  988. {
  989. focus_a_widget_if_possible(FocusSource::Mouse);
  990. }
  991. bool Window::is_active() const
  992. {
  993. VERIFY(Application::the());
  994. return this == Application::the()->active_window();
  995. }
  996. Gfx::Bitmap* Window::back_bitmap()
  997. {
  998. return m_back_store ? &m_back_store->bitmap() : nullptr;
  999. }
  1000. Menu& Window::add_menu(String name)
  1001. {
  1002. if (!m_menubar)
  1003. set_menubar(GUI::Menubar::construct());
  1004. return m_menubar->add_menu(move(name));
  1005. }
  1006. void Window::set_menubar(RefPtr<Menubar> menubar)
  1007. {
  1008. if (m_menubar == menubar)
  1009. return;
  1010. if (m_menubar)
  1011. m_menubar->notify_removed_from_window({});
  1012. m_menubar = move(menubar);
  1013. if (m_window_id && m_menubar) {
  1014. m_menubar->notify_added_to_window({});
  1015. WindowServerConnection::the().async_set_window_menubar(m_window_id, m_menubar->menubar_id());
  1016. }
  1017. }
  1018. bool Window::is_modified() const
  1019. {
  1020. if (!m_window_id)
  1021. return false;
  1022. return WindowServerConnection::the().is_window_modified(m_window_id);
  1023. }
  1024. void Window::set_modified(bool modified)
  1025. {
  1026. if (!m_window_id)
  1027. return;
  1028. WindowServerConnection::the().async_set_window_modified(m_window_id, modified);
  1029. }
  1030. }