remove modules and have single event manager (atomics feature parity + preparation for local state)
This commit is contained in:
@@ -1,26 +1,172 @@
|
|||||||
use crate::{Ui, UiModule, Widget, WidgetId, WidgetRef, util::HashMap};
|
use crate::{
|
||||||
|
ActiveData, IdLike, LayerId, Ui, Widget, WidgetData, WidgetId, WidgetRef, util::HashMap,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
hash::Hash,
|
any::{Any, TypeId},
|
||||||
ops::{Index, IndexMut},
|
ops::{Index, IndexMut},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait Event: Sized {
|
pub trait Event: Sized + 'static + Clone {
|
||||||
type Module<Ctx: 'static>: EventModule<Self, Ctx>;
|
type Data<'a> = ();
|
||||||
type Data: Clone;
|
type State: Default = ();
|
||||||
|
#[allow(unused_variables)]
|
||||||
|
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EventLike {
|
||||||
|
type Event: Event;
|
||||||
|
fn into_event(self) -> Self::Event;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Event> EventLike for E {
|
||||||
|
type Event = Self;
|
||||||
|
|
||||||
|
fn into_event(self) -> Self::Event {
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventCtx<'a, Ctx, Data> {
|
pub struct EventCtx<'a, Ctx, Data> {
|
||||||
pub ui: &'a mut Ui,
|
pub ui: &'a mut Ui,
|
||||||
pub state: &'a mut Ctx,
|
pub state: &'a mut Ctx,
|
||||||
pub data: Data,
|
pub data: &'a mut Data,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> {
|
||||||
pub widget: WidgetRef<W>,
|
pub widget: WidgetRef<W>,
|
||||||
pub ui: &'a mut Ui,
|
pub ui: &'a mut Ui,
|
||||||
pub state: &'a mut Ctx,
|
pub state: &'a mut Ctx,
|
||||||
pub data: Data,
|
pub data: &'a mut Data,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct EventManager {
|
||||||
|
types: HashMap<TypeId, Box<dyn EventManagerLike>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl EventManager {
|
||||||
|
pub fn get_type<E: EventLike, Ctx: 'static>(&mut self) -> &mut TypeEventManager<E::Event, Ctx> {
|
||||||
|
self.types
|
||||||
|
.entry(Self::type_key::<E, Ctx>())
|
||||||
|
.or_insert(Box::new(TypeEventManager::<E::Event, Ctx>::default()))
|
||||||
|
.as_any()
|
||||||
|
.downcast_mut()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn register<I: IdLike, E: EventLike, Ctx: 'static>(
|
||||||
|
&mut self,
|
||||||
|
id: I,
|
||||||
|
event: E,
|
||||||
|
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
|
||||||
|
) {
|
||||||
|
self.get_type::<E, Ctx>().register(id, event, f);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn type_key<E: EventLike, Ctx: 'static>() -> TypeId {
|
||||||
|
TypeId::of::<TypeEventManager<E::Event, Ctx>>()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, id: WidgetId) {
|
||||||
|
for m in self.types.values_mut() {
|
||||||
|
m.remove(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub 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, active: &ActiveData) {
|
||||||
|
for t in &data.event_mgrs {
|
||||||
|
self.types.get_mut(t).unwrap().undraw(active);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait EventManagerLike {
|
||||||
|
fn remove(&mut self, id: WidgetId);
|
||||||
|
fn draw(&mut self, data: &ActiveData);
|
||||||
|
fn undraw(&mut self, data: &ActiveData);
|
||||||
|
fn as_any(&mut self) -> &mut dyn Any;
|
||||||
|
}
|
||||||
|
|
||||||
|
type EventData<E, Ctx> = (E, Rc<dyn for<'a> EventFn<Ctx, <E as Event>::Data<'a>>>);
|
||||||
|
pub struct TypeEventManager<E: Event, Ctx> {
|
||||||
|
// TODO: reduce visiblity!!
|
||||||
|
pub active: HashMap<LayerId, HashMap<WidgetId, E::State>>,
|
||||||
|
map: HashMap<WidgetId, Vec<EventData<E, Ctx>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Event, Ctx: 'static> EventManagerLike for TypeEventManager<E, Ctx> {
|
||||||
|
fn remove(&mut self, id: WidgetId) {
|
||||||
|
self.map.remove(&id);
|
||||||
|
for layer in self.active.values_mut() {
|
||||||
|
layer.remove(&id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn draw(&mut self, data: &ActiveData) {
|
||||||
|
self.active
|
||||||
|
.entry(data.layer)
|
||||||
|
.or_default()
|
||||||
|
.entry(data.id)
|
||||||
|
.or_default();
|
||||||
|
}
|
||||||
|
fn undraw(&mut self, data: &ActiveData) {
|
||||||
|
if let Some(layer) = self.active.get_mut(&data.layer) {
|
||||||
|
layer.remove(&data.id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fn as_any(&mut self) -> &mut dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Event, Ctx> Default for TypeEventManager<E, Ctx> {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
active: Default::default(),
|
||||||
|
map: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<E: Event, Ctx: 'static> TypeEventManager<E, Ctx> {
|
||||||
|
fn register(
|
||||||
|
&mut self,
|
||||||
|
id: impl IdLike,
|
||||||
|
event: impl EventLike<Event = E>,
|
||||||
|
f: impl for<'a> EventFn<Ctx, E::Data<'a>>,
|
||||||
|
) {
|
||||||
|
let event = event.into_event();
|
||||||
|
self.map
|
||||||
|
.entry(id.id())
|
||||||
|
.or_default()
|
||||||
|
.push((event, Rc::new(f)));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_fn<'a>(
|
||||||
|
&mut self,
|
||||||
|
id: impl IdLike,
|
||||||
|
) -> impl for<'b> FnOnce(EventCtx<Ctx, E::Data<'b>>) + 'a {
|
||||||
|
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||||
|
move |ctx| {
|
||||||
|
for (e, f) in fs {
|
||||||
|
if e.should_run(ctx.data) {
|
||||||
|
f(EventCtx {
|
||||||
|
ui: ctx.ui,
|
||||||
|
state: ctx.state,
|
||||||
|
data: ctx.data,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
impl<'a, Ctx, Data, W2, W: Widget> Index<WidgetRef<W>> for EventIdCtx<'a, Ctx, Data, W2> {
|
||||||
@@ -52,120 +198,18 @@ impl<F: Fn(EventIdCtx<Ctx, Data, W>) + 'static, Ctx, Data, W: ?Sized> WidgetEven
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait DefaultEvent: Hash + Eq + 'static {
|
|
||||||
type Data: Clone = ();
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: DefaultEvent> Event for E {
|
|
||||||
type Module<Ctx: 'static> = DefaultEventModule<E, Ctx>;
|
|
||||||
type Data = E::Data;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait EventModule<E: Event, Ctx>: UiModule + Default {
|
|
||||||
fn register(&mut self, id: WidgetId, event: E, f: impl EventFn<Ctx, E::Data>);
|
|
||||||
fn run<'a>(
|
|
||||||
&self,
|
|
||||||
id: WidgetId,
|
|
||||||
event: E,
|
|
||||||
) -> Option<impl Fn(EventCtx<Ctx, E::Data>) + use<'a, Self, E, Ctx>>;
|
|
||||||
}
|
|
||||||
|
|
||||||
type EventFnMap<Ctx, Data> = HashMap<WidgetId, Vec<Rc<dyn EventFn<Ctx, Data>>>>;
|
|
||||||
pub struct DefaultEventModule<E: Event, Ctx> {
|
|
||||||
map: HashMap<E, EventFnMap<Ctx, <E as Event>::Data>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: Event + 'static, Ctx: 'static> UiModule for DefaultEventModule<E, Ctx> {
|
|
||||||
fn on_remove(&mut self, id: WidgetId) {
|
|
||||||
for map in self.map.values_mut() {
|
|
||||||
map.remove(&id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait HashableEvent: Event + Hash + Eq + 'static {}
|
|
||||||
impl<E: Event + Hash + Eq + 'static> HashableEvent for E {}
|
|
||||||
|
|
||||||
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>(
|
|
||||||
&self,
|
|
||||||
id: WidgetId,
|
|
||||||
event: E,
|
|
||||||
) -> Option<impl Fn(EventCtx<Ctx, E::Data>) + use<'a, E, Ctx>> {
|
|
||||||
if let Some(map) = self.map.get(&event)
|
|
||||||
&& let Some(fs) = map.get(&id)
|
|
||||||
{
|
|
||||||
let fs = fs.clone();
|
|
||||||
Some(move |ctx: EventCtx<Ctx, <E as Event>::Data>| {
|
|
||||||
for f in &fs {
|
|
||||||
f(EventCtx {
|
|
||||||
ui: ctx.ui,
|
|
||||||
state: ctx.state,
|
|
||||||
data: ctx.data.clone(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<E: HashableEvent, Ctx: 'static> DefaultEventModule<E, Ctx> {
|
|
||||||
pub fn run_all(&self, event: E, ctx: EventCtx<Ctx, E::Data>)
|
|
||||||
where
|
|
||||||
E::Data: Clone,
|
|
||||||
{
|
|
||||||
if let Some(map) = self.map.get(&event) {
|
|
||||||
for fs in map.values() {
|
|
||||||
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> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
map: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Ui {
|
impl Ui {
|
||||||
pub fn run_event<E: Event, Ctx: 'static, W>(
|
pub fn run_event<E: EventLike, Ctx: 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
ctx: &mut Ctx,
|
ctx: &mut Ctx,
|
||||||
id: WidgetRef<W>,
|
id: impl IdLike,
|
||||||
event: E,
|
data: &mut <E::Event as Event>::Data<'_>,
|
||||||
data: E::Data,
|
|
||||||
) {
|
) {
|
||||||
if let Some(f) = self
|
let f = self.data.events.get_type::<E, Ctx>().run_fn(id);
|
||||||
.data
|
|
||||||
.modules
|
|
||||||
.get_mut::<E::Module<Ctx>>()
|
|
||||||
.run(id.id(), event)
|
|
||||||
{
|
|
||||||
f(EventCtx {
|
f(EventCtx {
|
||||||
ui: self,
|
ui: self,
|
||||||
state: ctx,
|
state: ctx,
|
||||||
data,
|
data,
|
||||||
});
|
})
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@
|
|||||||
|
|
||||||
mod attr;
|
mod attr;
|
||||||
mod event;
|
mod event;
|
||||||
mod module;
|
|
||||||
mod num;
|
mod num;
|
||||||
mod orientation;
|
mod orientation;
|
||||||
mod painter;
|
mod painter;
|
||||||
@@ -27,7 +26,6 @@ pub mod util;
|
|||||||
|
|
||||||
pub use attr::*;
|
pub use attr::*;
|
||||||
pub use event::*;
|
pub use event::*;
|
||||||
pub use module::*;
|
|
||||||
pub use num::*;
|
pub use num::*;
|
||||||
pub use orientation::*;
|
pub use orientation::*;
|
||||||
pub use painter::*;
|
pub use painter::*;
|
||||||
|
|||||||
@@ -1,32 +0,0 @@
|
|||||||
use std::any::{Any, TypeId};
|
|
||||||
|
|
||||||
use crate::{ActiveData, WidgetId, util::HashMap};
|
|
||||||
|
|
||||||
#[allow(unused_variables)]
|
|
||||||
pub trait UiModule: Any {
|
|
||||||
fn on_draw(&mut self, inst: &ActiveData) {}
|
|
||||||
fn on_undraw(&mut self, inst: &ActiveData) {}
|
|
||||||
fn on_remove(&mut self, id: WidgetId) {}
|
|
||||||
fn on_move(&mut self, inst: &ActiveData) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct Modules {
|
|
||||||
map: HashMap<TypeId, Box<dyn UiModule>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Modules {
|
|
||||||
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut (dyn UiModule + 'static)> {
|
|
||||||
self.map.values_mut().map(|m| m.as_mut())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_mut<M: UiModule + Default>(&mut self) -> &mut M {
|
|
||||||
let rf = self
|
|
||||||
.map
|
|
||||||
.entry(TypeId::of::<M>())
|
|
||||||
.or_insert_with(|| Box::new(M::default()))
|
|
||||||
.as_mut();
|
|
||||||
let any: &mut dyn Any = &mut *rf;
|
|
||||||
any.downcast_mut().unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
Axis, Len, Modules, PrimitiveLayers, RenderedText, Size, TextAttrs, TextBuffer, TextData,
|
Axis, EventManager, Len, PrimitiveLayers, RenderedText, Size, TextAttrs, TextBuffer, TextData,
|
||||||
TextureHandle, Textures, UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets,
|
TextureHandle, Textures, UiRegion, UiVec2, WidgetHandle, WidgetId, Widgets,
|
||||||
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
render::{Mask, MaskIdx, Primitive, PrimitiveHandle, PrimitiveInst},
|
||||||
util::{HashMap, HashSet, TrackedArena, Vec2},
|
util::{HashMap, HashSet, TrackedArena, Vec2},
|
||||||
@@ -28,7 +28,7 @@ struct PainterCtx<'a> {
|
|||||||
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,
|
pub events: &'a mut EventManager,
|
||||||
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)>,
|
||||||
pub needs_redraw: &'a mut HashSet<WidgetId>,
|
pub needs_redraw: &'a mut HashSet<WidgetId>,
|
||||||
@@ -66,7 +66,7 @@ pub struct PainterData {
|
|||||||
pub textures: Textures,
|
pub textures: Textures,
|
||||||
pub text: TextData,
|
pub text: TextData,
|
||||||
pub output_size: Vec2,
|
pub output_size: Vec2,
|
||||||
pub modules: Modules,
|
pub events: EventManager,
|
||||||
pub px_dependent: HashSet<WidgetId>,
|
pub px_dependent: HashSet<WidgetId>,
|
||||||
pub masks: TrackedArena<Mask, u32>,
|
pub masks: TrackedArena<Mask, u32>,
|
||||||
}
|
}
|
||||||
@@ -150,7 +150,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;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -204,7 +204,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;
|
||||||
}
|
}
|
||||||
@@ -231,7 +231,7 @@ impl<'a> PainterCtx<'a> {
|
|||||||
let children_height = painter.children_height;
|
let children_height = painter.children_height;
|
||||||
|
|
||||||
// add to active
|
// add to active
|
||||||
let instance = ActiveData {
|
let active = ActiveData {
|
||||||
id,
|
id,
|
||||||
region,
|
region,
|
||||||
parent,
|
parent,
|
||||||
@@ -260,16 +260,15 @@ impl<'a> PainterCtx<'a> {
|
|||||||
|
|
||||||
// remove old children that weren't kept
|
// remove old children that weren't kept
|
||||||
for c in &old_children {
|
for c in &old_children {
|
||||||
if !instance.children.contains(c) {
|
if !active.children.contains(c) {
|
||||||
self.remove_rec(*c);
|
self.remove_rec(*c);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// update modules
|
// update modules
|
||||||
for m in self.modules.iter_mut() {
|
let data = self.widgets.data(id).unwrap();
|
||||||
m.on_draw(&instance);
|
self.events.draw(data, &active);
|
||||||
}
|
self.active.insert(id, active);
|
||||||
self.active.insert(id, instance);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn mov(&mut self, id: WidgetId, from: UiRegion, to: UiRegion) {
|
fn mov(&mut self, id: WidgetId, from: UiRegion, to: UiRegion) {
|
||||||
@@ -279,9 +278,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();
|
||||||
@@ -291,26 +287,27 @@ 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<ActiveData> {
|
fn remove(&mut self, id: WidgetId, undraw: bool) -> Option<ActiveData> {
|
||||||
let mut inst = self.active.remove(&id);
|
let mut active = self.active.remove(&id);
|
||||||
if let Some(inst) = &mut inst {
|
if let Some(active) = &mut active {
|
||||||
for h in &inst.primitives {
|
for h in &active.primitives {
|
||||||
let mask = self.layers.free(h);
|
let mask = self.layers.free(h);
|
||||||
if mask != MaskIdx::NONE {
|
if mask != MaskIdx::NONE {
|
||||||
self.masks.remove(mask);
|
self.masks.remove(mask);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inst.textures.clear();
|
active.textures.clear();
|
||||||
self.textures.free();
|
self.textures.free();
|
||||||
for m in self.modules.iter_mut() {
|
if undraw {
|
||||||
m.on_undraw(inst);
|
let data = self.widgets.data(id).unwrap();
|
||||||
|
self.events.undraw(data, active);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
inst
|
active
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remove_rec(&mut self, id: WidgetId) -> Option<ActiveData> {
|
fn remove_rec(&mut self, id: WidgetId) -> Option<ActiveData> {
|
||||||
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);
|
||||||
@@ -329,7 +326,7 @@ impl PainterData {
|
|||||||
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,
|
events: &mut self.events,
|
||||||
masks: &mut self.masks,
|
masks: &mut self.masks,
|
||||||
cache_width: Default::default(),
|
cache_width: Default::default(),
|
||||||
cache_height: Default::default(),
|
cache_height: 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,
|
||||||
@@ -49,13 +51,13 @@ impl<T: Default> Layers<T> {
|
|||||||
self.vec.push(LayerNode::head());
|
self.vec.push(LayerNode::head());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push(&mut self, node: LayerNode<T>) -> usize {
|
fn push(&mut self, node: LayerNode<T>) -> LayerId {
|
||||||
let i = self.vec.len();
|
let i = self.vec.len();
|
||||||
self.vec.push(node);
|
self.vec.push(node);
|
||||||
i
|
i
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next(&mut self, i: usize) -> usize {
|
pub fn next(&mut self, i: LayerId) -> LayerId {
|
||||||
if let Ptr::Next(i) = self.vec[i].next {
|
if let Ptr::Next(i) = self.vec[i].next {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@@ -75,7 +77,7 @@ impl<T: Default> Layers<T> {
|
|||||||
i_new
|
i_new
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn child(&mut self, i: usize) -> usize {
|
pub fn child(&mut self, i: LayerId) -> LayerId {
|
||||||
if let Some(c) = self.vec[i].child {
|
if let Some(c) = self.vec[i].child {
|
||||||
return c.head;
|
return c.head;
|
||||||
}
|
}
|
||||||
@@ -100,11 +102,11 @@ impl<T: Default> Layers<T> {
|
|||||||
self.vec.iter_mut().map(|n| &mut n.data).enumerate()
|
self.vec.iter_mut().map(|n| &mut n.data).enumerate()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter(&self) -> impl Iterator<Item = (usize, &T)> {
|
pub fn iter(&self) -> impl Iterator<Item = (LayerId, &T)> {
|
||||||
self.indices().map(|i| (i, &self.vec[i].data))
|
self.indices().map(|i| (i, &self.vec[i].data))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn iter_depth(&self) -> impl Iterator<Item = ((usize, usize), &T)> {
|
pub fn iter_depth(&self) -> impl Iterator<Item = ((LayerId, usize), &T)> {
|
||||||
self.indices()
|
self.indices()
|
||||||
.map(|i| ((i, self.vec[i].depth), &self.vec[i].data))
|
.map(|i| ((i, self.vec[i].depth), &self.vec[i].data))
|
||||||
}
|
}
|
||||||
@@ -115,7 +117,11 @@ impl<T: Default> Layers<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl PrimitiveLayers {
|
impl PrimitiveLayers {
|
||||||
pub fn write<P: Primitive>(&mut self, layer: usize, info: PrimitiveInst<P>) -> PrimitiveHandle {
|
pub fn write<P: Primitive>(
|
||||||
|
&mut self,
|
||||||
|
layer: LayerId,
|
||||||
|
info: PrimitiveInst<P>,
|
||||||
|
) -> PrimitiveHandle {
|
||||||
self[layer].write(layer, info)
|
self[layer].write(layer, info)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,16 +136,16 @@ impl<T: Default> Default for Layers<T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Index<usize> for Layers<T> {
|
impl<T> Index<LayerId> for Layers<T> {
|
||||||
type Output = T;
|
type Output = T;
|
||||||
|
|
||||||
fn index(&self, index: usize) -> &Self::Output {
|
fn index(&self, index: LayerId) -> &Self::Output {
|
||||||
&self.vec[index].data
|
&self.vec[index].data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> IndexMut<usize> for Layers<T> {
|
impl<T> IndexMut<LayerId> for Layers<T> {
|
||||||
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
fn index_mut(&mut self, index: LayerId) -> &mut Self::Output {
|
||||||
&mut self.vec[index].data
|
&mut self.vec[index].data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
ActiveData, Event, EventFn, EventModule, IdLike, PainterData, PixelRegion, TextureHandle,
|
ActiveData, Event, EventFn, EventLike, EventManager, IdLike, PainterData, PixelRegion,
|
||||||
Widget, WidgetHandle, WidgetId, WidgetLike, util::Vec2,
|
TextureHandle, Widget, WidgetHandle, WidgetId, WidgetLike, util::Vec2,
|
||||||
};
|
};
|
||||||
use image::DynamicImage;
|
use image::DynamicImage;
|
||||||
use std::{
|
use std::{
|
||||||
@@ -24,11 +24,11 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// useful for debugging
|
/// useful for debugging
|
||||||
pub fn set_label<W: ?Sized>(&mut self, id: &WidgetHandle<W>, label: String) {
|
pub fn set_label(&mut self, id: impl IdLike, label: String) {
|
||||||
self.data.widgets.data_mut(id.id()).unwrap().label = label;
|
self.data.widgets.data_mut(id.id()).unwrap().label = label;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn label<W: ?Sized>(&self, id: &WidgetHandle<W>) -> &String {
|
pub fn label(&self, id: impl IdLike) -> &String {
|
||||||
&self.data.widgets.data(id.id()).unwrap().label
|
&self.data.widgets.data(id.id()).unwrap().label
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,16 +63,19 @@ impl Ui {
|
|||||||
self.data.textures.add(image)
|
self.data.textures.add(image)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn register_event<I: IdLike, E: Event, Ctx: 'static>(
|
pub fn register_event<E: EventLike, Ctx: 'static>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: &I,
|
id: impl IdLike,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl EventFn<Ctx, E::Data>,
|
f: impl for<'a> EventFn<Ctx, <E::Event as Event>::Data<'a>>,
|
||||||
) {
|
) {
|
||||||
|
self.data.events.register(id.id(), event, f);
|
||||||
self.data
|
self.data
|
||||||
.modules
|
.widgets
|
||||||
.get_mut::<E::Module<Ctx>>()
|
.data_mut(id)
|
||||||
.register(id.id(), event, f);
|
.unwrap()
|
||||||
|
.event_mgrs
|
||||||
|
.insert(EventManager::type_key::<E, Ctx>());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||||
@@ -81,10 +84,9 @@ impl Ui {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn redraw_all(&mut self) {
|
pub fn redraw_all(&mut self) {
|
||||||
for (_, inst) in self.data.active.drain() {
|
for (id, active) in self.data.active.drain() {
|
||||||
for m in self.data.modules.iter_mut() {
|
let data = self.data.widgets.data(id).unwrap();
|
||||||
m.on_undraw(&inst);
|
self.data.events.undraw(data, &active);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// free before bc nothing should exist
|
// free before bc nothing should exist
|
||||||
self.free();
|
self.free();
|
||||||
@@ -116,9 +118,7 @@ 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.recv.try_iter() {
|
for id in self.recv.try_iter() {
|
||||||
for m in self.data.modules.iter_mut() {
|
self.data.events.remove(id);
|
||||||
m.on_remove(id);
|
|
||||||
}
|
|
||||||
self.data.widgets.delete(id);
|
self.data.widgets.delete(id);
|
||||||
}
|
}
|
||||||
self.data.textures.free();
|
self.data.textures.free();
|
||||||
|
|||||||
26
core/src/widget/data.rs
Normal file
26
core/src/widget/data.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
use std::any::TypeId;
|
||||||
|
|
||||||
|
use crate::{Widget, util::HashSet};
|
||||||
|
|
||||||
|
pub struct WidgetData {
|
||||||
|
pub widget: Box<dyn Widget>,
|
||||||
|
pub label: String,
|
||||||
|
pub event_mgrs: HashSet<TypeId>,
|
||||||
|
/// dynamic borrow checking
|
||||||
|
pub borrowed: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
Self {
|
||||||
|
widget: Box::new(widget),
|
||||||
|
label,
|
||||||
|
borrowed: false,
|
||||||
|
event_mgrs: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -94,6 +94,13 @@ pub trait IdLike {
|
|||||||
fn id(&self) -> WidgetId;
|
fn id(&self) -> WidgetId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
|
||||||
|
type Widget = W;
|
||||||
|
fn id(&self) -> WidgetId {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn id(&self) -> WidgetId {
|
fn id(&self) -> WidgetId {
|
||||||
@@ -108,6 +115,13 @@ impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl IdLike for WidgetId {
|
||||||
|
type Widget = dyn Widget;
|
||||||
|
fn id(&self) -> WidgetId {
|
||||||
|
*self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
||||||
|
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
use crate::{Len, Painter, SizeCtx, Ui};
|
use crate::{Len, Painter, SizeCtx, Ui};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
|
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,5 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
IdLike, Widget, WidgetId,
|
IdLike, Widget, WidgetData, WidgetId,
|
||||||
util::{DynBorrower, HashSet, SlotVec},
|
util::{DynBorrower, HashSet, SlotVec},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -9,13 +9,6 @@ pub struct Widgets {
|
|||||||
vec: SlotVec<WidgetData>,
|
vec: SlotVec<WidgetData>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct WidgetData {
|
|
||||||
pub widget: Box<dyn Widget>,
|
|
||||||
pub label: String,
|
|
||||||
/// dynamic borrow checking
|
|
||||||
pub borrowed: bool,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Widgets {
|
impl Widgets {
|
||||||
pub fn has_updates(&self) -> bool {
|
pub fn has_updates(&self) -> bool {
|
||||||
!self.updates.is_empty()
|
!self.updates.is_empty()
|
||||||
@@ -60,35 +53,23 @@ impl Widgets {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||||
let mut label = std::any::type_name::<W>().to_string();
|
self.vec.add(WidgetData::new(widget))
|
||||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
|
||||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
|
||||||
}
|
|
||||||
self.insert_any(Box::new(widget), label)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn data(&self, id: WidgetId) -> Option<&WidgetData> {
|
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||||
self.vec.get(id)
|
self.vec.get(id.id())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn label(&self, id: WidgetId) -> &String {
|
pub fn label(&self, id: impl IdLike) -> &String {
|
||||||
&self.data(id).unwrap().label
|
&self.data(id.id()).unwrap().label
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn data_mut(&mut self, id: WidgetId) -> Option<&mut WidgetData> {
|
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||||
self.vec.get_mut(id)
|
self.vec.get_mut(id.id())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn insert_any(&mut self, widget: Box<dyn Widget>, label: String) -> WidgetId {
|
pub fn delete(&mut self, id: impl IdLike) {
|
||||||
self.vec.add(WidgetData {
|
self.vec.free(id.id());
|
||||||
widget,
|
|
||||||
label,
|
|
||||||
borrowed: false,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn delete(&mut self, id: WidgetId) {
|
|
||||||
self.vec.free(id);
|
|
||||||
// not sure if there's any point in this
|
// not sure if there's any point in this
|
||||||
// self.updates.remove(&id);
|
// self.updates.remove(&id);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -125,7 +125,8 @@ impl DefaultAppState for Client {
|
|||||||
add_text.h.width(rest(1)),
|
add_text.h.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.r, Submit, ());
|
ctx.ui
|
||||||
|
.run_event::<Submit, _>(ctx.state, add_text.r, &mut ());
|
||||||
})
|
})
|
||||||
.sized((40, 40)),
|
.sized((40, 40)),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,21 +1,29 @@
|
|||||||
use crate::prelude::*;
|
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 + Widget> 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| {
|
ui.register_event(container, CursorSense::click_or_drag(), move |ctx| {
|
||||||
let ui = ctx.ui;
|
let region = ctx.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 = ctx.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(
|
||||||
|
ctx.ui,
|
||||||
|
id,
|
||||||
|
ctx.state,
|
||||||
|
pos,
|
||||||
|
size,
|
||||||
|
ctx.data.sense.is_dragging(),
|
||||||
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -26,18 +34,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| {
|
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
|
||||||
select(ctx.ui, id, ctx.state, ctx.data);
|
select(
|
||||||
|
ctx.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.edit(ui)
|
id.edit(ui).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,9 +1,9 @@
|
|||||||
use iris_core::DefaultEvent;
|
use iris_core::Event;
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||||
pub struct Submit;
|
pub struct Submit;
|
||||||
impl DefaultEvent for Submit {}
|
impl Event for Submit {}
|
||||||
|
|
||||||
#[derive(Eq, PartialEq, Hash, Clone)]
|
#[derive(Eq, PartialEq, Hash, Clone)]
|
||||||
pub struct Edited;
|
pub struct Edited;
|
||||||
impl DefaultEvent for Edited {}
|
impl Event for Edited {}
|
||||||
|
|||||||
@@ -11,12 +11,14 @@ mod attr;
|
|||||||
mod event;
|
mod event;
|
||||||
mod input;
|
mod input;
|
||||||
mod render;
|
mod render;
|
||||||
|
mod sense;
|
||||||
|
|
||||||
pub use app::*;
|
pub use app::*;
|
||||||
pub use attr::*;
|
pub use attr::*;
|
||||||
pub use event::*;
|
pub use event::*;
|
||||||
pub use input::*;
|
pub use input::*;
|
||||||
pub use render::*;
|
pub use render::*;
|
||||||
|
pub use sense::*;
|
||||||
|
|
||||||
pub type Proxy<Event> = EventLoopProxy<Event>;
|
pub type Proxy<Event> = EventLoopProxy<Event>;
|
||||||
pub type DefaultApp<Data> = App<DefaultState<Data>>;
|
pub type DefaultApp<Data> = App<DefaultState<Data>>;
|
||||||
@@ -131,13 +133,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, ());
|
ui.run_event::<Submit, _>(app_state, sel, &mut ());
|
||||||
}
|
}
|
||||||
TextInputResult::Paste => {
|
TextInputResult::Paste => {
|
||||||
if let Ok(t) = ui_state.clipboard.get_text() {
|
if let Ok(t) = ui_state.clipboard.get_text() {
|
||||||
text.insert(&t);
|
text.insert(&t);
|
||||||
}
|
}
|
||||||
ui.run_event(app_state, sel, Edited, ());
|
ui.run_event::<Edited, _>(app_state, sel, &mut ());
|
||||||
}
|
}
|
||||||
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) {
|
||||||
@@ -145,7 +147,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
TextInputResult::Used => {
|
TextInputResult::Used => {
|
||||||
ui.run_event(app_state, sel, Edited, ());
|
ui.run_event::<Edited, _>(app_state, sel, &mut ());
|
||||||
}
|
}
|
||||||
TextInputResult::Unused => {}
|
TextInputResult::Unused => {}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use iris_core::util::HashMap;
|
|
||||||
use std::{
|
use std::{
|
||||||
ops::{BitOr, Deref, DerefMut},
|
ops::{BitOr, Deref, DerefMut},
|
||||||
rc::Rc,
|
rc::Rc,
|
||||||
@@ -23,8 +22,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)
|
||||||
@@ -69,6 +82,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 {
|
||||||
@@ -97,70 +120,24 @@ 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,
|
||||||
|
/// 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: &ActiveData) {
|
|
||||||
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: &ActiveData) {
|
|
||||||
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: &ActiveData) {
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@@ -172,55 +149,39 @@ impl SensorUi for Ui {
|
|||||||
cursor: &CursorState,
|
cursor: &CursorState,
|
||||||
window_size: Vec2,
|
window_size: Vec2,
|
||||||
) {
|
) {
|
||||||
CursorModule::<Ctx>::run(self, ctx, cursor, window_size);
|
let layers = std::mem::take(&mut self.data.layers);
|
||||||
}
|
let mut active =
|
||||||
}
|
std::mem::take(&mut self.data.events.get_type::<CursorSense, Ctx>().active);
|
||||||
|
for layer in layers.indices().rev() {
|
||||||
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.layers);
|
|
||||||
let mut module = std::mem::take(ui.data.modules.get_mut::<Self>());
|
|
||||||
|
|
||||||
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(&layer).into_iter().flatten() {
|
||||||
let group = module.map.get_mut(id).unwrap();
|
let shape = self.data.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 {
|
|
||||||
cursor: cursor.pos - region.top_left,
|
|
||||||
size: region.bot_right - region.top_left,
|
size: region.bot_right - region.top_left,
|
||||||
scroll_delta: cursor.scroll_delta,
|
scroll_delta: cursor.scroll_delta,
|
||||||
sense,
|
hover: state.hover,
|
||||||
|
cursor,
|
||||||
|
// this does not have any meaning;
|
||||||
|
// might wanna set up Event to have a prepare stage
|
||||||
|
sense: CursorSense::Hovering,
|
||||||
};
|
};
|
||||||
(sensor.f)(EventCtx {
|
self.run_event::<CursorSense, Ctx>(ctx, *id, &mut data);
|
||||||
ui,
|
|
||||||
state: ctx,
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if sensed {
|
if sensed {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
self.data.events.get_type::<CursorSense, Ctx>().active = active;
|
||||||
let ui_mod = ui.data.modules.get_mut::<Self>();
|
self.data.layers = layers;
|
||||||
std::mem::swap(ui_mod, &mut module);
|
|
||||||
ui_mod.merge(module);
|
|
||||||
ui.data.layers = layers;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -288,66 +249,10 @@ impl ActivationState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Event for CursorSenses {
|
impl EventLike for CursorSense {
|
||||||
type Module<Ctx: 'static> = CursorModule<Ctx>;
|
type Event = CursorSenses;
|
||||||
type Data = CursorData;
|
fn into_event(self) -> Self::Event {
|
||||||
}
|
self.into()
|
||||||
|
|
||||||
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>(
|
|
||||||
&self,
|
|
||||||
id: WidgetId,
|
|
||||||
event: E,
|
|
||||||
) -> Option<impl Fn(EventCtx<Ctx, <E as Event>::Data>) + use<'a, E, Ctx>> {
|
|
||||||
let senses = event.into();
|
|
||||||
if let Some(group) = self.map.get(&id) {
|
|
||||||
let fs: Vec<_> = group
|
|
||||||
.sensors
|
|
||||||
.iter()
|
|
||||||
.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(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -387,12 +292,3 @@ impl BitOr<CursorSense> for CursorSenses {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<Ctx> Default for CursorModule<Ctx> {
|
|
||||||
fn default() -> Self {
|
|
||||||
Self {
|
|
||||||
map: Default::default(),
|
|
||||||
active: Default::default(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
34
src/event.rs
34
src/event.rs
@@ -5,14 +5,14 @@ pub mod eventable {
|
|||||||
|
|
||||||
widget_trait! {
|
widget_trait! {
|
||||||
pub trait Eventable;
|
pub trait Eventable;
|
||||||
fn on<E: Event, Ctx: 'static>(
|
fn on<E: EventLike, Ctx: 'static>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
|
f: impl for<'a> WidgetEventFn<Ctx, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||||
) -> impl WidgetIdFn<WL::Widget> {
|
) -> impl WidgetIdFn<WL::Widget> {
|
||||||
move |ui| {
|
move |ui| {
|
||||||
let id = self.handles(ui);
|
let id = self.handles(ui);
|
||||||
ui.register_event(&id.r, event, move |ctx| {
|
ui.register_event(id.r, event.into_event(), move |ctx| {
|
||||||
f(EventIdCtx {
|
f(EventIdCtx {
|
||||||
widget: id.r,
|
widget: id.r,
|
||||||
state: ctx.state,
|
state: ctx.state,
|
||||||
@@ -35,21 +35,25 @@ macro_rules! event_ctx {
|
|||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use $crate::prelude::*;
|
use $crate::prelude::*;
|
||||||
|
|
||||||
pub trait EventableCtx<WL: WidgetLike<Tag>, Tag, Ctx: 'static> {
|
widget_trait! {
|
||||||
fn on<E: Event>(
|
pub trait EventableCtx;
|
||||||
|
fn on<E: EventLike>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl WidgetEventFn<Ctx, E::Data, WL::Widget>,
|
f: impl for<'a> WidgetEventFn<$ty, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||||
) -> impl WidgetIdFn<WL::Widget>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<WL: WidgetLike<Tag>, Tag> EventableCtx<WL, Tag, $ty> for WL {
|
|
||||||
fn on<E: Event>(
|
|
||||||
self,
|
|
||||||
event: E,
|
|
||||||
f: impl WidgetEventFn<$ty, E::Data, WL::Widget>,
|
|
||||||
) -> impl WidgetIdFn<WL::Widget> {
|
) -> impl WidgetIdFn<WL::Widget> {
|
||||||
eventable::Eventable::on(self, event, f)
|
move |ui| {
|
||||||
|
let id = self.handles(ui);
|
||||||
|
ui.register_event(id.r, event.into_event(), move |ctx| {
|
||||||
|
f(EventIdCtx {
|
||||||
|
widget: id.r,
|
||||||
|
state: ctx.state,
|
||||||
|
data: ctx.data,
|
||||||
|
ui: ctx.ui,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
id.h
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ mod mask;
|
|||||||
mod position;
|
mod position;
|
||||||
mod ptr;
|
mod ptr;
|
||||||
mod rect;
|
mod rect;
|
||||||
mod sense;
|
|
||||||
mod text;
|
mod text;
|
||||||
mod trait_fns;
|
mod trait_fns;
|
||||||
|
|
||||||
@@ -12,6 +11,5 @@ pub use mask::*;
|
|||||||
pub use position::*;
|
pub use position::*;
|
||||||
pub use ptr::*;
|
pub use ptr::*;
|
||||||
pub use rect::*;
|
pub use rect::*;
|
||||||
pub use sense::*;
|
|
||||||
pub use text::*;
|
pub use text::*;
|
||||||
pub use trait_fns::*;
|
pub use trait_fns::*;
|
||||||
|
|||||||
Reference in New Issue
Block a user