remove state generic from a lot of things
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
use crate::{Ui, WidgetIdFn, WidgetLike, WidgetRef};
|
||||
use crate::{HasUi, WidgetIdFn, WidgetLike, WidgetRef};
|
||||
|
||||
pub trait WidgetAttr<State, W: ?Sized> {
|
||||
type Input;
|
||||
fn run(ui: &mut Ui<State>, id: WidgetRef<State, W>, input: Self::Input);
|
||||
fn run(state: &mut State, id: WidgetRef<W>, input: Self::Input);
|
||||
}
|
||||
|
||||
pub trait Attrable<State, W: ?Sized, Tag> {
|
||||
fn attr<A: WidgetAttr<State, W>>(self, input: A::Input) -> impl WidgetIdFn<State, W>;
|
||||
}
|
||||
|
||||
impl<State, WL: WidgetLike<State, Tag>, Tag> Attrable<State, WL::Widget, Tag> for WL {
|
||||
impl<State: HasUi, 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);
|
||||
|state| {
|
||||
let id = self.add(state);
|
||||
A::run(state, id.weak(), input);
|
||||
id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,13 +8,13 @@ pub struct EventCtx<'a, State, Data> {
|
||||
}
|
||||
|
||||
pub struct EventIdCtx<'a, State, Data, W: ?Sized> {
|
||||
pub widget: WidgetRef<State, W>,
|
||||
pub widget: WidgetRef<W>,
|
||||
pub state: &'a mut State,
|
||||
pub data: &'a mut Data,
|
||||
}
|
||||
|
||||
impl<State: HasUi, Data, W: ?Sized> Deref for EventIdCtx<'_, State, Data, W> {
|
||||
type Target = Ui<State>;
|
||||
type Target = Ui;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.state.ui_ref()
|
||||
@@ -27,7 +27,7 @@ impl<State: HasUi, Data, W: ?Sized> DerefMut for EventIdCtx<'_, State, Data, W>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, State: HasUi + 'static, Data, W: Widget<State>> EventIdCtx<'a, State, Data, W> {
|
||||
impl<'a, State: HasUi + 'static, Data, W: Widget> EventIdCtx<'a, State, Data, W> {
|
||||
pub fn widget(&mut self) -> &mut W {
|
||||
&mut self.state.ui()[self.widget]
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::{
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasUi, IdLike, LayerId, WidgetData, WidgetId,
|
||||
ActiveData, Event, EventCtx, EventFn, EventLike, HasRsc, HasUi, IdLike, LayerId, WidgetData,
|
||||
WidgetId,
|
||||
util::{HashMap, TypeMap},
|
||||
};
|
||||
use std::{any::TypeId, rc::Rc};
|
||||
@@ -21,7 +22,7 @@ impl<State: 'static> EventManager<State> {
|
||||
self.types.type_or_default()
|
||||
}
|
||||
|
||||
pub fn register<I: IdLike<State>, E: EventLike>(
|
||||
pub fn register<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: I,
|
||||
event: E,
|
||||
@@ -33,20 +34,28 @@ impl<State: 'static> EventManager<State> {
|
||||
pub fn type_key<E: EventLike>() -> TypeId {
|
||||
TypeId::of::<TypeEventManager<State, E::Event>>()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn remove(&mut self, id: WidgetId) {
|
||||
pub trait EventsLike {
|
||||
fn remove(&mut self, id: WidgetId);
|
||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData);
|
||||
}
|
||||
|
||||
impl<State: 'static> EventsLike for EventManager<State> {
|
||||
fn remove(&mut self, id: WidgetId) {
|
||||
for m in self.types.values_mut() {
|
||||
m.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn draw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
fn draw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().draw(active);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn undraw(&mut self, data: &WidgetData<State>, active: &ActiveData) {
|
||||
fn undraw(&mut self, data: &WidgetData, active: &ActiveData) {
|
||||
for t in &data.event_mgrs {
|
||||
self.types.get_mut(t).unwrap().undraw(active);
|
||||
}
|
||||
@@ -99,7 +108,7 @@ impl<State, E: Event> Default for TypeEventManager<State, E> {
|
||||
impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
fn register(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
event: impl EventLike<Event = E>,
|
||||
f: impl for<'a> EventFn<State, E::Data<'a>>,
|
||||
) {
|
||||
@@ -112,7 +121,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<State, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
@@ -128,21 +137,26 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasEvents: Sized + HasUi {
|
||||
fn run_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
);
|
||||
}
|
||||
pub trait HasEvents: Sized {
|
||||
type State: HasRsc<Rsc = Self>;
|
||||
|
||||
impl<State: HasUi + 'static> HasEvents for State {
|
||||
fn run_event<E: EventLike>(
|
||||
fn events(&mut self) -> &mut EventManager<Self::State>;
|
||||
|
||||
fn register_event<I: IdLike, E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<Self>,
|
||||
data: &mut <E::Event as Event>::Data<'_>,
|
||||
) {
|
||||
let f = self.ui().events.get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
id: I,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<Self::State, <E::Event as Event>::Data<'a>>,
|
||||
) where
|
||||
Self: HasUi,
|
||||
{
|
||||
let id = id.id();
|
||||
self.events().register(id, event, f);
|
||||
self.ui()
|
||||
.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::<Self::State>::type_key::<E>());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
mod ctx;
|
||||
mod manager;
|
||||
mod rsc;
|
||||
|
||||
pub use ctx::*;
|
||||
pub use manager::*;
|
||||
pub use rsc::*;
|
||||
|
||||
pub trait Event: Sized + 'static + Clone {
|
||||
type Data<'a> = ();
|
||||
|
||||
29
core/src/event/rsc.rs
Normal file
29
core/src/event/rsc.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::{Event, EventCtx, EventLike, EventManager, HasEvents, HasUi, IdLike};
|
||||
|
||||
pub trait HasRsc: Sized + 'static {
|
||||
type Rsc: HasEvents<State = Self> + HasUi;
|
||||
fn rsc(&self) -> &Self::Rsc;
|
||||
fn rsc_mut(&mut self) -> &mut Self::Rsc;
|
||||
|
||||
fn run_event<E: EventLike>(&mut self, id: impl IdLike, data: &mut <E::Event as Event>::Data<'_>)
|
||||
where
|
||||
Self: 'static,
|
||||
{
|
||||
let f = self.rsc_mut().events().get_type::<E>().run_fn(id);
|
||||
f(EventCtx { state: self, data })
|
||||
}
|
||||
|
||||
fn events(&mut self) -> &mut EventManager<Self> {
|
||||
self.rsc_mut().events()
|
||||
}
|
||||
}
|
||||
|
||||
impl<R: HasRsc> HasUi for R {
|
||||
fn ui_ref(&self) -> &crate::Ui {
|
||||
self.rsc().ui_ref()
|
||||
}
|
||||
|
||||
fn ui(&mut self) -> &mut crate::Ui {
|
||||
self.rsc_mut().ui()
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,6 @@ mod num;
|
||||
mod orientation;
|
||||
mod primitive;
|
||||
mod render;
|
||||
mod typed;
|
||||
mod ui;
|
||||
mod widget;
|
||||
|
||||
|
||||
@@ -59,7 +59,7 @@ impl UiRenderNode {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update<State>(&mut self, device: &Device, queue: &Queue, ui: &mut Ui<State>) {
|
||||
pub fn update(&mut self, device: &Device, queue: &Queue, ui: &mut Ui) {
|
||||
self.active.clear();
|
||||
for (i, primitives) in ui.layers.iter_mut() {
|
||||
self.active.push(i);
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
#[macro_export]
|
||||
macro_rules! core_state {
|
||||
($vis: vis $state: ty) => {
|
||||
$vis type WidgetHandle<W = dyn Widget<$state>> = $crate::WidgetHandle<$state, W>;
|
||||
$vis type WidgetHandles<W = dyn Widget<$state>> = $crate::WidgetHandles<$state, W>;
|
||||
$vis type WidgetRef<W = dyn Widget<$state>> = $crate::WidgetRef<$state, W>;
|
||||
$vis type Ui = $crate::Ui<$state>;
|
||||
};
|
||||
}
|
||||
@@ -1,20 +1,22 @@
|
||||
use crate::{
|
||||
ActiveData, Axis, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
|
||||
ActiveData, Axis, EventsLike, Painter, SizeCtx, Ui, UiRegion, UiVec2, WidgetId,
|
||||
render::MaskIdx,
|
||||
util::{HashSet, forget_ref},
|
||||
};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
/// state maintained between widgets during painting
|
||||
pub struct DrawState<'a, State> {
|
||||
pub(super) ui: &'a mut Ui<State>,
|
||||
pub struct DrawState<'a> {
|
||||
pub(super) ui: &'a mut Ui,
|
||||
pub(super) events: &'a mut dyn EventsLike,
|
||||
draw_started: HashSet<WidgetId>,
|
||||
}
|
||||
|
||||
impl<'a, State: 'static> DrawState<'a, State> {
|
||||
pub fn new(ui: &'a mut Ui<State>) -> Self {
|
||||
impl<'a> DrawState<'a> {
|
||||
pub fn new(ui: &'a mut Ui, events: &'a mut dyn EventsLike) -> Self {
|
||||
Self {
|
||||
ui,
|
||||
events,
|
||||
draw_started: Default::default(),
|
||||
}
|
||||
}
|
||||
@@ -23,12 +25,10 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
while let Some(&id) = self.widgets.needs_redraw.iter().next() {
|
||||
self.redraw(id);
|
||||
}
|
||||
self.free();
|
||||
self.ui.free(self.events);
|
||||
}
|
||||
|
||||
/// redraws a widget that's currently active (drawn)
|
||||
/// can be called on something already drawn or removed,
|
||||
/// will just return if so
|
||||
pub fn redraw(&mut self, id: WidgetId) {
|
||||
self.widgets.needs_redraw.remove(&id);
|
||||
self.draw_started.remove(&id);
|
||||
@@ -65,11 +65,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
);
|
||||
}
|
||||
|
||||
pub(super) fn size_ctx<'b>(
|
||||
&'b mut self,
|
||||
source: WidgetId,
|
||||
outer: UiVec2,
|
||||
) -> SizeCtx<'b, State> {
|
||||
pub(super) fn size_ctx<'b>(&'b mut self, source: WidgetId, outer: UiVec2) -> SizeCtx<'b> {
|
||||
SizeCtx {
|
||||
source,
|
||||
cache: &mut self.ui.cache,
|
||||
@@ -86,10 +82,10 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
// free all resources & cache
|
||||
for (id, active) in self.ui.active.drain() {
|
||||
let data = self.ui.widgets.data(id).unwrap();
|
||||
self.ui.events.undraw(data, &active);
|
||||
self.events.undraw(data, &active);
|
||||
}
|
||||
self.ui.cache.clear();
|
||||
self.free();
|
||||
self.ui.free(self.events);
|
||||
self.layers.clear();
|
||||
self.widgets.needs_redraw.clear();
|
||||
|
||||
@@ -107,26 +103,11 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
mask: MaskIdx,
|
||||
old_children: Option<Vec<WidgetId>>,
|
||||
) {
|
||||
// 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.draw_started.contains(&id) {
|
||||
// panic!(
|
||||
// "Cannot draw the same widget ({}) twice (1)",
|
||||
// self.widgets.data(&id).unwrap().label
|
||||
// );
|
||||
// }
|
||||
let mut old_children = old_children.unwrap_or_default();
|
||||
if let Some(active) = self.ui.active.get_mut(&id)
|
||||
&& !self.ui.widgets.needs_redraw.contains(&id)
|
||||
{
|
||||
// check to see if we can skip drawing first
|
||||
if active.parent != parent {
|
||||
panic!("Cannot draw the same widget twice (2)");
|
||||
}
|
||||
if active.region == region {
|
||||
return;
|
||||
} else if active.region.size() == region.size() {
|
||||
@@ -190,7 +171,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
|
||||
// update modules
|
||||
let data = self.ui.widgets.data(id).unwrap();
|
||||
self.ui.events.draw(data, &active);
|
||||
self.events.draw(data, &active);
|
||||
self.active.insert(id, active);
|
||||
}
|
||||
|
||||
@@ -222,7 +203,7 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
self.textures.free();
|
||||
if undraw {
|
||||
let data = self.ui.widgets.data(id).unwrap();
|
||||
self.ui.events.undraw(data, active);
|
||||
self.events.undraw(data, active);
|
||||
}
|
||||
}
|
||||
active
|
||||
@@ -240,14 +221,14 @@ impl<'a, State: 'static> DrawState<'a, State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State> Deref for DrawState<'_, State> {
|
||||
type Target = Ui<State>;
|
||||
impl Deref for DrawState<'_> {
|
||||
type Target = Ui;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.ui
|
||||
}
|
||||
}
|
||||
impl<State> DerefMut for DrawState<'_, State> {
|
||||
impl DerefMut for DrawState<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.ui
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Event, EventFn, EventLike, EventManager, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData,
|
||||
TextureHandle, Textures, Widget, WidgetHandle, WidgetId, WidgetLike, Widgets,
|
||||
EventsLike, IdLike, Mask, PixelRegion, PrimitiveLayers, TextData, TextureHandle, Textures,
|
||||
Widget, WidgetHandle, WidgetId, Widgets,
|
||||
ui::draw_state::DrawState,
|
||||
util::{HashMap, TrackedArena, Vec2},
|
||||
};
|
||||
@@ -22,10 +22,9 @@ use cache::*;
|
||||
pub use painter::Painter;
|
||||
pub use size::*;
|
||||
|
||||
pub struct Ui<State> {
|
||||
pub struct Ui {
|
||||
// TODO: edit visibilities
|
||||
pub widgets: Widgets<State>,
|
||||
pub events: EventManager<State>,
|
||||
pub widgets: Widgets,
|
||||
|
||||
// retained painter state
|
||||
pub active: HashMap<WidgetId, ActiveData>,
|
||||
@@ -36,56 +35,44 @@ pub struct Ui<State> {
|
||||
pub masks: TrackedArena<Mask, u32>,
|
||||
pub cache: Cache,
|
||||
|
||||
root: Option<WidgetHandle<State>>,
|
||||
pub root: Option<WidgetHandle>,
|
||||
old_root: Option<WidgetId>,
|
||||
recv: Receiver<WidgetId>,
|
||||
send: Sender<WidgetId>,
|
||||
full_redraw: bool,
|
||||
resized: bool,
|
||||
}
|
||||
|
||||
pub trait HasUi: Sized {
|
||||
fn ui_ref(&self) -> &Ui<Self>;
|
||||
fn ui(&mut self) -> &mut Ui<Self>;
|
||||
pub trait HasUi: Sized + 'static {
|
||||
fn ui_ref(&self) -> &Ui;
|
||||
fn ui(&mut self) -> &mut Ui;
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
/// useful for debugging
|
||||
pub fn set_label(&mut self, id: impl IdLike<State>, label: String) {
|
||||
pub fn set_label(&mut self, id: impl IdLike, label: String) {
|
||||
self.widgets.data_mut(id.id()).unwrap().label = label;
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.widgets.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn add_widget<W: Widget<State>>(&mut self, w: W) -> WidgetHandle<State, W> {
|
||||
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetHandle<W> {
|
||||
WidgetHandle::new(self.widgets.add(w), self.send.clone())
|
||||
}
|
||||
|
||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<State, Tag>) {
|
||||
self.root = Some(w.add(self));
|
||||
self.full_redraw = true;
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.widgets.get(id)
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -96,49 +83,39 @@ impl<State: 'static> Ui<State> {
|
||||
self.textures.add(image)
|
||||
}
|
||||
|
||||
pub fn register_event<E: EventLike>(
|
||||
&mut self,
|
||||
id: impl IdLike<State>,
|
||||
event: E,
|
||||
f: impl for<'a> EventFn<State, <E::Event as Event>::Data<'a>>,
|
||||
) {
|
||||
self.events.register(id.id(), event, f);
|
||||
self.widgets
|
||||
.data_mut(id)
|
||||
.unwrap()
|
||||
.event_mgrs
|
||||
.insert(EventManager::<State>::type_key::<E>());
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
self.output_size = size.into();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
pub fn update(&mut self) {
|
||||
if self.full_redraw {
|
||||
DrawState::new(self).redraw_all();
|
||||
self.full_redraw = false;
|
||||
pub fn update(&mut self, events: &mut dyn EventsLike) {
|
||||
if self.root_changed() {
|
||||
DrawState::new(self, events).redraw_all();
|
||||
self.old_root = self.root.as_ref().map(|r| r.id());
|
||||
} else if self.widgets.has_updates() {
|
||||
DrawState::new(self).redraw_updates();
|
||||
DrawState::new(self, events).redraw_updates();
|
||||
}
|
||||
if self.resized {
|
||||
self.resized = false;
|
||||
DrawState::new(self).redraw_all();
|
||||
DrawState::new(self, events).redraw_all();
|
||||
}
|
||||
}
|
||||
|
||||
/// free any resources that don't have references anymore
|
||||
fn free(&mut self) {
|
||||
fn free(&mut self, events: &mut dyn EventsLike) {
|
||||
for id in self.recv.try_iter() {
|
||||
self.events.remove(id);
|
||||
events.remove(id);
|
||||
self.widgets.delete(id);
|
||||
}
|
||||
self.textures.free();
|
||||
}
|
||||
|
||||
pub fn root_changed(&self) -> bool {
|
||||
self.root.as_ref().map(|r| r.id()) != self.old_root
|
||||
}
|
||||
|
||||
pub fn needs_redraw(&self) -> bool {
|
||||
self.full_redraw || self.widgets.has_updates()
|
||||
self.root_changed() || self.widgets.has_updates()
|
||||
}
|
||||
|
||||
pub fn num_widgets(&self) -> usize {
|
||||
@@ -161,7 +138,7 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn window_region(&self, id: &impl IdLike<State>) -> Option<PixelRegion> {
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let region = self.active.get(&id.id())?.region;
|
||||
Some(region.to_px(self.output_size))
|
||||
}
|
||||
@@ -174,7 +151,7 @@ impl<State: 'static> Ui<State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static, I: IdLike<State>> Index<I> for Ui<State>
|
||||
impl<I: IdLike> Index<I> for Ui
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -185,7 +162,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static, I: IdLike<State>> IndexMut<I> for Ui<State>
|
||||
impl<I: IdLike> IndexMut<I> for Ui
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
@@ -194,12 +171,11 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Default for Ui<State> {
|
||||
impl Default for Ui {
|
||||
fn default() -> Self {
|
||||
let (send, recv) = channel();
|
||||
Self {
|
||||
widgets: Default::default(),
|
||||
events: Default::default(),
|
||||
active: Default::default(),
|
||||
layers: Default::default(),
|
||||
masks: Default::default(),
|
||||
@@ -207,8 +183,8 @@ impl<State: 'static> Default for Ui<State> {
|
||||
textures: Default::default(),
|
||||
cache: Default::default(),
|
||||
output_size: Vec2::ZERO,
|
||||
root: Default::default(),
|
||||
full_redraw: false,
|
||||
root: None,
|
||||
old_root: None,
|
||||
send,
|
||||
recv,
|
||||
resized: false,
|
||||
|
||||
@@ -7,8 +7,8 @@ use crate::{
|
||||
};
|
||||
|
||||
/// makes your surfaces look pretty
|
||||
pub struct Painter<'a, 'b, State> {
|
||||
pub(super) state: &'a mut DrawState<'b, State>,
|
||||
pub struct Painter<'a, 'b> {
|
||||
pub(super) state: &'a mut DrawState<'b>,
|
||||
|
||||
pub(super) region: UiRegion,
|
||||
pub(super) mask: MaskIdx,
|
||||
@@ -19,7 +19,7 @@ pub struct Painter<'a, 'b, State> {
|
||||
pub(super) id: WidgetId,
|
||||
}
|
||||
|
||||
impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
impl<'a, 'c> Painter<'a, 'c> {
|
||||
fn primitive_at<P: Primitive>(&mut self, primitive: P, region: UiRegion) {
|
||||
let h = self.state.layers.write(
|
||||
self.layer,
|
||||
@@ -52,17 +52,17 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>) {
|
||||
pub fn widget<W: ?Sized>(&mut self, id: &WidgetHandle<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<State, W>, region: UiRegion) {
|
||||
pub fn widget_within<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
|
||||
self.widget_at(id, region.within(&self.region));
|
||||
}
|
||||
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<State, W>, region: UiRegion) {
|
||||
fn widget_at<W: ?Sized>(&mut self, id: &WidgetHandle<W>, region: UiRegion) {
|
||||
self.children.push(id.id());
|
||||
self.state
|
||||
.draw_inner(self.layer, id.id(), region, Some(self.id), self.mask, None);
|
||||
@@ -95,15 +95,11 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
self.region
|
||||
}
|
||||
|
||||
pub fn size<W: ?Sized + Widget<State>>(&mut self, id: &WidgetHandle<State, W>) -> Size {
|
||||
pub fn size<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>) -> Size {
|
||||
self.size_ctx().size(id)
|
||||
}
|
||||
|
||||
pub fn len_axis<W: ?Sized + Widget<State>>(
|
||||
&mut self,
|
||||
id: &WidgetHandle<State, W>,
|
||||
axis: Axis,
|
||||
) -> Len {
|
||||
pub fn len_axis<W: ?Sized + Widget>(&mut self, id: &WidgetHandle<W>, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.size_ctx().width(id),
|
||||
Axis::Y => self.size_ctx().height(id),
|
||||
@@ -138,7 +134,7 @@ impl<'a, 'c, State: 'static> Painter<'a, 'c, State> {
|
||||
&self.id
|
||||
}
|
||||
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_, State> {
|
||||
pub fn size_ctx(&mut self) -> SizeCtx<'_> {
|
||||
self.state.size_ctx(self.id, self.region.size())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,11 +3,11 @@ use crate::{
|
||||
UiVec2, WidgetAxisFns, WidgetId, Widgets, XAxis, YAxis, ui::cache::Cache, util::Vec2,
|
||||
};
|
||||
|
||||
pub struct SizeCtx<'a, State> {
|
||||
pub struct SizeCtx<'a> {
|
||||
pub text: &'a mut TextData,
|
||||
pub textures: &'a mut Textures,
|
||||
pub(super) source: WidgetId,
|
||||
pub(super) widgets: &'a Widgets<State>,
|
||||
pub(super) widgets: &'a Widgets,
|
||||
pub(super) cache: &'a mut Cache,
|
||||
/// TODO: should this be pub? rn used for sized
|
||||
pub outer: UiVec2,
|
||||
@@ -15,7 +15,7 @@ pub struct SizeCtx<'a, State> {
|
||||
pub(super) id: WidgetId,
|
||||
}
|
||||
|
||||
impl<State: 'static> SizeCtx<'_, State> {
|
||||
impl SizeCtx<'_> {
|
||||
pub fn id(&self) -> &WidgetId {
|
||||
&self.id
|
||||
}
|
||||
@@ -45,22 +45,22 @@ impl<State: 'static> SizeCtx<'_, State> {
|
||||
len
|
||||
}
|
||||
|
||||
pub fn width(&mut self, id: impl IdLike<State>) -> Len {
|
||||
pub fn width(&mut self, id: impl IdLike) -> Len {
|
||||
self.len_inner::<XAxis>(id.id())
|
||||
}
|
||||
|
||||
pub fn height(&mut self, id: impl IdLike<State>) -> Len {
|
||||
pub fn height(&mut self, id: impl IdLike) -> Len {
|
||||
self.len_inner::<YAxis>(id.id())
|
||||
}
|
||||
|
||||
pub fn len_axis(&mut self, id: impl IdLike<State>, axis: Axis) -> Len {
|
||||
pub fn len_axis(&mut self, id: impl IdLike, axis: Axis) -> Len {
|
||||
match axis {
|
||||
Axis::X => self.width(id),
|
||||
Axis::Y => self.height(id),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn size(&mut self, id: impl IdLike<State>) -> Size {
|
||||
pub fn size(&mut self, id: impl IdLike) -> Size {
|
||||
let id = id.id();
|
||||
Size {
|
||||
x: self.width(id),
|
||||
|
||||
@@ -2,16 +2,16 @@ use std::any::TypeId;
|
||||
|
||||
use crate::{Widget, util::HashSet};
|
||||
|
||||
pub struct WidgetData<State> {
|
||||
pub widget: Box<dyn Widget<State>>,
|
||||
pub struct WidgetData {
|
||||
pub widget: Box<dyn Widget>,
|
||||
pub label: String,
|
||||
pub event_mgrs: HashSet<TypeId>,
|
||||
/// dynamic borrow checking
|
||||
pub borrowed: bool,
|
||||
}
|
||||
|
||||
impl<State> WidgetData<State> {
|
||||
pub fn new<W: Widget<State>>(widget: W) -> Self {
|
||||
impl WidgetData {
|
||||
pub fn new<W: Widget>(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,12 +1,7 @@
|
||||
use std::{
|
||||
marker::{PhantomData, Unsize},
|
||||
mem::MaybeUninit,
|
||||
ops::CoerceUnsized,
|
||||
sync::mpsc::Sender,
|
||||
};
|
||||
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
|
||||
|
||||
use crate::{
|
||||
Ui, Widget,
|
||||
Widget,
|
||||
util::{RefCounter, SlotId},
|
||||
};
|
||||
|
||||
@@ -17,42 +12,39 @@ 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<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
|
||||
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<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub struct WidgetRef<W: ?Sized = dyn Widget> {
|
||||
pub(super) id: WidgetId,
|
||||
#[allow(unused)]
|
||||
ty: *const W,
|
||||
state: PhantomData<State>,
|
||||
}
|
||||
|
||||
pub struct WidgetHandles<State, W: ?Sized = dyn Widget<State>> {
|
||||
pub h: WidgetHandle<State, W>,
|
||||
pub r: WidgetRef<State, W>,
|
||||
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
|
||||
pub h: WidgetHandle<W>,
|
||||
pub r: WidgetRef<W>,
|
||||
}
|
||||
|
||||
impl<State, W: Widget<State> + ?Sized + Unsize<dyn Widget<State>>> WidgetHandle<State, W> {
|
||||
pub fn any(self) -> WidgetHandle<State, dyn Widget<State>> {
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, W: ?Sized> WidgetHandle<State, W> {
|
||||
impl<W: ?Sized> WidgetHandle<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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,28 +56,24 @@ impl<State, W: ?Sized> WidgetHandle<State, W> {
|
||||
self.counter.refs()
|
||||
}
|
||||
|
||||
pub fn weak(&self) -> WidgetRef<State, W> {
|
||||
pub fn weak(&self) -> WidgetRef<W> {
|
||||
let Self { ty, id, .. } = *self;
|
||||
WidgetRef {
|
||||
ty,
|
||||
id,
|
||||
state: PhantomData,
|
||||
}
|
||||
WidgetRef { ty, id }
|
||||
}
|
||||
|
||||
pub fn handles(self) -> WidgetHandles<State, W> {
|
||||
pub fn handles(self) -> WidgetHandles<W> {
|
||||
let r = self.weak();
|
||||
WidgetHandles { h: self, r }
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, W: ?Sized> WidgetRef<State, W> {
|
||||
impl<W: ?Sized> WidgetRef<W> {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
|
||||
impl<W: ?Sized> Drop for WidgetHandle<W> {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() {
|
||||
let _ = self.send.send(self.id);
|
||||
@@ -93,64 +81,52 @@ impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
|
||||
}
|
||||
}
|
||||
|
||||
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 WidgetIdFn<State, W: ?Sized = dyn Widget>: FnOnce(&mut State) -> WidgetHandle<W> {}
|
||||
impl<State, W: ?Sized, F: FnOnce(&mut State) -> WidgetHandle<W>> WidgetIdFn<State, W> for F {}
|
||||
|
||||
pub trait IdLike<State> {
|
||||
type Widget: Widget<State> + ?Sized + 'static;
|
||||
pub trait IdLike {
|
||||
type Widget: Widget + ?Sized + 'static;
|
||||
fn id(&self) -> WidgetId;
|
||||
}
|
||||
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for &WidgetHandle<State, W> {
|
||||
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetHandle<State, W> {
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetRef<State, W> {
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> IdLike<State> for WidgetId {
|
||||
type Widget = dyn Widget<State>;
|
||||
impl IdLike for WidgetId {
|
||||
type Widget = dyn Widget;
|
||||
fn id(&self) -> WidgetId {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
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<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<State, W: ?Sized> Clone for WidgetRef<State, W> {
|
||||
impl<W: ?Sized> Clone for WidgetRef<W> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
impl<State, W: ?Sized> Copy for WidgetRef<State, W> {}
|
||||
impl<State, W: ?Sized> PartialEq for WidgetRef<State, W> {
|
||||
impl<W: ?Sized> Copy for WidgetRef<W> {}
|
||||
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id == other.id
|
||||
}
|
||||
|
||||
@@ -1,36 +1,38 @@
|
||||
use crate::HasUi;
|
||||
|
||||
use super::*;
|
||||
use std::marker::Unsize;
|
||||
|
||||
pub trait WidgetLike<State: 'static, Tag>: Sized {
|
||||
type Widget: Widget<State> + ?Sized + Unsize<dyn Widget<State>> + 'static;
|
||||
pub trait WidgetLike<State: 'static + HasUi, Tag>: Sized {
|
||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
||||
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, Self::Widget>;
|
||||
fn add(self, state: &mut State) -> WidgetHandle<Self::Widget>;
|
||||
|
||||
fn with_id<W2>(
|
||||
self,
|
||||
f: impl FnOnce(&mut Ui<State>, WidgetHandle<State, Self::Widget>) -> WidgetHandle<State, W2>,
|
||||
f: impl FnOnce(&mut State, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
|
||||
) -> impl WidgetIdFn<State, W2> {
|
||||
move |ui| {
|
||||
let id = self.add(ui);
|
||||
f(ui, id)
|
||||
move |state| {
|
||||
let id = self.add(state);
|
||||
f(state, id)
|
||||
}
|
||||
}
|
||||
|
||||
fn set_root(self, ui: &mut Ui<State>) {
|
||||
ui.set_root(self);
|
||||
fn set_root(self, state: &mut State) {
|
||||
state.ui().root = Some(self.add(state));
|
||||
}
|
||||
|
||||
fn handles(self, ui: &mut Ui<State>) -> WidgetHandles<State, Self::Widget> {
|
||||
self.add(ui).handles()
|
||||
fn handles(self, state: &mut State) -> WidgetHandles<Self::Widget> {
|
||||
self.add(state).handles()
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetArrLike<State, const LEN: usize, Tag> {
|
||||
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, LEN>;
|
||||
fn add(self, state: &mut State) -> WidgetArr<LEN>;
|
||||
}
|
||||
|
||||
impl<State, const LEN: usize> WidgetArrLike<State, LEN, ArrTag> for WidgetArr<State, LEN> {
|
||||
fn ui(self, _: &mut Ui<State>) -> WidgetArr<State, LEN> {
|
||||
impl<State, const LEN: usize> WidgetArrLike<State, LEN, ArrTag> for WidgetArr<LEN> {
|
||||
fn add(self, _: &mut State) -> WidgetArr<LEN> {
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -41,12 +43,12 @@ macro_rules! impl_widget_arr {
|
||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||
};
|
||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||
impl<State: 'static, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
||||
fn ui(self, ui: &mut Ui<State>) -> WidgetArr<State, $n> {
|
||||
impl<State: 'static + HasUi, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
||||
fn add(self, state: &mut State) -> WidgetArr<$n> {
|
||||
#[allow(non_snake_case)]
|
||||
let ($($W,)*) = self;
|
||||
WidgetArr::new(
|
||||
[$($W.add(ui),)*],
|
||||
[$($W.add(state),)*],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{Axis, AxisT, Len, Painter, SizeCtx, Ui};
|
||||
use crate::{Axis, AxisT, Len, Painter, SizeCtx};
|
||||
use std::any::Any;
|
||||
|
||||
mod data;
|
||||
@@ -13,18 +13,18 @@ pub use like::*;
|
||||
pub use tag::*;
|
||||
pub use widgets::*;
|
||||
|
||||
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;
|
||||
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 WidgetAxisFns<State> {
|
||||
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx<State>) -> Len;
|
||||
pub trait WidgetAxisFns {
|
||||
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx) -> Len;
|
||||
}
|
||||
|
||||
impl<State, W: Widget<State> + ?Sized> WidgetAxisFns<State> for W {
|
||||
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx<State>) -> Len {
|
||||
impl<W: Widget + ?Sized> WidgetAxisFns for W {
|
||||
fn desired_len<A: AxisT>(&mut self, ctx: &mut SizeCtx) -> Len {
|
||||
match A::get() {
|
||||
Axis::X => self.desired_width(ctx),
|
||||
Axis::Y => self.desired_height(ctx),
|
||||
@@ -32,17 +32,17 @@ impl<State, W: Widget<State> + ?Sized> WidgetAxisFns<State> for W {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State> Widget<State> for () {
|
||||
fn draw(&mut self, _: &mut Painter<State>) {}
|
||||
fn desired_width(&mut self, _: &mut SizeCtx<State>) -> Len {
|
||||
impl Widget for () {
|
||||
fn draw(&mut self, _: &mut Painter) {}
|
||||
fn desired_width(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::ZERO
|
||||
}
|
||||
fn desired_height(&mut self, _: &mut SizeCtx<State>) -> Len {
|
||||
fn desired_height(&mut self, _: &mut SizeCtx) -> Len {
|
||||
Len::ZERO
|
||||
}
|
||||
}
|
||||
|
||||
impl<State> dyn Widget<State> {
|
||||
impl dyn Widget {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
@@ -55,31 +55,31 @@ impl<State> dyn Widget<State> {
|
||||
/// 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<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 trait WidgetFn<State, W: Widget + ?Sized>: FnOnce(&mut State) -> W {}
|
||||
impl<State, W: Widget + ?Sized, F: FnOnce(&mut State) -> W> WidgetFn<State, W> for F {}
|
||||
|
||||
pub struct WidgetArr<State, const LEN: usize> {
|
||||
pub arr: [WidgetHandle<State>; LEN],
|
||||
pub struct WidgetArr<const LEN: usize> {
|
||||
pub arr: [WidgetHandle; LEN],
|
||||
}
|
||||
|
||||
impl<State, const LEN: usize> WidgetArr<State, LEN> {
|
||||
pub fn new(arr: [WidgetHandle<State>; LEN]) -> Self {
|
||||
impl<const LEN: usize> WidgetArr<LEN> {
|
||||
pub fn new(arr: [WidgetHandle; LEN]) -> Self {
|
||||
Self { arr }
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetOption<State> {
|
||||
fn get(self, ui: &mut Ui<State>) -> Option<WidgetHandle<State>>;
|
||||
fn get(self, state: &mut State) -> Option<WidgetHandle>;
|
||||
}
|
||||
|
||||
impl<State> WidgetOption<State> for () {
|
||||
fn get(self, _: &mut Ui<State>) -> Option<WidgetHandle<State>> {
|
||||
fn get(self, _: &mut State) -> Option<WidgetHandle> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
impl<State, F: FnOnce(&mut State) -> Option<WidgetHandle>> WidgetOption<State> for F {
|
||||
fn get(self, state: &mut State) -> Option<WidgetHandle> {
|
||||
self(state)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
use std::marker::Unsize;
|
||||
|
||||
use crate::HasUi;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub struct ArrTag;
|
||||
|
||||
pub struct WidgetTag;
|
||||
impl<State: 'static, W: Widget<State>> WidgetLike<State, WidgetTag> for W {
|
||||
impl<State: HasUi, W: Widget> WidgetLike<State, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
ui.add_widget(self)
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
state.ui().add_widget(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnTag;
|
||||
impl<State: 'static, W: Widget<State>, F: FnOnce(&mut Ui<State>) -> W> WidgetLike<State, FnTag>
|
||||
for F
|
||||
{
|
||||
impl<State: HasUi, W: Widget, F: FnOnce(&mut State) -> W> WidgetLike<State, FnTag> for F {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
self(ui).add(ui)
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
self(state).add(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdTag;
|
||||
impl<State: 'static, W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static>
|
||||
WidgetLike<State, IdTag> for WidgetHandle<State, W>
|
||||
impl<State: HasUi, W: ?Sized + Widget + Unsize<dyn Widget> + 'static> WidgetLike<State, IdTag>
|
||||
for WidgetHandle<W>
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
fn add(self, _: &mut State) -> WidgetHandle<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdFnTag;
|
||||
impl<
|
||||
State: 'static,
|
||||
W: ?Sized + Widget<State> + Unsize<dyn Widget<State>> + 'static,
|
||||
F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>,
|
||||
State: HasUi,
|
||||
W: ?Sized + Widget + Unsize<dyn Widget> + 'static,
|
||||
F: FnOnce(&mut State) -> WidgetHandle<W>,
|
||||
> WidgetLike<State, IdFnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui<State>) -> WidgetHandle<State, W> {
|
||||
self(ui)
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
self(state)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,12 +3,12 @@ use crate::{
|
||||
util::{DynBorrower, HashSet, SlotVec, forget_mut, to_mut},
|
||||
};
|
||||
|
||||
pub struct Widgets<State> {
|
||||
pub struct Widgets {
|
||||
pub needs_redraw: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData<State>>,
|
||||
vec: SlotVec<WidgetData>,
|
||||
}
|
||||
|
||||
impl<State> Default for Widgets<State> {
|
||||
impl Default for Widgets {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
needs_redraw: Default::default(),
|
||||
@@ -17,23 +17,23 @@ impl<State> Default for Widgets<State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Widgets<State> {
|
||||
impl Widgets {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.needs_redraw.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget> {
|
||||
Some(self.vec.get(id)?.widget.as_ref())
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget<State>> {
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
|
||||
self.needs_redraw.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<'a>(&self, id: WidgetId) -> WidgetWrapper<'a, State> {
|
||||
pub(crate) fn get_dyn_dynamic<'a>(&self, id: WidgetId) -> WidgetWrapper<'a> {
|
||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
let data = unsafe { forget_mut(to_mut(self.vec.get(id).unwrap())) };
|
||||
@@ -43,37 +43,37 @@ impl<State: 'static> Widgets<State> {
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike>(&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<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
pub fn get_mut<I: IdLike>(&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<State>>(&mut self, widget: W) -> WidgetId {
|
||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
self.vec.add(WidgetData::new(widget))
|
||||
}
|
||||
|
||||
pub fn data(&self, id: impl IdLike<State>) -> Option<&WidgetData<State>> {
|
||||
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||
self.vec.get(id.id())
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: impl IdLike<State>) -> Option<&mut WidgetData<State>> {
|
||||
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||
self.vec.get_mut(id.id())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: impl IdLike<State>) {
|
||||
pub fn delete(&mut self, id: impl IdLike) {
|
||||
self.vec.free(id.id());
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
@@ -85,4 +85,4 @@ impl<State: 'static> Widgets<State> {
|
||||
}
|
||||
}
|
||||
|
||||
pub type WidgetWrapper<'a, State> = DynBorrower<'a, dyn Widget<State>>;
|
||||
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
|
||||
|
||||
Reference in New Issue
Block a user