chore: Upgrade GPUI 2025/02/10 (#610)

- Replace `Window::parent_view_id()` with `Window::current_view()`
https://github.com/zed-industries/zed/pull/24212
- Fix SvgImg crash by move `use_asset` to `request_layout`.
- Fix Input to implement `character_index_for_point` method
https://github.com/zed-industries/zed/pull/23989
- Fix Input IME position to under the cursor line.
  <img width="504" alt="image"
src="https://github.com/user-attachments/assets/8a6f56c8-6aae-4c0c-8c5a-3959d6a9d224"
/>
- Fix Input overflow sub crash.
This commit is contained in:
Jason Lee 2025-02-10 11:53:25 +08:00 committed by GitHub
parent b1bae7ca21
commit a5db381ef4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 111 additions and 290 deletions

278
Cargo.lock generated
View file

@ -133,7 +133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52bca67b61cb81e5553babde81b8211f713cb6db79766f80168f3e5f40ea6c82"
dependencies = [
"ash",
"raw-window-handle 0.6.2",
"raw-window-handle",
"raw-window-metal",
]
@ -550,7 +550,7 @@ dependencies = [
"objc2-metal",
"objc2-quartz-core",
"objc2-ui-kit",
"raw-window-handle 0.6.2",
"raw-window-handle",
"slab",
"wasm-bindgen",
"web-sys",
@ -952,7 +952,7 @@ dependencies = [
[[package]]
name = "collections"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"indexmap 2.7.1",
"rustc-hash 2.1.0",
@ -1316,7 +1316,7 @@ dependencies = [
[[package]]
name = "derive_refineable"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"proc-macro2",
"quote",
@ -2275,7 +2275,7 @@ dependencies = [
[[package]]
name = "gpui"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"anyhow",
"as-raw-xcb-connection",
@ -2326,7 +2326,7 @@ dependencies = [
"postage",
"profiling",
"rand 0.8.5",
"raw-window-handle 0.6.2",
"raw-window-handle",
"refineable",
"resvg",
"schemars",
@ -2351,9 +2351,8 @@ dependencies = [
"wayland-cursor",
"wayland-protocols",
"wayland-protocols-plasma",
"windows 0.58.0",
"windows",
"windows-core 0.58.0",
"wry 0.41.0",
"x11-clipboard",
"x11rb",
"xim",
@ -2382,13 +2381,13 @@ dependencies = [
"unicode-segmentation",
"usvg",
"uuid",
"wry 0.48.1",
"wry",
]
[[package]]
name = "gpui_macros"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"proc-macro2",
"quote",
@ -2578,7 +2577,7 @@ dependencies = [
[[package]]
name = "http_client"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"anyhow",
"bytes",
@ -3145,7 +3144,7 @@ dependencies = [
[[package]]
name = "media"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"anyhow",
"bindgen",
@ -3247,20 +3246,6 @@ dependencies = [
"getrandom 0.2.15",
]
[[package]]
name = "ndk"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "451422b7e4718271c8b5b3aadf5adedba43dc76312454b387e98fae0fc951aa0"
dependencies = [
"bitflags 1.3.2",
"jni-sys",
"ndk-sys 0.4.1+23.1.7779620",
"num_enum 0.5.11",
"raw-window-handle 0.5.2",
"thiserror 1.0.66",
]
[[package]]
name = "ndk"
version = "0.9.0"
@ -3270,27 +3255,12 @@ dependencies = [
"bitflags 2.6.0",
"jni-sys",
"log",
"ndk-sys 0.6.0+11769913",
"num_enum 0.7.3",
"raw-window-handle 0.6.2",
"ndk-sys",
"num_enum",
"raw-window-handle",
"thiserror 1.0.66",
]
[[package]]
name = "ndk-context"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b"
[[package]]
name = "ndk-sys"
version = "0.4.1+23.1.7779620"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cf2aae958bd232cac5069850591667ad422d263686d75b52a065f9badeee5a3"
dependencies = [
"jni-sys",
]
[[package]]
name = "ndk-sys"
version = "0.6.0+11769913"
@ -3469,34 +3439,13 @@ dependencies = [
"libc",
]
[[package]]
name = "num_enum"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1f646caf906c20226733ed5b1374287eb97e3c2a5c227ce668c1f2ce20ae57c9"
dependencies = [
"num_enum_derive 0.5.11",
]
[[package]]
name = "num_enum"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179"
dependencies = [
"num_enum_derive 0.7.3",
]
[[package]]
name = "num_enum_derive"
version = "0.5.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcbff9bc912032c62bf65ef1d5aea88983b420f4f839db1e9b0c281a25c9c799"
dependencies = [
"proc-macro-crate 1.3.1",
"proc-macro2",
"quote",
"syn 1.0.109",
"num_enum_derive",
]
[[package]]
@ -3505,7 +3454,7 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56"
dependencies = [
"proc-macro-crate 3.2.0",
"proc-macro-crate 1.3.1",
"proc-macro2",
"quote",
"syn 2.0.96",
@ -3518,7 +3467,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915b1b472bc21c53464d6c8461c9d3af805ba1ef837e1cac254428f4a77177b1"
dependencies = [
"malloc_buf",
"objc_exception",
]
[[package]]
@ -3739,24 +3687,6 @@ dependencies = [
"objc2-foundation",
]
[[package]]
name = "objc_exception"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad970fb455818ad6cba4c122ad012fae53ae8b4795f86378bce65e4f6bab2ca4"
dependencies = [
"cc",
]
[[package]]
name = "objc_id"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c92d4ddb4bd7b50d730c215ff871754d0da6b2178849f8a2a2ab69712d0c073b"
dependencies = [
"objc",
]
[[package]]
name = "once_cell"
version = "1.20.2"
@ -4423,12 +4353,6 @@ dependencies = [
"rgb",
]
[[package]]
name = "raw-window-handle"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2ff9a1f06a88b01621b7ae906ef0211290d1c8a168a15542486a8f61c0833b9"
[[package]]
name = "raw-window-handle"
version = "0.6.2"
@ -4444,7 +4368,7 @@ dependencies = [
"cocoa 0.25.0",
"core-graphics 0.23.2",
"objc",
"raw-window-handle 0.6.2",
"raw-window-handle",
]
[[package]]
@ -4500,7 +4424,7 @@ dependencies = [
[[package]]
name = "refineable"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"derive_refineable",
]
@ -4828,7 +4752,7 @@ checksum = "d369a96f978623eb3dc28807c4852d6cc617fed53da5d3c400feff1ef34a714a"
[[package]]
name = "semantic_version"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"anyhow",
"serde",
@ -5185,6 +5109,7 @@ dependencies = [
"gpui",
"gpui-component",
"rand 0.8.5",
"raw-window-handle",
"regex",
"rust-embed",
"serde",
@ -5267,7 +5192,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "sum_tree"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"arrayvec",
"log",
@ -5927,7 +5852,7 @@ checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9"
[[package]]
name = "util"
version = "0.1.0"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#d45d61a264afd26e20e7e06e6c4121ce8e9fd046"
source = "git+https://github.com/huacnlee/zed.git?branch=webview#7c1a1305091a5b2463ca9779dcf2b97a0ce52f22"
dependencies = [
"anyhow",
"async-fs 2.1.2",
@ -6262,43 +6187,18 @@ dependencies = [
"system-deps",
]
[[package]]
name = "webview2-com"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6516cfa64c6b3212686080eeec378e662c2af54bb2a5b2a22749673f5cb2226f"
dependencies = [
"webview2-com-macros 0.7.0",
"webview2-com-sys 0.31.0",
"windows 0.57.0",
"windows-core 0.57.0",
"windows-implement 0.57.0",
"windows-interface 0.57.0",
]
[[package]]
name = "webview2-com"
version = "0.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "823e7ebcfaea51e78f72c87fc3b65a1e602c321f407a0b36dbb327d7bb7cd921"
dependencies = [
"webview2-com-macros 0.8.0",
"webview2-com-sys 0.34.0",
"windows 0.58.0",
"webview2-com-macros",
"webview2-com-sys",
"windows",
"windows-core 0.58.0",
"windows-implement 0.58.0",
"windows-interface 0.58.0",
]
[[package]]
name = "webview2-com-macros"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac1345798ecd8122468840bcdf1b95e5dc6d2206c5e4b0eafa078d061f59c9bc"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
"windows-implement",
"windows-interface",
]
[[package]]
@ -6312,17 +6212,6 @@ dependencies = [
"syn 2.0.96",
]
[[package]]
name = "webview2-com-sys"
version = "0.31.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c76d5b77320ff155660be1df3e6588bc85c75f1a9feef938cc4dc4dd60d1d7cf"
dependencies = [
"thiserror 1.0.66",
"windows 0.57.0",
"windows-core 0.57.0",
]
[[package]]
name = "webview2-com-sys"
version = "0.34.0"
@ -6330,7 +6219,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a82bce72db6e5ee83c68b5de1e2cd6ea195b9fbff91cb37df5884cbe3222df4"
dependencies = [
"thiserror 1.0.66",
"windows 0.58.0",
"windows",
"windows-core 0.58.0",
]
@ -6371,16 +6260,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12342cb4d8e3b046f3d80effd474a7a02447231330ef77d71daa6fbc40681143"
dependencies = [
"windows-core 0.57.0",
"windows-targets 0.52.6",
]
[[package]]
name = "windows"
version = "0.58.0"
@ -6400,42 +6279,19 @@ dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
dependencies = [
"windows-implement 0.57.0",
"windows-interface 0.57.0",
"windows-result 0.1.2",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-core"
version = "0.58.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99"
dependencies = [
"windows-implement 0.58.0",
"windows-interface 0.58.0",
"windows-result 0.2.0",
"windows-implement",
"windows-interface",
"windows-result",
"windows-strings",
"windows-targets 0.52.6",
]
[[package]]
name = "windows-implement"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9107ddc059d5b6fbfbffdfa7a7fe3e22a226def0b2608f72e9d552763d3e1ad7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
]
[[package]]
name = "windows-implement"
version = "0.58.0"
@ -6447,17 +6303,6 @@ dependencies = [
"syn 2.0.96",
]
[[package]]
name = "windows-interface"
version = "0.57.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29bee4b38ea3cde66011baa44dba677c432a78593e202392d1e9070cf2a7fca7"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
]
[[package]]
name = "windows-interface"
version = "0.58.0"
@ -6469,15 +6314,6 @@ dependencies = [
"syn 2.0.96",
]
[[package]]
name = "windows-result"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
dependencies = [
"windows-targets 0.52.6",
]
[[package]]
name = "windows-result"
version = "0.2.0"
@ -6493,7 +6329,7 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10"
dependencies = [
"windows-result 0.2.0",
"windows-result",
"windows-targets 0.52.6",
]
@ -6757,48 +6593,6 @@ dependencies = [
"winapi",
]
[[package]]
name = "wry"
version = "0.41.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68b00c945786b02d7805d09a969fa36d0eee4e0bd4fb3ec2a79d2bf45a1b44cd"
dependencies = [
"base64",
"block",
"cocoa 0.25.0",
"core-graphics 0.23.2",
"crossbeam-channel",
"dpi",
"dunce",
"gdkx11",
"gtk",
"html5ever",
"http",
"javascriptcore-rs",
"jni",
"kuchikiki",
"libc",
"ndk 0.7.0",
"ndk-context",
"ndk-sys 0.4.1+23.1.7779620",
"objc",
"objc_id",
"once_cell",
"percent-encoding",
"raw-window-handle 0.6.2",
"sha2",
"soup3",
"tao-macros",
"thiserror 1.0.66",
"webkit2gtk",
"webkit2gtk-sys",
"webview2-com 0.31.0",
"windows 0.57.0",
"windows-core 0.57.0",
"windows-version",
"x11-dl",
]
[[package]]
name = "wry"
version = "0.48.1"
@ -6819,7 +6613,7 @@ dependencies = [
"jni",
"kuchikiki",
"libc",
"ndk 0.9.0",
"ndk",
"objc2",
"objc2-app-kit",
"objc2-foundation",
@ -6827,7 +6621,7 @@ dependencies = [
"objc2-web-kit",
"once_cell",
"percent-encoding",
"raw-window-handle 0.6.2",
"raw-window-handle",
"sha2",
"soup3",
"tao-macros",
@ -6835,8 +6629,8 @@ dependencies = [
"url",
"webkit2gtk",
"webkit2gtk-sys",
"webview2-com 0.34.0",
"windows 0.58.0",
"webview2-com",
"windows",
"windows-core 0.58.0",
"windows-version",
"x11-dl",

View file

@ -17,6 +17,7 @@ rust-embed.workspace = true
serde = "1"
serde_json = "1"
unindent = "0.2.3"
raw-window-handle = { version = "0.6", features = ["std"] }
[lints]
workspace = true

View file

@ -6,6 +6,7 @@ use gpui::{
Focusable, InteractiveElement as _, IntoElement, ParentElement, Render, SharedString, Styled,
Task, Timer, WeakEntity, Window,
};
use raw_window_handle::HasWindowHandle;
use gpui_component::{
button::{Button, ButtonVariant, ButtonVariants as _},
@ -524,7 +525,9 @@ impl Render for ModalStory {
cx.listener(|_, _, window, cx| {
let webview = cx.new(|cx| {
let webview = wry::WebViewBuilder::new()
.build_as_child(&window.raw_window_handle())
.build_as_child(
&window.window_handle().expect("No window handle"),
)
.unwrap();
WebView::new(webview, window, cx)

View file

@ -9,6 +9,7 @@ use gpui_component::{
webview::WebView,
wry, ActiveTheme,
};
use raw_window_handle::HasWindowHandle;
pub struct WebViewStory {
focus_handle: FocusHandle,
@ -32,7 +33,7 @@ impl WebViewStory {
let webview = cx.new(|cx| {
let webview = wry::WebViewBuilder::new()
.build_as_child(&window.raw_window_handle())
.build_as_child(&window.window_handle().expect("No window handle"))
.unwrap();
WebView::new(webview, window, cx)
});

View file

@ -203,10 +203,7 @@ impl RenderOnce for Drawer {
// Body
div().flex_1().overflow_hidden().child(
v_flex()
.scrollable(
window.parent_view_id().unwrap_or_default(),
ScrollbarAxis::Vertical,
)
.scrollable(window.current_view(), ScrollbarAxis::Vertical)
.child(self.content),
),
)

View file

@ -514,7 +514,7 @@ impl Element for TextElement {
}
for line in prepaint.lines.iter() {
let p = point(origin.x, origin.y + offset_y);
_ = line.paint(p, line_height, TextAlign::Left, window, cx);
_ = line.paint(p, line_height, TextAlign::Left, None, window, cx);
offset_y += line.size(line_height).height;
}

View file

@ -1522,10 +1522,14 @@ impl EntityInputHandler for TextInput {
let mut index_offset = 0;
for line in lines.iter() {
if let Some(p) = line.position_for_index(range.start - index_offset, line_height) {
if let Some(p) =
line.position_for_index(range.start.saturating_sub(index_offset), line_height)
{
start_origin = Some(p + point(px(0.), y_offset));
}
if let Some(p) = line.position_for_index(range.end - index_offset, line_height) {
if let Some(p) =
line.position_for_index(range.end.saturating_sub(index_offset), line_height)
{
end_origin = Some(p + point(px(0.), y_offset));
}
@ -1537,11 +1541,34 @@ impl EntityInputHandler for TextInput {
index_offset += line.len();
}
let start_origin = start_origin.unwrap_or_default();
let end_origin = end_origin.unwrap_or_default();
Some(Bounds::from_corners(
bounds.origin + start_origin.unwrap_or_default(),
bounds.origin + end_origin.unwrap_or_default(),
bounds.origin + start_origin,
// + line_height for show IME panel under the cursor line.
bounds.origin + point(end_origin.x, end_origin.y + line_height),
))
}
fn character_index_for_point(
&mut self,
point: gpui::Point<Pixels>,
_window: &mut Window,
_cx: &mut Context<Self>,
) -> Option<usize> {
let line_height = self.last_line_height;
let line_point = self.last_bounds?.localize(&point)?;
let lines = self.last_layout.as_ref()?;
for line in lines.iter() {
if let Ok(utf8_index) = line.index_for_position(line_point, line_height) {
return Some(self.offset_to_utf16(utf8_index));
}
}
None
}
}
impl Focusable for TextInput {

View file

@ -5,9 +5,9 @@ use std::{
};
use gpui::{
px, size, App, Asset, Bounds, Element, Hitbox, ImageCacheError, InteractiveElement,
Interactivity, IntoElement, IsZero, Pixels, RenderImage, SharedString, Size, StyleRefinement,
Styled, Window,
px, size, App, Asset, Bounds, Element, ElementId, GlobalElementId, Hitbox, ImageCacheError,
InteractiveElement, Interactivity, IntoElement, IsZero, Pixels, RenderImage, SharedString,
Size, StyleRefinement, Styled, Window,
};
use image::Frame;
use smallvec::SmallVec;
@ -193,16 +193,16 @@ impl IntoElement for SvgImg {
}
impl Element for SvgImg {
type RequestLayoutState = ();
type PrepaintState = Option<Hitbox>;
type RequestLayoutState = Option<Arc<RenderImage>>;
type PrepaintState = (Option<Hitbox>, Option<Arc<RenderImage>>);
fn id(&self) -> Option<gpui::ElementId> {
fn id(&self) -> Option<ElementId> {
self.interactivity.element_id.clone()
}
fn request_layout(
&mut self,
global_id: Option<&gpui::GlobalElementId>,
global_id: Option<&GlobalElementId>,
window: &mut Window,
cx: &mut App,
) -> (gpui::LayoutId, Self::RequestLayoutState) {
@ -212,56 +212,55 @@ impl Element for SvgImg {
window.request_layout(style, None, cx)
});
(layout_id, ())
let source = self.source.clone();
let size = self.size;
let data = if let Some(source) = source {
match window.use_asset::<Image>(&ImageSource { source, size }, cx) {
Some(Ok(data)) => Some(data),
_ => None,
}
} else {
None
};
(layout_id, data)
}
fn prepaint(
&mut self,
global_id: Option<&gpui::GlobalElementId>,
bounds: gpui::Bounds<gpui::Pixels>,
_: &mut Self::RequestLayoutState,
global_id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
state: &mut Self::RequestLayoutState,
window: &mut Window,
cx: &mut App,
) -> Self::PrepaintState {
self.interactivity.prepaint(
let hitbox = self.interactivity.prepaint(
global_id,
bounds,
bounds.size,
window,
cx,
|_, _, hitbox, _, _| hitbox,
)
);
(hitbox, state.clone())
}
fn paint(
&mut self,
global_id: Option<&gpui::GlobalElementId>,
bounds: gpui::Bounds<gpui::Pixels>,
global_id: Option<&GlobalElementId>,
bounds: Bounds<Pixels>,
_: &mut Self::RequestLayoutState,
hitbox: &mut Self::PrepaintState,
state: &mut Self::PrepaintState,
window: &mut Window,
cx: &mut App,
) {
let source = self.source.clone();
self.interactivity.paint(
global_id,
bounds,
hitbox.as_ref(),
window,
cx,
|_style, window, cx| {
let size = self.size;
let data = if let Some(source) = source {
match window.use_asset::<Image>(&ImageSource { source, size }, cx) {
Some(Ok(data)) => Some(data),
_ => None,
}
} else {
None
};
let size = self.size;
let hitbox = state.0.as_ref();
let data = state.1.clone();
self.interactivity
.paint(global_id, bounds, hitbox, window, cx, |_, window, _| {
if let Some(data) = data {
// To calculate the ratio of the original image size to the container bounds size.
// Scale by shortest side (width or height) to get a fit image.
@ -294,8 +293,7 @@ impl Element for SvgImg {
Err(err) => eprintln!("failed to paint svg image: {:?}", err),
}
}
},
)
})
}
}