Window.cpp 33 KB

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