Add outset border functions to Printer

This commit is contained in:
Alexandre Bury 2016-08-05 10:49:16 -07:00
parent 215e88ae3e
commit 921e4a451e
2 changed files with 47 additions and 30 deletions

View File

@ -133,35 +133,14 @@ impl Printer {
} }
let size = size - (1, 1); let size = size - (1, 1);
let borders = if let Some(borders) = self.theme.borders { self.with_high_border(invert, |s| {
borders
} else {
// No border, no box...
// TODO: still allow other boxes?
// For instance make the check in the view.
return;
};
self.with_color(match borders {
BorderStyle::Outset if !invert => {
ColorStyle::Tertiary
}
_ => ColorStyle::Primary,
},
|s| {
s.print(start, ""); s.print(start, "");
s.print(start + size.keep_y(), ""); s.print(start + size.keep_y(), "");
s.print_hline(start + (1, 0), size.x - 1, ""); s.print_hline(start + (1, 0), size.x - 1, "");
s.print_vline(start + (0, 1), size.y - 1, ""); s.print_vline(start + (0, 1), size.y - 1, "");
}); });
self.with_color(match borders { self.with_low_border(invert, |s| {
BorderStyle::Outset if invert => {
ColorStyle::Tertiary
}
_ => ColorStyle::Primary,
},
|s| {
s.print(start + size.keep_x(), ""); s.print(start + size.keep_x(), "");
s.print(start + size, ""); s.print(start + size, "");
s.print_hline(start + (1, 0) + size.keep_y(), size.x - 1, ""); s.print_hline(start + (1, 0) + size.keep_y(), size.x - 1, "");
@ -169,6 +148,42 @@ impl Printer {
}); });
} }
/// Runs the given function using a color depending on the theme.
///
/// * If the theme's borders is `None`, return without calling `f`.
/// * If the theme's borders is "outset" and `invert` is `false`,
/// use `ColorStyle::Tertiary`.
/// * Otherwise, use `ColorStyle::Primary`.
pub fn with_high_border<F>(&self, invert: bool, f: F)
where F: FnOnce(&Printer)
{
let color = match self.theme.borders {
None => return,
Some(BorderStyle::Outset) if !invert => ColorStyle::Tertiary,
_ => ColorStyle::Primary,
};
self.with_color(color, f);
}
/// Runs the given function using a color depending on the theme.
///
/// * If the theme's borders is `None`, return without calling `f`.
/// * If the theme's borders is "outset" and `invert` is `true`,
/// use `ColorStyle::Tertiary`.
/// * Otherwise, use `ColorStyle::Primary`.
pub fn with_low_border<F>(&self, invert: bool, f: F)
where F: FnOnce(&Printer)
{
let color = match self.theme.borders {
None => return,
Some(BorderStyle::Outset) if invert => ColorStyle::Secondary,
_ => ColorStyle::Primary,
};
self.with_color(color, f);
}
/// Apply a selection style and call the given function. /// Apply a selection style and call the given function.
/// ///
/// * If `selection` is `false`, simply uses `ColorStyle::Primary`. /// * If `selection` is `false`, simply uses `ColorStyle::Primary`.

View File

@ -226,8 +226,10 @@ impl View for Dialog {
return; return;
} }
let x = (printer.size.x - len) / 2; let x = (printer.size.x - len) / 2;
printer.with_high_border(false, |printer| {
printer.print((x - 2, 0), ""); printer.print((x - 2, 0), "");
printer.print((x + len, 0), ""); printer.print((x + len, 0), "");
});
printer.with_color(ColorStyle::TitlePrimary, printer.with_color(ColorStyle::TitlePrimary,
|p| p.print((x, 0), &self.title)); |p| p.print((x, 0), &self.title));