Skip to main content

gpui_component/input/
token.rs

1use super::InlineTokenContext;
2use crate::{ActiveTheme as _, Icon};
3use gpui::{
4    App, InteractiveElement as _, IntoElement, ParentElement as _, RenderOnce, StyleRefinement,
5    Styled, Window, div, prelude::FluentBuilder as _,
6};
7use palette::WithAlpha as _;
8
9/// The element an [`InlineToken`](super::InlineToken) renders as by default. Editing
10/// and activation belong to the input.
11#[derive(IntoElement)]
12pub struct InputToken {
13    context: InlineTokenContext,
14    icon: Option<Icon>,
15    style: StyleRefinement,
16}
17impl InputToken {
18    pub fn new(context: &InlineTokenContext) -> Self {
19        Self {
20            context: context.clone(),
21            icon: None,
22            style: Default::default(),
23        }
24    }
25    pub fn icon(mut self, icon: impl Into<Icon>) -> Self {
26        self.icon = Some(icon.into());
27        self
28    }
29}
30impl Styled for InputToken {
31    fn style(&mut self) -> &mut StyleRefinement {
32        &mut self.style
33    }
34}
35impl RenderOnce for InputToken {
36    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
37        div()
38            .id("input-token")
39            .flex()
40            .items_center()
41            .gap_1()
42            .px_1()
43            .h(self.context.line_height())
44            .max_w(self.context.available_width())
45            .rounded(cx.theme().radius)
46            .border_1()
47            .map(|this| {
48                // The selection color is translucent as a fill; its opaque form
49                // is the matching border.
50                if self.context.is_selected() {
51                    this.bg(cx.theme().selection)
52                        .border_color(cx.theme().selection.with_alpha(1.))
53                } else {
54                    this.bg(cx.theme().muted).border_color(cx.theme().border)
55                }
56            })
57            .text_color(cx.theme().foreground)
58            .when(self.context.is_disabled(), |this| this.opacity(0.5))
59            .when_some(self.icon, |this, icon| {
60                this.child(icon.size_3().flex_shrink_0())
61            })
62            .child(
63                div()
64                    .min_w_0()
65                    .text_ellipsis()
66                    .child(self.context.token().label().clone()),
67            )
68            .refine_style(&self.style)
69    }
70}