Layout utils

This commit is contained in:
veeso
2021-03-14 14:30:37 +01:00
parent 8f3fe14843
commit bda69c661f
2 changed files with 73 additions and 10 deletions

View File

@@ -27,6 +27,7 @@
pub mod components; pub mod components;
pub mod props; pub mod props;
pub mod view; pub mod view;
pub mod utils;
// locals // locals
use props::{PropValue, Props, PropsBuilder}; use props::{PropValue, Props, PropsBuilder};
@@ -61,20 +62,11 @@ pub enum Msg {
#[derive(std::fmt::Debug, PartialEq, Eq)] #[derive(std::fmt::Debug, PartialEq, Eq)]
pub enum Payload { pub enum Payload {
Text(String), Text(String),
Signed(isize), //Signed(isize),
Unsigned(usize), Unsigned(usize),
None, None,
} }
// -- RenderData
/// ## RenderData
///
/// RenderData is the object which contains data related to the component render
pub struct RenderData {
pub cursor: usize, // Cursor position
}
// -- Component // -- Component
/// ## Component /// ## Component

71
src/ui/layout/utils.rs Normal file
View File

@@ -0,0 +1,71 @@
//! ## 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);
}
}