Fix focus for stack and text views.

And updated Lorem example with a Quit button under the scrollable text.
This commit is contained in:
Alexandre Bury 2015-05-30 21:53:25 -07:00
parent 6b781684ef
commit db39069260
3 changed files with 17 additions and 5 deletions

View File

@ -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))

View File

@ -15,6 +15,8 @@ pub struct StackView {
struct Layer {
view: Box<View>,
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<T: 'static + View>(&mut self, mut view: T) {
view.take_focus();
pub fn add_layer<T: 'static + View>(&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;
}
}
}

View File

@ -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;