//! Preview themes while browsing; persist the choice only on Enter. use opencode_ui::select::{Select, select}; use crate::dialogs::settings::{set_path, update}; use crate::prelude::*; pub fn install(tui: &Rc) { let command = TuiCommand::new("theme.switch", bind!(tui, |tui, _, _| show(&tui, None))) .bind(Bind::Off) .title("Switch theme") .group("System") .palette() .slash("themes"); tui.keymap .install("theme list", [KeymapLayer::global(vec![command]), KeymapLayer::default().bindings(["theme.switch"])]); } /// `DialogThemeList`, in place of any open dialog. `on_close` returns to the caller (Settings) on Escape and /// after a pick, instead of closing every dialog. pub fn show(tui: &Tui, on_close: Option>) { let list = Rc::new(List { tui: tui.weak(), initial: tui.theme.borrow().selected().to_owned(), on_close, confirmed: Cell::new(false), select: RefCell::new(None), }); list.open(); } struct List { tui: Weak, /// The theme the dialog opened with, restored unless a theme is picked. initial: String, on_close: Option>, confirmed: Cell, select: RefCell>>, } impl List { fn open(self: &Rc) { let Some(tui) = self.tui.upgrade() else { return }; let list = self.clone(); // The theme names in `localeCompare` base order. let mut names = tui.theme.borrow().names(); names.sort_by_key(|name| name.to_lowercase()); let rows: Vec<_> = names.into_iter().map(|name| SelectOption::new(name.clone(), name)).collect(); tui.dialogs.replace( move |cx| { let mut props = SelectProps::new("Themes", rows.clone()); props.search_in_title = true; props.title_placeholder = Some("Search themes".to_owned()); props.current = Some(list.initial.clone()); props.on_move = Some(Rc::new({ let list = Rc::downgrade(&list); bind!(list, |list, option| list.preview(&option.value)) })); props.on_select = Some(Rc::new({ let list = Rc::downgrade(&list); bind!(list, |list, option| list.pick(&option.value)) })); props.on_filter = Some(Rc::new({ let list = Rc::downgrade(&list); bind!(list, |list, query| list.filtered(query)) })); props.on_cancel = list.on_close.clone(); let (view, handle) = select(cx, props); *list.select.borrow_mut() = Some(handle); // The dialog owns the list; its callbacks hold weak references. let owner = list.clone(); cx.on_dismiss(move || owner.closed()); view }, None, DialogOptions::default(), ); } /// `themes.set(name)` without the write. fn preview(&self, name: &str) { let Some(tui) = self.tui.upgrade() else { return }; if tui.theme.borrow().selected() == name { return; } tui.theme.borrow_mut().config_name(name); tui.theme_changed(); } /// `onFilter`: the first match, or the opening theme once the filter is empty. fn filtered(self: &Rc, query: &str) { if query.is_empty() { let initial = self.initial.clone(); self.preview(&initial); return; } // The select filters the rows after it reports the query. let Some(tui) = self.tui.upgrade() else { return }; let list = self.clone(); tui.app.defer(move |_| { let first = list.select.borrow().as_ref().and_then(|select| select.filtered().into_iter().next()); if let Some(first) = first { list.preview(&first.value); } }); } fn pick(self: &Rc, name: &str) { let Some(tui) = self.tui.upgrade() else { return }; self.confirmed.set(true); if tui.theme.borrow().selected() != name { tui.theme.borrow_mut().config_name(name); tui.theme_changed(); } let name = Value::from(name); update(&tui, move |draft| set_path(draft, &["theme", "name"], name)); match &self.on_close { Some(on_close) => on_close(), None => tui.dialogs.clear(), } } /// Restore the opening theme on cancel. fn closed(&self) { if self.confirmed.get() { return; } let Some(tui) = self.tui.upgrade() else { return }; if tui.theme.borrow().selected() == self.initial { return; } tui.theme.borrow_mut().config_name(&self.initial); tui.theme_changed(); } }