Window.cpp 37 KB

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