leetcode_tui_shared/
layout.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// ┌─────────────────────────────────────────────────────────────────────────┐
// │                                  Leetui                                 │
// │  ┌─Topics─────┐ ┌─Questions──────────────────────────────────────────┐  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │          ┌─┘Popup────────────────────────────────────┐            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          │                                           │            │  │
// │  │          └─┬─┬───────────────────────────────────────┘            │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  └────────────┘ └────────────────────────────────────────────────────┘  │
// │  /search                                                      [?] Help  │
// └─────────────────────────────────────────────────────────────────────────┘

// ┌─────────────────────────────────────────────────────────────────────────┐
// │                                  Leetui                                 │
// │  ┌─Topics─────┐ ┌─Stats──────────────────────────────────────────────┐  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  │            │ │                                                    │  │
// │  └────────────┘ └────────────────────────────────────────────────────┘  │
// │  /search                                                      [?] Help  │
// └─────────────────────────────────────────────────────────────────────────┘

use ratatui::{
    prelude::*,
    widgets::{Block, Borders, Widget},
};

#[derive(Debug, Clone)]
pub struct BlockAreas {
    pub inner: Rect,
    pub outer: Rect,
}

impl From<Rect> for BlockAreas {
    fn from(value: Rect) -> Self {
        Self {
            outer: value,
            inner: value.blockify(),
        }
    }
}

#[derive(Debug)]
pub struct Window {
    pub root: Root,
}

#[derive(Debug)]
pub struct StatusBar {
    pub search_area: Rect,
    pub message_area: Rect,
}

impl From<Rect> for StatusBar {
    fn from(value: Rect) -> Self {
        let left_and_right_div = Layout::new()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
            .split(value);
        Self {
            search_area: left_and_right_div[0],
            message_area: left_and_right_div[1],
        }
    }
}

#[derive(Debug)]
pub struct Root {
    pub top_bar: Rect,
    pub center_layout: CenterLayout,
    pub status_bar: StatusBar,
    pub popup: BlockAreas,
    pub q_stats: BlockAreas,
}

#[derive(Debug)]
pub struct CenterLayout {
    pub question: BlockAreas,
    pub topic: BlockAreas,
}

impl CenterLayout {
    fn new(chunks: Rect) -> Self {
        let center_chunks = Layout::new()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Percentage(20), Constraint::Percentage(80)].as_ref())
            .split(chunks);

        let topic_area = center_chunks[0];
        let question_area = center_chunks[1];
        Self {
            question: question_area.into(),
            topic: topic_area.into(),
        }
    }
}

impl Root {
    fn new(ar: Rect) -> Self {
        let chunks = Layout::new()
            .direction(Direction::Vertical)
            .constraints(
                [
                    Constraint::Length(1),
                    Constraint::Min(0),
                    Constraint::Length(1),
                ]
                .as_ref(),
            )
            .split(ar);

        let mut r = Self {
            top_bar: chunks[0],
            center_layout: CenterLayout::new(chunks[1]),
            status_bar: chunks[2].into(),
            popup: centered_rect(60, 60, ar).into(),
            q_stats: centered_rect(60, 60, ar).into(),
        };
        r.q_stats = r.center_layout.question.clone();
        r
    }
}

impl Default for Window {
    fn default() -> Self {
        let term_size = super::tui::Term::size();
        let window_rect = Rect::new(0, 0, term_size.columns, term_size.rows);
        Self {
            root: Root::new(window_rect),
        }
    }
}

pub trait GetWindowStats {
    fn get_window(&self) -> Window {
        Window::default()
    }
}

impl<T> GetWindowStats for T where T: Widget {}

trait Blockify {
    fn blockify(self) -> Rect;
}

impl Blockify for Rect {
    fn blockify(self) -> Rect {
        Block::default().borders(Borders::ALL).inner(self)
    }
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints(
            [
                Constraint::Percentage((100 - percent_y) / 2),
                Constraint::Percentage(percent_y),
                Constraint::Percentage((100 - percent_y) / 2),
            ]
            .as_ref(),
        )
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints(
            [
                Constraint::Percentage((100 - percent_x) / 2),
                Constraint::Percentage(percent_x),
                Constraint::Percentage((100 - percent_x) / 2),
            ]
            .as_ref(),
        )
        .split(popup_layout[1])[1]
}