From b872714dc17baefeb232c547662a56b144b7d4eb Mon Sep 17 00:00:00 2001 From: kosay Date: Tue, 4 Jul 2023 00:55:24 +0900 Subject: [PATCH 1/3] feat(ui/popup): Adjust popup size to 85% of the container --- src/ui/popup.rs | 45 +++++++++++++++++---------------------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/src/ui/popup.rs b/src/ui/popup.rs index bb884384..8078633a 100644 --- a/src/ui/popup.rs +++ b/src/ui/popup.rs @@ -1,7 +1,7 @@ use crossterm::event::{KeyEvent, MouseEvent}; use ratatui::{ backend::Backend, - layout::{Constraint, Direction, Layout, Margin, Rect}, + layout::{Margin, Rect}, widgets::Clear, Frame, }; @@ -29,39 +29,33 @@ use super::{ /// │ ▼ │ /// └─────────────────────────────────────────────────┘ #[derive(Debug)] -pub struct PopupChunkSize { - pub margin_left: Constraint, - pub margin_right: Constraint, - pub margin_top: Constraint, - pub margin_bottom: Constraint, - pub content_width: Constraint, - pub content_height: Constraint, +struct PopupChunkSize { + // content width percentage (0.0 ~ 100.0) + width: f32, + // content height percentage 0.0 ~ 100.0 + height: f32, } impl Default for PopupChunkSize { fn default() -> Self { Self { - margin_left: Constraint::Percentage(5), - margin_right: Constraint::Percentage(5), - margin_top: Constraint::Percentage(5), - margin_bottom: Constraint::Percentage(5), - content_width: Constraint::Percentage(90), - content_height: Constraint::Percentage(90), + width: 85.0, + height: 85.0, } } } impl PopupChunkSize { fn chunk(&self, parent_chunk: Rect) -> Rect { - let chunk = Layout::default() - .direction(Direction::Vertical) - .constraints([self.margin_top, self.content_height, self.margin_bottom]) - .split(parent_chunk); - - Layout::default() - .direction(Direction::Horizontal) - .constraints([self.margin_left, self.content_width, self.margin_right]) - .split(chunk[1])[1] + let horizontal_margin = + (parent_chunk.width as f32 * ((100.0 - self.width) / 2.0 / 100.0)).round() as u16; + let vertical_margin = + (parent_chunk.height as f32 * ((100.0 - self.height) / 2.0 / 100.0)).round() as u16; + + parent_chunk.inner(&Margin { + vertical: vertical_margin, + horizontal: horizontal_margin, + }) } } @@ -80,11 +74,6 @@ impl<'a> Popup<'a> { } } - pub fn chunk_size(mut self, chunk_size: PopupChunkSize) -> Self { - self.chunk_size = chunk_size; - self - } - pub fn chunk(&self) -> Rect { self.chunk } From ed1cf2515ace74f6da23bdbfe1fe98e245b57694 Mon Sep 17 00:00:00 2001 From: kosay Date: Tue, 4 Jul 2023 00:59:44 +0900 Subject: [PATCH 2/3] fix(ui): Fix issue of crashing due to overflow when resizing to an extremely small size --- src/ui/widget/complex/multiple_select.rs | 4 ++-- src/ui/widget/text.rs | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ui/widget/complex/multiple_select.rs b/src/ui/widget/complex/multiple_select.rs index 3ab13a5e..c2a52b0e 100644 --- a/src/ui/widget/complex/multiple_select.rs +++ b/src/ui/widget/complex/multiple_select.rs @@ -236,7 +236,7 @@ impl<'a> SelectForm<'a> { let (cx, cy, cw, ch) = ( self.chunk.x, self.chunk.y, - self.chunk.width / 2 - 1, + (self.chunk.width / 2).saturating_sub(1), self.chunk.height, ); @@ -259,7 +259,7 @@ impl<'a> SelectForm<'a> { let left_chunk = Rect::new(cx, cy, cw, ch); let center_chunk = Rect::new(cx, cy + ch, cw, 1); - let right_chunk = Rect::new(cx, center_chunk.y + 1, cw, ch - margin); + let right_chunk = Rect::new(cx, center_chunk.y + 1, cw, ch.saturating_sub(margin)); ([left_chunk, center_chunk, right_chunk], "↓".to_string()) } diff --git a/src/ui/widget/text.rs b/src/ui/widget/text.rs index f0d198e0..bb464c58 100644 --- a/src/ui/widget/text.rs +++ b/src/ui/widget/text.rs @@ -133,7 +133,12 @@ impl Default for SearchForm { impl SearchForm { fn update_chunk(&mut self, chunk: Rect) { - self.chunk = Rect::new(chunk.x, chunk.y + chunk.height - 1, chunk.width, 1); + self.chunk = Rect::new( + chunk.x, + chunk.y + chunk.height.saturating_sub(1), + chunk.width, + 1, + ); } fn word(&self) -> String { From 111a051c9ca275201f16c545a972835c388c415a Mon Sep 17 00:00:00 2001 From: kosay Date: Tue, 4 Jul 2023 01:07:21 +0900 Subject: [PATCH 3/3] chore(ui/popup): update comment [ci skip] --- src/ui/popup.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ui/popup.rs b/src/ui/popup.rs index 8078633a..1f167228 100644 --- a/src/ui/popup.rs +++ b/src/ui/popup.rs @@ -30,9 +30,9 @@ use super::{ /// └─────────────────────────────────────────────────┘ #[derive(Debug)] struct PopupChunkSize { - // content width percentage (0.0 ~ 100.0) + /// content width percentage (0.0 ~ 100.0) width: f32, - // content height percentage 0.0 ~ 100.0 + /// content height percentage (0.0 ~ 100.0) height: f32, }