diff --git a/src/ui/layout/mod.rs b/src/ui/layout/mod.rs index d6c8106..4b9bd54 100644 --- a/src/ui/layout/mod.rs +++ b/src/ui/layout/mod.rs @@ -27,6 +27,7 @@ pub mod components; pub mod props; pub mod view; +pub mod utils; // locals use props::{PropValue, Props, PropsBuilder}; @@ -61,20 +62,11 @@ pub enum Msg { #[derive(std::fmt::Debug, PartialEq, Eq)] pub enum Payload { Text(String), - Signed(isize), + //Signed(isize), Unsigned(usize), 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 diff --git a/src/ui/layout/utils.rs b/src/ui/layout/utils.rs new file mode 100644 index 0000000..bf4bd40 --- /dev/null +++ b/src/ui/layout/utils.rs @@ -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 . +* +*/ + +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); + } +}