Window.cpp 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/Debug.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/JsonObject.h>
  29. #include <AK/NeverDestroyed.h>
  30. #include <AK/ScopeGuard.h>
  31. #include <LibCore/EventLoop.h>
  32. #include <LibCore/MimeData.h>
  33. #include <LibGUI/Action.h>
  34. #include <LibGUI/Application.h>
  35. #include <LibGUI/Desktop.h>
  36. #include <LibGUI/Event.h>
  37. #include <LibGUI/MenuBar.h>
  38. #include <LibGUI/Painter.h>
  39. #include <LibGUI/Widget.h>
  40. #include <LibGUI/Window.h>
  41. #include <LibGUI/WindowServerConnection.h>
  42. #include <LibGfx/Bitmap.h>
  43. #include <fcntl.h>
  44. #include <serenity.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. namespace GUI {
  49. static i32 s_next_backing_store_serial;
  50. class WindowBackingStore {
  51. public:
  52. WindowBackingStore(NonnullRefPtr<Gfx::Bitmap> bitmap)
  53. : m_bitmap(bitmap)
  54. , m_serial(++s_next_backing_store_serial)
  55. {
  56. }
  57. Gfx::Bitmap& bitmap() { return *m_bitmap; }
  58. const Gfx::Bitmap& bitmap() const { return *m_bitmap; }
  59. Gfx::IntSize size() const { return m_bitmap->size(); }
  60. i32 serial() const { return m_serial; }
  61. private:
  62. NonnullRefPtr<Gfx::Bitmap> m_bitmap;
  63. const i32 m_serial;
  64. };
  65. static NeverDestroyed<HashTable<Window*>> all_windows;
  66. static NeverDestroyed<HashMap<int, Window*>> reified_windows;
  67. Window* Window::from_window_id(int window_id)
  68. {
  69. auto it = reified_windows->find(window_id);
  70. if (it != reified_windows->end())
  71. return (*it).value;
  72. return nullptr;
  73. }
  74. Window::Window(Core::Object* parent)
  75. : Core::Object(parent)
  76. {
  77. all_windows->set(this);
  78. m_rect_when_windowless = { -5000, -5000, 140, 140 };
  79. m_title_when_windowless = "GUI::Window";
  80. register_property(
  81. "title",
  82. [this] { return title(); },
  83. [this](auto& value) {
  84. set_title(value.to_string());
  85. return true;
  86. });
  87. register_property("visible", [this] { return is_visible(); });
  88. register_property("active", [this] { return is_active(); });
  89. REGISTER_BOOL_PROPERTY("minimizable", is_minimizable, set_minimizable);
  90. REGISTER_BOOL_PROPERTY("resizable", is_resizable, set_resizable);
  91. REGISTER_BOOL_PROPERTY("fullscreen", is_fullscreen, set_fullscreen);
  92. REGISTER_RECT_PROPERTY("rect", rect, set_rect);
  93. REGISTER_SIZE_PROPERTY("base_size", base_size, set_base_size);
  94. REGISTER_SIZE_PROPERTY("size_increment", size_increment, set_size_increment);
  95. }
  96. Window::~Window()
  97. {
  98. if (m_menubar)
  99. m_menubar->notify_removed_from_window({});
  100. all_windows->remove(this);
  101. hide();
  102. }
  103. void Window::close()
  104. {
  105. hide();
  106. if (on_close)
  107. on_close();
  108. }
  109. void Window::move_to_front()
  110. {
  111. if (!is_visible())
  112. return;
  113. WindowServerConnection::the().send_sync<Messages::WindowServer::MoveWindowToFront>(m_window_id);
  114. }
  115. void Window::show()
  116. {
  117. if (is_visible())
  118. return;
  119. auto* parent_window = find_parent_window();
  120. m_cursor = Gfx::StandardCursor::None;
  121. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::CreateWindow>(
  122. m_rect_when_windowless,
  123. !m_moved_by_client,
  124. m_has_alpha_channel,
  125. m_modal,
  126. m_minimizable,
  127. m_resizable,
  128. m_fullscreen,
  129. m_frameless,
  130. m_accessory,
  131. m_opacity_when_windowless,
  132. m_alpha_hit_threshold,
  133. m_base_size,
  134. m_size_increment,
  135. m_minimum_size_when_windowless,
  136. m_resize_aspect_ratio,
  137. (i32)m_window_type,
  138. m_title_when_windowless,
  139. parent_window ? parent_window->window_id() : 0);
  140. m_window_id = response->window_id();
  141. m_visible = true;
  142. apply_icon();
  143. if (m_menubar) {
  144. // This little dance makes us create a server-side menubar.
  145. auto menubar = move(m_menubar);
  146. set_menubar(menubar);
  147. }
  148. reified_windows->set(m_window_id, this);
  149. Application::the()->did_create_window({});
  150. update();
  151. }
  152. Window* Window::find_parent_window()
  153. {
  154. for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
  155. if (is<Window>(ancestor))
  156. return static_cast<Window*>(ancestor);
  157. }
  158. return nullptr;
  159. }
  160. void Window::server_did_destroy()
  161. {
  162. reified_windows->remove(m_window_id);
  163. m_window_id = 0;
  164. m_visible = false;
  165. m_pending_paint_event_rects.clear();
  166. m_back_store = nullptr;
  167. m_front_store = nullptr;
  168. m_cursor = Gfx::StandardCursor::None;
  169. }
  170. void Window::hide()
  171. {
  172. if (!is_visible())
  173. return;
  174. auto response = WindowServerConnection::the().send_sync<Messages::WindowServer::DestroyWindow>(m_window_id);
  175. server_did_destroy();
  176. for (auto child_window_id : response->destroyed_window_ids()) {
  177. if (auto* window = Window::from_window_id(child_window_id)) {
  178. window->server_did_destroy();
  179. }
  180. }
  181. if (auto* app = Application::the()) {
  182. bool app_has_visible_windows = false;
  183. for (auto& window : *all_windows) {
  184. if (window->is_visible()) {
  185. app_has_visible_windows = true;
  186. break;
  187. }
  188. }
  189. if (!app_has_visible_windows)
  190. app->did_delete_last_window({});
  191. }
  192. }
  193. void Window::set_title(const StringView& title)
  194. {
  195. m_title_when_windowless = title;
  196. if (!is_visible())
  197. return;
  198. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowTitle>(m_window_id, title);
  199. }
  200. String Window::title() const
  201. {
  202. if (!is_visible())
  203. return m_title_when_windowless;
  204. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowTitle>(m_window_id)->title();
  205. }
  206. Gfx::IntRect Window::applet_rect_on_screen() const
  207. {
  208. VERIFY(m_window_type == WindowType::Applet);
  209. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetAppletRectOnScreen>(m_window_id)->rect();
  210. }
  211. Gfx::IntRect Window::rect() const
  212. {
  213. if (!is_visible())
  214. return m_rect_when_windowless;
  215. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowRect>(m_window_id)->rect();
  216. }
  217. void Window::set_rect(const Gfx::IntRect& a_rect)
  218. {
  219. if (a_rect.location() != m_rect_when_windowless.location()) {
  220. m_moved_by_client = true;
  221. }
  222. m_rect_when_windowless = a_rect;
  223. if (!is_visible()) {
  224. if (m_main_widget)
  225. m_main_widget->resize(m_rect_when_windowless.size());
  226. return;
  227. }
  228. auto window_rect = WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowRect>(m_window_id, a_rect)->rect();
  229. if (m_back_store && m_back_store->size() != window_rect.size())
  230. m_back_store = nullptr;
  231. if (m_front_store && m_front_store->size() != window_rect.size())
  232. m_front_store = nullptr;
  233. if (m_main_widget)
  234. m_main_widget->resize(window_rect.size());
  235. }
  236. Gfx::IntSize Window::minimum_size() const
  237. {
  238. if (!is_visible())
  239. return m_minimum_size_when_windowless;
  240. return WindowServerConnection::the().send_sync<Messages::WindowServer::GetWindowMinimumSize>(m_window_id)->size();
  241. }
  242. void Window::set_minimum_size(const Gfx::IntSize& size)
  243. {
  244. m_minimum_size_modified = true;
  245. m_minimum_size_when_windowless = size;
  246. if (is_visible())
  247. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowMinimumSize>(m_window_id, size);
  248. }
  249. void Window::center_on_screen()
  250. {
  251. auto window_rect = rect();
  252. window_rect.center_within(Desktop::the().rect());
  253. set_rect(window_rect);
  254. }
  255. void Window::center_within(const Window& other)
  256. {
  257. if (this == &other)
  258. return;
  259. auto window_rect = rect();
  260. window_rect.center_within(other.rect());
  261. set_rect(window_rect);
  262. }
  263. void Window::set_window_type(WindowType window_type)
  264. {
  265. m_window_type = window_type;
  266. if (!m_minimum_size_modified) {
  267. // Apply minimum size defaults.
  268. if (m_window_type == WindowType::Normal || m_window_type == WindowType::ToolWindow)
  269. m_minimum_size_when_windowless = { 50, 50 };
  270. else
  271. m_minimum_size_when_windowless = { 1, 1 };
  272. }
  273. }
  274. void Window::set_cursor(Gfx::StandardCursor cursor)
  275. {
  276. if (m_cursor == cursor)
  277. return;
  278. m_cursor = cursor;
  279. m_custom_cursor = nullptr;
  280. update_cursor();
  281. }
  282. void Window::set_cursor(const Gfx::Bitmap& cursor)
  283. {
  284. if (m_custom_cursor == &cursor)
  285. return;
  286. m_cursor = Gfx::StandardCursor::None;
  287. m_custom_cursor = &cursor;
  288. update_cursor();
  289. }
  290. void Window::handle_drop_event(DropEvent& event)
  291. {
  292. if (!m_main_widget)
  293. return;
  294. auto result = m_main_widget->hit_test(event.position());
  295. auto local_event = make<DropEvent>(result.local_position, event.text(), event.mime_data());
  296. VERIFY(result.widget);
  297. result.widget->dispatch_event(*local_event, this);
  298. Application::the()->set_drag_hovered_widget({}, nullptr);
  299. }
  300. void Window::handle_mouse_event(MouseEvent& event)
  301. {
  302. if (m_global_cursor_tracking_widget) {
  303. auto window_relative_rect = m_global_cursor_tracking_widget->window_relative_rect();
  304. Gfx::IntPoint local_point { event.x() - window_relative_rect.x(), event.y() - window_relative_rect.y() };
  305. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  306. m_global_cursor_tracking_widget->dispatch_event(*local_event, this);
  307. return;
  308. }
  309. if (m_automatic_cursor_tracking_widget) {
  310. auto window_relative_rect = m_automatic_cursor_tracking_widget->window_relative_rect();
  311. Gfx::IntPoint local_point { event.x() - window_relative_rect.x(), event.y() - window_relative_rect.y() };
  312. auto local_event = make<MouseEvent>((Event::Type)event.type(), local_point, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  313. m_automatic_cursor_tracking_widget->dispatch_event(*local_event, this);
  314. if (event.buttons() == 0)
  315. m_automatic_cursor_tracking_widget = nullptr;
  316. return;
  317. }
  318. if (!m_main_widget)
  319. return;
  320. auto result = m_main_widget->hit_test(event.position());
  321. auto local_event = make<MouseEvent>((Event::Type)event.type(), result.local_position, event.buttons(), event.button(), event.modifiers(), event.wheel_delta());
  322. VERIFY(result.widget);
  323. set_hovered_widget(result.widget);
  324. if (event.buttons() != 0 && !m_automatic_cursor_tracking_widget)
  325. m_automatic_cursor_tracking_widget = *result.widget;
  326. if (result.widget != m_global_cursor_tracking_widget.ptr())
  327. result.widget->dispatch_event(*local_event, this);
  328. if (!m_pending_paint_event_rects.is_empty()) {
  329. MultiPaintEvent paint_event(move(m_pending_paint_event_rects), size());
  330. handle_multi_paint_event(paint_event);
  331. }
  332. }
  333. void Window::handle_multi_paint_event(MultiPaintEvent& event)
  334. {
  335. if (!is_visible())
  336. return;
  337. if (!m_main_widget)
  338. return;
  339. auto rects = event.rects();
  340. if (!m_pending_paint_event_rects.is_empty()) {
  341. // It's possible that there had been some calls to update() that
  342. // haven't been flushed. We can handle these right now, avoiding
  343. // another round trip.
  344. rects.append(move(m_pending_paint_event_rects));
  345. }
  346. VERIFY(!rects.is_empty());
  347. if (m_back_store && m_back_store->size() != event.window_size()) {
  348. // Eagerly discard the backing store if we learn from this paint event that it needs to be bigger.
  349. // Otherwise we would have to wait for a resize event to tell us. This way we don't waste the
  350. // effort on painting into an undersized bitmap that will be thrown away anyway.
  351. m_back_store = nullptr;
  352. }
  353. bool created_new_backing_store = !m_back_store;
  354. if (!m_back_store) {
  355. m_back_store = create_backing_store(event.window_size());
  356. VERIFY(m_back_store);
  357. } else if (m_double_buffering_enabled) {
  358. bool still_has_pixels = m_back_store->bitmap().set_nonvolatile();
  359. if (!still_has_pixels) {
  360. m_back_store = create_backing_store(event.window_size());
  361. VERIFY(m_back_store);
  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().post_message(Messages::WindowServer::DidFinishPainting(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. return;
  386. }
  387. if (m_focused_widget)
  388. return m_focused_widget->dispatch_event(event, this);
  389. if (m_main_widget)
  390. return m_main_widget->dispatch_event(event, this);
  391. }
  392. void Window::handle_resize_event(ResizeEvent& event)
  393. {
  394. auto new_size = event.size();
  395. if (m_back_store && m_back_store->size() != new_size)
  396. m_back_store = nullptr;
  397. if (!m_pending_paint_event_rects.is_empty()) {
  398. m_pending_paint_event_rects.clear_with_capacity();
  399. m_pending_paint_event_rects.append({ {}, new_size });
  400. }
  401. m_rect_when_windowless = { {}, new_size };
  402. if (m_main_widget)
  403. m_main_widget->set_relative_rect({ {}, new_size });
  404. }
  405. void Window::handle_input_entered_or_left_event(Core::Event& event)
  406. {
  407. m_is_active_input = event.type() == Event::WindowInputEntered;
  408. if (on_active_input_change)
  409. on_active_input_change(m_is_active_input);
  410. if (m_main_widget)
  411. m_main_widget->dispatch_event(event, this);
  412. if (m_focused_widget)
  413. m_focused_widget->update();
  414. }
  415. void Window::handle_became_active_or_inactive_event(Core::Event& event)
  416. {
  417. if (event.type() == Event::WindowBecameActive)
  418. Application::the()->window_did_become_active({}, *this);
  419. else
  420. Application::the()->window_did_become_inactive({}, *this);
  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_close_request()
  427. {
  428. if (on_close_request) {
  429. if (on_close_request() == Window::CloseRequestDecision::StayOpen)
  430. return;
  431. }
  432. close();
  433. }
  434. void Window::handle_theme_change_event(ThemeChangeEvent& event)
  435. {
  436. if (!m_main_widget)
  437. return;
  438. auto dispatch_theme_change = [&](auto& widget, auto recursive) {
  439. widget.dispatch_event(event, this);
  440. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  441. widget.dispatch_event(event, this);
  442. recursive(widget, recursive);
  443. return IterationDecision::Continue;
  444. });
  445. };
  446. dispatch_theme_change(*m_main_widget.ptr(), dispatch_theme_change);
  447. }
  448. void Window::handle_screen_rect_change_event(ScreenRectChangeEvent& event)
  449. {
  450. if (!m_main_widget)
  451. return;
  452. auto dispatch_screen_rect_change = [&](auto& widget, auto recursive) {
  453. widget.dispatch_event(event, this);
  454. widget.for_each_child_widget([&](auto& widget) -> IterationDecision {
  455. widget.dispatch_event(event, this);
  456. recursive(widget, recursive);
  457. return IterationDecision::Continue;
  458. });
  459. };
  460. dispatch_screen_rect_change(*m_main_widget.ptr(), dispatch_screen_rect_change);
  461. screen_rect_change_event(event);
  462. }
  463. void Window::handle_drag_move_event(DragEvent& event)
  464. {
  465. if (!m_main_widget)
  466. return;
  467. auto result = m_main_widget->hit_test(event.position());
  468. VERIFY(result.widget);
  469. Application::the()->set_drag_hovered_widget({}, result.widget, result.local_position, event.mime_types());
  470. // NOTE: Setting the drag hovered widget may have executed arbitrary code, so re-check that the widget is still there.
  471. if (!result.widget)
  472. return;
  473. if (result.widget->has_pending_drop()) {
  474. DragEvent drag_move_event(static_cast<Event::Type>(event.type()), result.local_position, event.mime_types());
  475. result.widget->dispatch_event(drag_move_event, this);
  476. }
  477. }
  478. void Window::handle_left_event()
  479. {
  480. set_hovered_widget(nullptr);
  481. Application::the()->set_drag_hovered_widget({}, nullptr);
  482. }
  483. void Window::event(Core::Event& event)
  484. {
  485. ScopeGuard guard([&] {
  486. // Accept the event so it doesn't bubble up to parent windows!
  487. event.accept();
  488. });
  489. if (event.type() == Event::Drop)
  490. return handle_drop_event(static_cast<DropEvent&>(event));
  491. if (event.type() == Event::MouseUp || event.type() == Event::MouseDown || event.type() == Event::MouseDoubleClick || event.type() == Event::MouseMove || event.type() == Event::MouseWheel)
  492. return handle_mouse_event(static_cast<MouseEvent&>(event));
  493. if (event.type() == Event::MultiPaint)
  494. return handle_multi_paint_event(static_cast<MultiPaintEvent&>(event));
  495. if (event.type() == Event::KeyUp || event.type() == Event::KeyDown)
  496. return handle_key_event(static_cast<KeyEvent&>(event));
  497. if (event.type() == Event::WindowBecameActive || event.type() == Event::WindowBecameInactive)
  498. return handle_became_active_or_inactive_event(event);
  499. if (event.type() == Event::WindowInputEntered || event.type() == Event::WindowInputLeft)
  500. return handle_input_entered_or_left_event(event);
  501. if (event.type() == Event::WindowCloseRequest)
  502. return handle_close_request();
  503. if (event.type() == Event::WindowLeft)
  504. return handle_left_event();
  505. if (event.type() == Event::Resize)
  506. return handle_resize_event(static_cast<ResizeEvent&>(event));
  507. if (event.type() > Event::__Begin_WM_Events && event.type() < Event::__End_WM_Events)
  508. return wm_event(static_cast<WMEvent&>(event));
  509. if (event.type() == Event::DragMove)
  510. return handle_drag_move_event(static_cast<DragEvent&>(event));
  511. if (event.type() == Event::ThemeChange)
  512. return handle_theme_change_event(static_cast<ThemeChangeEvent&>(event));
  513. if (event.type() == Event::ScreenRectChange)
  514. return handle_screen_rect_change_event(static_cast<ScreenRectChangeEvent&>(event));
  515. Core::Object::event(event);
  516. }
  517. bool Window::is_visible() const
  518. {
  519. return m_visible;
  520. }
  521. void Window::update()
  522. {
  523. auto rect = this->rect();
  524. update({ 0, 0, rect.width(), rect.height() });
  525. }
  526. void Window::force_update()
  527. {
  528. if (!is_visible())
  529. return;
  530. auto rect = this->rect();
  531. WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, { { 0, 0, rect.width(), rect.height() } }, true));
  532. }
  533. void Window::update(const Gfx::IntRect& a_rect)
  534. {
  535. if (!is_visible())
  536. return;
  537. for (auto& pending_rect : m_pending_paint_event_rects) {
  538. if (pending_rect.contains(a_rect)) {
  539. #if UPDATE_COALESCING_DEBUG
  540. dbgln("Ignoring {} since it's contained by pending rect {}", a_rect, pending_rect);
  541. #endif
  542. return;
  543. }
  544. }
  545. if (m_pending_paint_event_rects.is_empty()) {
  546. deferred_invoke([this](auto&) {
  547. auto rects = move(m_pending_paint_event_rects);
  548. if (rects.is_empty())
  549. return;
  550. WindowServerConnection::the().post_message(Messages::WindowServer::InvalidateRect(m_window_id, rects, false));
  551. });
  552. }
  553. m_pending_paint_event_rects.append(a_rect);
  554. }
  555. void Window::set_main_widget(Widget* widget)
  556. {
  557. if (m_main_widget == widget)
  558. return;
  559. if (m_main_widget) {
  560. m_main_widget->set_window(nullptr);
  561. remove_child(*m_main_widget);
  562. }
  563. m_main_widget = widget;
  564. if (m_main_widget) {
  565. add_child(*widget);
  566. auto new_window_rect = rect();
  567. if (m_main_widget->min_width() >= 0)
  568. new_window_rect.set_width(max(new_window_rect.width(), m_main_widget->min_width()));
  569. if (m_main_widget->min_height() >= 0)
  570. new_window_rect.set_height(max(new_window_rect.height(), m_main_widget->min_height()));
  571. set_rect(new_window_rect);
  572. m_main_widget->set_relative_rect({ {}, new_window_rect.size() });
  573. m_main_widget->set_window(this);
  574. if (m_main_widget->focus_policy() != FocusPolicy::NoFocus)
  575. m_main_widget->set_focus(true);
  576. }
  577. update();
  578. }
  579. void Window::set_focused_widget(Widget* widget, FocusSource source)
  580. {
  581. if (m_focused_widget == widget)
  582. return;
  583. WeakPtr<Widget> previously_focused_widget = m_focused_widget;
  584. m_focused_widget = widget;
  585. if (previously_focused_widget) {
  586. Core::EventLoop::current().post_event(*previously_focused_widget, make<FocusEvent>(Event::FocusOut, source));
  587. previously_focused_widget->update();
  588. if (previously_focused_widget && previously_focused_widget->on_focus_change)
  589. previously_focused_widget->on_focus_change(previously_focused_widget->is_focused(), source);
  590. }
  591. if (m_focused_widget) {
  592. Core::EventLoop::current().post_event(*m_focused_widget, make<FocusEvent>(Event::FocusIn, source));
  593. m_focused_widget->update();
  594. if (m_focused_widget && m_focused_widget->on_focus_change)
  595. m_focused_widget->on_focus_change(m_focused_widget->is_focused(), source);
  596. }
  597. }
  598. void Window::set_global_cursor_tracking_widget(Widget* widget)
  599. {
  600. if (widget == m_global_cursor_tracking_widget)
  601. return;
  602. m_global_cursor_tracking_widget = widget;
  603. }
  604. void Window::set_automatic_cursor_tracking_widget(Widget* widget)
  605. {
  606. if (widget == m_automatic_cursor_tracking_widget)
  607. return;
  608. m_automatic_cursor_tracking_widget = widget;
  609. }
  610. void Window::set_has_alpha_channel(bool value)
  611. {
  612. if (m_has_alpha_channel == value)
  613. return;
  614. m_has_alpha_channel = value;
  615. if (!is_visible())
  616. return;
  617. m_pending_paint_event_rects.clear();
  618. m_back_store = nullptr;
  619. m_front_store = nullptr;
  620. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowHasAlphaChannel>(m_window_id, value);
  621. update();
  622. }
  623. void Window::set_double_buffering_enabled(bool value)
  624. {
  625. VERIFY(!is_visible());
  626. m_double_buffering_enabled = value;
  627. }
  628. void Window::set_opacity(float opacity)
  629. {
  630. m_opacity_when_windowless = opacity;
  631. if (!is_visible())
  632. return;
  633. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowOpacity>(m_window_id, opacity);
  634. }
  635. void Window::set_alpha_hit_threshold(float threshold)
  636. {
  637. if (threshold < 0.0f)
  638. threshold = 0.0f;
  639. else if (threshold > 1.0f)
  640. threshold = 1.0f;
  641. if (m_alpha_hit_threshold == threshold)
  642. return;
  643. m_alpha_hit_threshold = threshold;
  644. if (!is_visible())
  645. return;
  646. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowAlphaHitThreshold>(m_window_id, threshold);
  647. }
  648. void Window::set_hovered_widget(Widget* widget)
  649. {
  650. if (widget == m_hovered_widget)
  651. return;
  652. if (m_hovered_widget)
  653. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Leave));
  654. m_hovered_widget = widget;
  655. if (m_hovered_widget)
  656. Core::EventLoop::current().post_event(*m_hovered_widget, make<Event>(Event::Enter));
  657. }
  658. void Window::set_current_backing_store(WindowBackingStore& backing_store, bool flush_immediately)
  659. {
  660. auto& bitmap = backing_store.bitmap();
  661. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBackingStore>(m_window_id, 32, bitmap.pitch(), bitmap.anon_fd(), backing_store.serial(), bitmap.has_alpha_channel(), bitmap.size(), flush_immediately);
  662. }
  663. void Window::flip(const Vector<Gfx::IntRect, 32>& dirty_rects)
  664. {
  665. swap(m_front_store, m_back_store);
  666. set_current_backing_store(*m_front_store);
  667. if (!m_back_store || m_back_store->size() != m_front_store->size()) {
  668. m_back_store = create_backing_store(m_front_store->size());
  669. VERIFY(m_back_store);
  670. memcpy(m_back_store->bitmap().scanline(0), m_front_store->bitmap().scanline(0), m_front_store->bitmap().size_in_bytes());
  671. m_back_store->bitmap().set_volatile();
  672. return;
  673. }
  674. // Copy whatever was painted from the front to the back.
  675. Painter painter(m_back_store->bitmap());
  676. for (auto& dirty_rect : dirty_rects)
  677. painter.blit(dirty_rect.location(), m_front_store->bitmap(), dirty_rect, 1.0f, false);
  678. m_back_store->bitmap().set_volatile();
  679. }
  680. OwnPtr<WindowBackingStore> Window::create_backing_store(const Gfx::IntSize& size)
  681. {
  682. auto format = m_has_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  683. VERIFY(!size.is_empty());
  684. size_t pitch = Gfx::Bitmap::minimum_pitch(size.width(), format);
  685. size_t size_in_bytes = size.height() * pitch;
  686. auto anon_fd = anon_create(round_up_to_power_of_two(size_in_bytes, PAGE_SIZE), O_CLOEXEC);
  687. if (anon_fd < 0) {
  688. perror("anon_create");
  689. return {};
  690. }
  691. // FIXME: Plumb scale factor here eventually.
  692. auto bitmap = Gfx::Bitmap::create_with_anon_fd(format, anon_fd, size, 1, {}, Gfx::Bitmap::ShouldCloseAnonymousFile::No);
  693. if (!bitmap)
  694. return {};
  695. return make<WindowBackingStore>(bitmap.release_nonnull());
  696. }
  697. void Window::set_modal(bool modal)
  698. {
  699. VERIFY(!is_visible());
  700. m_modal = modal;
  701. }
  702. void Window::wm_event(WMEvent&)
  703. {
  704. }
  705. void Window::screen_rect_change_event(ScreenRectChangeEvent&)
  706. {
  707. }
  708. void Window::set_icon(const Gfx::Bitmap* icon)
  709. {
  710. if (m_icon == icon)
  711. return;
  712. Gfx::IntSize icon_size = icon ? icon->size() : Gfx::IntSize(16, 16);
  713. m_icon = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, icon_size);
  714. VERIFY(m_icon);
  715. if (icon) {
  716. Painter painter(*m_icon);
  717. painter.blit({ 0, 0 }, *icon, icon->rect());
  718. }
  719. apply_icon();
  720. }
  721. void Window::apply_icon()
  722. {
  723. if (!m_icon)
  724. return;
  725. if (!is_visible())
  726. return;
  727. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowIconBitmap>(m_window_id, m_icon->to_shareable_bitmap());
  728. }
  729. void Window::start_interactive_resize()
  730. {
  731. WindowServerConnection::the().post_message(Messages::WindowServer::StartWindowResize(m_window_id));
  732. }
  733. Vector<Widget*> Window::focusable_widgets(FocusSource source) const
  734. {
  735. if (!m_main_widget)
  736. return {};
  737. HashTable<Widget*> seen_widgets;
  738. Vector<Widget*> collected_widgets;
  739. Function<void(Widget&)> collect_focusable_widgets = [&](auto& widget) {
  740. bool widget_accepts_focus = false;
  741. switch (source) {
  742. case FocusSource::Keyboard:
  743. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::TabFocus);
  744. break;
  745. case FocusSource::Mouse:
  746. widget_accepts_focus = has_flag(widget.focus_policy(), FocusPolicy::ClickFocus);
  747. break;
  748. case FocusSource::Programmatic:
  749. widget_accepts_focus = widget.focus_policy() != FocusPolicy::NoFocus;
  750. break;
  751. }
  752. if (widget_accepts_focus) {
  753. auto& effective_focus_widget = widget.focus_proxy() ? *widget.focus_proxy() : widget;
  754. if (seen_widgets.set(&effective_focus_widget) == AK::HashSetResult::InsertedNewEntry)
  755. collected_widgets.append(&effective_focus_widget);
  756. }
  757. widget.for_each_child_widget([&](auto& child) {
  758. if (!child.is_visible())
  759. return IterationDecision::Continue;
  760. if (!child.is_enabled())
  761. return IterationDecision::Continue;
  762. collect_focusable_widgets(child);
  763. return IterationDecision::Continue;
  764. });
  765. };
  766. collect_focusable_widgets(const_cast<Widget&>(*m_main_widget));
  767. return collected_widgets;
  768. }
  769. void Window::set_fullscreen(bool fullscreen)
  770. {
  771. if (m_fullscreen == fullscreen)
  772. return;
  773. m_fullscreen = fullscreen;
  774. if (!is_visible())
  775. return;
  776. WindowServerConnection::the().send_sync<Messages::WindowServer::SetFullscreen>(m_window_id, fullscreen);
  777. }
  778. void Window::set_frameless(bool frameless)
  779. {
  780. if (m_frameless == frameless)
  781. return;
  782. m_frameless = frameless;
  783. if (!is_visible())
  784. return;
  785. WindowServerConnection::the().send_sync<Messages::WindowServer::SetFrameless>(m_window_id, frameless);
  786. }
  787. bool Window::is_maximized() const
  788. {
  789. if (!is_visible())
  790. return false;
  791. return WindowServerConnection::the().send_sync<Messages::WindowServer::IsMaximized>(m_window_id)->maximized();
  792. }
  793. void Window::schedule_relayout()
  794. {
  795. if (m_layout_pending)
  796. return;
  797. m_layout_pending = true;
  798. deferred_invoke([this](auto&) {
  799. if (main_widget())
  800. main_widget()->do_layout();
  801. update();
  802. m_layout_pending = false;
  803. });
  804. }
  805. void Window::refresh_system_theme()
  806. {
  807. WindowServerConnection::the().post_message(Messages::WindowServer::RefreshSystemTheme());
  808. }
  809. void Window::for_each_window(Badge<WindowServerConnection>, Function<void(Window&)> callback)
  810. {
  811. for (auto& e : *reified_windows) {
  812. VERIFY(e.value);
  813. callback(*e.value);
  814. }
  815. }
  816. void Window::update_all_windows(Badge<WindowServerConnection>)
  817. {
  818. for (auto& e : *reified_windows) {
  819. e.value->force_update();
  820. }
  821. }
  822. void Window::notify_state_changed(Badge<WindowServerConnection>, bool minimized, bool occluded)
  823. {
  824. m_visible_for_timer_purposes = !minimized && !occluded;
  825. // When double buffering is enabled, minimization/occlusion means we can mark the front bitmap volatile (in addition to the back bitmap.)
  826. // When double buffering is disabled, there is only the back bitmap (which we can now mark volatile!)
  827. auto& store = m_double_buffering_enabled ? m_front_store : m_back_store;
  828. if (!store)
  829. return;
  830. if (minimized || occluded) {
  831. store->bitmap().set_volatile();
  832. } else {
  833. if (!store->bitmap().set_nonvolatile()) {
  834. store = nullptr;
  835. update();
  836. }
  837. }
  838. }
  839. Action* Window::action_for_key_event(const KeyEvent& event)
  840. {
  841. Shortcut shortcut(event.modifiers(), (KeyCode)event.key());
  842. Action* found_action = nullptr;
  843. for_each_child_of_type<Action>([&](auto& action) {
  844. if (action.shortcut() == shortcut) {
  845. found_action = &action;
  846. return IterationDecision::Break;
  847. }
  848. return IterationDecision::Continue;
  849. });
  850. return found_action;
  851. }
  852. void Window::set_base_size(const Gfx::IntSize& base_size)
  853. {
  854. if (m_base_size == base_size)
  855. return;
  856. m_base_size = base_size;
  857. if (is_visible())
  858. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
  859. }
  860. void Window::set_size_increment(const Gfx::IntSize& size_increment)
  861. {
  862. if (m_size_increment == size_increment)
  863. return;
  864. m_size_increment = size_increment;
  865. if (is_visible())
  866. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowBaseSizeAndSizeIncrement>(m_window_id, m_base_size, m_size_increment);
  867. }
  868. void Window::set_resize_aspect_ratio(const Optional<Gfx::IntSize>& ratio)
  869. {
  870. if (m_resize_aspect_ratio == ratio)
  871. return;
  872. m_resize_aspect_ratio = ratio;
  873. if (is_visible())
  874. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowResizeAspectRatio>(m_window_id, m_resize_aspect_ratio);
  875. }
  876. void Window::did_add_widget(Badge<Widget>, Widget&)
  877. {
  878. if (!m_focused_widget)
  879. focus_a_widget_if_possible(FocusSource::Mouse);
  880. }
  881. void Window::did_remove_widget(Badge<Widget>, Widget& widget)
  882. {
  883. if (m_focused_widget == &widget)
  884. m_focused_widget = nullptr;
  885. if (m_hovered_widget == &widget)
  886. m_hovered_widget = nullptr;
  887. if (m_global_cursor_tracking_widget == &widget)
  888. m_global_cursor_tracking_widget = nullptr;
  889. if (m_automatic_cursor_tracking_widget == &widget)
  890. m_automatic_cursor_tracking_widget = nullptr;
  891. }
  892. void Window::set_progress(int progress)
  893. {
  894. VERIFY(m_window_id);
  895. WindowServerConnection::the().post_message(Messages::WindowServer::SetWindowProgress(m_window_id, progress));
  896. }
  897. void Window::update_cursor()
  898. {
  899. Gfx::StandardCursor new_cursor;
  900. if (m_hovered_widget && m_hovered_widget->override_cursor() != Gfx::StandardCursor::None)
  901. new_cursor = m_hovered_widget->override_cursor();
  902. else
  903. new_cursor = m_cursor;
  904. if (m_effective_cursor == new_cursor)
  905. return;
  906. m_effective_cursor = new_cursor;
  907. if (m_custom_cursor)
  908. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCustomCursor>(m_window_id, m_custom_cursor->to_shareable_bitmap());
  909. else
  910. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowCursor>(m_window_id, (u32)m_effective_cursor);
  911. }
  912. void Window::focus_a_widget_if_possible(FocusSource source)
  913. {
  914. auto focusable_widgets = this->focusable_widgets(source);
  915. if (!focusable_widgets.is_empty())
  916. set_focused_widget(focusable_widgets[0], source);
  917. }
  918. void Window::did_disable_focused_widget(Badge<Widget>)
  919. {
  920. focus_a_widget_if_possible(FocusSource::Mouse);
  921. }
  922. bool Window::is_active() const
  923. {
  924. VERIFY(Application::the());
  925. return this == Application::the()->active_window();
  926. }
  927. Gfx::Bitmap* Window::back_bitmap()
  928. {
  929. return m_back_store ? &m_back_store->bitmap() : nullptr;
  930. }
  931. void Window::set_menubar(RefPtr<MenuBar> menubar)
  932. {
  933. if (m_menubar == menubar)
  934. return;
  935. if (m_menubar)
  936. m_menubar->notify_removed_from_window({});
  937. m_menubar = move(menubar);
  938. if (m_window_id && m_menubar) {
  939. m_menubar->notify_added_to_window({});
  940. WindowServerConnection::the().send_sync<Messages::WindowServer::SetWindowMenubar>(m_window_id, m_menubar->menubar_id());
  941. }
  942. }
  943. }