remove modules and have single event manager (atomics feature parity + preparation for local state)
This commit is contained in:
26
core/src/widget/data.rs
Normal file
26
core/src/widget/data.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
use std::any::TypeId;
|
||||
|
||||
use crate::{Widget, util::HashSet};
|
||||
|
||||
pub struct WidgetData {
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub label: String,
|
||||
pub event_mgrs: HashSet<TypeId>,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
}
|
||||
|
||||
impl WidgetData {
|
||||
pub fn new<W: Widget>(widget: W) -> Self {
|
||||
let mut label = std::any::type_name::<W>().to_string();
|
||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
||||
}
|
||||
Self {
|
||||
widget: Box::new(widget),
|
||||
label,
|
||||
borrowed: false,
|
||||
event_mgrs: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -94,6 +94,13 @@ pub trait IdLike {
|
||||
fn id(&self) -> WidgetId;
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
@@ -108,6 +115,13 @@ impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
|
||||
}
|
||||
}
|
||||
|
||||
impl IdLike for WidgetId {
|
||||
type Widget = dyn Widget;
|
||||
fn id(&self) -> WidgetId {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
||||
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use crate::{Len, Painter, SizeCtx, Ui};
|
||||
use std::any::Any;
|
||||
|
||||
mod data;
|
||||
mod handle;
|
||||
mod like;
|
||||
mod tag;
|
||||
mod widgets;
|
||||
|
||||
pub use data::*;
|
||||
pub use handle::*;
|
||||
pub use like::*;
|
||||
pub use tag::*;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::{
|
||||
IdLike, Widget, WidgetId,
|
||||
IdLike, Widget, WidgetData, WidgetId,
|
||||
util::{DynBorrower, HashSet, SlotVec},
|
||||
};
|
||||
|
||||
@@ -9,13 +9,6 @@ pub struct Widgets {
|
||||
vec: SlotVec<WidgetData>,
|
||||
}
|
||||
|
||||
pub struct WidgetData {
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub label: String,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.updates.is_empty()
|
||||
@@ -60,35 +53,23 @@ impl Widgets {
|
||||
}
|
||||
|
||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
let mut label = std::any::type_name::<W>().to_string();
|
||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
||||
}
|
||||
self.insert_any(Box::new(widget), label)
|
||||
self.vec.add(WidgetData::new(widget))
|
||||
}
|
||||
|
||||
pub fn data(&self, id: WidgetId) -> Option<&WidgetData> {
|
||||
self.vec.get(id)
|
||||
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||
self.vec.get(id.id())
|
||||
}
|
||||
|
||||
pub fn label(&self, id: WidgetId) -> &String {
|
||||
&self.data(id).unwrap().label
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: WidgetId) -> Option<&mut WidgetData> {
|
||||
self.vec.get_mut(id)
|
||||
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||
self.vec.get_mut(id.id())
|
||||
}
|
||||
|
||||
pub fn insert_any(&mut self, widget: Box<dyn Widget>, label: String) -> WidgetId {
|
||||
self.vec.add(WidgetData {
|
||||
widget,
|
||||
label,
|
||||
borrowed: false,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: WidgetId) {
|
||||
self.vec.free(id);
|
||||
pub fn delete(&mut self, id: impl IdLike) {
|
||||
self.vec.free(id.id());
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user