use crate::{
direction::Direction,
event::{AnyCb, Event, EventResult},
rect::Rect,
view::{CannotFocus, Selector, View, ViewNotFound},
Printer, Vec2,
};
pub trait ViewWrapper: 'static {
type V: View + ?Sized;
fn with_view<F, R>(&self, f: F) -> Option<R>
where
F: FnOnce(&Self::V) -> R;
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
where
F: FnOnce(&mut Self::V) -> R;
fn into_inner(self) -> Result<Self::V, Self>
where
Self: Sized,
Self::V: Sized,
{
Err(self)
}
fn wrap_draw(&self, printer: &Printer) {
self.with_view(|v| v.draw(printer));
}
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
self.with_view_mut(|v| v.required_size(req))
.unwrap_or_else(Vec2::zero)
}
fn wrap_on_event(&mut self, ch: Event) -> EventResult {
self.with_view_mut(|v| v.on_event(ch))
.unwrap_or(EventResult::Ignored)
}
fn wrap_layout(&mut self, size: Vec2) {
self.with_view_mut(|v| v.layout(size));
}
fn wrap_take_focus(
&mut self,
source: Direction,
) -> Result<EventResult, CannotFocus> {
self.with_view_mut(|v| v.take_focus(source))
.unwrap_or(Err(CannotFocus))
}
fn wrap_call_on_any(&mut self, selector: &Selector, callback: AnyCb) {
self.with_view_mut(|v| v.call_on_any(selector, callback));
}
fn wrap_focus_view(
&mut self,
selector: &Selector,
) -> Result<EventResult, ViewNotFound> {
self.with_view_mut(|v| v.focus_view(selector))
.unwrap_or(Err(ViewNotFound))
}
fn wrap_needs_relayout(&self) -> bool {
self.with_view(View::needs_relayout).unwrap_or(true)
}
fn wrap_important_area(&self, size: Vec2) -> Rect {
self.with_view(|v| v.important_area(size))
.unwrap_or_else(|| Rect::from_size(Vec2::zero(), size))
}
}
impl<T: ViewWrapper> View for T {
fn draw(&self, printer: &Printer) {
self.wrap_draw(printer);
}
fn required_size(&mut self, req: Vec2) -> Vec2 {
self.wrap_required_size(req)
}
fn on_event(&mut self, ch: Event) -> EventResult {
self.wrap_on_event(ch)
}
fn layout(&mut self, size: Vec2) {
self.wrap_layout(size);
}
fn take_focus(
&mut self,
source: Direction,
) -> Result<EventResult, CannotFocus> {
self.wrap_take_focus(source)
}
fn call_on_any(&mut self, selector: &Selector, callback: AnyCb) {
self.wrap_call_on_any(selector, callback)
}
fn needs_relayout(&self) -> bool {
self.wrap_needs_relayout()
}
fn focus_view(
&mut self,
selector: &Selector,
) -> Result<EventResult, ViewNotFound> {
self.wrap_focus_view(selector)
}
fn important_area(&self, size: Vec2) -> Rect {
self.wrap_important_area(size)
}
}
#[macro_export]
macro_rules! wrap_impl {
(self.$v:ident: $t:ty) => {
type V = $t;
fn with_view<F, R>(&self, f: F) -> ::std::option::Option<R>
where
F: ::std::ops::FnOnce(&Self::V) -> R,
{
::std::option::Option::Some(f(&self.$v))
}
fn with_view_mut<F, R>(&mut self, f: F) -> ::std::option::Option<R>
where
F: ::std::ops::FnOnce(&mut Self::V) -> R,
{
::std::option::Option::Some(f(&mut self.$v))
}
fn into_inner(self) -> ::std::result::Result<Self::V, Self>
where
Self::V: ::std::marker::Sized,
{
::std::result::Result::Ok(self.$v)
}
};
}
#[macro_export]
macro_rules! inner_getters {
(self.$v:ident: $t:ty) => {
pub fn get_inner(&self) -> &$t {
&self.$v
}
pub fn get_inner_mut(&mut self) -> &mut $t {
&mut self.$v
}
};
}