cursive/src/view/view_wrapper.rs

72 lines
1.5 KiB
Rust
Raw Normal View History

2015-05-19 02:41:35 +00:00
use vec::Vec2;
use view::{View,SizeRequest};
use printer::Printer;
use event::EventResult;
pub trait ViewWrapper {
fn get_view(&self) -> &View;
fn get_view_mut(&mut self) -> &mut View;
fn wrap_draw(&self, printer: &Printer, focused: bool) {
self.get_view().draw(printer, focused);
2015-05-19 02:41:35 +00:00
}
fn wrap_get_min_size(&self, req: SizeRequest) -> Vec2 {
self.get_view().get_min_size(req)
}
fn wrap_on_key_event(&mut self, ch: i32) -> EventResult {
self.get_view_mut().on_key_event(ch)
}
fn wrap_layout(&mut self, size: Vec2) {
self.get_view_mut().layout(size);
}
}
impl <T: ViewWrapper> View for T {
fn draw(&self, printer: &Printer, focused: bool) {
self.wrap_draw(printer, focused);
2015-05-19 02:41:35 +00:00
}
fn get_min_size(&self, req: SizeRequest) -> Vec2 {
self.wrap_get_min_size(req)
}
fn on_key_event(&mut self, ch: i32) -> EventResult {
self.wrap_on_key_event(ch)
}
fn layout(&mut self, size: Vec2) {
self.wrap_layout(size);
}
}
#[macro_export]
macro_rules! wrap_impl {
(self.$v:ident) => {
fn get_view(&self) -> &View {
&self.$v
}
fn get_view_mut(&mut self) -> &mut View {
&mut self.$v
}
};
}
#[macro_export]
macro_rules! wrap_impl_box {
(self.$v:ident) => {
fn get_view(&self) -> &View {
&*self.$v
}
fn get_view_mut(&mut self) -> &mut View {
&mut *self.$v
}
};
}