From 26a994a4b8860c7ebf8c490f0b8d64b493ffe6ea Mon Sep 17 00:00:00 2001 From: drendog Date: Sat, 6 Dec 2025 01:37:56 +0100 Subject: [PATCH] refactor: appstate directly instead of wrap to construct ctx --- crates/composition/src/event_loop.rs | 65 +++++++++++---------------- crates/composition/src/layer_shika.rs | 2 +- crates/composition/src/shell.rs | 2 +- crates/composition/src/system.rs | 2 +- 4 files changed, 30 insertions(+), 41 deletions(-) diff --git a/crates/composition/src/event_loop.rs b/crates/composition/src/event_loop.rs index 7dc0aca..9af60a0 100644 --- a/crates/composition/src/event_loop.rs +++ b/crates/composition/src/event_loop.rs @@ -6,7 +6,6 @@ use layer_shika_adapters::platform::calloop::{ }; use layer_shika_adapters::{AppState, WindowingSystemFacade}; use std::cell::RefCell; -use std::marker::PhantomData; use std::os::unix::io::AsFd; use std::rc::Weak; use std::time::{Duration, Instant}; @@ -15,61 +14,51 @@ pub trait FromAppState<'a> { fn from_app_state(app_state: &'a mut AppState) -> Self; } -pub struct EventLoopHandleBase { +pub struct EventLoopHandleBase { system: Weak>, - _marker: PhantomData Ctx>, } -impl EventLoopHandleBase { +impl EventLoopHandleBase { pub fn new(system: Weak>) -> Self { - Self { - system, - _marker: PhantomData, - } + Self { system } } -} -impl EventLoopHandleBase -where - for<'a> Ctx: FromAppState<'a> + 'a, -{ - pub fn insert_source(&self, source: S, mut callback: F) -> Result + pub fn insert_source(&self, source: S, callback: F) -> Result where S: EventSource + 'static, - F: FnMut(S::Event, &mut S::Metadata, Ctx) -> R + 'static, + F: FnMut(S::Event, &mut S::Metadata, &mut AppState) -> R + 'static, { let system = self.system.upgrade().ok_or(Error::SystemDropped)?; let loop_handle = system.borrow().inner_ref().event_loop_handle(); - loop_handle - .insert_source(source, move |event, metadata, app_state| { - let ctx = Ctx::from_app_state(app_state); - callback(event, metadata, ctx) - }) - .map_err(|e| { - Error::Adapter( - EventLoopError::InsertSource { - message: format!("{e:?}"), - } - .into(), - ) - }) + loop_handle.insert_source(source, callback).map_err(|e| { + Error::Adapter( + EventLoopError::InsertSource { + message: format!("{e:?}"), + } + .into(), + ) + }) } pub fn add_timer(&self, duration: Duration, mut callback: F) -> Result where - F: FnMut(Instant, Ctx) -> TimeoutAction + 'static, + F: FnMut(Instant, &mut AppState) -> TimeoutAction + 'static, { let timer = Timer::from_duration(duration); - self.insert_source(timer, move |deadline, (), ctx| callback(deadline, ctx)) + self.insert_source(timer, move |deadline, (), app_state| { + callback(deadline, app_state) + }) } pub fn add_timer_at(&self, deadline: Instant, mut callback: F) -> Result where - F: FnMut(Instant, Ctx) -> TimeoutAction + 'static, + F: FnMut(Instant, &mut AppState) -> TimeoutAction + 'static, { let timer = Timer::from_deadline(deadline); - self.insert_source(timer, move |deadline, (), ctx| callback(deadline, ctx)) + self.insert_source(timer, move |deadline, (), app_state| { + callback(deadline, app_state) + }) } pub fn add_channel( @@ -78,12 +67,12 @@ where ) -> Result<(RegistrationToken, channel::Sender)> where T: 'static, - F: FnMut(T, Ctx) + 'static, + F: FnMut(T, &mut AppState) + 'static, { let (sender, receiver) = channel::channel(); - let token = self.insert_source(receiver, move |event, (), ctx| { + let token = self.insert_source(receiver, move |event, (), app_state| { if let channel::Event::Msg(msg) = event { - callback(msg, ctx); + callback(msg, app_state); } })?; Ok((token, sender)) @@ -98,11 +87,11 @@ where ) -> Result where T: AsFd + 'static, - F: FnMut(Ctx) + 'static, + F: FnMut(&mut AppState) + 'static, { let generic = Generic::new(fd, interest, mode); - self.insert_source(generic, move |_readiness, _fd, ctx| { - callback(ctx); + self.insert_source(generic, move |_readiness, _fd, app_state| { + callback(app_state); Ok(PostAction::Continue) }) } diff --git a/crates/composition/src/layer_shika.rs b/crates/composition/src/layer_shika.rs index 5f72037..85fdd59 100644 --- a/crates/composition/src/layer_shika.rs +++ b/crates/composition/src/layer_shika.rs @@ -815,7 +815,7 @@ impl ShellRuntime for Runtime { } } -pub type EventLoopHandle = EventLoopHandleBase>; +pub type EventLoopHandle = EventLoopHandleBase; pub struct EventContext<'a> { app_state: &'a mut AppState, diff --git a/crates/composition/src/shell.rs b/crates/composition/src/shell.rs index 7334c52..9c85634 100644 --- a/crates/composition/src/shell.rs +++ b/crates/composition/src/shell.rs @@ -847,7 +847,7 @@ impl ShellRuntime for Shell { } } -pub type ShellEventLoopHandle = EventLoopHandleBase>; +pub type ShellEventLoopHandle = EventLoopHandleBase; pub struct ShellEventContext<'a> { app_state: &'a mut AppState, diff --git a/crates/composition/src/system.rs b/crates/composition/src/system.rs index ecc6039..197a76e 100644 --- a/crates/composition/src/system.rs +++ b/crates/composition/src/system.rs @@ -105,7 +105,7 @@ impl ShellControl { } } -pub type EventLoopHandle = EventLoopHandleBase>; +pub type EventLoopHandle = EventLoopHandleBase; pub struct EventContext<'a> { app_state: &'a mut AppState,