Multi-line popups; wrap texts and align popup texts to the center

This commit is contained in:
ChristianVisintin
2020-12-05 23:07:23 +01:00
parent 79fadf64de
commit 8e8247c4a9

View File

@@ -74,7 +74,7 @@ impl FileTransferActivity {
// Get pwd // Get pwd
let remote_wrkdir: PathBuf = match self.client.pwd() { let remote_wrkdir: PathBuf = match self.client.pwd() {
Ok(p) => p, Ok(p) => p,
Err(_) => PathBuf::from("/") Err(_) => PathBuf::from("/"),
}; };
f.render_stateful_widget( f.render_stateful_widget(
self.draw_remote_explorer(remote_wrkdir), self.draw_remote_explorer(remote_wrkdir),
@@ -109,9 +109,10 @@ impl FileTransferActivity {
self.draw_popup_alert(color.clone(), txt.clone(), popup_area.width), self.draw_popup_alert(color.clone(), txt.clone(), popup_area.width),
popup_area, popup_area,
), ),
PopupType::Fatal(txt) => { PopupType::Fatal(txt) => f.render_widget(
f.render_widget(self.draw_popup_fatal(txt.clone(), popup_area.width), popup_area) self.draw_popup_fatal(txt.clone(), popup_area.width),
} popup_area,
),
PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area), PopupType::Help => f.render_widget(self.draw_popup_help(), popup_area),
PopupType::Input(txt, _) => { PopupType::Input(txt, _) => {
f.render_widget(self.draw_popup_input(txt.clone()), popup_area); f.render_widget(self.draw_popup_input(txt.clone()), popup_area);
@@ -124,12 +125,14 @@ impl FileTransferActivity {
PopupType::Progress(txt) => { PopupType::Progress(txt) => {
f.render_widget(self.draw_popup_progress(txt.clone()), popup_area) f.render_widget(self.draw_popup_progress(txt.clone()), popup_area)
} }
PopupType::Wait(txt) => { PopupType::Wait(txt) => f.render_widget(
f.render_widget(self.draw_popup_wait(txt.clone(), popup_area.width), popup_area) self.draw_popup_wait(txt.clone(), popup_area.width),
} popup_area,
PopupType::YesNo(txt, _, _) => { ),
f.render_widget(self.draw_popup_yesno(txt.clone(), popup_area.width), popup_area) PopupType::YesNo(txt, _, _) => f.render_widget(
} self.draw_popup_yesno(txt.clone(), popup_area.width),
popup_area,
),
} }
} }
}); });
@@ -192,7 +195,11 @@ impl FileTransferActivity {
}, },
_ => Style::default(), _ => Style::default(),
}) })
.title(format!("{}:{} ", self.params.address, remote_wrkdir.display())), .title(format!(
"{}:{} ",
self.params.address,
remote_wrkdir.display()
)),
) )
.start_corner(Corner::TopLeft) .start_corner(Corner::TopLeft)
.highlight_style( .highlight_style(
@@ -293,20 +300,47 @@ impl FileTransferActivity {
/// ### draw_popup_alert /// ### draw_popup_alert
/// ///
/// Draw alert popup /// Draw alert popup
pub(super) fn draw_popup_alert(&self, color: Color, text: String, width: u16) -> Paragraph { pub(super) fn draw_popup_alert(&self, color: Color, text: String, width: u16) -> List {
// Align text to center // Wraps texts
Paragraph::new(self.align_text_center(text.as_str(), width)) let message_rows = textwrap::wrap(text.as_str(), width as usize);
let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from(
self.align_text_center(msg, width),
)));
}
List::new(lines)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(color))
.title("Alert"),
)
.start_corner(Corner::TopLeft)
.style(Style::default().fg(color)) .style(Style::default().fg(color))
.block(Block::default().borders(Borders::ALL).title("Alert"))
} }
/// ### draw_popup_fatal /// ### draw_popup_fatal
/// ///
/// Draw fatal error popup /// Draw fatal error popup
pub(super) fn draw_popup_fatal(&self, text: String, width: u16) -> Paragraph { pub(super) fn draw_popup_fatal(&self, text: String, width: u16) -> List {
Paragraph::new(self.align_text_center(text.as_str(), width)) // Wraps texts
.style(Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)) let message_rows = textwrap::wrap(text.as_str(), width as usize);
.block(Block::default().borders(Borders::ALL).title("Fatal error")) let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from(
self.align_text_center(msg, width),
)));
}
List::new(lines)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::Red))
.title("Fatal error"),
)
.start_corner(Corner::TopLeft)
.style(Style::default().fg(Color::Red))
} }
/// ### draw_popup_input /// ### draw_popup_input
/// ///
@@ -347,10 +381,24 @@ impl FileTransferActivity {
/// ### draw_popup_wait /// ### draw_popup_wait
/// ///
/// Draw wait popup /// Draw wait popup
pub(super) fn draw_popup_wait(&self, text: String, width: u16) -> Paragraph { pub(super) fn draw_popup_wait(&self, text: String, width: u16) -> List {
Paragraph::new(self.align_text_center(text.as_str(), width)) // Wraps texts
let message_rows = textwrap::wrap(text.as_str(), width as usize);
let mut lines: Vec<ListItem> = Vec::new();
for msg in message_rows.iter() {
lines.push(ListItem::new(Spans::from(
self.align_text_center(msg, width),
)));
}
List::new(lines)
.block(
Block::default()
.borders(Borders::ALL)
.border_style(Style::default().fg(Color::White))
.title("Please wait"),
)
.start_corner(Corner::TopLeft)
.style(Style::default().add_modifier(Modifier::BOLD)) .style(Style::default().add_modifier(Modifier::BOLD))
.block(Block::default().borders(Borders::ALL).title("Please wait"))
} }
/// ### draw_popup_yesno /// ### draw_popup_yesno
@@ -363,7 +411,11 @@ impl FileTransferActivity {
DialogYesNoOption::No => 1, DialogYesNoOption::No => 1,
}; };
Tabs::new(choices) Tabs::new(choices)
.block(Block::default().borders(Borders::ALL).title(self.align_text_center(text.as_str(), width))) .block(
Block::default()
.borders(Borders::ALL)
.title(self.align_text_center(text.as_str(), width)),
)
.select(index) .select(index)
.style(Style::default()) .style(Style::default())
.highlight_style( .highlight_style(
@@ -534,14 +586,17 @@ impl FileTransferActivity {
} }
/// align_text_center /// align_text_center
/// ///
/// Align text to center for a given width /// Align text to center for a given width
fn align_text_center(&self, text: &str, width: u16) -> String { fn align_text_center(&self, text: &str, width: u16) -> String {
let indent_size: usize = match (width as usize) >= text.len() { // NOTE: The check prevents underflow let indent_size: usize = match (width as usize) >= text.len() {
// NOTE: The check prevents underflow
true => (width as usize - text.len()) / 2, true => (width as usize - text.len()) / 2,
false => 0 false => 0,
}; };
textwrap::indent(text, (0..indent_size).map(|_| " ").collect::<String>().as_str()) textwrap::indent(
text,
(0..indent_size).map(|_| " ").collect::<String>().as_str(),
)
} }
} }