update px dependent on resize + move painter data into struct in ui

This commit is contained in:
2025-09-28 13:14:51 -04:00
parent 61df088cc7
commit c98a43f94d
7 changed files with 99 additions and 105 deletions

View File

@@ -169,8 +169,8 @@ impl<Ctx: UiCtx + 'static> SensorCtx for Ctx {
impl<Ctx: UiCtx + 'static> CursorModule<Ctx> { impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) { pub fn run(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
let layers = std::mem::take(&mut ctx.ui().layers); let layers = std::mem::take(&mut ctx.ui().data.layers);
let mut module = std::mem::take(ctx.ui().modules.get_mut::<Self>()); let mut module = std::mem::take(ctx.ui().data.modules.get_mut::<Self>());
for i in layers.indices().rev() { for i in layers.indices().rev() {
let Some(list) = module.active.get_mut(&i) else { let Some(list) = module.active.get_mut(&i) else {
@@ -203,10 +203,10 @@ impl<Ctx: UiCtx + 'static> CursorModule<Ctx> {
} }
} }
let ui_mod = ctx.ui().modules.get_mut::<Self>(); let ui_mod = ctx.ui().data.modules.get_mut::<Self>();
std::mem::swap(ui_mod, &mut module); std::mem::swap(ui_mod, &mut module);
ui_mod.merge(module); ui_mod.merge(module);
ctx.ui().layers = layers; ctx.ui().data.layers = layers;
} }
} }

View File

@@ -124,12 +124,8 @@ impl FnOnce<(&mut Ui,)> for TextBuilder {
extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output { extern "rust-call" fn call_once(self, args: (&mut Ui,)) -> Self::Output {
let mut buf = let mut buf =
TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height)); TextBuffer::new_empty(Metrics::new(self.attrs.font_size, self.attrs.line_height));
buf.set_text( let font_system = &mut args.0.data.text.font_system;
&mut args.0.text.font_system, buf.set_text(font_system, &self.content, &Attrs::new(), Shaping::Advanced);
&self.content,
&Attrs::new(),
Shaping::Advanced,
);
let mut text = Text { let mut text = Text {
content: self.content.into(), content: self.content.into(),
buf, buf,
@@ -138,8 +134,7 @@ impl FnOnce<(&mut Ui,)> for TextBuilder {
size: Vec2::ZERO, size: Vec2::ZERO,
}; };
text.content.changed = false; text.content.changed = false;
self.attrs self.attrs.apply(font_system, &mut text.buf, None);
.apply(&mut args.0.text.font_system, &mut text.buf, None);
text text
} }
} }

View File

@@ -322,14 +322,10 @@ impl FnOnce<(&mut Ui,)> for TextEditBuilder {
cursor: None, cursor: None,
size: Vec2::ZERO, size: Vec2::ZERO,
}; };
text.buf.set_text( let font_system = &mut args.0.data.text.font_system;
&mut args.0.text.font_system, text.buf
&self.content, .set_text(font_system, &self.content, &Attrs::new(), Shaping::Advanced);
&Attrs::new(), self.attrs.apply(font_system, &mut text.buf, None);
Shaping::Advanced,
);
self.attrs
.apply(&mut args.0.text.font_system, &mut text.buf, None);
text text
} }
} }

View File

