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

@ -74,7 +74,7 @@ use crate::{
DeviceEvent, Event, Force, Ime, InnerSizeWriter, RawKeyEvent, Touch, TouchPhase,
WindowEvent,
},
event_loop::{ControlFlow, DeviceEvents, EventLoopClosed, EventLoopWindowTarget as RootELW},
event_loop::{ActiveEventLoop as RootAEL, ControlFlow, DeviceEvents, EventLoopClosed},
keyboard::ModifiersState,
platform::pump_events::PumpStatus,
platform_impl::platform::{
@ -156,7 +156,7 @@ pub(crate) enum ProcResult {
pub struct EventLoop<T: 'static> {
user_event_sender: Sender<T>,
user_event_receiver: Receiver<T>,
window_target: RootELW,
window_target: RootAEL,
msg_hook: Option<Box<dyn FnMut(*const c_void) -> bool + 'static>>,
}
@ -176,7 +176,7 @@ impl Default for PlatformSpecificEventLoopAttributes {
}
}
pub struct EventLoopWindowTarget {
pub struct ActiveEventLoop {
thread_id: u32,
thread_msg_target: HWND,
pub(crate) runner_shared: EventLoopRunnerShared<UserEventPlaceholder>,
@ -215,8 +215,8 @@ impl<T: 'static> EventLoop<T> {
Ok(EventLoop {
user_event_sender,
user_event_receiver,
window_target: RootELW {
p: EventLoopWindowTarget {
window_target: RootAEL {
p: ActiveEventLoop {
thread_id,
thread_msg_target,
runner_shared,
@ -227,20 +227,20 @@ impl<T: 'static> EventLoop<T> {
})
}
pub fn window_target(&self) -> &RootELW {
pub fn window_target(&self) -> &RootAEL {
&self.window_target
}
pub fn run<F>(mut self, event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<T>, &RootELW),
F: FnMut(Event<T>, &RootAEL),
{
self.run_on_demand(event_handler)
}
pub fn run_on_demand<F>(&mut self, mut event_handler: F) -> Result<(), EventLoopError>
where
F: FnMut(Event<T>, &RootELW),
F: FnMut(Event<T>, &RootAEL),
{
{
let runner = &self.window_target.p.runner_shared;
@ -302,7 +302,7 @@ impl<T: 'static> EventLoop<T> {
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut event_handler: F) -> PumpStatus
where
F: FnMut(Event<T>, &RootELW),
F: FnMut(Event<T>, &RootAEL),
{
{
let runner = &self.window_target.p.runner_shared;
@ -522,7 +522,7 @@ impl<T: 'static> EventLoop<T> {
}
}
impl EventLoopWindowTarget {
impl ActiveEventLoop {
#[inline(always)]
pub(crate) fn create_thread_executor(&self) -> EventLoopThreadExecutor {
EventLoopThreadExecutor {

View file

@ -22,7 +22,7 @@ use crate::{
dpi::PhysicalSize,
};
use super::{util, EventLoopWindowTarget};
use super::{util, ActiveEventLoop};
impl Pixel {
fn convert_to_bgra(&mut self) {
@ -237,7 +237,7 @@ impl WinCursor {
}
}
pub(crate) fn build(cursor: OnlyCursorImageBuilder, _: &EventLoopWindowTarget) -> Self {
pub(crate) fn build(cursor: OnlyCursorImageBuilder, _: &ActiveEventLoop) -> Self {
match Self::new(&cursor.0) {
Ok(cursor) => cursor,
Err(err) => {

View file

@ -8,7 +8,7 @@ use windows_sys::Win32::{
pub(crate) use self::{
event_loop::{
EventLoop, EventLoopProxy, EventLoopWindowTarget, OwnedDisplayHandle,
ActiveEventLoop, EventLoop, EventLoopProxy, OwnedDisplayHandle,
PlatformSpecificEventLoopAttributes,
},
icon::{SelectedCursor, WinIcon},
@ -28,7 +28,7 @@ use crate::keyboard::Key;
use crate::platform::windows::{BackdropType, Color, CornerPreference};
#[derive(Clone, Debug)]
pub struct PlatformSpecificWindowBuilderAttributes {
pub struct PlatformSpecificWindowAttributes {
pub owner: Option<HWND>,
pub menu: Option<HMENU>,
pub taskbar_icon: Option<Icon>,
@ -45,7 +45,7 @@ pub struct PlatformSpecificWindowBuilderAttributes {
pub corner_preference: Option<CornerPreference>,
}
impl Default for PlatformSpecificWindowBuilderAttributes {
impl Default for PlatformSpecificWindowAttributes {
fn default() -> Self {
Self {
owner: None,
@ -66,8 +66,8 @@ impl Default for PlatformSpecificWindowBuilderAttributes {
}
}
unsafe impl Send for PlatformSpecificWindowBuilderAttributes {}
unsafe impl Sync for PlatformSpecificWindowBuilderAttributes {}
unsafe impl Send for PlatformSpecificWindowAttributes {}
unsafe impl Sync for PlatformSpecificWindowAttributes {}
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId(u32);

View file

@ -74,7 +74,7 @@ use crate::{
},
dpi::{dpi_to_scale_factor, enable_non_client_dpi_scaling, hwnd_dpi},
drop_handler::FileDropHandler,
event_loop::{self, EventLoopWindowTarget, DESTROY_MSG_ID},
event_loop::{self, ActiveEventLoop, DESTROY_MSG_ID},
icon::{self, IconType, WinCursor},
ime::ImeContext,
keyboard::KeyEventBuilder,
@ -103,7 +103,7 @@ pub(crate) struct Window {
impl Window {
pub(crate) fn new(
event_loop: &EventLoopWindowTarget,
event_loop: &ActiveEventLoop,
w_attr: WindowAttributes,
) -> Result<Window, RootOsError> {
// We dispatch an `init` function because of code style.
@ -1141,7 +1141,7 @@ impl Drop for Window {
pub(super) struct InitData<'a> {
// inputs
pub event_loop: &'a EventLoopWindowTarget,
pub event_loop: &'a ActiveEventLoop,
pub attributes: WindowAttributes,
pub window_flags: WindowFlags,
// outputs
@ -1339,7 +1339,7 @@ impl<'a> InitData<'a> {
}
unsafe fn init(
attributes: WindowAttributes,
event_loop: &EventLoopWindowTarget,
event_loop: &ActiveEventLoop,
) -> Result<Window, RootOsError> {
let title = util::encode_wide(&attributes.title);