Deprecate window creation with stale event loop

Creating window when event loop is not running generally doesn't work,
since a bunch of events and sync OS requests can't be processed. This
is also an issue on e.g. Android, since window can't be created outside
event loop easily.

Thus deprecate the window creation when event loop is not running,
as well as other resource creation to running event loop.

Given that all the examples use the bad pattern of creating the window
when event loop is not running and also most example existence is
questionable, since they show single thing and the majority of their
code is window/event loop initialization, they wore merged into
a single example 'window.rs' example that showcases very simple
application using winit.

Fixes #3399.
This commit is contained in:
Kirill Chibisov 2024-01-31 17:29:59 +04:00
parent 19190a95a0
commit 3fb93b4f83
90 changed files with 1594 additions and 3495 deletions

View file

@ -22,7 +22,7 @@ use web_sys::{
use super::backend::Style;
use super::main_thread::{MainThreadMarker, MainThreadSafe};
use super::r#async::{AbortHandle, Abortable, DropAbortHandle, Notified, Notifier};
use super::EventLoopWindowTarget;
use super::ActiveEventLoop;
use crate::cursor::{BadImage, Cursor, CursorImage, CustomCursor as RootCustomCursor};
use crate::platform::web::CustomCursorError;
@ -75,10 +75,7 @@ impl PartialEq for CustomCursor {
impl Eq for CustomCursor {}
impl CustomCursor {
pub(crate) fn build(
builder: CustomCursorBuilder,
window_target: &EventLoopWindowTarget,
) -> Self {
pub(crate) fn build(builder: CustomCursorBuilder, window_target: &ActiveEventLoop) -> Self {
match builder {
CustomCursorBuilder::Image(image) => Self::build_spawn(
window_target,
@ -110,11 +107,7 @@ impl CustomCursor {
}
}
fn build_spawn<F, S>(
window_target: &EventLoopWindowTarget,
task: F,
animation: bool,
) -> CustomCursor
fn build_spawn<F, S>(window_target: &ActiveEventLoop, task: F, animation: bool) -> CustomCursor
where
F: 'static + Future<Output = Result<S, CustomCursorError>>,
S: Into<ImageState>,
@ -172,7 +165,7 @@ impl CustomCursor {
pub(crate) fn build_async(
builder: CustomCursorBuilder,
window_target: &EventLoopWindowTarget,
window_target: &ActiveEventLoop,
) -> CustomCursorFuture {
let CustomCursor { animation, state } = Self::build(builder, window_target);
let binding = state.get(window_target.runner.main_thread()).borrow();

View file

@ -3,7 +3,7 @@ use std::sync::mpsc::{self, Receiver, Sender};
use crate::error::EventLoopError;
use crate::event::Event;
use crate::event_loop::EventLoopWindowTarget as RootEventLoopWindowTarget;
use crate::event_loop::ActiveEventLoop as RootActiveEventLoop;
use super::{backend, device, window};
@ -13,10 +13,10 @@ mod state;
mod window_target;
pub(crate) use proxy::EventLoopProxy;
pub(crate) use window_target::{EventLoopWindowTarget, OwnedDisplayHandle};
pub(crate) use window_target::{ActiveEventLoop, OwnedDisplayHandle};
pub struct EventLoop<T: 'static> {
elw: RootEventLoopWindowTarget,
elw: RootActiveEventLoop,
user_event_sender: Sender<T>,
user_event_receiver: Receiver<T>,
}
@ -27,8 +27,8 @@ pub(crate) struct PlatformSpecificEventLoopAttributes {}
impl<T> EventLoop<T> {
pub(crate) fn new(_: &PlatformSpecificEventLoopAttributes) -> Result<Self, EventLoopError> {
let (user_event_sender, user_event_receiver) = mpsc::channel();
let elw = RootEventLoopWindowTarget {
p: EventLoopWindowTarget::new(),
let elw = RootActiveEventLoop {
p: ActiveEventLoop::new(),
_marker: PhantomData,
};
Ok(EventLoop {
@ -40,9 +40,9 @@ impl<T> EventLoop<T> {
pub fn run<F>(self, mut event_handler: F) -> !
where
F: FnMut(Event<T>, &RootEventLoopWindowTarget),
F: FnMut(Event<T>, &RootActiveEventLoop),
{
let target = RootEventLoopWindowTarget {
let target = RootActiveEventLoop {
p: self.elw.p.clone(),
_marker: PhantomData,
};
@ -77,9 +77,9 @@ impl<T> EventLoop<T> {
pub fn spawn<F>(self, mut event_handler: F)
where
F: 'static + FnMut(Event<T>, &RootEventLoopWindowTarget),
F: 'static + FnMut(Event<T>, &RootActiveEventLoop),
{
let target = RootEventLoopWindowTarget {
let target = RootActiveEventLoop {
p: self.elw.p.clone(),
_marker: PhantomData,
};
@ -105,7 +105,7 @@ impl<T> EventLoop<T> {
EventLoopProxy::new(self.elw.p.waker(), self.user_event_sender.clone())
}
pub fn window_target(&self) -> &RootEventLoopWindowTarget {
pub fn window_target(&self) -> &RootActiveEventLoop {
&self.elw
}
}

View file

@ -214,7 +214,7 @@ impl Shared {
// Set the event callback to use for the event loop runner
// This the event callback is a fairly thin layer over the user-provided callback that closes
// over a RootEventLoopWindowTarget reference
// over a RootActiveEventLoop reference
pub fn set_listener(&self, event_handler: Box<EventHandler>) {
{
let mut runner = self.0.runner.borrow_mut();
@ -735,7 +735,7 @@ impl Shared {
// * `self`, i.e. the item which triggered this event loop wakeup, which
// is usually a `wasm-bindgen` `Closure`, which will be dropped after
// returning to the JS glue code.
// * The `EventLoopWindowTarget` leaked inside `EventLoop::run` due to the
// * The `ActiveEventLoop` leaked inside `EventLoop::run` due to the
// JS exception thrown at the end.
// * For each undropped `Window`:
// * The `register_redraw_request` closure.

View file

@ -43,12 +43,12 @@ impl Clone for ModifiersShared {
}
#[derive(Clone)]
pub struct EventLoopWindowTarget {
pub struct ActiveEventLoop {
pub(crate) runner: runner::Shared,
modifiers: ModifiersShared,
}
impl EventLoopWindowTarget {
impl ActiveEventLoop {
pub fn new() -> Self {
Self {
runner: runner::Shared::new(),

View file

@ -12,7 +12,7 @@
// for winit's cross-platform structures. They are all relatively simple translations.
//
// The event_loop module handles listening for and processing events. 'Proxy' implements
// EventLoopProxy and 'WindowTarget' implements EventLoopWindowTarget. WindowTarget also handles
// EventLoopProxy and 'WindowTarget' implements ActiveEventLoop. WindowTarget also handles
// registering the event handlers. The 'Execution' struct in the 'runner' module handles taking
// incoming events (from the registered handlers) and ensuring they are passed to the user in a
// compliant way.
@ -33,11 +33,11 @@ mod backend;
pub use self::device::DeviceId;
pub use self::error::OsError;
pub(crate) use self::event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle,
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
PlatformSpecificEventLoopAttributes,
};
pub use self::monitor::{MonitorHandle, VideoModeHandle};
pub use self::window::{PlatformSpecificWindowBuilderAttributes, Window, WindowId};
pub use self::window::{PlatformSpecificWindowAttributes, Window, WindowId};
pub(crate) use self::keyboard::KeyEventExtra;
pub(crate) use crate::icon::NoIcon as PlatformIcon;

View file

@ -8,7 +8,7 @@ use crate::window::{
use super::main_thread::{MainThreadMarker, MainThreadSafe};
use super::r#async::Dispatcher;
use super::{backend, monitor::MonitorHandle, EventLoopWindowTarget, Fullscreen};
use super::{backend, monitor::MonitorHandle, ActiveEventLoop, Fullscreen};
use web_sys::HtmlCanvasElement;
use std::cell::RefCell;
@ -29,7 +29,7 @@ pub struct Inner {
impl Window {
pub(crate) fn new(
target: &EventLoopWindowTarget,
target: &ActiveEventLoop,
mut attr: WindowAttributes,
) -> Result<Self, RootOE> {
let id = target.generate_id();
@ -466,14 +466,14 @@ impl From<u64> for WindowId {
}
#[derive(Clone, Debug)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub struct PlatformSpecificWindowAttributes {
pub(crate) canvas: Option<Arc<MainThreadSafe<backend::RawCanvasType>>>,
pub(crate) prevent_default: bool,
pub(crate) focusable: bool,
pub(crate) append: bool,
}
impl PlatformSpecificWindowBuilderAttributes {
impl PlatformSpecificWindowAttributes {
pub(crate) fn set_canvas(&mut self, canvas: Option<backend::RawCanvasType>) {
let Some(canvas) = canvas else {
self.canvas = None;
@ -487,7 +487,7 @@ impl PlatformSpecificWindowBuilderAttributes {
}
}
impl Default for PlatformSpecificWindowBuilderAttributes {
impl Default for PlatformSpecificWindowAttributes {
fn default() -> Self {
Self {
canvas: None,