refcount ids and delete unused
This commit is contained in:
6
TODO
Normal file
6
TODO
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
images
|
||||||
|
text
|
||||||
|
ui ctx = ui + app ctx for sensors
|
||||||
|
free / delete widgets
|
||||||
|
abstract sensors to work with any event, maybe associate data as well?
|
||||||
|
make senses orable so you can select multiple
|
||||||
@@ -7,7 +7,7 @@ pub struct Stack {
|
|||||||
impl<Ctx> Widget<Ctx> for Stack {
|
impl<Ctx> Widget<Ctx> for Stack {
|
||||||
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
fn draw(&self, painter: &mut crate::Painter<Ctx>) {
|
||||||
for child in &self.children {
|
for child in &self.children {
|
||||||
painter.draw(child);
|
painter.draw_within(child, painter.region);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
147
src/layout/id.rs
Normal file
147
src/layout/id.rs
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
use std::{
|
||||||
|
any::TypeId,
|
||||||
|
marker::PhantomData,
|
||||||
|
sync::{
|
||||||
|
Arc,
|
||||||
|
atomic::{AtomicU32, Ordering},
|
||||||
|
mpsc::Sender,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{FnTag, Ui, Widget, WidgetLike, WidgetTag, util::Id};
|
||||||
|
|
||||||
|
pub struct AnyWidget;
|
||||||
|
|
||||||
|
/// An identifier for a widget that can index a UI to get the associated widget.
|
||||||
|
/// It should always remain valid; it keeps a ref count and removes the widget from the UI if all
|
||||||
|
/// references are dropped.
|
||||||
|
///
|
||||||
|
/// W does not need to implement widget so that AnyWidget is valid;
|
||||||
|
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct WidgetId<W = AnyWidget> {
|
||||||
|
pub(super) ty: TypeId,
|
||||||
|
pub(super) id: Id,
|
||||||
|
pub(super) refcount: Arc<AtomicU32>,
|
||||||
|
pub(super) send: Sender<Id>,
|
||||||
|
_pd: PhantomData<W>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W> std::fmt::Debug for WidgetId<W> {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
self.id.fmt(f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W> Clone for WidgetId<W> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
self.refcount.fetch_add(1, Ordering::Release);
|
||||||
|
Self {
|
||||||
|
id: self.id.duplicate(),
|
||||||
|
ty: self.ty,
|
||||||
|
refcount: self.refcount.clone(),
|
||||||
|
send: self.send.clone(),
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W> WidgetId<W> {
|
||||||
|
pub(super) fn new(id: Id, ty: TypeId, send: Sender<Id>) -> Self {
|
||||||
|
Self {
|
||||||
|
ty,
|
||||||
|
id,
|
||||||
|
refcount: AtomicU32::new(0).into(),
|
||||||
|
send,
|
||||||
|
_pd: PhantomData,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
||||||
|
self.cast_type()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
|
||||||
|
// safety: self is repr(C) and generic only used for phantom data
|
||||||
|
unsafe { std::mem::transmute(self) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn cast_type<W2>(self) -> WidgetId<W2> {
|
||||||
|
unsafe { std::mem::transmute(self) }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn refs(&self) -> u32 {
|
||||||
|
self.refcount.load(Ordering::Acquire)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W> Drop for WidgetId<W> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let refs = self.refcount.fetch_sub(1, Ordering::Release);
|
||||||
|
if refs == 0 {
|
||||||
|
let _ = self.send.send(self.id.duplicate());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct IdTag;
|
||||||
|
|
||||||
|
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
||||||
|
macro_rules! WidgetIdFnRet {
|
||||||
|
($W:ty, $Ctx:ty) => {
|
||||||
|
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W>
|
||||||
|
};
|
||||||
|
($W:ty, $Ctx:ty, $($use:tt)*) => {
|
||||||
|
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W> + use<$($use)*>
|
||||||
|
};
|
||||||
|
}
|
||||||
|
pub(crate) use WidgetIdFnRet;
|
||||||
|
|
||||||
|
pub trait Idable<Ctx, Tag> {
|
||||||
|
type Widget: Widget<Ctx>;
|
||||||
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
||||||
|
fn id<'a>(
|
||||||
|
self,
|
||||||
|
id: &WidgetId<Self::Widget>,
|
||||||
|
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
let id = id.clone();
|
||||||
|
move |ui| {
|
||||||
|
self.set(ui, &id);
|
||||||
|
id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
|
||||||
|
type Widget = W;
|
||||||
|
|
||||||
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||||
|
ui.set(id, self);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
|
||||||
|
type Widget = W;
|
||||||
|
|
||||||
|
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
||||||
|
let w = self(ui);
|
||||||
|
ui.set(id, w);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: 'static, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
|
||||||
|
type Widget = W;
|
||||||
|
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
|
||||||
|
type Widget = W;
|
||||||
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||||
|
self(ui)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,7 @@ mod sense;
|
|||||||
mod ui;
|
mod ui;
|
||||||
mod vec2;
|
mod vec2;
|
||||||
mod widget;
|
mod widget;
|
||||||
|
mod id;
|
||||||
|
|
||||||
pub use color::*;
|
pub use color::*;
|
||||||
pub use painter::*;
|
pub use painter::*;
|
||||||
@@ -13,5 +14,6 @@ pub use sense::*;
|
|||||||
pub use ui::*;
|
pub use ui::*;
|
||||||
pub use vec2::*;
|
pub use vec2::*;
|
||||||
pub use widget::*;
|
pub use widget::*;
|
||||||
|
pub use id::*;
|
||||||
|
|
||||||
pub type UiColor = Color<u8>;
|
pub type UiColor = Color<u8>;
|
||||||
|
|||||||
@@ -12,27 +12,19 @@ pub struct UiPos {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UiPos {
|
impl UiPos {
|
||||||
pub const fn anchor_offset(anchor_x: f32, anchor_y: f32, offset_x: f32, offset_y: f32) -> Self {
|
|
||||||
Self {
|
|
||||||
anchor: vec2(anchor_x, anchor_y),
|
|
||||||
offset: vec2(offset_x, offset_y),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn center() -> Self {
|
pub const fn center() -> Self {
|
||||||
Self::anchor_offset(0.5, 0.5, 0.0, 0.0)
|
Self::anchor(vec2(0.5, 0.5))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn anchor(anchor: Vec2) -> Self {
|
pub const fn anchor(anchor: Vec2) -> Self {
|
||||||
Self::anchor_offset(anchor.x, anchor.y, 0.0, 0.0)
|
Self {
|
||||||
|
anchor,
|
||||||
|
offset: Vec2::ZERO,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn top_left() -> Self {
|
pub const fn corner(corner: Corner) -> Self {
|
||||||
Self::anchor_offset(0.0, 0.0, 0.0, 0.0)
|
Self::anchor(corner.anchor())
|
||||||
}
|
|
||||||
|
|
||||||
pub const fn bottom_right() -> Self {
|
|
||||||
Self::anchor_offset(1.0, 1.0, 0.0, 0.0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn shift(&mut self, offset: Vec2) {
|
pub const fn shift(&mut self, offset: Vec2) {
|
||||||
@@ -126,8 +118,8 @@ pub struct UiRegion {
|
|||||||
impl UiRegion {
|
impl UiRegion {
|
||||||
pub const fn full() -> Self {
|
pub const fn full() -> Self {
|
||||||
Self {
|
Self {
|
||||||
top_left: UiPos::top_left(),
|
top_left: UiPos::corner(Corner::TopLeft),
|
||||||
bot_right: UiPos::bottom_right(),
|
bot_right: UiPos::corner(Corner::BotRight),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn center() -> Self {
|
pub fn center() -> Self {
|
||||||
@@ -178,17 +170,10 @@ impl UiRegion {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn top_left() -> Self {
|
pub fn corner(corner: Corner) -> Self {
|
||||||
Self {
|
Self {
|
||||||
top_left: UiPos::top_left(),
|
top_left: UiPos::corner(corner),
|
||||||
bot_right: UiPos::top_left(),
|
bot_right: UiPos::corner(corner),
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn bottom_right() -> Self {
|
|
||||||
Self {
|
|
||||||
top_left: UiPos::bottom_right(),
|
|
||||||
bot_right: UiPos::bottom_right(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -236,3 +221,22 @@ impl UIScalarView<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, Debug)]
|
||||||
|
pub enum Corner {
|
||||||
|
TopLeft,
|
||||||
|
TopRight,
|
||||||
|
BotLeft,
|
||||||
|
BotRight,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Corner {
|
||||||
|
pub const fn anchor(&self) -> Vec2 {
|
||||||
|
match self {
|
||||||
|
Corner::TopLeft => vec2(0.0, 0.0),
|
||||||
|
Corner::TopRight => vec2(1.0, 0.0),
|
||||||
|
Corner::BotLeft => vec2(0.0, 1.0),
|
||||||
|
Corner::BotRight => vec2(1.0, 1.0),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::{
|
|||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
any::{Any, TypeId},
|
||||||
ops::{Index, IndexMut},
|
ops::{Index, IndexMut},
|
||||||
|
sync::mpsc::{Receiver, Sender, channel},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Ui<Ctx> {
|
pub struct Ui<Ctx> {
|
||||||
@@ -13,6 +14,8 @@ pub struct Ui<Ctx> {
|
|||||||
base: Option<WidgetId>,
|
base: Option<WidgetId>,
|
||||||
widgets: Widgets<Ctx>,
|
widgets: Widgets<Ctx>,
|
||||||
updates: Vec<WidgetId>,
|
updates: Vec<WidgetId>,
|
||||||
|
del_recv: Receiver<Id>,
|
||||||
|
del_send: Sender<Id>,
|
||||||
pub(super) active_sensors: ActiveSensors,
|
pub(super) active_sensors: ActiveSensors,
|
||||||
pub(super) sensor_map: SensorMap<Ctx>,
|
pub(super) sensor_map: SensorMap<Ctx>,
|
||||||
primitives: Primitives,
|
primitives: Primitives,
|
||||||
@@ -35,9 +38,9 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
pub fn push<W: Widget<Ctx>>(&mut self, w: W) -> WidgetId<W> {
|
||||||
let id = self.ids.next();
|
let id = self.id();
|
||||||
self.widgets.insert(id.duplicate(), w);
|
self.widgets.insert(id.id.duplicate(), w);
|
||||||
WidgetId::new(id, TypeId::of::<W>())
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set<W: Widget<Ctx>>(&mut self, i: &WidgetId<W>, w: W) {
|
pub fn set<W: Widget<Ctx>>(&mut self, i: &WidgetId<W>, w: W) {
|
||||||
@@ -65,7 +68,7 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
pub fn id<W: Widget<Ctx>>(&mut self) -> WidgetId<W> {
|
||||||
WidgetId::new(self.ids.next(), TypeId::of::<W>())
|
WidgetId::new(self.ids.next(), TypeId::of::<W>(), self.del_send.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
pub fn redraw_all(&mut self, ctx: &mut Ctx)
|
||||||
@@ -89,6 +92,10 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
where
|
where
|
||||||
Ctx: 'static,
|
Ctx: 'static,
|
||||||
{
|
{
|
||||||
|
while let Ok(id) = self.del_recv.try_recv() {
|
||||||
|
self.widgets.delete(id);
|
||||||
|
}
|
||||||
|
|
||||||
if self.full_redraw {
|
if self.full_redraw {
|
||||||
self.redraw_all(ctx);
|
self.redraw_all(ctx);
|
||||||
self.full_redraw = false;
|
self.full_redraw = false;
|
||||||
@@ -105,6 +112,10 @@ impl<Ctx> Ui<Ctx> {
|
|||||||
pub fn needs_redraw(&self) -> bool {
|
pub fn needs_redraw(&self) -> bool {
|
||||||
self.full_redraw || !self.updates.is_empty()
|
self.full_redraw || !self.updates.is_empty()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn num_widgets(&self) -> usize {
|
||||||
|
self.widgets.len()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
impl<W: Widget<Ctx>, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
|
||||||
@@ -146,6 +157,18 @@ impl<Ctx> Widgets<Ctx> {
|
|||||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
|
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget<Ctx>>) {
|
||||||
self.0.insert(id, widget);
|
self.0.insert(id, widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete(&mut self, id: Id) {
|
||||||
|
self.0.remove(&id);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.0.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.0.is_empty()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> dyn Widget<Ctx> {
|
impl<Ctx> dyn Widget<Ctx> {
|
||||||
@@ -160,6 +183,7 @@ impl<Ctx> dyn Widget<Ctx> {
|
|||||||
|
|
||||||
impl<Ctx: 'static> Default for Ui<Ctx> {
|
impl<Ctx: 'static> Default for Ui<Ctx> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
let (del_send, del_recv) = channel();
|
||||||
Self {
|
Self {
|
||||||
ids: Default::default(),
|
ids: Default::default(),
|
||||||
base: Default::default(),
|
base: Default::default(),
|
||||||
@@ -169,6 +193,8 @@ impl<Ctx: 'static> Default for Ui<Ctx> {
|
|||||||
full_redraw: false,
|
full_redraw: false,
|
||||||
active_sensors: Default::default(),
|
active_sensors: Default::default(),
|
||||||
sensor_map: Default::default(),
|
sensor_map: Default::default(),
|
||||||
|
del_send,
|
||||||
|
del_recv,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,72 +1,12 @@
|
|||||||
use std::{
|
use crate::{Painter, Ui, WidgetId, WidgetIdFnRet};
|
||||||
any::{Any, TypeId},
|
use std::{any::Any, marker::PhantomData};
|
||||||
marker::PhantomData,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{Painter, Ui, util::Id};
|
|
||||||
|
|
||||||
pub trait Widget<Ctx>: Any {
|
pub trait Widget<Ctx>: Any {
|
||||||
fn draw(&self, painter: &mut Painter<Ctx>);
|
fn draw(&self, painter: &mut Painter<Ctx>);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct AnyWidget;
|
|
||||||
|
|
||||||
/// An identifier for a widget.
|
|
||||||
/// Can index a UI to get the associated widget.
|
|
||||||
///
|
|
||||||
/// W does not need to implement widget so that AnyWidget is valid;
|
|
||||||
/// Instead, add generic bounds on methods that take an ID if they need specific data.
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Eq, Hash, PartialEq)]
|
|
||||||
pub struct WidgetId<W = AnyWidget> {
|
|
||||||
pub(super) ty: TypeId,
|
|
||||||
pub(super) id: Id,
|
|
||||||
_pd: PhantomData<W>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W> std::fmt::Debug for WidgetId<W> {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
self.id.fmt(f)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: temp
|
|
||||||
impl<W> Clone for WidgetId<W> {
|
|
||||||
fn clone(&self) -> Self {
|
|
||||||
Self::new(self.id.duplicate(), self.ty)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W> WidgetId<W> {
|
|
||||||
pub(super) fn new(id: Id, ty: TypeId) -> Self {
|
|
||||||
Self {
|
|
||||||
ty,
|
|
||||||
id,
|
|
||||||
_pd: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn erase_type(self) -> WidgetId<AnyWidget> {
|
|
||||||
self.cast_type()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
|
|
||||||
// safety: self is repr(C) and generic only used for phantom data
|
|
||||||
unsafe { std::mem::transmute(self) }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn cast_type<W2>(self) -> WidgetId<W2> {
|
|
||||||
WidgetId {
|
|
||||||
ty: self.ty,
|
|
||||||
id: self.id,
|
|
||||||
_pd: PhantomData,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WidgetTag;
|
pub struct WidgetTag;
|
||||||
pub struct FnTag;
|
pub struct FnTag;
|
||||||
pub struct IdTag;
|
|
||||||
|
|
||||||
pub trait WidgetLike<Ctx, Tag> {
|
pub trait WidgetLike<Ctx, Tag> {
|
||||||
type Widget: 'static;
|
type Widget: 'static;
|
||||||
@@ -85,63 +25,18 @@ pub trait WidgetLike<Ctx, 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<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
|
// pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
|
||||||
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
|
|
||||||
|
|
||||||
// copium for rust analyzer
|
/// 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
|
||||||
|
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
|
||||||
macro_rules! WidgetFnRet {
|
macro_rules! WidgetFnRet {
|
||||||
($W:ty, $Ctx:ty) => {
|
($W:ty, $Ctx:ty) => {
|
||||||
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $W
|
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $W
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
pub(crate) use WidgetFnRet;
|
pub(crate) use WidgetFnRet;
|
||||||
macro_rules! WidgetIdFnRet {
|
|
||||||
($W:ty, $Ctx:ty) => {
|
|
||||||
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W>
|
|
||||||
};
|
|
||||||
($W:ty, $Ctx:ty, $($use:tt)*) => {
|
|
||||||
impl FnOnce(&mut $crate::Ui<$Ctx>) -> $crate::WidgetId<$W> + use<$($use)*>
|
|
||||||
};
|
|
||||||
}
|
|
||||||
pub(crate) use WidgetIdFnRet;
|
|
||||||
|
|
||||||
pub trait Idable<Ctx, Tag> {
|
|
||||||
type Widget: Widget<Ctx>;
|
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
|
|
||||||
fn id<'a>(
|
|
||||||
self,
|
|
||||||
id: &WidgetId<Self::Widget>,
|
|
||||||
) -> WidgetIdFnRet!(Self::Widget, Ctx, 'a, Self, Ctx, Tag)
|
|
||||||
where
|
|
||||||
Self: Sized,
|
|
||||||
{
|
|
||||||
let id = id.clone();
|
|
||||||
move |ui| {
|
|
||||||
self.set(ui, &id);
|
|
||||||
id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
|
|
||||||
type Widget = W;
|
|
||||||
|
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
|
||||||
ui.set(id, self);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
|
|
||||||
type Widget = W;
|
|
||||||
|
|
||||||
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
|
|
||||||
let w = self(ui);
|
|
||||||
ui.set(id, w);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
|
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
@@ -151,13 +46,6 @@ impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> f
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
|
|
||||||
type Widget = W;
|
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
||||||
self(ui)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
|
||||||
@@ -165,13 +53,6 @@ impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: 'static, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
|
|
||||||
type Widget = W;
|
|
||||||
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
|
|
||||||
self
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct WidgetArr<const LEN: usize, Ws> {
|
pub struct WidgetArr<const LEN: usize, Ws> {
|
||||||
pub arr: [WidgetId; LEN],
|
pub arr: [WidgetId; LEN],
|
||||||
_pd: PhantomData<Ws>,
|
_pd: PhantomData<Ws>,
|
||||||
|
|||||||
@@ -22,13 +22,13 @@ pub struct Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub struct UiIds {
|
pub struct UiIds {
|
||||||
test: WidgetId<Span>,
|
span_add: WidgetId<Span>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
pub fn create_ui() -> (Ui<Self>, UiIds) {
|
pub fn create_ui() -> (Ui<Self>, UiIds) {
|
||||||
let mut ui = Ui::new();
|
let mut ui = Ui::new();
|
||||||
let test = ui.id();
|
let span_add = ui.id();
|
||||||
let rect = Rect {
|
let rect = Rect {
|
||||||
color: UiColor::WHITE,
|
color: UiColor::WHITE,
|
||||||
radius: 20.0,
|
radius: 20.0,
|
||||||
@@ -73,7 +73,7 @@ impl Client {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
let span_add_test = ui.add(Span::empty(Dir::RIGHT).id(&test));
|
let span_add_test = ui.add(Span::empty(Dir::RIGHT).id(&span_add));
|
||||||
let main: WidgetId<Regioned> = ui.id();
|
let main: WidgetId<Regioned> = ui.id();
|
||||||
|
|
||||||
fn switch_button<To>(
|
fn switch_button<To>(
|
||||||
@@ -100,7 +100,7 @@ impl Client {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
let buttons = ui.add(
|
let tabs = ui.add(
|
||||||
(
|
(
|
||||||
switch_button(UiColor::RED, &main, &pad_test),
|
switch_button(UiColor::RED, &main, &pad_test),
|
||||||
switch_button(UiColor::GREEN, &main, &span_test),
|
switch_button(UiColor::GREEN, &main, &span_test),
|
||||||
@@ -108,22 +108,36 @@ impl Client {
|
|||||||
)
|
)
|
||||||
.span(Dir::RIGHT, [1, 1, 1]),
|
.span(Dir::RIGHT, [1, 1, 1]),
|
||||||
);
|
);
|
||||||
|
let test_button = Rect::new(Color::PURPLE)
|
||||||
|
.radius(30)
|
||||||
|
.on(Sense::PressStart, move |ui, _| {
|
||||||
|
println!("{}", ui.num_widgets());
|
||||||
|
})
|
||||||
|
.region(
|
||||||
|
UiRegion::corner(Corner::BotRight)
|
||||||
|
.size((150, 150))
|
||||||
|
.shifted((-75, -75)),
|
||||||
|
);
|
||||||
|
|
||||||
|
let s = span_add.clone();
|
||||||
|
let del_button = Rect::new(Color::RED)
|
||||||
|
.radius(30)
|
||||||
|
.on(Sense::PressStart, move |ui, _| {
|
||||||
|
ui[&s].children.pop();
|
||||||
|
})
|
||||||
|
.region(
|
||||||
|
UiRegion::corner(Corner::BotLeft)
|
||||||
|
.size((150, 150))
|
||||||
|
.shifted((75, -75)),
|
||||||
|
);
|
||||||
ui.set_base(
|
ui.set_base(
|
||||||
(
|
(
|
||||||
buttons,
|
tabs,
|
||||||
(
|
(pad_test.pad(10).id(&main), test_button, del_button).stack(),
|
||||||
pad_test.pad(10).id(&main),
|
|
||||||
Rect::new(Color::PURPLE).radius(30).region(
|
|
||||||
UiRegion::bottom_right()
|
|
||||||
.size((150, 150))
|
|
||||||
.shifted((-75, -75)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.stack(),
|
|
||||||
)
|
)
|
||||||
.span(Dir::DOWN, [fixed(40), ratio(1)]),
|
.span(Dir::DOWN, [fixed(40), ratio(1)]),
|
||||||
);
|
);
|
||||||
(ui, UiIds { test })
|
(ui, UiIds { span_add })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
|
pub fn new(window: Arc<Window>, ui: UiIds) -> Self {
|
||||||
@@ -151,7 +165,7 @@ impl Client {
|
|||||||
WindowEvent::KeyboardInput { event, .. } => {
|
WindowEvent::KeyboardInput { event, .. } => {
|
||||||
if event.state.is_pressed() {
|
if event.state.is_pressed() {
|
||||||
let child = ui.add(Rect::new(Color::YELLOW)).erase_type();
|
let child = ui.add(Rect::new(Color::YELLOW)).erase_type();
|
||||||
ui[&self.ui.test].children.push((child, fixed(20.0)));
|
ui[&self.ui.span_add].children.push((child, fixed(20.0)));
|
||||||
self.renderer.window().request_redraw();
|
self.renderer.window().request_redraw();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user