gpui: Make image example work regardless of how it is run (#24645)

This PR updates the GPUI `image` example such that it works when run in
the following ways:

- `cargo run -p gpui --example image` from the repository root
- `cargo run --example image` from within `crates/gpui`

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2025-02-11 08:34:38 -05:00 committed by GitHub
parent 6a40a400bd
commit 04d65cb3cd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 9 deletions

1
Cargo.lock generated
View file

@ -5546,6 +5546,7 @@ dependencies = [
"rand 0.8.5",
"raw-window-handle",
"refineable",
"reqwest_client",
"resvg",
"schemars",
"seahash",

View file

@ -202,11 +202,12 @@ windows-core = "0.58"
backtrace = "0.3"
collections = { workspace = true, features = ["test-support"] }
env_logger.workspace = true
rand.workspace = true
util = { workspace = true, features = ["test-support"] }
http_client = { workspace = true, features = ["test-support"] }
unicode-segmentation.workspace = true
lyon = { version = "1.0", features = ["extra"] }
rand.workspace = true
unicode-segmentation.workspace = true
reqwest_client = { workspace = true, features = ["test-support"] }
util = { workspace = true, features = ["test-support"] }
[target.'cfg(target_os = "windows")'.build-dependencies]
embed-resource = "3.0"

View file

@ -1,6 +1,5 @@
use std::fs;
use std::path::PathBuf;
use std::str::FromStr;
use std::sync::Arc;
use anyhow::Result;
@ -9,6 +8,7 @@ use gpui::{
Bounds, Context, ImageSource, KeyBinding, Menu, MenuItem, Point, SharedString, SharedUri,
TitlebarOptions, Window, WindowBounds, WindowOptions,
};
use reqwest_client::ReqwestClient;
struct Assets {
base: PathBuf,
@ -127,11 +127,16 @@ actions!(image, [Quit]);
fn main() {
env_logger::init();
let manifest_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
Application::new()
.with_assets(Assets {
base: PathBuf::from("crates/gpui/examples"),
base: manifest_dir.join("examples"),
})
.run(|cx: &mut App| {
.run(move |cx: &mut App| {
let http_client = ReqwestClient::user_agent("gpui example").unwrap();
cx.set_http_client(Arc::new(http_client));
cx.activate(true);
cx.on_action(|_: &Quit, cx| cx.quit());
cx.bind_keys([KeyBinding::new("cmd-q", Quit, None)]);
@ -158,9 +163,7 @@ fn main() {
cx.open_window(window_options, |_, cx| {
cx.new(|_| ImageShowcase {
// Relative path to your root project path
local_resource: PathBuf::from_str("crates/gpui/examples/image/app-icon.png")
.unwrap()
.into(),
local_resource: manifest_dir.join("examples/image/app-icon.png").into(),
remote_resource: "https://picsum.photos/512/512".into(),