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
use chargrid_render::*;
#[cfg(feature = "serialize")]
use serde::{Deserialize, Serialize};

/// The characters comprising a border. By default, borders are made of unicode
/// box-drawing characters, but they can be changed to arbitrary characters via
/// this struct.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone, Copy)]
pub struct BorderChars {
    pub top: char,
    pub bottom: char,
    pub left: char,
    pub right: char,
    pub top_left: char,
    pub top_right: char,
    pub bottom_left: char,
    pub bottom_right: char,
    pub before_title: char,
    pub after_title: char,
}

impl Default for BorderChars {
    fn default() -> Self {
        Self::single()
    }
}

impl BorderChars {
    pub fn single() -> Self {
        Self {
            top: '─',
            bottom: '─',
            left: '│',
            right: '│',
            top_left: '┌',
            top_right: '┐',
            bottom_left: '└',
            bottom_right: '┘',
            before_title: '┤',
            after_title: '├',
        }
    }
}

/// The space in cells between the edge of the bordered area
/// and the element inside.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Default, Debug, Clone, Copy)]
pub struct BorderPadding {
    pub top: u32,
    pub bottom: u32,
    pub left: u32,
    pub right: u32,
}

impl BorderPadding {
    pub fn all(padding: u32) -> Self {
        Self {
            top: padding,
            bottom: padding,
            left: padding,
            right: padding,
        }
    }
}

/// Decorate another element with a border.
/// It's possible to give the border a title, in which case
/// the text appears in the top-left corner.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct BorderStyle {
    pub title: Option<String>,
    pub padding: BorderPadding,
    pub chars: BorderChars,
    pub foreground: Rgb24,
    pub background: Option<Rgb24>,
    pub bold: bool,
    pub title_style: Style,
}

impl Default for BorderStyle {
    fn default() -> Self {
        Self::new()
    }
}

impl BorderStyle {
    pub fn new() -> Self {
        Self {
            title: None,
            padding: Default::default(),
            chars: Default::default(),
            foreground: Rgb24::new(255, 255, 255),
            background: None,
            bold: false,
            title_style: Style::new(),
        }
    }

    pub fn new_with_title<S: Into<String>>(title: S) -> Self {
        Self {
            title: Some(title.into()),
            ..Self::new()
        }
    }
    fn child_offset(&self) -> Coord {
        Coord {
            x: (self.padding.left + 1) as i32,
            y: (self.padding.top + 1) as i32,
        }
    }
    fn child_constrain_size_by(&self) -> Size {
        Size::new(
            self.padding.left + self.padding.right + 2,
            self.padding.top + self.padding.bottom + 2,
        )
    }
    fn span_offset(&self) -> Coord {
        Coord {
            x: (self.padding.left + self.padding.right + 1) as i32,
            y: (self.padding.top + self.padding.bottom + 1) as i32,
        }
    }
    fn view_cell(&self, character: char) -> ViewCell {
        ViewCell {
            character: Some(character),
            style: Style {
                foreground: Some(self.foreground),
                background: self.background,
                bold: Some(self.bold),
                underline: Some(false),
            },
        }
    }
}

fn draw_border<F, C>(style: &BorderStyle, size: Size, context: ViewContext<C>, frame: &mut F)
where
    C: ColModify,
    F: Frame,
{
    let span = style.span_offset() + size;
    frame.set_cell_relative(Coord::new(0, 0), 0, style.view_cell(style.chars.top_left), context);
    frame.set_cell_relative(
        Coord::new(span.x, 0),
        0,
        style.view_cell(style.chars.top_right),
        context,
    );
    frame.set_cell_relative(
        Coord::new(0, span.y),
        0,
        style.view_cell(style.chars.bottom_left),
        context,
    );
    frame.set_cell_relative(
        Coord::new(span.x, span.y),
        0,
        style.view_cell(style.chars.bottom_right),
        context,
    );
    let title_offset = if let Some(title) = style.title.as_ref() {
        let before = Coord::new(1, 0);
        let after = Coord::new(title.len() as i32 + 2, 0);
        frame.set_cell_relative(before, 0, style.view_cell(style.chars.before_title), context);
        frame.set_cell_relative(after, 0, style.view_cell(style.chars.after_title), context);
        for (index, ch) in title.chars().enumerate() {
            let coord = Coord::new(index as i32 + 2, 0);
            frame.set_cell_relative(
                coord,
                0,
                ViewCell {
                    style: style.title_style,
                    character: Some(ch),
                },
                context,
            );
        }
        title.len() as i32 + 2
    } else {
        0
    };
    for i in (1 + title_offset)..span.x {
        frame.set_cell_relative(Coord::new(i, 0), 0, style.view_cell(style.chars.top), context);
    }
    for i in 1..span.x {
        frame.set_cell_relative(Coord::new(i, span.y), 0, style.view_cell(style.chars.bottom), context);
    }
    for i in 1..span.y {
        frame.set_cell_relative(Coord::new(0, i), 0, style.view_cell(style.chars.left), context);
        frame.set_cell_relative(Coord::new(span.x, i), 0, style.view_cell(style.chars.right), context);
    }
}

fn border_view<V, T, F, C>(mut view: V, data: T, style: &BorderStyle, context: ViewContext<C>, frame: &mut F)
where
    V: View<T>,
    C: ColModify,
    F: Frame,
{
    let child_context = context
        .add_offset(style.child_offset())
        .constrain_size_by(style.child_constrain_size_by());
    let size = view.view_size(data, child_context, frame);
    draw_border(style, size, context, frame);
}

pub struct BorderView<'s, V> {
    pub view: V,
    pub style: &'s BorderStyle,
}

impl<'s, V, T> View<T> for BorderView<'s, V>
where
    V: View<T>,
{
    fn view<F: Frame, C: ColModify>(&mut self, data: T, context: ViewContext<C>, frame: &mut F) {
        border_view(&mut self.view, data, self.style, context, frame);
    }
}