mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-27 15:13:13 -04:00
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.
128 lines
3.4 KiB
Rust
128 lines
3.4 KiB
Rust
#![allow(clippy::unnecessary_cast)]
|
|
|
|
use icrate::AppKit::{NSResponder, NSWindow};
|
|
use icrate::Foundation::{MainThreadBound, MainThreadMarker, NSObject};
|
|
use objc2::rc::{autoreleasepool, Id};
|
|
use objc2::{declare_class, mutability, ClassType, DeclaredClass};
|
|
|
|
use super::event_loop::ActiveEventLoop;
|
|
use super::window_delegate::WindowDelegate;
|
|
use crate::error::OsError as RootOsError;
|
|
use crate::window::WindowAttributes;
|
|
|
|
pub(crate) struct Window {
|
|
window: MainThreadBound<Id<WinitWindow>>,
|
|
/// The window only keeps a weak reference to this, so we must keep it around here.
|
|
delegate: MainThreadBound<Id<WindowDelegate>>,
|
|
}
|
|
|
|
impl Drop for Window {
|
|
fn drop(&mut self) {
|
|
self.window
|
|
.get_on_main(|window| autoreleasepool(|_| window.close()))
|
|
}
|
|
}
|
|
|
|
impl Window {
|
|
pub(crate) fn new(
|
|
window_target: &ActiveEventLoop,
|
|
attributes: WindowAttributes,
|
|
) -> Result<Self, RootOsError> {
|
|
let mtm = window_target.mtm;
|
|
let delegate = autoreleasepool(|_| WindowDelegate::new(attributes, mtm))?;
|
|
Ok(Window {
|
|
window: MainThreadBound::new(delegate.window().retain(), mtm),
|
|
delegate: MainThreadBound::new(delegate, mtm),
|
|
})
|
|
}
|
|
|
|
pub(crate) fn maybe_queue_on_main(&self, f: impl FnOnce(&WindowDelegate) + Send + 'static) {
|
|
// For now, don't actually do queuing, since it may be less predictable
|
|
self.maybe_wait_on_main(f)
|
|
}
|
|
|
|
pub(crate) fn maybe_wait_on_main<R: Send>(
|
|
&self,
|
|
f: impl FnOnce(&WindowDelegate) -> R + Send,
|
|
) -> R {
|
|
self.delegate.get_on_main(|delegate| f(delegate))
|
|
}
|
|
|
|
#[cfg(feature = "rwh_06")]
|
|
#[inline]
|
|
pub(crate) fn raw_window_handle_rwh_06(
|
|
&self,
|
|
) -> Result<rwh_06::RawWindowHandle, rwh_06::HandleError> {
|
|
if let Some(mtm) = MainThreadMarker::new() {
|
|
Ok(self.delegate.get(mtm).raw_window_handle_rwh_06())
|
|
} else {
|
|
Err(rwh_06::HandleError::Unavailable)
|
|
}
|
|
}
|
|
|
|
#[cfg(feature = "rwh_06")]
|
|
#[inline]
|
|
pub(crate) fn raw_display_handle_rwh_06(
|
|
&self,
|
|
) -> Result<rwh_06::RawDisplayHandle, rwh_06::HandleError> {
|
|
Ok(rwh_06::RawDisplayHandle::AppKit(
|
|
rwh_06::AppKitDisplayHandle::new(),
|
|
))
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub struct WindowId(pub usize);
|
|
|
|
impl WindowId {
|
|
pub const unsafe fn dummy() -> Self {
|
|
Self(0)
|
|
}
|
|
}
|
|
|
|
impl From<WindowId> for u64 {
|
|
fn from(window_id: WindowId) -> Self {
|
|
window_id.0 as u64
|
|
}
|
|
}
|
|
|
|
impl From<u64> for WindowId {
|
|
fn from(raw_id: u64) -> Self {
|
|
Self(raw_id as usize)
|
|
}
|
|
}
|
|
|
|
declare_class!(
|
|
#[derive(Debug)]
|
|
pub struct WinitWindow;
|
|
|
|
unsafe impl ClassType for WinitWindow {
|
|
#[inherits(NSResponder, NSObject)]
|
|
type Super = NSWindow;
|
|
type Mutability = mutability::MainThreadOnly;
|
|
const NAME: &'static str = "WinitWindow";
|
|
}
|
|
|
|
impl DeclaredClass for WinitWindow {}
|
|
|
|
unsafe impl WinitWindow {
|
|
#[method(canBecomeMainWindow)]
|
|
fn can_become_main_window(&self) -> bool {
|
|
trace_scope!("canBecomeMainWindow");
|
|
true
|
|
}
|
|
|
|
#[method(canBecomeKeyWindow)]
|
|
fn can_become_key_window(&self) -> bool {
|
|
trace_scope!("canBecomeKeyWindow");
|
|
true
|
|
}
|
|
}
|
|
);
|
|
|
|
impl WinitWindow {
|
|
pub(super) fn id(&self) -> WindowId {
|
|
WindowId(self as *const Self as usize)
|
|
}
|
|
}
|