work
This commit is contained in:
@@ -1,22 +1,34 @@
|
||||
use crate::{
|
||||
ActiveData, Axis, EventsLike, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
|
||||
ActiveData, Axis, EventsLike, Painter, PainterData, SizeCtx, StrongWidget, UiRegion,
|
||||
UiRenderState, UiVec2, WidgetId, Widgets,
|
||||
render::MaskIdx,
|
||||
util::{HashSet, forget_ref},
|
||||
};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
/// state maintained between widgets during painting
|
||||
pub struct DrawState<'a> {
|
||||
pub(super) ui: &'a mut Ui,
|
||||
pub struct Drawer<'a> {
|
||||
pub(super) widgets: &'a mut Widgets,
|
||||
pub(super) data: &'a mut PainterData,
|
||||
pub(super) events: &'a mut dyn EventsLike,
|
||||
pub(super) render: &'a mut UiRenderState,
|
||||
root: Option<&'a StrongWidget>,
|
||||
draw_started: HashSet<WidgetId>,
|
||||
}
|
||||
|
||||
impl<'a> DrawState<'a> {
|
||||
pub fn new(ui: &'a mut Ui, events: &'a mut dyn EventsLike) -> Self {
|
||||
impl<'a> Drawer<'a> {
|
||||
pub fn new(
|
||||
widgets: &'a mut Widgets,
|
||||
data: &'a mut PainterData,
|
||||
render: &'a mut UiRenderState,
|
||||
events: &'a mut dyn EventsLike,
|
||||
root: Option<&'a StrongWidget>,
|
||||
) -> Self {
|
||||
Self {
|
||||
ui,
|
||||
widgets,
|
||||
data,
|
||||
events,
|
||||
render,
|
||||
root,
|
||||
draw_started: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -34,13 +46,17 @@ impl<'a> DrawState<'a> {
|
||||
self.draw_started.remove(&id);
|
||||
// check if parent depends on the desired size of this, if so then redraw it first
|
||||
for axis in [Axis::X, Axis::Y] {
|
||||
if let Some(&(outer, old)) = self.cache.size.axis_dyn(axis).get(&id)
|
||||
&& let Some(current) = self.active.get(&id)
|
||||
if let Some(&(outer, old)) = self.render.cache.size.axis_dyn(axis).get(&id)
|
||||
&& let Some(current) = self.render.active.get(&id)
|
||||
&& let Some(pid) = current.parent
|
||||
{
|
||||
self.cache.size.axis_dyn(axis).remove(&id);
|
||||
self.render.cache.size.axis_dyn(axis).remove(&id);
|
||||
let new = self.size_ctx(id, outer).len_axis(id, axis);
|
||||
self.cache.size.axis_dyn(axis).insert(id, (outer, new));
|
||||
self.render
|
||||
.cache
|
||||
.size
|
||||
.axis_dyn(axis)
|
||||
.insert(id, (outer, new));
|
||||
if new != old {
|
||||
self.redraw(pid);
|
||||
}
|
||||
@@ -68,27 +84,27 @@ impl<'a> DrawState<'a> {
|
||||
pub(super) fn size_ctx<'b>(&'b mut self, source: WidgetId, outer: UiVec2) -> SizeCtx<'b> {
|
||||
SizeCtx {
|
||||
source,
|
||||
cache: &mut self.ui.cache,
|
||||
text: &mut self.ui.text,
|
||||
textures: &mut self.ui.textures,
|
||||
widgets: &self.ui.widgets,
|
||||
cache: &mut self.render.cache,
|
||||
text: &mut self.data.text,
|
||||
textures: &mut self.data.textures,
|
||||
widgets: &self.widgets,
|
||||
outer,
|
||||
output_size: self.ui.output_size,
|
||||
output_size: self.render.output_size,
|
||||
id: source,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn redraw_all(&mut self) {
|
||||
// free all resources & cache
|
||||
for (_, active) in self.ui.active.drain() {
|
||||
for (_, active) in self.render.active.drain() {
|
||||
self.events.undraw(&active);
|
||||
}
|
||||
self.ui.cache.clear();
|
||||
self.render.cache.clear();
|
||||
self.ui.free(self.events);
|
||||
self.layers.clear();
|
||||
self.render.layers.clear();
|
||||
self.widgets.needs_redraw.clear();
|
||||
|
||||
if let Some(id) = &self.ui.root {
|
||||
if let Some(id) = self.root {
|
||||
self.draw_inner(0, id.id(), UiRegion::FULL, None, MaskIdx::NONE, None);
|
||||
}
|
||||
}
|
||||
@@ -103,8 +119,8 @@ impl<'a> DrawState<'a> {
|
||||
old_children: Option<Vec<WidgetId>>,
|
||||
) {
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
if let Some(active) = self.ui.active.get_mut(&id)
|
||||
&& !self.ui.widgets.needs_redraw.contains(&id)
|
||||
if let Some(active) = self.render.active.get_mut(&id)
|
||||
&& !self.widgets.needs_redraw.contains(&id)
|
||||
{
|
||||
// check to see if we can skip drawing first
|
||||
if active.region == region {
|
||||
@@ -124,7 +140,7 @@ impl<'a> DrawState<'a> {
|
||||
self.draw_started.insert(id);
|
||||
|
||||
let mut painter = Painter {
|
||||
state: self,
|
||||
drawer: self,
|
||||
region,
|
||||
mask,
|
||||
layer,
|
||||
@@ -134,12 +150,12 @@ impl<'a> DrawState<'a> {
|
||||
children: Vec::new(),
|
||||
};
|
||||
|
||||
let mut widget = painter.state.widgets.get_dyn_dynamic(id);
|
||||
let mut widget = painter.drawer.widgets.get_dyn_dynamic(id);
|
||||
widget.draw(&mut painter);
|
||||
drop(widget);
|
||||
|
||||
let Painter {
|
||||
state: _,
|
||||
drawer: _,
|
||||
region,
|
||||
mask,
|
||||
textures,
|
||||
@@ -170,13 +186,13 @@ impl<'a> DrawState<'a> {
|
||||
|
||||
// update modules
|
||||
self.events.draw(&active);
|
||||
self.active.insert(id, active);
|
||||
self.render.active.insert(id, active);
|
||||
}
|
||||
|
||||
fn mov(&mut self, id: WidgetId, from: UiRegion, to: UiRegion) {
|
||||
let active = self.ui.active.get_mut(&id).unwrap();
|
||||
let active = self.render.active.get_mut(&id).unwrap();
|
||||
for h in &active.primitives {
|
||||
let region = self.ui.layers[h.layer].region_mut(h);
|
||||
let region = self.render.layers[h.layer].region_mut(h);
|
||||
*region = region.outside(&from).within(&to);
|
||||
}
|
||||
active.region = active.region.outside(&from).within(&to);
|
||||
@@ -189,16 +205,16 @@ impl<'a> DrawState<'a> {
|
||||
|
||||
/// NOTE: instance textures are cleared and self.textures freed
|
||||
fn remove(&mut self, id: WidgetId, undraw: bool) -> Option<ActiveData> {
|
||||
let mut active = self.active.remove(&id);
|
||||
let mut active = self.render.active.remove(&id);
|
||||
if let Some(active) = &mut active {
|
||||
for h in &active.primitives {
|
||||
let mask = self.layers.free(h);
|
||||
let mask = self.render.layers.free(h);
|
||||
if mask != MaskIdx::NONE {
|
||||
self.masks.remove(mask);
|
||||
self.data.masks.remove(mask);
|
||||
}
|
||||
}
|
||||
active.textures.clear();
|
||||
self.textures.free();
|
||||
self.data.textures.free();
|
||||
if undraw {
|
||||
self.events.undraw(active);
|
||||
}
|
||||
@@ -207,7 +223,7 @@ impl<'a> DrawState<'a> {
|
||||
}
|
||||
|
||||
fn remove_rec(&mut self, id: WidgetId) -> Option<ActiveData> {
|
||||
self.cache.remove(id);
|
||||
self.render.cache.remove(id);
|
||||
let inst = self.remove(id, true);
|
||||
if let Some(inst) = &inst {
|
||||
for c in &inst.children {
|
||||
@@ -217,16 +233,3 @@ impl<'a> DrawState<'a> {
|
||||
inst
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for DrawState<'_> {
|
||||
type Target = Ui;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.ui
|
||||
}
|
||||
}
|
||||
impl DerefMut for DrawState<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.ui
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,211 +1,22 @@
|
||||
use crate::{
|
||||
EventsLike, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData, TextureHandle, Textures,
|
||||
Widget, WidgetHandle, WidgetId, Widgets,
|
||||
ui::draw_state::DrawState,
|
||||
util::{HashMap, TrackedArena, Vec2},
|
||||
};
|
||||
use image::DynamicImage;
|
||||
use std::{
|
||||
ops::{Index, IndexMut},
|
||||
sync::mpsc::{Receiver, channel},
|
||||
};
|
||||
use crate::{WeakWidget, Widget, Widgets};
|
||||
|
||||
mod active;
|
||||
mod cache;
|
||||
mod draw_state;
|
||||
mod painter;
|
||||
mod render_state;
|
||||
mod size;
|
||||
mod state;
|
||||
|
||||
pub use active::*;
|
||||
use cache::*;
|
||||
pub use painter::Painter;
|
||||
pub use render_state::*;
|
||||
pub use size::*;
|
||||
|
||||
pub struct Ui {
|
||||
// TODO: edit visibilities
|
||||
pub widgets: Widgets,
|
||||
pub struct Ui {}
|
||||
|
||||
// retained painter state
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
pub layers: PrimitiveLayers,
|
||||
pub textures: Textures,
|
||||
pub text: TextData,
|
||||
output_size: Vec2,
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
pub cache: Cache,
|
||||
|
||||
pub root: Option<WidgetHandle>,
|
||||
old_root: Option<WidgetId>,
|
||||
recv: Receiver<WidgetId>,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
pub trait HasUi: Sized {
|
||||
fn ui(&self) -> &Ui;
|
||||
fn ui_mut(&mut self) -> &mut Ui;
|
||||
}
|
||||
|
||||
impl HasUi for Ui {
|
||||
fn ui(&self) -> &Ui {
|
||||
self
|
||||
}
|
||||
|
||||
fn ui_mut(&mut self) -> &mut Ui {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
/// useful for debugging
|
||||
pub fn set_label(&mut self, id: impl IdLike, label: String) {
|
||||
self.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.widgets.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized + Widget,
|
||||
{
|
||||
self.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized + Widget,
|
||||
{
|
||||
self.widgets.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
|
||||
self.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn update(&mut self, events: &mut dyn EventsLike) {
|
||||
if !self.widgets.waiting.is_empty() {
|
||||
let len = self.widgets.waiting.len();
|
||||
let all: Vec<_> = self
|
||||
.widgets
|
||||
.waiting
|
||||
.iter()
|
||||
.map(|&w| format!("'{}' ({w:?})", self.label(w)))
|
||||
.collect();
|
||||
panic!(
|
||||
"{len} widget(s) were never upgraded\n\
|
||||
this is likely a memory leak; consider upgrading to strong if you plan on using it later\n\
|
||||
weak widgets: {all:#?}"
|
||||
);
|
||||
}
|
||||
if self.root_changed() {
|
||||
DrawState::new(self, events).redraw_all();
|
||||
self.old_root = self.root.as_ref().map(|r| r.id());
|
||||
} else if self.widgets.has_updates() {
|
||||
DrawState::new(self, events).redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
self.resized = false;
|
||||
DrawState::new(self, events).redraw_all();
|
||||
}
|
||||
}
|
||||
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self, events: &mut dyn EventsLike) {
|
||||
for id in self.recv.try_iter() {
|
||||
events.remove(id);
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
}
|
||||
|
||||
pub fn root_changed(&self) -> bool {
|
||||
self.root.as_ref().map(|r| r.id()) != self.old_root
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
self.root_changed() || self.widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
self.widgets.len()
|
||||
}
|
||||
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.active.len()
|
||||
}
|
||||
|
||||
pub fn debug_layers(&self) {
|
||||
for ((idx, depth), primitives) in self.layers.iter_depth() {
|
||||
let indent = " ".repeat(depth * 2);
|
||||
let len = primitives.instances().len();
|
||||
print!("{indent}{idx}: {len} primitives");
|
||||
if len >= 1 {
|
||||
print!(" ({})", primitives.instances()[0].binding);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let region = self.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.output_size))
|
||||
}
|
||||
|
||||
pub fn debug(&self, label: &str) -> impl Iterator<Item = &ActiveData> {
|
||||
self.active.iter().filter_map(move |(&id, inst)| {
|
||||
let l = self.widgets.label(id);
|
||||
if l == label { Some(inst) } else { None }
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> Index<I> for Ui
|
||||
where
|
||||
I::Widget: Sized + Widget,
|
||||
{
|
||||
type Output = I::Widget;
|
||||
|
||||
fn index(&self, id: I) -> &Self::Output {
|
||||
self.get(&id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> IndexMut<I> for Ui
|
||||
where
|
||||
I::Widget: Sized + Widget,
|
||||
{
|
||||
fn index_mut(&mut self, id: I) -> &mut Self::Output {
|
||||
self.get_mut(&id).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Ui {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
widgets: Widgets::new(send),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
masks: Default::default(),
|
||||
text: Default::default(),
|
||||
textures: Default::default(),
|
||||
cache: Default::default(),
|
||||
output_size: Vec2::ZERO,
|
||||
root: None,
|
||||
old_root: None,
|
||||
recv,
|
||||
resized: false,
|
||||
}
|
||||
}
|
||||
pub trait UiRsc: Sized {
|
||||
fn add_widget<W: Widget>(&mut self, widget: W) -> WeakWidget<W>;
|
||||
fn widgets(&self) -> &Widgets;
|
||||
fn widgets_mut(&mut self) -> &mut Widgets;
|
||||
}
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
use crate::{
|
||||
Axis, Len, RenderedText, Size, SizeCtx, TextAttrs, TextBuffer, TextData, TextureHandle,
|
||||
UiRegion, Widget, WidgetHandle, WidgetId,
|
||||
Axis, Len, RenderedText, Size, SizeCtx, StrongWidget, TextAttrs, TextBuffer, TextData,
|
||||
TextureHandle, UiRegion, Widget, WidgetId,
|
||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||
ui::draw_state::DrawState,
|
||||
ui::draw_state::Drawer,
|
||||
util::Vec2,
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'b> {
|
||||
pub(super) state: &'a mut DrawState<'b>,
|
||||
pub(super) drawer: &'a mut Drawer<'b>,
|
||||
|
||||
pub(super) region: UiRegion,
|
||||
pub(super) mask: MaskIdx,
|
||||
@@ -21,7 +21,7 @@ pub struct Painter<'a, 'b> {
|
||||
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.state.layers.write(
|
||||
let h = self.drawer.layers.write(
|
||||
self.layer,
|
||||
PrimitiveInst {
|
||||
id: self.id,
|
||||
@@ -32,7 +32,7 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
);
|
||||
if self.mask != MaskIdx::NONE {
|
||||
// TODO: I have no clue if this works at all :joy:
|
||||
self.state.masks.push_ref(self.mask);
|
||||
self.drawer.masks.push_ref(self.mask);
|
||||
}
|
||||
self.primitives.push(h);
|
||||
}
|
||||
@@ -48,23 +48,23 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
|
||||
pub fn set_mask(&mut self, region: UiRegion) {
|
||||
assert!(self.mask == MaskIdx::NONE);
|
||||
self.mask = self.state.masks.push(Mask { region });
|
||||
self.mask = self.drawer.masks.push(Mask { region });
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<W>) {
|
||||
pub fn widget<W: ?Sized>(&mut self, id: &StrongWidget<W>) {
|
||||
self.widget_at(id, self.region);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one.
|
||||
/// Useful for drawing child widgets in select areas.
|
||||
pub fn widget_within<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
|
||||
pub fn widget_within<W: ?Sized>(&mut self, id: &StrongWidget<W>, region: UiRegion) {
|
||||
self.widget_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &StrongWidget<W>, region: UiRegion) {
|
||||
self.children.push(id.id());
|
||||
self.state
|
||||
self.drawer
|
||||
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
|
||||
}
|
||||
|
||||
@@ -85,21 +85,21 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
|
||||
/// returns (handle, offset from top left)
|
||||
pub fn render_text(&mut self, buffer: &mut TextBuffer, attrs: &TextAttrs) -> RenderedText {
|
||||
self.state
|
||||
self.drawer
|
||||
.ui
|
||||
.text
|
||||
.draw(buffer, attrs, &mut self.state.ui.textures)
|
||||
.draw(buffer, attrs, &mut self.drawer.ui.textures)
|
||||
}
|
||||
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.region
|
||||
}
|
||||
|
||||
pub fn size<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>) -> Size {
|
||||
pub fn size<W: ?Sized + Widget>(&mut self, id: &StrongWidget<W>) -> Size {
|
||||
self.size_ctx().size(id)
|
||||
}
|
||||
|
||||
pub fn len_axis<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>, axis: Axis) -> Len {
|
||||
pub fn len_axis<W: ?Sized + Widget>(&mut self, id: &StrongWidget<W>, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.size_ctx().width(id),
|
||||
Axis::Y => self.size_ctx().height(id),
|
||||
@@ -107,27 +107,27 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
pub fn output_size(&self) -> Vec2 {
|
||||
self.state.output_size
|
||||
self.drawer.output_size
|
||||
}
|
||||
|
||||
pub fn px_size(&mut self) -> Vec2 {
|
||||
self.region.size().to_abs(self.state.output_size)
|
||||
self.region.size().to_abs(self.drawer.output_size)
|
||||
}
|
||||
|
||||
pub fn text_data(&mut self) -> &mut TextData {
|
||||
&mut self.state.text
|
||||
&mut self.drawer.text
|
||||
}
|
||||
|
||||
pub fn child_layer(&mut self) {
|
||||
self.layer = self.state.layers.child(self.layer);
|
||||
self.layer = self.drawer.layers.child(self.layer);
|
||||
}
|
||||
|
||||
pub fn next_layer(&mut self) {
|
||||
self.layer = self.state.layers.next(self.layer);
|
||||
self.layer = self.drawer.layers.next(self.layer);
|
||||
}
|
||||
|
||||
pub fn label(&self) -> &str {
|
||||
&self.state.widgets.data(self.id).unwrap().label
|
||||
&self.drawer.widgets.data(self.id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn id(&self) -> &WidgetId {
|
||||
@@ -135,6 +135,6 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||
self.state.size_ctx(self.id, self.region.size())
|
||||
self.drawer.size_ctx(self.id, self.region.size())
|
||||
}
|
||||
}
|
||||
|
||||
116
core/src/ui/render_state.rs
Normal file
116
core/src/ui/render_state.rs
Normal file
@@ -0,0 +1,116 @@
|
||||
use crate::{
|
||||
ActiveData, EventsLike, IdLike, PixelRegion, PrimitiveLayers, StrongWidget, WidgetId, Widgets,
|
||||
ui::{cache::Cache, draw_state::Drawer},
|
||||
util::{HashMap, Vec2},
|
||||
};
|
||||
|
||||
pub struct UiRenderState {
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
pub layers: PrimitiveLayers,
|
||||
pub(super) output_size: Vec2,
|
||||
pub cache: Cache,
|
||||
|
||||
old_root: Option<WidgetId>,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
impl UiRenderState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
cache: Default::default(),
|
||||
output_size: Vec2::ZERO,
|
||||
old_root: None,
|
||||
resized: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn update<'a>(
|
||||
&mut self,
|
||||
root: impl Into<Option<&'a StrongWidget>>,
|
||||
widgets: &mut Widgets,
|
||||
events: &mut dyn EventsLike,
|
||||
) {
|
||||
// safety mechanism for memory leaks; might wanna return a result instead so user can
|
||||
// decide whether to panic or not
|
||||
if !widgets.waiting.is_empty() {
|
||||
let len = widgets.waiting.len();
|
||||
let all: Vec<_> = widgets
|
||||
.waiting
|
||||
.iter()
|
||||
.map(|&w| format!("'{}' ({w:?})", widgets.label(w)))
|
||||
.collect();
|
||||
panic!(
|
||||
"{len} widget(s) were never upgraded\n\
|
||||
this is likely a memory leak; consider upgrading to strong if you plan on using it later\n\
|
||||
weak widgets: {all:#?}"
|
||||
);
|
||||
}
|
||||
if self.root_changed(root) {
|
||||
Drawer::new(self, events).redraw_all();
|
||||
self.old_root = root.into().map(|r| r.id());
|
||||
} else if widgets.has_updates() {
|
||||
Drawer::new(self, events).redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
self.resized = false;
|
||||
Drawer::new(self, events).redraw_all();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn root_changed<'a>(&self, root: impl Into<Option<&'a StrongWidget>>) -> bool {
|
||||
root.into().map(|r| r.id()) != self.old_root
|
||||
}
|
||||
|
||||
pub fn needs_redraw<'a>(
|
||||
&self,
|
||||
root: impl Into<Option<&'a StrongWidget>>,
|
||||
widgets: &Widgets,
|
||||
) -> bool {
|
||||
self.root_changed(root) || widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn active_widgets(&self) -> usize {
|
||||
self.active.len()
|
||||
}
|
||||
|
||||
pub fn debug(&self, widgets: &Widgets, label: &str) -> impl Iterator<Item = &ActiveData> {
|
||||
self.active.iter().filter_map(move |(&id, inst)| {
|
||||
let l = widgets.label(id);
|
||||
if l == label { Some(inst) } else { None }
|
||||
})
|
||||
}
|
||||
|
||||
pub fn debug_layers(&self) {
|
||||
for ((idx, depth), primitives) in self.layers.iter_depth() {
|
||||
let indent = " ".repeat(depth * 2);
|
||||
let len = primitives.instances().len();
|
||||
print!("{indent}{idx}: {len} primitives");
|
||||
if len >= 1 {
|
||||
print!(" ({})", primitives.instances()[0].binding);
|
||||
}
|
||||
println!();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let region = self.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.output_size))
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasRoot {
|
||||
fn set_root(&mut self, root: StrongWidget);
|
||||
}
|
||||
|
||||
impl Default for UiRenderState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user