refactor: appstate directly instead of wrap to construct ctx

This commit is contained in:
drendog 2025-12-06 01:37:56 +01:00
parent 24ed27d5c3
commit 26a994a4b8
Signed by: dwenya
GPG key ID: 8DD77074645332D0
4 changed files with 30 additions and 41 deletions

View file

@ -6,7 +6,6 @@ use layer_shika_adapters::platform::calloop::{
}; };
use layer_shika_adapters::{AppState, WindowingSystemFacade}; use layer_shika_adapters::{AppState, WindowingSystemFacade};
use std::cell::RefCell; use std::cell::RefCell;
use std::marker::PhantomData;
use std::os::unix::io::AsFd; use std::os::unix::io::AsFd;
use std::rc::Weak; use std::rc::Weak;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
@ -15,38 +14,24 @@ pub trait FromAppState<'a> {
fn from_app_state(app_state: &'a mut AppState) -> Self; fn from_app_state(app_state: &'a mut AppState) -> Self;
} }
pub struct EventLoopHandleBase<Ctx> { pub struct EventLoopHandleBase {
system: Weak<RefCell<WindowingSystemFacade>>, system: Weak<RefCell<WindowingSystemFacade>>,
_marker: PhantomData<fn(&mut AppState) -> Ctx>,
} }
impl<Ctx> EventLoopHandleBase<Ctx> { impl EventLoopHandleBase {
pub fn new(system: Weak<RefCell<WindowingSystemFacade>>) -> Self { pub fn new(system: Weak<RefCell<WindowingSystemFacade>>) -> Self {
Self { Self { system }
system,
_marker: PhantomData,
} }
}
}
impl<Ctx> EventLoopHandleBase<Ctx> pub fn insert_source<S, F, R>(&self, source: S, callback: F) -> Result<RegistrationToken>
where
for<'a> Ctx: FromAppState<'a> + 'a,
{
pub fn insert_source<S, F, R>(&self, source: S, mut callback: F) -> Result<RegistrationToken>
where where
S: EventSource<Ret = R> + 'static, S: EventSource<Ret = R> + '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 system = self.system.upgrade().ok_or(Error::SystemDropped)?;
let loop_handle = system.borrow().inner_ref().event_loop_handle(); let loop_handle = system.borrow().inner_ref().event_loop_handle();
loop_handle loop_handle.insert_source(source, callback).map_err(|e| {
.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( Error::Adapter(
EventLoopError::InsertSource { EventLoopError::InsertSource {
message: format!("{e:?}"), message: format!("{e:?}"),
@ -58,18 +43,22 @@ where
pub fn add_timer<F>(&self, duration: Duration, mut callback: F) -> Result<RegistrationToken> pub fn add_timer<F>(&self, duration: Duration, mut callback: F) -> Result<RegistrationToken>
where where
F: FnMut(Instant, Ctx) -> TimeoutAction + 'static, F: FnMut(Instant, &mut AppState) -> TimeoutAction + 'static,
{ {
let timer = Timer::from_duration(duration); 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<F>(&self, deadline: Instant, mut callback: F) -> Result<RegistrationToken> pub fn add_timer_at<F>(&self, deadline: Instant, mut callback: F) -> Result<RegistrationToken>
where where
F: FnMut(Instant, Ctx) -> TimeoutAction + 'static, F: FnMut(Instant, &mut AppState) -> TimeoutAction + 'static,
{ {
let timer = Timer::from_deadline(deadline); 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<T, F>( pub fn add_channel<T, F>(
@ -78,12 +67,12 @@ where
) -> Result<(RegistrationToken, channel::Sender<T>)> ) -> Result<(RegistrationToken, channel::Sender<T>)>
where where
T: 'static, T: 'static,
F: FnMut(T, Ctx) + 'static, F: FnMut(T, &mut AppState) + 'static,
{ {
let (sender, receiver) = channel::channel(); 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 { if let channel::Event::Msg(msg) = event {
callback(msg, ctx); callback(msg, app_state);
} }
})?; })?;
Ok((token, sender)) Ok((token, sender))
@ -98,11 +87,11 @@ where
) -> Result<RegistrationToken> ) -> Result<RegistrationToken>
where where
T: AsFd + 'static, T: AsFd + 'static,
F: FnMut(Ctx) + 'static, F: FnMut(&mut AppState) + 'static,
{ {
let generic = Generic::new(fd, interest, mode); let generic = Generic::new(fd, interest, mode);
self.insert_source(generic, move |_readiness, _fd, ctx| { self.insert_source(generic, move |_readiness, _fd, app_state| {
callback(ctx); callback(app_state);
Ok(PostAction::Continue) Ok(PostAction::Continue)
}) })
} }

View file

@ -815,7 +815,7 @@ impl ShellRuntime for Runtime {
} }
} }
pub type EventLoopHandle = EventLoopHandleBase<EventContext<'static>>; pub type EventLoopHandle = EventLoopHandleBase;
pub struct EventContext<'a> { pub struct EventContext<'a> {
app_state: &'a mut AppState, app_state: &'a mut AppState,

View file

@ -847,7 +847,7 @@ impl ShellRuntime for Shell {
} }
} }
pub type ShellEventLoopHandle = EventLoopHandleBase<ShellEventContext<'static>>; pub type ShellEventLoopHandle = EventLoopHandleBase;
pub struct ShellEventContext<'a> { pub struct ShellEventContext<'a> {
app_state: &'a mut AppState, app_state: &'a mut AppState,

View file

@ -105,7 +105,7 @@ impl ShellControl {
} }
} }
pub type EventLoopHandle = EventLoopHandleBase<EventContext<'static>>; pub type EventLoopHandle = EventLoopHandleBase;
pub struct EventContext<'a> { pub struct EventContext<'a> {
app_state: &'a mut AppState, app_state: &'a mut AppState,