2024-01-25 05:26:50 +01:00
|
|
|
use std::cell::{Cell, RefCell};
|
2024-01-14 03:37:53 +01:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
use std::mem;
|
2024-02-28 04:33:47 +01:00
|
|
|
use std::rc::Weak;
|
2024-01-25 05:26:50 +01:00
|
|
|
use std::sync::{Arc, Mutex};
|
2024-01-14 03:37:53 +01:00
|
|
|
use std::time::Instant;
|
|
|
|
|
|
2023-07-29 00:33:03 +02:00
|
|
|
use objc2::rc::Id;
|
2023-08-02 16:30:41 +02:00
|
|
|
use objc2::runtime::AnyObject;
|
2023-12-23 18:04:24 +01:00
|
|
|
use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};
|
2024-04-18 17:34:19 +02:00
|
|
|
use objc2_app_kit::{NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate};
|
|
|
|
|
use objc2_foundation::{MainThreadMarker, NSObject, NSObjectProtocol, NSSize};
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2024-02-28 04:33:47 +01:00
|
|
|
use super::event_handler::EventHandler;
|
|
|
|
|
use super::event_loop::{stop_app_immediately, ActiveEventLoop, PanicInfo};
|
2024-01-14 03:37:53 +01:00
|
|
|
use super::observer::{EventLoopWaker, RunLoop};
|
|
|
|
|
use super::window::WinitWindow;
|
|
|
|
|
use super::{menu, WindowId, DEVICE_ID};
|
|
|
|
|
use crate::dpi::PhysicalSize;
|
|
|
|
|
use crate::event::{DeviceEvent, Event, InnerSizeWriter, StartCause, WindowEvent};
|
2024-02-28 04:33:47 +01:00
|
|
|
use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow};
|
2024-01-14 03:37:53 +01:00
|
|
|
use crate::window::WindowId as RootWindowId;
|
2022-06-08 11:50:26 -07:00
|
|
|
|
2024-04-18 17:34:19 +02:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct Policy(NSApplicationActivationPolicy);
|
|
|
|
|
|
|
|
|
|
impl Default for Policy {
|
|
|
|
|
fn default() -> Self {
|
|
|
|
|
Self(NSApplicationActivationPolicy::Regular)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-14 03:37:53 +01:00
|
|
|
#[derive(Debug, Default)]
|
2023-12-23 18:04:24 +01:00
|
|
|
pub(super) struct State {
|
2024-04-18 17:34:19 +02:00
|
|
|
activation_policy: Policy,
|
2023-12-23 18:04:24 +01:00
|
|
|
default_menu: bool,
|
|
|
|
|
activate_ignoring_other_apps: bool,
|
2024-02-28 04:33:47 +01:00
|
|
|
event_handler: EventHandler,
|
2024-01-14 03:37:53 +01:00
|
|
|
stop_on_launch: Cell<bool>,
|
|
|
|
|
stop_before_wait: Cell<bool>,
|
|
|
|
|
stop_after_wait: Cell<bool>,
|
|
|
|
|
stop_on_redraw: Cell<bool>,
|
|
|
|
|
/// Whether `applicationDidFinishLaunching:` has been run or not.
|
|
|
|
|
is_launched: Cell<bool>,
|
|
|
|
|
/// Whether an `EventLoop` is currently running.
|
|
|
|
|
is_running: Cell<bool>,
|
|
|
|
|
/// Whether the user has requested the event loop to exit.
|
|
|
|
|
exit: Cell<bool>,
|
|
|
|
|
control_flow: Cell<ControlFlow>,
|
|
|
|
|
waker: RefCell<EventLoopWaker>,
|
|
|
|
|
start_time: Cell<Option<Instant>>,
|
|
|
|
|
wait_timeout: Cell<Option<Instant>>,
|
|
|
|
|
pending_events: RefCell<VecDeque<QueuedEvent>>,
|
|
|
|
|
pending_redraw: RefCell<Vec<WindowId>>,
|
2024-05-06 16:29:07 +02:00
|
|
|
// NOTE: This is strongly referenced by our `NSWindowDelegate` and our `NSView` subclass, and
|
|
|
|
|
// as such should be careful to not add fields that, in turn, strongly reference those.
|
2023-12-23 18:04:24 +01:00
|
|
|
}
|
2022-06-09 10:08:52 -04:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
declare_class!(
|
2024-01-14 03:37:53 +01:00
|
|
|
#[derive(Debug)]
|
2023-12-23 18:04:24 +01:00
|
|
|
pub(super) struct ApplicationDelegate;
|
2023-07-29 00:33:03 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
unsafe impl ClassType for ApplicationDelegate {
|
|
|
|
|
type Super = NSObject;
|
2023-12-23 23:07:55 +01:00
|
|
|
type Mutability = mutability::MainThreadOnly;
|
2022-09-02 18:46:18 +02:00
|
|
|
const NAME: &'static str = "WinitApplicationDelegate";
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 18:04:24 +01:00
|
|
|
impl DeclaredClass for ApplicationDelegate {
|
|
|
|
|
type Ivars = State;
|
|
|
|
|
}
|
2019-05-01 17:03:30 -06:00
|
|
|
|
2023-12-23 23:07:55 +01:00
|
|
|
unsafe impl NSObjectProtocol for ApplicationDelegate {}
|
|
|
|
|
|
|
|
|
|
unsafe impl NSApplicationDelegate for ApplicationDelegate {
|
2024-05-06 16:29:07 +02:00
|
|
|
// NOTE: This will, globally, only be run once, no matter how many
|
2024-01-14 03:37:53 +01:00
|
|
|
// `EventLoop`s the user creates.
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(applicationDidFinishLaunching:)]
|
2023-08-02 16:30:41 +02:00
|
|
|
fn did_finish_launching(&self, _sender: Option<&AnyObject>) {
|
2022-09-02 18:46:18 +02:00
|
|
|
trace_scope!("applicationDidFinishLaunching:");
|
2024-01-14 03:37:53 +01:00
|
|
|
self.ivars().is_launched.set(true);
|
|
|
|
|
|
|
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
// We need to delay setting the activation policy and activating the app
|
|
|
|
|
// until `applicationDidFinishLaunching` has been called. Otherwise the
|
|
|
|
|
// menu bar is initially unresponsive on macOS 10.15.
|
2024-04-18 17:34:19 +02:00
|
|
|
app.setActivationPolicy(self.ivars().activation_policy.0);
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
window_activation_hack(&app);
|
|
|
|
|
#[allow(deprecated)]
|
|
|
|
|
app.activateIgnoringOtherApps(self.ivars().activate_ignoring_other_apps);
|
|
|
|
|
|
|
|
|
|
if self.ivars().default_menu {
|
|
|
|
|
// The menubar initialization should be before the `NewEvents` event, to allow
|
|
|
|
|
// overriding of the default menu even if it's created
|
|
|
|
|
menu::initialize(&app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
self.ivars().waker.borrow_mut().start();
|
|
|
|
|
|
|
|
|
|
self.set_is_running(true);
|
|
|
|
|
self.dispatch_init_events();
|
|
|
|
|
|
2024-02-23 14:37:21 +04:00
|
|
|
// If the application is being launched via `EventLoop::pump_app_events()` then we'll
|
2024-01-14 03:37:53 +01:00
|
|
|
// want to stop the app once it is launched (and return to the external loop)
|
|
|
|
|
//
|
|
|
|
|
// In this case we still want to consider Winit's `EventLoop` to be "running",
|
|
|
|
|
// so we call `start_running()` above.
|
|
|
|
|
if self.ivars().stop_on_launch.get() {
|
2024-05-06 16:29:07 +02:00
|
|
|
// NOTE: the original idea had been to only stop the underlying `RunLoop`
|
2024-01-14 03:37:53 +01:00
|
|
|
// for the app but that didn't work as expected (`-[NSApplication run]`
|
|
|
|
|
// effectively ignored the attempt to stop the RunLoop and re-started it).
|
|
|
|
|
//
|
|
|
|
|
// So we return from `pump_events` by stopping the application.
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
stop_app_immediately(&app);
|
|
|
|
|
}
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2023-07-29 00:33:03 +02:00
|
|
|
#[method(applicationWillTerminate:)]
|
2023-08-02 16:30:41 +02:00
|
|
|
fn will_terminate(&self, _sender: Option<&AnyObject>) {
|
2022-09-02 18:46:18 +02:00
|
|
|
trace_scope!("applicationWillTerminate:");
|
|
|
|
|
// TODO: Notify every window that it will be destroyed, like done in iOS?
|
2024-01-14 03:37:53 +01:00
|
|
|
self.internal_exit();
|
2022-09-02 18:46:18 +02:00
|
|
|
}
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|
2022-09-02 18:46:18 +02:00
|
|
|
);
|
2021-04-30 11:31:28 +02:00
|
|
|
|
2022-09-02 18:46:18 +02:00
|
|
|
impl ApplicationDelegate {
|
|
|
|
|
pub(super) fn new(
|
2023-12-23 23:07:55 +01:00
|
|
|
mtm: MainThreadMarker,
|
2022-09-02 18:46:18 +02:00
|
|
|
activation_policy: NSApplicationActivationPolicy,
|
|
|
|
|
default_menu: bool,
|
2022-11-23 14:42:46 +02:00
|
|
|
activate_ignoring_other_apps: bool,
|
2023-07-29 00:33:03 +02:00
|
|
|
) -> Id<Self> {
|
2023-12-23 23:07:55 +01:00
|
|
|
let this = mtm.alloc().set_ivars(State {
|
2024-04-18 17:34:19 +02:00
|
|
|
activation_policy: Policy(activation_policy),
|
2023-12-23 18:04:24 +01:00
|
|
|
default_menu,
|
|
|
|
|
activate_ignoring_other_apps,
|
2024-01-14 03:37:53 +01:00
|
|
|
..Default::default()
|
2023-12-23 18:04:24 +01:00
|
|
|
});
|
|
|
|
|
unsafe { msg_send_id![super(this), init] }
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
pub fn get(mtm: MainThreadMarker) -> Id<Self> {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
let delegate =
|
|
|
|
|
unsafe { app.delegate() }.expect("a delegate was not configured on the application");
|
|
|
|
|
if delegate.is_kind_of::<Self>() {
|
|
|
|
|
// SAFETY: Just checked that the delegate is an instance of `ApplicationDelegate`
|
|
|
|
|
unsafe { Id::cast(delegate) }
|
|
|
|
|
} else {
|
|
|
|
|
panic!("tried to get a delegate that was not the one Winit has registered")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 04:33:47 +01:00
|
|
|
/// Place the event handler in the application delegate for the duration
|
|
|
|
|
/// of the given closure.
|
|
|
|
|
pub fn set_event_handler<R>(
|
2024-01-14 03:37:53 +01:00
|
|
|
&self,
|
2024-02-28 04:33:47 +01:00
|
|
|
handler: impl FnMut(Event<HandlePendingUserEvents>, &RootActiveEventLoop),
|
|
|
|
|
closure: impl FnOnce() -> R,
|
|
|
|
|
) -> R {
|
|
|
|
|
self.ivars().event_handler.set(handler, closure)
|
2024-01-14 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// If `pump_events` is called to progress the event loop then we
|
2024-02-19 11:58:44 +07:00
|
|
|
/// bootstrap the event loop via `-[NSApplication run]` but will use
|
2024-01-14 03:37:53 +01:00
|
|
|
/// `CFRunLoopRunInMode` for subsequent calls to `pump_events`.
|
|
|
|
|
pub fn set_stop_on_launch(&self) {
|
|
|
|
|
self.ivars().stop_on_launch.set(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_stop_before_wait(&self, value: bool) {
|
|
|
|
|
self.ivars().stop_before_wait.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_stop_after_wait(&self, value: bool) {
|
|
|
|
|
self.ivars().stop_after_wait.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_stop_on_redraw(&self, value: bool) {
|
|
|
|
|
self.ivars().stop_on_redraw.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_wait_timeout(&self, value: Option<Instant>) {
|
|
|
|
|
self.ivars().wait_timeout.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Clears the `running` state and resets the `control_flow` state when an `EventLoop` exits.
|
|
|
|
|
///
|
2024-05-06 16:29:07 +02:00
|
|
|
/// NOTE: that if the `NSApplication` has been launched then that state is preserved,
|
2024-01-14 03:37:53 +01:00
|
|
|
/// and we won't need to re-launch the app if subsequent EventLoops are run.
|
|
|
|
|
pub fn internal_exit(&self) {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::LoopExiting);
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
self.set_is_running(false);
|
|
|
|
|
self.set_stop_on_redraw(false);
|
|
|
|
|
self.set_stop_before_wait(false);
|
|
|
|
|
self.set_stop_after_wait(false);
|
|
|
|
|
self.set_wait_timeout(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_launched(&self) -> bool {
|
|
|
|
|
self.ivars().is_launched.get()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_is_running(&self, value: bool) {
|
|
|
|
|
self.ivars().is_running.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn is_running(&self) -> bool {
|
|
|
|
|
self.ivars().is_running.get()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn exit(&self) {
|
|
|
|
|
self.ivars().exit.set(true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn clear_exit(&self) {
|
|
|
|
|
self.ivars().exit.set(false)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn exiting(&self) -> bool {
|
|
|
|
|
self.ivars().exit.get()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_control_flow(&self, value: ControlFlow) {
|
|
|
|
|
self.ivars().control_flow.set(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn control_flow(&self) -> ControlFlow {
|
|
|
|
|
self.ivars().control_flow.get()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn queue_window_event(&self, window_id: WindowId, event: WindowEvent) {
|
|
|
|
|
self.ivars()
|
|
|
|
|
.pending_events
|
|
|
|
|
.borrow_mut()
|
|
|
|
|
.push_back(QueuedEvent::WindowEvent(window_id, event));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn queue_device_event(&self, event: DeviceEvent) {
|
|
|
|
|
self.ivars().pending_events.borrow_mut().push_back(QueuedEvent::DeviceEvent(event));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn queue_static_scale_factor_changed_event(
|
|
|
|
|
&self,
|
|
|
|
|
window: Id<WinitWindow>,
|
|
|
|
|
suggested_size: PhysicalSize<u32>,
|
|
|
|
|
scale_factor: f64,
|
|
|
|
|
) {
|
|
|
|
|
self.ivars().pending_events.borrow_mut().push_back(QueuedEvent::ScaleFactorChanged {
|
|
|
|
|
window,
|
|
|
|
|
suggested_size,
|
|
|
|
|
scale_factor,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn handle_redraw(&self, window_id: WindowId) {
|
|
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
// Redraw request might come out of order from the OS.
|
2024-02-28 04:33:47 +01:00
|
|
|
// -> Don't go back into the event handler when our callstack originates from there
|
|
|
|
|
if !self.ivars().event_handler.in_use() {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::WindowEvent {
|
2024-01-14 03:37:53 +01:00
|
|
|
window_id: RootWindowId(window_id),
|
|
|
|
|
event: WindowEvent::RedrawRequested,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// `pump_events` will request to stop immediately _after_ dispatching RedrawRequested
|
|
|
|
|
// events as a way to ensure that `pump_events` can't block an external loop
|
|
|
|
|
// indefinitely
|
|
|
|
|
if self.ivars().stop_on_redraw.get() {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
stop_app_immediately(&app);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn queue_redraw(&self, window_id: WindowId) {
|
|
|
|
|
let mut pending_redraw = self.ivars().pending_redraw.borrow_mut();
|
|
|
|
|
if !pending_redraw.contains(&window_id) {
|
|
|
|
|
pending_redraw.push(window_id);
|
|
|
|
|
}
|
|
|
|
|
unsafe { RunLoop::get() }.wakeup();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 05:26:50 +01:00
|
|
|
fn handle_event(&self, event: Event<HandlePendingUserEvents>) {
|
2024-02-28 04:33:47 +01:00
|
|
|
self.ivars().event_handler.handle_event(event, &ActiveEventLoop::new_root(self.retain()))
|
2024-01-14 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// dispatch `NewEvents(Init)` + `Resumed`
|
|
|
|
|
pub fn dispatch_init_events(&self) {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::NewEvents(StartCause::Init));
|
2024-01-14 03:37:53 +01:00
|
|
|
// NB: For consistency all platforms must emit a 'resumed' event even though macOS
|
|
|
|
|
// applications don't themselves have a formal suspend/resume lifecycle.
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::Resumed);
|
2024-01-14 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called by RunLoopObserver after finishing waiting for new events
|
|
|
|
|
pub fn wakeup(&self, panic_info: Weak<PanicInfo>) {
|
|
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
let panic_info = panic_info
|
|
|
|
|
.upgrade()
|
|
|
|
|
.expect("The panic info must exist here. This failure indicates a developer error.");
|
|
|
|
|
|
2024-02-28 04:33:47 +01:00
|
|
|
// Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779
|
|
|
|
|
if panic_info.is_panicking() || !self.ivars().event_handler.ready() || !self.is_running() {
|
2024-01-14 03:37:53 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.ivars().stop_after_wait.get() {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
stop_app_immediately(&app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let start = self.ivars().start_time.get().unwrap();
|
|
|
|
|
let cause = match self.control_flow() {
|
|
|
|
|
ControlFlow::Poll => StartCause::Poll,
|
|
|
|
|
ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None },
|
|
|
|
|
ControlFlow::WaitUntil(requested_resume) => {
|
|
|
|
|
if Instant::now() >= requested_resume {
|
|
|
|
|
StartCause::ResumeTimeReached { start, requested_resume }
|
|
|
|
|
} else {
|
|
|
|
|
StartCause::WaitCancelled { start, requested_resume: Some(requested_resume) }
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::NewEvents(cause));
|
2024-01-14 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Called by RunLoopObserver before waiting for new events
|
|
|
|
|
pub fn cleared(&self, panic_info: Weak<PanicInfo>) {
|
|
|
|
|
let mtm = MainThreadMarker::from(self);
|
|
|
|
|
let panic_info = panic_info
|
|
|
|
|
.upgrade()
|
|
|
|
|
.expect("The panic info must exist here. This failure indicates a developer error.");
|
|
|
|
|
|
2024-02-28 04:33:47 +01:00
|
|
|
// Return when in event handler due to https://github.com/rust-windowing/winit/issues/1779
|
|
|
|
|
// XXX: how does it make sense that `event_handler.ready()` can ever return `false` here if
|
|
|
|
|
// we're about to return to the `CFRunLoop` to poll for new events?
|
|
|
|
|
if panic_info.is_panicking() || !self.ivars().event_handler.ready() || !self.is_running() {
|
2024-01-14 03:37:53 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::UserEvent(HandlePendingUserEvents));
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
let events = mem::take(&mut *self.ivars().pending_events.borrow_mut());
|
|
|
|
|
for event in events {
|
|
|
|
|
match event {
|
|
|
|
|
QueuedEvent::WindowEvent(window_id, event) => {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::WindowEvent {
|
2024-01-14 03:37:53 +01:00
|
|
|
window_id: RootWindowId(window_id),
|
|
|
|
|
event,
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
QueuedEvent::DeviceEvent(event) => {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::DeviceEvent { device_id: DEVICE_ID, event });
|
2024-01-14 03:37:53 +01:00
|
|
|
},
|
|
|
|
|
QueuedEvent::ScaleFactorChanged { window, suggested_size, scale_factor } => {
|
2024-02-28 04:33:47 +01:00
|
|
|
let new_inner_size = Arc::new(Mutex::new(suggested_size));
|
|
|
|
|
let scale_factor_changed_event = Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(window.id()),
|
|
|
|
|
event: WindowEvent::ScaleFactorChanged {
|
|
|
|
|
scale_factor,
|
|
|
|
|
inner_size_writer: InnerSizeWriter::new(Arc::downgrade(
|
|
|
|
|
&new_inner_size,
|
|
|
|
|
)),
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
self.handle_event(scale_factor_changed_event);
|
|
|
|
|
|
|
|
|
|
let physical_size = *new_inner_size.lock().unwrap();
|
|
|
|
|
drop(new_inner_size);
|
2024-05-24 00:40:07 +08:00
|
|
|
if physical_size != suggested_size {
|
|
|
|
|
let logical_size = physical_size.to_logical(scale_factor);
|
|
|
|
|
let size = NSSize::new(logical_size.width, logical_size.height);
|
|
|
|
|
window.setContentSize(size);
|
|
|
|
|
}
|
2024-02-28 04:33:47 +01:00
|
|
|
|
|
|
|
|
let resized_event = Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(window.id()),
|
|
|
|
|
event: WindowEvent::Resized(physical_size),
|
|
|
|
|
};
|
|
|
|
|
self.handle_event(resized_event);
|
2024-01-14 03:37:53 +01:00
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let redraw = mem::take(&mut *self.ivars().pending_redraw.borrow_mut());
|
|
|
|
|
for window_id in redraw {
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::WindowEvent {
|
2024-01-14 03:37:53 +01:00
|
|
|
window_id: RootWindowId(window_id),
|
|
|
|
|
event: WindowEvent::RedrawRequested,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 05:26:50 +01:00
|
|
|
self.handle_event(Event::AboutToWait);
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
if self.exiting() {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
stop_app_immediately(&app);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if self.ivars().stop_before_wait.get() {
|
|
|
|
|
let app = NSApplication::sharedApplication(mtm);
|
|
|
|
|
stop_app_immediately(&app);
|
|
|
|
|
}
|
|
|
|
|
self.ivars().start_time.set(Some(Instant::now()));
|
|
|
|
|
let wait_timeout = self.ivars().wait_timeout.get(); // configured by pump_events
|
|
|
|
|
let app_timeout = match self.control_flow() {
|
|
|
|
|
ControlFlow::Wait => None,
|
|
|
|
|
ControlFlow::Poll => Some(Instant::now()),
|
|
|
|
|
ControlFlow::WaitUntil(instant) => Some(instant),
|
|
|
|
|
};
|
|
|
|
|
self.ivars().waker.borrow_mut().start_at(min_timeout(wait_timeout, app_timeout));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) enum QueuedEvent {
|
|
|
|
|
WindowEvent(WindowId, WindowEvent),
|
|
|
|
|
DeviceEvent(DeviceEvent),
|
|
|
|
|
ScaleFactorChanged {
|
|
|
|
|
window: Id<WinitWindow>,
|
|
|
|
|
suggested_size: PhysicalSize<u32>,
|
|
|
|
|
scale_factor: f64,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-25 05:26:50 +01:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub(crate) struct HandlePendingUserEvents;
|
2024-01-14 03:37:53 +01:00
|
|
|
|
|
|
|
|
/// Returns the minimum `Option<Instant>`, taking into account that `None`
|
|
|
|
|
/// equates to an infinite timeout, not a zero timeout (so can't just use
|
|
|
|
|
/// `Option::min`)
|
|
|
|
|
fn min_timeout(a: Option<Instant>, b: Option<Instant>) -> Option<Instant> {
|
|
|
|
|
a.map_or(b, |a_timeout| b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout))))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// A hack to make activation of multiple windows work when creating them before
|
|
|
|
|
/// `applicationDidFinishLaunching:` / `Event::Event::NewEvents(StartCause::Init)`.
|
|
|
|
|
///
|
|
|
|
|
/// Alternative to this would be the user calling `window.set_visible(true)` in
|
|
|
|
|
/// `StartCause::Init`.
|
|
|
|
|
///
|
|
|
|
|
/// If this becomes too bothersome to maintain, it can probably be removed
|
|
|
|
|
/// without too much damage.
|
|
|
|
|
fn window_activation_hack(app: &NSApplication) {
|
|
|
|
|
// TODO: Proper ordering of the windows
|
|
|
|
|
app.windows().into_iter().for_each(|window| {
|
|
|
|
|
// Call `makeKeyAndOrderFront` if it was called on the window in `WinitWindow::new`
|
2024-02-19 11:58:44 +07:00
|
|
|
// This way we preserve the user's desired initial visibility status
|
2024-01-14 03:37:53 +01:00
|
|
|
// TODO: Also filter on the type/"level" of the window, and maybe other things?
|
|
|
|
|
if window.isVisible() {
|
2024-02-25 19:20:39 -08:00
|
|
|
tracing::trace!("Activating visible window");
|
2024-01-14 03:37:53 +01:00
|
|
|
window.makeKeyAndOrderFront(None);
|
|
|
|
|
} else {
|
2024-02-25 19:20:39 -08:00
|
|
|
tracing::trace!("Skipping activating invisible window");
|
2024-01-14 03:37:53 +01:00
|
|
|
}
|
|
|
|
|
})
|
2021-04-30 11:31:28 +02:00
|
|
|
}
|