rat_theme/
dark_theme.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
//!
//! Implements a dark theme.
//!
use crate::Scheme;
use rat_widget::button::ButtonStyle;
use rat_widget::file_dialog::FileDialogStyle;
use rat_widget::line_number::LineNumberStyle;
use rat_widget::list::ListStyle;
use rat_widget::menu::MenuStyle;
use rat_widget::msgdialog::MsgDialogStyle;
use rat_widget::scrolled::ScrollStyle;
use rat_widget::splitter::SplitStyle;
use rat_widget::tabbed::TabbedStyle;
use rat_widget::table::TableStyle;
use rat_widget::text_input::TextInputStyle;
use rat_widget::textarea::TextAreaStyle;
use ratatui::prelude::{Style, Stylize};
use ratatui::widgets::Block;

/// One sample theme which prefers dark colors from the color-scheme
/// and generates styles for widgets.
///
/// The widget set fits for the widgets provided by
/// [rat-widget](https://www.docs.rs/rat-widget), for other needs
/// take it as an idea for your own implementation.
///
#[derive(Debug, Clone)]
pub struct DarkTheme {
    s: Scheme,
    name: String,
}

impl DarkTheme {
    pub fn new(name: String, s: Scheme) -> Self {
        Self { s, name }
    }
}

impl DarkTheme {
    /// Some display name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Hint at dark.
    pub fn dark_theme(&self) -> bool {
        true
    }

    /// The underlying scheme.
    pub fn scheme(&self) -> &Scheme {
        &self.s
    }

    /// Create a style from the given white shade.
    /// n is `0..=3`
    pub fn white(&self, n: usize) -> Style {
        self.s.style(self.s.white[n])
    }

    /// Create a style from the given black shade.
    /// n is `0..=3`
    pub fn black(&self, n: usize) -> Style {
        self.s.style(self.s.black[n])
    }

    /// Create a style from the given gray shade.
    /// n is `0..=3`
    pub fn gray(&self, n: usize) -> Style {
        self.s.style(self.s.gray[n])
    }

    /// Create a style from the given red shade.
    /// n is `0..=3`
    pub fn red(&self, n: usize) -> Style {
        self.s.style(self.s.red[n])
    }

    /// Create a style from the given orange shade.
    /// n is `0..=3`
    pub fn orange(&self, n: usize) -> Style {
        self.s.style(self.s.orange[n])
    }

    /// Create a style from the given yellow shade.
    /// n is `0..=3`
    pub fn yellow(&self, n: usize) -> Style {
        self.s.style(self.s.yellow[n])
    }

    /// Create a style from the given limegreen shade.
    /// n is `0..=3`
    pub fn limegreen(&self, n: usize) -> Style {
        self.s.style(self.s.limegreen[n])
    }

    /// Create a style from the given green shade.
    /// n is `0..=3`
    pub fn green(&self, n: usize) -> Style {
        self.s.style(self.s.green[n])
    }

    /// Create a style from the given bluegreen shade.
    /// n is `0..=3`
    pub fn bluegreen(&self, n: usize) -> Style {
        self.s.style(self.s.bluegreen[n])
    }

    /// Create a style from the given cyan shade.
    /// n is `0..=3`
    pub fn cyan(&self, n: usize) -> Style {
        self.s.style(self.s.cyan[n])
    }

    /// Create a style from the given blue shade.
    /// n is `0..=3`
    pub fn blue(&self, n: usize) -> Style {
        self.s.style(self.s.blue[n])
    }

    /// Create a style from the given deepblue shade.
    /// n is `0..=3`
    pub fn deepblue(&self, n: usize) -> Style {
        self.s.style(self.s.deepblue[n])
    }

    /// Create a style from the given purple shade.
    /// n is `0..=3`
    pub fn purple(&self, n: usize) -> Style {
        self.s.style(self.s.purple[n])
    }

    /// Create a style from the given magenta shade.
    /// n is `0..=3`
    pub fn magenta(&self, n: usize) -> Style {
        self.s.style(self.s.magenta[n])
    }

    /// Create a style from the given redpink shade.
    /// n is `0..=3`
    pub fn redpink(&self, n: usize) -> Style {
        self.s.style(self.s.redpink[n])
    }

    /// Create a style from the given primary shade.
    /// n is `0..=3`
    pub fn primary(&self, n: usize) -> Style {
        self.s.style(self.s.primary[n])
    }

    /// Create a style from the given secondary shade.
    /// n is `0..=3`
    pub fn secondary(&self, n: usize) -> Style {
        self.s.style(self.s.secondary[n])
    }

    /// Focus style
    pub fn focus(&self) -> Style {
        let bg = self.s.primary[2];
        Style::default().fg(self.s.text_color(bg)).bg(bg)
    }

    /// Selection style
    pub fn select(&self) -> Style {
        let bg = self.s.secondary[1];
        Style::default().fg(self.s.text_color(bg)).bg(bg)
    }

    /// Text field style.
    pub fn text_input(&self) -> Style {
        Style::default().fg(self.s.black[0]).bg(self.s.gray[3])
    }

    /// Focused text field style.
    pub fn text_focus(&self) -> Style {
        let bg = self.s.primary[0];
        Style::default().fg(self.s.text_color(bg)).bg(bg)
    }

    /// Text selection style.
    pub fn text_select(&self) -> Style {
        let bg = self.s.secondary[0];
        Style::default().fg(self.s.text_color(bg)).bg(bg)
    }

    /// Data display style. Used for lists, tables, ...
    pub fn data(&self) -> Style {
        Style::default().fg(self.s.white[0]).bg(self.s.black[1])
    }

