IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)
This commit is contained in:
112
src/layout/ui.rs
112
src/layout/ui.rs
@@ -2,21 +2,21 @@ use image::DynamicImage;
|
||||
|
||||
use crate::{
|
||||
layout::{
|
||||
ActiveSensors, PainterCtx, SensorMap, StaticWidgetId, TextData, TextureHandle, Textures,
|
||||
Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
|
||||
ActiveSensors, PainterCtx, Sensor, SensorMap, StaticWidgetId, TextData, TextureHandle,
|
||||
Textures, Vec2, Widget, WidgetId, WidgetInstance, WidgetLike,
|
||||
},
|
||||
render::Primitives,
|
||||
util::{HashMap, Id, IdTracker},
|
||||
util::{DynBorrower, HashMap, HashSet, Id, IdTracker},
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
ops::{Deref, DerefMut, Index, IndexMut},
|
||||
ops::{Index, IndexMut},
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
pub struct Ui {
|
||||
pub struct Ui<Ctx> {
|
||||
root: Option<WidgetId>,
|
||||
widgets: Widgets,
|
||||
pub(super) widgets: Widgets,
|
||||
updates: Vec<Id>,
|
||||
recv: Receiver<Id>,
|
||||
pub(super) send: Sender<Id>,
|
||||
@@ -24,11 +24,11 @@ pub struct Ui {
|
||||
// TODO: make these non pub(crate)
|
||||
pub(crate) primitives: Primitives,
|
||||
pub(crate) textures: Textures,
|
||||
pub(crate) text: TextData,
|
||||
text: TextData,
|
||||
full_redraw: bool,
|
||||
|
||||
pub(super) active: Active,
|
||||
pub(super) sensor_map: SensorMap,
|
||||
pub(super) sensor_map: SensorMap<Ctx>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
@@ -38,19 +38,23 @@ pub struct Widgets {
|
||||
}
|
||||
|
||||
pub struct WidgetData {
|
||||
widget: Box<dyn Widget>,
|
||||
borrowed: bool,
|
||||
label: String,
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub label: String,
|
||||
pub sized_children: HashSet<Id>,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
/// whether this widget has any sensors
|
||||
pub sensor: bool,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
|
||||
impl<Ctx> Ui<Ctx> {
|
||||
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Ctx, Tag, Widget = W>) -> WidgetId<W> {
|
||||
w.add(self)
|
||||
}
|
||||
|
||||
pub fn add_static<W: Widget, Tag>(
|
||||
&mut self,
|
||||
w: impl WidgetLike<Tag, Widget = W>,
|
||||
w: impl WidgetLike<Ctx, Tag, Widget = W>,
|
||||
) -> StaticWidgetId<W> {
|
||||
let id = w.add(self);
|
||||
id.into_static()
|
||||
@@ -58,7 +62,7 @@ impl Ui {
|
||||
|
||||
/// useful for debugging
|
||||
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
|
||||
*self.widgets.label_mut(id).unwrap() = label;
|
||||
self.widgets.data_mut(&id.id).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
|
||||
@@ -75,8 +79,8 @@ impl Ui {
|
||||
self.widgets.insert(id.id.duplicate(), w);
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||
self.root = Some(w.add(self).erase_type());
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
|
||||
self.root = Some(w.add(self).any());
|
||||
self.full_redraw = true;
|
||||
}
|
||||
|
||||
@@ -121,7 +125,6 @@ impl Ui {
|
||||
let mut ctx = PainterCtx::new(
|
||||
&self.widgets,
|
||||
&mut self.primitives,
|
||||
&self.sensor_map,
|
||||
&mut self.active,
|
||||
&mut self.textures,
|
||||
&mut self.text,
|
||||
@@ -145,7 +148,6 @@ impl Ui {
|
||||
let mut ctx = PainterCtx::new(
|
||||
&self.widgets,
|
||||
&mut self.primitives,
|
||||
&self.sensor_map,
|
||||
&mut self.active,
|
||||
&mut self.textures,
|
||||
&mut self.text,
|
||||
@@ -160,6 +162,7 @@ impl Ui {
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self) {
|
||||
for id in self.recv.try_iter() {
|
||||
self.sensor_map.remove(&id);
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
@@ -176,9 +179,18 @@ impl Ui {
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.active.widgets.len()
|
||||
}
|
||||
|
||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor<Ctx>) {
|
||||
self.sensor_map
|
||||
.entry(id.id.duplicate())
|
||||
.or_default()
|
||||
.sensors
|
||||
.push(f);
|
||||
self.widgets.data_mut(&id.id).unwrap().sensor = true;
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
||||
impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
||||
type Output = W;
|
||||
|
||||
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
|
||||
@@ -186,14 +198,14 @@ impl<W: Widget> Index<&WidgetId<W>> for Ui {
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
|
||||
impl<W: Widget, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
|
||||
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
|
||||
self.updates.push(id.id.duplicate());
|
||||
self.get_mut(id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
|
||||
impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
|
||||
type Output = W;
|
||||
|
||||
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
|
||||
@@ -201,7 +213,7 @@ impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
|
||||
impl<W: Widget, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
|
||||
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
|
||||
self.updates.push(id.id.id());
|
||||
self.widgets.get_static_mut(&id).unwrap()
|
||||
@@ -233,10 +245,7 @@ impl Widgets {
|
||||
if data.borrowed {
|
||||
panic!("tried to mutably borrow the same widget twice");
|
||||
}
|
||||
WidgetWrapper {
|
||||
widget: data.widget.as_mut(),
|
||||
borrowed: &mut data.borrowed,
|
||||
}
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get_static<W: Widget>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
|
||||
@@ -259,12 +268,12 @@ impl Widgets {
|
||||
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string());
|
||||
}
|
||||
|
||||
pub fn label<W>(&self, id: &WidgetId<W>) -> Option<&String> {
|
||||
self.map.get(&id.id).map(|d| &d.label)
|
||||
pub fn data(&self, id: &Id) -> Option<&WidgetData> {
|
||||
self.map.get(id)
|
||||
}
|
||||
|
||||
pub fn label_mut<W>(&mut self, id: &WidgetId<W>) -> Option<&mut String> {
|
||||
self.map.get_mut(&id.id).map(|d| &mut d.label)
|
||||
pub fn data_mut(&mut self, id: &Id) -> Option<&mut WidgetData> {
|
||||
self.map.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>, label: String) {
|
||||
@@ -272,8 +281,10 @@ impl Widgets {
|
||||
id,
|
||||
WidgetData {
|
||||
widget,
|
||||
borrowed: false,
|
||||
label,
|
||||
sized_children: HashSet::new(),
|
||||
borrowed: false,
|
||||
sensor: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -296,36 +307,7 @@ impl Widgets {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WidgetWrapper<'a> {
|
||||
widget: &'a mut dyn Widget,
|
||||
borrowed: &'a mut bool,
|
||||
}
|
||||
|
||||
impl<'a> WidgetWrapper<'a> {
|
||||
pub fn new(widget: &'a mut dyn Widget, borrowed: &'a mut bool) -> Self {
|
||||
Self { widget, borrowed }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WidgetWrapper<'_> {
|
||||
fn drop(&mut self) {
|
||||
*self.borrowed = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for WidgetWrapper<'_> {
|
||||
type Target = dyn Widget;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.widget
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for WidgetWrapper<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.widget
|
||||
}
|
||||
}
|
||||
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
|
||||
|
||||
impl dyn Widget {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
@@ -337,7 +319,7 @@ impl dyn Widget {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Ui {
|
||||
impl<Ctx> Default for Ui<Ctx> {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
@@ -377,8 +359,8 @@ impl Active {
|
||||
instance
|
||||
}
|
||||
|
||||
pub fn add(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap) {
|
||||
if sensors_map.get(id).is_some() {
|
||||
pub fn add(&mut self, id: &Id, instance: WidgetInstance, widgets: &Widgets) {
|
||||
if widgets.data(id).is_some_and(|w| w.sensor) {
|
||||
self.sensors.insert(id.duplicate(), instance.region);
|
||||
}
|
||||
self.widgets.insert(id.duplicate(), instance);
|
||||
|
||||
Reference in New Issue
Block a user