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 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<Ctx> {
pub struct EventLoopHandleBase {
system: Weak<RefCell<WindowingSystemFacade>>,
_marker: PhantomData<fn(&mut AppState) -> Ctx>,
}
impl<Ctx> EventLoopHandleBase<Ctx> {
impl EventLoopHandleBase {
pub fn new(system: Weak<RefCell<WindowingSystemFacade>>) -> Self {
Self {
system,
_marker: PhantomData,
}
Self { system }
}
}
impl<Ctx> EventLoopHandleBase<Ctx>
where
for<'a> Ctx: FromAppState<'a> + 'a,
{
pub fn insert_source<S, F, R>(&self, source: S, mut callback: F) -> Result<RegistrationToken>
pub fn insert_source<S, F, R>(&self, source: S, callback: F) -> Result<RegistrationToken>
where
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 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<F>(&self, duration: Duration, mut callback: F) -> Result<RegistrationToken>
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<F>(&self, deadline: Instant, mut callback: F) -> Result<RegistrationToken>
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<T, F>(
@ -78,12 +67,12 @@ where
) -> Result<(RegistrationToken, channel::Sender<T>)>
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<RegistrationToken>
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)
})
}

View file

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