use crate::bind; use crate::dialog::{ DialogButton, DialogButtons, DialogCx, DialogSize, FocusId, FooterAction, HeaderProps, header, layout_view, }; use crate::keymap::{KeymapLayer, TuiCommand}; use opentui::keymap::{Condition, Disposer}; use opentui::prelude::*; use std::cell::Cell; use std::rc::{Rc, Weak}; /// Builds the content above the field. pub type Description = Rc AnyWidget>; /// `DialogPromptAction`: a footer action whose command is bound while the field has focus. #[derive(Clone)] pub struct PromptAction { pub title: String, pub command: String, /// The command's title in help listings; `title` by default. pub command_title: Option, pub run: Rc, } #[derive(Clone, Default)] pub struct PromptProps { pub title: String, pub size: DialogSize, /// Muted line below the title. pub subtitle: Option, /// Content above the field. pub description: Option, pub placeholder: Option, pub value: Option, pub busy: bool, pub busy_text: Option, pub actions: Vec, pub on_confirm: Option, pub on_cancel: Option>, } struct Inner { cx: Rc, props: PromptProps, busy: Cell, field: Textarea, busy_line: Text, footer_slot: View, } /// The prompt's handle. Cloning is cheap. #[derive(Clone)] pub struct Prompt(Rc); /// Builds `DialogPrompt` in `cx`. pub fn prompt(cx: &Rc, props: PromptProps) -> (View, Prompt) { let app = &cx.app; let theme = &cx.theme; cx.dialogs.set_size(props.size); let field = Textarea::new(app) .mouse_pointer(MousePointer::DEFAULT) .height(1) .wrap(WrapMode::None) .placeholder(props.placeholder.as_deref().unwrap_or("Enter text")) .placeholder_color(theme.text.muted); if let Some(value) = &props.value { field.set_text(value); } let busy_line = Text::new(app, "").fg(theme.text.muted); let inner = Rc::new(Inner { cx: cx.clone(), busy: Cell::new(props.busy), props, field, busy_line, footer_slot: View::new(app), }); let owner = inner.clone(); cx.on_close(Disposer::new(move || drop(owner))); // A build in a new theme while saving stays busy, so the user cannot submit twice. let kept = cx.keep(bind!(inner, |inner| Some((inner.field.text(), inner.field.cursor_offset(), inner.busy.get())))); if let Some((text, cursor, busy)) = kept.flatten() { inner.field.set_text(&text); inner.field.set_cursor_offset(cursor); inner.busy.set(busy); } let prompt = Prompt(inner); prompt.register_layers(); let props = &prompt.0.props; let body = View::new(app).gap(1); if let Some(description) = &props.description { body.add(&description(cx)); } body.add(&prompt.0.field).add(&prompt.0.busy_line); let on_close: Rc = Rc::new(bind!(prompt.0, |inner| { if inner.busy.get() { return; } match &inner.props.on_cancel { Some(cancel) => cancel(), None => inner.cx.dialogs.clear(), } })); let head = header( cx, HeaderProps { title: props.title.clone(), description: props.subtitle.clone(), on_close: Some(on_close), ..HeaderProps::default() }, ); let field = prompt.0.field.clone(); cx.editor(move || { if !field.is_focused() || field.text().is_empty() { return false; } field.set_text(""); true }); prompt.apply_busy(); if !prompt.busy() { cx.focus(&prompt.0.field); } let root = layout_view(cx, &head, &body, Some(&prompt.0.footer_slot), crate::dialog::DIALOG_INSET, 1); (root, prompt) } impl Prompt { pub fn value(&self) -> String { self.0.field.text() } /// Replaces the value and moves the cursor to its end (the handle actions receive). pub fn set_value(&self, value: &str) { self.0.field.set_text(value); } pub fn busy(&self) -> bool { self.0.busy.get() } /// Turns the busy state on or off: the field blurs and greys out, and the footer actions disable. pub fn set_busy(&self, busy: bool) { self.0.busy.set(busy); self.apply_busy(); let field = &self.0.field; if busy && field.is_focused() { self.0.cx.app.blur(); } if !busy { field.focus(); } } pub fn field(&self) -> &Textarea { &self.0.field } fn confirm(&self) { if self.0.busy.get() { return; } if let Some(confirm) = &self.0.props.on_confirm { confirm(&self.0.field.text()); } } fn register_layers(&self) { let weak = Rc::downgrade(&self.0); let busy = Condition::when(bind!(weak, |inner| !inner.busy.get())); let mut commands = vec![ TuiCommand::new("dialog.prompt.submit", bind!(weak, |inner, _, _| Prompt(inner).confirm())) .title("Submit dialog prompt") .group("Dialog"), ]; for action in &self.0.props.actions { let run = action.run.clone(); commands.push( TuiCommand::new(action.command.clone(), bind!(weak, |inner, _, _| run(&Prompt(inner)))) .title(action.command_title.clone().unwrap_or_else(|| action.title.clone())) .group("Dialog"), ); } // Dialog form semantics win over the managed textarea input layer. self.0.cx.layer(KeymapLayer:: { target: Some(self.0.field.id()), enabled: busy, priority: 1, commands, ..KeymapLayer::default() }); if self.0.props.on_cancel.is_some() { self.0.cx.layer(KeymapLayer:: { priority: 1, commands: vec![ TuiCommand::inline( "escape", bind!(weak, |inner, _, _| { if inner.cx.app.selected_text().is_some() { inner.cx.app.clear_selection(); return; } if !inner.busy.get() && let Some(cancel) = &inner.props.on_cancel { cancel(); } }), ) .title("Back") .group("Dialog"), ], ..KeymapLayer::default() }); } } fn apply_busy(&self) { let inner = &self.0; let theme = &inner.cx.theme; let busy = inner.busy.get(); let text = if busy { theme.text.formfield.disabled } else { theme.text.formfield.base }; inner.field.colors(text, Color::TRANSPARENT).focused_colors(text, Color::TRANSPARENT).cursor_color(if busy { theme.background.formfield.disabled } else { theme.text.base }); inner.busy_line.set_text(format!("⋯ {}", inner.props.busy_text.as_deref().unwrap_or("Working…"))).visible(busy); self.render_footer(); } // A Submit button, with the extra actions as hints on the left. fn render_footer(&self) { let inner = &self.0; let cx = &inner.cx; let slot = &inner.footer_slot; slot.clear(); let weak: Weak = Rc::downgrade(&self.0); let submit = DialogButton { label: "Submit".to_owned(), run: Rc::new(bind!(weak, |inner| Prompt(inner).confirm())) }; let hints: Vec = inner .props .actions .iter() .map(|action| { let run = action.run.clone(); FooterAction::new(action.title.clone()) .command(action.command.clone()) .disabled(inner.busy.get()) .on_trigger(bind!(self.0, |inner| run(&Prompt(inner)))) }) .collect(); slot.add(&DialogButtons::with_hints(cx, vec![submit], 0, hints).view); } }