Skip to content

Commit

Permalink
Move calc_centered_dialog_rect to widget module
Browse files Browse the repository at this point in the history
  • Loading branch information
lusingander committed Dec 30, 2024
1 parent 9b88aa5 commit 6bba16b
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
19 changes: 0 additions & 19 deletions src/ui/common.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
use chrono::{DateTime, Local};
use ratatui::layout::{Constraint, Layout, Rect};

pub fn calc_centered_dialog_rect(r: Rect, dialog_width: u16, dialog_height: u16) -> Rect {
let vertical_pad = (r.height - dialog_height) / 2;
let vertical_layout = Layout::vertical(Constraint::from_lengths([
vertical_pad,
dialog_height,
vertical_pad,
]))
.split(r);

let horizontal_pad = (r.width - dialog_width) / 2;
Layout::horizontal(Constraint::from_lengths([
horizontal_pad,
dialog_width,
horizontal_pad,
]))
.split(vertical_layout[1])[1]
}

pub fn format_size_byte(size_byte: usize) -> String {
humansize::format_size_i(size_byte, humansize::BINARY)
Expand Down
1 change: 1 addition & 0 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod bar;
mod common;
mod copy_detail_dialog;
mod dialog;
mod divider;
Expand Down
19 changes: 19 additions & 0 deletions src/widget/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use ratatui::layout::{Constraint, Layout, Rect};

pub fn calc_centered_dialog_rect(r: Rect, dialog_width: u16, dialog_height: u16) -> Rect {
let vertical_pad = (r.height - dialog_height) / 2;
let vertical_layout = Layout::vertical(Constraint::from_lengths([
vertical_pad,
dialog_height,
vertical_pad,
]))
.split(r);

let horizontal_pad = (r.width - dialog_width) / 2;
Layout::horizontal(Constraint::from_lengths([
horizontal_pad,
dialog_width,
horizontal_pad,
]))
.split(vertical_layout[1])[1]
}
3 changes: 1 addition & 2 deletions src/widget/copy_detail_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ use ratatui::{
use crate::{
color::ColorTheme,
object::{BucketItem, FileDetail, ObjectItem},
ui::common::calc_centered_dialog_rect,
widget::Dialog,
widget::{common::calc_centered_dialog_rect, Dialog},
};

#[derive(Default)]
Expand Down
5 changes: 4 additions & 1 deletion src/widget/input_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use ratatui::{
};
use tui_input::{backend::crossterm::EventHandler, Input};

use crate::{color::ColorTheme, ui::common::calc_centered_dialog_rect, widget::Dialog};
use crate::{
color::ColorTheme,
widget::{common::calc_centered_dialog_rect, Dialog},
};

#[derive(Debug, Default)]
pub struct InputDialogState {
Expand Down
5 changes: 4 additions & 1 deletion src/widget/loading_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use ratatui::{
widgets::{Block, BorderType, Padding, Paragraph, Widget, WidgetRef},
};

use crate::{color::ColorTheme, ui::common::calc_centered_dialog_rect, widget::Dialog};
use crate::{
color::ColorTheme,
widget::{common::calc_centered_dialog_rect, Dialog},
};

#[derive(Debug, Default)]
struct LoadingDialogColor {
Expand Down
5 changes: 4 additions & 1 deletion src/widget/sort_list_dialog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use ratatui::{
widgets::{block::Title, Block, BorderType, List, ListItem, Padding, Widget, WidgetRef},
};

use crate::{color::ColorTheme, ui::common::calc_centered_dialog_rect, widget::Dialog};
use crate::{
color::ColorTheme,
widget::{common::calc_centered_dialog_rect, Dialog},
};

#[derive(Default)]
#[zero_indexed_enum]
Expand Down

0 comments on commit 6bba16b

Please sign in to comment.