use crate::{
components::{
form::{self, FieldSize, rem},
input::FIELD_MOTION,
},
density::{self, Density},
icons::{CarbonIcon, Icon, IconSize},
motion,
theme::{LayerLevel, color},
tokens::{ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::{
App, CursorStyle, ElementId, Entity, Focusable as _, Hsla, InteractiveElement as _,
IntoElement, ParentElement, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled,
Window,
component::{
Sizable as _, Size,
input::{Input as KitInput, InputState},
},
div,
prelude::FluentBuilder as _,
};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SearchGeometry {
pub height: f32,
pub padding_inline: f32,
pub magnifier_inset: f32,
pub magnifier: f32,
pub close: f32,
}
impl SearchGeometry {
pub fn of(size: FieldSize, density: Density) -> Self {
let height = size.height(density);
Self {
height,
padding_inline: height,
magnifier_inset: (height - 16.) / 2.,
magnifier: 16.,
close: height,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SearchLook {
pub background: Hsla,
pub rule: Hsla,
pub text: Hsla,
pub placeholder: Hsla,
pub magnifier: Hsla,
pub close_icon: Hsla,
pub close_hover: Hsla,
pub close_active: Hsla,
}
impl SearchLook {
pub fn of(level: LayerLevel, disabled: bool) -> Self {
Self {
background: color(level.field()),
rule: if disabled {
Hsla::transparent_black()
} else {
color(level.border_strong())
},
text: color(if disabled {
ColorRole::TextDisabled
} else {
ColorRole::TextPrimary
}),
placeholder: color(ColorRole::TextPlaceholder),
magnifier: color(if disabled {
ColorRole::IconOnColorDisabled
} else {
ColorRole::IconSecondary
}),
close_icon: color(ColorRole::IconPrimary),
close_hover: color(level.field_hover()),
close_active: color(ColorRole::BackgroundSelected),
}
}
}
pub fn shows_clear(empty: bool, disabled: bool) -> bool {
!empty && !disabled
}
#[derive(IntoElement)]
pub struct PatternSearch {
id: ElementId,
state: Entity<InputState>,
label: Option<SharedString>,
placeholder: Option<SharedString>,
close_label: SharedString,
size: FieldSize,
density: Option<Density>,
disabled: bool,
level: LayerLevel,
}
impl PatternSearch {
pub fn new(id: impl Into<ElementId>, state: &Entity<InputState>) -> Self {
Self {
id: id.into(),
state: state.clone(),
label: None,
placeholder: None,
close_label: SharedString::default(),
size: FieldSize::Md,
density: None,
disabled: false,
level: LayerLevel::One,
}
}
pub fn label(mut self, label: impl Into<SharedString>) -> Self {
self.label = Some(label.into());
self
}
pub fn placeholder(mut self, text: impl Into<SharedString>) -> Self {
self.placeholder = Some(text.into());
self
}
pub fn close_label(mut self, label: impl Into<SharedString>) -> Self {
self.close_label = label.into();
self
}
pub fn size(mut self, size: FieldSize) -> Self {
self.size = size;
self
}
pub fn density(mut self, density: Density) -> Self {
self.density = Some(density);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn layer(mut self, level: LayerLevel) -> Self {
self.level = level;
self
}
}
impl RenderOnce for PatternSearch {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let id = self.id;
let g = SearchGeometry::of(self.size, self.density.unwrap_or_else(density::current));
let look = SearchLook::of(self.level, self.disabled);
let state = self.state.clone();
let focus = state.read(cx).focus_handle(cx);
let focused = focus.is_focused(window) && !self.disabled;
let empty = state.read(cx).value().is_empty();
let outline = motion::animate_color(
(id.clone(), "outline"),
if focused {
color(ColorRole::Focus)
} else {
Hsla::transparent_black()
},
FIELD_MOTION,
window,
cx,
);
let editor = KitInput::new(&state)
.appearance(false)
.with_size(Size::XSmall)
.disabled(self.disabled)
.when_some(self.label.clone(), |this, label| this.aria_label(label))
.p_0()
.h_full()
.flex_1()
.min_w_0()
.text_size(rem(14.))
.line_height(rem(18.))
.text_color(look.text);
let placeholder = self.placeholder.filter(|_| empty).map(|text| {
div()
.absolute()
.left(rem(g.padding_inline))
.right(rem(g.padding_inline))
.top_0()
.bottom_0()
.flex()
.items_center()
.overflow_hidden()
.whitespace_nowrap()
.text_color(look.placeholder)
.child(text)
});
let magnifier = div()
.absolute()
.top_0()
.bottom_0()
.left(rem(g.magnifier_inset))
.flex()
.items_center()
.child(
Icon::carbon(CarbonIcon::Search)
.size(IconSize::S16)
.color(look.magnifier),
);
let clear = shows_clear(empty, self.disabled).then(|| {
let close_focus = window
.use_keyed_state((id.clone(), "close-focus"), cx, |_, cx| cx.focus_handle())
.read(cx)
.clone();
let close_focused = close_focus.is_focused(window);
let state = state.clone();
gpui_kit::base::Button::new((id.clone(), "close"))
.track_focus(&close_focus)
.aria_label(self.close_label.clone())
.absolute()
.top_0()
.right_0()
.size(rem(g.close))
.flex()
.items_center()
.justify_center()
.cursor(CursorStyle::PointingHand)
.hover(move |s| s.bg(look.close_hover))
.active(move |s| s.bg(look.close_active))
.child(
Icon::carbon(CarbonIcon::Close)
.size(IconSize::S16)
.color(look.close_icon),
)
.when(close_focused, |this| {
this.child(form::inset_outline(2., color(ColorRole::Focus)))
})
.on_click(move |_, window, cx| {
state.update(cx, |s, cx| {
s.set_value("", window, cx);
s.focus(window, cx);
})
})
});
div()
.id(id.clone())
.relative()
.flex()
.flex_1()
.items_center()
.w_full()
.min_w_0()
.h(rem(g.height))
.pl(rem(g.padding_inline))
.pr(rem(g.padding_inline))
.bg(look.background)
.type_style(TypeStyle::BodyCompact01)
.when(self.disabled, |this| {
this.cursor(CursorStyle::OperationNotAllowed)
})
.child(form::bottom_rule(look.rule))
.children(placeholder)
.child(editor)
.child(magnifier)
.children(clear)
.child(form::inset_outline(2., outline))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn geometry_follows_the_layout_height() {
let lg = SearchGeometry::of(FieldSize::Lg, Density::Comfortable);
assert_eq!((lg.height, lg.padding_inline, lg.close), (48., 48., 48.));
assert_eq!(lg.magnifier_inset, 16.);
let sm = SearchGeometry::of(FieldSize::Sm, Density::Comfortable);
assert_eq!((sm.height, sm.magnifier_inset), (32., 8.));
let compact = SearchGeometry::of(FieldSize::Lg, Density::Compact);
assert_eq!(compact.height, 32.);
}
#[test]
fn look_matches_the_css() {
let look = SearchLook::of(LayerLevel::One, false);
assert_eq!(look.background, color(ColorRole::Field01));
assert_eq!(look.rule, color(ColorRole::BorderStrong01));
assert_eq!(look.magnifier, color(ColorRole::IconSecondary));
assert_eq!(look.close_hover, color(ColorRole::FieldHover01));
let off = SearchLook::of(LayerLevel::One, true);
assert_eq!(off.rule, Hsla::transparent_black());
assert_eq!(off.text, color(ColorRole::TextDisabled));
assert_eq!(off.magnifier, color(ColorRole::IconOnColorDisabled));
}
#[test]
fn clear_shows_only_with_text() {
assert!(shows_clear(false, false));
assert!(!shows_clear(true, false));
assert!(!shows_clear(false, true));
}
}