IDC FINALLY OH MY GOD (I think like ctx + resize propagation + some other stuff)
This commit is contained in:
@@ -78,7 +78,7 @@ impl<W> WidgetId<W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
||||
pub fn any(self) -> WidgetId<AnyWidget> {
|
||||
self.cast_type()
|
||||
}
|
||||
|
||||
@@ -127,14 +127,14 @@ impl<W> Drop for WidgetId<W> {
|
||||
pub struct IdTag;
|
||||
pub struct IdFnTag;
|
||||
|
||||
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
|
||||
impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
|
||||
pub trait WidgetIdFn<W, Ctx>: FnOnce(&mut Ui<Ctx>) -> WidgetId<W> {}
|
||||
impl<W, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetIdFn<W, Ctx> for F {}
|
||||
|
||||
/// TODO: does this ever make sense to use? it allows for invalid ids
|
||||
pub trait Idable<Tag> {
|
||||
pub trait Idable<Ctx, Tag> {
|
||||
type Widget: Widget;
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
||||
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -144,7 +144,7 @@ pub trait Idable<Tag> {
|
||||
id
|
||||
}
|
||||
}
|
||||
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -156,33 +156,33 @@ pub trait Idable<Tag> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> Idable<WidgetTag> for W {
|
||||
impl<W: Widget, Ctx> Idable<Ctx, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||
ui.set(id, self);
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce(&mut Ui) -> W, W: Widget> Idable<FnTag> for F {
|
||||
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget, Ctx> Idable<Ctx, FnTag> for F {
|
||||
type Widget = W;
|
||||
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
|
||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||
let w = self(ui);
|
||||
ui.set(id, w);
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: 'static> WidgetLike<IdTag> for WidgetId<W> {
|
||||
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for WidgetId<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
|
||||
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdFnTag> for F {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
@@ -191,11 +191,14 @@ impl<W> StaticWidgetId<W> {
|
||||
pub fn to_id(&self, send: &Sender<Id>) -> WidgetId<W> {
|
||||
WidgetId::new(self.id.id(), self.ty, send.clone(), true)
|
||||
}
|
||||
pub fn any(self) -> StaticWidgetId<AnyWidget> {
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
|
||||
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for StaticWidgetId<W> {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self.id(&ui.send)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,8 @@ use std::ops::Range;
|
||||
|
||||
use crate::{
|
||||
layout::{
|
||||
Active, Cursor, SensorMap, SizeCtx, TextAttrs, TextBuffer, TextData, TextOffset,
|
||||
TextureHandle, Textures, UiRegion, Vec2, WidgetId, Widgets,
|
||||
Active, Cursor, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle, Textures,
|
||||
UiRegion, Vec2, WidgetId, Widgets,
|
||||
},
|
||||
render::{Primitive, PrimitiveHandle, Primitives},
|
||||
util::{HashSet, Id},
|
||||
@@ -15,12 +15,12 @@ pub struct Painter<'a, 'c> {
|
||||
textures: Vec<TextureHandle>,
|
||||
primitives: Vec<PrimitiveHandle>,
|
||||
children: Vec<Id>,
|
||||
sized_children: HashSet<Id>,
|
||||
id: Id,
|
||||
}
|
||||
|
||||
pub struct PainterCtx<'a> {
|
||||
pub widgets: &'a Widgets,
|
||||
pub sensor_map: &'a SensorMap,
|
||||
pub active: &'a mut Active,
|
||||
pub primitives: &'a mut Primitives,
|
||||
pub textures: &'a mut Textures,
|
||||
@@ -36,6 +36,7 @@ pub struct WidgetInstance {
|
||||
pub textures: Vec<TextureHandle>,
|
||||
pub primitives: Vec<PrimitiveHandle>,
|
||||
pub children: Vec<Id>,
|
||||
pub resize: Option<Id>,
|
||||
pub span: Range<usize>,
|
||||
}
|
||||
|
||||
@@ -43,7 +44,6 @@ impl<'a> PainterCtx<'a> {
|
||||
pub fn new(
|
||||
widgets: &'a Widgets,
|
||||
primitives: &'a mut Primitives,
|
||||
sensor_map: &'a SensorMap,
|
||||
active: &'a mut Active,
|
||||
textures: &'a mut Textures,
|
||||
text: &'a mut TextData,
|
||||
@@ -51,7 +51,6 @@ impl<'a> PainterCtx<'a> {
|
||||
) -> Self {
|
||||
Self {
|
||||
widgets,
|
||||
sensor_map,
|
||||
active,
|
||||
primitives,
|
||||
textures,
|
||||
@@ -63,11 +62,24 @@ impl<'a> PainterCtx<'a> {
|
||||
|
||||
pub fn redraw(&mut self, id: &Id) {
|
||||
self.drawing.clear();
|
||||
let Some(active) = self.active.widgets.get(id) else {
|
||||
return;
|
||||
};
|
||||
|
||||
if let Some(rid) = &active.resize {
|
||||
self.redraw(&rid.duplicate());
|
||||
if self.drawing.contains(id) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
let Some(active) = self.remove(id) else {
|
||||
return;
|
||||
};
|
||||
self.primitives.set_pos(active.span.start);
|
||||
self.draw_inner(id, active.region, active.parent(), Some(active.children));
|
||||
self.active.widgets.get_mut(id).unwrap().resize = active.resize;
|
||||
|
||||
let delta = self.primitives.apply(active.span.clone());
|
||||
if delta != 0
|
||||
&& let Some(parent) = active.parent
|
||||
@@ -90,13 +102,20 @@ impl<'a> PainterCtx<'a> {
|
||||
parent: Option<Id>,
|
||||
old_children: Option<Vec<Id>>,
|
||||
) {
|
||||
// I have no idea if these checks work lol
|
||||
// the idea is u can't redraw stuff u already drew,
|
||||
// and if parent is different then there's another copy with a different parent
|
||||
// but this has a very weird issue where you can't move widgets unless u remove first
|
||||
// so swapping is impossible rn I think?
|
||||
// there's definitely better solutions like a counter (>1 = panic) but don't care rn
|
||||
if self.drawing.contains(id) {
|
||||
panic!("Cannot draw the same widget twice");
|
||||
panic!("Cannot draw the same widget twice (1)");
|
||||
}
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
let mut resize = None;
|
||||
if let Some(active) = self.active.widgets.get(id) {
|
||||
if active.parent != parent {
|
||||
panic!("Cannot draw the same widget twice");
|
||||
panic!("Cannot draw the same widget twice (2)");
|
||||
}
|
||||
if active.region == region {
|
||||
self.primitives.skip(&active.span);
|
||||
@@ -106,6 +125,7 @@ impl<'a> PainterCtx<'a> {
|
||||
// else if active.region.size() == region.size() { move }
|
||||
let active = self.remove(id).unwrap();
|
||||
old_children = active.children;
|
||||
resize = active.resize;
|
||||
}
|
||||
|
||||
let mut painter = Painter {
|
||||
@@ -115,6 +135,7 @@ impl<'a> PainterCtx<'a> {
|
||||
primitives: Vec::new(),
|
||||
ctx: self,
|
||||
children: Vec::new(),
|
||||
sized_children: HashSet::new(),
|
||||
};
|
||||
|
||||
// draw widgets
|
||||
@@ -122,6 +143,8 @@ impl<'a> PainterCtx<'a> {
|
||||
painter.ctx.widgets.get_dyn_dynamic(id).draw(&mut painter);
|
||||
let end_i = painter.ctx.primitives.cur_pos();
|
||||
|
||||
let sized_children = painter.sized_children;
|
||||
|
||||
// add to active
|
||||
let instance = WidgetInstance {
|
||||
id: id.duplicate(),
|
||||
@@ -131,14 +154,20 @@ impl<'a> PainterCtx<'a> {
|
||||
span: start_i..end_i,
|
||||
primitives: painter.primitives,
|
||||
children: painter.children,
|
||||
resize,
|
||||
};
|
||||
for cid in sized_children {
|
||||
if let Some(w) = self.active.widgets.get_mut(&cid) {
|
||||
w.resize = Some(id.duplicate())
|
||||
}
|
||||
}
|
||||
for c in &old_children {
|
||||
if !instance.children.contains(c) {
|
||||
self.remove_rec(c);
|
||||
}
|
||||
}
|
||||
|
||||
self.active.add(id, instance, self.sensor_map);
|
||||
self.active.add(id, instance, self.widgets);
|
||||
}
|
||||
|
||||
fn remove(&mut self, id: &Id) -> Option<WidgetInstance> {
|
||||
@@ -168,8 +197,6 @@ impl<'a> PainterCtx<'a> {
|
||||
let instance = self.active.widgets.get_mut(&parent).unwrap();
|
||||
let end = &mut instance.span.end;
|
||||
*end = end.strict_add_signed(delta);
|
||||
// ids are supposed to be unique so theoretically no cloning is needed
|
||||
// leaving for now for testing
|
||||
let parent = instance.parent();
|
||||
for child in instance
|
||||
.children
|
||||
@@ -186,6 +213,7 @@ impl<'a> PainterCtx<'a> {
|
||||
|
||||
fn shift_child(&mut self, child: &Id, start: usize, delta: isize) {
|
||||
let instance = self.active.widgets.get_mut(child).unwrap();
|
||||
// = also prevents the original id from getting shifted
|
||||
if instance.span.start <= start {
|
||||
return;
|
||||
}
|
||||
@@ -263,6 +291,7 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||
self.sized_children.insert(id.id.duplicate());
|
||||
self.ctx
|
||||
.widgets
|
||||
.get_dyn_dynamic(&id.id)
|
||||
@@ -275,10 +304,36 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
text: self.ctx.text,
|
||||
textures: self.ctx.textures,
|
||||
widgets: self.ctx.widgets,
|
||||
checked: &mut self.sized_children,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a> {
|
||||
pub size: Vec2,
|
||||
pub text: &'a mut TextData,
|
||||
pub textures: &'a mut Textures,
|
||||
widgets: &'a Widgets,
|
||||
checked: &'a mut HashSet<Id>,
|
||||
}
|
||||
|
||||
impl SizeCtx<'_> {
|
||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||
self.checked.insert(id.id.duplicate());
|
||||
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
|
||||
}
|
||||
pub fn draw_text(
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
content: &str,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &Cursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.text
|
||||
.draw(buffer, content, attrs, cursor, self.textures)
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetInstance {
|
||||
pub fn parent(&self) -> Option<Id> {
|
||||
self.parent.as_ref().map(|p| p.duplicate())
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
use std::any::Any;
|
||||
|
||||
use crate::{
|
||||
layout::{Ui, UiRegion, Vec2, WidgetId},
|
||||
layout::{Ui, UiRegion, Vec2},
|
||||
util::{HashMap, Id, bitflags},
|
||||
};
|
||||
|
||||
@@ -30,19 +28,18 @@ pub enum ActivationState {
|
||||
Off,
|
||||
}
|
||||
|
||||
pub struct Sensor {
|
||||
pub struct Sensor<Ctx> {
|
||||
pub senses: Senses,
|
||||
pub f: Box<dyn SenseFn>,
|
||||
pub f: Box<dyn SenseFn<Ctx>>,
|
||||
}
|
||||
|
||||
pub type SensorMap = HashMap<Id, SensorGroup>;
|
||||
pub type SensorMap<Ctx> = HashMap<Id, SensorGroup<Ctx>>;
|
||||
pub type ActiveSensors = HashMap<Id, SenseShape>;
|
||||
pub type SenseShape = UiRegion;
|
||||
#[derive(Default)]
|
||||
pub struct SensorGroup {
|
||||
pub struct SensorGroup<Ctx> {
|
||||
pub hover: ActivationState,
|
||||
pub cursor: ActivationState,
|
||||
pub sensors: Vec<Sensor>,
|
||||
pub sensors: Vec<Sensor<Ctx>>,
|
||||
}
|
||||
|
||||
pub struct SenseCtx {
|
||||
@@ -50,41 +47,37 @@ pub struct SenseCtx {
|
||||
pub size: Vec2,
|
||||
}
|
||||
|
||||
pub trait SenseFn: FnMut(&mut Ui, SenseCtx) + 'static {}
|
||||
impl<F: FnMut(&mut Ui, SenseCtx) + 'static> SenseFn for F {}
|
||||
pub trait SenseFn<Ctx>: FnMut(&mut Ctx, SenseCtx) + 'static {}
|
||||
impl<F: FnMut(&mut Ctx, SenseCtx) + 'static, Ctx> SenseFn<Ctx> for F {}
|
||||
|
||||
impl Ui {
|
||||
pub fn add_sensor<W>(&mut self, id: &WidgetId<W>, f: Sensor) {
|
||||
self.sensor_map
|
||||
.entry(id.id.duplicate())
|
||||
.or_default()
|
||||
.sensors
|
||||
.push(f);
|
||||
}
|
||||
pub trait UiCtx {
|
||||
fn ui(&mut self) -> &mut Ui<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
}
|
||||
|
||||
pub fn run_sensors(&mut self, cursor: &CursorState, window_size: Vec2) {
|
||||
let active = std::mem::take(&mut self.active.sensors);
|
||||
let mut map = std::mem::take(&mut self.sensor_map);
|
||||
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);
|
||||
group.hover.update(in_shape);
|
||||
group.cursor.update(cursor.pressed && in_shape);
|
||||
pub fn run_sensors<Ctx: UiCtx>(ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
||||
let active = std::mem::take(&mut ctx.ui().active.sensors);
|
||||
let mut map = std::mem::take(&mut ctx.ui().sensor_map);
|
||||
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);
|
||||
group.hover.update(in_shape);
|
||||
group.cursor.update(cursor.pressed && in_shape);
|
||||
|
||||
for sensor in &mut group.sensors {
|
||||
if should_run(sensor.senses, group.cursor, group.hover) {
|
||||
let ctx = SenseCtx {
|
||||
cursor: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
};
|
||||
(sensor.f)(self, ctx);
|
||||
}
|
||||
for sensor in &mut group.sensors {
|
||||
if should_run(sensor.senses, group.cursor, group.hover) {
|
||||
let sctx = SenseCtx {
|
||||
cursor: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
};
|
||||
(sensor.f)(ctx, sctx);
|
||||
}
|
||||
}
|
||||
self.sensor_map = map;
|
||||
self.active.sensors = active;
|
||||
}
|
||||
ctx.ui().sensor_map = map;
|
||||
ctx.ui().active.sensors = active;
|
||||
}
|
||||
|
||||
pub fn should_run(senses: Senses, cursor: ActivationState, hover: ActivationState) -> bool {
|
||||
@@ -138,3 +131,13 @@ impl ActivationState {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Ctx> Default for SensorGroup<Ctx> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
hover: Default::default(),
|
||||
cursor: Default::default(),
|
||||
sensors: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -33,8 +33,8 @@ pub enum Cursor {
|
||||
#[default]
|
||||
None,
|
||||
Select {
|
||||
line: usize,
|
||||
idx: usize,
|
||||
line: isize,
|
||||
col: isize,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -78,15 +78,14 @@ impl TextData {
|
||||
let mut max_y = 0;
|
||||
let c = attrs.color;
|
||||
let mut max_width = 0.0f32;
|
||||
let mut i = 0;
|
||||
let mut cursor_x = 0;
|
||||
for (run_i, run) in buffer.layout_runs().enumerate() {
|
||||
if let Cursor::Select { line, .. } = cursor
|
||||
&& *line == run_i
|
||||
&& *line == run_i as isize
|
||||
{
|
||||
cursor_x = run.line_w as i32;
|
||||
}
|
||||
for glyph in run.glyphs.iter() {
|
||||
for (i, glyph) in run.glyphs.iter().enumerate() {
|
||||
let physical_glyph = glyph.physical((0., 0.), 1.0);
|
||||
|
||||
let glyph_color = match glyph.color_opt {
|
||||
@@ -94,13 +93,12 @@ impl TextData {
|
||||
None => cosmic_text::Color::rgba(c.r, c.g, c.b, c.a),
|
||||
};
|
||||
|
||||
if let Cursor::Select { idx, line } = cursor
|
||||
&& *line == run_i
|
||||
&& *idx == i
|
||||
if let Cursor::Select { col: idx, line } = cursor
|
||||
&& *line == run_i as isize
|
||||
&& *idx == i as isize
|
||||
{
|
||||
cursor_x = physical_glyph.x;
|
||||
}
|
||||
i += 1;
|
||||
|
||||
self.swash_cache.with_pixels(
|
||||
&mut self.font_system,
|
||||
@@ -133,19 +131,23 @@ impl TextData {
|
||||
image.put_pixel(x, y, color);
|
||||
}
|
||||
if let &Cursor::Select { line, .. } = cursor {
|
||||
let x = (cursor_x - min_x) as u32;
|
||||
for y in 0..attrs.line_height as u32 {
|
||||
// no clue if this is good or bad for non integer values
|
||||
// depends on how the layouting is actually done
|
||||
let x = (cursor_x - min_x) as u32;
|
||||
let y = (y as f32 + attrs.line_height * line as f32 - min_y as f32) as u32;
|
||||
image.put_pixel(x, y, Rgba(c.as_arr()));
|
||||
}
|
||||
}
|
||||
let mut lines = buffer.lines.len();
|
||||
if content.ends_with('\n') {
|
||||
lines += 1;
|
||||
}
|
||||
let offset = TextOffset {
|
||||
top_left: Vec2::new(min_x as f32, min_y as f32),
|
||||
bot_right: Vec2::new(
|
||||
max_width - max_x as f32,
|
||||
attrs.line_height * buffer.lines.len() as f32 - max_y as f32,
|
||||
attrs.line_height * lines as f32 - max_y as f32,
|
||||
),
|
||||
};
|
||||
(textures.add(image), offset)
|
||||
|
||||
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);
|
||||
|
||||
@@ -1,7 +1,4 @@
|
||||
use crate::layout::{
|
||||
Cursor, Painter, StaticWidgetId, TextAttrs, TextBuffer, TextData, TextOffset, TextureHandle,
|
||||
Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets,
|
||||
};
|
||||
use crate::layout::{Painter, SizeCtx, StaticWidgetId, Ui, Vec2, WidgetId, WidgetIdFn};
|
||||
|
||||
use std::{any::Any, marker::PhantomData};
|
||||
|
||||
@@ -12,39 +9,16 @@ pub trait Widget: Any {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a> {
|
||||
pub size: Vec2,
|
||||
pub text: &'a mut TextData,
|
||||
pub textures: &'a mut Textures,
|
||||
pub(super) widgets: &'a Widgets,
|
||||
}
|
||||
|
||||
impl SizeCtx<'_> {
|
||||
pub fn size<W>(&mut self, id: &WidgetId<W>) -> Vec2 {
|
||||
self.widgets.get_dyn_dynamic(&id.id).get_size(self)
|
||||
}
|
||||
pub fn draw_text(
|
||||
&mut self,
|
||||
buffer: &mut TextBuffer,
|
||||
content: &str,
|
||||
attrs: &TextAttrs,
|
||||
cursor: &Cursor,
|
||||
) -> (TextureHandle, TextOffset) {
|
||||
self.text
|
||||
.draw(buffer, content, attrs, cursor, self.textures)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WidgetTag;
|
||||
pub struct FnTag;
|
||||
|
||||
pub trait WidgetLike<Tag> {
|
||||
pub trait WidgetLike<Ctx, Tag> {
|
||||
type Widget: 'static;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
|
||||
fn with_id<W2>(
|
||||
self,
|
||||
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
||||
) -> impl WidgetIdFn<W2>
|
||||
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
|
||||
) -> impl WidgetIdFn<W2, Ctx>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -53,7 +27,7 @@ pub trait WidgetLike<Tag> {
|
||||
f(ui, id)
|
||||
}
|
||||
}
|
||||
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
|
||||
fn add_static(self, ui: &mut Ui<Ctx>) -> StaticWidgetId<Self::Widget>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -64,19 +38,19 @@ pub trait WidgetLike<Tag> {
|
||||
/// A function that returns a widget given a UI.
|
||||
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
||||
/// don't need to be IDs yet
|
||||
pub trait WidgetFn<W: Widget>: FnOnce(&mut Ui) -> W {}
|
||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||
pub trait WidgetFn<W: Widget, Ctx>: FnOnce(&mut Ui<Ctx>) -> W {}
|
||||
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetFn<W, Ctx> for F {}
|
||||
|
||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetLike<Ctx, FnTag> for F {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
self(ui).add(ui)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> WidgetLike<WidgetTag> for W {
|
||||
impl<W: Widget, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetId<W> {
|
||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||
ui.add_widget(self)
|
||||
}
|
||||
}
|
||||
@@ -96,22 +70,22 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
|
||||
}
|
||||
|
||||
pub struct ArrTag;
|
||||
pub trait WidgetArrLike<const LEN: usize, Tags> {
|
||||
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
|
||||
type Ws;
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
|
||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
|
||||
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
|
||||
type Ws = Ws;
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
|
||||
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
|
||||
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
|
||||
type Ws = (W::Widget,);
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
|
||||
WidgetArr::new([self.add(ui).erase_type()])
|
||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
|
||||
WidgetArr::new([self.add(ui).any()])
|
||||
}
|
||||
}
|
||||
|
||||
@@ -121,9 +95,9 @@ macro_rules! impl_widget_arr {
|
||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||
};
|
||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
|
||||
impl<Ctx, $($W: WidgetLike<Ctx, $Tag>,$Tag,)*> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
|
||||
type Ws = ($($W::Widget,)*);
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
||||
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
|
||||
#[allow(non_snake_case)]
|
||||
let ($($W,)*) = self;
|
||||
WidgetArr::new(
|
||||
|
||||
Reference in New Issue
Block a user