Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Add `SharedUrl` type (#3975)
Marshall Bowersf0ef63bfa0a6
diff --git a/crates/client/src/user.rs b/crates/client/src/user.rsindex 1c288c875db..4453bb40eaa 100644--- a/crates/client/src/user.rs+++ b/crates/client/src/user.rs@@ -3,7 +3,7 @@ use anyhow::{anyhow, Context, Result};use collections::{hash_map::Entry, HashMap, HashSet};use feature_flags::FeatureFlagAppExt;use futures::{channel::mpsc, Future, StreamExt};-use gpui::{AsyncAppContext, EventEmitter, Model, ModelContext, SharedString, Task};+use gpui::{AsyncAppContext, EventEmitter, Model, ModelContext, SharedUrl, Task};use postage::{sink::Sink, watch};use rpc::proto::{RequestMessage, UsersResponse};use std::sync::{Arc, Weak};@@ -19,7 +19,7 @@ pub struct ParticipantIndex(pub u32);pub struct User {pub id: UserId,pub github_login: String,- pub avatar_uri: SharedString,+ pub avatar_uri: SharedUrl,}#[derive(Clone, Debug, PartialEq, Eq)]diff --git a/crates/collab_ui/src/notifications/collab_notification.rs b/crates/collab_ui/src/notifications/collab_notification.rsindex e2ef06f9a58..fa0b0a1b147 100644--- a/crates/collab_ui/src/notifications/collab_notification.rs+++ b/crates/collab_ui/src/notifications/collab_notification.rs@@ -1,10 +1,10 @@-use gpui::{img, prelude::*, AnyElement};+use gpui::{img, prelude::*, AnyElement, SharedUrl};use smallvec::SmallVec;use ui::prelude::*;#[derive(IntoElement)]pub struct CollabNotification {- avatar_uri: SharedString,+ avatar_uri: SharedUrl,accept_button: Button,dismiss_button: Button,children: SmallVec<[AnyElement; 2]>,@@ -12,7 +12,7 @@ pub struct CollabNotification {impl CollabNotification {pub fn new(- avatar_uri: impl Into<SharedString>,+ avatar_uri: impl Into<SharedUrl>,accept_button: Button,dismiss_button: Button,) -> Self {diff --git a/crates/gpui/src/elements/img.rs b/crates/gpui/src/elements/img.rsindex 650b5b666bc..71a51351fdb 100644--- a/crates/gpui/src/elements/img.rs+++ b/crates/gpui/src/elements/img.rs@@ -2,7 +2,7 @@ use std::sync::Arc;use crate::{point, size, BorrowWindow, Bounds, DevicePixels, Element, ImageData, InteractiveElement,- InteractiveElementState, Interactivity, IntoElement, LayoutId, Pixels, SharedString, Size,+ InteractiveElementState, Interactivity, IntoElement, LayoutId, Pixels, SharedUrl, Size,StyleRefinement, Styled, WindowContext,};use futures::FutureExt;@@ -12,13 +12,13 @@ use util::ResultExt;#[derive(Clone, Debug)]pub enum ImageSource {/// Image content will be loaded from provided URI at render time.- Uri(SharedString),+ Uri(SharedUrl),Data(Arc<ImageData>),Surface(CVImageBuffer),}-impl From<SharedString> for ImageSource {- fn from(value: SharedString) -> Self {+impl From<SharedUrl> for ImageSource {+ fn from(value: SharedUrl) -> Self {Self::Uri(value)}}diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rsindex d5236d8f08c..6f5e30149d9 100644--- a/crates/gpui/src/gpui.rs+++ b/crates/gpui/src/gpui.rs@@ -18,6 +18,7 @@ mod platform;pub mod prelude;mod scene;mod shared_string;+mod shared_url;mod style;mod styled;mod subscription;@@ -67,6 +68,7 @@ pub use refineable::*;pub use scene::*;use seal::Sealed;pub use shared_string::*;+pub use shared_url::*;pub use smol::Timer;pub use style::*;pub use styled::*;diff --git a/crates/gpui/src/image_cache.rs b/crates/gpui/src/image_cache.rsindex f80b0f0c2f7..0d6ec81557a 100644--- a/crates/gpui/src/image_cache.rs+++ b/crates/gpui/src/image_cache.rs@@ -1,4 +1,4 @@-use crate::{ImageData, ImageId, SharedString};+use crate::{ImageData, ImageId, SharedUrl};use collections::HashMap;use futures::{future::{BoxFuture, Shared},@@ -44,7 +44,7 @@ impl From<ImageError> for Error {pub struct ImageCache {client: Arc<dyn HttpClient>,- images: Arc<Mutex<HashMap<SharedString, FetchImageFuture>>>,+ images: Arc<Mutex<HashMap<SharedUrl, FetchImageFuture>>>,}type FetchImageFuture = Shared<BoxFuture<'static, Result<Arc<ImageData>, Error>>>;@@ -59,7 +59,7 @@ impl ImageCache {pub fn get(&self,- uri: impl Into<SharedString>,+ uri: impl Into<SharedUrl>,) -> Shared<BoxFuture<'static, Result<Arc<ImageData>, Error>>> {let uri = uri.into();let mut images = self.images.lock();diff --git a/crates/gpui/src/shared_url.rs b/crates/gpui/src/shared_url.rsnew file mode 100644index 00000000000..8fb90189436--- /dev/null+++ b/crates/gpui/src/shared_url.rs@@ -0,0 +1,25 @@+use derive_more::{Deref, DerefMut};++use crate::SharedString;++/// A [`SharedString`] containing a URL.+#[derive(Deref, DerefMut, Default, PartialEq, Eq, Hash, Clone)]+pub struct SharedUrl(SharedString);++impl std::fmt::Debug for SharedUrl {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ self.0.fmt(f)+ }+}++impl std::fmt::Display for SharedUrl {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(f, "{}", self.0.as_ref())+ }+}++impl<T: Into<SharedString>> From<T> for SharedUrl {+ fn from(value: T) -> Self {+ Self(value.into())+ }+}diff --git a/crates/ui/src/components/stories/list_item.rs b/crates/ui/src/components/stories/list_item.rsindex 487f1427917..a25b07df849 100644--- a/crates/ui/src/components/stories/list_item.rs+++ b/crates/ui/src/components/stories/list_item.rs@@ -1,4 +1,4 @@-use gpui::Render;+use gpui::{Render, SharedUrl};use story::Story;use crate::{prelude::*, Avatar};@@ -43,7 +43,7 @@ impl Render for ListItemStory {.child(ListItem::new("with_start slot avatar").child("Hello, world!")- .start_slot(Avatar::new(SharedString::from(+ .start_slot(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1714999?v=4",))),)@@ -51,7 +51,7 @@ impl Render for ListItemStory {.child(ListItem::new("with_left_avatar").child("Hello, world!")- .end_slot(Avatar::new(SharedString::from(+ .end_slot(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1714999?v=4",))),)@@ -62,23 +62,23 @@ impl Render for ListItemStory {.end_slot(h_stack().gap_2()- .child(Avatar::new(SharedString::from(+ .child(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1789?v=4",)))- .child(Avatar::new(SharedString::from(+ .child(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1789?v=4",)))- .child(Avatar::new(SharedString::from(+ .child(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1789?v=4",)))- .child(Avatar::new(SharedString::from(+ .child(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1789?v=4",)))- .child(Avatar::new(SharedString::from(+ .child(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1789?v=4",))),)- .end_hover_slot(Avatar::new(SharedString::from(+ .end_hover_slot(Avatar::new(SharedUrl::from("https://avatars.githubusercontent.com/u/1714999?v=4",))),)