From c0b737de4af2e1d3cc6d3086e8264482233450b1 Mon Sep 17 00:00:00 2001 From: Kirill Chibisov Date: Sat, 3 May 2025 20:27:48 +0900 Subject: [PATCH] winit-core: move application --- examples/application.rs | 7 +-- src/lib.rs | 3 +- src/platform/macos.rs | 54 ++----------------- winit-core/src/application/macos.rs | 52 ++++++++++++++++++ .../src/application/mod.rs | 18 +++---- winit-core/src/event.rs | 1 + winit-core/src/lib.rs | 1 + 7 files changed, 68 insertions(+), 68 deletions(-) create mode 100644 winit-core/src/application/macos.rs rename src/application.rs => winit-core/src/application/mod.rs (97%) diff --git a/examples/application.rs b/examples/application.rs index aee0dc09..aa869384 100644 --- a/examples/application.rs +++ b/examples/application.rs @@ -25,9 +25,7 @@ use winit::icon::{Icon, RgbaIcon}; use winit::keyboard::{Key, ModifiersState}; use winit::monitor::Fullscreen; #[cfg(macos_platform)] -use winit::platform::macos::{ - ApplicationHandlerExtMacOS, OptionAsAlt, WindowAttributesMacOS, WindowExtMacOS, -}; +use winit::platform::macos::{OptionAsAlt, WindowAttributesMacOS, WindowExtMacOS}; #[cfg(any(x11_platform, wayland_platform))] use winit::platform::startup_notify::{self, EventLoopExtStartupNotify, WindowExtStartupNotify}; #[cfg(wayland_platform)] @@ -37,6 +35,7 @@ use winit::platform::web::{ActiveEventLoopExtWeb, WindowAttributesWeb}; #[cfg(x11_platform)] use winit::platform::x11::{ActiveEventLoopExtX11, WindowAttributesX11}; use winit::window::{CursorGrabMode, ResizeDirection, Theme, Window, WindowAttributes, WindowId}; +use winit_core::application::macos::ApplicationHandlerExtMacOS; #[path = "util/tracing.rs"] mod tracing; @@ -577,7 +576,6 @@ impl ApplicationHandler for Application { } } - #[cfg(target_os = "macos")] fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> { Some(self) } @@ -589,7 +587,6 @@ impl Drop for Application { } } -#[cfg(target_os = "macos")] impl ApplicationHandlerExtMacOS for Application { fn standard_key_binding( &mut self, diff --git a/src/lib.rs b/src/lib.rs index 01c134e9..bda19f10 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -296,11 +296,10 @@ pub use dpi; pub use rwh_06 as raw_window_handle; -pub mod application; #[cfg(any(doc, doctest, test))] pub mod changelog; pub mod event_loop; -pub use winit_core::{cursor, error, event, icon, keyboard, monitor, window}; +pub use winit_core::{application, cursor, error, event, icon, keyboard, monitor, window}; #[macro_use] mod os_error; mod platform_impl; diff --git a/src/platform/macos.rs b/src/platform/macos.rs index d5e2262c..dcfa7e8b 100644 --- a/src/platform/macos.rs +++ b/src/platform/macos.rs @@ -69,13 +69,14 @@ use std::os::raw::c_void; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize}; +#[doc(inline)] +pub use winit_core::application::macos::ApplicationHandlerExtMacOS; use winit_core::window::PlatformWindowAttributes; -use crate::application::ApplicationHandler; use crate::event_loop::{ActiveEventLoop, EventLoopBuilder}; use crate::monitor::MonitorHandle; use crate::platform_impl::MonitorHandle as MacOsMonitorHandle; -use crate::window::{Window, WindowId}; +use crate::window::Window; /// Additional methods on [`Window`] that are specific to MacOS. pub trait WindowExtMacOS { @@ -610,52 +611,3 @@ pub enum OptionAsAlt { #[default] None, } - -/// Additional events on [`ApplicationHandler`] that are specific to macOS. -/// -/// This can be registered with [`ApplicationHandler::macos_handler`]. -pub trait ApplicationHandlerExtMacOS: ApplicationHandler { - /// The system interpreted a keypress as a standard key binding command. - /// - /// Examples include inserting tabs and newlines, or moving the insertion point, see - /// [`NSStandardKeyBindingResponding`] for the full list of key bindings. They are often text - /// editing related. - /// - /// This corresponds to the [`doCommandBySelector:`] method on `NSTextInputClient`. - /// - /// The `action` parameter contains the string representation of the selector. Examples include - /// `"insertBacktab:"`, `"indent:"` and `"noop:"`. - /// - /// # Example - /// - /// ```ignore - /// impl ApplicationHandlerExtMacOS for App { - /// fn standard_key_binding( - /// &mut self, - /// event_loop: &dyn ActiveEventLoop, - /// window_id: WindowId, - /// action: &str, - /// ) { - /// match action { - /// "moveBackward:" => self.cursor.position -= 1, - /// "moveForward:" => self.cursor.position += 1, - /// _ => {} // Ignore other actions - /// } - /// } - /// } - /// ``` - /// - /// [`NSStandardKeyBindingResponding`]: https://developer.apple.com/documentation/appkit/nsstandardkeybindingresponding?language=objc - /// [`doCommandBySelector:`]: https://developer.apple.com/documentation/appkit/nstextinputclient/1438256-docommandbyselector?language=objc - #[doc(alias = "doCommandBySelector:")] - fn standard_key_binding( - &mut self, - event_loop: &dyn ActiveEventLoop, - window_id: WindowId, - action: &str, - ) { - let _ = event_loop; - let _ = window_id; - let _ = action; - } -} diff --git a/winit-core/src/application/macos.rs b/winit-core/src/application/macos.rs new file mode 100644 index 00000000..d18b4090 --- /dev/null +++ b/winit-core/src/application/macos.rs @@ -0,0 +1,52 @@ +use crate::application::ApplicationHandler; +use crate::event_loop::ActiveEventLoop; +use crate::window::WindowId; + +/// Additional events on [`ApplicationHandler`] that are specific to macOS. +/// +/// This can be registered with [`ApplicationHandler::macos_handler`]. +pub trait ApplicationHandlerExtMacOS: ApplicationHandler { + /// The system interpreted a keypress as a standard key binding command. + /// + /// Examples include inserting tabs and newlines, or moving the insertion point, see + /// [`NSStandardKeyBindingResponding`] for the full list of key bindings. They are often text + /// editing related. + /// + /// This corresponds to the [`doCommandBySelector:`] method on `NSTextInputClient`. + /// + /// The `action` parameter contains the string representation of the selector. Examples include + /// `"insertBacktab:"`, `"indent:"` and `"noop:"`. + /// + /// # Example + /// + /// ```ignore + /// impl ApplicationHandlerExtMacOS for App { + /// fn standard_key_binding( + /// &mut self, + /// event_loop: &dyn ActiveEventLoop, + /// window_id: WindowId, + /// action: &str, + /// ) { + /// match action { + /// "moveBackward:" => self.cursor.position -= 1, + /// "moveForward:" => self.cursor.position += 1, + /// _ => {} // Ignore other actions + /// } + /// } + /// } + /// ``` + /// + /// [`NSStandardKeyBindingResponding`]: https://developer.apple.com/documentation/appkit/nsstandardkeybindingresponding?language=objc + /// [`doCommandBySelector:`]: https://developer.apple.com/documentation/appkit/nstextinputclient/1438256-docommandbyselector?language=objc + #[doc(alias = "doCommandBySelector:")] + fn standard_key_binding( + &mut self, + event_loop: &dyn ActiveEventLoop, + window_id: WindowId, + action: &str, + ) { + let _ = event_loop; + let _ = window_id; + let _ = action; + } +} diff --git a/src/application.rs b/winit-core/src/application/mod.rs similarity index 97% rename from src/application.rs rename to winit-core/src/application/mod.rs index 0020ca99..b579cb40 100644 --- a/src/application.rs +++ b/winit-core/src/application/mod.rs @@ -2,10 +2,10 @@ use crate::event::{DeviceEvent, DeviceId, StartCause, WindowEvent}; use crate::event_loop::ActiveEventLoop; -#[cfg(macos_platform)] -use crate::platform::macos::ApplicationHandlerExtMacOS; use crate::window::WindowId; +pub mod macos; + /// The handler of application-level events. /// /// See [the top-level docs] for example usage, and [`EventLoop::run_app`] for an overview of when @@ -135,8 +135,9 @@ pub trait ApplicationHandler { /// use std::thread; /// use std::time::Duration; /// - /// use winit::application::ApplicationHandler; - /// use winit::event_loop::{ActiveEventLoop, EventLoop}; + /// use winit::event_loop::EventLoop; + /// use winit_core::application::ApplicationHandler; + /// use winit_core::event_loop::ActiveEventLoop; /// /// struct MyApp { /// receiver: mpsc::Receiver, @@ -350,9 +351,8 @@ pub trait ApplicationHandler { /// The macOS-specific handler. /// /// The return value from this should not change at runtime. - #[cfg(macos_platform)] #[inline(always)] - fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> { + fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> { None } } @@ -419,9 +419,8 @@ impl ApplicationHandler for &mut A { (**self).memory_warning(event_loop); } - #[cfg(macos_platform)] #[inline] - fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> { + fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> { (**self).macos_handler() } } @@ -488,9 +487,8 @@ impl ApplicationHandler for Box { (**self).memory_warning(event_loop); } - #[cfg(macos_platform)] #[inline] - fn macos_handler(&mut self) -> Option<&mut dyn ApplicationHandlerExtMacOS> { + fn macos_handler(&mut self) -> Option<&mut dyn macos::ApplicationHandlerExtMacOS> { (**self).macos_handler() } } diff --git a/winit-core/src/event.rs b/winit-core/src/event.rs index 46ca6a53..b4435d7a 100644 --- a/winit-core/src/event.rs +++ b/winit-core/src/event.rs @@ -1133,6 +1133,7 @@ mod tests { use std::collections::{BTreeSet, HashSet}; use dpi::PhysicalPosition; + use crate::event; macro_rules! foreach_event { diff --git a/winit-core/src/lib.rs b/winit-core/src/lib.rs index b8a08af9..b9305a9a 100644 --- a/winit-core/src/lib.rs +++ b/winit-core/src/lib.rs @@ -3,6 +3,7 @@ pub mod as_any; pub mod cursor; #[macro_use] pub mod error; +pub mod application; pub mod event; pub mod event_loop; pub mod icon;