tasks initial impl (still working on task_on trait method)
This commit is contained in:
10
Cargo.lock
generated
10
Cargo.lock
generated
@@ -1047,6 +1047,7 @@ dependencies = [
|
|||||||
"iris-core",
|
"iris-core",
|
||||||
"iris-macro",
|
"iris-macro",
|
||||||
"pollster",
|
"pollster",
|
||||||
|
"tokio",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"wgpu",
|
"wgpu",
|
||||||
"winit",
|
"winit",
|
||||||
@@ -2575,6 +2576,15 @@ version = "0.1.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tokio"
|
||||||
|
version = "1.49.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "72a2903cd7736441aac9df9d7688bd0ce48edccaadf181c3b90be801e81d3d86"
|
||||||
|
dependencies = [
|
||||||
|
"pin-project-lite",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "toml"
|
name = "toml"
|
||||||
version = "0.8.23"
|
version = "0.8.23"
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "iris"
|
name = "iris"
|
||||||
default-run = "test"
|
|
||||||
version.workspace = true
|
version.workspace = true
|
||||||
edition.workspace = true
|
edition.workspace = true
|
||||||
|
|
||||||
@@ -16,6 +15,10 @@ arboard = { workspace = true, features = ["wayland-data-control"] }
|
|||||||
pollster = { workspace = true }
|
pollster = { workspace = true }
|
||||||
wgpu = { workspace = true }
|
wgpu = { workspace = true }
|
||||||
image = { workspace = true }
|
image = { workspace = true }
|
||||||
|
tokio = { workspace = true, features = ["sync", "rt", "rt-multi-thread"] }
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
tokio = { workspace = true, features = ["sync", "rt", "rt-multi-thread", "time"] }
|
||||||
|
|
||||||
[workspace]
|
[workspace]
|
||||||
members = ["core", "macro"]
|
members = ["core", "macro"]
|
||||||
@@ -36,3 +39,4 @@ fxhash = "0.2.1"
|
|||||||
arboard = "3.6.1"
|
arboard = "3.6.1"
|
||||||
iris-core = { path = "core" }
|
iris-core = { path = "core" }
|
||||||
iris-macro = { path = "macro" }
|
iris-macro = { path = "macro" }
|
||||||
|
tokio = "1.49.0"
|
||||||
|
|||||||
@@ -2,9 +2,11 @@ use crate::{
|
|||||||
Event, EventCtx, EventLike, EventManager, HasUi, IdLike, Widget, WidgetEventFn, WidgetRef,
|
Event, EventCtx, EventLike, EventManager, HasUi, IdLike, Widget, WidgetEventFn, WidgetRef,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub trait HasEvents: Sized + HasUi + 'static {
|
pub trait HasState: 'static {
|
||||||
type State;
|
type State;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HasEvents: Sized + HasUi + HasState {
|
||||||
fn events(&self) -> &EventManager<Self>;
|
fn events(&self) -> &EventManager<Self>;
|
||||||
fn events_mut(&mut self) -> &mut EventManager<Self>;
|
fn events_mut(&mut self) -> &mut EventManager<Self>;
|
||||||
|
|
||||||
@@ -18,7 +20,7 @@ pub trait HasEvents: Sized + HasUi + 'static {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RunEvents: HasEvents + 'static {
|
pub trait RunEvents: HasEvents {
|
||||||
fn run_event<E: EventLike>(
|
fn run_event<E: EventLike>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: impl IdLike,
|
id: impl IdLike,
|
||||||
@@ -29,4 +31,4 @@ pub trait RunEvents: HasEvents + 'static {
|
|||||||
f(EventCtx { state, data }, self)
|
f(EventCtx { state, data }, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<T: HasEvents + 'static> RunEvents for T {}
|
impl<T: HasEvents> RunEvents for T {}
|
||||||
|
|||||||
@@ -169,3 +169,5 @@ fn null_ptr<W: ?Sized>() -> *const W {
|
|||||||
unsafe { std::mem::transmute_copy(&[0usize; 1]) }
|
unsafe { std::mem::transmute_copy(&[0usize; 1]) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl<W: ?Sized> Send for WidgetRef<W> {}
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 8.7 KiB After Width: | Height: | Size: 8.7 KiB |
33
examples/task.rs
Normal file
33
examples/task.rs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
use std::time::Duration;
|
||||||
|
|
||||||
|
iris::state_prelude!(DefaultRsc<State>);
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
DefaultApp::<State>::run();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(DefaultUiState)]
|
||||||
|
struct State {
|
||||||
|
ui_state: DefaultUiState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultAppState for State {
|
||||||
|
fn new(ui_state: DefaultUiState, rsc: &mut DefaultRsc<Self>, _: Proxy<Self::Event>) -> Self {
|
||||||
|
let rect = rect(Color::RED).add(rsc);
|
||||||
|
rect.on(CursorSense::click(), move |_, rsc| {
|
||||||
|
rsc.tasks.spawn(async move |ctx| {
|
||||||
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
||||||
|
ctx.update(move |_, rsc| {
|
||||||
|
let rect = rect(rsc);
|
||||||
|
if rect.color == Color::RED {
|
||||||
|
rect.color = Color::GREEN;
|
||||||
|
} else {
|
||||||
|
rect.color = Color::RED;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
})
|
||||||
|
.set_root(rsc);
|
||||||
|
Self { ui_state }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ mod event;
|
|||||||
mod input;
|
mod input;
|
||||||
mod render;
|
mod render;
|
||||||
mod sense;
|
mod sense;
|
||||||
|
mod task;
|
||||||
|
|
||||||
pub use app::*;
|
pub use app::*;
|
||||||
pub use attr::*;
|
pub use attr::*;
|
||||||
@@ -24,6 +25,7 @@ pub use event::*;
|
|||||||
pub use input::*;
|
pub use input::*;
|
||||||
pub use render::*;
|
pub use render::*;
|
||||||
pub use sense::*;
|
pub use sense::*;
|
||||||
|
pub use task::*;
|
||||||
|
|
||||||
pub type Proxy<Event> = EventLoopProxy<Event>;
|
pub type Proxy<Event> = EventLoopProxy<Event>;
|
||||||
|
|
||||||
@@ -79,19 +81,25 @@ pub trait DefaultAppState: HasDefaultUiState {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct DefaultRsc<State> {
|
pub struct DefaultRsc<State: 'static> {
|
||||||
pub ui: Ui,
|
pub ui: Ui,
|
||||||
pub events: EventManager<Self>,
|
pub events: EventManager<Self>,
|
||||||
|
pub tasks: Tasks<Self>,
|
||||||
_state: PhantomData<State>,
|
_state: PhantomData<State>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State> Default for DefaultRsc<State> {
|
impl<State> DefaultRsc<State> {
|
||||||
fn default() -> Self {
|
fn init(window: Arc<Window>) -> (Self, TaskMsgReceiver<Self>) {
|
||||||
Self {
|
let (tasks, recv) = Tasks::init(window);
|
||||||
ui: Default::default(),
|
(
|
||||||
events: Default::default(),
|
Self {
|
||||||
_state: Default::default(),
|
ui: Default::default(),
|
||||||
}
|
events: Default::default(),
|
||||||
|
tasks,
|
||||||
|
_state: Default::default(),
|
||||||
|
},
|
||||||
|
recv,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,9 +113,11 @@ impl<State> HasUi for DefaultRsc<State> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State: 'static> HasEvents for DefaultRsc<State> {
|
impl<State: 'static> HasState for DefaultRsc<State> {
|
||||||
type State = State;
|
type State = State;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<State: 'static> HasEvents for DefaultRsc<State> {
|
||||||
fn events(&self) -> &EventManager<Self> {
|
fn events(&self) -> &EventManager<Self> {
|
||||||
&self.events
|
&self.events
|
||||||
}
|
}
|
||||||
@@ -120,11 +130,9 @@ impl<State: 'static> HasEvents for DefaultRsc<State> {
|
|||||||
pub struct DefaultApp<State: DefaultAppState> {
|
pub struct DefaultApp<State: DefaultAppState> {
|
||||||
rsc: DefaultRsc<State>,
|
rsc: DefaultRsc<State>,
|
||||||
state: State,
|
state: State,
|
||||||
|
task_recv: TaskMsgReceiver<DefaultRsc<State>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl StateLike for DefaultRsc<State> {
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
||||||
type Event = State::Event;
|
type Event = State::Event;
|
||||||
|
|
||||||
@@ -132,9 +140,14 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
let window = event_loop
|
let window = event_loop
|
||||||
.create_window(State::window_attributes())
|
.create_window(State::window_attributes())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let mut rsc = DefaultRsc::default();
|
let default_state = DefaultUiState::new(window);
|
||||||
let state = State::new(DefaultUiState::new(window), &mut rsc, proxy);
|
let (mut rsc, task_recv) = DefaultRsc::init(default_state.window.clone());
|
||||||
Self { rsc, state }
|
let state = State::new(default_state, &mut rsc, proxy);
|
||||||
|
Self {
|
||||||
|
rsc,
|
||||||
|
state,
|
||||||
|
task_recv,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
|
fn event(&mut self, event: Self::Event, _: &ActiveEventLoop) {
|
||||||
@@ -142,6 +155,10 @@ impl<State: DefaultAppState> AppState for DefaultApp<State> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
fn window_event(&mut self, event: WindowEvent, event_loop: &ActiveEventLoop) {
|
||||||
|
for update in self.task_recv.try_iter() {
|
||||||
|
update(&mut self.state, &mut self.rsc);
|
||||||
|
}
|
||||||
|
|
||||||
let ui_state = self.state.default_state_mut();
|
let ui_state = self.state.default_state_mut();
|
||||||
let input_changed = ui_state.input.event(&event);
|
let input_changed = ui_state.input.event(&event);
|
||||||
let cursor_state = ui_state.cursor_state().clone();
|
let cursor_state = ui_state.cursor_state().clone();
|
||||||
|
|||||||
85
src/default/task.rs
Normal file
85
src/default/task.rs
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
use iris_core::HasState;
|
||||||
|
use std::{
|
||||||
|
pin::Pin,
|
||||||
|
sync::{
|
||||||
|
Arc,
|
||||||
|
mpsc::{Receiver as SyncReceiver, Sender as SyncSender, channel as sync_channel},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use tokio::{
|
||||||
|
runtime::Runtime,
|
||||||
|
sync::mpsc::{
|
||||||
|
UnboundedReceiver as AsyncReceiver, UnboundedSender as AsyncSender,
|
||||||
|
unbounded_channel as async_channel,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
use winit::window::Window;
|
||||||
|
|
||||||
|
pub type TaskMsgSender<Rsc> = SyncSender<Box<dyn TaskUpdate<Rsc>>>;
|
||||||
|
pub type TaskMsgReceiver<Rsc> = SyncReceiver<Box<dyn TaskUpdate<Rsc>>>;
|
||||||
|
|
||||||
|
pub trait TaskUpdate<Rsc: HasState>: FnOnce(&mut Rsc::State, &mut Rsc) + Send {}
|
||||||
|
impl<F: FnOnce(&mut Rsc::State, &mut Rsc) + Send, Rsc: HasState> TaskUpdate<Rsc> for F {}
|
||||||
|
|
||||||
|
pub struct Tasks<Rsc: HasState> {
|
||||||
|
start: AsyncSender<BoxTask>,
|
||||||
|
window: Arc<Window>,
|
||||||
|
msg_send: SyncSender<Box<dyn TaskUpdate<Rsc>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct TaskCtx<Rsc: HasState> {
|
||||||
|
send: TaskMsgSender<Rsc>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<Rsc: HasState> TaskCtx<Rsc> {
|
||||||
|
pub fn update(&mut self, f: impl TaskUpdate<Rsc> + 'static) {
|
||||||
|
let _ = self.send.send(Box::new(f));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<Rsc: HasState + 'static> TaskCtx<Rsc> {
|
||||||
|
fn new(send: TaskMsgSender<Rsc>) -> Self {
|
||||||
|
Self { send }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type BoxTask = Pin<Box<dyn Future<Output = ()> + Send>>;
|
||||||
|
|
||||||
|
impl<Rsc: HasState> Tasks<Rsc> {
|
||||||
|
pub fn init(window: Arc<Window>) -> (Self, TaskMsgReceiver<Rsc>) {
|
||||||
|
let (start, start_recv) = async_channel();
|
||||||
|
let (msgs, msgs_recv) = sync_channel();
|
||||||
|
std::thread::spawn(|| {
|
||||||
|
let rt = Runtime::new().unwrap();
|
||||||
|
rt.block_on(listen(start_recv))
|
||||||
|
});
|
||||||
|
(
|
||||||
|
Self {
|
||||||
|
start,
|
||||||
|
msg_send: msgs,
|
||||||
|
window,
|
||||||
|
},
|
||||||
|
msgs_recv,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
window.request_redraw();
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn listen(mut recv: AsyncReceiver<BoxTask>) {
|
||||||
|
while let Some(task) = recv.recv().await {
|
||||||
|
tokio::spawn(task);
|
||||||
|
}
|
||||||
|
}
|
||||||
61
src/event.rs
61
src/event.rs
@@ -1,17 +1,20 @@
|
|||||||
use crate::prelude::*;
|
use iris_core::*;
|
||||||
|
use iris_macro::*;
|
||||||
|
|
||||||
|
use crate::default::{TaskCtx, Tasks};
|
||||||
|
|
||||||
pub mod eventable {
|
pub mod eventable {
|
||||||
use super::*;
|
use super::*;
|
||||||
widget_trait! {
|
widget_trait! {
|
||||||
pub trait Eventable<Rsc: HasEvents + 'static>;
|
pub trait Eventable<Rsc: HasEvents>;
|
||||||
fn on<E: EventLike>(
|
fn on<E: EventLike>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
f: impl for<'a> WidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, WL::Widget>,
|
f: impl for<'a> WidgetEventFn<Rsc, <E::Event as Event>::Data<'a>, WL::Widget>,
|
||||||
) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
||||||
move |state| {
|
move |rsc| {
|
||||||
let id = self.add(state);
|
let id = self.add(rsc);
|
||||||
state.register_event(id, event.into_event(), move |ctx, rsc| {
|
rsc.register_event(id, event.into_event(), move |ctx, rsc| {
|
||||||
f(&mut EventIdCtx {
|
f(&mut EventIdCtx {
|
||||||
widget: id,
|
widget: id,
|
||||||
state: ctx.state,
|
state: ctx.state,
|
||||||
@@ -22,4 +25,52 @@ pub mod eventable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait HasTasks: Sized + HasState + HasEvents {
|
||||||
|
fn tasks_mut(&mut self) -> &mut Tasks<Self>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait AsyncWidgetEventFn<Rsc: HasEvents, Data, W: ?Sized>:
|
||||||
|
AsyncFn(&mut AsyncEventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static
|
||||||
|
{
|
||||||
|
}
|
||||||
|
impl<
|
||||||
|
Rsc: HasEvents,
|
||||||
|
F: AsyncFn(&mut AsyncEventIdCtx<Rsc, Data, W>, &mut Rsc) + 'static,
|
||||||
|
Data,
|
||||||
|
W: ?Sized,
|
||||||
|
> AsyncWidgetEventFn<Rsc, Data, W> for F
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct AsyncEventIdCtx<'a, 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>,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#![feature(associated_type_defaults)]
|
#![feature(associated_type_defaults)]
|
||||||
#![feature(unsize)]
|
#![feature(unsize)]
|
||||||
#![feature(option_into_flat_iter)]
|
#![feature(option_into_flat_iter)]
|
||||||
|
#![feature(async_fn_traits)]
|
||||||
|
|
||||||
pub mod default;
|
pub mod default;
|
||||||
pub mod event;
|
pub mod event;
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ macro_rules! event_state {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
$vis type EventManager = $crate::prelude::EventManager<<$rsc as HasEvents>::State>;
|
$vis type EventManager = $crate::prelude::EventManager<<$rsc as HasState>::State>;
|
||||||
$vis use local_event_trait::*;
|
$vis use local_event_trait::*;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ widget_trait! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn scrollable(self) -> impl WidgetIdFn<Rsc, Scroll> where Rsc: HasEvents {
|
fn scrollable(self) -> impl WidgetIdFn<Rsc, Scroll> where Rsc: HasEvents {
|
||||||
use eventable::*;
|
use eventable::Eventable;
|
||||||
move |state| {
|
move |state| {
|
||||||
Scroll::new(self.add_strong(state), Axis::Y)
|
Scroll::new(self.add_strong(state), Axis::Y)
|
||||||
.on(CursorSense::Scroll, |ctx, rsc| {
|
.on(CursorSense::Scroll, |ctx, rsc| {
|
||||||
|
|||||||
Reference in New Issue
Block a user