iOS: Avoid RefCell and static mut (#4255)

* iOS: Refactor queued_gpu_redraws out from AppStateImpl

To allow AppStateImpl to be Copy, and to move redraws into the window in
the future.

* iOS AppState: Avoid RefCell and static mut

Instead, prefer Cell and Copy types, as those will never have crashes
on re-entrancy / if forgetting to make a state transition.
This commit is contained in:
Mads Marquart
2025-06-07 23:16:41 +02:00
committed by GitHub
parent f1e0f6c646
commit e540062ac0
2 changed files with 115 additions and 184 deletions

View File

@@ -39,7 +39,7 @@ pub struct ActiveEventLoop {
impl RootActiveEventLoop for ActiveEventLoop {
fn create_proxy(&self) -> CoreEventLoopProxy {
CoreEventLoopProxy::new(AppState::get_mut(self.mtm).event_loop_proxy().clone())
CoreEventLoopProxy::new(AppState::get(self.mtm).event_loop_proxy().clone())
}
fn create_window(
@@ -73,7 +73,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
fn listen_device_events(&self, _allowed: DeviceEvents) {}
fn set_control_flow(&self, control_flow: ControlFlow) {
AppState::get_mut(self.mtm).set_control_flow(control_flow)
AppState::get(self.mtm).set_control_flow(control_flow)
}
fn system_theme(&self) -> Option<Theme> {
@@ -81,7 +81,7 @@ impl RootActiveEventLoop for ActiveEventLoop {
}
fn control_flow(&self) -> ControlFlow {
AppState::get_mut(self.mtm).control_flow()
AppState::get(self.mtm).control_flow()
}
fn exit(&self) {
@@ -146,13 +146,9 @@ impl EventLoop {
let mtm = MainThreadMarker::new()
.expect("On iOS, `EventLoop` must be created on the main thread");
static mut SINGLETON_INIT: bool = false;
unsafe {
if SINGLETON_INIT {
// Required, AppState is global state, and event loop can only be run once.
return Err(EventLoopError::RecreationAttempt);
}
SINGLETON_INIT = true;
if !AppState::setup_global(mtm) {
// Required, AppState is global state, and event loop can only be run once.
return Err(EventLoopError::RecreationAttempt);
}
// this line sets up the main run loop before `UIApplicationMain`