@@ -55,7 +55,8 @@ impl<W: WidgetLike<Tag>, Tag> Eventable<W::Widget, Tag> for W {
) -> impl WidgetIdFn<W::Widget> { ) -> impl WidgetIdFn<W::Widget> {
move |ui| { move |ui| {
let id = self.add(ui); let id = self.add(ui);
ui.modules ui.data
.modules
.get_mut::<E::Module<Ctx>>() .get_mut::<E::Module<Ctx>>()
.register(id.id, event, f); .register(id.id, event, f);
id id
@@ -180,6 +181,7 @@ impl Ui {
) { ) {
if let Some(f) = ctx if let Some(f) = ctx
.ui() .ui()
.data
.modules .modules
.get_mut::<E::Module<Ctx>>() .get_mut::<E::Module<Ctx>>()
.run(&id.id, event) .run(&id.id, event)

View File

@@ -1,7 +1,7 @@
use crate::{ use crate::{
layout::{ layout::{
ActiveWidgets, Layers, Modules, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Layers, Modules, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures,
Textures, UiRegion, UiVec2, Vec2, WidgetId, Widgets, UiRegion, UiVec2, Vec2, WidgetId, Widgets,
}, },
render::{Primitive, PrimitiveHandle}, render::{Primitive, PrimitiveHandle},
util::{HashMap, HashSet, Id}, util::{HashMap, HashSet, Id},
@@ -16,19 +16,19 @@ pub struct Painter<'a, 'c> {
sized_children: HashMap<Id, UiVec2>, sized_children: HashMap<Id, UiVec2>,
/// whether this widget depends on region's final pixel size or not /// whether this widget depends on region's final pixel size or not
/// TODO: decide if point (pt) should be used here instead of px /// TODO: decide if point (pt) should be used here instead of px
px_dependent: bool,
pub layer: usize, pub layer: usize,
id: Id, id: Id,
} }
pub struct PainterCtx<'a> { pub struct PainterCtx<'a> {
pub widgets: &'a Widgets, pub widgets: &'a Widgets,
pub active: &'a mut ActiveWidgets, pub active: &'a mut HashMap<Id, WidgetInstance>,
pub layers: &'a mut Layers, pub layers: &'a mut Layers,
pub textures: &'a mut Textures, pub textures: &'a mut Textures,
pub text: &'a mut TextData, pub text: &'a mut TextData,
pub screen_size: Vec2, pub screen_size: Vec2,
pub modules: &'a mut Modules, pub modules: &'a mut Modules,
pub px_dependent: &'a mut HashSet<Id>,
drawing: HashSet<Id>, drawing: HashSet<Id>,
} }
@@ -43,24 +43,43 @@ pub struct WidgetInstance {
pub layer: usize, pub layer: usize,
} }
impl<'a> PainterCtx<'a> { pub struct PainterData {
pub fn new( pub widgets: Widgets,
widgets: &'a Widgets, pub active: HashMap<Id, WidgetInstance>,
layers: &'a mut Layers, pub layers: Layers,
active: &'a mut ActiveWidgets, pub textures: Textures,
modules: &'a mut Modules, pub text: TextData,
textures: &'a mut Textures, pub output_size: Vec2,
text: &'a mut TextData, pub modules: Modules,
screen_size: Vec2, pub px_dependent: HashSet<Id>,
) -> Self { }
impl Default for PainterData {
fn default() -> Self {
Self { Self {
widgets, widgets: Widgets::new(),
active, layers: Default::default(),
layers, textures: Textures::new(),
textures, text: TextData::default(),
text, active: Default::default(),
screen_size, output_size: Vec2::ZERO,
modules, modules: Modules::default(),
px_dependent: Default::default(),
}
}
}
impl<'a> PainterCtx<'a> {
pub fn new(data: &'a mut PainterData) -> Self {
Self {
widgets: &data.widgets,
active: &mut data.active,
layers: &mut data.layers,
textures: &mut data.textures,
text: &mut data.text,
screen_size: data.output_size,
modules: &mut data.modules,
px_dependent: &mut data.px_dependent,
drawing: HashSet::default(), drawing: HashSet::default(),
} }
} }
@@ -157,7 +176,6 @@ impl<'a> PainterCtx<'a> {
ctx: self, ctx: self,
children: Vec::new(), children: Vec::new(),
sized_children: Default::default(), sized_children: Default::default(),
px_dependent: false,
}; };
// draw widgets // draw widgets
@@ -224,6 +242,7 @@ impl<'a> PainterCtx<'a> {
m.on_undraw(inst); m.on_undraw(inst);
} }
} }
self.px_dependent.remove(&id);
inst inst
} }
@@ -317,7 +336,7 @@ impl<'a, 'c> Painter<'a, 'c> {
} }
pub fn px_size(&mut self) -> Vec2 { pub fn px_size(&mut self) -> Vec2 {
self.px_dependent = true; self.ctx.px_dependent.insert(self.id);
self.region.in_size(self.ctx.screen_size) self.region.in_size(self.ctx.screen_size)
} }

View File

