Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the control flow exception on web #309

Merged
merged 1 commit into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions maplibre-winit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ use maplibre::{
};
use winit::{
event::{ElementState, Event, KeyEvent, WindowEvent},
event_loop::ActiveEventLoop,
keyboard::{Key, NamedKey},
};

use crate::input::{InputController, UpdateState};

pub mod input;

use maplibre::event_loop::EventLoopError;
#[cfg(target_os = "android")]
pub use winit::platform::android::activity as android_activity;

Expand Down Expand Up @@ -75,7 +77,7 @@ pub struct WinitEventLoop<ET: 'static> {
impl<ET: 'static + PartialEq + Debug> EventLoop<ET> for WinitEventLoop<ET> {
type EventLoopProxy = WinitEventLoopProxy<ET>;

fn run<E>(self, mut map: Map<E>, max_frames: Option<u64>)
fn run<E>(self, mut map: Map<E>, max_frames: Option<u64>) -> Result<(), EventLoopError>
where
E: Environment,
<E::MapWindowConfig as MapWindowConfig>::MapWindow: HeadedMapWindow,
Expand All @@ -86,21 +88,20 @@ impl<ET: 'static + PartialEq + Debug> EventLoop<ET> for WinitEventLoop<ET> {
let mut input_controller = InputController::new(0.2, 100.0, 0.1);
let mut scale_factor = map.window().scale_factor();

self.event_loop
.run(move |event, window_target| {
#[cfg(target_os = "android")]
if !map.is_initialized() && event == Event::Resumed {
use tokio::{runtime::Handle, task};

task::block_in_place(|| {
Handle::current().block_on(async {
map.initialize_renderer().await.unwrap();
})
});
return;
}
let loop_ = move |event, window_target: &ActiveEventLoop| {
#[cfg(target_os = "android")]
if !map.is_initialized() && event == Event::Resumed {
use tokio::{runtime::Handle, task};

task::block_in_place(|| {
Handle::current().block_on(async {
map.initialize_renderer().await.unwrap();
})
});
return;
}

match event {
match event {
Event::DeviceEvent {
ref event,
.. // We're not using device_id currently
Expand Down Expand Up @@ -182,7 +183,16 @@ impl<ET: 'static + PartialEq + Debug> EventLoop<ET> for WinitEventLoop<ET> {
}
_ => {}
}
});
};

#[cfg(target_arch = "wasm32")]
{
winit::platform::web::EventLoopExtWebSys::spawn(self.event_loop, loop_);
return Ok(());
}

#[cfg(not(target_arch = "wasm32"))]
return self.event_loop.run(loop_).map_err(|_| EventLoopError);
}

fn create_proxy(&self) -> Self::EventLoopProxy {
Expand Down
3 changes: 2 additions & 1 deletion maplibre-winit/src/noweb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ pub fn run_headed_map<P>(

map.window_mut()
.take_event_loop()
.expect("Event loop is not available")
.expect("event loop is not available")
.run(map, None)
.expect("event loop creation failed")
})
}
7 changes: 6 additions & 1 deletion maplibre/src/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@ pub enum SendEventError {
Closed,
}

/// When sending events to an event loop errors can occur.
#[derive(Error, Debug)]
#[error("event loop creation failed")]
pub struct EventLoopError;

pub trait EventLoopProxy<T: 'static> {
fn send_event(&self, event: T) -> Result<(), SendEventError>;
}

pub trait EventLoop<ET: 'static + PartialEq> {
type EventLoopProxy: EventLoopProxy<ET>;

fn run<E>(self, map: Map<E>, max_frames: Option<u64>)
fn run<E>(self, map: Map<E>, max_frames: Option<u64>) -> Result<(), EventLoopError>
where
E: Environment,
<E::MapWindowConfig as MapWindowConfig>::MapWindow: HeadedMapWindow;
Expand Down
Loading