use std::rc::Rc;
use gpui_kit::component::menu::PopupMenu;
use gpui_kit::{App, Context, Entity, SharedString, WeakEntity, Window};
use subspace_lucide::IconName;
use super::{ListDashboard, activate, update};
use crate::Callback;
use crate::row_menu::{self, GroupedMenu, MenuCommand, MenuGroup, RowMenu};
const BLOCKED_REASON: &str = "Finish the open dialog first.";
pub(crate) fn line_menu<D: ListDashboard>(
key: SharedString,
facts: RowMenu,
header: bool,
cx: &mut Context<D>,
) -> impl Fn(PopupMenu, &mut Window, &mut Context<PopupMenu>) -> PopupMenu + 'static {
let entity = cx.entity().downgrade();
move |menu, window, cx| {
let grouped = line_commands(&entity, &key, &facts, header, cx);
row_menu::build(menu, grouped, window, cx)
}
}
pub(super) fn line_commands<D: ListDashboard>(
entity: &WeakEntity<D>,
key: &SharedString,
facts: &RowMenu,
header: bool,
cx: &mut App,
) -> GroupedMenu {
let reason = SharedString::new_static(BLOCKED_REASON);
let items = row_menu::row_menu_items(facts);
let Some(board) = entity.upgrade() else {
return row_menu::grouped_unselected(items, &reason);
};
match select_line(&board, key, cx) {
true => selected_line_commands(board, key, facts, header, cx),
false => {
let grouped = row_menu::grouped_unselected(items, &reason);
with_blocked_fold(grouped, &board, key, header, cx)
}
}
}
fn select_line<D: ListDashboard>(board: &Entity<D>, key: &SharedString, cx: &mut App) -> bool {
board.update(cx, |this, cx| {
update(this, cx, |list| list.select(key.clone()));
this.list().selected() == Some(key)
})
}
fn selected_line_commands<D: ListDashboard>(
board: Entity<D>,
key: &SharedString,
facts: &RowMenu,
header: bool,
cx: &mut App,
) -> GroupedMenu {
let entity = board.downgrade();
let open = facts.opens_details.then(|| open_callback(entity.clone()));
let mut grouped = row_menu::grouped(row_menu::row_menu_items(facts), open);
if header {
let folded = board.read(cx).list().is_folded(key);
grouped.push(fold_command(entity, key.clone(), folded));
}
board_commands(grouped, board, cx)
}
fn with_blocked_fold<D: ListDashboard>(
mut grouped: GroupedMenu,
board: &Entity<D>,
key: &SharedString,
header: bool,
cx: &App,
) -> GroupedMenu {
if header {
let folded = board.read(cx).list().is_folded(key);
let fold = fold_command(board.downgrade(), key.clone(), folded);
grouped.push(fold.disabled(true, BLOCKED_REASON));
}
grouped
}
fn open_callback<D: ListDashboard>(entity: WeakEntity<D>) -> Callback {
let open: Callback = Rc::new(move |_, cx| {
let _ = entity.update(cx, |this: &mut D, cx| activate(this, cx));
});
open
}
fn board_commands<D: ListDashboard>(
mut grouped: GroupedMenu,
board: Entity<D>,
cx: &App,
) -> GroupedMenu {
grouped.extend(D::row_commands(&board, cx));
grouped.extend_act(Box::new(move |menu, window, cx| {
let menu = board.read(cx).extend_row_menu(menu, cx);
D::row_submenus(&board, menu, window, cx)
}))
}
fn fold_command<D: ListDashboard>(
entity: WeakEntity<D>,
group: SharedString,
folded: bool,
) -> MenuCommand {
let (label, icon) = match folded {
true => ("Unfold group", IconName::ChevronsUpDown),
false => ("Fold group", IconName::ChevronsDownUp),
};
MenuCommand::new(MenuGroup::View, label, icon, move |_, cx| {
let group = group.clone();
let _ = entity.update(cx, |this: &mut D, cx| {
update(this, cx, |list| list.toggle(&group));
});
})
}
#[cfg(test)]
mod tests;