    /// Background for dialogs.
    pub fn dialog_style(&self) -> Style {
        Style::default().fg(self.s.white[2]).bg(self.s.gray[1])
    }

    /// Style for the status line.
    pub fn status_style(&self) -> Style {
        Style::default().fg(self.s.white[0]).bg(self.s.black[2])
    }

    /// Style for LineNumbers.
    pub fn line_nr_style(&self) -> LineNumberStyle {
        LineNumberStyle {
            style: self.data().fg(self.s.gray[0]),
            cursor: Some(self.text_select()),
            ..LineNumberStyle::default()
        }
    }

    /// Complete TextAreaStyle
    pub fn textarea_style(&self) -> TextAreaStyle {
        TextAreaStyle {
            style: self.data(),
            focus: Some(self.focus()),
            select: Some(self.text_select()),
            ..TextAreaStyle::default()
        }
    }

    /// Complete TextInputStyle
    pub fn input_style(&self) -> TextInputStyle {
        TextInputStyle {
            style: self.text_input(),
            focus: Some(self.text_focus()),
            select: Some(self.text_select()),
            invalid: Some(Style::default().bg(self.s.red[3])),
            ..TextInputStyle::default()
        }
    }

    /// Complete MenuStyle
    pub fn menu_style(&self) -> MenuStyle {
        let menu = Style::default().fg(self.s.white[3]).bg(self.s.black[2]);
        MenuStyle {
            style: menu,
            title: Some(Style::default().fg(self.s.black[0]).bg(self.s.yellow[2])),
            select: Some(self.select()),
            focus: Some(self.focus()),
            right: Some(Style::default().fg(self.s.bluegreen[0])),
            disabled: Some(Style::default().fg(self.s.gray[0])),
            highlight: Some(Style::default().underlined()),
            popup_block: Some(Block::bordered()),
            ..Default::default()
        }
    }

    /// Complete FTableStyle
    pub fn table_style(&self) -> TableStyle {
        TableStyle {
            style: self.data(),
            select_row: Some(self.select()),
            show_row_focus: true,
            focus_style: Some(self.focus()),
            ..Default::default()
        }
    }

    /// Complete ListStyle
    pub fn list_style(&self) -> ListStyle {
        ListStyle {
            style: self.data(),
            select: Some(self.select()),
            focus: Some(self.focus()),
            ..Default::default()
        }
    }

    /// Complete ButtonStyle
    pub fn button_style(&self) -> ButtonStyle {
        ButtonStyle {
            style: Style::default()
                .fg(self.s.text_color(self.s.primary[0]))
                .bg(self.s.primary[0]),
            focus: Some(
                Style::default()
                    .fg(self.s.text_color(self.s.primary[3]))
                    .bg(self.s.primary[3]),
            ),
            armed: Some(Style::default().fg(self.s.black[0]).bg(self.s.secondary[0])),
            ..Default::default()
        }
    }

    /// Complete ScrolledStyle
    pub fn scroll_style(&self) -> ScrollStyle {
        let style = Style::default().fg(self.s.gray[0]).bg(self.s.black[1]);
        let arrow_style = Style::default().fg(self.s.secondary[0]).bg(self.s.black[1]);
        ScrollStyle {
            thumb_style: Some(style),
            track_style: Some(style),
            min_style: Some(style),
            begin_style: Some(arrow_style),
            end_style: Some(arrow_style),
            ..Default::default()
        }
    }

    /// Complete Split style
    pub fn split_style(&self) -> SplitStyle {
        let style = Style::default().fg(self.s.gray[0]).bg(self.s.black[1]);
        let arrow_style = Style::default().fg(self.s.secondary[0]).bg(self.s.black[1]);
        SplitStyle {
            style,
            arrow_style: Some(arrow_style),
            drag_style: Some(self.focus()),
            ..Default::default()
        }
    }

    /// Complete Tabbed style
    pub fn tabbed_style(&self) -> TabbedStyle {
        let style = Style::default().fg(self.s.gray[0]).bg(self.s.black[1]);
        TabbedStyle {
            style,
            tab: Some(self.gray(1)),
            select: Some(self.gray(3)),
            focus: Some(self.focus()),
            ..Default::default()
        }
    }

    /// Complete StatusLineStyle for a StatusLine with 3 indicator fields.
    /// This is what I need for the
    /// [minimal](https://github.com/thscharler/rat-salsa/blob/master/examples/minimal.rs)
    /// example, which shows timings for Render/Event/Action.
    pub fn statusline_style(&self) -> Vec<Style> {
        let s = &self.s;
        vec![
            self.status_style(),
            Style::default().fg(s.text_color(s.white[0])).bg(s.blue[3]),
            Style::default().fg(s.text_color(s.white[0])).bg(s.blue[2]),
            Style::default().fg(s.text_color(s.white[0])).bg(s.blue[1]),
        ]
    }

    /// Complete MsgDialogStyle.
    pub fn msg_dialog_style(&self) -> MsgDialogStyle {
        MsgDialogStyle {
            style: self.dialog_style(),
            button: self.button_style(),
            ..Default::default()
        }
    }

    pub fn file_dialog_style(&self) -> FileDialogStyle {
        FileDialogStyle {
            style: self.dialog_style(),
            list: Some(self.data()),
            path: Some(self.text_input()),
            name: Some(self.text_input()),
            new: Some(self.text_input()),
            invalid: Some(Style::new().fg(self.s.red[3]).bg(self.s.gray[2])),
            select: Some(self.select()),
            focus: Some(self.focus()),
            button: Some(self.button_style()),
            ..Default::default()
        }
    }
}