FINALLY transition events to global; slow text sending bug tho
This commit is contained in:
@@ -10,4 +10,3 @@ bytemuck ={ workspace = true }
|
|||||||
image = { workspace = true }
|
image = { workspace = true }
|
||||||
cosmic-text = { workspace = true }
|
cosmic-text = { workspace = true }
|
||||||
fxhash = { workspace = true }
|
fxhash = { workspace = true }
|
||||||
|
|
||||||
|
|||||||
@@ -1,187 +1,192 @@
|
|||||||
use std::{hash::Hash, rc::Rc};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{Ui, UiModule, WidgetId, WidgetRef},
|
layout::{LayerId, UiId, Widget, WidgetId, WidgetInstance, WidgetRef},
|
||||||
util::HashMap,
|
util::{Handle, HashMap, RefGuardMut},
|
||||||
|
};
|
||||||
|
use std::{
|
||||||
|
any::{Any, TypeId},
|
||||||
|
ops::DerefMut,
|
||||||
|
sync::{Arc, LazyLock, Mutex},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Event: Sized {
|
pub trait Async: Send + Sync + 'static {}
|
||||||
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
|
impl<T: Send + Sync + 'static> Async for T {}
|
||||||
type Data: Clone;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct EventCtx<'a, Ctx, Data> {
|
pub trait Event: Sized + Async + Clone {
|
||||||
pub ui: &'a mut Ui,
|
type Data<'a> = ();
|
||||||
pub state: &'a mut Ctx,
|
type State: Sync + Send + Default = ();
|
||||||
pub data: Data,
|
#[allow(unused_variables)]
|
||||||
}
|
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
|
||||||
|
true
|
||||||
pub type ECtx<'a, Ctx, Data, W> = EventIdCtx<'a, Ctx, Data, W>;
|
|
||||||
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
|
||||||
pub widget: WidgetRef<W>,
|
|
||||||
pub ui: &'a mut Ui,
|
|
||||||
pub state: &'a mut Ctx,
|
|
||||||
pub data: Data,
|
|
||||||
}
|
|
||||||
|
|
||||||
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 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
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ui {
|
|
||||||
pub fn register_event<W: ?Sized, E: Event, Ctx: 'static>(
|
|
||||||
&mut self,
|
|
||||||
id: WidgetRef<W>,
|
|
||||||
event: E,
|
|
||||||
f: impl EventFn<Ctx, E::Data>,
|
|
||||||
) {
|
|
||||||
self.data
|
|
||||||
.modules
|
|
||||||
.get_mut::<E::Module<Ctx>>()
|
|
||||||
.register(id.id(), event, f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_widget_event<W: ?Sized + 'static, E: Event, Ctx: 'static>(
|
/// THIS SHOULD NOT BE OVERWRITTEN; the default impl runs this event
|
||||||
&mut self,
|
fn run<Ctx: 'static, W>(id: WidgetRef<W>, data: &mut Self::Data<'_>, state: &mut Ctx) {
|
||||||
id: WidgetRef<W>,
|
Events::<Self, Ctx>::run(id.id(), data, state);
|
||||||
event: E,
|
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
|
||||||
) {
|
|
||||||
self.data
|
|
||||||
.modules
|
|
||||||
.get_mut::<E::Module<Ctx>>()
|
|
||||||
.register(id.id(), event, move |ctx| {
|
|
||||||
f(EventIdCtx {
|
|
||||||
widget: id,
|
|
||||||
ui: ctx.ui,
|
|
||||||
state: ctx.state,
|
|
||||||
data: ctx.data,
|
|
||||||
})
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DefaultEvent: Hash + Eq + 'static {
|
pub trait EventAlias {
|
||||||
type Data: Clone;
|
type Event: Event;
|
||||||
|
fn into_event(self) -> Self::Event;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: DefaultEvent> Event for E {
|
impl<E: Event> EventAlias for E {
|
||||||
type Module<Ctx: 'static> = DefaultEventModule<E, Ctx>;
|
type Event = Self;
|
||||||
type Data = E::Data;
|
|
||||||
|
fn into_event(self) -> Self::Event {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
|
type EventData<E, Ctx> = (E, Arc<dyn for<'a> EventFn<Ctx, <E as Event>::Data<'a>>>);
|
||||||
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, E::Data>);
|
pub struct Events<E: Event, Ctx> {
|
||||||
fn run<'a>(
|
active: HashMap<UiId, HashMap<LayerId, HashMap<WidgetId, E::State>>>,
|
||||||
&mut self,
|
map: HashMap<WidgetId, Vec<EventData<E, Ctx>>>,
|
||||||
id: WidgetId,
|
|
||||||
event: E,
|
|
||||||
) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>) + use<'a, Self, Ctx, E>>;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
|
pub static EVENT_TYPES: LazyLock<Mutex<HashMap<TypeId, Handle<dyn EventManager>>>> =
|
||||||
pub struct DefaultEventModule<E: Event, Ctx> {
|
LazyLock::new(|| Mutex::new(HashMap::default()));
|
||||||
map: HashMap<E, EventFnMap<Ctx, <E as Event>::Data>>,
|
|
||||||
|
pub trait EventManager: Any + Send + Sync {
|
||||||
|
fn remove(&mut self, id: WidgetId) -> Box<dyn Any>;
|
||||||
|
fn draw(&mut self, ui: UiId, inst: &WidgetInstance);
|
||||||
|
fn undraw(&mut self, ui: UiId, inst: &WidgetInstance);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Event + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, Ctx> {
|
impl<E: Event, Ctx: 'static> EventManager for Events<E, Ctx> {
|
||||||
fn on_remove(&mut self, id: &WidgetId) {
|
/// so... there is a deadlock if this is called in WidgetData::drop
|
||||||
for map in self.map.values_mut() {
|
/// and some function in here owns another WidgetId
|
||||||
map.remove(id);
|
/// so for now you need to drop the lock on self, THEN drop the contents
|
||||||
|
fn remove(&mut self, id: WidgetId) -> Box<dyn Any> {
|
||||||
|
let contents = self.map.remove(&id);
|
||||||
|
for ui in self.active.values_mut() {
|
||||||
|
for layer in ui.values_mut() {
|
||||||
|
layer.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Box::new(contents)
|
||||||
|
}
|
||||||
|
fn draw(&mut self, ui: UiId, inst: &WidgetInstance) {
|
||||||
|
self.active
|
||||||
|
.entry(ui)
|
||||||
|
.or_default()
|
||||||
|
.entry(inst.layer)
|
||||||
|
.or_default()
|
||||||
|
.entry(inst.id)
|
||||||
|
.or_default();
|
||||||
|
}
|
||||||
|
fn undraw(&mut self, ui: UiId, inst: &WidgetInstance) {
|
||||||
|
// rust pls
|
||||||
|
(|| {
|
||||||
|
self.active
|
||||||
|
.get_mut(&ui)?
|
||||||
|
.get_mut(&inst.layer)?
|
||||||
|
.remove(&inst.id);
|
||||||
|
Some(())
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type UiActive<State> = HashMap<LayerId, HashMap<WidgetId, State>>;
|
||||||
|
|
||||||
|
impl<E: Event, Ctx: 'static> Events<E, Ctx> {
|
||||||
|
fn id() -> TypeId {
|
||||||
|
TypeId::of::<Self>()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
map: Default::default(),
|
||||||
|
active: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub trait HashableEvent: Event + Hash + Eq + 'static {}
|
pub fn active(id: UiId) -> impl DerefMut<Target = UiActive<E::State>> {
|
||||||
impl<E: Event + Hash + Eq + 'static> HashableEvent for E {}
|
RefGuardMut::map(Self::get(), |s| s.active.entry(id).or_default())
|
||||||
|
|
||||||
impl<E: HashableEvent, Ctx: 'static> EventModule<E, Ctx> for DefaultEventModule<E, Ctx> {
|
|
||||||
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
|
||||||
self.map
|
|
||||||
.entry(event)
|
|
||||||
.or_default()
|
|
||||||
.entry(id)
|
|
||||||
.or_default()
|
|
||||||
.push(Rc::new(f));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run<'a>(
|
fn register_<W: Widget + ?Sized>(
|
||||||
&mut self,
|
id: WidgetRef<W>,
|
||||||
id: WidgetId,
|
event: impl Into<E>,
|
||||||
event: E,
|
f: impl for<'a> EventFn<Ctx, <E as Event>::Data<'a>>,
|
||||||
) -> Option<impl FnOnce(EventCtx<Ctx, E::Data>) + use<'a, Ctx, E>> {
|
) {
|
||||||
if let Some(map) = self.map.get(&event)
|
let Some(mut data) = id.data_mut() else {
|
||||||
&& let Some(fs) = map.get(&id)
|
return;
|
||||||
{
|
};
|
||||||
let fs = fs.clone();
|
data.event_managers.insert(Self::id());
|
||||||
Some(move |ctx: EventCtx<Ctx, <E as Event>::Data>| {
|
Self::get()
|
||||||
for f in fs {
|
.map
|
||||||
f(EventCtx {
|
.entry(id.id())
|
||||||
ui: ctx.ui,
|
.or_default()
|
||||||
state: ctx.state,
|
.push((event.into(), Arc::new(f)));
|
||||||
data: ctx.data.clone(),
|
}
|
||||||
})
|
|
||||||
}
|
pub fn register<W: Widget + ?Sized + 'static>(
|
||||||
|
id: WidgetRef<W>,
|
||||||
|
event: impl Into<E>,
|
||||||
|
f: impl for<'a> EventIdFn<Ctx, <E as Event>::Data<'a>, W>,
|
||||||
|
) {
|
||||||
|
Self::register_(id, event, move |ctx| {
|
||||||
|
f(EventIdCtx {
|
||||||
|
widget: id,
|
||||||
|
state: ctx.state,
|
||||||
|
data: ctx.data,
|
||||||
})
|
})
|
||||||
} else {
|
});
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
pub fn run(id: WidgetId, data: &mut E::Data<'_>, state: &mut Ctx) {
|
||||||
pub fn run_all(&mut self, event: E, ctx: EventCtx<Ctx, E::Data>)
|
let s = Self::get();
|
||||||
where
|
if let Some(fs) = s.map.get(&id) {
|
||||||
E::Data: Clone,
|
let fs = fs.clone();
|
||||||
{
|
drop(s);
|
||||||
if let Some(map) = self.map.get(&event) {
|
for (e, f) in fs {
|
||||||
for fs in map.values() {
|
if e.should_run(data) {
|
||||||
let fs = fs.clone();
|
f(EventCtx { state, data })
|
||||||
for f in fs {
|
|
||||||
f(EventCtx {
|
|
||||||
ui: ctx.ui,
|
|
||||||
state: ctx.state,
|
|
||||||
data: ctx.data.clone(),
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: Event + 'static, Ctx: 'static> Default for DefaultEventModule<E, Ctx> {
|
pub fn run_all(data: &mut E::Data<'_>, state: &mut Ctx) {
|
||||||
fn default() -> Self {
|
let s = Self::get();
|
||||||
Self {
|
// TODO: should probably document that added events won't run same frame
|
||||||
map: Default::default(),
|
let mut map = s.map.clone();
|
||||||
|
drop(s);
|
||||||
|
for fs in map.values_mut() {
|
||||||
|
for (e, f) in fs {
|
||||||
|
if e.should_run(data) {
|
||||||
|
f(EventCtx { state, data })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// this some bull
|
||||||
|
fn get<'a>() -> RefGuardMut<'a, Self> {
|
||||||
|
let any = EVENT_TYPES
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.entry(Self::id())
|
||||||
|
.or_insert_with(|| Handle::from(Self::new()))
|
||||||
|
.clone();
|
||||||
|
unsafe { any.downcast() }.get_take_mut()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ui {
|
pub struct EventCtx<'a, Ctx, Data> {
|
||||||
pub fn run_event<E: Event, Ctx: 'static, W: ?Sized>(
|
pub state: &'a mut Ctx,
|
||||||
&mut self,
|
pub data: &'a mut Data,
|
||||||
ctx: &mut Ctx,
|
|
||||||
id: WidgetRef<W>,
|
|
||||||
event: E,
|
|
||||||
data: E::Data,
|
|
||||||
) {
|
|
||||||
if let Some(f) = self
|
|
||||||
.data
|
|
||||||
.modules
|
|
||||||
.get_mut::<E::Module<Ctx>>()
|
|
||||||
.run(id.id(), event)
|
|
||||||
{
|
|
||||||
eprintln!("FIXME");
|
|
||||||
f(EventCtx {
|
|
||||||
ui: self,
|
|
||||||
state: ctx,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
||||||
|
pub widget: WidgetRef<W>,
|
||||||
|
pub state: &'a mut Ctx,
|
||||||
|
pub data: &'a mut Data,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EventFn<Ctx, Data>: Fn(EventCtx<Ctx, Data>) + Async {}
|
||||||
|
impl<F: Fn(EventCtx<Ctx, Data>) + Async, Ctx, Data> EventFn<Ctx, Data> for F {}
|
||||||
|
|
||||||
|
pub trait EventIdFn<Ctx, Data, W: ?Sized>: Fn(EventIdCtx<Ctx, Data, W>) + Async {}
|
||||||
|
impl<F: Fn(EventIdCtx<Ctx, Data, W>) + Async, Ctx, Data, W: ?Sized> EventIdFn<Ctx, Data, W> for F {}
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ use std::{marker::Unsize, sync::mpsc::Sender};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
Axis, Len, Modules, PrimitiveLayers, RenderedText, Size, TextAttrs, TextBuffer, TextData,
|
Axis, LayerId, Len, PrimitiveLayers, RenderedText, Size, TextAttrs, TextBuffer, TextData,
|
||||||
TextureHandle, Textures, UiRegion, UiVec2, Vec2, Widget, WidgetHandle, WidgetId,
|
TextureHandle, Textures, UiId, UiRegion, UiVec2, Vec2, Widget, WidgetHandle, WidgetId,
|
||||||
WidgetUpdate,
|
WidgetUpdate,
|
||||||
},
|
},
|
||||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||||
@@ -27,13 +27,13 @@ pub struct Painter<'a, 'c> {
|
|||||||
|
|
||||||
/// context for a painter; lets you draw and redraw widgets
|
/// context for a painter; lets you draw and redraw widgets
|
||||||
struct PainterCtx<'a> {
|
struct PainterCtx<'a> {
|
||||||
|
ui_id: UiId,
|
||||||
pub active: &'a mut HashMap<WidgetId, WidgetInstance>,
|
pub active: &'a mut HashMap<WidgetId, WidgetInstance>,
|
||||||
pub layers: &'a mut PrimitiveLayers,
|
pub layers: &'a mut PrimitiveLayers,
|
||||||
pub textures: &'a mut Textures,
|
pub textures: &'a mut Textures,
|
||||||
pub masks: &'a mut TrackedArena<Mask, u32>,
|
pub masks: &'a mut TrackedArena<Mask, u32>,
|
||||||
pub text: &'a mut TextData,
|
pub text: &'a mut TextData,
|
||||||
pub output_size: Vec2,
|
pub output_size: Vec2,
|
||||||
pub modules: &'a mut Modules,
|
|
||||||
send: &'a Sender<WidgetUpdate>,
|
send: &'a Sender<WidgetUpdate>,
|
||||||
pub cache_width: HashMap<WidgetId, (UiVec2, Len)>,
|
pub cache_width: HashMap<WidgetId, (UiVec2, Len)>,
|
||||||
pub cache_height: HashMap<WidgetId, (UiVec2, Len)>,
|
pub cache_height: HashMap<WidgetId, (UiVec2, Len)>,
|
||||||
@@ -60,32 +60,32 @@ pub struct WidgetInstance {
|
|||||||
pub children: Vec<WidgetId>,
|
pub children: Vec<WidgetId>,
|
||||||
pub resize: ResizeRef,
|
pub resize: ResizeRef,
|
||||||
pub mask: MaskIdx,
|
pub mask: MaskIdx,
|
||||||
pub layer: usize,
|
pub layer: LayerId,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// data to be stored in Ui to create PainterCtxs easily
|
/// data to be stored in Ui to create PainterCtxs easily
|
||||||
/// TODO: actually use this LMAO
|
/// TODO: actually use this LMAO
|
||||||
pub struct PainterData {
|
pub struct PainterData {
|
||||||
|
ui_id: UiId,
|
||||||
pub active: HashMap<WidgetId, WidgetInstance>,
|
pub active: HashMap<WidgetId, WidgetInstance>,
|
||||||
pub layers: PrimitiveLayers,
|
pub layers: PrimitiveLayers,
|
||||||
pub textures: Textures,
|
pub textures: Textures,
|
||||||
pub text: TextData,
|
pub text: TextData,
|
||||||
pub output_size: Vec2,
|
pub output_size: Vec2,
|
||||||
pub modules: Modules,
|
|
||||||
pub px_dependent: HashSet<WidgetId>,
|
pub px_dependent: HashSet<WidgetId>,
|
||||||
pub masks: TrackedArena<Mask, u32>,
|
pub masks: TrackedArena<Mask, u32>,
|
||||||
send: Sender<WidgetUpdate>,
|
send: Sender<WidgetUpdate>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PainterData {
|
impl PainterData {
|
||||||
pub fn new(send: Sender<WidgetUpdate>) -> Self {
|
pub fn new(ui_id: UiId, send: Sender<WidgetUpdate>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
ui_id,
|
||||||
active: Default::default(),
|
active: Default::default(),
|
||||||
layers: Default::default(),
|
layers: Default::default(),
|
||||||
textures: Default::default(),
|
textures: Default::default(),
|
||||||
text: Default::default(),
|
text: Default::default(),
|
||||||
output_size: Default::default(),
|
output_size: Default::default(),
|
||||||
modules: Default::default(),
|
|
||||||
px_dependent: Default::default(),
|
px_dependent: Default::default(),
|
||||||
masks: Default::default(),
|
masks: Default::default(),
|
||||||
send,
|
send,
|
||||||
@@ -175,7 +175,7 @@ impl<'a> PainterCtx<'a> {
|
|||||||
return finish(self, resize);
|
return finish(self, resize);
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(active) = self.remove(id) else {
|
let Some(active) = self.remove(id, false) else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -231,7 +231,7 @@ impl<'a> PainterCtx<'a> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// if not, then maintain resize and track old children to remove unneeded
|
// if not, then maintain resize and track old children to remove unneeded
|
||||||
let active = self.remove(id).unwrap();
|
let active = self.remove(id, false).unwrap();
|
||||||
old_children = active.children;
|
old_children = active.children;
|
||||||
resize = active.resize;
|
resize = active.resize;
|
||||||
}
|
}
|
||||||
@@ -293,10 +293,10 @@ impl<'a> PainterCtx<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update modules
|
// run events
|
||||||
for m in self.modules.iter_mut() {
|
id.map_event_managers(|m| {
|
||||||
m.on_draw(&instance);
|
m.draw(self.ui_id, &instance);
|
||||||
}
|
});
|
||||||
self.active.insert(id, instance);
|
self.active.insert(id, instance);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -307,9 +307,6 @@ impl<'a> PainterCtx<'a> {
|
|||||||
*region = region.outside(&from).within(&to);
|
*region = region.outside(&from).within(&to);
|
||||||
}
|
}
|
||||||
active.region = active.region.outside(&from).within(&to);
|
active.region = active.region.outside(&from).within(&to);
|
||||||
for m in self.modules.iter_mut() {
|
|
||||||
m.on_move(active);
|
|
||||||
}
|
|
||||||
// children will not be changed, so this technically should not be needed
|
// children will not be changed, so this technically should not be needed
|
||||||
// probably need unsafe
|
// probably need unsafe
|
||||||
let children = active.children.clone();
|
let children = active.children.clone();
|
||||||
@@ -319,7 +316,7 @@ impl<'a> PainterCtx<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// NOTE: instance textures are cleared and self.textures freed
|
/// NOTE: instance textures are cleared and self.textures freed
|
||||||
fn remove(&mut self, id: WidgetId) -> Option<WidgetInstance> {
|
fn remove(&mut self, id: WidgetId, undraw: bool) -> Option<WidgetInstance> {
|
||||||
let mut inst = self.active.remove(&id);
|
let mut inst = self.active.remove(&id);
|
||||||
if let Some(inst) = &mut inst {
|
if let Some(inst) = &mut inst {
|
||||||
for h in &inst.primitives {
|
for h in &inst.primitives {
|
||||||
@@ -330,15 +327,17 @@ impl<'a> PainterCtx<'a> {
|
|||||||
}
|
}
|
||||||
inst.textures.clear();
|
inst.textures.clear();
|
||||||
self.textures.free();
|
self.textures.free();
|
||||||
for m in self.modules.iter_mut() {
|
if undraw {
|
||||||
m.on_undraw(inst);
|
id.map_event_managers(|m| {
|
||||||
|
m.undraw(self.ui_id, inst);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inst
|
inst
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_rec(&mut self, id: WidgetId) -> Option<WidgetInstance> {
|
fn remove_rec(&mut self, id: WidgetId) -> Option<WidgetInstance> {
|
||||||
let inst = self.remove(id);
|
let inst = self.remove(id, true);
|
||||||
if let Some(inst) = &inst {
|
if let Some(inst) = &inst {
|
||||||
for c in &inst.children {
|
for c in &inst.children {
|
||||||
self.remove_rec(*c);
|
self.remove_rec(*c);
|
||||||
@@ -351,12 +350,12 @@ impl<'a> PainterCtx<'a> {
|
|||||||
impl PainterData {
|
impl PainterData {
|
||||||
fn ctx(&mut self, needs_redraw: HashSet<WidgetId>) -> PainterCtx<'_> {
|
fn ctx(&mut self, needs_redraw: HashSet<WidgetId>) -> PainterCtx<'_> {
|
||||||
PainterCtx {
|
PainterCtx {
|
||||||
|
ui_id: self.ui_id,
|
||||||
active: &mut self.active,
|
active: &mut self.active,
|
||||||
layers: &mut self.layers,
|
layers: &mut self.layers,
|
||||||
textures: &mut self.textures,
|
textures: &mut self.textures,
|
||||||
text: &mut self.text,
|
text: &mut self.text,
|
||||||
output_size: self.output_size,
|
output_size: self.output_size,
|
||||||
modules: &mut self.modules,
|
|
||||||
masks: &mut self.masks,
|
masks: &mut self.masks,
|
||||||
send: &self.send,
|
send: &self.send,
|
||||||
cache_width: Default::default(),
|
cache_width: Default::default(),
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ use std::ops::{Index, IndexMut};
|
|||||||
|
|
||||||
use crate::render::{MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst, Primitives};
|
use crate::render::{MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst, Primitives};
|
||||||
|
|
||||||
|
pub type LayerId = usize;
|
||||||
|
|
||||||
struct LayerNode<T> {
|
struct LayerNode<T> {
|
||||||
next: Ptr,
|
next: Ptr,
|
||||||
prev: Ptr,
|
prev: Ptr,
|
||||||
|
|||||||
@@ -5,21 +5,27 @@ use crate::{
|
|||||||
IdLike, PainterData, PixelRegion, TextureHandle, Vec2, WidgetHandle, WidgetId,
|
IdLike, PainterData, PixelRegion, TextureHandle, Vec2, WidgetHandle, WidgetId,
|
||||||
WidgetInstance, WidgetLike, WidgetUpdate,
|
WidgetInstance, WidgetLike, WidgetUpdate,
|
||||||
},
|
},
|
||||||
util::HashSet,
|
util::{HashMap, HashSet, Id, StaticIdTracker},
|
||||||
};
|
};
|
||||||
use std::sync::mpsc::{Receiver, channel};
|
use std::sync::mpsc::{Receiver, channel};
|
||||||
|
|
||||||
|
static ID_TRACKER: StaticIdTracker = StaticIdTracker::new();
|
||||||
|
pub type UiId = Id;
|
||||||
|
|
||||||
pub struct Ui {
|
pub struct Ui {
|
||||||
|
id: UiId,
|
||||||
pub(crate) data: PainterData,
|
pub(crate) data: PainterData,
|
||||||
root: Option<WidgetHandle>,
|
root: Option<WidgetHandle>,
|
||||||
updates: HashSet<WidgetId>,
|
updates: HashSet<WidgetId>,
|
||||||
free: Vec<WidgetId>,
|
|
||||||
recv: Receiver<WidgetUpdate>,
|
recv: Receiver<WidgetUpdate>,
|
||||||
full_redraw: bool,
|
full_redraw: bool,
|
||||||
resized: bool,
|
resized: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
|
pub fn id(&self) -> UiId {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
|
||||||
self.root = Some(w.add(self));
|
self.root = Some(w.add(self));
|
||||||
self.full_redraw = true;
|
self.full_redraw = true;
|
||||||
@@ -39,10 +45,10 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn redraw_all(&mut self) {
|
fn redraw_all(&mut self) {
|
||||||
for (_, inst) in self.data.active.drain() {
|
for (id, inst) in self.data.active.drain() {
|
||||||
for m in self.data.modules.iter_mut() {
|
id.map_event_managers(|m| {
|
||||||
m.on_undraw(&inst);
|
m.undraw(self.id, &inst);
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
// free before bc nothing should exist
|
// free before bc nothing should exist
|
||||||
self.free();
|
self.free();
|
||||||
@@ -52,15 +58,8 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn update(&mut self) -> bool {
|
pub fn update(&mut self) -> bool {
|
||||||
for update in self.recv.try_iter() {
|
for id in self.recv.try_iter() {
|
||||||
match update {
|
self.updates.insert(id);
|
||||||
WidgetUpdate::Drop(id) => {
|
|
||||||
self.free.push(id);
|
|
||||||
}
|
|
||||||
WidgetUpdate::Mutate(id) => {
|
|
||||||
self.updates.insert(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if self.full_redraw {
|
if self.full_redraw {
|
||||||
self.redraw_all();
|
self.redraw_all();
|
||||||
@@ -85,11 +84,6 @@ impl Ui {
|
|||||||
|
|
||||||
/// free any resources that don't have references anymore
|
/// free any resources that don't have references anymore
|
||||||
fn free(&mut self) {
|
fn free(&mut self) {
|
||||||
for id in self.free.drain(..) {
|
|
||||||
for m in self.data.modules.iter_mut() {
|
|
||||||
m.on_remove(&id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.data.textures.free();
|
self.data.textures.free();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,6 +95,10 @@ impl Ui {
|
|||||||
self.data.active.len()
|
self.data.active.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn active(&self) -> &HashMap<WidgetId, WidgetInstance> {
|
||||||
|
&self.data.active
|
||||||
|
}
|
||||||
|
|
||||||
pub fn debug_layers(&self) {
|
pub fn debug_layers(&self) {
|
||||||
for ((idx, depth), primitives) in self.data.layers.iter_depth() {
|
for ((idx, depth), primitives) in self.data.layers.iter_depth() {
|
||||||
let indent = " ".repeat(depth * 2);
|
let indent = " ".repeat(depth * 2);
|
||||||
@@ -141,14 +139,21 @@ impl Ui {
|
|||||||
impl Default for Ui {
|
impl Default for Ui {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
let (send, recv) = channel();
|
let (send, recv) = channel();
|
||||||
|
let id = ID_TRACKER.next();
|
||||||
Self {
|
Self {
|
||||||
data: PainterData::new(send),
|
id,
|
||||||
|
data: PainterData::new(id, send),
|
||||||
root: Default::default(),
|
root: Default::default(),
|
||||||
updates: Default::default(),
|
updates: Default::default(),
|
||||||
free: Default::default(),
|
|
||||||
full_redraw: false,
|
full_redraw: false,
|
||||||
recv,
|
recv,
|
||||||
resized: false,
|
resized: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Drop for Ui {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
ID_TRACKER.free(self.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
40
core/src/layout/widget/data.rs
Normal file
40
core/src/layout/widget/data.rs
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
use std::{any::TypeId, sync::mpsc::Sender};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
layout::{EVENT_TYPES, EventManager, WIDGETS, WidgetId, WidgetUpdate},
|
||||||
|
util::{HashSet, RefGuardMut},
|
||||||
|
};
|
||||||
|
|
||||||
|
pub struct WidgetData<W: ?Sized> {
|
||||||
|
pub id: WidgetId,
|
||||||
|
pub send: Option<Sender<WidgetUpdate>>,
|
||||||
|
pub label: String,
|
||||||
|
pub event_managers: HashSet<TypeId>,
|
||||||
|
pub widget: W,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: ?Sized> WidgetData<W> {
|
||||||
|
pub(crate) fn event_managers(&self) -> impl Iterator<Item = RefGuardMut<'_, dyn EventManager>> {
|
||||||
|
self.event_managers.iter().map(|id| {
|
||||||
|
EVENT_TYPES
|
||||||
|
.lock()
|
||||||
|
.unwrap()
|
||||||
|
.get_mut(id)
|
||||||
|
.unwrap()
|
||||||
|
.clone()
|
||||||
|
.get_take_mut()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<W: ?Sized> Drop for WidgetData<W> {
|
||||||
|
fn drop(&mut self) {
|
||||||
|
WIDGETS.remove(self.id);
|
||||||
|
for mut m in self.event_managers() {
|
||||||
|
// refer to src/layout/event.rs for why this is like this
|
||||||
|
let stuff = m.remove(self.id);
|
||||||
|
drop(m);
|
||||||
|
drop(stuff);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,11 @@
|
|||||||
use std::{
|
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized};
|
||||||
marker::Unsize,
|
|
||||||
mem::MaybeUninit,
|
|
||||||
ops::{CoerceUnsized, Deref, DerefMut},
|
|
||||||
sync::mpsc::Sender,
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{IdFnTag, IdTag, Ui, WIDGETS, Widget, WidgetLike},
|
layout::{EventManager, IdFnTag, IdTag, Ui, WIDGETS, Widget, WidgetData, WidgetLike},
|
||||||
util::{Handle, Ref, RefMap, RefMapMut, RefMut, WeakHandle},
|
util::{
|
||||||
|
Handle, MappedRefGuard, MappedRefGuardMut, Ref, RefGuard, RefGuardMut, RefMap, RefMapMut,
|
||||||
|
RefMut, WeakHandle,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(super) type SlotTy = u32;
|
pub(super) type SlotTy = u32;
|
||||||
@@ -24,17 +22,7 @@ pub struct WidgetHandle<W: ?Sized = dyn Widget>(WidgetId, Handle<WidgetData<W>>)
|
|||||||
/// A weak handle to a widget. Implements Copy so you can easily pass into closures
|
/// A weak handle to a widget. Implements Copy so you can easily pass into closures
|
||||||
pub struct WidgetRef<W: ?Sized = dyn Widget>(WidgetId, *const W);
|
pub struct WidgetRef<W: ?Sized = dyn Widget>(WidgetId, *const W);
|
||||||
|
|
||||||
pub(crate) struct WidgetData<W: ?Sized> {
|
pub type WidgetUpdate = WidgetId;
|
||||||
pub id: WidgetId,
|
|
||||||
pub send: Option<Sender<WidgetUpdate>>,
|
|
||||||
pub label: String,
|
|
||||||
pub widget: W,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub enum WidgetUpdate {
|
|
||||||
Drop(WidgetId),
|
|
||||||
Mutate(WidgetId),
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: Widget> WidgetHandle<W> {
|
impl<W: Widget> WidgetHandle<W> {
|
||||||
pub(super) fn new(id: WidgetId, widget: W) -> Self {
|
pub(super) fn new(id: WidgetId, widget: W) -> Self {
|
||||||
@@ -49,6 +37,7 @@ impl<W: Widget> WidgetHandle<W> {
|
|||||||
widget,
|
widget,
|
||||||
send: None,
|
send: None,
|
||||||
label,
|
label,
|
||||||
|
event_managers: Default::default(),
|
||||||
}
|
}
|
||||||
.into(),
|
.into(),
|
||||||
)
|
)
|
||||||
@@ -82,16 +71,20 @@ impl<W: ?Sized> WidgetHandle<W> {
|
|||||||
pub fn get_mut(&self) -> RefMapMut<'_, W> {
|
pub fn get_mut(&self) -> RefMapMut<'_, W> {
|
||||||
let inner = self.1.get_mut();
|
let inner = self.1.get_mut();
|
||||||
if let Some(send) = &inner.send {
|
if let Some(send) = &inner.send {
|
||||||
let _ = send.send(WidgetUpdate::Mutate(self.0));
|
let _ = send.send(self.0);
|
||||||
}
|
}
|
||||||
RefMut::map(inner, |i| &mut i.widget)
|
RefMut::map(inner, |i| &mut i.widget)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn data(&self) -> Ref<'_, WidgetData<W>> {
|
||||||
|
self.1.get()
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn data_mut(&self) -> RefMut<'_, WidgetData<W>> {
|
pub(crate) fn data_mut(&self) -> RefMut<'_, WidgetData<W>> {
|
||||||
self.1.get_mut()
|
self.1.get_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut_quiet(&self) -> RefMapMut<'_, W> {
|
pub(crate) fn get_mut_quiet(&self) -> RefMapMut<'_, W> {
|
||||||
RefMut::map(self.1.get_mut(), |i| &mut i.widget)
|
RefMut::map(self.1.get_mut(), |i| &mut i.widget)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,65 +102,53 @@ impl<W: ?Sized> WidgetHandle<W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn weak(&self) -> WidgetRef<W> {
|
pub fn weak(&self) -> WidgetRef<W> {
|
||||||
WidgetRef(self.0, null_ptr())
|
WidgetRef::new(self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub type WRef<'a, W> = MappedRefGuard<'a, WidgetData<W>, W>;
|
||||||
|
pub type WRefMut<'a, W> = MappedRefGuardMut<'a, WidgetData<W>, W>;
|
||||||
|
|
||||||
impl<W: Widget> WidgetRef<W> {
|
impl<W: Widget> WidgetRef<W> {
|
||||||
fn handle(&self) -> Handle<WidgetData<W>> {
|
fn handle(&self) -> Handle<WidgetData<W>> {
|
||||||
WIDGETS.get_type(self.0).unwrap()
|
WIDGETS.get_type(self.0).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get<'a>(&self) -> WRef<'a, W> {
|
pub fn get<'a>(&self) -> WRef<'a, W> {
|
||||||
let handle = self.handle();
|
RefGuard::map(self.handle().get_take(), |i| &mut i.widget)
|
||||||
WRef {
|
|
||||||
val: unsafe {
|
|
||||||
std::mem::transmute::<RefMap<'_, W>, RefMap<'_, W>>(Ref::map(handle.get(), |i| {
|
|
||||||
&mut i.widget
|
|
||||||
}))
|
|
||||||
},
|
|
||||||
_handle: handle,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_mut<'a>(&self) -> WRefMut<'a, W> {
|
pub fn get_mut<'a>(&self) -> WRefMut<'a, W> {
|
||||||
let handle = self.handle();
|
let inner = self.handle().get_take_mut();
|
||||||
if let Some(send) = &handle.get().send {
|
|
||||||
let _ = send.send(WidgetUpdate::Mutate(self.0));
|
if let Some(send) = &inner.send {
|
||||||
}
|
let _ = send.send(self.0);
|
||||||
WRefMut {
|
|
||||||
val: unsafe {
|
|
||||||
std::mem::transmute::<RefMapMut<'_, W>, RefMapMut<'_, W>>(RefMut::map(
|
|
||||||
handle.get_mut(),
|
|
||||||
|i| &mut i.widget,
|
|
||||||
))
|
|
||||||
},
|
|
||||||
_handle: handle,
|
|
||||||
}
|
}
|
||||||
|
RefGuardMut::map(inner, |i| &mut i.widget)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WRef<'a, W> {
|
impl<W: ?Sized + Widget> WidgetRef<W> {
|
||||||
_handle: Handle<WidgetData<W>>,
|
pub fn data<'a>(&self) -> Option<RefGuard<'a, WidgetData<dyn Widget>>> {
|
||||||
val: RefMap<'a, W>,
|
Some(WIDGETS.get(self.0)?.get_take())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WRefMut<'a, W> {
|
pub(crate) fn data_mut(&self) -> Option<RefGuardMut<'_, WidgetData<dyn Widget>>> {
|
||||||
_handle: Handle<WidgetData<W>>,
|
Some(WIDGETS.get(self.0)?.get_take_mut())
|
||||||
val: RefMapMut<'a, W>,
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetRef<W> {
|
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetRef<W> {
|
||||||
pub fn any(self) -> WidgetRef<dyn Widget> {
|
pub fn any(self) -> WidgetRef<dyn Widget> {
|
||||||
WidgetRef(self.0, null_ptr())
|
WidgetRef::new(self.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn null_ptr<T: ?Sized>() -> *const T {
|
|
||||||
unsafe { MaybeUninit::zeroed().assume_init() }
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: ?Sized> WidgetRef<W> {
|
impl<W: ?Sized> WidgetRef<W> {
|
||||||
|
fn new(id: WidgetId) -> Self {
|
||||||
|
Self(id, unsafe { MaybeUninit::zeroed().assume_init() })
|
||||||
|
}
|
||||||
|
|
||||||
pub fn id(&self) -> WidgetId {
|
pub fn id(&self) -> WidgetId {
|
||||||
self.0
|
self.0
|
||||||
}
|
}
|
||||||
@@ -178,13 +159,17 @@ impl WidgetId {
|
|||||||
pub(crate) fn strong(self) -> Option<WidgetHandle> {
|
pub(crate) fn strong(self) -> Option<WidgetHandle> {
|
||||||
Some(WidgetHandle(self, WIDGETS.get(self)?))
|
Some(WidgetHandle(self, WIDGETS.get(self)?))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl<W: ?Sized> Drop for WidgetData<W> {
|
pub fn weak(self) -> WidgetRef {
|
||||||
fn drop(&mut self) {
|
WidgetRef::new(self)
|
||||||
if let Some(send) = &self.send {
|
}
|
||||||
WIDGETS.remove(self.id);
|
|
||||||
let _ = send.send(WidgetUpdate::Drop(self.id));
|
pub(crate) fn map_event_managers(&self, f: impl Fn(&mut dyn EventManager)) {
|
||||||
|
let Some(data) = self.weak().data() else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
for mut m in data.event_managers() {
|
||||||
|
f(&mut *m)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -252,28 +237,8 @@ impl<W: ?Sized> Copy for WidgetRef<W> {}
|
|||||||
|
|
||||||
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.0 == other.0 && self.1 == other.1
|
self.0 == other.0
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Deref for WRef<'_, T> {
|
|
||||||
type Target = T;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> Deref for WRefMut<'_, T> {
|
|
||||||
type Target = T;
|
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
|
||||||
&self.val
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<T> DerefMut for WRefMut<'_, T> {
|
|
||||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
||||||
&mut self.val
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
unsafe impl<W: ?Sized> Send for WidgetRef<W> {}
|
||||||
|
unsafe impl<W: ?Sized> Sync for WidgetRef<W> {}
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
|
mod data;
|
||||||
mod handle;
|
mod handle;
|
||||||
mod like;
|
mod like;
|
||||||
mod tag;
|
mod tag;
|
||||||
mod widgets;
|
mod widgets;
|
||||||
|
|
||||||
|
pub use data::*;
|
||||||
pub use handle::*;
|
pub use handle::*;
|
||||||
pub use like::*;
|
pub use like::*;
|
||||||
pub use tag::*;
|
pub use tag::*;
|
||||||
|
|||||||
@@ -1,7 +1,4 @@
|
|||||||
use std::{
|
use std::sync::{Mutex, MutexGuard};
|
||||||
any::Any,
|
|
||||||
sync::{Mutex, MutexGuard},
|
|
||||||
};
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
layout::{
|
layout::{
|
||||||
@@ -47,12 +44,7 @@ impl Widgets {
|
|||||||
if slot.genr != id.genr {
|
if slot.genr != id.genr {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(
|
Some(unsafe { slot.data.clone().downcast() }.clone().strong()?)
|
||||||
unsafe { slot.data.clone().downcast() }
|
|
||||||
.unwrap()
|
|
||||||
.clone()
|
|
||||||
.strong()?,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ pub struct Arena<T, I> {
|
|||||||
tracker: IdTracker<I>,
|
tracker: IdTracker<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, I: IdNum> Arena<T, I> {
|
impl<T, I: const IdNum> Arena<T, I> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
data: Vec::new(),
|
data: Vec::new(),
|
||||||
@@ -36,7 +36,7 @@ impl<T, I: IdNum> Arena<T, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, I: IdNum> Default for Arena<T, I> {
|
impl<T, I: const IdNum> Default for Arena<T, I> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
@@ -48,7 +48,7 @@ pub struct TrackedArena<T, I> {
|
|||||||
pub changed: bool,
|
pub changed: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, I: IdNum> TrackedArena<T, I> {
|
impl<T, I: const IdNum> TrackedArena<T, I> {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: Arena::default(),
|
inner: Arena::default(),
|
||||||
@@ -86,7 +86,7 @@ impl<T, I: IdNum> TrackedArena<T, I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, I: IdNum> Default for TrackedArena<T, I> {
|
impl<T, I: const IdNum> Default for TrackedArena<T, I> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,18 +1,21 @@
|
|||||||
|
//! A helper type for Arc<Mutex<T>>
|
||||||
|
//! Currently there are mut versions (of Ref stuff) which don't do anything different under the hood,
|
||||||
|
//! leaving them in for now in case I decide RwLock is a desired feature
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
any::{Any, TypeId},
|
|
||||||
marker::Unsize,
|
marker::Unsize,
|
||||||
ops::CoerceUnsized,
|
ops::{CoerceUnsized, Deref, DerefMut},
|
||||||
sync::{Arc, Mutex, Weak},
|
sync::{Arc, MappedMutexGuard, Mutex, MutexGuard, Weak},
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct Handle<T: ?Sized>(Arc<Mutex<T>>);
|
pub struct Handle<T: ?Sized>(Arc<Mutex<T>>);
|
||||||
pub struct WeakHandle<T: ?Sized>(Weak<Mutex<T>>);
|
pub struct WeakHandle<T: ?Sized>(Weak<Mutex<T>>);
|
||||||
|
|
||||||
pub type Ref<'a, T> = std::sync::MutexGuard<'a, T>;
|
pub type Ref<'a, T> = MutexGuard<'a, T>;
|
||||||
pub type RefMut<'a, T> = std::sync::MutexGuard<'a, T>;
|
pub type RefMut<'a, T> = MutexGuard<'a, T>;
|
||||||
|
|
||||||
pub type RefMap<'a, T> = std::sync::MappedMutexGuard<'a, T>;
|
pub type RefMap<'a, T> = MappedMutexGuard<'a, T>;
|
||||||
pub type RefMapMut<'a, T> = std::sync::MappedMutexGuard<'a, T>;
|
pub type RefMapMut<'a, T> = MappedMutexGuard<'a, T>;
|
||||||
|
|
||||||
impl<T: ?Sized> Handle<T> {
|
impl<T: ?Sized> Handle<T> {
|
||||||
pub fn get(&self) -> Ref<'_, T> {
|
pub fn get(&self) -> Ref<'_, T> {
|
||||||
@@ -23,6 +26,30 @@ impl<T: ?Sized> Handle<T> {
|
|||||||
self.0.lock().unwrap()
|
self.0.lock().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_take<'a>(self) -> RefGuard<'a, T> {
|
||||||
|
let handle = self;
|
||||||
|
RefGuard {
|
||||||
|
guard: unsafe {
|
||||||
|
std::mem::transmute::<MutexGuard<'_, T>, MutexGuard<'_, T>>(
|
||||||
|
handle.0.lock().unwrap(),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_take_mut<'a>(self) -> RefGuardMut<'a, T> {
|
||||||
|
let handle = self;
|
||||||
|
RefGuardMut {
|
||||||
|
guard: unsafe {
|
||||||
|
std::mem::transmute::<MutexGuard<'_, T>, MutexGuard<'_, T>>(
|
||||||
|
handle.0.lock().unwrap(),
|
||||||
|
)
|
||||||
|
},
|
||||||
|
handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn refs(&self) -> usize {
|
pub fn refs(&self) -> usize {
|
||||||
Arc::strong_count(&self.0)
|
Arc::strong_count(&self.0)
|
||||||
}
|
}
|
||||||
@@ -30,6 +57,20 @@ impl<T: ?Sized> Handle<T> {
|
|||||||
pub fn weak(&self) -> WeakHandle<T> {
|
pub fn weak(&self) -> WeakHandle<T> {
|
||||||
WeakHandle(Arc::downgrade(&self.0))
|
WeakHandle(Arc::downgrade(&self.0))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Safety
|
||||||
|
/// you must guarantee the type outside
|
||||||
|
/// ideally you check the typeid, but this is often used
|
||||||
|
/// when the trait object is wrapped one or more times
|
||||||
|
/// and you'd have to implement each one individually
|
||||||
|
pub unsafe fn downcast<U: 'static>(self) -> Handle<U>
|
||||||
|
where
|
||||||
|
T: 'static,
|
||||||
|
{
|
||||||
|
let raw: *const Mutex<T> = Arc::into_raw(self.0);
|
||||||
|
let raw: *const Mutex<U> = raw.cast();
|
||||||
|
Handle(unsafe { Arc::from_raw(raw) })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> WeakHandle<T> {
|
impl<T: ?Sized> WeakHandle<T> {
|
||||||
@@ -43,13 +84,13 @@ impl<T: ?Sized> WeakHandle<T> {
|
|||||||
|
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// you must guarantee the type outside
|
/// you must guarantee the type outside
|
||||||
pub unsafe fn downcast<U: 'static>(self) -> Option<WeakHandle<U>>
|
pub unsafe fn downcast<U: 'static>(self) -> WeakHandle<U>
|
||||||
where
|
where
|
||||||
T: 'static,
|
T: 'static,
|
||||||
{
|
{
|
||||||
let raw: *const Mutex<T> = self.0.into_raw();
|
let raw: *const Mutex<T> = self.0.into_raw();
|
||||||
let raw: *const Mutex<U> = raw.cast();
|
let raw: *const Mutex<U> = raw.cast();
|
||||||
Some(WeakHandle(unsafe { Weak::from_raw(raw) }))
|
WeakHandle(unsafe { Weak::from_raw(raw) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,3 +120,108 @@ impl<T> From<T> for Handle<T> {
|
|||||||
|
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Handle<U>> for Handle<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<Handle<U>> for Handle<T> {}
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WeakHandle<U>> for WeakHandle<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WeakHandle<U>> for WeakHandle<T> {}
|
||||||
|
|
||||||
|
// yucky
|
||||||
|
|
||||||
|
// TODO: is drop order important here?
|
||||||
|
// something stupid could happen that I don't know about
|
||||||
|
// if handle is dropped before guard
|
||||||
|
pub struct RefGuard<'a, T: ?Sized> {
|
||||||
|
guard: MutexGuard<'a, T>,
|
||||||
|
handle: Handle<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RefGuardMut<'a, T: ?Sized> {
|
||||||
|
guard: MutexGuard<'a, T>,
|
||||||
|
handle: Handle<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MappedRefGuard<'a, T: ?Sized, U> {
|
||||||
|
guard: MappedMutexGuard<'a, U>,
|
||||||
|
handle: Handle<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct MappedRefGuardMut<'a, T: ?Sized, U> {
|
||||||
|
guard: MappedMutexGuard<'a, U>,
|
||||||
|
handle: Handle<T>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> RefGuard<'a, T> {
|
||||||
|
pub fn map<U>(s: Self, f: impl FnOnce(&mut T) -> &mut U) -> MappedRefGuard<'a, T, U> {
|
||||||
|
MappedRefGuard {
|
||||||
|
guard: MutexGuard::map(s.guard, f),
|
||||||
|
handle: s.handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized> RefGuardMut<'a, T> {
|
||||||
|
pub fn map<U>(s: Self, f: impl FnOnce(&mut T) -> &mut U) -> MappedRefGuardMut<'a, T, U> {
|
||||||
|
MappedRefGuardMut {
|
||||||
|
guard: MutexGuard::map(s.guard, f),
|
||||||
|
handle: s.handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized, U> MappedRefGuard<'a, T, U> {
|
||||||
|
pub fn map<U2>(s: Self, f: impl FnOnce(&mut U) -> &mut U2) -> MappedRefGuard<'a, T, U2> {
|
||||||
|
MappedRefGuard {
|
||||||
|
guard: MappedMutexGuard::map(s.guard, f),
|
||||||
|
handle: s.handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: ?Sized, U> MappedRefGuardMut<'a, T, U> {
|
||||||
|
pub fn map<U2>(s: Self, f: impl FnOnce(&mut U) -> &mut U2) -> MappedRefGuardMut<'a, T, U2> {
|
||||||
|
MappedRefGuardMut {
|
||||||
|
guard: MappedMutexGuard::map(s.guard, f),
|
||||||
|
handle: s.handle,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> Deref for RefGuard<'_, T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> Deref for RefGuardMut<'_, T> {
|
||||||
|
type Target = T;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized> DerefMut for RefGuardMut<'_, T> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized, U> Deref for MappedRefGuard<'_, T, U> {
|
||||||
|
type Target = U;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized, U> Deref for MappedRefGuardMut<'_, T, U> {
|
||||||
|
type Target = U;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
&self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: ?Sized, U> DerefMut for MappedRefGuardMut<'_, T, U> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
&mut self.guard
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
use std::sync::Mutex;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
#[derive(Eq, Hash, PartialEq, Debug, Clone, Copy, bytemuck::Zeroable)]
|
#[derive(Eq, Hash, PartialEq, Debug, Clone, Copy, bytemuck::Zeroable)]
|
||||||
pub struct Id<I = u64>(I);
|
pub struct Id<I = u64>(I);
|
||||||
@@ -9,7 +11,14 @@ pub struct IdTracker<I = u64> {
|
|||||||
cur: Id<I>,
|
cur: Id<I>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: IdNum> IdTracker<I> {
|
impl<I: const IdNum> IdTracker<I> {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self {
|
||||||
|
free: Vec::new(),
|
||||||
|
cur: Id(I::first()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[allow(clippy::should_implement_trait)]
|
#[allow(clippy::should_implement_trait)]
|
||||||
pub fn next(&mut self) -> Id<I> {
|
pub fn next(&mut self) -> Id<I> {
|
||||||
if let Some(id) = self.free.pop() {
|
if let Some(id) = self.free.pop() {
|
||||||
@@ -43,22 +52,19 @@ impl<I: IdNum> Id<I> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I: IdNum> Default for IdTracker<I> {
|
impl<I: const IdNum> Default for IdTracker<I> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self::new()
|
||||||
free: Vec::new(),
|
|
||||||
cur: Id(I::first()),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IdNum {
|
pub const trait IdNum {
|
||||||
fn first() -> Self;
|
fn first() -> Self;
|
||||||
fn next(&self) -> Self;
|
fn next(&self) -> Self;
|
||||||
fn idx(&self) -> usize;
|
fn idx(&self) -> usize;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IdNum for u64 {
|
impl const IdNum for u64 {
|
||||||
fn first() -> Self {
|
fn first() -> Self {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
@@ -72,7 +78,7 @@ impl IdNum for u64 {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IdNum for u32 {
|
impl const IdNum for u32 {
|
||||||
fn first() -> Self {
|
fn first() -> Self {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
@@ -85,3 +91,33 @@ impl IdNum for u32 {
|
|||||||
*self as usize
|
*self as usize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct StaticIdTracker<I = u64>(Mutex<IdTracker<I>>);
|
||||||
|
|
||||||
|
impl<I: const IdNum> StaticIdTracker<I> {
|
||||||
|
pub const fn new() -> Self {
|
||||||
|
Self(Mutex::new(IdTracker::new()))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(clippy::should_implement_trait)]
|
||||||
|
pub fn next(&self) -> Id<I> {
|
||||||
|
let mut s = self.0.lock().unwrap();
|
||||||
|
if let Some(id) = s.free.pop() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
let next = s.cur.next();
|
||||||
|
std::mem::replace(&mut s.cur, next)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
|
pub fn free(&self, id: Id<I>) {
|
||||||
|
let mut s = self.0.lock().unwrap();
|
||||||
|
s.free.push(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I: const IdNum> Default for StaticIdTracker<I> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,6 +24,12 @@ impl RefCounter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for RefCounter {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self::new()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Clone for RefCounter {
|
impl Clone for RefCounter {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
self.0.fetch_add(1, Ordering::Release);
|
self.0.fetch_add(1, Ordering::Release);
|
||||||
|
|||||||
@@ -2,12 +2,13 @@ extern crate proc_macro;
|
|||||||
use proc_macro::TokenStream;
|
use proc_macro::TokenStream;
|
||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{
|
use syn::{
|
||||||
Block, Ident, Signature, Token, Visibility,
|
Attribute, Block, Ident, ItemTrait, Signature, Token, Visibility,
|
||||||
parse::{Parse, ParseStream, Result},
|
parse::{Parse, ParseStream, Result},
|
||||||
parse_macro_input,
|
parse_macro_input, parse_quote,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Input {
|
struct Input {
|
||||||
|
attrs: Vec<Attribute>,
|
||||||
vis: Visibility,
|
vis: Visibility,
|
||||||
name: Ident,
|
name: Ident,
|
||||||
fns: Vec<InputFn>,
|
fns: Vec<InputFn>,
|
||||||
@@ -20,6 +21,7 @@ struct InputFn {
|
|||||||
|
|
||||||
impl Parse for Input {
|
impl Parse for Input {
|
||||||
fn parse(input: ParseStream) -> Result<Self> {
|
fn parse(input: ParseStream) -> Result<Self> {
|
||||||
|
let attrs = input.call(Attribute::parse_outer)?;
|
||||||
let vis = input.parse()?;
|
let vis = input.parse()?;
|
||||||
input.parse::<Token![trait]>()?;
|
input.parse::<Token![trait]>()?;
|
||||||
let name = input.parse()?;
|
let name = input.parse()?;
|
||||||
@@ -33,27 +35,23 @@ impl Parse for Input {
|
|||||||
if !input.is_empty() {
|
if !input.is_empty() {
|
||||||
input.error("function expected");
|
input.error("function expected");
|
||||||
}
|
}
|
||||||
Ok(Input { vis, name, fns })
|
Ok(Input {
|
||||||
|
attrs,
|
||||||
|
vis,
|
||||||
|
name,
|
||||||
|
fns,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pub trait $name<W: WidgetLike<Tag>, Tag> {
|
|
||||||
// $(
|
|
||||||
// fn $fn $(<$($T $(: $TT $(+ $TL)?)?,)*>)?($self $(, $arg: $ty)*) -> $ret;
|
|
||||||
// )*
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// impl<W: WidgetLike<Tag>, Tag> $name<W, Tag> for W {
|
|
||||||
// $(
|
|
||||||
// fn $fn $(<$($T $(: $TT $(+ $TL)?)?,)*>)?($self $(, $arg: $ty)*) -> $ret {
|
|
||||||
// $code
|
|
||||||
// }
|
|
||||||
// )*
|
|
||||||
// }
|
|
||||||
|
|
||||||
#[proc_macro]
|
#[proc_macro]
|
||||||
pub fn widget_trait(input: TokenStream) -> TokenStream {
|
pub fn widget_trait(input: TokenStream) -> TokenStream {
|
||||||
let Input { vis, name, fns } = parse_macro_input!(input as Input);
|
let Input {
|
||||||
|
attrs,
|
||||||
|
vis,
|
||||||
|
name,
|
||||||
|
fns,
|
||||||
|
} = parse_macro_input!(input as Input);
|
||||||
|
|
||||||
let sigs: Vec<_> = fns.iter().map(|f| f.sig.clone()).collect();
|
let sigs: Vec<_> = fns.iter().map(|f| f.sig.clone()).collect();
|
||||||
let impls: Vec<_> = fns
|
let impls: Vec<_> = fns
|
||||||
@@ -61,10 +59,16 @@ pub fn widget_trait(input: TokenStream) -> TokenStream {
|
|||||||
.map(|InputFn { sig, body }| quote! { #sig #body })
|
.map(|InputFn { sig, body }| quote! { #sig #body })
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
TokenStream::from(quote! {
|
let mut trai: ItemTrait = parse_quote!(
|
||||||
#vis trait #name<WL: WidgetLike<Tag>, Tag> {
|
#vis trait #name<WL: WidgetLike<Tag>, Tag> {
|
||||||
#(#sigs;)*
|
#(#sigs;)*
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
trai.attrs = attrs;
|
||||||
|
|
||||||
|
TokenStream::from(quote! {
|
||||||
|
#trai
|
||||||
|
|
||||||
impl<WL: WidgetLike<Tag>, Tag> #name<WL, Tag> for WL {
|
impl<WL: WidgetLike<Tag>, Tag> #name<WL, Tag> for WL {
|
||||||
#(#impls)*
|
#(#impls)*
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ impl DefaultAppState for Client {
|
|||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |ctx| {
|
||||||
let child = image(include_bytes!("assets/sungals.png"))
|
let child = image(include_bytes!("assets/sungals.png"))
|
||||||
.center()
|
.center()
|
||||||
.add(ctx.ui);
|
.add(ctx.data.ui);
|
||||||
span_add_.get_mut().children.push(child);
|
span_add_.get_mut().children.push(child);
|
||||||
})
|
})
|
||||||
.sized((150, 150))
|
.sized((150, 150))
|
||||||
@@ -113,7 +113,9 @@ impl DefaultAppState for Client {
|
|||||||
.text_align(Align::LEFT)
|
.text_align(Align::LEFT)
|
||||||
.wrap(true)
|
.wrap(true)
|
||||||
.attr::<Selectable>(());
|
.attr::<Selectable>(());
|
||||||
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx.ui);
|
let msg_box = text
|
||||||
|
.background(rect(Color::WHITE.darker(0.5)))
|
||||||
|
.add(&mut Ui::new());
|
||||||
texts_.get_mut().children.push(msg_box);
|
texts_.get_mut().children.push(msg_box);
|
||||||
})
|
})
|
||||||
.add(ui);
|
.add(ui);
|
||||||
@@ -126,7 +128,7 @@ impl DefaultAppState for Client {
|
|||||||
add_text.width(rest(1)),
|
add_text.width(rest(1)),
|
||||||
Rect::new(Color::GREEN)
|
Rect::new(Color::GREEN)
|
||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |ctx| {
|
||||||
ctx.ui.run_event(ctx.state, add_text_, Submit, ());
|
Events::<Submit, _>::run(add_text_.id(), &mut (), ctx.state);
|
||||||
})
|
})
|
||||||
.sized((40, 40)),
|
.sized((40, 40)),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,20 +2,22 @@ use crate::{default::UiState, prelude::*};
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
use winit::dpi::{LogicalPosition, LogicalSize};
|
use winit::dpi::{LogicalPosition, LogicalSize};
|
||||||
|
|
||||||
|
event_ctx!(UiState);
|
||||||
|
|
||||||
pub struct Selector;
|
pub struct Selector;
|
||||||
|
|
||||||
impl<W: 'static> WidgetAttr<W> for Selector {
|
impl<W: Widget + 'static> WidgetAttr<W> for Selector {
|
||||||
type Input = WidgetRef<TextEdit>;
|
type Input = WidgetRef<TextEdit>;
|
||||||
|
|
||||||
fn run(ui: &mut Ui, container: WidgetRef<W>, id: Self::Input) {
|
fn run(ui: &mut Ui, container: WidgetRef<W>, id: Self::Input) {
|
||||||
ui.register_event(container, CursorSense::click_or_drag(), move |mut ctx| {
|
container.on(CursorSense::click_or_drag(), move |ctx| {
|
||||||
let ui = ctx.ui;
|
let ui = &mut ctx.data.ui;
|
||||||
let region = ui.window_region(&id).unwrap();
|
let region = ui.window_region(&id).unwrap();
|
||||||
let id_pos = region.top_left;
|
let id_pos = region.top_left;
|
||||||
let container_pos = ui.window_region(&container).unwrap().top_left;
|
let container_pos = ui.window_region(&container).unwrap().top_left;
|
||||||
ctx.data.cursor += container_pos - id_pos;
|
let pos = ctx.data.pos + container_pos - id_pos;
|
||||||
ctx.data.size = region.size();
|
let size = region.size();
|
||||||
select(ui, id, ctx.state, ctx.data);
|
select(ui, id, ctx.state, pos, size, ctx.data.sense.is_dragging());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,18 +28,31 @@ impl WidgetAttr<TextEdit> for Selectable {
|
|||||||
type Input = ();
|
type Input = ();
|
||||||
|
|
||||||
fn run(ui: &mut Ui, id: WidgetRef<TextEdit>, _: Self::Input) {
|
fn run(ui: &mut Ui, id: WidgetRef<TextEdit>, _: Self::Input) {
|
||||||
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
|
id.on(CursorSense::click_or_drag(), move |ctx| {
|
||||||
select(ctx.ui, id, ctx.state, ctx.data);
|
select(
|
||||||
|
ctx.data.ui,
|
||||||
|
id,
|
||||||
|
ctx.state,
|
||||||
|
ctx.data.pos,
|
||||||
|
ctx.data.size,
|
||||||
|
ctx.data.sense.is_dragging(),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn select(ui: &mut Ui, id: WidgetRef<TextEdit>, state: &mut UiState, data: CursorData) {
|
fn select(
|
||||||
|
ui: &mut Ui,
|
||||||
|
id: WidgetRef<TextEdit>,
|
||||||
|
state: &mut UiState,
|
||||||
|
pos: Vec2,
|
||||||
|
size: Vec2,
|
||||||
|
dragging: bool,
|
||||||
|
) {
|
||||||
let now = Instant::now();
|
let now = Instant::now();
|
||||||
let recent = (now - state.last_click) < Duration::from_millis(300);
|
let recent = (now - state.last_click) < Duration::from_millis(300);
|
||||||
state.last_click = now;
|
state.last_click = now;
|
||||||
id.get_mut()
|
id.get_mut().select(pos, size, dragging, recent);
|
||||||
.select(data.cursor, data.size, data.sense.is_dragging(), recent);
|
|
||||||
if let Some(region) = ui.window_region(&id) {
|
if let Some(region) = ui.window_region(&id) {
|
||||||
state.window.set_ime_allowed(true);
|
state.window.set_ime_allowed(true);
|
||||||
state.window.set_ime_cursor_area(
|
state.window.set_ime_cursor_area(
|
||||||
|
|||||||
@@ -1,15 +1,9 @@
|
|||||||
use crate::layout::DefaultEvent;
|
use iris_core::layout::Event;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Submit;
|
pub struct Submit;
|
||||||
|
impl Event for Submit {}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Edited;
|
pub struct Edited;
|
||||||
|
impl Event for Edited {}
|
||||||
impl DefaultEvent for Submit {
|
|
||||||
type Data = ();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DefaultEvent for Edited {
|
|
||||||
type Data = ();
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -130,13 +130,13 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
|||||||
ui_state.window.set_ime_allowed(false);
|
ui_state.window.set_ime_allowed(false);
|
||||||
}
|
}
|
||||||
TextInputResult::Submit => {
|
TextInputResult::Submit => {
|
||||||
ui.run_event(app_state, sel, Submit, ());
|
Events::<Submit, _>::run(sel.id(), &mut (), app_state);
|
||||||
}
|
}
|
||||||
TextInputResult::Paste => {
|
TextInputResult::Paste => {
|
||||||
if let Ok(t) = ui_state.clipboard.get_text() {
|
if let Ok(t) = ui_state.clipboard.get_text() {
|
||||||
sel.get_mut().insert(&t);
|
sel.get_mut().insert(&t);
|
||||||
}
|
}
|
||||||
ui.run_event(app_state, sel, Edited, ());
|
Events::<Edited, _>::run(sel.id(), &mut (), app_state);
|
||||||
}
|
}
|
||||||
TextInputResult::Copy(text) => {
|
TextInputResult::Copy(text) => {
|
||||||
if let Err(err) = ui_state.clipboard.set_text(text) {
|
if let Err(err) = ui_state.clipboard.set_text(text) {
|
||||||
@@ -144,7 +144,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TextInputResult::Used => {
|
TextInputResult::Used => {
|
||||||
ui.run_event(app_state, sel, Edited, ());
|
Events::<Edited, _>::run(sel.id(), &mut (), app_state);
|
||||||
}
|
}
|
||||||
TextInputResult::Unused => {}
|
TextInputResult::Unused => {}
|
||||||
}
|
}
|
||||||
|
|||||||
54
src/event.rs
54
src/event.rs
@@ -6,14 +6,14 @@ pub mod eventable {
|
|||||||
|
|
||||||
widget_trait! {
|
widget_trait! {
|
||||||
pub trait Eventable;
|
pub trait Eventable;
|
||||||
fn on<E: Event, Ctx: 'static>(
|
fn on<E: EventAlias, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
|
f: impl for<'a> EventIdFn<Ctx, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||||
) -> impl WidgetIdFn<WL::Widget> {
|
) -> impl WidgetIdFn<WL::Widget> {
|
||||||
move |ui| {
|
move |ui| {
|
||||||
let id = self.add(ui);
|
let id = self.add(ui);
|
||||||
ui.register_widget_event(id.weak(), event, f);
|
Events::<E::Event, Ctx>::register(id.weak(), event.into_event(), f);
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,53 +26,37 @@ macro_rules! event_ctx {
|
|||||||
($ty: ty) => {
|
($ty: ty) => {
|
||||||
mod local_event_trait {
|
mod local_event_trait {
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::marker::Sized;
|
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use $crate::prelude::*;
|
use $crate::prelude::*;
|
||||||
|
|
||||||
#[allow(unused)]
|
widget_trait! {
|
||||||
pub trait EventableCtx<W: ?Sized, Tag, Ctx: 'static> {
|
#[allow(unused)]
|
||||||
fn on<E: Event>(
|
pub trait EventableCtx;
|
||||||
|
fn on<E: EventAlias>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||||
) -> impl WidgetIdFn<W> + EventableCtx<W, IdFnTag, Ctx>;
|
) -> impl WidgetIdFn<WL::Widget> {
|
||||||
}
|
|
||||||
|
|
||||||
impl<WL: WidgetLike<Tag>, Tag> EventableCtx<WL::Widget, Tag, $ty> for WL {
|
|
||||||
fn on<E: Event>(
|
|
||||||
self,
|
|
||||||
event: E,
|
|
||||||
f: impl WidgetEventFn<$ty, E::Data, WL::Widget>,
|
|
||||||
) -> impl WidgetIdFn<WL::Widget> + EventableCtx<WL::Widget, IdFnTag, $ty> {
|
|
||||||
eventable::Eventable::on(self, event, f)
|
eventable::Eventable::on(self, event, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
use std::marker::Sized;
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub trait EventableCtxUi<W: ?Sized, Tag, Ctx: 'static>
|
pub trait EventableCtxRef<W: Widget + ?Sized> {
|
||||||
where
|
fn on<E: EventAlias>(
|
||||||
WidgetRef<W>: EventableCtx<W, Tag, Ctx>,
|
self,
|
||||||
{
|
|
||||||
fn on<E: Event>(
|
|
||||||
&mut self,
|
|
||||||
widget: WidgetRef<W>,
|
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, W>,
|
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, W>,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<W: ?Sized + 'static, Tag> EventableCtxUi<W, Tag, $ty> for Ui
|
impl<W: Widget + ?Sized> EventableCtxRef<W> for WidgetRef<W> {
|
||||||
where
|
fn on<E: EventAlias>(
|
||||||
WidgetRef<W>: EventableCtx<W, Tag, $ty>,
|
self,
|
||||||
{
|
|
||||||
fn on<E: Event>(
|
|
||||||
&mut self,
|
|
||||||
widget: WidgetRef<W>,
|
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<$ty, E::Data, W>,
|
f: impl for<'a> EventIdFn<$ty, <E::Event as Event>::Data<'a>, W>,
|
||||||
) {
|
) {
|
||||||
self.register_widget_event(widget, event, f);
|
Events::<E::Event, $ty>::register(self, event.into_event(), f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,5 @@
|
|||||||
|
use crate::layout::{UiRegion, Vec2};
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use crate::{
|
|
||||||
layout::{UiModule, UiRegion, Vec2},
|
|
||||||
util::HashMap,
|
|
||||||
};
|
|
||||||
use std::{
|
use std::{
|
||||||
ops::{BitOr, Deref, DerefMut},
|
ops::{BitOr, Deref, DerefMut},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
@@ -26,8 +23,22 @@ pub enum CursorSense {
|
|||||||
Scroll,
|
Scroll,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
pub struct CursorSenses(Vec<CursorSense>);
|
pub struct CursorSenses(Vec<CursorSense>);
|
||||||
|
|
||||||
|
impl Event for CursorSenses {
|
||||||
|
type Data<'a> = CursorData<'a>;
|
||||||
|
type State = SensorState;
|
||||||
|
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
|
||||||
|
if let Some(sense) = should_run(self, data.cursor, data.hover) {
|
||||||
|
data.sense = sense;
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl CursorSense {
|
impl CursorSense {
|
||||||
pub fn click() -> Self {
|
pub fn click() -> Self {
|
||||||
Self::PressStart(CursorButton::Left)
|
Self::PressStart(CursorButton::Left)
|
||||||
@@ -72,6 +83,16 @@ impl CursorButtons {
|
|||||||
self.middle.end_frame();
|
self.middle.end_frame();
|
||||||
self.right.end_frame();
|
self.right.end_frame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn iter(&self) -> impl Iterator<Item = (CursorButton, &ActivationState)> {
|
||||||
|
[
|
||||||
|
CursorButton::Left,
|
||||||
|
CursorButton::Middle,
|
||||||
|
CursorButton::Right,
|
||||||
|
]
|
||||||
|
.into_iter()
|
||||||
|
.map(|b| (b, self.select(&b)))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CursorState {
|
impl CursorState {
|
||||||
@@ -100,70 +121,25 @@ pub struct Sensor<Ctx, Data> {
|
|||||||
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
pub f: Rc<dyn EventFn<Ctx, Data>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type SensorMap<Ctx, Data> = HashMap<WidgetId, SensorGroup<Ctx, Data>>;
|
|
||||||
pub type SenseShape = UiRegion;
|
pub type SenseShape = UiRegion;
|
||||||
pub struct SensorGroup<Ctx, Data> {
|
|
||||||
|
#[derive(Default, Debug)]
|
||||||
|
pub struct SensorState {
|
||||||
pub hover: ActivationState,
|
pub hover: ActivationState,
|
||||||
pub sensors: Vec<Sensor<Ctx, Data>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
pub struct CursorData<'a> {
|
||||||
pub struct CursorData {
|
/// where this widget was hit
|
||||||
pub cursor: Vec2,
|
pub pos: Vec2,
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
pub scroll_delta: Vec2,
|
pub scroll_delta: Vec2,
|
||||||
/// the (first) sense that triggered this event
|
pub hover: ActivationState,
|
||||||
/// the senses are checked in order
|
pub cursor: &'a CursorState,
|
||||||
|
pub ui: &'a mut Ui,
|
||||||
|
/// the first sense that triggered this
|
||||||
pub sense: CursorSense,
|
pub sense: CursorSense,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CursorModule<Ctx> {
|
|
||||||
map: SensorMap<Ctx, CursorData>,
|
|
||||||
active: HashMap<usize, HashMap<WidgetId, SenseShape>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Ctx: 'static> UiModule for CursorModule<Ctx> {
|
|
||||||
fn on_draw(&mut self, inst: &WidgetInstance) {
|
|
||||||
if self.map.contains_key(&inst.id) {
|
|
||||||
self.active
|
|
||||||
.entry(inst.layer)
|
|
||||||
.or_default()
|
|
||||||
.insert(inst.id, inst.region);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_undraw(&mut self, inst: &WidgetInstance) {
|
|
||||||
if let Some(layer) = self.active.get_mut(&inst.layer) {
|
|
||||||
layer.remove(&inst.id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_remove(&mut self, id: &WidgetId) {
|
|
||||||
self.map.remove(id);
|
|
||||||
for layer in self.active.values_mut() {
|
|
||||||
layer.remove(id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn on_move(&mut self, inst: &WidgetInstance) {
|
|
||||||
if let Some(map) = self.active.get_mut(&inst.layer)
|
|
||||||
&& let Some(region) = map.get_mut(&inst.id)
|
|
||||||
{
|
|
||||||
*region = inst.region;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Ctx> CursorModule<Ctx> {
|
|
||||||
pub fn merge(&mut self, other: Self) {
|
|
||||||
for (id, group) in other.map {
|
|
||||||
for sensor in group.sensors {
|
|
||||||
self.map.entry(id).or_default().sensors.push(sensor);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait SensorUi {
|
pub trait SensorUi {
|
||||||
fn run_sensors<Ctx: 'static>(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2);
|
fn run_sensors<Ctx: 'static>(&mut self, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2);
|
||||||
}
|
}
|
||||||
@@ -175,55 +151,41 @@ impl SensorUi for Ui {
|
|||||||
cursor: &CursorState,
|
cursor: &CursorState,
|
||||||
window_size: Vec2,
|
window_size: Vec2,
|
||||||
) {
|
) {
|
||||||
CursorModule::<Ctx>::run(self, ctx, cursor, window_size);
|
let ui_id = self.id();
|
||||||
}
|
let layers = std::mem::take(&mut self.data_mut().layers);
|
||||||
}
|
let mut active = std::mem::take(&mut *Events::<CursorSenses, Ctx>::active(ui_id));
|
||||||
|
|
||||||
impl<Ctx: 'static> CursorModule<Ctx> {
|
|
||||||
pub fn run(ui: &mut Ui, ctx: &mut Ctx, cursor: &CursorState, window_size: Vec2) {
|
|
||||||
let layers = std::mem::take(&mut ui.data_mut().layers);
|
|
||||||
let mut module = std::mem::take(ui.data_mut().modules.get_mut::<Self>());
|
|
||||||
|
|
||||||
for i in layers.indices().rev() {
|
for i in layers.indices().rev() {
|
||||||
let Some(list) = module.active.get_mut(&i) else {
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let mut sensed = false;
|
let mut sensed = false;
|
||||||
for (id, shape) in list.iter() {
|
for (id, state) in active.get_mut(&i).into_iter().flatten() {
|
||||||
let group = module.map.get_mut(id).unwrap();
|
let shape = self.active().get(id).unwrap().region;
|
||||||
let region = shape.to_px(window_size);
|
let region = shape.to_px(window_size);
|
||||||
let in_shape = cursor.exists && region.contains(cursor.pos);
|
let in_shape = cursor.exists && region.contains(cursor.pos);
|
||||||
group.hover.update(in_shape);
|
state.hover.update(in_shape);
|
||||||
if group.hover == ActivationState::Off {
|
if state.hover == ActivationState::Off {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
sensed = true;
|
sensed = true;
|
||||||
|
|
||||||
for sensor in &mut group.sensors {
|
let mut data = CursorData {
|
||||||
if let Some(sense) = should_run(&sensor.senses, cursor, group.hover) {
|
pos: cursor.pos - region.top_left,
|
||||||
let data = CursorData {
|
size: region.bot_right - region.top_left,
|
||||||
cursor: cursor.pos - region.top_left,
|
scroll_delta: cursor.scroll_delta,
|
||||||
size: region.bot_right - region.top_left,
|
hover: state.hover,
|
||||||
scroll_delta: cursor.scroll_delta,
|
cursor,
|
||||||
sense,
|
ui: self,
|
||||||
};
|
// this does not have any meaning;
|
||||||
(sensor.f)(EventCtx {
|
// might wanna set up Event to have a prepare stage
|
||||||
ui,
|
sense: CursorSense::Hovering,
|
||||||
state: ctx,
|
};
|
||||||
data,
|
Events::<CursorSenses, Ctx>::run(*id, &mut data, ctx);
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if sensed {
|
if sensed {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let ui_mod = ui.data_mut().modules.get_mut::<Self>();
|
*Events::<CursorSenses, Ctx>::active(ui_id) = active;
|
||||||
std::mem::swap(ui_mod, &mut module);
|
self.data_mut().layers = layers;
|
||||||
ui_mod.merge(module);
|
|
||||||
ui.data_mut().layers = layers;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,66 +253,10 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for CursorSenses {
|
impl EventAlias for CursorSense {
|
||||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
type Event = CursorSenses;
|
||||||
type Data = CursorData;
|
fn into_event(self) -> Self::Event {
|
||||||
}
|
CursorSenses::from(self)
|
||||||
|
|
||||||
impl Event for CursorSense {
|
|
||||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
|
||||||
type Data = CursorData;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: Event<Data = <CursorSenses as Event>::Data> + Into<CursorSenses>, Ctx: 'static>
|
|
||||||
EventModule<E, Ctx> for CursorModule<Ctx>
|
|
||||||
{
|
|
||||||
fn register(&mut self, id: WidgetId, senses: E, f: impl EventFn<Ctx, <E as Event>::Data>) {
|
|
||||||
// TODO: does not add to active if currently active
|
|
||||||
self.map.entry(id).or_default().sensors.push(Sensor {
|
|
||||||
senses: senses.into(),
|
|
||||||
f: Rc::new(f),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
fn run<'a>(
|
|
||||||
&mut self,
|
|
||||||
id: WidgetId,
|
|
||||||
event: E,
|
|
||||||
) -> Option<impl FnOnce(EventCtx<Ctx, <E as Event>::Data>) + use<'a, Ctx, E>> {
|
|
||||||
let senses = event.into();
|
|
||||||
if let Some(group) = self.map.get_mut(&id) {
|
|
||||||
let fs: Vec<_> = group
|
|
||||||
.sensors
|
|
||||||
.iter_mut()
|
|
||||||
.filter_map(|sensor| {
|
|
||||||
if sensor.senses.iter().any(|s| senses.contains(s)) {
|
|
||||||
Some(sensor.f.clone())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.collect();
|
|
||||||
Some(move |ctx: EventCtx<Ctx, CursorData>| {
|
|
||||||
for f in fs {
|
|
||||||
f(EventCtx {
|
|
||||||
state: ctx.state,
|
|
||||||
ui: ctx.ui,
|
|
||||||
data: ctx.data.clone(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<Ctx, Data> Default for SensorGroup<Ctx, Data> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
hover: Default::default(),
|
|
||||||
sensors: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -390,12 +296,3 @@ impl BitOr<CursorSense> for CursorSenses {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Default for CursorModule<Ctx> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
map: Default::default(),
|
|
||||||
active: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user