pub(super) mod menu;
use gpui_kit::{App, Context, SharedString, Window};
use subspace_dashboard::{PaletteCommand, PaletteOption, SubList, Toast, prefs};
use super::{ACTIVE_KEY, OpenSettings, SubspaceApp};
use crate::registry::{self, BoardFactories, BoardRegistration};
use crate::settings::board_choice::{self, Applied};
pub(super) const ADD_SPACE: &str = "Add a Space";
pub(super) const ADD_SPACE_ENTRY: &str = "Add a Space…";
pub(super) const MANAGE_SPACES: &str = "Manage Spaces…";
pub(super) const ALL_ON: &str = "Every Space is on.";
const CHOOSE_SPACE: &str = "Choose a Space to add";
const PALETTE_KEYWORDS: &str = "add open turn on enable space board plus new";
const MANAGE_VALUE: &str = ":manage-spaces";
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct OffSpace {
pub(super) id: &'static str,
pub(super) title: SharedString,
pub(super) description: SharedString,
pub(super) icon: Option<&'static str>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum Row {
Title,
Space(OffSpace),
AllOn,
Separator,
Manage,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(super) enum Pick<'a> {
Space(&'a str),
Manage,
}
pub(super) fn offered(included: Vec<OffSpace>, on: &[&str]) -> Vec<OffSpace> {
included
.into_iter()
.filter(|space| !on.contains(&space.id))
.collect()
}
pub(super) fn menu_rows(offered: Vec<OffSpace>) -> Vec<Row> {
let mut rows = vec![Row::Title];
if offered.is_empty() {
rows.push(Row::AllOn);
} else {
rows.extend(offered.into_iter().map(Row::Space));
}
rows.extend([Row::Separator, Row::Manage]);
rows
}
pub(super) fn opened_message(title: &str, applied: Applied) -> String {
match applied {
Applied::Now => format!("Opened {title}."),
Applied::AfterRestart => format!("Restart the app to open {title}."),
}
}
pub(super) fn open_failed_message(title: &str, error: &str) -> String {
format!("Couldn't open {title}: {error}")
}
pub(super) fn manage_target(
offered: &[OffSpace],
first: Option<&'static str>,
) -> Option<&'static str> {
offered.first().map(|space| space.id).or(first)
}
pub(super) fn palette_options(offered: &[OffSpace]) -> Vec<PaletteOption> {
offered
.iter()
.map(|space| {
PaletteOption::new(space.id, space.title.clone()).hint(space.description.clone())
})
.chain([PaletteOption::new(MANAGE_VALUE, MANAGE_SPACES)])
.collect()
}
pub(super) fn palette_placeholder(offered: &[OffSpace]) -> &'static str {
if offered.is_empty() {
ALL_ON
} else {
CHOOSE_SPACE
}
}
pub(super) fn pick(value: &str) -> Pick<'_> {
if value == MANAGE_VALUE {
Pick::Manage
} else {
Pick::Space(value)
}
}
pub(super) fn off_spaces(cx: &App) -> Vec<OffSpace> {
let on: Vec<&'static str> = cx
.try_global::<BoardFactories>()
.map(|boards| boards.0.iter().map(|board| board.id).collect())
.unwrap_or_default();
let included = registry::included_registrations(cx)
.iter()
.map(off_space)
.collect();
offered(included, &on)
}
fn off_space(registration: &BoardRegistration) -> OffSpace {
OffSpace {
id: registration.id,
title: registration.title.to_string().into(),
description: registration.description.to_string().into(),
icon: registration.icon,
}
}
pub(super) fn palette_command(cx: &mut Context<SubspaceApp>) -> PaletteCommand {
let spaces = off_spaces(cx);
let app = cx.weak_entity();
let list = SubList::new(
palette_placeholder(&spaces),
palette_options(&spaces),
move |value, window, cx| {
_ = app.update(cx, |app, cx| match pick(value) {
Pick::Space(id) => app.open_space(id, window, cx),
Pick::Manage => app.manage_spaces(window, cx),
});
},
);
PaletteCommand::sub(ADD_SPACE_ENTRY, list).keywords(PALETTE_KEYWORDS)
}
impl SubspaceApp {
pub(super) fn open_space(&mut self, id: &str, window: &mut Window, cx: &mut Context<Self>) {
let Some(space) = off_spaces(cx).into_iter().find(|space| space.id == id) else {
return;
};
match board_choice::save_board_enabled(space.id, true, window, cx) {
Ok(applied) => self.opened_space(&space, applied, window, cx),
Err(error) => {
self.push_toast(Toast::error(open_failed_message(&space.title, &error)), cx)
}
}
}
fn opened_space(
&mut self,
space: &OffSpace,
applied: Applied,
window: &mut Window,
cx: &mut Context<Self>,
) {
match applied {
Applied::Now => self.show_opened(space.id, window, cx),
Applied::AfterRestart => prefs::set(cx, ACTIVE_KEY, space.id),
}
self.push_toast(Toast::info(opened_message(&space.title, applied)), cx);
}
fn show_opened(&mut self, id: &'static str, window: &mut Window, cx: &mut Context<Self>) {
self.reconcile_boards(window, cx);
if let Some(index) = self.dashboards.iter().position(|board| board.id == id) {
self.switch_to(index, window, cx);
}
}
pub(super) fn manage_spaces(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let first = registry::included_registrations(cx)
.first()
.map(|board| board.id);
match manage_target(&off_spaces(cx), first) {
Some(id) => self.open_board_settings(id, window, cx),
None => self.open_settings(&OpenSettings, window, cx),
}
}
}
#[cfg(test)]
mod tests {
use super::{
ALL_ON, Applied, CHOOSE_SPACE, MANAGE_SPACES, MANAGE_VALUE, OffSpace, Pick, Row,
manage_target, menu_rows, offered, open_failed_message, opened_message, palette_options,
palette_placeholder, pick,
};
fn space(id: &'static str) -> OffSpace {
OffSpace {
id,
title: id.to_uppercase().into(),
description: format!("About {id}").into(),
icon: None,
}
}
fn ids(spaces: &[OffSpace]) -> Vec<&'static str> {
spaces.iter().map(|space| space.id).collect()
}
#[test]
fn the_menu_offers_the_spaces_without_a_tab_in_product_order() {
let included = vec![
space("alpha"),
space("beta"),
space("gamma"),
space("delta"),
];
let shown = offered(included, &["gamma", "alpha"]);
assert_eq!(ids(&shown), ["beta", "delta"]);
assert!(offered(vec![space("alpha")], &["alpha"]).is_empty());
}
#[test]
fn the_menu_lists_the_off_spaces_then_manage_spaces() {
assert_eq!(
menu_rows(vec![space("beta"), space("delta")]),
[
Row::Title,
Row::Space(space("beta")),
Row::Space(space("delta")),
Row::Separator,
Row::Manage,
]
);
}
#[test]
fn with_every_space_on_the_menu_says_so_and_keeps_manage_spaces() {
assert_eq!(
menu_rows(Vec::new()),
[Row::Title, Row::AllOn, Row::Separator, Row::Manage]
);
assert_eq!(palette_placeholder(&[]), ALL_ON);
assert_eq!(palette_placeholder(&[space("beta")]), CHOOSE_SPACE);
}
#[test]
fn the_three_notices_mirror_closing() {
assert_eq!(opened_message("Actions", Applied::Now), "Opened Actions.");
assert_eq!(
opened_message("Actions", Applied::AfterRestart),
"Restart the app to open Actions."
);
assert_eq!(
open_failed_message("Actions", "the config file changed"),
"Couldn't open Actions: the config file changed"
);
}
#[test]
fn manage_spaces_opens_the_first_off_space_or_else_the_first_space() {
assert_eq!(
manage_target(&[space("beta"), space("delta")], Some("alpha")),
Some("beta")
);
assert_eq!(manage_target(&[], Some("alpha")), Some("alpha"));
assert_eq!(manage_target(&[], None), None);
}
#[test]
fn the_palette_lists_the_same_choices_and_tells_them_apart() {
let options = palette_options(&[space("beta")]);
let labels: Vec<_> = options
.iter()
.map(|option| option.label.to_string())
.collect();
assert_eq!(labels, ["BETA", MANAGE_SPACES]);
assert_eq!(options[0].hint.as_deref(), Some("About beta"));
assert_eq!(pick(&options[0].value), Pick::Space("beta"));
assert_eq!(pick(&options[1].value), Pick::Manage);
assert_eq!(pick(MANAGE_VALUE), Pick::Manage);
}
}