RE ADD CONTEXT
This commit is contained in:
@@ -1,16 +1,19 @@
|
||||
use crate::{Ui, WidgetIdFn, WidgetLike, WidgetRef};
|
||||
|
||||
pub trait WidgetAttr<W: ?Sized> {
|
||||
pub trait WidgetAttr<State, W: ?Sized> {
|
||||
type Input;
|
||||
fn run(ui: &mut Ui, id: WidgetRef<W>, input: Self::Input);
|
||||
fn run(ui: &mut Ui<State>, id: WidgetRef<State, W>, input: Self::Input);
|
||||
}
|
||||
|
||||
pub trait Attrable<W: ?Sized, Tag> {
|
||||
fn attr<A: WidgetAttr<W>>(self, input: A::Input) -> impl WidgetIdFn<W>;
|
||||
pub trait Attrable<State, W: ?Sized, Tag> {
|
||||
fn attr<A: WidgetAttr<State, W>>(self, input: A::Input) -> impl WidgetIdFn<State, W>;
|
||||
}
|
||||
|
||||
impl<WL: WidgetLike<Tag>, Tag> Attrable<WL::Widget, Tag> for WL {
|
||||
fn attr<A: WidgetAttr<WL::Widget>>(self, input: A::Input) -> impl WidgetIdFn<WL::Widget> {
|
||||
impl<State, WL: WidgetLike<State, Tag>, Tag> Attrable<State, WL::Widget, Tag> for WL {
|
||||
fn attr<A: WidgetAttr<State, WL::Widget>>(
|
||||
self,
|
||||
input: A::Input,
|
||||
) -> impl WidgetIdFn<State, WL::Widget> {
|
||||
|ui| {
|
||||
let id = self.add(ui);
|
||||
A::run(ui, id.weak(), input);
|
||||
|
||||
@@ -1,36 +1,34 @@
|
||||
use std::ops::{Index, IndexMut};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::{Ui, Widget, WidgetRef};
|
||||
use crate::{HasUi, Ui, Widget, WidgetRef};
|
||||
|
||||
pub struct EventCtx<'a, Ctx, Data> {
|
||||
pub ui: &'a mut Ui,
|
||||
pub state: &'a mut Ctx,
|
||||
pub struct EventCtx<'a, State, Data> {
|
||||
pub state: &'a mut State,
|
||||
pub data: &'a mut Data,
|
||||
}
|
||||
|
||||
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
||||
pub widget: WidgetRef<W>,
|
||||
pub ui: &'a mut Ui,
|
||||
pub state: &'a mut Ctx,
|
||||
pub struct EventIdCtx<'a, State, Data, W: ?Sized> {
|
||||
pub widget: WidgetRef<State, W>,
|
||||
pub state: &'a mut State,
|
||||
pub data: &'a mut Data,
|
||||
}
|
||||
|
||||
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
||||
type Output = W;
|
||||
impl<State: HasUi, Data, W: ?Sized> Deref for EventIdCtx<'_, State, Data, W> {
|
||||
type Target = Ui<State>;
|
||||
|
||||
fn index(&self, index: WidgetRef<W>) -> &Self::Output {
|
||||
&self.ui[index]
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.state.ui_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Ctx, Data, W2, W: Widget> IndexMut<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
||||
fn index_mut(&mut self, index: WidgetRef<W>) -> &mut Self::Output {
|
||||
&mut self.ui[index]
|
||||
impl<State: HasUi, Data, W: ?Sized> DerefMut for EventIdCtx<'_, State, Data, W> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.state.ui()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Ctx, Data, W: Widget> EventIdCtx<'a, Ctx, Data, W> {
|
||||
impl<'a, State: HasUi + 'static, Data, W: Widget<State>> EventIdCtx<'a, State, Data, W> {
|
||||
pub fn widget(&mut self) -> &mut W {
|
||||
&mut self.ui[self.widget]
|
||||
&mut self.state.ui()[self.widget]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,30 +1,37 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, IdLike, LayerId, Ui, WidgetData, WidgetId,
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasUi, IdLike, LayerId, WidgetData, WidgetId,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct EventManager {
|
||||
types: TypeMap<dyn EventManagerLike>,
|
||||
pub struct EventManager<State> {
|
||||
types: TypeMap<dyn EventManagerLike<State>>,
|
||||
}
|
||||
|
||||
impl EventManager {
|
||||
pub fn get_type<E: EventLike, Ctx: 'static>(&mut self) -> &mut TypeEventManager<E::Event, Ctx> {
|
||||
impl<State> Default for EventManager<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
types: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> EventManager<State> {
|
||||
pub fn get_type<E: EventLike>(&mut self) -> &mut TypeEventManager<State, E::Event> {
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike, E: EventLike, Ctx: 'static>(
|
||||
pub fn register<I: IdLike<State>, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.get_type::<E, Ctx>().register(id, event, f);
|
||||
self.get_type::<E>().register(id, event, f);
|
||||
}
|
||||
|
||||
pub fn type_key<E: EventLike, Ctx: 'static>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<E::Event, Ctx>>()
|
||||
pub fn type_key<E: EventLike>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<State, E::Event>>()
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: WidgetId) {
|
||||
@@ -33,33 +40,33 @@ impl EventManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
pub fn draw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().draw(active);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
pub fn undraw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().undraw(active);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EventManagerLike {
|
||||
pub trait EventManagerLike<State> {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &ActiveData);
|
||||
fn undraw(&mut self, data: &ActiveData);
|
||||
}
|
||||
|
||||
type EventData<E, Ctx> = (E, Rc<dyn for<'a> EventFn<Ctx, <E as Event>::Data<'a>>>);
|
||||
pub struct TypeEventManager<E: Event, Ctx> {
|
||||
type EventData<State, E> = (E, Rc<dyn for<'a> EventFn<State, <E as Event>::Data<'a>>>);
|
||||
pub struct TypeEventManager<State, E: Event> {
|
||||
// TODO: reduce visiblity!!
|
||||
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
||||
map: HashMap<WidgetId, Vec<EventData<E, Ctx>>>,
|
||||
map: HashMap<WidgetId, Vec<EventData<State, E>>>,
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
|
||||
impl<State, E: Event> EventManagerLike<State> for TypeEventManager<State, E> {
|
||||
fn remove(&mut self, id: WidgetId) {
|
||||
self.map.remove(&id);
|
||||
for layer in self.active.values_mut() {
|
||||
@@ -80,7 +87,7 @@ impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
|
||||
impl<State, E: Event> Default for TypeEventManager<State, E> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
active: Default::default(),
|
||||
@@ -89,12 +96,12 @@ impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
|
||||
impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
fn register(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
id: impl IdLike<State>,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<Ctx, E::Data<'a>>,
|
||||
f: impl for<'a> EventFn<State, E::Data<'a>>,
|
||||
) {
|
||||
let event = event.into_event();
|
||||
self.map
|
||||
@@ -105,14 +112,13 @@ impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<Ctx, E::Data<'b>>) + 'a {
|
||||
id: impl IdLike<State>,
|
||||
) -> impl for<'b> FnOnce(EventCtx<State, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
for (e, f) in fs {
|
||||
if e.should_run(ctx.data) {
|
||||
f(EventCtx {
|
||||
ui: ctx.ui,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
})
|
||||
@@ -122,18 +128,21 @@ impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn run_event<E: EventLike, Ctx: 'static>(
|
||||
pub trait HasEvents: Sized + HasUi {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
ctx: &mut Ctx,
|
||||
id: impl IdLike,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
);
|
||||
}
|
||||
|
||||
impl<State: HasUi + 'static> HasEvents for State {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.data.events.get_type::<E, Ctx>().run_fn(id);
|
||||
f(EventCtx {
|
||||
ui: self,
|
||||
state: ctx,
|
||||
data,
|
||||
})
|
||||
let f = self.ui().data.events.get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,11 +26,11 @@ impl<E: Event> EventLike for E {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + 'static {}
|
||||
impl<F: Fn(EventCtx<Ctx, Data>) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||
pub trait EventFn<State, Data>: Fn(EventCtx<State, Data>) + 'static {}
|
||||
impl<State, F: Fn(EventCtx<State, Data>) + 'static, Data> EventFn<State, Data> for F {}
|
||||
|
||||
pub trait WidgetEventFn<Ctx, Data, W: ?Sized>: Fn(EventIdCtx<Ctx, Data, W>) + 'static {}
|
||||
impl<F: Fn(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEventFn<Ctx, Data, W>
|
||||
for F
|
||||
pub trait WidgetEventFn<State, Data, W: ?Sized>: Fn(EventIdCtx<State, Data, W>) + 'static {}
|
||||
impl<State, F: Fn(EventIdCtx<State, Data, W>) + 'static, Data, W: ?Sized>
|
||||
WidgetEventFn<State, Data, W> for F
|
||||
{
|
||||
}
|
||||
|
||||
@@ -6,8 +6,8 @@ use crate::{
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'c> {
|
||||
ctx: &'a mut PainterCtx<'c>,
|
||||
pub struct Painter<'a, 'c, State> {
|
||||
ctx: &'a mut PainterCtx<'c, State>,
|
||||
region: UiRegion,
|
||||
mask: MaskIdx,
|
||||
textures: Vec<TextureHandle>,
|
||||
@@ -20,15 +20,15 @@ pub struct Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
/// context for a painter; lets you draw and redraw widgets
|
||||
struct PainterCtx<'a> {
|
||||
pub widgets: &'a Widgets,
|
||||
struct PainterCtx<'a, State> {
|
||||
pub widgets: &'a Widgets<State>,
|
||||
pub active: &'a mut HashMap<WidgetId, ActiveData>,
|
||||
pub layers: &'a mut PrimitiveLayers,
|
||||
pub textures: &'a mut Textures,
|
||||
pub masks: &'a mut TrackedArena<Mask, u32>,
|
||||
pub text: &'a mut TextData,
|
||||
pub output_size: Vec2,
|
||||
pub events: &'a mut EventManager,
|
||||
pub events: &'a mut EventManager<State>,
|
||||
pub cache_width: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
pub cache_height: HashMap<WidgetId, (UiVec2, Len)>,
|
||||
pub needs_redraw: &'a mut HashSet<WidgetId>,
|
||||
@@ -58,20 +58,35 @@ pub struct ActiveData {
|
||||
}
|
||||
|
||||
/// data to be stored in Ui to create PainterCtxs easily
|
||||
#[derive(Default)]
|
||||
pub struct PainterData {
|
||||
pub widgets: Widgets,
|
||||
pub struct PainterData<State> {
|
||||
pub widgets: Widgets<State>,
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
pub layers: PrimitiveLayers,
|
||||
pub textures: Textures,
|
||||
pub text: TextData,
|
||||
pub output_size: Vec2,
|
||||
pub events: EventManager,
|
||||
pub events: EventManager<State>,
|
||||
pub px_dependent: HashSet<WidgetId>,
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
}
|
||||
|
||||
impl<'a> PainterCtx<'a> {
|
||||
impl<State> Default for PainterData<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
widgets: Default::default(),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
textures: Default::default(),
|
||||
text: Default::default(),
|
||||
output_size: Default::default(),
|
||||
events: Default::default(),
|
||||
px_dependent: Default::default(),
|
||||
masks: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, State: 'static> PainterCtx<'a, State> {
|
||||
/// redraws a widget that's currently active (drawn)
|
||||
/// can be called on something already drawn or removed,
|
||||
/// will just return if so
|
||||
@@ -317,8 +332,8 @@ impl<'a> PainterCtx<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
impl PainterData {
|
||||
fn ctx<'a>(&'a mut self, needs_redraw: &'a mut HashSet<WidgetId>) -> PainterCtx<'a> {
|
||||
impl<State: 'static> PainterData<State> {
|
||||
fn ctx<'a>(&'a mut self, needs_redraw: &'a mut HashSet<WidgetId>) -> PainterCtx<'a, State> {
|
||||
PainterCtx {
|
||||
widgets: &self.widgets,
|
||||
active: &mut self.active,
|
||||
@@ -351,7 +366,7 @@ impl PainterData {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.ctx.layers.write(
|
||||
self.layer,
|
||||
@@ -384,17 +399,17 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
}
|
||||
|
||||
/// 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: &WidgetHandle<State, 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: &WidgetHandle<State, 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: &WidgetHandle<State, W>, region: UiRegion) {
|
||||
self.children.push(id.id());
|
||||
self.ctx
|
||||
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
|
||||
@@ -424,18 +439,18 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
self.region
|
||||
}
|
||||
|
||||
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<W>) -> Size {
|
||||
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Size {
|
||||
self.size_ctx().size(id)
|
||||
}
|
||||
|
||||
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<W>, axis: Axis) -> Len {
|
||||
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.size_ctx().width(id),
|
||||
Axis::Y => self.size_ctx().height(id),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
|
||||
SizeCtx {
|
||||
text: self.ctx.text,
|
||||
textures: self.ctx.textures,
|
||||
@@ -480,11 +495,11 @@ impl<'a, 'c> Painter<'a, 'c> {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SizeCtx<'a> {
|
||||
pub struct SizeCtx<'a, State> {
|
||||
pub text: &'a mut TextData,
|
||||
pub textures: &'a mut Textures,
|
||||
source: WidgetId,
|
||||
widgets: &'a Widgets,
|
||||
widgets: &'a Widgets<State>,
|
||||
cache_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
|
||||
cache_height: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
|
||||
checked_width: &'a mut HashMap<WidgetId, (UiVec2, Len)>,
|
||||
@@ -495,7 +510,7 @@ pub struct SizeCtx<'a> {
|
||||
id: WidgetId,
|
||||
}
|
||||
|
||||
impl SizeCtx<'_> {
|
||||
impl<State: 'static> SizeCtx<'_, State> {
|
||||
pub fn id(&self) -> &WidgetId {
|
||||
&self.id
|
||||
}
|
||||
@@ -547,22 +562,22 @@ impl SizeCtx<'_> {
|
||||
len
|
||||
}
|
||||
|
||||
pub fn width<W: ?Sized>(&mut self, id: &WidgetHandle<W>) -> Len {
|
||||
pub fn width<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
|
||||
self.width_inner(id.id())
|
||||
}
|
||||
|
||||
pub fn height<W: ?Sized>(&mut self, id: &WidgetHandle<W>) -> Len {
|
||||
pub fn height<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Len {
|
||||
self.height_inner(id.id())
|
||||
}
|
||||
|
||||
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<W>, axis: Axis) -> Len {
|
||||
pub fn len_axis<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.width(id),
|
||||
Axis::Y => self.height(id),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<W>) -> Size {
|
||||
pub fn size<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) -> Size {
|
||||
Size {
|
||||
x: self.width(id),
|
||||
y: self.height(id),
|
||||
|
||||
@@ -59,7 +59,7 @@ impl UiRenderNode {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
||||
pub fn update<State>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<State>) {
|
||||
self.active.clear();
|
||||
for (i, primitives) in ui.data.layers.iter_mut() {
|
||||
self.active.push(i);
|
||||
|
||||
@@ -8,35 +8,43 @@ use std::{
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
pub struct Ui {
|
||||
pub struct Ui<State> {
|
||||
// TODO: make this at least pub(super)
|
||||
pub data: PainterData,
|
||||
root: Option<WidgetHandle>,
|
||||
pub data: PainterData<State>,
|
||||
root: Option<WidgetHandle<State>>,
|
||||
recv: Receiver<WidgetId>,
|
||||
pub(super) send: Sender<WidgetId>,
|
||||
full_redraw: bool,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetHandle<W> {
|
||||
pub trait HasUi: Sized {
|
||||
fn ui_ref(&self) -> &Ui<Self>;
|
||||
fn ui(&mut self) -> &mut Ui<Self>;
|
||||
}
|
||||
|
||||
impl<State: 'static> Ui<State> {
|
||||
pub fn add<W: Widget<State>, Tag>(
|
||||
&mut self,
|
||||
w: impl WidgetLike<State, Tag, Widget = W>,
|
||||
) -> WidgetHandle<State, W> {
|
||||
w.add(self)
|
||||
}
|
||||
|
||||
/// useful for debugging
|
||||
pub fn set_label(&mut self, id: impl IdLike, label: String) {
|
||||
pub fn set_label(&mut self, id: impl IdLike<State>, label: String) {
|
||||
self.data.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
&self.data.widgets.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetHandle<W> {
|
||||
pub fn add_widget<W: Widget<State>>(&mut self, w: W) -> WidgetHandle<State, W> {
|
||||
WidgetHandle::new(self.data.widgets.add(w), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<State, Tag>) {
|
||||
self.root = Some(w.add(self));
|
||||
self.full_redraw = true;
|
||||
}
|
||||
@@ -45,14 +53,14 @@ impl Ui {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.data.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -63,11 +71,11 @@ impl Ui {
|
||||
self.data.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn register_event<E: EventLike, Ctx: 'static>(
|
||||
pub fn register_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
id: impl IdLike<State>,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.data.events.register(id.id(), event, f);
|
||||
self.data
|
||||
@@ -75,7 +83,7 @@ impl Ui {
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::type_key::<E, Ctx>());
|
||||
.insert(EventManager::<State>::type_key::<E>());
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
@@ -148,7 +156,7 @@ impl Ui {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
pub fn window_region(&self, id: &impl IdLike<State>) -> Option<PixelRegion> {
|
||||
let region = self.data.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.data.output_size))
|
||||
}
|
||||
@@ -161,7 +169,7 @@ impl Ui {
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> Index<I> for Ui
|
||||
impl<State: 'static, I: IdLike<State>> Index<I> for Ui<State>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -172,7 +180,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<I: IdLike> IndexMut<I> for Ui
|
||||
impl<State: 'static, I: IdLike<State>> IndexMut<I> for Ui<State>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -181,7 +189,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Ui {
|
||||
impl<State: 'static> Default for Ui<State> {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
|
||||
@@ -2,16 +2,16 @@ use std::any::TypeId;
|
||||
|
||||
use crate::{Widget, util::HashSet};
|
||||
|
||||
pub struct WidgetData {
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub struct WidgetData<State> {
|
||||
pub widget: Box<dyn Widget<State>>,
|
||||
pub label: String,
|
||||
pub event_mgrs: HashSet<TypeId>,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
}
|
||||
|
||||
impl WidgetData {
|
||||
pub fn new<W: Widget>(widget: W) -> Self {
|
||||
impl<State> WidgetData<State> {
|
||||
pub fn new<W: Widget<State>>(widget: W) -> Self {
|
||||
let mut label = std::any::type_name::<W>().to_string();
|
||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
|
||||
use std::{
|
||||
marker::{PhantomData, Unsize},
|
||||
mem::MaybeUninit,
|
||||
ops::CoerceUnsized,
|
||||
sync::mpsc::Sender,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
Ui, Widget,
|
||||
@@ -12,43 +17,46 @@ pub type WidgetId = SlotId;
|
||||
/// a signal is sent to the owning UI to clean up the resources.
|
||||
///
|
||||
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
|
||||
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
|
||||
pub struct WidgetHandle<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub(super) id: WidgetId,
|
||||
counter: RefCounter,
|
||||
send: Sender<WidgetId>,
|
||||
ty: *const W,
|
||||
state: PhantomData<State>,
|
||||
}
|
||||
|
||||
/// A weak handle to a widget.
|
||||
/// Will not keep it alive, but can still be used for indexing like WidgetHandle.
|
||||
pub struct WidgetRef<W: ?Sized = dyn Widget> {
|
||||
pub struct WidgetRef<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub(super) id: WidgetId,
|
||||
#[allow(unused)]
|
||||
ty: *const W,
|
||||
state: PhantomData<State>,
|
||||
}
|
||||
|
||||
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
|
||||
pub h: WidgetHandle<W>,
|
||||
pub r: WidgetRef<W>,
|
||||
pub struct WidgetHandles<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub h: WidgetHandle<State, W>,
|
||||
pub r: WidgetRef<State, W>,
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
||||
impl<State, W: Widget<State> + ?Sized + Unsize<dyn Widget<State>>> WidgetHandle<State, W> {
|
||||
pub fn any(self) -> WidgetHandle<State, dyn Widget<State>> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetHandle<W> {
|
||||
impl<State, W: ?Sized> WidgetHandle<State, W> {
|
||||
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
ty: unsafe { MaybeUninit::zeroed().assume_init() },
|
||||
state: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_any(&self) -> &WidgetHandle<dyn Widget> {
|
||||
pub fn as_any(&self) -> &WidgetHandle<State, dyn Widget<State>> {
|
||||
// SAFETY: self is repr(C) and generic only used for phantom data
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
@@ -61,24 +69,28 @@ impl<W: ?Sized> WidgetHandle<W> {
|
||||
self.counter.refs()
|
||||
}
|
||||
|
||||
pub fn weak(&self) -> WidgetRef<W> {
|
||||
pub fn weak(&self) -> WidgetRef<State, W> {
|
||||
let Self { ty, id, .. } = *self;
|
||||
WidgetRef { ty, id }
|
||||
WidgetRef {
|
||||
ty,
|
||||
id,
|
||||
state: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handles(self) -> WidgetHandles<W> {
|
||||
pub fn handles(self) -> WidgetHandles<State, W> {
|
||||
let r = self.weak();
|
||||
WidgetHandles { h: self, r }
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetRef<W> {
|
||||
impl<State, W: ?Sized> WidgetRef<State, W> {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Drop for WidgetHandle<W> {
|
||||
impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(self.id);
|
||||
@@ -86,52 +98,64 @@ impl<W: ?Sized> Drop for WidgetHandle<W> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetIdFn<W: ?Sized = dyn Widget>: FnOnce(&mut Ui) -> WidgetHandle<W> {}
|
||||
impl<W: ?Sized, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetIdFn<W> for F {}
|
||||
pub trait WidgetIdFn<State, W: ?Sized = dyn Widget<State>>:
|
||||
FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>
|
||||
{
|
||||
}
|
||||
impl<State, W: ?Sized, F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>> WidgetIdFn<State, W>
|
||||
for F
|
||||
{
|
||||
}
|
||||
|
||||
pub trait IdLike {
|
||||
type Widget: Widget + ?Sized + 'static;
|
||||
pub trait IdLike<State> {
|
||||
type Widget: Widget<State> + ?Sized + 'static;
|
||||
fn id(&self) -> WidgetId;
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for &WidgetHandle<State, W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetHandle<State, W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetRef<State, W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl IdLike for WidgetId {
|
||||
type Widget = dyn Widget;
|
||||
impl<State: 'static> IdLike<State> for WidgetId {
|
||||
type Widget = dyn Widget<State>;
|
||||
fn id(&self) -> WidgetId {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized, State> CoerceUnsized<WidgetHandle<State, U>>
|
||||
for WidgetHandle<State, T>
|
||||
{
|
||||
}
|
||||
impl<State, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<State, U>>
|
||||
for WidgetRef<State, T>
|
||||
{
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Clone for WidgetRef<W> {
|
||||
impl<State, W: ?Sized> Clone for WidgetRef<State, W> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
impl<W: ?Sized> Copy for WidgetRef<W> {}
|
||||
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
||||
impl<State, W: ?Sized> Copy for WidgetRef<State, W> {}
|
||||
impl<State, W: ?Sized> PartialEq for WidgetRef<State, W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
|
||||
@@ -1,48 +1,48 @@
|
||||
use super::*;
|
||||
use std::marker::Unsize;
|
||||
|
||||
pub trait WidgetLike<Tag>: Sized {
|
||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
||||
pub trait WidgetLike<State: 'static, Tag>: Sized {
|
||||
type Widget: Widget<State> + ?Sized + Unsize<dyn Widget<State>> + 'static;
|
||||
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>;
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, Self::Widget>;
|
||||
|
||||
fn with_id<W2>(
|
||||
self,
|
||||
f: impl FnOnce(&mut Ui, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
|
||||
) -> impl WidgetIdFn<W2> {
|
||||
f: impl FnOnce(&mut Ui<State>, WidgetHandle<State, Self::Widget>) -> WidgetHandle<State, W2>,
|
||||
) -> impl WidgetIdFn<State, W2> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
f(ui, id)
|
||||
}
|
||||
}
|
||||
|
||||
fn set_root(self, ui: &mut Ui) {
|
||||
fn set_root(self, ui: &mut Ui<State>) {
|
||||
ui.set_root(self);
|
||||
}
|
||||
|
||||
fn handles(self, ui: &mut Ui) -> WidgetHandles<Self::Widget> {
|
||||
fn handles(self, ui: &mut Ui<State>) -> WidgetHandles<State, Self::Widget> {
|
||||
self.add(ui).handles()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetArrLike<const LEN: usize, Tag> {
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN>;
|
||||
pub trait WidgetArrLike<State, const LEN: usize, Tag> {
|
||||
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, LEN>;
|
||||
}
|
||||
|
||||
impl<const LEN: usize> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN> {
|
||||
fn ui(self, _: &mut Ui) -> WidgetArr<LEN> {
|
||||
impl<State, const LEN: usize> WidgetArrLike<State, LEN, ArrTag> for WidgetArr<State, LEN> {
|
||||
fn ui(self, _: &mut Ui<State>) -> WidgetArr<State, LEN> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
// I hate this language it's so bad why do I even use it
|
||||
// variadic generics please save us
|
||||
macro_rules! impl_widget_arr {
|
||||
($n:expr;$($W:ident)*) => {
|
||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||
};
|
||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
|
||||
fn ui(self, ui: &mut Ui) -> WidgetArr<$n> {
|
||||
impl<State: 'static, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
||||
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, $n> {
|
||||
#[allow(non_snake_case)]
|
||||
let ($($W,)*) = self;
|
||||
WidgetArr::new(
|
||||
|
||||
@@ -13,23 +13,23 @@ pub use like::*;
|
||||
pub use tag::*;
|
||||
pub use widgets::*;
|
||||
|
||||
pub trait Widget: Any {
|
||||
fn draw(&mut self, painter: &mut Painter);
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len;
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len;
|
||||
pub trait Widget<State>: Any {
|
||||
fn draw(&mut self, painter: &mut Painter<State>);
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
||||
}
|
||||
|
||||
impl Widget for () {
|
||||
fn draw(&mut self, _: &mut Painter) {}
|
||||
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
||||
impl<State> Widget<State> for () {
|
||||
fn draw(&mut self, _: &mut Painter<State>) {}
|
||||
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
|
||||
Len::ZERO
|
||||
}
|
||||
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
|
||||
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
|
||||
Len::ZERO
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Widget {
|
||||
impl<State> dyn Widget<State> {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
@@ -42,31 +42,31 @@ impl dyn Widget {
|
||||
/// 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 + ?Sized>: FnOnce(&mut Ui) -> W {}
|
||||
impl<W: Widget + ?Sized, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||
pub trait WidgetFn<State, W: Widget<State> + ?Sized>: FnOnce(&mut Ui<State>) -> W {}
|
||||
impl<State, W: Widget<State> + ?Sized, F: FnOnce(&mut Ui<State>) -> W> WidgetFn<State, W> for F {}
|
||||
|
||||
pub struct WidgetArr<const LEN: usize> {
|
||||
pub arr: [WidgetHandle; LEN],
|
||||
pub struct WidgetArr<State, const LEN: usize> {
|
||||
pub arr: [WidgetHandle<State>; LEN],
|
||||
}
|
||||
|
||||
impl<const LEN: usize> WidgetArr<LEN> {
|
||||
pub fn new(arr: [WidgetHandle; LEN]) -> Self {
|
||||
impl<State, const LEN: usize> WidgetArr<State, LEN> {
|
||||
pub fn new(arr: [WidgetHandle<State>; LEN]) -> Self {
|
||||
Self { arr }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetOption {
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetHandle>;
|
||||
pub trait WidgetOption<State> {
|
||||
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>>;
|
||||
}
|
||||
|
||||
impl WidgetOption for () {
|
||||
fn get(self, _: &mut Ui) -> Option<WidgetHandle> {
|
||||
impl<State> WidgetOption<State> for () {
|
||||
fn get(self, _: &mut Ui<State>) -> Option<WidgetHandle<State>> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce(&mut Ui) -> Option<WidgetHandle>> WidgetOption for F {
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetHandle> {
|
||||
impl<State, F: FnOnce(&mut Ui<State>) -> Option<WidgetHandle<State>>> WidgetOption<State> for F {
|
||||
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,35 +5,42 @@ use super::*;
|
||||
pub struct ArrTag;
|
||||
|
||||
pub struct WidgetTag;
|
||||
impl<W: Widget> WidgetLike<WidgetTag> for W {
|
||||
impl<State: 'static, W: Widget<State>> WidgetLike<State, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
ui.add_widget(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnTag;
|
||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||
impl<State: 'static, W: Widget<State>, F: FnOnce(&mut Ui<State>) -> W> WidgetLike<State, FnTag>
|
||||
for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
self(ui).add(ui)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdTag;
|
||||
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static> WidgetLike<IdTag> for WidgetHandle<W> {
|
||||
impl<State: 'static, W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static>
|
||||
WidgetLike<State, IdTag> for WidgetHandle<State, W>
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui) -> WidgetHandle<W> {
|
||||
fn add(self, _: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdFnTag;
|
||||
impl<W: ?Sized + Widget + Unsize<dyn Widget> + 'static, F: FnOnce(&mut Ui) -> WidgetHandle<W>>
|
||||
WidgetLike<IdFnTag> for F
|
||||
impl<
|
||||
State: 'static,
|
||||
W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static,
|
||||
F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>,
|
||||
> WidgetLike<State, IdFnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,34 +3,44 @@ use crate::{
|
||||
util::{DynBorrower, HashSet, SlotVec},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets {
|
||||
pub struct Widgets<State> {
|
||||
pub updates: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData>,
|
||||
vec: SlotVec<WidgetData<State>>,
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
impl<State> Default for Widgets<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
updates: Default::default(),
|
||||
vec: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Widgets<State> {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.updates.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget> {
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
|
||||
Some(self.vec.get(id)?.widget.as_ref())
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget<State>> {
|
||||
self.updates.insert(id);
|
||||
Some(self.vec.get_mut(id)?.widget.as_mut())
|
||||
}
|
||||
|
||||
/// get_dyn but dynamic borrow checking of widgets
|
||||
/// lets you do recursive (tree) operations, like the painter does
|
||||
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
|
||||
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_, State> {
|
||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
#[allow(mutable_transmutes)]
|
||||
let data = unsafe {
|
||||
std::mem::transmute::<&WidgetData, &mut WidgetData>(self.vec.get(id).unwrap())
|
||||
std::mem::transmute::<&WidgetData<State>, &mut WidgetData<State>>(
|
||||
self.vec.get(id).unwrap(),
|
||||
)
|
||||
};
|
||||
if data.borrowed {
|
||||
panic!("tried to mutably borrow the same widget twice");
|
||||
@@ -38,37 +48,37 @@ impl Widgets {
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.get_dyn(id.id())?.as_any().downcast_ref()
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut()
|
||||
}
|
||||
|
||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
pub fn add<W: Widget<State>>(&mut self, widget: W) -> WidgetId {
|
||||
self.vec.add(WidgetData::new(widget))
|
||||
}
|
||||
|
||||
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||
pub fn data(&self, id: impl IdLike<State>) -> Option<&WidgetData<State>> {
|
||||
self.vec.get(id.id())
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
&self.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||
pub fn data_mut(&mut self, id: impl IdLike<State>) -> Option<&mut WidgetData<State>> {
|
||||
self.vec.get_mut(id.id())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: impl IdLike) {
|
||||
pub fn delete(&mut self, id: impl IdLike<State>) {
|
||||
self.vec.free(id.id());
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
@@ -80,4 +90,4 @@ impl Widgets {
|
||||
}
|
||||
}
|
||||
|
||||
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
|
||||
pub type WidgetWrapper<'a, State> = DynBorrower<'a, dyn Widget<State>>;
|
||||
|
||||
Reference in New Issue
Block a user