iris: fold render state into Ui

This commit is contained in:
iris committed 2026-09-10 23:58:43 -04:00
1 parent 44a5da378b
commit 9b4c690916
25 files changed
+421 -390

No files matched your search

+84 -2
View File
@@ -1,6 +1,11 @@
use crate::{
Mask, MoveOffset, Paints, TextData, Textures, WeakWidget, WidgetId, Widgets, util::TrackedArena,
};
use std::{
cell::{Ref, RefCell, RefMut},
ops::{Deref, DerefMut},
rc::Rc,
};
mod access;
mod active;
@@ -23,6 +28,75 @@ pub struct UiData {
animating: Vec<WidgetId>,
}
#[derive(Clone)]
pub struct RenderHandle {
pub(crate) render_state: Rc<RefCell<UiRenderState>>,
}
impl RenderHandle {
/// The retained result of the last completed frame. The framework holds
/// the corresponding mutable borrow for the whole of a render update, so
/// a read attempted while that state is incomplete fails at the boundary
/// instead of observing half a frame.
pub fn get(&self) -> Ref<'_, UiRenderState> {
self.render_state
.try_borrow()
.expect("render state cannot be read while a frame is being rendered")
}
pub(crate) fn get_mut(&self) -> RefMut<'_, UiRenderState> {
self.render_state
.try_borrow_mut()
.expect("render state cannot be mutated while it is being read")
}
}
impl Default for RenderHandle {
fn default() -> Self {
Self {
render_state: Rc::new(RefCell::new(UiRenderState::new())),
}
}
}
#[derive(Default)]
pub struct Ui {
data: UiData,
pub(crate) render_state: RenderHandle,
}
impl Ui {
/// A read-only handle to the retained result of the last completed frame.
/// The handle is owned so a caller may keep its read guard while mutating
/// unrelated resources on the `Rsc` that owns this `Ui`.
pub fn render_state(&self) -> RenderHandle {
self.render_state.clone()
}
pub fn resize(&self, size: impl Into<crate::util::Vec2>) {
self.render_state.get_mut().resize(size);
}
pub fn set_density(&mut self, density: f32) {
self.data.text.density = density;
self.render_state.get_mut().set_density(density);
}
}
impl Deref for Ui {
type Target = UiData;
fn deref(&self) -> &Self::Target {
&self.data
}
}
impl DerefMut for Ui {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.data
}
}
impl UiData {
/// Ask for `id`'s [`crate::Widget::tick`] to run every frame until it
/// says it is done. Idempotent -- registering an already-animating
@@ -48,8 +122,16 @@ impl UiData {
}
pub trait UiRsc {
fn ui(&self) -> &UiData;
fn ui_mut(&mut self) -> &mut UiData;
fn ui(&self) -> &Ui;
fn ui_mut(&mut self) -> &mut Ui;
fn draw<'a>(&mut self, root: impl Into<Option<&'a crate::StrongWidget>>)
where
Self: Sized,
{
let render_state = self.ui().render_state.clone();
render_state.get_mut().update(root, self);
}
#[allow(unused_variables)]
fn on_add(&mut self, id: WeakWidget) {}