use std::cell::RefCell;
use crate::support::point::Point;
use crate::support::rect::Rect;
use crate::support::canvas::Canvas;
use crate::view::View;
use super::Element;
pub struct BasicContext<'a> {
pub view: &'a View,
pub canvas: &'a RefCell<Canvas>,
}
impl<'a> BasicContext<'a> {
pub fn new(view: &'a View, canvas: &'a RefCell<Canvas>) -> Self {
Self { view, canvas }
}
pub fn view_bounds(&self) -> Rect {
self.view.bounds()
}
pub fn cursor_pos(&self) -> Point {
self.view.cursor_pos()
}
}
pub struct Context<'a> {
pub view: &'a View,
pub canvas: &'a RefCell<Canvas>,
pub element: Option<&'a dyn Element>,
pub parent: Option<&'a Context<'a>>,
pub bounds: Rect,
pub enabled: bool,
}
impl<'a> Context<'a> {
pub fn new(view: &'a View, canvas: &'a RefCell<Canvas>, bounds: Rect) -> Self {
Self {
view,
canvas,
element: None,
parent: None,
bounds,
enabled: true,
}
}
pub fn with_bounds(&self, bounds: Rect) -> Context<'a> {
Context {
view: self.view,
canvas: self.canvas,
element: self.element,
parent: None, bounds,
enabled: self.enabled,
}
}
pub fn view_bounds(&self) -> Rect {
self.view.bounds()
}
pub fn cursor_pos(&self) -> Point {
self.view.cursor_pos()
}
pub fn is_enabled(&self) -> bool {
self.enabled
}
}
pub struct ContextBuilder<'a> {
view: &'a View,
element: Option<&'a dyn Element>,
parent: Option<&'a Context<'a>>,
bounds: Rect,
enabled: bool,
}
impl<'a> ContextBuilder<'a> {
pub fn from_parent(parent: &'a Context<'a>) -> Self {
Self {
view: parent.view,
element: parent.element,
parent: Some(parent),
bounds: parent.bounds,
enabled: parent.enabled,
}
}
pub fn bounds(mut self, bounds: Rect) -> Self {
self.bounds = bounds;
self
}
pub fn element(mut self, element: &'a dyn Element) -> Self {
self.element = Some(element);
self.enabled = self.enabled && element.is_enabled();
self
}
pub fn build(self, canvas: &'a RefCell<Canvas>) -> Context<'a> {
Context {
view: self.view,
canvas,
element: self.element,
parent: self.parent,
bounds: self.bounds,
enabled: self.enabled,
}
}
}