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:
parent
4f5312804d
commit
4739797e5d
3 changed files with 116 additions and 23 deletions
|
@ -1,17 +1,17 @@
|
|||
use gpui::Position;
|
||||
use gpui::Transformation;
|
||||
|
||||
use crate::prelude::*;
|
||||
use crate::{prelude::*, AnyIcon};
|
||||
|
||||
#[derive(Default)]
|
||||
pub enum IndicatorStyle {
|
||||
#[default]
|
||||
Dot,
|
||||
Bar,
|
||||
Icon(AnyIcon),
|
||||
}
|
||||
|
||||
#[derive(IntoElement)]
|
||||
pub struct Indicator {
|
||||
position: Position,
|
||||
style: IndicatorStyle,
|
||||
pub color: Color,
|
||||
}
|
||||
|
@ -19,7 +19,6 @@ pub struct Indicator {
|
|||
impl Indicator {
|
||||
pub fn dot() -> Self {
|
||||
Self {
|
||||
position: Position::Relative,
|
||||
style: IndicatorStyle::Dot,
|
||||
color: Color::Default,
|
||||
}
|
||||
|
@ -27,32 +26,71 @@ impl Indicator {
|
|||
|
||||
pub fn bar() -> Self {
|
||||
Self {
|
||||
position: Position::Relative,
|
||||
style: IndicatorStyle::Dot,
|
||||
color: Color::Default,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn icon(icon: impl Into<AnyIcon>) -> Self {
|
||||
Self {
|
||||
style: IndicatorStyle::Icon(icon.into()),
|
||||
color: Color::Default,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn color(mut self, color: Color) -> Self {
|
||||
self.color = color;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn absolute(mut self) -> Self {
|
||||
self.position = Position::Absolute;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for Indicator {
|
||||
fn render(self, cx: &mut WindowContext) -> impl IntoElement {
|
||||
div()
|
||||
.flex_none()
|
||||
.map(|this| match self.style {
|
||||
IndicatorStyle::Dot => this.w_1p5().h_1p5().rounded_full(),
|
||||
IndicatorStyle::Bar => this.w_full().h_1p5().rounded_t_md(),
|
||||
})
|
||||
.when(self.position == Position::Absolute, |this| this.absolute())
|
||||
.bg(self.color.color(cx))
|
||||
let container = div().flex_none();
|
||||
|
||||
match self.style {
|
||||
IndicatorStyle::Icon(icon) => container
|
||||
.child(icon.map(|icon| icon.custom_size(rems_from_px(8.)).color(self.color))),
|
||||
IndicatorStyle::Dot => container
|
||||
.w_1p5()
|
||||
.h_1p5()
|
||||
.rounded_full()
|
||||
.bg(self.color.color(cx)),
|
||||
IndicatorStyle::Bar => container
|
||||
.w_full()
|
||||
.h_1p5()
|
||||
.rounded_t_md()
|
||||
.bg(self.color.color(cx)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(IntoElement)]
|
||||
pub struct IndicatorIcon {
|
||||
icon: Icon,
|
||||
transformation: Option<Transformation>,
|
||||
}
|
||||
|
||||
impl IndicatorIcon {
|
||||
pub fn new(icon: Icon) -> Self {
|
||||
Self {
|
||||
icon,
|
||||
transformation: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn transformation(mut self, transformation: Transformation) -> Self {
|
||||
self.transformation = Some(transformation);
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl RenderOnce for IndicatorIcon {
|
||||
fn render(self, _cx: &mut WindowContext) -> impl IntoElement {
|
||||
self.icon
|
||||
.custom_size(rems_from_px(8.))
|
||||
.when_some(self.transformation, |this, transformation| {
|
||||
this.transform(transformation)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue