2019-02-28 23:54:12 +00:00
|
|
|
use crate::align::*;
|
|
|
|
use crate::event::{Event, EventResult};
|
|
|
|
use crate::rect::Rect;
|
|
|
|
use crate::theme::ColorStyle;
|
|
|
|
use crate::view::{View, ViewWrapper};
|
|
|
|
use crate::Printer;
|
2020-01-06 23:51:38 +00:00
|
|
|
use crate::Vec2;
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::With;
|
2019-03-01 00:04:14 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2016-07-25 20:35:46 +00:00
|
|
|
|
|
|
|
/// Draws a border around a wrapped view.
|
2017-12-12 01:31:55 +00:00
|
|
|
#[derive(Debug)]
|
2020-07-03 06:58:54 +00:00
|
|
|
pub struct Panel<V> {
|
2018-10-02 16:02:10 +00:00
|
|
|
// Inner view
|
2016-07-25 20:35:46 +00:00
|
|
|
view: V,
|
2018-10-02 16:02:10 +00:00
|
|
|
|
|
|
|
// Possibly empty title.
|
|
|
|
title: String,
|
|
|
|
|
|
|
|
// Where to put the title position
|
|
|
|
title_position: HAlign,
|
|
|
|
|
|
|
|
// `true` when we needs to relayout
|
|
|
|
invalidated: bool,
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 06:58:54 +00:00
|
|
|
new_default!(Panel<V: Default>);
|
|
|
|
|
|
|
|
impl<V> Panel<V> {
|
2016-07-25 20:35:46 +00:00
|
|
|
/// Creates a new panel around the given view.
|
|
|
|
pub fn new(view: V) -> Self {
|
2018-10-02 16:02:10 +00:00
|
|
|
Panel {
|
|
|
|
view,
|
|
|
|
title: String::new(),
|
|
|
|
title_position: HAlign::Center,
|
|
|
|
invalidated: true,
|
|
|
|
}
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
2018-01-26 11:06:14 +00:00
|
|
|
|
2018-10-02 16:02:10 +00:00
|
|
|
/// Sets the title of the dialog.
|
|
|
|
///
|
|
|
|
/// If not empty, it will be visible at the top.
|
|
|
|
pub fn title<S: Into<String>>(self, label: S) -> Self {
|
|
|
|
self.with(|s| s.set_title(label))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the title of the dialog.
|
|
|
|
pub fn set_title<S: Into<String>>(&mut self, label: S) {
|
|
|
|
self.title = label.into();
|
|
|
|
self.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the horizontal position of the title in the dialog.
|
|
|
|
/// The default position is `HAlign::Center`
|
|
|
|
pub fn title_position(self, align: HAlign) -> Self {
|
|
|
|
self.with(|s| s.set_title_position(align))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the horizontal position of the title in the dialog.
|
|
|
|
/// The default position is `HAlign::Center`
|
|
|
|
pub fn set_title_position(&mut self, align: HAlign) {
|
|
|
|
self.title_position = align;
|
|
|
|
}
|
|
|
|
|
2020-07-04 03:38:08 +00:00
|
|
|
fn draw_title(&self, printer: &Printer) {
|
2018-10-02 16:02:10 +00:00
|
|
|
if !self.title.is_empty() {
|
|
|
|
let len = self.title.width();
|
2019-03-03 09:37:50 +00:00
|
|
|
let spacing = 3; //minimum distance to borders
|
|
|
|
let spacing_both_ends = 2 * spacing;
|
|
|
|
if len + spacing_both_ends > printer.size.x {
|
2018-10-02 16:02:10 +00:00
|
|
|
return;
|
|
|
|
}
|
2019-03-01 00:04:14 +00:00
|
|
|
let x = spacing
|
|
|
|
+ self
|
|
|
|
.title_position
|
2019-03-03 09:37:50 +00:00
|
|
|
.get_offset(len, printer.size.x - spacing_both_ends);
|
2018-10-02 16:02:10 +00:00
|
|
|
printer.with_high_border(false, |printer| {
|
|
|
|
printer.print((x - 2, 0), "┤ ");
|
|
|
|
printer.print((x + len, 0), " ├");
|
|
|
|
});
|
|
|
|
|
|
|
|
printer.with_color(ColorStyle::title_primary(), |p| {
|
|
|
|
p.print((x, 0), &self.title)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn invalidate(&mut self) {
|
|
|
|
self.invalidated = true;
|
|
|
|
}
|
2019-10-26 02:53:43 +00:00
|
|
|
|
|
|
|
inner_getters!(self.view: V);
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<V: View> ViewWrapper for Panel<V> {
|
2016-09-20 00:11:00 +00:00
|
|
|
wrap_impl!(self.view: V);
|
2016-07-25 20:35:46 +00:00
|
|
|
|
2017-10-13 03:43:33 +00:00
|
|
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
|
|
|
self.view.on_event(event.relativized((1, 1)))
|
|
|
|
}
|
|
|
|
|
2017-01-24 06:52:29 +00:00
|
|
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
2016-07-25 20:35:46 +00:00
|
|
|
// TODO: make borders conditional?
|
2017-08-14 23:32:01 +00:00
|
|
|
let req = req.saturating_sub((2, 2));
|
2017-03-06 22:53:10 +00:00
|
|
|
|
|
|
|
self.view.required_size(req) + (2, 2)
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
|
|
|
|
2020-07-04 03:38:08 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer) {
|
2016-08-05 00:27:04 +00:00
|
|
|
printer.print_box((0, 0), printer.size, true);
|
2018-10-02 16:02:10 +00:00
|
|
|
self.draw_title(&printer);
|
|
|
|
|
2018-04-03 23:38:54 +00:00
|
|
|
let printer = printer.offset((1, 1)).shrinked((1, 1));
|
2018-03-22 22:21:54 +00:00
|
|
|
self.view.draw(&printer);
|
2017-03-15 23:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn wrap_layout(&mut self, size: Vec2) {
|
2017-08-14 23:32:01 +00:00
|
|
|
self.view.layout(size.saturating_sub((2, 2)));
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|
2018-03-22 22:21:54 +00:00
|
|
|
|
|
|
|
fn wrap_important_area(&self, size: Vec2) -> Rect {
|
|
|
|
let inner_size = size.saturating_sub((2, 2));
|
|
|
|
self.view.important_area(inner_size) + (1, 1)
|
|
|
|
}
|
2018-10-02 16:02:10 +00:00
|
|
|
|
|
|
|
fn wrap_needs_relayout(&self) -> bool {
|
2018-10-02 18:20:14 +00:00
|
|
|
self.invalidated || self.view.needs_relayout()
|
2018-10-02 16:02:10 +00:00
|
|
|
}
|
2016-07-25 20:35:46 +00:00
|
|
|
}
|