remove typed stuff / just specify rsc if needed
This commit is contained in:
@@ -127,6 +127,12 @@ impl<State: 'static> HasEvents for DefaultRsc<State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> HasTasks for DefaultRsc<State> {
|
||||
fn tasks_mut(&mut self) -> &mut Tasks<Self> {
|
||||
&mut self.tasks
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefaultApp<State: DefaultAppState> {
|
||||
rsc: DefaultRsc<State>,
|
||||
state: State,
|
||||
@@ -169,7 +175,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
||||
if input_changed {
|
||||
let window_size = ui_state.window_size();
|
||||
self.rsc
|
||||
.run_sensors(&mut self.state, &cursor_state, window_size);
|
||||
.run_sensors(&mut self.state, cursor_state, window_size);
|
||||
}
|
||||
let ui = &mut self.rsc.ui;
|
||||
let ui_state = self.state.default_state_mut();
|
||||
@@ -200,13 +206,13 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
||||
ui_state.window.set_ime_allowed(false);
|
||||
}
|
||||
TextInputResult::Submit => {
|
||||
self.rsc.run_event::<Submit>(sel, &mut (), &mut self.state);
|
||||
self.rsc.run_event::<Submit>(sel, (), &mut self.state);
|
||||
}
|
||||
TextInputResult::Paste => {
|
||||
if let Ok(t) = ui_state.clipboard.get_text() {
|
||||
text.insert(&t);
|
||||
}
|
||||
self.rsc.run_event::<Edited>(sel, &mut (), &mut self.state);
|
||||
self.rsc.run_event::<Edited>(sel, (), &mut self.state);
|
||||
}
|
||||
TextInputResult::Copy(text) => {
|
||||
if let Err(err) = ui_state.clipboard.set_text(text) {
|
||||
@@ -214,7 +220,7 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
||||
}
|
||||
}
|
||||
TextInputResult::Used => {
|
||||
self.rsc.run_event::<Edited>(sel, &mut (), &mut self.state);
|
||||
self.rsc.run_event::<Edited>(sel, (), &mut self.state);
|
||||
}
|
||||
TextInputResult::Unused => {}
|
||||
}
|
||||
|
||||
@@ -26,14 +26,15 @@ pub enum CursorSense {
|
||||
pub struct CursorSenses(Vec<CursorSense>);
|
||||
|
||||
impl Event for CursorSenses {
|
||||
type Data<'a> = CursorData<'a>;
|
||||
type Data = CursorData;
|
||||
type State = SensorState;
|
||||
fn should_run(&self, data: &mut Self::Data<'_>) -> bool {
|
||||
if let Some(sense) = should_run(self, data.cursor, data.hover) {
|
||||
fn should_run(&self, data: &Self::Data) -> Option<Self::Data> {
|
||||
if let Some(sense) = should_run(self, &data.cursor, data.hover) {
|
||||
let mut data = data.clone();
|
||||
data.sense = sense;
|
||||
true
|
||||
Some(data)
|
||||
} else {
|
||||
false
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -127,23 +128,24 @@ pub struct SensorState {
|
||||
pub hover: ActivationState,
|
||||
}
|
||||
|
||||
pub struct CursorData<'a> {
|
||||
#[derive(Clone)]
|
||||
pub struct CursorData {
|
||||
/// where this widget was hit
|
||||
pub pos: Vec2,
|
||||
pub size: Vec2,
|
||||
pub scroll_delta: Vec2,
|
||||
pub hover: ActivationState,
|
||||
pub cursor: &'a CursorState,
|
||||
pub cursor: CursorState,
|
||||
/// the first sense that triggered this
|
||||
pub sense: CursorSense,
|
||||
}
|
||||
|
||||
pub trait SensorUi<Rsc: HasEvents> {
|
||||
fn run_sensors(&mut self, state: &mut Rsc::State, cursor: &CursorState, window_size: Vec2);
|
||||
fn run_sensors(&mut self, state: &mut Rsc::State, cursor: CursorState, window_size: Vec2);
|
||||
}
|
||||
|
||||
impl<Rsc: HasEvents> SensorUi<Rsc> for Rsc {
|
||||
fn run_sensors(&mut self, state: &mut Rsc::State, cursor: &CursorState, window_size: Vec2) {
|
||||
fn run_sensors(&mut self, state: &mut Rsc::State, cursor: CursorState, window_size: Vec2) {
|
||||
let layers = std::mem::take(&mut self.ui_mut().layers);
|
||||
let mut active = std::mem::take(&mut self.events_mut().get_type::<CursorSense>().active);
|
||||
for layer in layers.indices().rev() {
|
||||
@@ -158,7 +160,9 @@ impl<Rsc: HasEvents> SensorUi<Rsc> for Rsc {
|
||||
}
|
||||
sensed = true;
|
||||
|
||||
let mut data = CursorData {
|
||||
let cursor = cursor.clone();
|
||||
|
||||
let data = CursorData {
|
||||
pos: cursor.pos - region.top_left,
|
||||
size: region.bot_right - region.top_left,
|
||||
scroll_delta: cursor.scroll_delta,
|
||||
@@ -168,7 +172,7 @@ impl<Rsc: HasEvents> SensorUi<Rsc> for Rsc {
|
||||
// might wanna set up Event to have a prepare stage
|
||||
sense: CursorSense::Hovering,
|
||||
};
|
||||
self.run_event::<CursorSense>(*id, &mut data, state);
|
||||
self.run_event::<CursorSense>(*id, data, state);
|
||||
}
|
||||
if sensed {
|
||||
break;
|
||||
|
||||
@@ -62,17 +62,14 @@ impl<Rsc: HasState> Tasks<Rsc> {
|
||||
)
|
||||
}
|
||||
|
||||
pub fn spawn<F: for<'a> AsyncFnOnce(&'a mut TaskCtx<Rsc>) + 'static + std::marker::Send>(
|
||||
&mut self,
|
||||
task: F,
|
||||
) where
|
||||
for<'a> <F as AsyncFnOnce<(&'a mut TaskCtx<Rsc>,)>>::CallOnceFuture: Send,
|
||||
pub fn spawn<F: AsyncFnOnce(TaskCtx<Rsc>) + 'static + std::marker::Send>(&mut self, task: F)
|
||||
where
|
||||
F::CallOnceFuture: Send,
|
||||
{
|
||||
let send = self.msg_send.clone();
|
||||
let window = self.window.clone();
|
||||
let _ = self.start.send(Box::pin(async move {
|
||||
let mut ctx = TaskCtx::new(send);
|
||||
task(&mut ctx).await;
|
||||
task(TaskCtx::new(send)).await;
|
||||
window.request_redraw();
|
||||
}));
|
||||
}
|
||||
|
||||
97
src/event.rs
97
src/event.rs
@@ -1,54 +1,60 @@
|
||||
use iris_core::*;
|
||||
use iris_macro::*;
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::default::{TaskCtx, Tasks};
|
||||
|
||||
pub mod eventable {
|
||||
use super::*;
|
||||
widget_trait! {
|
||||
pub trait Eventable<Rsc: HasEvents>;
|
||||
fn on<E: EventLike>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl for<'a> WidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||
) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
||||
move |rsc| {
|
||||
let id = self.add(rsc);
|
||||
rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
||||
f(&mut EventIdCtx {
|
||||
pub trait Eventable<Rsc: HasEvents, Tag>: WidgetLike<Rsc, Tag> {
|
||||
fn on<E: EventLike>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl WidgetEventFn<Rsc, <E::Event as Event>::Data, Self::Widget>,
|
||||
) -> impl WidgetIdFn<Rsc, Self::Widget> {
|
||||
move |rsc| {
|
||||
let id = self.add(rsc);
|
||||
rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
||||
f(
|
||||
EventIdCtx {
|
||||
widget: id,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
}, rsc);
|
||||
});
|
||||
id
|
||||
}
|
||||
},
|
||||
rsc,
|
||||
);
|
||||
});
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<WL: WidgetLike<Rsc, Tag>, Rsc: HasEvents, Tag> Eventable<Rsc, Tag> for WL {}
|
||||
|
||||
// widget_trait! {
|
||||
// pub trait TaskEventable<Rsc: HasEvents + HasTasks>;
|
||||
// fn task_on<E: EventLike>(
|
||||
// self,
|
||||
// event: E,
|
||||
// f: impl for<'a> AsyncWidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||
// ) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
||||
// move |rsc| {
|
||||
// let id = self.add(rsc);
|
||||
// rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
||||
// rsc.tasks_mut().spawn(async move |task| {
|
||||
// f(&mut AsyncEventIdCtx {
|
||||
// widget: id,
|
||||
// state: ctx.state,
|
||||
// data: ctx.data,
|
||||
// task,
|
||||
// }, rsc).await;
|
||||
// });
|
||||
// });
|
||||
// id
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
widget_trait! {
|
||||
pub trait TaskEventable<Rsc: HasEvents + HasTasks>;
|
||||
fn task_on<E: EventLike, F: AsyncWidgetEventFn<Rsc, <E::Event as Event>::Data, WL::Widget>>(
|
||||
self,
|
||||
event: E,
|
||||
f: F,
|
||||
) -> impl WidgetIdFn<Rsc, WL::Widget>
|
||||
where <E::Event as Event>::Data: Send,
|
||||
for<'a> F::CallRefFuture<'a>: Send,
|
||||
{
|
||||
let f = Arc::new(f);
|
||||
move |rsc| {
|
||||
let id = self.add(rsc);
|
||||
rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
||||
let data = ctx.data;
|
||||
let f = f.clone();
|
||||
rsc.tasks_mut().spawn(async move |task| {
|
||||
f(AsyncEventIdCtx {
|
||||
widget: id,
|
||||
data,
|
||||
task,
|
||||
}).await;
|
||||
});
|
||||
});
|
||||
id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait HasTasks: Sized + HasState + HasEvents {
|
||||
@@ -56,21 +62,20 @@ pub trait HasTasks: Sized + HasState + HasEvents {
|
||||
}
|
||||
|
||||
pub trait AsyncWidgetEventFn<Rsc: HasEvents, Data, W: ?Sized>:
|
||||
AsyncFn(&mut AsyncEventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static
|
||||
AsyncFn(AsyncEventIdCtx<Rsc, Data, W>) + Send + Sync + 'static
|
||||
{
|
||||
}
|
||||
impl<
|
||||
Rsc: HasEvents,
|
||||
F: AsyncFn(&mut AsyncEventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static,
|
||||
F: AsyncFn(AsyncEventIdCtx<Rsc, Data, W>) + Send + Sync + 'static,
|
||||
Data,
|
||||
W: ?Sized,
|
||||
> AsyncWidgetEventFn<Rsc, Data, W> for F
|
||||
{
|
||||
}
|
||||
|
||||
pub struct AsyncEventIdCtx<'a, Rsc: HasEvents, Data, W: ?Sized> {
|
||||
pub struct AsyncEventIdCtx<Rsc: HasEvents, Data, W: ?Sized> {
|
||||
pub widget: WidgetRef<W>,
|
||||
pub state: &'a mut Rsc::State,
|
||||
pub data: &'a mut Data,
|
||||
pub task: &'a mut TaskCtx<Rsc>,
|
||||
pub data: Data,
|
||||
pub task: TaskCtx<Rsc>,
|
||||
}
|
||||
|
||||
16
src/lib.rs
16
src/lib.rs
@@ -8,25 +8,11 @@
|
||||
|
||||
pub mod default;
|
||||
pub mod event;
|
||||
pub mod typed;
|
||||
pub mod widget;
|
||||
|
||||
pub use iris_core as core;
|
||||
pub use iris_macro as macros;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! state_prelude {
|
||||
($vis:vis $state:ty) => {
|
||||
iris::event_state!($vis $state);
|
||||
$vis use iris::{
|
||||
default::*,
|
||||
core::{len_fns::*, util::Vec2, *},
|
||||
macros::*,
|
||||
widget::*,
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
pub mod prelude {
|
||||
use super::*;
|
||||
pub use default::*;
|
||||
@@ -36,5 +22,5 @@ pub mod prelude {
|
||||
pub use widget::*;
|
||||
|
||||
pub use iris_core::util::Vec2;
|
||||
pub use typed::*;
|
||||
pub use len_fns::*;
|
||||
}
|
||||
|
||||
37
src/typed.rs
37
src/typed.rs
@@ -1,37 +0,0 @@
|
||||
#[macro_export]
|
||||
macro_rules! event_state {
|
||||
($vis:vis $rsc:ty) => {
|
||||
mod local_event_trait {
|
||||
use super::*;
|
||||
#[allow(unused_imports)]
|
||||
use $crate::prelude::*;
|
||||
pub trait EventableCtx<WL: WidgetLike<$rsc, Tag>, Tag> {
|
||||
fn on<E: EventLike>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl for<'a> WidgetEventFn<
|
||||
$rsc,
|
||||
<E::Event as Event>::Data<'a>,
|
||||
WL::Widget
|
||||
>,
|
||||
) -> impl WidgetIdFn<$rsc, WL::Widget>;
|
||||
}
|
||||
impl<WL: WidgetLike<$rsc, Tag>, Tag> EventableCtx<WL, Tag> for WL {
|
||||
fn on<E: EventLike>(
|
||||
self,
|
||||
event: E,
|
||||
f: impl for<'a> WidgetEventFn<
|
||||
$rsc,
|
||||
<E::Event as Event>::Data<'a>,
|
||||
WL::Widget
|
||||
>,
|
||||
) -> impl WidgetIdFn<$rsc, WL::Widget> {
|
||||
eventable::Eventable::on(self, event, f)
|
||||
}
|
||||
}
|
||||
}
|
||||
$vis type EventManager = $crate::prelude::EventManager<<$rsc as HasState>::State>;
|
||||
$vis use local_event_trait::*;
|
||||
};
|
||||
}
|
||||
pub use event_state;
|
||||
@@ -84,7 +84,6 @@ widget_trait! {
|
||||
}
|
||||
|
||||
fn scrollable(self) -> impl WidgetIdFn<Rsc, Scroll> where Rsc: HasEvents {
|
||||
use eventable::Eventable;
|
||||
move |state| {
|
||||
Scroll::new(self.add_strong(state), Axis::Y)
|
||||
.on(CursorSense::Scroll, |ctx, rsc| {
|
||||
@@ -132,9 +131,9 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait CoreWidgetArr<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> {
|
||||
fn span(self, dir: Dir) -> SpanBuilder<State, LEN, Wa, Tag>;
|
||||
fn stack(self) -> StackBuilder<State, LEN, Wa, Tag>;
|
||||
pub trait CoreWidgetArr<Rsc, const LEN: usize, Wa: WidgetArrLike<Rsc, LEN, Tag>, Tag> {
|
||||
fn span(self, dir: Dir) -> SpanBuilder<Rsc, LEN, Wa, Tag>;
|
||||
fn stack(self) -> StackBuilder<Rsc, LEN, Wa, Tag>;
|
||||
}
|
||||
|
||||
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
|
||||
|
||||
Reference in New Issue
Block a user