use embedded_graphics::primitives::Rectangle;
use heapless::Vec;
use crate::Localized;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum ViewKind {
Custom,
Image,
Text,
Scroll,
Stack,
Tab,
Split,
AlertHost,
List,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ViewProperties<'a> {
pub title: Option<Localized<'a>>,
pub alpha: u8,
pub hidden: bool,
pub clips_to_bounds: bool,
pub user_interaction_enabled: bool,
}
impl Default for ViewProperties<'_> {
fn default() -> Self {
Self {
title: None,
alpha: 255,
hidden: false,
clips_to_bounds: false,
user_interaction_enabled: true,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ChildView<'a, ViewId> {
pub id: ViewId,
pub kind: ViewKind,
pub frame: Rectangle,
pub properties: ViewProperties<'a>,
}
impl<'a, ViewId> ChildView<'a, ViewId> {
pub const fn new(id: ViewId, frame: Rectangle) -> Self {
Self {
id,
kind: ViewKind::Custom,
frame,
properties: ViewProperties {
title: None,
alpha: 255,
hidden: false,
clips_to_bounds: false,
user_interaction_enabled: true,
},
}
}
pub const fn with_kind(mut self, kind: ViewKind) -> Self {
self.kind = kind;
self
}
pub const fn with_title(mut self, title: Localized<'a>) -> Self {
self.properties.title = Some(title);
self
}
pub const fn with_alpha(mut self, alpha: u8) -> Self {
self.properties.alpha = alpha;
self
}
pub const fn with_hidden(mut self, hidden: bool) -> Self {
self.properties.hidden = hidden;
self
}
pub const fn with_clipping(mut self, clips_to_bounds: bool) -> Self {
self.properties.clips_to_bounds = clips_to_bounds;
self
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ViewRegistration<'a, ViewId, const N: usize> {
frame: Rectangle,
properties: ViewProperties<'a>,
children: Vec<ChildView<'a, ViewId>, N>,
}
impl<'a, ViewId, const N: usize> ViewRegistration<'a, ViewId, N> {
pub fn new(frame: Rectangle) -> Self {
Self {
frame,
properties: ViewProperties::default(),
children: Vec::new(),
}
}
pub const fn frame(&self) -> Rectangle {
self.frame
}
pub fn set_frame(&mut self, frame: Rectangle) {
self.frame = frame;
}
pub const fn properties(&self) -> &ViewProperties<'a> {
&self.properties
}
pub fn set_title(&mut self, title: Localized<'a>) {
self.properties.title = Some(title);
}
pub fn set_alpha(&mut self, alpha: u8) {
self.properties.alpha = alpha;
}
pub fn set_hidden(&mut self, hidden: bool) {
self.properties.hidden = hidden;
}
pub fn set_clips_to_bounds(&mut self, clips_to_bounds: bool) {
self.properties.clips_to_bounds = clips_to_bounds;
}
pub fn set_user_interaction_enabled(&mut self, enabled: bool) {
self.properties.user_interaction_enabled = enabled;
}
pub fn clear_children(&mut self) {
self.children.clear();
}
pub fn add_child(&mut self, child: ChildView<'a, ViewId>) -> Result<(), ChildView<'a, ViewId>> {
self.children.push(child)
}
pub fn children(&self) -> &[ChildView<'a, ViewId>] {
self.children.as_slice()
}
}