remove modules and have single event manager (atomics feature parity + preparation for local state)

This commit is contained in:
2025-12-12 01:46:24 -05:00
parent a708813ce7
commit 37b1987aa8
17 changed files with 390 additions and 432 deletions

View File

@@ -125,7 +125,8 @@ impl DefaultAppState for Client {
add_text.h.width(rest(1)),
Rect::new(Color::GREEN)
.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)),
)

View File

@@ -1,21 +1,29 @@
use crate::prelude::*;
use crate::{default::UiState, prelude::*};
use std::time::{Duration, Instant};
use winit::dpi::{LogicalPosition, LogicalSize};
event_ctx!(UiState);
pub struct Selector;
impl<W: 'static + Widget> WidgetAttr<W> for Selector {
impl<W: Widget + 'static> WidgetAttr<W> for Selector {
type Input = WidgetRef<TextEdit>;
fn run(ui: &mut Ui, container: WidgetRef<W>, id: Self::Input) {
ui.register_event(&container, CursorSense::click_or_drag(), move |mut ctx| {
let ui = ctx.ui;
let region = ui.window_region(&id).unwrap();
ui.register_event(container, CursorSense::click_or_drag(), move |ctx| {
let region = ctx.ui.window_region(&id).unwrap();
let id_pos = region.top_left;
let container_pos = ui.window_region(&container).unwrap().top_left;
ctx.data.cursor += container_pos - id_pos;
ctx.data.size = region.size();
select(ui, id, ctx.state, ctx.data);
let container_pos = ctx.ui.window_region(&container).unwrap().top_left;
let pos = ctx.data.pos + container_pos - id_pos;
let size = region.size();
select(
ctx.ui,
id,
ctx.state,
pos,
size,
ctx.data.sense.is_dragging(),
);
});
}
}
@@ -26,18 +34,31 @@ impl WidgetAttr<TextEdit> for Selectable {
type Input = ();
fn run(ui: &mut Ui, id: WidgetRef<TextEdit>, _: Self::Input) {
ui.register_event(&id, CursorSense::click_or_drag(), move |ctx| {
select(ctx.ui, id, ctx.state, ctx.data);
ui.register_event(id, CursorSense::click_or_drag(), move |ctx| {
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 recent = (now - state.last_click) < Duration::from_millis(300);
state.last_click = now;
id.edit(ui)
.select(data.cursor, data.size, data.sense.is_dragging(), recent);
id.edit(ui).select(pos, size, dragging, recent);
if let Some(region) = ui.window_region(&id) {
state.window.set_ime_allowed(true);
state.window.set_ime_cursor_area(

View File

@@ -1,9 +1,9 @@
use iris_core::DefaultEvent;
use iris_core::Event;
#[derive(Eq, PartialEq, Hash, Clone)]
pub struct Submit;
impl DefaultEvent for Submit {}
impl Event for Submit {}
#[derive(Eq, PartialEq, Hash, Clone)]
pub struct Edited;
impl DefaultEvent for Edited {}
impl Event for Edited {}

View File

@@ -11,12 +11,14 @@ mod attr;
mod event;
mod input;
mod render;
mod sense;
pub use app::*;
pub use attr::*;
pub use event::*;
pub use input::*;
pub use render::*;
pub use sense::*;
pub type Proxy<Event> = EventLoopProxy<Event>;
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);
}
TextInputResult::Submit => {
ui.run_event(app_state, sel, Submit, ());
ui.run_event::<Submit, _>(app_state, sel, &mut ());
}
TextInputResult::Paste => {
if let Ok(t) = ui_state.clipboard.get_text() {
text.insert(&t);
}
ui.run_event(app_state, sel, Edited, ());
ui.run_event::<Edited, _>(app_state, sel, &mut ());
}
TextInputResult::Copy(text) => {
if let Err(err) = ui_state.clipboard.set_text(text) {
@@ -145,7 +147,7 @@ impl<State: DefaultAppState> AppState for DefaultState<State> {
}
}
TextInputResult::Used => {
ui.run_event(app_state, sel, Edited, ());
ui.run_event::<Edited, _>(app_state, sel, &mut ());
}
TextInputResult::Unused => {}
}

View File

@@ -1,5 +1,4 @@
use crate::prelude::*;
use iris_core::util::HashMap;
use std::{
ops::{BitOr, Deref, DerefMut},
rc::Rc,
@@ -23,8 +22,22 @@ pub enum CursorSense {
Scroll,
}
#[derive(Clone)]
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 {
pub fn click() -> Self {
Self::PressStart(CursorButton::Left)
@@ -69,6 +82,16 @@ impl CursorButtons {
self.middle.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 {
@@ -97,70 +120,24 @@ pub struct Sensor<Ctx, Data> {
pub f: Rc<dyn EventFn<Ctx, Data>>,
}
pub type SensorMap<Ctx, Data> = HashMap<WidgetId, SensorGroup<Ctx, Data>>;
pub type SenseShape = UiRegion;
pub struct SensorGroup<Ctx, Data> {
#[derive(Default, Debug)]
pub struct SensorState {
pub hover: ActivationState,
pub sensors: Vec<Sensor<Ctx, Data>>,
}
#[derive(Clone)]
pub struct CursorData {
pub cursor: Vec2,
pub struct CursorData<'a> {
/// where this widget was hit
pub pos: Vec2,
pub size: Vec2,
pub scroll_delta: Vec2,
/// the (first) sense that triggered this event
/// the senses are checked in order
pub hover: ActivationState,
pub cursor: &'a CursorState,
/// the first sense that triggered this
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 {
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,
window_size: Vec2,
) {
CursorModule::<Ctx>::run(self, ctx, cursor, window_size);
}
}
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 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() {
let mut sensed = false;
for (id, shape) in list.iter() {
let group = module.map.get_mut(id).unwrap();
for (id, state) in active.get_mut(&layer).into_iter().flatten() {
let shape = self.data.active.get(id).unwrap().region;
let region = shape.to_px(window_size);
let in_shape = cursor.exists && region.contains(cursor.pos);
group.hover.update(in_shape);
if group.hover == ActivationState::Off {
state.hover.update(in_shape);
if state.hover == ActivationState::Off {
continue;
}
sensed = true;
for sensor in &mut group.sensors {
if let Some(sense) = should_run(&sensor.senses, cursor, group.hover) {
let data = CursorData {
cursor: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
scroll_delta: cursor.scroll_delta,
sense,
};
(sensor.f)(EventCtx {
ui,
state: ctx,
data,
});
}
}
let mut data = CursorData {
pos: cursor.pos - region.top_left,
size: region.bot_right - region.top_left,
scroll_delta: cursor.scroll_delta,
hover: state.hover,
cursor,
// this does not have any meaning;
// might wanna set up Event to have a prepare stage
sense: CursorSense::Hovering,
};
self.run_event::<CursorSense, Ctx>(ctx, *id, &mut data);
}
if sensed {
break;
}
}
let ui_mod = ui.data.modules.get_mut::<Self>();
std::mem::swap(ui_mod, &mut module);
ui_mod.merge(module);
ui.data.layers = layers;
self.data.events.get_type::<CursorSense, Ctx>().active = active;
self.data.layers = layers;
}
}
@@ -288,66 +249,10 @@ impl ActivationState {
}
}
impl Event for CursorSenses {
type Module<Ctx: 'static> = CursorModule<Ctx>;
type Data = CursorData;
}
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(),
}
impl EventLike for CursorSense {
type Event = CursorSenses;
fn into_event(self) -> Self::Event {
self.into()
}
}
@@ -387,12 +292,3 @@ impl BitOr<CursorSense> for CursorSenses {
self
}
}
impl<Ctx> Default for CursorModule<Ctx> {
fn default() -> Self {
Self {
map: Default::default(),
active: Default::default(),
}
}
}

View File

@@ -5,14 +5,14 @@ pub mod eventable {
widget_trait! {
pub trait Eventable;
fn on<E: Event, Ctx: 'static>(
fn on<E: EventLike, Ctx: 'static>(
self,
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> {
move |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 {
widget: id.r,
state: ctx.state,
@@ -35,21 +35,25 @@ macro_rules! event_ctx {
#[allow(unused_imports)]
use $crate::prelude::*;
pub trait EventableCtx<WL: WidgetLike<Tag>, Tag, Ctx: 'static> {
fn on<E: Event>(
widget_trait! {
pub trait EventableCtx;
fn on<E: EventLike>(
self,
event: E,
f: impl WidgetEventFn<Ctx, E::Data, 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>,
f: impl for<'a> WidgetEventFn<$ty, <E::Event as Event>::Data<'a>, 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
}
}
}
}

View File

@@ -3,7 +3,6 @@ mod mask;
mod position;
mod ptr;
mod rect;
mod sense;
mod text;
mod trait_fns;
@@ -12,6 +11,5 @@ pub use mask::*;
pub use position::*;
pub use ptr::*;
pub use rect::*;
pub use sense::*;
pub use text::*;
pub use trait_fns::*;