pub struct Dialog {
pub open: bool,
pub submitted: bool,
pub working_input: String,
pub submitted_input: String,
/* private fields */
}Expand description
The data structure for the dialog.
Fields§
§open: boolWhether or not the dialog box is open.
submitted: boolWhether or not input has been submitted.
working_input: StringThe text being written into the dialog box when it’s open. This field can be used to
pre-populate the dialog with a value before it is opened. It will be cleared when the
user presses Esc or Enter.
submitted_input: StringThe text that has been written and is submitted for use when the user presses Enter. Any
surrounding whitespace will be trimmed.
Implementations§
Source§impl Dialog
impl Dialog
Sourcepub fn key_action(&mut self, key_code: &KeyCode)
pub fn key_action(&mut self, key_code: &KeyCode)
Respond to key press.
Examples found in repository?
73fn run(
74 app: &mut App,
75 terminal: &mut Terminal<CrosstermBackend<Stdout>>,
76) -> Result<(), std::io::Error> {
77 while !app.exit {
78 // Redraw the frame every time.
79 terminal.draw(|frame| render(frame, app))?;
80
81 // Handle user input.
82 match event::read()? {
83 Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
84 // Pass all `key_event.code`s to a dialog if open.
85 // (The dialog handles closing itself, when the user presses `Enter` or `Esc`.)
86 if app.dialog.open {
87 app.dialog.key_action(&key_event.code);
88 if app.dialog.submitted {
89 // Here is where you'd do something more significant.
90 app.text = app.dialog.submitted_input.clone();
91 }
92 // Otherwise handle them here.
93 } else {
94 match key_event.code {
95 KeyCode::Char('q') => app.exit = true,
96 // Your app needs to open the dialog.
97 KeyCode::Char('d') => app.dialog.open = true,
98 _ => (),
99 }
100 }
101 }
102 _ => (),
103 };
104 }
105 Ok(())
106}Sourcepub fn title_top(&mut self, title: &str) -> Self
pub fn title_top(&mut self, title: &str) -> Self
Set the top title of the block surrounding the widget.
If the method is not used, there will be no top title.
Examples found in repository?
108fn render(frame: &mut Frame, app: &mut App) {
109 // Layout the rectangles of the UI.
110 let horizontal = Layout::horizontal([Constraint::Fill(1), Constraint::Length(SIDEBAR_SIZE)]);
111
112 let [left, right] = horizontal.areas(frame.area());
113
114 // Left - main content.
115 let main_block = Block::default()
116 .title_top(Line::from(" tui-dialog example ").bold().centered())
117 .borders(Borders::ALL)
118 .border_set(border::THICK)
119 .padding(Padding::horizontal(1));
120
121 // Right - Controls menu
122 let controls_block = Block::default()
123 .title_top(Line::from(" Controls").centered().bold())
124 .borders(Borders::ALL)
125 .border_set(border::THICK);
126
127 let controls_content: Vec<Line<'_>> = vec![
128 vec!["d".blue(), " Show dialog".into()].into(),
129 vec!["q".blue(), " Quit".into()].into(),
130 ];
131
132 let controls = Paragraph::new(controls_content).block(controls_block);
133 frame.render_widget(controls, right);
134 frame.render_widget(Paragraph::new(app.text.clone()).block(main_block), left);
135
136 let dialog_area = centered_rect(frame.area(), 45, 5, -(SIDEBAR_SIZE as i16), 0);
137 frame.render_widget(app.dialog.title_top("Enter text:"), dialog_area);
138}Sourcepub fn title_bottom(&mut self, title: &str) -> Self
pub fn title_bottom(&mut self, title: &str) -> Self
Set the bottom title of the block surrounding the widget.
If the method is not used, the bottom title will default to BOTTOM_TITLE.
Sourcepub fn borders(&mut self, borders: Borders) -> Self
pub fn borders(&mut self, borders: Borders) -> Self
Set borders of the block surrounding the widget.
If the method is not used, the borders will default to ratatui::widgets::Borders::ALL.
Sourcepub fn style(&mut self, style: Style) -> Self
pub fn style(&mut self, style: Style) -> Self
Set the style of the widget.
If the method is not used, the style will be the default with a
ratatui::style::Color::DarkGray background.
Trait Implementations§
impl StructuralPartialEq for Dialog
Auto Trait Implementations§
impl Freeze for Dialog
impl RefUnwindSafe for Dialog
impl Send for Dialog
impl Sync for Dialog
impl Unpin for Dialog
impl UnwindSafe for Dialog
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more