mirror of
https://github.com/veeso/termscp.git
synced 2026-07-14 20:13:46 +02:00
72 lines
2.1 KiB
Rust
72 lines
2.1 KiB
Rust
//! ## Utils
|
|
//!
|
|
//! `Utils` implements utilities functions to work with layouts
|
|
|
|
/*
|
|
*
|
|
* Copyright (C) 2020-2021 Christian Visintin - christian.visintin1997@gmail.com
|
|
*
|
|
* This file is part of "TermSCP"
|
|
*
|
|
* TermSCP is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* TermSCP is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with TermSCP. If not, see <http://www.gnu.org/licenses/>.
|
|
*
|
|
*/
|
|
|
|
use tui::layout::{Constraint, Direction, Layout, Rect};
|
|
|
|
/// ### draw_area_in
|
|
///
|
|
/// Draw an area (WxH / 3) in the middle of the parent area
|
|
pub fn draw_area_in(parent: Rect, width: u16, height: u16) -> Rect {
|
|
let new_area = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.constraints(
|
|
[
|
|
Constraint::Percentage((100 - height) / 2),
|
|
Constraint::Percentage(height),
|
|
Constraint::Percentage((100 - height) / 2),
|
|
]
|
|
.as_ref(),
|
|
)
|
|
.split(parent);
|
|
Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints(
|
|
[
|
|
Constraint::Percentage((100 - width) / 2),
|
|
Constraint::Percentage(width),
|
|
Constraint::Percentage((100 - width) / 2),
|
|
]
|
|
.as_ref(),
|
|
)
|
|
.split(new_area[1])[1]
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_ui_layout_utils_draw_area_in() {
|
|
let area: Rect = Rect::new(0, 0, 1024, 512);
|
|
let child: Rect = draw_area_in(area, 75, 30);
|
|
println!("{:?}", child);
|
|
assert_eq!(child.x, 43);
|
|
assert_eq!(child.y, 63);
|
|
assert_eq!(child.width, 271);
|
|
assert_eq!(child.height, 54);
|
|
}
|
|
}
|