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

@ -308,7 +308,7 @@ impl EventState {
pub struct EventLoop<T> {
windows: Vec<(Arc<RedoxSocket>, EventState)>,
window_target: event_loop::EventLoopWindowTarget,
window_target: event_loop::ActiveEventLoop,
user_events_sender: mpsc::Sender<T>,
user_events_receiver: mpsc::Receiver<T>,
}
@ -340,8 +340,8 @@ impl<T: 'static> EventLoop<T> {
Ok(Self {
windows: Vec::new(),
window_target: event_loop::EventLoopWindowTarget {
p: EventLoopWindowTarget {
window_target: event_loop::ActiveEventLoop {
p: ActiveEventLoop {
control_flow: Cell::new(ControlFlow::default()),
exit: Cell::new(false),
creates: Mutex::new(VecDeque::new()),
@ -544,10 +544,10 @@ impl<T: 'static> EventLoop<T> {
pub fn run<F>(mut self, mut event_handler_inner: F) -> Result<(), EventLoopError>
where
F: FnMut(event::Event<T>, &event_loop::EventLoopWindowTarget),
F: FnMut(event::Event<T>, &event_loop::ActiveEventLoop),
{
let mut event_handler =
move |event: event::Event<T>, window_target: &event_loop::EventLoopWindowTarget| {
move |event: event::Event<T>, window_target: &event_loop::ActiveEventLoop| {
event_handler_inner(event, window_target);
};
@ -754,7 +754,7 @@ impl<T: 'static> EventLoop<T> {
Ok(())
}
pub fn window_target(&self) -> &event_loop::EventLoopWindowTarget {
pub fn window_target(&self) -> &event_loop::ActiveEventLoop {
&self.window_target
}
@ -794,7 +794,7 @@ impl<T> Clone for EventLoopProxy<T> {
impl<T> Unpin for EventLoopProxy<T> {}
pub struct EventLoopWindowTarget {
pub struct ActiveEventLoop {
control_flow: Cell<ControlFlow>,
exit: Cell<bool>,
pub(super) creates: Mutex<VecDeque<Arc<RedoxSocket>>>,
@ -804,7 +804,7 @@ pub struct EventLoopWindowTarget {
pub(super) wake_socket: Arc<TimeSocket>,
}
impl EventLoopWindowTarget {
impl ActiveEventLoop {
pub fn primary_monitor(&self) -> Option<MonitorHandle> {
Some(MonitorHandle)
}

View file

@ -11,9 +11,7 @@ use crate::{
keyboard::Key,
};
pub(crate) use self::event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle,
};
pub(crate) use self::event_loop::{ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle};
mod event_loop;
pub use self::window::Window;
@ -135,7 +133,7 @@ impl DeviceId {
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct PlatformSpecificWindowBuilderAttributes;
pub struct PlatformSpecificWindowAttributes;
struct WindowProperties<'a> {
flags: &'a str,

View file

@ -13,8 +13,7 @@ use crate::{
};
use super::{
EventLoopWindowTarget, MonitorHandle, OsError, RedoxSocket, TimeSocket, WindowId,
WindowProperties,
ActiveEventLoop, MonitorHandle, OsError, RedoxSocket, TimeSocket, WindowId, WindowProperties,
};
// These values match the values uses in the `window_new` function in orbital:
@ -37,7 +36,7 @@ pub struct Window {
impl Window {
pub(crate) fn new(
el: &EventLoopWindowTarget,
el: &ActiveEventLoop,
attrs: window::WindowAttributes,
) -> Result<Self, error::OsError> {
let scale = MonitorHandle.scale_factor();