implement passable #[command]
Some checks are pending
/ build (push) Waiting to run

This commit is contained in:
i do not exist 2026-08-09 09:23:24 +03:00
parent 68236a7210
commit ae496987c8
7 changed files with 458 additions and 172 deletions

View file

@ -1,6 +1,17 @@
use crate::*;
def_command!(FileBrowserCommand: |sampler: Sampler|{
impl App {
/// Return reference to content browser if open.
///
/// ```
/// assert_eq!(tek::App::default().browser(), None);
/// ```
pub fn browser (&self) -> Option<&Browse> {
if let Dialog::Browse(_, ref b) = self.dialog { Some(b) } else { None }
}
}
def_command!(FileBrowserCommand: |_browse: Browse|{
//("begin" [] Some(Self::Begin))
//("cancel" [] Some(Self::Cancel))
//("confirm" [] Some(Self::Confirm))
@ -539,3 +550,20 @@ def_command!(CropCommand: |pool: Pool| {
Ok(None)
}
});
pub fn scan (dir: &PathBuf) -> Usually<(Vec<OsString>, Vec<OsString>)> {
let (mut subdirs, mut files) = std::fs::read_dir(dir)?
.fold((vec!["..".into()], vec![]), |(mut subdirs, mut files), entry|{
let entry = entry.expect("failed to read drectory entry");
let meta = entry.metadata().expect("failed to read entry metadata");
if meta.is_file() {
files.push(entry.file_name());
} else if meta.is_dir() {
subdirs.push(entry.file_name());
}
(subdirs, files)
});
subdirs.sort();
files.sort();
Ok((subdirs, files))
}

View file

@ -33,6 +33,7 @@ pub fn draw_dialog (
}
impl App {
pub fn get_dialog (&self, src: impl Language) -> Perhaps<Dialog> {
src.word()?.map(|word|Ok(match word {
":dialog/none" => Dialog::None,
@ -64,31 +65,36 @@ impl App {
)
})).transpose()
}
/// Set modal dialog.
/// Set currently active modal dialog.
///
/// ```
/// let previous: tek::Dialog = tek::App::default().set_dialog(tek::Dialog::welcome());
/// let previous: tek::Dialog = tek::App::default().set_dialog(&tek::Dialog::welcome());
/// ```
pub fn set_dialog (&mut self, mut dialog: Dialog) -> Dialog {
pub fn set_dialog (&mut self, dialog: &Dialog) -> Dialog {
let mut dialog = dialog.clone();
std::mem::swap(&mut self.dialog, &mut dialog);
dialog
}
pub fn inc (&mut self, axis: &ControlAxis) -> Perhaps<AppCommand> {
Ok(match (&self.dialog, axis) {
(Dialog::None, _) => todo!(),
(Dialog::Menu(_, _), ControlAxis::Y) =>
AppCommand::SetDialog { dialog: self.dialog.menu_next() }.act(self)?,
AppCommand::SetDialog(self.dialog.menu_next()).act(self)?,
_ => todo!()
})
}
pub fn dec (&mut self, axis: &ControlAxis) -> Perhaps<AppCommand> {
Ok(match (&self.dialog, axis) {
(Dialog::None, _) => None,
(Dialog::Menu(_, _), ControlAxis::Y) =>
AppCommand::SetDialog { dialog: self.dialog.menu_prev() }.act(self)?,
AppCommand::SetDialog(self.dialog.menu_prev()).act(self)?,
_ => todo!()
})
}
pub fn confirm (&mut self) -> Perhaps<AppCommand> {
Ok(match &self.dialog {
Dialog::Menu(index, items) => {