Add input support to linear layouts

This commit is contained in:
Alexandre Bury 2015-06-08 15:11:44 -07:00
parent d5a796a4cd
commit daaff26e1c
2 changed files with 37 additions and 0 deletions

View File

@ -1,5 +1,6 @@
use vec::Vec2;
#[derive(Clone,Copy,PartialEq)]
pub enum Orientation {
Horizontal,
Vertical,

View File

@ -2,6 +2,7 @@ use view::{View,SizeRequest,DimensionRequest};
use vec::Vec2;
use printer::Printer;
use orientation::Orientation;
use event::{Event,EventResult,Key};
struct Child {
view: Box<View>,
@ -12,6 +13,7 @@ struct Child {
pub struct LinearLayout {
children: Vec<Child>,
orientation: Orientation,
focus: usize,
}
@ -20,6 +22,7 @@ impl LinearLayout {
LinearLayout {
children: Vec::new(),
orientation: orientation,
focus: 0,
}
}
@ -154,4 +157,37 @@ impl View for LinearLayout {
// Find out who's fluid, if any.
}
fn on_event(&mut self, event: Event) -> EventResult {
match self.children[self.focus].view.on_event(event) {
EventResult::Ignored => match event {
Event::KeyEvent(Key::Tab) if self.focus > 0 => {
self.focus -= 1;
EventResult::Consumed(None)
},
Event::KeyEvent(Key::ShiftTab) if self.focus+1 < self.children.len() => {
self.focus += 1;
EventResult::Consumed(None)
}
Event::KeyEvent(Key::Left) if self.orientation == Orientation::Horizontal && self.focus > 0 => {
self.focus -= 1;
EventResult::Consumed(None)
},
Event::KeyEvent(Key::Up) if self.orientation == Orientation::Vertical && self.focus > 0 => {
self.focus -= 1;
EventResult::Consumed(None)
},
Event::KeyEvent(Key::Right) if self.orientation == Orientation::Horizontal && self.focus+1 < self.children.len() => {
self.focus += 1;
EventResult::Consumed(None)
},
Event::KeyEvent(Key::Down) if self.orientation == Orientation::Vertical && self.focus+1 < self.children.len() => {
self.focus += 1;
EventResult::Consumed(None)
},
_ => EventResult::Ignored,
},
res => res,
}
}
}