REACTIVITY

This commit is contained in:
2025-08-24 20:34:19 -04:00
parent 50ccf7393d
commit 6bb6db32a6
7 changed files with 305 additions and 59 deletions

View File

@@ -2,52 +2,67 @@ use image::GenericImageView;
use crate::{
layout::{
ActiveSensors, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiPos, UiRegion,
Vec2, WidgetId, Widgets,
Active, SensorMap, TextAttrs, TextData, TextureHandle, Textures, UiRegion, Vec2, WidgetId,
WidgetInstance, Widgets,
},
render::{Primitive, Primitives},
render::{Primitive, PrimitiveHandle, Primitives},
util::Id,
};
struct State {
region: UiRegion,
children: Vec<Id>,
parent: Option<Id>,
// TODO: there's probably a better way but idc at this point
id: Option<Id>,
}
pub struct Painter<'a, Ctx: 'static> {
nodes: &'a Widgets<Ctx>,
widgets: &'a Widgets<Ctx>,
ctx: &'a mut Ctx,
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
primitives: &'a mut Primitives,
pub(super) active: &'a mut Active,
pub(super) primitives: &'a mut Primitives,
textures: &'a mut Textures,
text: &'a mut TextData,
region: UiRegion,
screen_size: Vec2,
/// state of what's currently being drawn
state: State,
}
impl<'a, Ctx> Painter<'a, Ctx> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
pub(super) fn new(
nodes: &'a Widgets<Ctx>,
primitives: &'a mut Primitives,
ctx: &'a mut Ctx,
sensors_map: &'a SensorMap<Ctx>,
active_sensors: &'a mut ActiveSensors,
active: &'a mut Active,
text: &'a mut TextData,
textures: &'a mut Textures,
screen_size: Vec2,
) -> Self {
Self {
nodes,
widgets: nodes,
ctx,
active_sensors,
active,
sensors_map,
primitives,
text,
region: UiRegion::full(),
textures,
screen_size,
state: State {
region: UiRegion::full(),
children: Vec::new(),
parent: None,
id: None,
},
}
}
/// Writes a primitive to be rendered
pub fn write<P: Primitive>(&mut self, data: P) {
self.primitives.write(data, self.region);
pub fn write<P: Primitive>(&mut self, data: P) -> PrimitiveHandle<P> {
self.primitives.write(data, self.state.region)
}
/// Draws a widget within this widget's region.
@@ -55,7 +70,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
where
Ctx: 'static,
{
self.draw_at(id, self.region);
self.draw_at(id, self.state.region);
}
/// Draws a widget somewhere within this one.
@@ -64,7 +79,7 @@ impl<'a, Ctx> Painter<'a, Ctx> {
where
Ctx: 'static,
{
self.draw_at(id, region.within(&self.region));
self.draw_at(id, region.within(&self.state.region));
}
/// Draws a widget in an arbitrary region.
@@ -72,15 +87,48 @@ impl<'a, Ctx> Painter<'a, Ctx> {
where
Ctx: 'static,
{
if self.sensors_map.get(&id.id).is_some() {
self.active_sensors.push((region, id.id.duplicate()));
self.draw_raw_at(&id.id, region);
}
fn draw_raw_at(&mut self, id: &Id, region: UiRegion)
where
Ctx: 'static,
{
if self.active.widgets.contains_key(id) {
panic!("widget drawn twice!");
}
self.state.children.push(id.duplicate());
// &mut self is passed to avoid copying all of the "static" pointers in self
// so need to save non static data here
let old = self.region;
self.region = region;
self.nodes.get_dyn(id).draw(self);
self.region = old;
let child_state = State {
region,
children: Vec::new(),
id: Some(id.duplicate()),
parent: self.state.id.as_ref().map(|i| i.duplicate()),
};
// save state
let self_state = std::mem::replace(&mut self.state, child_state);
// draw widgets
let start_i = self.primitives.cur_pos();
self.widgets.get_dyn(id).draw(self);
let end_i = self.primitives.cur_pos();
// restore state
let child_state = std::mem::replace(&mut self.state, self_state);
// add to active
self.active.add(
id,
WidgetInstance {
region,
primitives: (start_i..end_i).into(),
children: child_state.children,
parent: child_state.parent,
},
self.sensors_map,
);
}
pub fn draw_texture(&mut self, handle: &TextureHandle) {
@@ -89,16 +137,16 @@ impl<'a, Ctx> Painter<'a, Ctx> {
}
pub fn draw_texture_at(&mut self, handle: &TextureHandle, region: UiRegion) {
let old = self.region;
self.region = region;
let old = self.state.region;
self.state.region = region;
self.draw_texture(handle);
self.region = old;
self.state.region = old;
}
pub fn draw_text(&mut self, content: &str, attrs: &TextAttrs) {
let handle = self.text.draw(content, attrs, self.textures);
let dims: Vec2 = self.textures[&handle].dimensions().into();
let mut region = self.region.center().expand(dims);
let mut region = self.state.region.center().expand(dims);
// TODO: I feel like this shouldn't be needed
// what this does is makes sure the text doesn't get squeezed into one pixel less
// I'm unsure exactly why it happens or if this will ever expand it too much
@@ -107,10 +155,42 @@ impl<'a, Ctx> Painter<'a, Ctx> {
}
pub fn region(&self) -> UiRegion {
self.region
self.state.region
}
pub fn ctx(&mut self) -> &mut Ctx {
self.ctx
}
pub(crate) fn redraw(&mut self, id: &Id) {
if !self.active.widgets.contains_key(id) {
return;
}
let instance = self.free(id);
self.state.id = instance.parent;
self.primitives.prepare(instance.primitives.into());
self.draw_raw_at(id, instance.region);
let delta = self.primitives.apply(instance.primitives.into());
if let Some(parent) = self.state.id.take() {
self.shift_end(parent, delta);
}
}
fn shift_end(&mut self, id: Id, delta: isize) {
let instance = self.active.widgets.get_mut(&id).unwrap();
let end = &mut instance.primitives.end;
*end = end.strict_add_signed(delta);
if let Some(parent) = &instance.parent {
let parent = parent.duplicate();
self.shift_end(parent, delta);
}
}
fn free(&mut self, id: &Id) -> WidgetInstance {
let instance = self.active.remove(id).unwrap();
for child in &instance.children {
self.free(child);
}
instance
}
}

View File

@@ -1,6 +1,6 @@
use crate::{
HashMap,
layout::{Ui, UiRegion, Vec2, WidgetId},
util::HashMap,
util::Id,
};
@@ -36,7 +36,7 @@ pub struct Sensor<Ctx> {
}
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
pub type ActiveSensors = Vec<(SenseShape, Id)>;
pub type ActiveSensors = HashMap<Id, SenseShape>;
pub type SenseShape = UiRegion;
#[derive(Clone)]
pub struct SenseTrigger {
@@ -74,9 +74,9 @@ impl<Ctx> Ui<Ctx> {
where
Ctx: 'static,
{
let mut active = std::mem::take(&mut self.active_sensors);
let active = std::mem::take(&mut self.active.sensors);
let mut map = std::mem::take(&mut self.sensor_map);
for (shape, id) in active.iter_mut().rev() {
for (id, shape) in active.iter() {
let group = &mut map.get_mut(id).unwrap();
let region = shape.to_screen(window_size);
let in_shape = cursor.exists && region.contains(cursor.pos);
@@ -90,7 +90,7 @@ impl<Ctx> Ui<Ctx> {
}
}
self.sensor_map = map;
self.active_sensors = active;
self.active.sensors = active;
}
}

View File

@@ -2,8 +2,8 @@ use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping, SwashCache};
use image::{Rgba, RgbaImage};
use crate::{
HashMap,
layout::{TextureHandle, Textures, UiColor},
util::HashMap,
};
pub(crate) struct TextData {

View File

@@ -1,17 +1,17 @@
use image::DynamicImage;
use crate::{
HashMap,
layout::{
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, Vec2, Widget,
WidgetId, WidgetLike,
ActiveSensors, Painter, SensorMap, TextData, TextureHandle, Textures, UiRegion, Vec2,
Widget, WidgetId, WidgetLike,
},
render::Primitives,
util::{Id, IdTracker},
util::{HashMap, Id, IdTracker},
};
use std::{
any::{Any, TypeId},
ops::{Index, IndexMut},
range::Range,
sync::mpsc::{Receiver, Sender, channel},
};
@@ -28,7 +28,7 @@ pub struct Ui<Ctx> {
pub(crate) text: TextData,
full_redraw: bool,
pub(super) active_sensors: ActiveSensors,
pub(super) active: Active,
pub(super) sensor_map: SensorMap<Ctx>,
}
@@ -96,7 +96,7 @@ impl<Ctx> Ui<Ctx> {
where
Ctx: 'static,
{
self.active_sensors.clear();
self.active.clear();
self.primitives.clear();
self.free();
let mut painter = Painter::new(
@@ -104,7 +104,7 @@ impl<Ctx> Ui<Ctx> {
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active_sensors,
&mut self.active,
&mut self.text,
&mut self.textures,
self.size,
@@ -112,6 +112,7 @@ impl<Ctx> Ui<Ctx> {
if let Some(base) = &self.base {
painter.draw(base);
}
self.primitives.replace();
}
pub fn update(&mut self, ctx: &mut Ctx)
@@ -122,9 +123,26 @@ impl<Ctx> Ui<Ctx> {
self.redraw_all(ctx);
self.full_redraw = false;
} else if !self.updates.is_empty() {
// TODO: partial updates
self.redraw_all(ctx);
self.updates.drain(..);
self.redraw_updates(ctx);
}
}
fn redraw_updates(&mut self, ctx: &mut Ctx)
where
Ctx: 'static,
{
let mut painter = Painter::new(
&self.widgets,
&mut self.primitives,
ctx,
&self.sensor_map,
&mut self.active,
&mut self.text,
&mut self.textures,
self.size,
);
for id in self.updates.drain(..) {
painter.redraw(&id.id);
}
}
@@ -168,8 +186,8 @@ impl<Ctx> Widgets<Ctx> {
}
}
pub fn get_dyn<W>(&self, id: &WidgetId<W>) -> &dyn Widget<Ctx> {
self.map.get(&id.id).unwrap().as_ref()
pub fn get_dyn(&self, id: &Id) -> &dyn Widget<Ctx> {
self.map.get(id).unwrap().as_ref()
}
pub fn get<W: Widget<Ctx>>(&self, id: &WidgetId<W>) -> Option<&W> {
@@ -231,7 +249,7 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
textures: Textures::new(),
text: TextData::default(),
full_redraw: false,
active_sensors: Default::default(),
active: Default::default(),
sensor_map: Default::default(),
send,
recv,
@@ -239,3 +257,37 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
}
}
}
pub struct WidgetInstance {
pub region: UiRegion,
pub primitives: Range<usize>,
pub children: Vec<Id>,
pub parent: Option<Id>,
}
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;
#[derive(Default)]
pub(super) struct Active {
pub sensors: ActiveSensors,
pub widgets: ActiveWidgets,
}
impl Active {
pub fn clear(&mut self) {
self.sensors.clear();
self.widgets.clear();
}
pub fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
let instance = self.widgets.remove(id);
self.sensors.remove(id);
instance
}
pub fn add<Ctx>(&mut self, id: &Id, instance: WidgetInstance, sensors_map: &SensorMap<Ctx>) {
if sensors_map.get(id).is_some() {
self.sensors.insert(id.duplicate(), instance.region);
}
self.widgets.insert(id.duplicate(), instance);
}
}