Add the ability to render icons as indicators (#11273)

This PR adds the ability to render `Icon`s as an `Indicator`.

Release Notes:

- N/A

Co-authored-by: Nate Butler <nate@zed.dev>
This commit is contained in:
Marshall Bowers 2024-05-01 17:52:26 -04:00 committed by GitHub
parent 4f5312804d
commit 4739797e5d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 116 additions and 23 deletions

View file

@ -1,8 +1,46 @@
use gpui::{svg, Hsla, IntoElement, Rems, Transformation};
use gpui::{svg, AnimationElement, Hsla, IntoElement, Rems, Transformation};
use strum::EnumIter;
use crate::{prelude::*, Indicator};
#[derive(IntoElement)]
pub enum AnyIcon {
Icon(Icon),
AnimatedIcon(AnimationElement<Icon>),
}
impl AnyIcon {
/// Returns a new [`AnyIcon`] after applying the given mapping function
/// to the contained [`Icon`].
pub fn map(self, f: impl FnOnce(Icon) -> Icon) -> Self {
match self {
Self::Icon(icon) => Self::Icon(f(icon)),
Self::AnimatedIcon(animated_icon) => Self::AnimatedIcon(animated_icon.map_element(f)),
}
}
}
impl From<Icon> for AnyIcon {
fn from(value: Icon) -> Self {
Self::Icon(value)
}
}
impl From<AnimationElement<Icon>> for AnyIcon {
fn from(value: AnimationElement<Icon>) -> Self {
Self::AnimatedIcon(value)
}
}
impl RenderOnce for AnyIcon {
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
match self {
Self::Icon(icon) => icon.into_any_element(),
Self::AnimatedIcon(animated_icon) => animated_icon.into_any_element(),
}
}
}
#[derive(Default, PartialEq, Copy, Clone)]
pub enum IconSize {
Indicator,
@ -236,7 +274,7 @@ impl IconName {
pub struct Icon {
path: SharedString,
color: Color,
size: IconSize,
size: Rems,
transformation: Transformation,
}
@ -245,7 +283,7 @@ impl Icon {
Self {
path: icon.path().into(),
color: Color::default(),
size: IconSize::default(),
size: IconSize::default().rems(),
transformation: Transformation::default(),
}
}
@ -254,7 +292,7 @@ impl Icon {
Self {
path: path.into(),
color: Color::default(),
size: IconSize::default(),
size: IconSize::default().rems(),
transformation: Transformation::default(),
}
}
@ -265,6 +303,14 @@ impl Icon {
}
pub fn size(mut self, size: IconSize) -> Self {
self.size = size.rems();
self
}
/// Sets a custom size for the icon, in [`Rems`].
///
/// Not to be exposed outside of the `ui` crate.
pub(crate) fn custom_size(mut self, size: Rems) -> Self {
self.size = size;
self
}
@ -279,7 +325,7 @@ impl RenderOnce for Icon {
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
svg()
.with_transformation(self.transformation)
.size(self.size.rems())
.size(self.size)
.flex_none()
.path(self.path)
.text_color(self.color.color(cx))