diff --git a/examples/lorem.rs b/examples/lorem.rs index 0a68a96..9140d72 100644 --- a/examples/lorem.rs +++ b/examples/lorem.rs @@ -19,7 +19,8 @@ fn main() { // The text is too long to fit on a line, so the view will wrap lines, // and will adapt to the terminal size. - siv.add_layer(TextView::new(&content)); + siv.add_layer(Dialog::new(TextView::new(&content)) + .button("Quit", |s| s.quit())); // Show a popup on top of the view. siv.add_layer(Dialog::new(TextView::new("Try resizing the terminal!\n(Press 'q' to quit when you're done.)")) .padding((0,0,0,0)) diff --git a/src/view/stack_view.rs b/src/view/stack_view.rs index 4525adc..3ddc61e 100644 --- a/src/view/stack_view.rs +++ b/src/view/stack_view.rs @@ -15,6 +15,8 @@ pub struct StackView { struct Layer { view: Box, size: Vec2, + // Has it received the gift yet? + virgin: bool, } impl StackView { @@ -26,11 +28,11 @@ impl StackView { } /// Add new view on top of the stack. - pub fn add_layer(&mut self, mut view: T) { - view.take_focus(); + pub fn add_layer(&mut self, view: T) { self.layers.push(Layer { view: Box::new(ShadowView::new(view)), size: Vec2::new(0,0), + virgin: true, }); } @@ -42,12 +44,13 @@ impl StackView { impl View for StackView { fn draw(&mut self, printer: &Printer) { - for v in self.layers.iter_mut() { + let last = self.layers.len(); + for (i,v) in self.layers.iter_mut().enumerate() { // Center the view let size = v.size; let offset = (printer.size - size) / 2; // TODO: only draw focus for the top view - v.view.draw(&printer.sub_printer(offset, size, true)); + v.view.draw(&printer.sub_printer(offset, size, i+1 == last)); } } @@ -66,6 +69,10 @@ impl View for StackView { for layer in self.layers.iter_mut() { layer.size = Vec2::min(size, layer.view.get_min_size(req)); layer.view.layout(layer.size); + if layer.virgin { + layer.view.take_focus(); + layer.virgin = false; + } } } diff --git a/src/view/text_view.rs b/src/view/text_view.rs index 05818c6..3ad7091 100644 --- a/src/view/text_view.rs +++ b/src/view/text_view.rs @@ -247,6 +247,10 @@ impl View for TextView { } } + fn take_focus(&mut self) -> bool { + self.view_height < self.rows.len() + } + fn layout(&mut self, size: Vec2) { // Compute the text rows. self.view_height = size.y;