Window.cpp 37 KB

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