use std::{any::TypeId, collections::HashSet};
use kurbo::Affine;
use crate::core::{
PropertySet, PropertyStackId, SubwindowId, Widget, WidgetId, WidgetTag, WidgetTagInner,
};
pub struct WidgetPod<W: Widget + ?Sized> {
id: WidgetId,
inner: WidgetPodInner<W>,
}
#[non_exhaustive]
pub struct NewWidget<W: ?Sized> {
pub widget: Box<W>,
pub(crate) id: WidgetId,
pub(crate) subwindow_id: Option<SubwindowId>,
pub(crate) action_type: TypeId,
#[cfg(debug_assertions)]
pub(crate) action_type_name: &'static str,
pub options: WidgetOptions,
pub properties: PropertySet,
pub property_stack_id: Option<PropertyStackId>,
pub classes: HashSet<String>,
pub(crate) tag: Option<WidgetTagInner>,
}
impl<W: Widget + ?Sized> std::fmt::Debug for WidgetPod<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("WidgetPod")
.field("id", &self.id)
.field("inner", &self.inner)
.finish()
}
}
impl<W: Widget + ?Sized> std::fmt::Debug for WidgetPodInner<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Create(arg0) => f.debug_tuple("Create").field(arg0).finish(),
Self::Inserted => write!(f, "Inserted"),
}
}
}
impl<W: Widget + ?Sized> std::fmt::Debug for NewWidget<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("NewWidget")
.field("widget_type", &self.widget.short_type_name())
.field("id", &self.id)
.field("options", &self.options)
.field("tag", &self.tag)
.field("classes", &self.classes)
.finish_non_exhaustive()
}
}
#[derive(Default, Debug)]
pub struct WidgetOptions {
pub transform: Affine,
pub disabled: bool,
}
enum WidgetPodInner<W: Widget + ?Sized> {
Create(Box<NewWidget<W>>),
Inserted,
}
impl<W: Widget> NewWidget<W> {
#[inline(always)]
pub fn new(inner: W) -> Self {
Self {
widget: Box::new(inner),
id: WidgetId::next(),
subwindow_id: None,
action_type: TypeId::of::<W::Action>(),
#[cfg(debug_assertions)]
action_type_name: std::any::type_name::<W::Action>(),
options: WidgetOptions::default(),
properties: PropertySet::default(),
property_stack_id: None,
classes: HashSet::new(),
tag: None,
}
}
}
impl<W: Widget + ?Sized> NewWidget<W> {
pub fn erased(self) -> NewWidget<dyn Widget> {
NewWidget {
widget: self.widget.as_box_dyn(),
subwindow_id: self.subwindow_id,
id: self.id,
action_type: self.action_type,
#[cfg(debug_assertions)]
action_type_name: self.action_type_name,
options: self.options,
properties: self.properties,
property_stack_id: self.property_stack_id,
tag: self.tag,
classes: self.classes,
}
}
pub fn with_tag(mut self, tag: WidgetTag<W>) -> Self {
self.tag = Some(tag.inner);
self
}
pub fn with_erased_tag(mut self, tag: WidgetTag<dyn Widget>) -> Self {
self.tag = Some(tag.inner);
self
}
pub fn with_props(mut self, props: impl Into<PropertySet>) -> Self {
if self.properties.map.is_empty() {
self.properties = props.into();
} else {
let props = props.into();
self.properties
.map
.extend(props.map.into_raw().into_values());
}
self
}
pub fn with_transform(mut self, transform: Affine) -> Self {
self.options.transform = transform;
self
}
pub fn with_property_stack(mut self, id: PropertyStackId) -> Self {
self.property_stack_id = Some(id);
self
}
pub fn with_class(mut self, class: &str) -> Self {
self.classes.insert(class.to_string());
self
}
pub fn with_classes(mut self, classes: impl IntoIterator<Item = String>) -> Self {
self.classes.extend(classes);
self
}
pub fn disabled(mut self, disabled: bool) -> Self {
self.options.disabled = disabled;
self
}
pub fn to_pod(self) -> WidgetPod<W> {
WidgetPod {
id: self.id,
inner: WidgetPodInner::Create(Box::new(self)),
}
}
pub fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget> WidgetPod<W> {
pub fn new(inner: W) -> Self {
NewWidget::new(inner).to_pod()
}
}
impl<W: Widget + ?Sized> WidgetPod<W> {
pub(crate) fn incomplete(&self) -> bool {
matches!(self.inner, WidgetPodInner::Create(_))
}
pub(crate) fn take_inner(&mut self) -> Option<NewWidget<W>> {
match std::mem::replace(&mut self.inner, WidgetPodInner::Inserted) {
WidgetPodInner::Create(widget) => Some(*widget),
WidgetPodInner::Inserted => None,
}
}
pub fn id(&self) -> WidgetId {
self.id
}
}