Skip to content

Commit

Permalink
Merge pull request #364 from sarub0b0/adjust-popup-size
Browse files Browse the repository at this point in the history
feat(ui/popup): Adjust popup size to 85% of the container
  • Loading branch information
sarub0b0 authored Jul 3, 2023
2 parents 9587df9 + 111a051 commit 6c10089
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
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

0 comments on commit 6c10089

Please sign in to comment.