Skip to content

Commit 9d5412f

Browse files
authored
Move iOS and macOS implementations into new apple module (#3756)
Move iOS and macOS implementations to a shared folder called `apple`, to allow us to reduce the code-duplication between these platforms in the future. The folder structure is now: - `src/platform_impl/apple/` - `appkit/` - `uikit/` - `example_shared_file.rs` - `mod.rs` * Add preliminary support for tvOS, watchOS and visionOS * Reduce duplication in Cargo.toml when specifying dependencies
1 parent ecb887e commit 9d5412f

28 files changed

+52
-59
lines changed

.github/CODEOWNERS

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
/src/platform/android.rs @MarijnS95
33
/src/platform_impl/android @MarijnS95
44

5-
# iOS
5+
# Apple (AppKit + UIKit)
66
/src/platform/ios.rs @madsmtm
7-
/src/platform_impl/ios @madsmtm
7+
/src/platform/macos.rs @madsmtm
8+
/src/platform_impl/apple @madsmtm
89

910
# Unix
1011
/src/platform_impl/linux/mod.rs @kchibisov
@@ -17,10 +18,6 @@
1718
/src/platform/x11.rs @kchibisov @notgull
1819
/src/platform_impl/linux/x11 @kchibisov @notgull
1920

20-
# macOS
21-
/src/platform/macos.rs @madsmtm
22-
/src/platform_impl/macos @madsmtm
23-
2421
# Web
2522
/src/platform/web.rs @daxpedda
2623
/src/platform_impl/web @daxpedda

Cargo.toml

+24-33
Original file line numberDiff line numberDiff line change
@@ -102,21 +102,21 @@ softbuffer = { version = "0.4.0", default-features = false, features = [
102102
"wayland-dlopen",
103103
] }
104104

105+
# Android
105106
[target.'cfg(target_os = "android")'.dependencies]
106107
android-activity = "0.6.0"
107108
ndk = { version = "0.9.0", default-features = false }
108109

109-
[target.'cfg(any(target_os = "ios", target_os = "macos"))'.dependencies]
110+
# AppKit or UIKit
111+
[target.'cfg(target_vendor = "apple")'.dependencies]
110112
core-foundation = "0.9.3"
111113
objc2 = "0.5.2"
112114

115+
# AppKit
113116
[target.'cfg(target_os = "macos")'.dependencies]
114117
core-graphics = "0.23.1"
115118
block2 = "0.5.1"
116-
117-
[target.'cfg(target_os = "macos")'.dependencies.objc2-foundation]
118-
version = "0.2.2"
119-
features = [
119+
objc2-foundation = { version = "0.2.2", features = [
120120
"block2",
121121
"dispatch",
122122
"NSArray",
@@ -134,11 +134,8 @@ features = [
134134
"NSString",
135135
"NSThread",
136136
"NSValue",
137-
]
138-
139-
[target.'cfg(target_os = "macos")'.dependencies.objc2-app-kit]
140-
version = "0.2.2"
141-
features = [
137+
] }
138+
objc2-app-kit = { version = "0.2.2", features = [
142139
"NSAppearance",
143140
"NSApplication",
144141
"NSBitmapImageRep",
@@ -165,11 +162,11 @@ features = [
165162
"NSWindow",
166163
"NSWindowScripting",
167164
"NSWindowTabGroup",
168-
]
165+
] }
169166

170-
[target.'cfg(target_os = "ios")'.dependencies.objc2-foundation]
171-
version = "0.2.2"
172-
features = [
167+
# UIKit
168+
[target.'cfg(all(target_vendor = "apple", not(target_os = "macos")))'.dependencies]
169+
objc2-foundation = { version = "0.2.2", features = [
173170
"dispatch",
174171
"NSArray",
175172
"NSEnumerator",
@@ -179,11 +176,8 @@ features = [
179176
"NSProcessInfo",
180177
"NSThread",
181178
"NSSet",
182-
]
183-
184-
[target.'cfg(target_os = "ios")'.dependencies.objc2-ui-kit]
185-
version = "0.2.2"
186-
features = [
179+
] }
180+
objc2-ui-kit = { version = "0.2.2", features = [
187181
"UIApplication",
188182
"UIDevice",
189183
"UIEvent",
@@ -202,14 +196,12 @@ features = [
202196
"UIView",
203197
"UIViewController",
204198
"UIWindow",
205-
]
199+
] }
206200

201+
# Windows
207202
[target.'cfg(target_os = "windows")'.dependencies]
208203
unicode-segmentation = "1.7.1"
209-
210-
[target.'cfg(target_os = "windows")'.dependencies.windows-sys]
211-
version = "0.52.0"
212-
features = [
204+
windows-sys = { version = "0.52.0", features = [
213205
"Win32_Devices_HumanInterfaceDevice",
214206
"Win32_Foundation",
215207
"Win32_Globalization",
@@ -234,9 +226,10 @@ features = [
234226
"Win32_UI_Shell",
235227
"Win32_UI_TextServices",
236228
"Win32_UI_WindowsAndMessaging",
237-
]
229+
] }
238230

239-
[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_os = "ios", target_os = "macos"))))'.dependencies]
231+
# Linux
232+
[target.'cfg(all(unix, not(any(target_os = "redox", target_family = "wasm", target_os = "android", target_vendor = "apple"))))'.dependencies]
240233
ahash = { version = "0.8.7", features = ["no-rng"], optional = true }
241234
bytemuck = { version = "1.13.1", default-features = false, optional = true }
242235
calloop = "0.12.3"
@@ -274,14 +267,14 @@ x11rb = { version = "0.13.0", default-features = false, features = [
274267
], optional = true }
275268
xkbcommon-dl = "0.4.2"
276269

270+
# Orbital
277271
[target.'cfg(target_os = "redox")'.dependencies]
278272
orbclient = { version = "0.3.47", default-features = false }
279273
redox_syscall = "0.4.1"
280274

281-
[target.'cfg(target_family = "wasm")'.dependencies.web_sys]
282-
package = "web-sys"
283-
version = "0.3.64"
284-
features = [
275+
# Web
276+
[target.'cfg(target_family = "wasm")'.dependencies]
277+
web_sys = { package = "web-sys", version = "0.3.64", features = [
285278
'AbortController',
286279
'AbortSignal',
287280
'Blob',
@@ -323,9 +316,7 @@ features = [
323316
'WheelEvent',
324317
'Worker',
325318
'Url',
326-
]
327-
328-
[target.'cfg(target_family = "wasm")'.dependencies]
319+
] }
329320
js-sys = "0.3.64"
330321
pin-project = "1"
331322
wasm-bindgen = "0.2"

build.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,9 @@ fn main() {
1010
android_platform: { target_os = "android" },
1111
web_platform: { all(target_family = "wasm", target_os = "unknown") },
1212
macos_platform: { target_os = "macos" },
13-
ios_platform: { target_os = "ios" },
13+
ios_platform: { all(target_vendor = "apple", not(target_os = "macos")) },
1414
windows_platform: { target_os = "windows" },
15-
apple: { any(target_os = "ios", target_os = "macos") },
16-
free_unix: { all(unix, not(apple), not(android_platform), not(target_os = "emscripten")) },
15+
free_unix: { all(unix, not(target_vendor = "apple"), not(android_platform), not(target_os = "emscripten")) },
1716
redox: { target_os = "redox" },
1817

1918
// Native displays.
File renamed without changes.
File renamed without changes.

src/platform_impl/macos/event.rs src/platform_impl/apple/appkit/event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ use objc2_app_kit::{NSEvent, NSEventModifierFlags, NSEventSubtype, NSEventType};
77
use objc2_foundation::{run_on_main, NSPoint};
88
use smol_str::SmolStr;
99

10+
use super::ffi;
1011
use crate::event::{ElementState, KeyEvent, Modifiers};
1112
use crate::keyboard::{
1213
Key, KeyCode, KeyLocation, ModifiersKeys, ModifiersState, NamedKey, NativeKey, NativeKeyCode,
1314
PhysicalKey,
1415
};
15-
use crate::platform_impl::platform::ffi;
1616

1717
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
1818
pub struct KeyEventExtra {

src/platform_impl/macos/event_loop.rs src/platform_impl/apple/appkit/event_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use objc2_foundation::{MainThreadMarker, NSObjectProtocol};
2323

2424
use super::app::WinitApplication;
2525
use super::app_state::ApplicationDelegate;
26+
use super::cursor::CustomCursor;
2627
use super::event::dummy_event;
2728
use super::monitor::{self, MonitorHandle};
2829
use super::observer::setup_control_flow_observers;
@@ -31,7 +32,6 @@ use crate::error::EventLoopError;
3132
use crate::event_loop::{ActiveEventLoop as RootWindowTarget, ControlFlow, DeviceEvents};
3233
use crate::platform::macos::ActivationPolicy;
3334
use crate::platform::pump_events::PumpStatus;
34-
use crate::platform_impl::platform::cursor::CustomCursor;
3535
use crate::window::{CustomCursor as RootCustomCursor, CustomCursorSource};
3636

3737
#[derive(Default)]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/platform_impl/macos/util.rs src/platform_impl/apple/appkit/util.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use tracing::trace;
22

33
macro_rules! trace_scope {
44
($s:literal) => {
5-
let _crate = $crate::platform_impl::platform::util::TraceGuard::new(module_path!(), $s);
5+
let _crate =
6+
$crate::platform_impl::platform::appkit::util::TraceGuard::new(module_path!(), $s);
67
};
78
}
89

File renamed without changes.
File renamed without changes.

src/platform_impl/apple/mod.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//! Apple/Darwin-specific implementations
2+
3+
#[cfg(target_os = "macos")]
4+
mod appkit;
5+
#[cfg(not(target_os = "macos"))]
6+
mod uikit;
7+
8+
#[cfg(target_os = "macos")]
9+
pub use self::appkit::*;
10+
#[cfg(not(target_os = "macos"))]
11+
pub use self::uikit::*;
File renamed without changes.

src/platform_impl/ios/event_loop.rs src/platform_impl/apple/uikit/event_loop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ use objc2::{msg_send_id, ClassType};
1717
use objc2_foundation::{MainThreadMarker, NSString};
1818
use objc2_ui_kit::{UIApplication, UIApplicationMain, UIDevice, UIScreen, UIUserInterfaceIdiom};
1919

20+
use super::app_state::EventLoopHandler;
2021
use crate::application::ApplicationHandler;
2122
use crate::error::EventLoopError;
2223
use crate::event::Event;
2324
use crate::event_loop::{ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents};
2425
use crate::platform::ios::Idiom;
25-
use crate::platform_impl::platform::app_state::EventLoopHandler;
2626
use crate::window::{CustomCursor, CustomCursorSource};
2727

2828
use super::app_delegate::AppDelegate;
File renamed without changes.

src/platform_impl/ios/monitor.rs src/platform_impl/apple/uikit/monitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use objc2::Message;
99
use objc2_foundation::{run_on_main, MainThreadBound, MainThreadMarker, NSInteger};
1010
use objc2_ui_kit::{UIScreen, UIScreenMode};
1111

12+
use super::app_state;
1213
use crate::dpi::{PhysicalPosition, PhysicalSize};
1314
use crate::monitor::VideoModeHandle as RootVideoModeHandle;
14-
use crate::platform_impl::platform::app_state;
1515

1616
// Workaround for `MainThreadBound` implementing almost no traits
1717
#[derive(Debug)]

src/platform_impl/ios/view.rs src/platform_impl/apple/uikit/view.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ use objc2_ui_kit::{
1414

1515
use super::app_state::{self, EventWrapper};
1616
use super::window::WinitUIWindow;
17+
use super::DEVICE_ID;
1718
use crate::dpi::PhysicalPosition;
1819
use crate::event::{Event, Force, Touch, TouchPhase, WindowEvent};
19-
use crate::platform_impl::platform::DEVICE_ID;
2020
use crate::window::{WindowAttributes, WindowId as RootWindowId};
2121

2222
pub struct WinitViewState {

src/platform_impl/ios/window.rs src/platform_impl/apple/uikit/window.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@ use tracing::{debug, warn};
1717
use super::app_state::EventWrapper;
1818
use super::view::WinitView;
1919
use super::view_controller::WinitViewController;
20+
use super::{app_state, monitor, ActiveEventLoop, Fullscreen, MonitorHandle};
2021
use crate::cursor::Cursor;
2122
use crate::dpi::{LogicalPosition, LogicalSize, PhysicalPosition, PhysicalSize, Position, Size};
2223
use crate::error::{ExternalError, NotSupportedError, OsError as RootOsError};
2324
use crate::event::{Event, WindowEvent};
2425
use crate::icon::Icon;
2526
use crate::platform::ios::{ScreenEdge, StatusBarStyle, ValidOrientations};
26-
use crate::platform_impl::platform::{
27-
app_state, monitor, ActiveEventLoop, Fullscreen, MonitorHandle,
28-
};
2927
use crate::window::{
3028
CursorGrabMode, ImePurpose, ResizeDirection, Theme, UserAttentionType, WindowAttributes,
3129
WindowButtons, WindowId as RootWindowId, WindowLevel,

src/platform_impl/mod.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ use crate::window::Fullscreen as RootFullscreen;
33

44
#[cfg(android_platform)]
55
mod android;
6-
#[cfg(ios_platform)]
7-
mod ios;
6+
#[cfg(target_vendor = "apple")]
7+
mod apple;
88
#[cfg(any(x11_platform, wayland_platform))]
99
mod linux;
10-
#[cfg(macos_platform)]
11-
mod macos;
1210
#[cfg(orbital_platform)]
1311
mod orbital;
1412
#[cfg(web_platform)]
@@ -18,12 +16,10 @@ mod windows;
1816

1917
#[cfg(android_platform)]
2018
use android as platform;
21-
#[cfg(ios_platform)]
22-
use ios as platform;
19+
#[cfg(target_vendor = "apple")]
20+
use apple as platform;
2321
#[cfg(any(x11_platform, wayland_platform))]
2422
use linux as platform;
25-
#[cfg(macos_platform)]
26-
use macos as platform;
2723
#[cfg(orbital_platform)]
2824
use orbital as platform;
2925
#[cfg(web_platform)]

0 commit comments

Comments
 (0)