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

Marshall Bowers created

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

Change summary

Cargo.lock                          |  1 +
crates/gpui/Cargo.toml              |  7 ++++---
crates/gpui/examples/image/image.rs | 15 +++++++++------
3 files changed, 14 insertions(+), 9 deletions(-)

Detailed changes

Cargo.lock 🔗

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

crates/gpui/Cargo.toml 🔗

@@ -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"

crates/gpui/examples/image/image.rs 🔗

@@ -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(),