Skip to content

Commit 69058c1

Browse files
author
cht
committed
add a popup to run
1 parent 556c79f commit 69058c1

1 file changed

Lines changed: 51 additions & 6 deletions

File tree

src/main.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ use crossterm::{
1717
use std::{error::Error, io};
1818
use tui::{
1919
backend::{Backend, CrosstermBackend},
20-
layout::{Constraint, Direction, Layout},
20+
layout::{Constraint, Direction, Layout,Rect},
2121
style::{Color, Modifier, Style},
2222
text::{Span, Spans, Text},
23-
widgets::{Block, Borders, List, ListItem, ListState, Paragraph},
23+
widgets::{Block, Borders, List, ListItem, ListState, Paragraph,Clear},
2424
Frame, Terminal,
2525
};
2626
use unicode_width::UnicodeWidthStr;
@@ -29,6 +29,7 @@ enum InputMode {
2929
Normal,
3030
Editing,
3131
Select,
32+
Popup,
3233
}
3334

3435
/// App holds the state of the application
@@ -42,6 +43,7 @@ struct App {
4243
state: ListState,
4344
index: Option<usize>,
4445
stateoflist: bool,
46+
show_popup : bool,
4547
}
4648
impl App {
4749
fn next(&mut self) {
@@ -87,6 +89,7 @@ impl Default for App {
8789
state: ListState::default(),
8890
index: None,
8991
stateoflist: false,
92+
show_popup: false,
9093
}
9194
}
9295
}
@@ -171,13 +174,24 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
171174
app.input_mode = InputMode::Normal;
172175
}
173176
KeyCode::Enter => {
174-
app.input = app.index.as_ref().unwrap().to_string();
177+
//app.input = app.index.as_ref().unwrap().to_string();
178+
app.show_popup = true;
179+
app.input_mode = InputMode::Popup;
175180
}
176181
_ => {}
177182
}
178183
} else {
179184
app.input_mode = InputMode::Normal;
180185
}
186+
},
187+
InputMode::Popup => {
188+
match key.code {
189+
KeyCode::Char('q') => {
190+
app.input_mode = InputMode::Select;
191+
app.show_popup = false;
192+
}
193+
_=> {}
194+
}
181195
}
182196
}
183197
}
@@ -199,7 +213,7 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
199213
.split(f.size());
200214

201215
let (msg, style) = match app.input_mode {
202-
InputMode::Normal => (
216+
InputMode::Normal | InputMode::Popup => (
203217
vec![
204218
Span::raw("Press "),
205219
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
@@ -239,13 +253,13 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
239253

240254
let input = Paragraph::new(app.input.as_ref())
241255
.style(match app.input_mode {
242-
InputMode::Normal | InputMode::Select => Style::default(),
256+
InputMode::Normal | InputMode::Select | InputMode::Popup=> Style::default(),
243257
InputMode::Editing => Style::default().fg(Color::Yellow),
244258
})
245259
.block(Block::default().borders(Borders::ALL).title("Input"));
246260
f.render_widget(input, chunks[1]);
247261
match app.input_mode {
248-
InputMode::Normal | InputMode::Select =>
262+
InputMode::Normal | InputMode::Select | InputMode::Popup =>
249263
// Hide the cursor. `Frame` does this by default, so we don't need to do anything here
250264
{}
251265

@@ -280,4 +294,35 @@ fn ui<B: Backend>(f: &mut Frame<B>, app: &mut App) {
280294
//let messages =
281295
// List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
282296
f.render_stateful_widget(messages, chunks[2], &mut app.state);
297+
if app.show_popup {
298+
let block = Block::default().title("Popup").borders(Borders::ALL);
299+
let area = centered_rect(60, 20, f.size());
300+
f.render_widget(Clear, area); //this clears out the background
301+
f.render_widget(block, area);
302+
}
303+
}
304+
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
305+
let popup_layout = Layout::default()
306+
.direction(Direction::Vertical)
307+
.constraints(
308+
[
309+
Constraint::Percentage((100 - percent_y) / 2),
310+
Constraint::Percentage(percent_y),
311+
Constraint::Percentage((100 - percent_y) / 2),
312+
]
313+
.as_ref(),
314+
)
315+
.split(r);
316+
317+
Layout::default()
318+
.direction(Direction::Horizontal)
319+
.constraints(
320+
[
321+
Constraint::Percentage((100 - percent_x) / 2),
322+
Constraint::Percentage(percent_x),
323+
Constraint::Percentage((100 - percent_x) / 2),
324+
]
325+
.as_ref(),
326+
)
327+
.split(popup_layout[1])[1]
283328
}

0 commit comments

Comments
 (0)