An inline status, system note or labeled separator in a conversation.
use gpui::{div, prelude::*, px, svg, App, Window};
use crate::theme::ActiveTheme;
use crate::ui::marker::{Marker, MarkerVariant};
use crate::ui::spinner::{Spinner, SpinnerSize};
use crate::icons::{CHECK, GIT_PULL_REQUEST};
pub fn marker_demo(_window: &mut Window, cx: &mut App) -> impl IntoElement {
let muted = cx.theme().muted_foreground;
let icon = |data: &'static [u8]| svg().data(data).size(px(16.0)).text_color(muted);
div()
.w(px(420.0))
.flex()
.flex_col()
.gap_4()
.child(
Marker::new("today")
.variant(MarkerVariant::Separator)
.child("Today"),
)
.child(
Marker::new("explored")
.variant(MarkerVariant::Border)
.icon(icon(CHECK))
.child("Explored 4 files"),
)
.child(Marker::new("note").child("You switched the model to Large."))
.child(
Marker::new("thinking")
.icon(Spinner::new().size(SpinnerSize::Md).color(muted))
.shimmer(true)
.child("Thinking about the release notes..."),
)
.child(
Marker::new("pull-request")
.icon(icon(GIT_PULL_REQUEST))
.on_click(|_, _, _| {})
.child("View the pull request"),
)
}Installation
Copies src/ui/marker.rs into your project, along with any components it depends on, and registers the module in src/ui/mod.rs.
Install the theme
Every component reads colors from
crate::theme. Follow the installation guide if you have not added it yet.Copy the source
Paste the code below into
src/ui/marker.rs.src/ui/marker.rs use std::time::Duration; use gpui::{ div, prelude::*, pulsating_between, px, Animation, AnimationExt, AnyElement, App, ClickEvent, ElementId, Window, }; use crate::theme::ActiveTheme; /// One shimmer sweep. GPUI can't mask text with a moving gradient, so the shimmer is a pulse. const SHIMMER: Duration = Duration::from_millis(1500); #[derive(Clone, Copy, Default, PartialEq)] pub enum MarkerVariant { /// An inline status or note. #[default] Default, /// The default marker with a line under the row. Border, /// A centered label with a divider line on each side. Separator, } type ClickHandler = Box<dyn Fn(&ClickEvent, &mut Window, &mut App) + 'static>; /// An inline status, system note or labeled separator in a conversation, such as /// "Explored 4 files" or "Today". Children are the text. #[derive(IntoElement)] pub struct Marker { id: ElementId, variant: MarkerVariant, icon: Option<AnyElement>, shimmer: bool, children: Vec<AnyElement>, on_click: Option<ClickHandler>, } impl Marker { pub fn new(id: impl Into<ElementId>) -> Self { Self { id: id.into(), variant: MarkerVariant::default(), icon: None, shimmer: false, children: Vec::new(), on_click: None, } } pub fn variant(mut self, variant: MarkerVariant) -> Self { self.variant = variant; self } /// A 16px icon before the text, such as a check or a `Spinner`. SVGs don't inherit the /// text color, so give them `theme.muted_foreground`. pub fn icon(mut self, icon: impl IntoElement) -> Self { self.icon = Some(icon.into_any_element()); self } /// Pulses the text while something is still streaming, e.g. "Thinking...". pub fn shimmer(mut self, shimmer: bool) -> Self { self.shimmer = shimmer; self } /// Makes the marker a link: underlined text that brightens on hover. pub fn on_click( mut self, handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static, ) -> Self { self.on_click = Some(Box::new(handler)); self } } impl ParentElement for Marker { fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) { self.children.extend(elements); } } impl RenderOnce for Marker { fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement { let theme = cx.theme(); let (muted_fg, fg, border) = (theme.muted_foreground, theme.foreground, theme.border); let separator = self.variant == MarkerVariant::Separator; let link = self.on_click.is_some(); let rule = || div().flex_1().min_w_0().h(px(1.0)).bg(border); let content = div() .id("content") .min_w_0() .when(separator, |this| this.flex_none()) .when(link, |this| this.underline()) .children(self.children); let content = if self.shimmer { content .with_animation( "shimmer", Animation::new(SHIMMER) .repeat() .with_easing(pulsating_between(0.4, 1.0)), |this, opacity| this.opacity(opacity), ) .into_any_element() } else { content.into_any_element() }; div() .id(self.id) .flex() .items_center() .gap_2() .w_full() .min_h(px(16.0)) .text_sm() .text_color(muted_fg) .when(self.variant == MarkerVariant::Border, |this| { this.border_b_1().border_color(border).pb_2() }) .when(separator, |this| this.child(rule().mr_1())) .when_some(self.icon, |this, icon| { this.child(div().flex().flex_none().size(px(16.0)).child(icon)) }) .child(content) .when(separator, |this| this.child(rule().ml_1())) .when_some(self.on_click, |this, handler| { this.cursor_pointer() .hover(move |style| style.text_color(fg)) .on_click(move |event, window, cx| handler(event, window, cx)) }) } }Register the module
// src/ui/mod.rs pub mod marker;
Usage
use crate::ui::marker::*;API Reference
MarkerVariant
MarkerVariant::DefaultAn inline status or note.
MarkerVariant::BorderThe default marker with a line under the row.
MarkerVariant::SeparatorA centered label with a divider line on each side.
Marker
An inline status, system note or labeled separator in a conversation, such as "Explored 4 files" or "Today". Children are the text.
Marker::new(id: impl Into<ElementId>) -> Self.variant(variant: MarkerVariant).icon(icon: impl IntoElement)A 16px icon before the text, such as a check or a Spinner. SVGs don't inherit the text color, so give them theme.muted_foreground.
.shimmer(shimmer: bool)Pulses the text while something is still streaming, e.g. "Thinking...".
.on_click(handler: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static)Makes the marker a link: underlined text that brightens on hover.