1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
use tui::{backend::Backend, layout::Rect, Frame};
use crate::tuice::{Bounds, DrawContext, Element, Event, LayoutNode, Size, Status};
use super::Axis;
pub struct FlexElement<'a, Message, B: Backend> {
/// Represents a ratio with other [`FlexElement`]s on how far to expand.
pub flex: u16,
element: Element<'a, Message, B>,
}
impl<'a, Message, B: Backend> FlexElement<'a, Message, B> {
pub fn new<I: Into<Element<'a, Message, B>>>(element: I) -> Self {
Self {
flex: 1,
element: element.into(),
}
}
pub fn with_flex<I: Into<Element<'a, Message, B>>>(element: I, flex: u16) -> Self {
Self {
flex,
element: element.into(),
}
}
pub fn with_no_flex<I: Into<Element<'a, Message, B>>>(element: I) -> Self {
Self {
flex: 0,
element: element.into(),
}
}
pub fn flex(mut self, flex: u16) -> Self {
self.flex = flex;
self
}
pub(crate) fn draw(&mut self, context: DrawContext<'_>, frame: &mut Frame<'_, B>) {
self.element.draw(context, frame)
}
pub(crate) fn on_event(
&mut self, area: Rect, event: Event, messages: &mut Vec<Message>,
) -> Status {
self.element.on_event(area, event, messages)
}
/// Assumes the flex is 0. Just calls layout on its child.
pub(crate) fn child_layout(&self, bounds: Bounds, node: &mut LayoutNode) -> Size {
self.element.layout(bounds, node)
}
/// Assumes the flex is NOT 0. Will call layout on its children, but will ignore
/// its sizing.
///
/// **Note it does NOT check for div by zero!** Please check this yourself.
pub(crate) fn ratio_layout(
&self, bounds: Bounds, total_flex: u16, node: &mut LayoutNode, parent_alignment: Axis,
) -> Size {
let (width, height) = match parent_alignment {
Axis::Horizontal => (bounds.max_width * self.flex / total_flex, bounds.max_height),
Axis::Vertical => (bounds.max_width, bounds.max_height * self.flex / total_flex),
};
self.element.layout(
Bounds {
min_width: width,
min_height: height,
max_width: width,
max_height: height,
},
node,
);
Size { width, height }
}
}
impl<'a, Message, B: Backend> From<Element<'a, Message, B>> for FlexElement<'a, Message, B> {
fn from(element: Element<'a, Message, B>) -> Self {
Self { flex: 0, element }
}
}
|