From 592ecf4c097e498c4ab3f6e8df7409925caa6ac7 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Tue, 12 Dec 2017 08:38:14 +0100 Subject: [PATCH] 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), " ├");