use std::rc::Rc;
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, App, ClickEvent, Div, ElementId, Entity, FocusHandle, InteractiveElement,
IntoElement, MouseButton, ParentElement, SharedString, StatefulInteractiveElement, Styled,
Window, div, px,
};
use super::row_order::{self, NavigatorRows, Step};
use crate::anchored::AnchoredContextMenuExt as _;
use crate::row_menu::{self, GroupedMenu};
use crate::tokens::Tokens;
use crate::{Activate, SelectNext, SelectPrevious};
const FILL_GAP_PX: f32 = 1.;
const COUNT_MONO_PX: f32 = 11.;
pub(super) type ClickHandler = Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>;
pub(super) type MenuBuilder = Rc<dyn Fn(&mut Window, &mut App) -> GroupedMenu>;
pub(super) fn with_menu<E>(
row: E,
menu: Option<MenuBuilder>,
select: Option<ClickHandler>,
) -> AnyElement
where
E: InteractiveElement + ParentElement + Styled + IntoElement + 'static,
{
let Some(commands) = menu else {
return row.into_any_element();
};
row.anchored_context_menu(move |menu, window, cx| {
if let Some(select) = &select {
select(&ClickEvent::default(), window, cx);
}
let grouped = commands(window, cx);
row_menu::build(menu, grouped, window, cx)
})
.into_any_element()
}
pub(super) fn fill(group: SharedString, selected: bool, focused: bool, tokens: &Tokens) -> Div {
div()
.absolute()
.top(px(FILL_GAP_PX))
.bottom(px(FILL_GAP_PX))
.left(tokens.row_inset)
.right(tokens.row_inset)
.when(tokens.inset_rows(), |fill| {
fill.rounded(tokens.control_radius)
})
.when(selected, |fill| fill.bg(tokens.select))
.when(!selected, |fill| {
fill.group_hover(group, |style| style.bg(tokens.hair))
})
.when(focused, |fill| fill.border_1().border_color(tokens.accent))
}
pub(super) fn count_mark(count: SharedString, mono: SharedString, tokens: &Tokens) -> Div {
let number = !count.is_empty() && count.chars().all(|c| c.is_ascii_digit());
div()
.flex_shrink_0()
.text_size(tokens.caption_size())
.text_color(tokens.muted)
.when(number, |mark| {
mark.font_family(mono)
.text_size(tokens.mono_size(COUNT_MONO_PX))
})
.child(count)
}
pub(super) fn with_handlers<E>(
row: E,
focus: FocusHandle,
rows: Option<Entity<NavigatorRows>>,
on_click: Option<ClickHandler>,
) -> E
where
E: StatefulInteractiveElement + gpui_kit::prelude::FluentBuilder,
{
let from = focus.clone();
let next = rows.clone();
row.on_mouse_down(MouseButton::Left, |_, window, _| window.prevent_default())
.on_action(move |_: &SelectNext, window, cx| {
row_order::step(next.as_ref(), &from, Step::Next, window, cx)
})
.on_action(move |_: &SelectPrevious, window, cx| {
row_order::step(rows.as_ref(), &focus, Step::Previous, window, cx)
})
.when_some(on_click, |row, on_click| {
let activate = on_click.clone();
row.on_click(move |event, window, cx| on_click(event, window, cx))
.on_action(move |_: &Activate, window, cx| {
activate(&ClickEvent::default(), window, cx)
})
})
}
pub(super) fn row_focus(
key: &SharedString,
focusable: bool,
window: &mut Window,
cx: &mut App,
) -> (FocusHandle, Option<Entity<NavigatorRows>>) {
let state = window.use_keyed_state(
(ElementId::Name(key.clone()), "nav-row-focus"),
cx,
|_, cx| cx.focus_handle().tab_stop(true),
);
let focus = state.read(cx).clone();
let rows = if focusable {
row_order::push(&focus, state.entity_id(), window, cx)
} else {
None
};
(focus, rows)
}