@@ -3,10 +3,9 @@ use image::DynamicImage;
use crate::{ use crate::{
core::{TextEdit, TextEditCtx}, core::{TextEdit, TextEditCtx},
layout::{ layout::{
Layers, Modules, PainterCtx, StaticWidgetId, TextData, TextureHandle, Textures, Vec2, PainterCtx, PainterData, StaticWidgetId, TextureHandle, Vec2, Widget, WidgetId, WidgetLike,
Widget, WidgetId, WidgetInstance, WidgetLike, Widgets,
}, },
util::{HashMap, Id}, util::Id,
}; };
use std::{ use std::{
any::{Any, TypeId}, any::{Any, TypeId},
@@ -15,20 +14,13 @@ use std::{
}; };
pub struct Ui { pub struct Ui {
pub(crate) data: PainterData,
root: Option<WidgetId>, root: Option<WidgetId>,
pub(super) widgets: Widgets,
updates: Vec<Id>, updates: Vec<Id>,
recv: Receiver<Id>, recv: Receiver<Id>,
pub(super) send: Sender<Id>, pub(super) send: Sender<Id>,
size: Vec2,
// TODO: make these non pub(crate)
pub(crate) layers: Layers,
pub(crate) textures: Textures,
pub(crate) text: TextData,
full_redraw: bool, full_redraw: bool,
resized: bool,
pub(crate) active: ActiveWidgets,
pub modules: Modules,
} }
impl Ui { impl Ui {
@@ -46,11 +38,11 @@ impl Ui {
/// useful for debugging /// useful for debugging
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) { pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
self.widgets.data_mut(&id.id).unwrap().label = label; self.data.widgets.data_mut(&id.id).unwrap().label = label;
} }
pub fn label<W>(&self, id: &WidgetId<W>) -> &String { pub fn label<W>(&self, id: &WidgetId<W>) -> &String {
&self.widgets.data(&id.id).unwrap().label &self.data.widgets.data(&id.id).unwrap().label
} }
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> { pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
@@ -59,12 +51,12 @@ impl Ui {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> { pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.id(); let id = self.id();
self.widgets.insert(id.id, w); self.data.widgets.insert(id.id, w);
id id
} }
pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) { pub fn set<W: Widget>(&mut self, id: &WidgetId<W>, w: W) {
self.widgets.insert(id.id, w); self.data.widgets.insert(id.id, w);
} }
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) { pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
@@ -77,16 +69,16 @@ impl Ui {
} }
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> { pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
self.widgets.get(id) self.data.widgets.get(id)
} }
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> { pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
self.widgets.get_mut(id) self.data.widgets.get_mut(id)
} }
pub fn id<W: Widget>(&mut self) -> WidgetId<W> { pub fn id<W: Widget>(&mut self) -> WidgetId<W> {
WidgetId::new( WidgetId::new(
self.widgets.reserve(), self.data.widgets.reserve(),
TypeId::of::<W>(), TypeId::of::<W>(),
self.send.clone(), self.send.clone(),
false, false,
@@ -99,26 +91,19 @@ impl Ui {
} }
pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle { pub fn add_texture(&mut self, image: DynamicImage) -> TextureHandle {
self.textures.add(image) self.data.textures.add(image)
} }
pub fn resize(&mut self, size: impl Into<Vec2>) { pub fn resize(&mut self, size: impl Into<Vec2>) {
self.size = size.into(); self.data.output_size = size.into();
self.resized = true;
} }
pub fn redraw_all(&mut self) { pub fn redraw_all(&mut self) {
self.active.clear(); self.data.active.clear();
// free before bc nothing should exist // free before bc nothing should exist
self.free(); self.free();
let mut ctx = PainterCtx::new( let mut ctx = PainterCtx::new(&mut self.data);
&self.widgets,
&mut self.layers,
&mut self.active,
&mut self.modules,
&mut self.textures,
&mut self.text,
self.size,
);
if let Some(root) = &self.root { if let Some(root) = &self.root {
ctx.draw(root.id); ctx.draw(root.id);
} }
@@ -131,18 +116,22 @@ impl Ui {
} else if !self.updates.is_empty() { } else if !self.updates.is_empty() {
self.redraw_updates(); self.redraw_updates();
} }
if self.resized {
self.resized = false;
self.redraw_size();
}
}
fn redraw_size(&mut self) {
let mut ctx = PainterCtx::new(&mut self.data);
let dep = ctx.px_dependent.clone();
for id in dep {
ctx.redraw(id);
}
} }
fn redraw_updates(&mut self) { fn redraw_updates(&mut self) {
let mut ctx = PainterCtx::new( let mut ctx = PainterCtx::new(&mut self.data);
&self.widgets,
&mut self.layers,
&mut self.active,
&mut self.modules,
&mut self.textures,
&mut self.text,
self.size,
);
for id in self.updates.drain(..) { for id in self.updates.drain(..) {
ctx.redraw(id); ctx.redraw(id);
} }
@@ -152,12 +141,12 @@ impl Ui {
/// free any resources that don't have references anymore /// free any resources that don't have references anymore
fn free(&mut self) { fn free(&mut self) {
for id in self.recv.try_iter() { for id in self.recv.try_iter() {
for m in self.modules.iter_mut() { for m in self.data.modules.iter_mut() {
m.on_remove(&id); m.on_remove(&id);
} }
self.widgets.delete(id); self.data.widgets.delete(id);
} }
self.textures.free(); self.data.textures.free();
} }
pub fn needs_redraw(&self) -> bool { pub fn needs_redraw(&self) -> bool {
@@ -165,18 +154,18 @@ impl Ui {
} }
pub fn num_widgets(&self) -> usize { pub fn num_widgets(&self) -> usize {
self.widgets.len() self.data.widgets.len()
} }
pub fn active_widgets(&self) -> usize { pub fn active_widgets(&self) -> usize {
self.active.len() self.data.active.len()
} }
pub fn text(&mut self, id: &WidgetId<TextEdit>) -> TextEditCtx<'_> { pub fn text(&mut self, id: &WidgetId<TextEdit>) -> TextEditCtx<'_> {
self.updates.push(id.id); self.updates.push(id.id);
TextEditCtx { TextEditCtx {
text: self.widgets.get_mut(id).unwrap(), text: self.data.widgets.get_mut(id).unwrap(),
font_system: &mut self.text.font_system, font_system: &mut self.data.text.font_system,
} }
} }
} }
@@ -200,14 +189,14 @@ impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W; type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output { fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
self.widgets.get_static(&id).unwrap() self.data.widgets.get_static(&id).unwrap()
} }
} }
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui { impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output { fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id); self.updates.push(id.id);
self.widgets.get_static_mut(&id).unwrap() self.data.widgets.get_static_mut(&id).unwrap()
} }
} }
@@ -225,20 +214,13 @@ impl Default for Ui {
fn default() -> Self { fn default() -> Self {
let (send, recv) = channel(); let (send, recv) = channel();
Self { Self {
data: PainterData::default(),
root: Default::default(), root: Default::default(),
widgets: Widgets::new(),
updates: Default::default(), updates: Default::default(),
layers: Default::default(),
textures: Textures::new(),
text: TextData::default(),
full_redraw: false, full_redraw: false,
active: Default::default(),
send, send,
recv, recv,
size: Vec2::ZERO, resized: false,
modules: Modules::default(),
} }
} }
} }
pub type ActiveWidgets = HashMap<Id, WidgetInstance>;

View File

@@ -59,11 +59,11 @@ impl UiRenderer {
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) { pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
self.active.clear(); self.active.clear();
for (i, ulayer) in ui.layers.iter_mut() { for (i, ulayer) in ui.data.layers.iter_mut() {
self.active.push(i); self.active.push(i);
let primitives = &mut ulayer.primitives; let primitives = &mut ulayer.primitives;
for change in primitives.apply_free() { for change in primitives.apply_free() {
if let Some(inst) = ui.active.get_mut(&change.id) { if let Some(inst) = ui.data.active.get_mut(&change.id) {
for h in &mut inst.primitives { for h in &mut inst.primitives {
if h.inst_idx == change.old { if h.inst_idx == change.old {
h.inst_idx = change.new; h.inst_idx = change.new;
@@ -98,7 +98,7 @@ impl UiRenderer {
) )
} }
} }
if self.textures.update(&mut ui.textures) { if self.textures.update(&mut ui.data.textures) {
self.rsc_group = Self::rsc_group(device, &self.rsc_layout, &self.textures) self.rsc_group = Self::rsc_group(device, &self.rsc_layout, &self.textures)
} }
} }