use gpui::prelude::FluentBuilder as _;
use gpui::{
AnyElement, App, Context, ElementId, Entity, EventEmitter, InteractiveElement, IntoElement,
MouseButton, ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement,
Styled, Window, canvas, deferred, div, px,
};
use super::{Icon, IconButton, TextField, TextFieldEvent, all, child_id};
use crate::interaction::{focus_ring, interactive, state_layer, use_interaction};
use crate::motion::{self, Spring, entrance, key};
use crate::theme::{Elevation, Theme, font_family};
const BAR_HEIGHT: f32 = 56.;
const RADIUS: f32 = 28.;
#[derive(Clone, Debug, PartialEq)]
pub enum SearchEvent {
Opened,
Changed(SharedString),
Submitted(SharedString),
Cleared,
Closed,
}
pub fn search_field(cx: &mut Context<TextField>) -> TextField {
TextField::new(cx)
.bare()
.text_size(px(16.))
.placeholder("Search")
}
pub struct Search {
field: Entity<TextField>,
expanded: bool,
blur_pending: bool,
}
impl EventEmitter<SearchEvent> for Search {}
impl Search {
pub fn new(field: Entity<TextField>, cx: &mut Context<Self>) -> Self {
cx.subscribe(
&field,
|this: &mut Self, _, event: &TextFieldEvent, cx| match forwarded(event) {
Some(event) => cx.emit(event),
None => this.close(cx),
},
)
.detach();
Self {
field,
expanded: false,
blur_pending: false,
}
}
pub fn expanded(mut self, expanded: bool) -> Self {
self.expanded = expanded;
self
}
pub fn field(&self) -> &Entity<TextField> {
&self.field
}
pub fn is_expanded(&self) -> bool {
self.expanded
}
pub fn text(&self, cx: &App) -> SharedString {
self.field.read(cx).text().clone()
}
pub fn open(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.blur_pending = false;
self.field.read(cx).focus(window);
if !self.expanded {
self.expanded = true;
cx.emit(SearchEvent::Opened);
cx.notify();
}
}
pub fn close(&mut self, cx: &mut Context<Self>) {
if self.expanded {
self.expanded = false;
self.blur_pending = true;
cx.emit(SearchEvent::Closed);
cx.notify();
}
}
pub fn set_text(&mut self, text: impl Into<SharedString>, cx: &mut Context<Self>) {
let text = text.into();
self.field.update(cx, |field, cx| field.set_text(text, cx));
}
pub fn clear(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.field.update(cx, |field, cx| field.set_text("", cx));
self.field.read(cx).focus(window);
cx.emit(SearchEvent::Cleared);
cx.notify();
}
}
fn forwarded(event: &TextFieldEvent) -> Option<SearchEvent> {
match event {
TextFieldEvent::Changed(text) => Some(SearchEvent::Changed(text.clone())),
TextFieldEvent::Submitted(text) => Some(SearchEvent::Submitted(text.clone())),
TextFieldEvent::Cancelled => None,
}
}
fn reveal_height(natural: f32, reveal: f32) -> Option<f32> {
(reveal < 1.).then(|| natural * reveal.max(0.))
}
#[derive(Clone, Copy, PartialEq, Eq)]
enum RowKind {
Docked,
Active,
}
fn own_press(el: impl IntoElement) -> impl IntoElement {
div()
.flex()
.flex_none()
.items_center()
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.child(el)
}
fn input_row(
id: &ElementId,
search: &Entity<Search>,
kind: RowKind,
trailing: Option<AnyElement>,
leaving: bool,
cx: &App,
) -> gpui::Div {
let s = *Theme::of(cx);
let field = search.read(cx).field.clone();
let text = field.read(cx).text().clone();
let leading = match kind {
RowKind::Docked => div()
.flex()
.flex_none()
.items_center()
.justify_center()
.size(px(40.))
.child(Icon::new("search").size(px(22.)).color(s.on_surface))
.into_any_element(),
RowKind::Active => {
let search = search.clone();
IconButton::new(child_id(id, "back"), "arrow-left")
.size(px(40.))
.tooltip("Back")
.on_click(move |_, _, cx| search.update(cx, |search, cx| search.close(cx)))
.into_any_element()
}
};
let input = if leaving {
div()
.flex_1()
.min_w_0()
.overflow_hidden()
.whitespace_nowrap()
.text_size(px(16.))
.child(text.clone())
.into_any_element()
} else {
div().flex_1().min_w_0().child(field).into_any_element()
};
let clear = (!text.is_empty()).then(|| {
let search = search.clone();
own_press(
IconButton::new(child_id(id, "clear"), "x")
.size(px(40.))
.tooltip("Clear")
.on_click(move |_, window, cx| {
search.update(cx, |search, cx| search.clear(window, cx))
}),
)
});
div()
.flex()
.items_center()
.gap(px(4.))
.w_full()
.h(px(BAR_HEIGHT))
.px(px(8.))
.font_family(font_family())
.text_color(s.on_surface)
.child(leading)
.child(input)
.children(clear)
.children(trailing.map(own_press))
}
#[derive(IntoElement)]
pub struct SearchBar {
id: ElementId,
search: Entity<Search>,
trailing: Option<AnyElement>,
active: bool,
open_on_press: bool,
}
impl SearchBar {
pub fn new(id: impl Into<ElementId>, search: &Entity<Search>) -> Self {
Self {
id: id.into(),
search: search.clone(),
trailing: None,
active: false,
open_on_press: true,
}
}
pub fn trailing(mut self, el: impl IntoElement) -> Self {
self.trailing = Some(el.into_any_element());
self
}
pub fn active(mut self, active: bool) -> Self {
self.active = active;
self
}
pub fn open_on_press(mut self, open: bool) -> Self {
self.open_on_press = open;
self
}
}
impl RenderOnce for SearchBar {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
if self.active {
return input_row(
&self.id,
&self.search,
RowKind::Active,
self.trailing,
false,
cx,
)
.into_any_element();
}
let (expanded, field) = {
let search = self.search.read(cx);
(search.expanded, search.field.clone())
};
if expanded {
return div().w_full().h(px(BAR_HEIGHT)).into_any_element();
}
let focused = field.read(cx).is_focused(window);
let blur = self
.search
.update(cx, |search, _| std::mem::take(&mut search.blur_pending));
if blur && focused {
window.blur();
}
let state = use_interaction(&self.id, window, cx);
let radii = all(px(RADIUS));
let layer = state_layer(&self.id, &state, radii, s.on_surface, window, cx);
let ring = focus_ring(&self.id, focused, radii, window, cx);
let row = input_row(
&self.id,
&self.search,
RowKind::Docked,
self.trailing,
false,
cx,
);
let bar = div()
.id(self.id.clone())
.relative()
.w_full()
.h(px(BAR_HEIGHT))
.rounded(px(RADIUS))
.bg(s.surface_container_high)
.child(layer)
.child(row)
.children(ring);
let search = self.search;
let open = self.open_on_press;
interactive(bar, &state, true, false)
.on_mouse_down(MouseButton::Left, move |_, window, cx| {
if open {
search.update(cx, |search, cx| search.open(window, cx));
} else {
field.read(cx).focus(window);
}
})
.into_any_element()
}
}
#[derive(IntoElement)]
pub struct SearchView {
id: ElementId,
search: Entity<Search>,
trailing: Option<AnyElement>,
empty_text: Option<SharedString>,
max_height: Pixels,
overlay: bool,
children: Vec<AnyElement>,
}
impl SearchView {
pub fn new(id: impl Into<ElementId>, search: &Entity<Search>) -> Self {
Self {
id: id.into(),
search: search.clone(),
trailing: None,
empty_text: None,
max_height: px(360.),
overlay: false,
children: Vec::new(),
}
}
pub fn trailing(mut self, el: impl IntoElement) -> Self {
self.trailing = Some(el.into_any_element());
self
}
pub fn empty_text(mut self, text: impl Into<SharedString>) -> Self {
self.empty_text = Some(text.into());
self
}
pub fn max_height(mut self, height: Pixels) -> Self {
self.max_height = height;
self
}
pub fn overlay(mut self, overlay: bool) -> Self {
self.overlay = overlay;
self
}
}
impl ParentElement for SearchView {
fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
self.children.extend(elements);
}
}
impl RenderOnce for SearchView {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let s = *Theme::of(cx);
let expanded = self.search.read(cx).expanded;
let Some(presence) = motion::presence(key(&self.id, "presence"), expanded, window, cx)
else {
return div().into_any_element();
};
let leaving = !expanded;
let reveal = entrance(
key(&self.id, "reveal"),
Spring::SPATIAL_FAST,
0.,
window,
cx,
)
.clamp(0., 1.);
let fade = entrance(key(&self.id, "fade"), Spring::EFFECTS, 0., window, cx).clamp(0., 1.);
let row = input_row(
&self.id,
&self.search,
RowKind::Active,
self.trailing,
leaving,
cx,
);
let results = if self.children.is_empty() {
self.empty_text.map(|text| {
div()
.p(px(16.))
.text_center()
.text_size(px(14.))
.text_color(s.on_surface_variant)
.child(text)
.into_any_element()
})
} else {
Some(
div()
.flex()
.flex_col()
.gap(px(2.))
.p(px(8.))
.children(self.children)
.into_any_element(),
)
};
let results = results.map(|results| {
let measured = window.use_keyed_state(key(&self.id, "results"), cx, |_, _| px(0.));
let height = *measured.read(cx);
let measure = canvas(
move |bounds, _, cx| measured.update(cx, |h, _| *h = bounds.size.height),
|_, _, _, _| {},
)
.absolute()
.size_full();
let natural = div()
.relative()
.flex()
.flex_col()
.flex_none()
.child(div().h(px(1.)).bg(s.outline_variant))
.child(
div()
.id(key(&self.id, "scroll"))
.max_h(self.max_height)
.overflow_y_scroll()
.opacity(if leaving { 1. } else { fade })
.child(results),
)
.child(measure);
match reveal_height(f32::from(height), reveal) {
None => natural,
Some(clip) => div()
.h(px(clip))
.flex_none()
.overflow_hidden()
.child(natural),
}
});
let panel = div()
.id(key(&self.id, "panel"))
.flex()
.flex_col()
.w_full()
.rounded(px(RADIUS))
.overflow_hidden()
.bg(s.surface_container_high)
.shadow(Elevation::Level3.shadow(&s))
.text_color(s.on_surface)
.font_family(font_family())
.when(leaving, |d| d.opacity(presence.clamp(0., 1.)))
.occlude()
.child(row)
.children(results);
if self.overlay {
deferred(div().absolute().top_0().left_0().right_0().child(panel))
.with_priority(1)
.into_any_element()
} else {
panel.into_any_element()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn text_events_become_search_events_and_escape_closes() {
let text: SharedString = "rust".into();
assert_eq!(
forwarded(&TextFieldEvent::Changed(text.clone())),
Some(SearchEvent::Changed(text.clone()))
);
assert_eq!(
forwarded(&TextFieldEvent::Submitted(text.clone())),
Some(SearchEvent::Submitted(text))
);
assert_eq!(forwarded(&TextFieldEvent::Cancelled), None);
}
#[test]
fn results_are_clipped_only_while_opening() {
assert_eq!(reveal_height(300., 0.), Some(0.));
assert_eq!(reveal_height(300., 0.25), Some(75.));
assert_eq!(reveal_height(300., 1.), None);
assert_eq!(reveal_height(300., -0.1), Some(0.));
}
}