rat_widget/
paired.rs

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
use crate::_private::NonExhaustive;
use map_range_int::MapRange;
use rat_reloc::RelocatableState;
use rat_text::HasScreenCursor;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::{StatefulWidget, Widget};
use std::cmp::min;
use std::marker::PhantomData;

/// How to split the area for the two widgets.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PairSplit {
    Fix(u16, u16),
    Fix1(u16),
    Fix2(u16),
    Ratio(u16, u16),
}

/// Renders 2 widgets side by side.
#[derive(Debug)]
pub struct Paired<'a, T, U> {
    first: T,
    second: U,
    split: PairSplit,
    spacing: u16,
    phantom: PhantomData<&'a ()>,
}

#[derive(Debug)]
pub struct PairedState<'a, TS, US> {
    pub first: &'a mut TS,
    pub second: &'a mut US,

    pub non_exhaustive: NonExhaustive,
}

impl<T, U> Paired<'_, T, U> {
    pub fn new(first: T, second: U) -> Self {
        Self {
            first,
            second,
            split: PairSplit::Ratio(1, 1),
            spacing: 1,
            phantom: Default::default(),
        }
    }

    pub fn split(mut self, split: PairSplit) -> Self {
        self.split = split;
        self
    }

    pub fn spacing(mut self, spacing: u16) -> Self {
        self.spacing = spacing;
        self
    }
}

impl<T, U> Paired<'_, T, U> {
    fn layout(&self, area: Rect) -> (u16, u16, u16) {
        let mut sp = self.spacing;

        match self.split {
            PairSplit::Fix(a, b) => {
                if a + sp + b > area.width {
                    let rest = area.width - (a + sp + b);
                    (a - rest / 2, sp, b - (rest - rest / 2))
                } else {
                    let rest = (a + sp + b) - area.width;
                    (a + rest / 2, sp, b + (rest - rest / 2))
                }
            }
            PairSplit::Fix1(a) => {
                if a > area.width {
                    sp = 0;
                    (area.width, sp, 0)
                } else {
                    (a, sp, area.width.saturating_sub(a + sp))
                }
            }
            PairSplit::Fix2(b) => {
                if b > area.width {
                    sp = 0;
                    (area.width, sp, 0)
                } else {
                    (area.width.saturating_sub(b + sp), sp, b)
                }
            }
            PairSplit::Ratio(a, b) => {
                sp = min(sp, area.width);
                (
                    a.map_range_unchecked((0, a + b), (0, area.width - sp)),
                    sp,
                    b.map_range_unchecked((0, a + b), (0, area.width - sp)),
                )
            }
        }
    }
}

impl<'a, T, U, TS, US> StatefulWidget for Paired<'a, T, U>
where
    T: StatefulWidget<State = TS>,
    U: StatefulWidget<State = US>,
    TS: 'a,
    US: 'a,
{
    type State = PairedState<'a, TS, US>;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        let (a, sp, b) = self.layout(area);

        let area_a = Rect::new(area.x, area.y, a, area.height);
        let area_b = Rect::new(area.x + a + sp, area.y, b, area.height);

        self.first.render(area_a, buf, state.first);
        self.second.render(area_b, buf, state.second);
    }
}

impl<T, U> Widget for Paired<'_, T, U>
where
    T: Widget,
    U: Widget,
{
    fn render(self, area: Rect, buf: &mut Buffer)
    where
        Self: Sized,
    {
        let (a, sp, b) = self.layout(area);

        let area_a = Rect::new(area.x, area.y, a, area.height);
        let area_b = Rect::new(area.x + a + sp, area.y, b, area.height);

        self.first.render(area_a, buf);
        self.second.render(area_b, buf);
    }
}

impl<TS, US> HasScreenCursor for PairedState<'_, TS, US>
where
    TS: HasScreenCursor,
    US: HasScreenCursor,
{
    fn screen_cursor(&self) -> Option<(u16, u16)> {
        self.first.screen_cursor().or(self.second.screen_cursor())
    }
}

impl<TS, US> RelocatableState for PairedState<'_, TS, US>
where
    TS: RelocatableState,
    US: RelocatableState,
{
    fn relocate(&mut self, shift: (i16, i16), clip: Rect) {
        self.first.relocate(shift, clip);
        self.second.relocate(shift, clip);
    }
}

impl<'a, TS, US> PairedState<'a, TS, US> {
    pub fn new(first: &'a mut TS, second: &'a mut US) -> Self {
        Self {
            first,
            second,
            non_exhaustive: NonExhaustive,
        }
    }
}