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:
parent
19190a95a0
commit
3fb93b4f83
90 changed files with 1594 additions and 3495 deletions
|
|
@ -25,13 +25,13 @@ use crate::event::{
|
|||
WindowEvent,
|
||||
};
|
||||
use crate::event::{InnerSizeWriter, MouseButton};
|
||||
use crate::event_loop::EventLoopWindowTarget as RootELW;
|
||||
use crate::event_loop::ActiveEventLoop as RootAEL;
|
||||
use crate::keyboard::ModifiersState;
|
||||
use crate::platform_impl::common::xkb::{self, XkbState};
|
||||
use crate::platform_impl::platform::common::xkb::Context;
|
||||
use crate::platform_impl::platform::x11::ime::{ImeEvent, ImeEventReceiver, ImeRequest};
|
||||
use crate::platform_impl::platform::x11::EventLoopWindowTarget;
|
||||
use crate::platform_impl::platform::EventLoopWindowTarget as PlatformEventLoopWindowTarget;
|
||||
use crate::platform_impl::platform::x11::ActiveEventLoop;
|
||||
use crate::platform_impl::platform::ActiveEventLoop as PlatformActiveEventLoop;
|
||||
use crate::platform_impl::x11::{
|
||||
atoms::*, mkdid, mkwid, util, CookieResultExt, Device, DeviceId, DeviceInfo, Dnd, DndState,
|
||||
GenericEventCookie, ImeReceiver, ScrollOrientation, UnownedWindow, WindowId,
|
||||
|
|
@ -48,7 +48,7 @@ pub struct EventProcessor {
|
|||
pub devices: RefCell<HashMap<DeviceId, Device>>,
|
||||
pub xi2ext: ExtensionInformation,
|
||||
pub xkbext: ExtensionInformation,
|
||||
pub target: RootELW,
|
||||
pub target: RootAEL,
|
||||
pub xkb_context: Context,
|
||||
// Number of touch events currently in progress
|
||||
pub num_touch: u32,
|
||||
|
|
@ -68,7 +68,7 @@ pub struct EventProcessor {
|
|||
impl EventProcessor {
|
||||
pub fn process_event<T: 'static, F>(&mut self, xev: &mut XEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
self.process_xevent(xev, &mut callback);
|
||||
|
||||
|
|
@ -135,7 +135,7 @@ impl EventProcessor {
|
|||
|
||||
fn process_xevent<T: 'static, F>(&mut self, xev: &mut XEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
if self.filter_event(xev) {
|
||||
return;
|
||||
|
|
@ -334,18 +334,18 @@ impl EventProcessor {
|
|||
|
||||
// NOTE: we avoid `self` to not borrow the entire `self` as not mut.
|
||||
/// Get the platform window target.
|
||||
pub fn window_target(window_target: &RootELW) -> &EventLoopWindowTarget {
|
||||
pub fn window_target(window_target: &RootAEL) -> &ActiveEventLoop {
|
||||
match &window_target.p {
|
||||
PlatformEventLoopWindowTarget::X(target) => target,
|
||||
PlatformActiveEventLoop::X(target) => target,
|
||||
#[cfg(wayland_platform)]
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the platform window target.
|
||||
pub fn window_target_mut(window_target: &mut RootELW) -> &mut EventLoopWindowTarget {
|
||||
pub fn window_target_mut(window_target: &mut RootAEL) -> &mut ActiveEventLoop {
|
||||
match &mut window_target.p {
|
||||
PlatformEventLoopWindowTarget::X(target) => target,
|
||||
PlatformActiveEventLoop::X(target) => target,
|
||||
#[cfg(wayland_platform)]
|
||||
_ => unreachable!(),
|
||||
}
|
||||
|
|
@ -353,7 +353,7 @@ impl EventProcessor {
|
|||
|
||||
fn client_message<T: 'static, F>(&mut self, xev: &XClientMessageEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let atoms = wt.xconn.atoms();
|
||||
|
|
@ -524,7 +524,7 @@ impl EventProcessor {
|
|||
|
||||
fn selection_notify<T: 'static, F>(&mut self, xev: &XSelectionEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let atoms = wt.xconn.atoms();
|
||||
|
|
@ -558,7 +558,7 @@ impl EventProcessor {
|
|||
|
||||
fn configure_notify<T: 'static, F>(&self, xev: &XConfigureEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -765,7 +765,7 @@ impl EventProcessor {
|
|||
|
||||
fn map_notify<T: 'static, F>(&self, xev: &XMapEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let window = xev.window as xproto::Window;
|
||||
let window_id = mkwid(window);
|
||||
|
|
@ -788,7 +788,7 @@ impl EventProcessor {
|
|||
|
||||
fn destroy_notify<T: 'static, F>(&self, xev: &XDestroyWindowEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -818,7 +818,7 @@ impl EventProcessor {
|
|||
|
||||
fn property_notify<T: 'static, F>(&mut self, xev: &XPropertyEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let atoms = wt.x_connection().atoms();
|
||||
|
|
@ -833,7 +833,7 @@ impl EventProcessor {
|
|||
|
||||
fn visibility_notify<T: 'static, F>(&self, xev: &XVisibilityEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let xwindow = xev.window as xproto::Window;
|
||||
|
||||
|
|
@ -850,7 +850,7 @@ impl EventProcessor {
|
|||
|
||||
fn expose<T: 'static, F>(&self, xev: &XExposeEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
// Multiple Expose events may be received for subareas of a window.
|
||||
// We issue `RedrawRequested` only for the last event of such a series.
|
||||
|
|
@ -873,7 +873,7 @@ impl EventProcessor {
|
|||
state: ElementState,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -976,7 +976,7 @@ impl EventProcessor {
|
|||
state: ElementState,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let window_id = mkwid(event.event as xproto::Window);
|
||||
|
|
@ -1046,7 +1046,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_mouse_motion<T: 'static, F>(&self, event: &XIDeviceEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1137,7 +1137,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_mouse_enter<T: 'static, F>(&self, event: &XIEnterEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1188,7 +1188,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_mouse_left<T: 'static, F>(&self, event: &XILeaveEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let window = event.event as xproto::Window;
|
||||
|
|
@ -1211,7 +1211,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_focused<T: 'static, F>(&mut self, xev: &XIFocusInEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let window = xev.event as xproto::Window;
|
||||
|
|
@ -1281,7 +1281,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_unfocused<T: 'static, F>(&mut self, xev: &XIFocusOutEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
let window = xev.event as xproto::Window;
|
||||
|
|
@ -1337,7 +1337,7 @@ impl EventProcessor {
|
|||
phase: TouchPhase,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1383,7 +1383,7 @@ impl EventProcessor {
|
|||
state: ElementState,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1404,7 +1404,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_raw_mouse_motion<T: 'static, F>(&self, xev: &XIRawEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1471,7 +1471,7 @@ impl EventProcessor {
|
|||
state: ElementState,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1499,7 +1499,7 @@ impl EventProcessor {
|
|||
|
||||
fn xinput2_hierarchy_changed<T: 'static, F>(&mut self, xev: &XIHierarchyEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
|
||||
|
|
@ -1532,7 +1532,7 @@ impl EventProcessor {
|
|||
|
||||
fn xkb_event<T: 'static, F>(&mut self, xev: &XkbAnyEvent, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
match xev.xkb_type {
|
||||
|
|
@ -1597,7 +1597,7 @@ impl EventProcessor {
|
|||
group: &XIModifierState,
|
||||
mut callback: F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
if let Some(state) = self.xkb_context.state_mut() {
|
||||
state.update_modifiers(
|
||||
|
|
@ -1616,7 +1616,7 @@ impl EventProcessor {
|
|||
|
||||
pub fn udpate_mods_from_core_event<T: 'static, F>(&mut self, state: u16, mut callback: F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let xkb_mask = self.xkb_mod_mask_from_core(state);
|
||||
let xkb_state = match self.xkb_context.state_mut() {
|
||||
|
|
@ -1693,7 +1693,7 @@ impl EventProcessor {
|
|||
/// Send modifiers for the active window.
|
||||
///
|
||||
/// The event won't be sent when the `modifiers` match the previously `sent` modifiers value.
|
||||
fn send_modifiers<T: 'static, F: FnMut(&RootELW, Event<T>)>(
|
||||
fn send_modifiers<T: 'static, F: FnMut(&RootAEL, Event<T>)>(
|
||||
&self,
|
||||
modifiers: ModifiersState,
|
||||
callback: &mut F,
|
||||
|
|
@ -1715,13 +1715,13 @@ impl EventProcessor {
|
|||
}
|
||||
|
||||
fn handle_pressed_keys<T: 'static, F>(
|
||||
target: &RootELW,
|
||||
target: &RootAEL,
|
||||
window_id: crate::window::WindowId,
|
||||
state: ElementState,
|
||||
xkb_context: &mut Context,
|
||||
callback: &mut F,
|
||||
) where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let device_id = mkdid(util::VIRTUAL_CORE_KEYBOARD);
|
||||
|
||||
|
|
@ -1768,7 +1768,7 @@ impl EventProcessor {
|
|||
|
||||
fn process_dpi_change<T: 'static, F>(&self, callback: &mut F)
|
||||
where
|
||||
F: FnMut(&RootELW, Event<T>),
|
||||
F: FnMut(&RootAEL, Event<T>),
|
||||
{
|
||||
let wt = Self::window_target(&self.target);
|
||||
wt.xconn
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ use super::{ControlFlow, OsError};
|
|||
use crate::{
|
||||
error::{EventLoopError, OsError as RootOsError},
|
||||
event::{Event, StartCause, WindowEvent},
|
||||
event_loop::{DeviceEvents, EventLoopClosed, EventLoopWindowTarget as RootELW},
|
||||
event_loop::{ActiveEventLoop as RootAEL, DeviceEvents, EventLoopClosed},
|
||||
platform::pump_events::PumpStatus,
|
||||
platform_impl::common::xkb::Context,
|
||||
platform_impl::platform::{min_timeout, WindowId},
|
||||
|
|
@ -126,7 +126,7 @@ impl<T> PeekableReceiver<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct EventLoopWindowTarget {
|
||||
pub struct ActiveEventLoop {
|
||||
xconn: Arc<XConnection>,
|
||||
wm_delete_window: xproto::Atom,
|
||||
net_wm_ping: xproto::Atom,
|
||||
|
|
@ -285,7 +285,7 @@ impl<T: 'static> EventLoop<T> {
|
|||
let xkb_context =
|
||||
Context::from_x11_xkb(xconn.xcb_connection().get_raw_xcb_connection()).unwrap();
|
||||
|
||||
let window_target = EventLoopWindowTarget {
|
||||
let window_target = ActiveEventLoop {
|
||||
ime,
|
||||
root,
|
||||
control_flow: Cell::new(ControlFlow::default()),
|
||||
|
|
@ -309,8 +309,8 @@ impl<T: 'static> EventLoop<T> {
|
|||
// Set initial device event filter.
|
||||
window_target.update_listen_device_events(true);
|
||||
|
||||
let root_window_target = RootELW {
|
||||
p: super::EventLoopWindowTarget::X(window_target),
|
||||
let root_window_target = RootAEL {
|
||||
p: super::ActiveEventLoop::X(window_target),
|
||||
_marker: PhantomData,
|
||||
};
|
||||
|
||||
|
|
@ -379,13 +379,13 @@ impl<T: 'static> EventLoop<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn window_target(&self) -> &RootELW {
|
||||
pub(crate) fn window_target(&self) -> &RootAEL {
|
||||
&self.event_processor.target
|
||||
}
|
||||
|
||||
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 exit = loop {
|
||||
match self.pump_events(None, &mut event_handler) {
|
||||
|
|
@ -415,7 +415,7 @@ impl<T: 'static> EventLoop<T> {
|
|||
|
||||
pub fn pump_events<F>(&mut self, timeout: Option<Duration>, mut callback: F) -> PumpStatus
|
||||
where
|
||||
F: FnMut(Event<T>, &RootELW),
|
||||
F: FnMut(Event<T>, &RootAEL),
|
||||
{
|
||||
if !self.loop_running {
|
||||
self.loop_running = true;
|
||||
|
|
@ -448,7 +448,7 @@ impl<T: 'static> EventLoop<T> {
|
|||
|
||||
pub fn poll_events_with_timeout<F>(&mut self, mut timeout: Option<Duration>, mut callback: F)
|
||||
where
|
||||
F: FnMut(Event<T>, &RootELW),
|
||||
F: FnMut(Event<T>, &RootAEL),
|
||||
{
|
||||
let start = Instant::now();
|
||||
|
||||
|
|
@ -526,7 +526,7 @@ impl<T: 'static> EventLoop<T> {
|
|||
|
||||
fn single_iteration<F>(&mut self, callback: &mut F, cause: StartCause)
|
||||
where
|
||||
F: FnMut(Event<T>, &RootELW),
|
||||
F: FnMut(Event<T>, &RootAEL),
|
||||
{
|
||||
callback(Event::NewEvents(cause), &self.event_processor.target);
|
||||
|
||||
|
|
@ -600,7 +600,7 @@ impl<T: 'static> EventLoop<T> {
|
|||
|
||||
fn drain_events<F>(&mut self, callback: &mut F)
|
||||
where
|
||||
F: FnMut(Event<T>, &RootELW),
|
||||
F: FnMut(Event<T>, &RootAEL),
|
||||
{
|
||||
let mut xev = MaybeUninit::uninit();
|
||||
|
||||
|
|
@ -655,7 +655,7 @@ impl<T> AsRawFd for EventLoop<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl EventLoopWindowTarget {
|
||||
impl ActiveEventLoop {
|
||||
/// Returns the `XConnection` of this events loop.
|
||||
#[inline]
|
||||
pub(crate) fn x_connection(&self) -> &Arc<XConnection> {
|
||||
|
|
@ -815,7 +815,7 @@ impl Deref for Window {
|
|||
|
||||
impl Window {
|
||||
pub(crate) fn new(
|
||||
event_loop: &EventLoopWindowTarget,
|
||||
event_loop: &ActiveEventLoop,
|
||||
attribs: WindowAttributes,
|
||||
) -> Result<Self, RootOsError> {
|
||||
let window = Arc::new(UnownedWindow::new(event_loop, attribs)?);
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ use x11rb::connection::Connection;
|
|||
|
||||
use crate::{platform_impl::PlatformCustomCursorBuilder, window::CursorIcon};
|
||||
|
||||
use super::super::EventLoopWindowTarget;
|
||||
use super::super::ActiveEventLoop;
|
||||
use super::*;
|
||||
|
||||
impl XConnection {
|
||||
|
|
@ -124,10 +124,7 @@ impl PartialEq for CustomCursor {
|
|||
impl Eq for CustomCursor {}
|
||||
|
||||
impl CustomCursor {
|
||||
pub(crate) fn build(
|
||||
builder: PlatformCustomCursorBuilder,
|
||||
p: &EventLoopWindowTarget,
|
||||
) -> CustomCursor {
|
||||
pub(crate) fn build(builder: PlatformCustomCursorBuilder, p: &ActiveEventLoop) -> CustomCursor {
|
||||
unsafe {
|
||||
let ximage = (p.xconn.xcursor.XcursorImageCreate)(
|
||||
builder.0.width as i32,
|
||||
|
|
|
|||
|
|
@ -44,8 +44,7 @@ use crate::{
|
|||
use super::{
|
||||
ffi,
|
||||
util::{self, SelectedCursor},
|
||||
CookieResultExt, EventLoopWindowTarget, ImeRequest, ImeSender, VoidCookie, WindowId,
|
||||
XConnection,
|
||||
ActiveEventLoop, CookieResultExt, ImeRequest, ImeSender, VoidCookie, WindowId, XConnection,
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -153,7 +152,7 @@ macro_rules! leap {
|
|||
impl UnownedWindow {
|
||||
#[allow(clippy::unnecessary_cast)]
|
||||
pub(crate) fn new(
|
||||
event_loop: &EventLoopWindowTarget,
|
||||
event_loop: &ActiveEventLoop,
|
||||
window_attrs: WindowAttributes,
|
||||
) -> Result<UnownedWindow, RootOsError> {
|
||||
let xconn = &event_loop.xconn;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue