Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ui/popup): Adjust popup size to 85% of the container #364

Merged
merged 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions src/ui/popup.rs
Original file line number Diff line number Diff line change
@@ -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,
};
Expand Down Expand Up @@ -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,
})
}
}

Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/ui/widget/complex/multiple_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);

Expand All @@ -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())
}
Expand Down
7 changes: 6 additions & 1 deletion src/ui/widget/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down