This commit is contained in:
2025-08-13 01:35:09 -04:00
parent 23a5ccd05e
commit c7e3225c5f
11 changed files with 247 additions and 142 deletions

View File

@@ -1,88 +1,115 @@
use crate::{
HashMap, Painter, Widget, WidgetId, WidgetLike, WidgetRef,
HashMap, Painter, Widget, WidgetId, WidgetLike,
primitive::Primitives,
util::{ID, IDTracker},
};
use std::{
any::{Any, TypeId},
cell::RefCell,
rc::Rc,
ops::{Index, IndexMut},
};
pub struct UI {
#[derive(Default)]
pub struct Ui {
ids: IDTracker,
base: Option<WidgetId>,
pub widgets: Widgets,
widgets: Widgets,
updates: Vec<WidgetId>,
primitives: Primitives,
full_redraw: bool,
}
#[derive(Default)]
pub struct Widgets(HashMap<ID, Box<dyn Widget>>);
#[derive(Clone)]
pub struct UIBuilder {
ui: Rc<RefCell<UI>>,
}
impl From<UI> for UIBuilder {
fn from(ui: UI) -> Self {
UIBuilder {
ui: Rc::new(RefCell::new(ui)),
}
}
}
impl UIBuilder {
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
WidgetRef::new([w.add(self).erase_type()])
impl Ui {
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetId<W> {
w.add(self)
}
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new([self.push(w)])
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
self.push(w)
}
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {
let mut ui = self.ui.borrow_mut();
let id = ui.ids.next();
ui.widgets.insert(id.duplicate(), w);
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.ids.next();
self.widgets.insert(id.duplicate(), w);
WidgetId::new(id, TypeId::of::<W>())
}
pub fn finish<W: WidgetLike>(mut self, base: W) -> UI {
let base = base.add(&mut self).erase_type();
let mut ui = Rc::into_inner(self.ui).unwrap().into_inner();
ui.base = Some(base);
ui
}
}
impl UI {
pub fn build() -> UIBuilder {
Self::empty().into()
pub fn set<W: Widget>(&mut self, i: &WidgetId<W>, w: W) {
self.widgets.insert(i.id.duplicate(), w);
}
pub fn empty() -> Self {
Self {
ids: IDTracker::new(),
base: None,
widgets: Widgets::new(),
}
pub fn set_base(&mut self, w: impl WidgetLike) {
self.base = Some(w.add(self).erase_type());
self.full_redraw = true;
}
pub fn to_primitives(&self) -> Primitives {
pub fn new() -> Self {
Self::default()
}
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.widgets.get(id)
}
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.widgets.get_mut(id)
}
pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
WidgetId::new(self.ids.next(), TypeId::of::<W>())
}
pub fn redraw_all(&mut self) {
let mut painter = Painter::new(&self.widgets);
if let Some(base) = &self.base {
painter.draw(base);
}
painter.finish()
self.primitives = painter.finish();
}
pub fn update(&mut self) -> Option<&Primitives> {
if self.full_redraw {
self.redraw_all();
self.full_redraw = false;
return Some(&self.primitives);
}
if self.updates.is_empty() {
return None;
}
self.redraw_all();
self.updates.drain(..);
Some(&self.primitives)
}
pub fn needs_redraw(&self) -> bool {
self.full_redraw || !self.updates.is_empty()
}
}
impl<W: Widget> Index<&WidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
self.get(id).unwrap()
}
}
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.clone().erase_type());
self.get_mut(id).unwrap()
}
}
impl Widgets {
fn new() -> Self {
Self(HashMap::new())
pub fn get_dyn(&self, id: &WidgetId) -> &dyn Widget {
self.0.get(&id.id).unwrap().as_ref()
}
pub fn get(&self, id: &WidgetId) -> &dyn Widget {
self.0.get(&id.id).unwrap().as_ref()
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.0.get(&id.id).unwrap().as_any().downcast_ref()
}
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
@@ -99,6 +126,10 @@ impl Widgets {
}
impl dyn Widget {
pub fn as_any(&self) -> &dyn Any {
self
}
pub fn as_any_mut(&mut self) -> &mut dyn Any {
self
}