From 592ecf4c097e498c4ab3f6e8df7409925caa6ac7 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 12 Dec 2017 08:38:14 +0100 Subject: [PATCH 1/3] added option to position the header in a dialog --- src/views/dialog.rs | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 47b02b3..6d470c2 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -49,6 +49,9 @@ pub struct Dialog { // Possibly empty title. title: String, + // Where to put the title position + title_position: HAlign, + // The actual inner view. content: SizedView>, @@ -85,6 +88,7 @@ impl Dialog { content: SizedView::new(Box::new(view)), buttons: Vec::new(), title: String::new(), + title_position: HAlign::Center, focus: Focus::Content, padding: Vec4::new(1, 1, 0, 0), borders: Vec4::new(1, 1, 1, 1), @@ -169,6 +173,16 @@ impl Dialog { self.title = label.into(); } + /// Sets the horizontal position of the title in the dialog. + 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. + pub fn set_title_position(&mut self, align: HAlign) { + self.title_position = align; + } + /// Sets the padding in the dialog (around content and buttons). pub fn padding>(mut self, padding: T) -> Self { self.padding = padding.into(); @@ -348,7 +362,11 @@ impl Dialog { if len + 4 > printer.size.x { return; } - let x = (printer.size.x - len) / 2; + let x = match self.title_position { + HAlign::Left => 3, + HAlign::Center => (printer.size.x - len) / 2, + HAlign::Right => printer.size.x - len - 3, + }; printer.with_high_border(false, |printer| { printer.print((x - 2, 0), "┤ "); printer.print((x + len, 0), " ├"); From f8e7427f75ffb4b8a33b823408284b9989f9554f Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 12 Dec 2017 09:07:13 +0100 Subject: [PATCH 2/3] simplified offset calculation in dialogboxtitle --- src/views/dialog.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 6d470c2..26adf22 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -362,11 +362,8 @@ impl Dialog { if len + 4 > printer.size.x { return; } - let x = match self.title_position { - HAlign::Left => 3, - HAlign::Center => (printer.size.x - len) / 2, - HAlign::Right => printer.size.x - len - 3, - }; + let spacing = 3; //minimum distance to borders + let x = spacing + self.title_position.get_offset(len, printer.size.x - 2 * spacing); printer.with_high_border(false, |printer| { printer.print((x - 2, 0), "┤ "); printer.print((x + len, 0), " ├"); From 9ce8896bba404656515da35e33c461c78c78ca19 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 12 Dec 2017 09:19:05 +0100 Subject: [PATCH 3/3] updated dialog doc to reflect the default position --- src/views/dialog.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 26adf22..880df08 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -174,11 +174,13 @@ impl Dialog { } /// 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; }