Wrap message box texts; renamed ctext to msgbox

This commit is contained in:
veeso
2021-03-21 12:53:11 +01:00
parent 9b00feb286
commit 41360bb2c5
7 changed files with 78 additions and 56 deletions

View File

@@ -26,7 +26,7 @@
// Locals // Locals
use super::{AuthActivity, Context, FileTransferProtocol}; use super::{AuthActivity, Context, FileTransferProtocol};
use crate::ui::layout::components::{ use crate::ui::layout::components::{
bookmark_list::BookmarkList, ctext::CText, input::Input, radio_group::RadioGroup, table::Table, bookmark_list::BookmarkList, msgbox::MsgBox, input::Input, radio_group::RadioGroup, table::Table,
text::Text, title::Title, text::Text, title::Title,
}; };
use crate::ui::layout::props::{ use crate::ui::layout::props::{
@@ -436,7 +436,7 @@ impl AuthActivity {
// Mount // Mount
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_ERROR, super::COMPONENT_TEXT_ERROR,
Box::new(CText::new( Box::new(MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::Red) .with_foreground(Color::Red)
.bold() .bold()

View File

@@ -331,7 +331,7 @@ impl FileTransferActivity {
pub(super) fn action_remote_saveas(&mut self, input: String) { pub(super) fn action_remote_saveas(&mut self, input: String) {
if let Some(idx) = self.get_remote_file_idx() { if let Some(idx) = self.get_remote_file_idx() {
// Get pwd // Get pwd
let wrkdir: PathBuf = self.remote.wrkdir.clone(); let wrkdir: PathBuf = self.local.wrkdir.clone();
if self.remote.get(idx).is_some() { if self.remote.get(idx).is_some() {
let file: FsEntry = self.remote.get(idx).unwrap().clone(); let file: FsEntry = self.remote.get(idx).unwrap().clone();
// Call upload; pass realfile, keep link name // Call upload; pass realfile, keep link name

View File

@@ -37,7 +37,7 @@ use super::{
use crate::fs::explorer::FileSorting; use crate::fs::explorer::FileSorting;
use crate::fs::FsEntry; use crate::fs::FsEntry;
use crate::ui::activities::keymap::*; use crate::ui::activities::keymap::*;
use crate::ui::layout::props::{TableBuilder, TextParts, TextSpan, TextSpanBuilder}; use crate::ui::layout::props::{PropValue, TableBuilder, TextParts, TextSpan, TextSpanBuilder};
use crate::ui::layout::{Msg, Payload}; use crate::ui::layout::{Msg, Payload};
// externals // externals
use bytesize::ByteSize; use bytesize::ByteSize;
@@ -470,9 +470,9 @@ impl FileTransferActivity {
} }
self.umount_saveas(); self.umount_saveas();
// Reload files // Reload files
match self.tab { match self.tab { // NOTE: Swapped is intentional
FileExplorerTab::Local => self.update_local_filelist(), FileExplorerTab::Local => self.update_remote_filelist(),
FileExplorerTab::Remote => self.update_remote_filelist(), FileExplorerTab::Remote => self.update_local_filelist(),
} }
} }
// -- fileinfo // -- fileinfo
@@ -769,6 +769,7 @@ impl FileTransferActivity {
Some(text), Some(text),
Some(vec![TextSpan::from(label)]), Some(vec![TextSpan::from(label)]),
)) ))
.with_value(PropValue::Float(self.transfer.progress / 100.0))
.build(); .build();
self.view.update(COMPONENT_PROGRESS_BAR, props) self.view.update(COMPONENT_PROGRESS_BAR, props)
} }

View File

@@ -33,7 +33,7 @@ use super::{Context, FileExplorerTab, FileTransferActivity};
use crate::fs::explorer::FileSorting; use crate::fs::explorer::FileSorting;
use crate::fs::FsEntry; use crate::fs::FsEntry;
use crate::ui::layout::components::{ use crate::ui::layout::components::{
ctext::CText, file_list::FileList, input::Input, logbox::LogBox, progress_bar::ProgressBar, msgbox::MsgBox, file_list::FileList, input::Input, logbox::LogBox, progress_bar::ProgressBar,
radio_group::RadioGroup, table::Table, radio_group::RadioGroup, table::Table,
}; };
use crate::ui::layout::props::{ use crate::ui::layout::props::{
@@ -280,7 +280,7 @@ impl FileTransferActivity {
// Mount // Mount
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_ERROR, super::COMPONENT_TEXT_ERROR,
Box::new(CText::new( Box::new(MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::Red) .with_foreground(Color::Red)
.bold() .bold()
@@ -303,7 +303,7 @@ impl FileTransferActivity {
// Mount // Mount
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_FATAL, super::COMPONENT_TEXT_FATAL,
Box::new(CText::new( Box::new(MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::Red) .with_foreground(Color::Red)
.bold() .bold()
@@ -319,7 +319,7 @@ impl FileTransferActivity {
// Mount // Mount
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_WAIT, super::COMPONENT_TEXT_WAIT,
Box::new(CText::new( Box::new(MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::White) .with_foreground(Color::White)
.bold() .bold()
@@ -501,8 +501,8 @@ impl FileTransferActivity {
super::COMPONENT_PROGRESS_BAR, super::COMPONENT_PROGRESS_BAR,
Box::new(ProgressBar::new( Box::new(ProgressBar::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::Black) .with_foreground(Color::LightGreen)
.with_background(Color::LightGreen) .with_background(Color::Black)
.with_texts(TextParts::new(Some(String::from("Please wait")), None)) .with_texts(TextParts::new(Some(String::from("Please wait")), None))
.build(), .build(),
)), )),

View File

@@ -29,7 +29,7 @@ use super::{Context, SetupActivity, ViewLayout};
use crate::filetransfer::FileTransferProtocol; use crate::filetransfer::FileTransferProtocol;
use crate::fs::explorer::GroupDirs; use crate::fs::explorer::GroupDirs;
use crate::ui::layout::components::{ use crate::ui::layout::components::{
bookmark_list::BookmarkList, ctext::CText, input::Input, radio_group::RadioGroup, table::Table, bookmark_list::BookmarkList, msgbox::MsgBox, input::Input, radio_group::RadioGroup, table::Table,
text::Text, text::Text,
}; };
use crate::ui::layout::props::{ use crate::ui::layout::props::{
@@ -386,7 +386,7 @@ impl SetupActivity {
// Mount // Mount
self.view.mount( self.view.mount(
super::COMPONENT_TEXT_ERROR, super::COMPONENT_TEXT_ERROR,
Box::new(CText::new( Box::new(MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_foreground(Color::Red) .with_foreground(Color::Red)
.bold() .bold()

View File

@@ -28,10 +28,10 @@ use super::{Canvas, Component, InputEvent, Msg, Payload, PropValue, Props, Props
// exports // exports
pub mod bookmark_list; pub mod bookmark_list;
pub mod ctext;
pub mod file_list; pub mod file_list;
pub mod input; pub mod input;
pub mod logbox; pub mod logbox;
pub mod msgbox;
pub mod progress_bar; pub mod progress_bar;
pub mod radio_group; pub mod radio_group;
pub mod table; pub mod table;

View File

@@ -1,6 +1,6 @@
//! ## CText //! ## MsgBox
//! //!
//! `CText` component renders a simple readonly no event associated centered text //! `MsgBox` component renders a simple readonly no event associated centered text
/* /*
* *
@@ -23,15 +23,17 @@
* *
*/ */
// deps
extern crate textwrap;
// locals // locals
use super::{Canvas, Component, InputEvent, Msg, Payload, Props, PropsBuilder}; use super::{Canvas, Component, InputEvent, Msg, Payload, Props, PropsBuilder};
use crate::utils::fmt::align_text_center; use crate::utils::fmt::align_text_center;
// ext // ext
use tui::{ use tui::{
layout::Rect, layout::{Corner, Rect},
style::Style, style::{Color, Style},
text::{Span, Spans, Text as TuiText}, text::{Span, Spans},
widgets::{Block, BorderType, Paragraph}, widgets::{Block, BorderType, List, ListItem},
}; };
// -- state // -- state
@@ -48,24 +50,24 @@ impl Default for OwnStates {
// -- component // -- component
pub struct CText { pub struct MsgBox {
props: Props, props: Props,
states: OwnStates, states: OwnStates,
} }
impl CText { impl MsgBox {
/// ### new /// ### new
/// ///
/// Instantiate a new Text component /// Instantiate a new Text component
pub fn new(props: Props) -> Self { pub fn new(props: Props) -> Self {
CText { MsgBox {
props, props,
states: OwnStates::default(), states: OwnStates::default(),
} }
} }
} }
impl Component for CText { impl Component for MsgBox {
/// ### render /// ### render
/// ///
/// Based on the current properties and states, renders a widget using the provided render engine in the provided Area /// Based on the current properties and states, renders a widget using the provided render engine in the provided Area
@@ -74,36 +76,55 @@ impl Component for CText {
fn render(&self, render: &mut Canvas, area: Rect) { fn render(&self, render: &mut Canvas, area: Rect) {
// Make a Span // Make a Span
if self.props.visible { if self.props.visible {
let spans: Vec<Span> = match self.props.texts.rows.as_ref() { let lines: Vec<ListItem> = match self.props.texts.rows.as_ref() {
None => Vec::new(), None => Vec::new(),
Some(rows) => rows Some(rows) => {
.iter() let mut lines: Vec<ListItem> = Vec::new();
.map(|x| { for line in rows.iter() {
Span::styled( // Keep line color, or use default
align_text_center(x.content.as_str(), area.width), let line_fg: Color = match line.fg {
Style::default() Color::Reset => self.props.foreground,
.add_modifier(x.get_modifiers()) _ => line.fg,
.fg(x.fg)
.bg(x.bg),
)
})
.collect(),
}; };
// Make text let line_bg: Color = match line.bg {
let mut text: TuiText = TuiText::from(Spans::from(spans)); Color::Reset => self.props.background,
// Apply style _ => line.bg,
text.patch_style( };
let mut spans: Vec<Span> = Vec::new();
let message_row =
textwrap::wrap(line.content.as_str(), area.width as usize);
for msg in message_row.iter() {
spans.push(Span::styled(
align_text_center(msg, area.width),
Style::default() Style::default()
.add_modifier(self.props.get_modifiers()) .add_modifier(line.get_modifiers())
.fg(self.props.foreground) .fg(line_fg)
.bg(self.props.background), .bg(line_bg),
); ));
}
lines.push(ListItem::new(Spans::from(spans)));
}
lines
}
};
let title: String = match self.props.texts.title.as_ref() {
Some(t) => t.clone(),
None => String::new(),
};
render.render_widget( render.render_widget(
Paragraph::new(text).block( List::new(lines)
.block(
Block::default() Block::default()
.borders(self.props.borders) .borders(self.props.borders)
.border_style(Style::default().fg(self.props.foreground)) .border_style(Style::default().fg(self.props.foreground))
.border_type(BorderType::Rounded), .border_type(BorderType::Rounded)
.title(title),
)
.start_corner(Corner::TopLeft)
.style(
Style::default()
.fg(self.props.foreground)
.bg(self.props.background),
), ),
area, area,
); );
@@ -180,8 +201,8 @@ mod tests {
use tui::style::Color; use tui::style::Color;
#[test] #[test]
fn test_ui_layout_components_ctext() { fn test_ui_layout_components_msgbox() {
let mut component: CText = CText::new( let mut component: MsgBox = MsgBox::new(
PropsBuilder::default() PropsBuilder::default()
.with_texts(TextParts::new( .with_texts(TextParts::new(
None, None,