diff --git a/CHANGELOG.md b/CHANGELOG.md index 929c3b2..59a7c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Released on FIXME: - **Ui**: - Transfer abortion is now more responsive - Selected files will now be rendered with **Reversed, underlined and italic** text modifiers instead of being prepended with `*`. + - Error popup message height is now calculated based on the content it must display. - **Midnight commander keys** - ``: Show help - ``: Save file as (actually I invented this) @@ -74,7 +75,8 @@ Released on FIXME: - By default the log level is now set to `INFO` - It is now possible to enable the `TRACE` level with the `-D` CLI option. - Dependencies: - - Updated `tui-realm` to `1.3.0` + - Added `unicode-width 0.1.8` + - Updated `tui-realm` to `1.4.2` - Updated `tui-realm-stdlib` to `1.1.4` - Removed `rust-s3`, `ssh2`, `suppaftp`; replaced by `remotefs 0.2.0`, `remotefs-aws-s3 0.1.0`, `remotefs-ftp 0.1.0` and `remotefs-ssh 0.1.0` - Removed `crossterm` (since bridged by tui-realm) diff --git a/Cargo.lock b/Cargo.lock index 801117f..368ef58 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2288,6 +2288,7 @@ dependencies = [ "toml", "tui-realm-stdlib", "tuirealm", + "unicode-width", "users", "whoami", "wildmatch", diff --git a/Cargo.toml b/Cargo.toml index 9acac0c..aa1c595 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -62,6 +62,7 @@ thiserror = "^1.0.0" toml = "0.5.8" tui-realm-stdlib = "^1.1.0" tuirealm = "^1.2.0" +unicode-width = "^0.1.8" whoami = "1.1.1" wildmatch = "2.0.0" diff --git a/src/ui/activities/filetransfer/view.rs b/src/ui/activities/filetransfer/view.rs index 64de25b..52d1cec 100644 --- a/src/ui/activities/filetransfer/view.rs +++ b/src/ui/activities/filetransfer/view.rs @@ -38,6 +38,7 @@ use tuirealm::event::{Key, KeyEvent, KeyModifiers}; use tuirealm::tui::layout::{Constraint, Direction, Layout}; use tuirealm::tui::widgets::Clear; use tuirealm::{Sub, SubClause, SubEventClause}; +use unicode_width::UnicodeWidthStr; impl FileTransferActivity { // -- init @@ -292,12 +293,22 @@ impl FileTransferActivity { // make popup self.app.view(&Id::SortingPopup, f, popup); } else if self.app.mounted(&Id::ErrorPopup) { - let popup = draw_area_in(f.size(), 50, 10); + // TODO: inject dynamic height here + let popup = draw_area_in( + f.size(), + 50, + self.calc_popup_height(Id::ErrorPopup, f.size().width, f.size().height), + ); f.render_widget(Clear, popup); // make popup self.app.view(&Id::ErrorPopup, f, popup); } else if self.app.mounted(&Id::FatalPopup) { - let popup = draw_area_in(f.size(), 50, 10); + // TODO: inject dynamic height here + let popup = draw_area_in( + f.size(), + 50, + self.calc_popup_height(Id::FatalPopup, f.size().width, f.size().height), + ); f.render_widget(Clear, popup); // make popup self.app.view(&Id::FatalPopup, f, popup); @@ -854,6 +865,39 @@ impl FileTransferActivity { let _ = self.app.umount(&Id::KeybindingsPopup); } + // -- dynamic size + + /// Given the id of the component to display and the width and height of the total area, + /// returns the height in percentage to the entire area height, that the popup should have + fn calc_popup_height(&self, id: Id, width: u16, height: u16) -> u16 { + // Get current text width + let text_width = self + .app + .query(&id, tuirealm::Attribute::Text) + .ok() + .flatten() + .map(|x| { + x.unwrap_payload() + .unwrap_vec() + .into_iter() + .map(|x| x.unwrap_text_span().content) + .collect::>() + .join("") + .width() as u16 + }) + .unwrap_or(0); + // Calc real width of a row in the popup + let row_width = (width / 2).saturating_sub(2); + // Calc row height in percentage (1 : height = x : 100) + let row_height_p = (100.0 / (height as f64)).ceil() as u16; + // Get amount of required rows NOTE: + 2 because of margins + let display_rows = ((text_width as f64) / (row_width as f64)).ceil() as u16 + 2; + // Return height (row_height_p * display_rows) + display_rows * row_height_p + } + + // -- global listener + fn mount_global_listener(&mut self) { assert!(self .app