use super::checkbox::{CheckboxLook, CheckboxLookState, CheckboxState, checkbox_box};
use super::form::rem;
use super::overlay::{self, DropAnchor};
use crate::{
density::Density,
icons::{CarbonIcon, Icon, IconSize},
motion::{self, Curve, Motion, Speed},
theme::{LayerLevel, color},
tokens::{self, ColorRole, TypeStyle},
typography::TypeStyled as _,
};
use gpui_kit::TestSupportExt as _;
use gpui_kit::{
AnyElement, App, ElementId, Hsla, InteractiveElement as _, IntoElement, MouseButton,
ParentElement, SharedString, StatefulInteractiveElement as _, Styled, Window, div,
prelude::FluentBuilder as _, px,
};
use std::{rc::Rc, time::Instant};
pub const MENU_MOTION: Motion = Motion::new(Curve::ProductiveStandard, Speed::Fast02).spatial(true);
pub const CHEVRON_MOTION: Motion =
Motion::new(Curve::ProductiveStandard, Speed::Fast01).spatial(true);
pub const MAX_VISIBLE_ROWS: f32 = 5.5;
#[derive(Clone, Debug, PartialEq)]
pub struct ListItem {
pub value: SharedString,
pub label: SharedString,
pub disabled: bool,
pub group: Option<SharedString>,
}
impl ListItem {
pub fn new(value: impl Into<SharedString>) -> Self {
let value = value.into();
Self {
label: value.clone(),
value,
disabled: false,
group: None,
}
}
pub fn labeled(value: impl Into<SharedString>, label: impl Into<SharedString>) -> Self {
Self {
value: value.into(),
label: label.into(),
disabled: false,
group: None,
}
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
}
impl From<&str> for ListItem {
fn from(value: &str) -> Self {
Self::new(SharedString::from(value.to_string()))
}
}
impl From<(&str, &str)> for ListItem {
fn from((value, label): (&str, &str)) -> Self {
Self::labeled(value.to_string(), label.to_string())
}
}
pub fn row_height(field_height: f32, density: Density) -> f32 {
match density {
Density::Compact => tokens::DENSITY_ROW_MD.px_value(),
Density::Comfortable => field_height,
}
}
pub fn menu_height(items: &[ListItem], shown: &[usize], row_height: f32) -> f32 {
let groups = shown.iter().filter(|&&i| items[i].group.is_some()).count();
let rows = (shown.len().max(1) + groups) as f32;
rows.min(MAX_VISIBLE_ROWS) * row_height
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct ItemState {
pub selected: bool,
pub highlighted: bool,
pub hovered: bool,
pub disabled: bool,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ItemLook {
pub background: Hsla,
pub text: Hsla,
pub outline: Option<Hsla>,
}
impl ItemLook {
pub fn of(level: LayerLevel, state: ItemState) -> Self {
if state.disabled {
return Self {
background: Hsla::transparent_black(),
text: color(ColorRole::TextDisabled),
outline: None,
};
}
let background = match (state.selected, state.hovered) {
(true, true) => color(level.layer_selected_hover()),
(true, false) => color(level.layer_selected()),
(false, true) => color(level.layer_hover()),
(false, false) => Hsla::transparent_black(),
};
let text = if state.selected || state.hovered || state.highlighted {
color(ColorRole::TextPrimary)
} else {
color(ColorRole::TextSecondary)
};
Self {
background,
text,
outline: state.highlighted.then(|| color(ColorRole::Focus)),
}
}
}
pub fn rule_visible(i: usize, states: &[ItemState]) -> bool {
if i == 0 {
return false;
}
let busy = |s: &ItemState| s.selected || s.hovered || s.highlighted;
!(busy(&states[i]) || busy(&states[i - 1]))
}
pub fn move_highlight(from: Option<usize>, delta: isize, disabled: &[bool]) -> Option<usize> {
let n = disabled.len() as isize;
if n == 0 {
return None;
}
let mut i = match from {
Some(i) => i as isize,
None if delta > 0 => -1,
None => n,
};
loop {
i += delta;
if i < 0 || i >= n {
return from;
}
if !disabled[i as usize] {
return Some(i as usize);
}
}
}
pub fn type_ahead(items: &[ListItem], prefix: &str, from: Option<usize>) -> Option<usize> {
if prefix.is_empty() || items.is_empty() {
return None;
}
let prefix = prefix.to_lowercase();
let n = items.len();
let start = match from {
Some(i) if prefix.chars().count() == 1 => i + 1,
Some(i) => i,
None => 0,
};
(0..n)
.map(|k| (start + k) % n)
.find(|&i| !items[i].disabled && items[i].label.to_lowercase().starts_with(&prefix))
}
#[derive(Default)]
pub(crate) struct ListState {
pub open: bool,
pub highlighted: Option<usize>,
pub hovered: Option<usize>,
pub typed: String,
pub typed_at: Option<Instant>,
pub anchor: DropAnchor,
}
impl ListState {
pub fn push_typed(&mut self, ch: &str) -> String {
let now = Instant::now();
if self
.typed_at
.is_none_or(|t| now.duration_since(t).as_millis() > 500)
{
self.typed.clear();
}
self.typed.push_str(ch);
self.typed_at = Some(now);
self.typed.clone()
}
}
pub(crate) fn chevron(
id: ElementId,
open: bool,
disabled: bool,
window: &mut Window,
cx: &mut App,
) -> impl IntoElement {
let angle = motion::animate(
(id, "chevron"),
if open { std::f32::consts::PI } else { 0. },
CHEVRON_MOTION,
window,
cx,
);
Icon::carbon(CarbonIcon::ChevronDown)
.size(IconSize::S16)
.color(color(if disabled {
ColorRole::IconDisabled
} else {
ColorRole::IconPrimary
}))
.rotate(gpui_kit::radians(angle))
}
pub(crate) type PickFn = Rc<dyn Fn(usize, &mut Window, &mut App)>;
pub(crate) type HoverFn = Rc<dyn Fn(Option<usize>, &mut Window, &mut App)>;
pub(crate) struct Menu<'a> {
pub id: ElementId,
pub items: &'a [ListItem],
pub shown: Vec<usize>,
pub selected: &'a [bool],
pub highlighted: Option<usize>,
pub hovered: Option<usize>,
pub row_height: f32,
pub width: f32,
pub max_height: f32,
pub level: LayerLevel,
pub checkboxes: bool,
pub empty: Option<SharedString>,
pub on_pick: PickFn,
pub on_hover: HoverFn,
pub on_close: overlay::Callback,
}
pub(crate) fn menu(menu: Menu<'_>, progress: f32) -> AnyElement {
let Menu {
id,
items,
shown,
selected,
highlighted,
hovered,
row_height,
width,
max_height,
level,
checkboxes,
empty,
on_pick,
on_hover,
on_close,
} = menu;
let states: Vec<ItemState> = shown
.iter()
.map(|&i| ItemState {
selected: selected.get(i).copied().unwrap_or(false),
highlighted: highlighted == Some(i),
hovered: hovered == Some(i),
disabled: items[i].disabled,
})
.collect();
let full = menu_height(items, &shown, row_height).min(max_height.max(row_height));
let pad = (row_height - 16.) / 2. - 1.;
let mut children: Vec<AnyElement> = Vec::new();
for (k, &i) in shown.iter().enumerate() {
let item = &items[i];
if let Some(group) = &item.group {
children.push(
div()
.h(rem(row_height))
.px(tokens::SPACING_05)
.flex()
.items_center()
.type_style(TypeStyle::Label01)
.text_color(color(ColorRole::TextSecondary))
.child(group.clone())
.into_any_element(),
);
}
let state = states[k];
let mut look = ItemLook::of(level, state);
if checkboxes && state.selected && !state.disabled {
look.text = color(ColorRole::TextPrimary);
}
let rule = rule_visible(k, &states) && item.group.is_none();
let on_pick = on_pick.clone();
let on_hover = on_hover.clone();
let label = item.label.clone();
let mark = checkboxes.then(|| {
let value = if state.selected {
CheckboxState::Checked
} else {
CheckboxState::Unchecked
};
checkbox_box(
CheckboxLook::of(CheckboxLookState {
value,
disabled: state.disabled,
..Default::default()
}),
value,
false,
)
});
children.push(
div()
.id((id.clone(), format!("item-{i}")))
.relative()
.h(rem(row_height))
.flex_shrink_0()
.bg(look.background)
.cursor(if state.disabled {
gpui_kit::CursorStyle::OperationNotAllowed
} else {
gpui_kit::CursorStyle::PointingHand
})
.on_hover(move |over, window, cx| {
on_hover(if *over { Some(i) } else { None }, window, cx)
})
.when(!state.disabled, |this| {
this.on_click(move |_, window, cx| on_pick(i, window, cx))
})
.child(
div()
.mx(tokens::SPACING_05)
.h_full()
.flex()
.flex_row()
.items_center()
.gap(rem(12.))
.pt(rem(pad))
.pb(rem(pad))
.pr(rem(24.))
.border_t_1()
.border_color(if rule {
color(level.border_subtle())
} else {
Hsla::transparent_black()
})
.type_style(TypeStyle::BodyCompact01)
.text_color(look.text)
.overflow_hidden()
.whitespace_nowrap()
.children(mark)
.child(label),
)
.when(state.selected && !checkboxes, |this| {
this.child(
div()
.absolute()
.top_0()
.bottom_0()
.right(rem(16.))
.flex()
.items_center()
.child(
Icon::carbon(CarbonIcon::Checkmark)
.size(IconSize::S16)
.color(color(ColorRole::IconPrimary)),
),
)
})
.when_some(look.outline, |this, outline| {
this.child(super::form::inset_outline(2., outline))
})
.into_any_element(),
);
}
if shown.is_empty()
&& let Some(text) = empty
{
children.push(
div()
.h(rem(row_height))
.px(tokens::SPACING_05)
.flex()
.items_center()
.type_style(TypeStyle::BodyCompact01)
.text_color(color(ColorRole::TextSecondary))
.child(text)
.into_any_element(),
);
}
div()
.id((id.clone(), "menu"))
.test_support()
.w(px(width.max(1.)))
.h(rem(full * progress.clamp(0., 1.)))
.overflow_hidden()
.bg(color(level.layer()))
.shadow(overlay::floating_shadow())
.on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
.on_mouse_down_out(move |_, window, cx| on_close(window, cx))
.child(
div()
.id((id, "scroll"))
.h(rem(full))
.overflow_y_scroll()
.flex()
.flex_col()
.children(children),
)
.into_any_element()
}
#[cfg(test)]
mod tests {
use super::*;
use crate::tokens::CarbonTheme;
fn hex(c: Hsla) -> String {
let c = c.to_rgb();
let b = |v: f32| (v * 255.).round() as u8;
format!("#{:02x}{:02x}{:02x}", b(c.r), b(c.g), b(c.b))
}
#[test]
fn item_colors_follow_list_box_css() {
crate::theme::tests_set(CarbonTheme::White);
let rest = ItemLook::of(LayerLevel::One, ItemState::default());
assert_eq!(rest.background.a, 0.);
assert_eq!(hex(rest.text), "#525252");
let hover = ItemLook::of(
LayerLevel::One,
ItemState {
hovered: true,
..Default::default()
},
);
assert_eq!(hex(hover.background), "#e8e8e8");
let selected = ItemLook::of(
LayerLevel::One,
ItemState {
selected: true,
..Default::default()
},
);
assert_eq!(hex(selected.background), "#e0e0e0");
assert_eq!(hex(selected.text), "#161616");
let highlighted = ItemLook::of(
LayerLevel::One,
ItemState {
highlighted: true,
..Default::default()
},
);
assert_eq!(highlighted.outline.map(hex).as_deref(), Some("#0f62fe"));
}
#[test]
fn rows_and_rules() {
assert_eq!(row_height(40., Density::Comfortable), 40.);
assert_eq!(row_height(48., Density::Compact), 32.);
let mut states = vec![ItemState::default(); 3];
assert!(!rule_visible(0, &states));
assert!(rule_visible(1, &states));
states[1].hovered = true;
assert!(!rule_visible(1, &states) && !rule_visible(2, &states));
}
#[test]
fn menu_height_counts_rows_and_headings_up_to_five_and_a_half() {
let items: Vec<ListItem> = ["prod", "staging", "eu", "us", "pci", "gov", "dev"]
.into_iter()
.map(ListItem::from)
.collect();
assert_eq!(menu_height(&items, &[0, 1, 2], 32.), 96.);
assert_eq!(menu_height(&items, &[0, 1, 2, 3, 4], 32.), 160.);
assert_eq!(menu_height(&items, &(0..7).collect::<Vec<_>>(), 32.), 176.);
assert_eq!(menu_height(&items, &[], 40.), 40.);
let mut grouped = items.clone();
grouped[0].group = Some("Environments".into());
assert_eq!(menu_height(&grouped, &[0, 1], 32.), 96.);
}
#[test]
fn keyboard_moves_and_type_ahead() {
let disabled = [false, true, false];
assert_eq!(move_highlight(None, 1, &disabled), Some(0));
assert_eq!(move_highlight(Some(0), 1, &disabled), Some(2));
assert_eq!(move_highlight(Some(2), 1, &disabled), Some(2));
assert_eq!(move_highlight(None, -1, &disabled), Some(2));
let items: Vec<ListItem> = ["React", "Angular", "Vue", "Svelte"]
.into_iter()
.map(ListItem::from)
.collect();
assert_eq!(type_ahead(&items, "v", None), Some(2));
assert_eq!(type_ahead(&items, "s", Some(0)), Some(3));
assert_eq!(type_ahead(&items, "x", None), None);
}
}