Skip to main content

ratatui_kit/components/
center.rs

1use crate::{AnyElement, Component, ComponentUpdater, Hooks, Props, element, prelude::View};
2use ratatui::layout::{Constraint, Direction, Flex};
3
4#[derive(Default, Props)]
5pub struct CenterProps<'a> {
6    pub width: Constraint,
7    pub height: Constraint,
8    pub children: Vec<AnyElement<'a>>,
9}
10
11// 居中布局组件
12pub struct Center;
13
14impl Component for Center {
15    type Props<'a> = CenterProps<'a>;
16
17    fn new(_props: &Self::Props<'_>) -> Self {
18        Self
19    }
20
21    fn update(
22        &mut self,
23        props: &mut Self::Props<'_>,
24        _hooks: Hooks,
25        updater: &mut ComponentUpdater,
26    ) {
27        updater.set_transparent_layout(true);
28        updater.update_children(
29            [element!(
30                View(
31                    justify_content:Flex::Center
32                ){
33                    View(
34                       height:props.height,
35                       justify_content:Flex::Center,
36                       flex_direction:Direction::Horizontal,
37                    ){
38                        View(
39                            width:props.width,
40                            justify_content:Flex::Center,
41                            flex_direction:Direction::Vertical,
42                        ){
43                            { props.children.iter_mut() }
44                        }
45                    }
46                }
47            )],
48            None,
49        );
50    }
51}