Skip to content

Commit

Permalink
Clean up iOS ffi.rs (#3530)
Browse files Browse the repository at this point in the history
This makes it easier to transition to a future autogenerated version of UIKit.
  • Loading branch information
madsmtm authored Feb 27, 2024
1 parent e41f0ea commit a4480a0
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 108 deletions.
11 changes: 9 additions & 2 deletions src/platform_impl/ios/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::{
window::{CustomCursor, CustomCursorSource},
};

use super::app_delegate::AppDelegate;
use super::{app_delegate::AppDelegate, uikit::UIUserInterfaceIdiom};
use super::{app_state, monitor, MonitorHandle};
use super::{
app_state::AppState,
Expand Down Expand Up @@ -227,7 +227,14 @@ impl<T: 'static> EventLoop<T> {
// EventLoopExtIOS
impl<T: 'static> EventLoop<T> {
pub fn idiom(&self) -> Idiom {
UIDevice::current(self.mtm).userInterfaceIdiom().into()
match UIDevice::current(self.mtm).userInterfaceIdiom() {
UIUserInterfaceIdiom::Unspecified => Idiom::Unspecified,
UIUserInterfaceIdiom::Phone => Idiom::Phone,
UIUserInterfaceIdiom::Pad => Idiom::Pad,
UIUserInterfaceIdiom::TV => Idiom::TV,
UIUserInterfaceIdiom::CarPlay => Idiom::CarPlay,
_ => Idiom::Unspecified,
}
}
}

Expand Down
76 changes: 0 additions & 76 deletions src/platform_impl/ios/ffi.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
mod app_delegate;
mod app_state;
mod event_loop;
mod ffi;
mod monitor;
mod uikit;
mod view;
Expand Down
21 changes: 18 additions & 3 deletions src/platform_impl/ios/uikit/device.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use icrate::Foundation::{MainThreadMarker, NSObject};
use icrate::Foundation::{MainThreadMarker, NSInteger, NSObject};
use objc2::encode::{Encode, Encoding};
use objc2::rc::Id;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};

use super::super::ffi::UIUserInterfaceIdiom;

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIDevice;
Expand All @@ -24,3 +23,19 @@ extern_methods!(
pub fn userInterfaceIdiom(&self) -> UIUserInterfaceIdiom;
}
);

#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIUserInterfaceIdiom(NSInteger);

unsafe impl Encode for UIUserInterfaceIdiom {
const ENCODING: Encoding = NSInteger::ENCODING;
}

impl UIUserInterfaceIdiom {
pub const Unspecified: UIUserInterfaceIdiom = UIUserInterfaceIdiom(-1);
pub const Phone: UIUserInterfaceIdiom = UIUserInterfaceIdiom(0);
pub const Pad: UIUserInterfaceIdiom = UIUserInterfaceIdiom(1);
pub const TV: UIUserInterfaceIdiom = UIUserInterfaceIdiom(2);
pub const CarPlay: UIUserInterfaceIdiom = UIUserInterfaceIdiom(3);
}
14 changes: 14 additions & 0 deletions src/platform_impl/ios/uikit/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use icrate::Foundation::NSUInteger;
use objc2::encode::{Encode, Encoding};

#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIRectEdge(pub NSUInteger);

impl UIRectEdge {
pub const NONE: Self = Self(0);
}

unsafe impl Encode for UIRectEdge {
const ENCODING: Encoding = NSUInteger::ENCODING;
}
4 changes: 3 additions & 1 deletion src/platform_impl/ios/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod application;
mod coordinate_space;
mod device;
mod event;
mod geometry;
mod gesture_recognizer;
mod responder;
mod screen;
Expand All @@ -22,8 +23,9 @@ mod window;

pub(crate) use self::application::UIApplication;
pub(crate) use self::coordinate_space::UICoordinateSpace;
pub(crate) use self::device::UIDevice;
pub(crate) use self::device::{UIDevice, UIUserInterfaceIdiom};
pub(crate) use self::event::UIEvent;
pub(crate) use self::geometry::UIRectEdge;
pub(crate) use self::gesture_recognizer::{
UIGestureRecognizer, UIGestureRecognizerState, UIPinchGestureRecognizer,
UIRotationGestureRecognizer, UITapGestureRecognizer,
Expand Down
11 changes: 0 additions & 11 deletions src/platform_impl/ios/uikit/status_bar_style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::platform::ios::StatusBarStyle;
use icrate::Foundation::NSInteger;
use objc2::encode::{Encode, Encoding};

Expand All @@ -12,16 +11,6 @@ pub enum UIStatusBarStyle {
DarkContent = 3,
}

impl From<StatusBarStyle> for UIStatusBarStyle {
fn from(value: StatusBarStyle) -> Self {
match value {
StatusBarStyle::Default => Self::Default,
StatusBarStyle::LightContent => Self::LightContent,
StatusBarStyle::DarkContent => Self::DarkContent,
}
}
}

unsafe impl Encode for UIStatusBarStyle {
const ENCODING: Encoding = NSInteger::ENCODING;
}
33 changes: 21 additions & 12 deletions src/platform_impl/ios/view_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,11 @@ use objc2::{declare_class, msg_send_id, mutability, ClassType, DeclaredClass};

use super::app_state::{self};
use super::uikit::{
UIDevice, UIInterfaceOrientationMask, UIResponder, UIStatusBarStyle, UIView, UIViewController,
};
use crate::{
platform::ios::ValidOrientations,
platform_impl::platform::ffi::{UIRectEdge, UIUserInterfaceIdiom},
window::WindowAttributes,
UIDevice, UIInterfaceOrientationMask, UIRectEdge, UIResponder, UIStatusBarStyle,
UIUserInterfaceIdiom, UIView, UIViewController,
};
use crate::platform::ios::{ScreenEdge, StatusBarStyle};
use crate::{platform::ios::ValidOrientations, window::WindowAttributes};

pub struct ViewControllerState {
prefers_status_bar_hidden: Cell<bool>,
Expand Down Expand Up @@ -77,7 +75,12 @@ impl WinitViewController {
self.setNeedsStatusBarAppearanceUpdate();
}

pub(crate) fn set_preferred_status_bar_style(&self, val: UIStatusBarStyle) {
pub(crate) fn set_preferred_status_bar_style(&self, val: StatusBarStyle) {
let val = match val {
StatusBarStyle::Default => UIStatusBarStyle::Default,
StatusBarStyle::LightContent => UIStatusBarStyle::LightContent,
StatusBarStyle::DarkContent => UIStatusBarStyle::DarkContent,
};
self.ivars().preferred_status_bar_style.set(val);
self.setNeedsStatusBarAppearanceUpdate();
}
Expand All @@ -92,7 +95,15 @@ impl WinitViewController {
}
}

pub(crate) fn set_preferred_screen_edges_deferring_system_gestures(&self, val: UIRectEdge) {
pub(crate) fn set_preferred_screen_edges_deferring_system_gestures(&self, val: ScreenEdge) {
let val = {
assert_eq!(
val.bits() & !ScreenEdge::ALL.bits(),
0,
"invalid `ScreenEdge`"
);
UIRectEdge(val.bits().into())
};
self.ivars()
.preferred_screen_edges_deferring_system_gestures
.set(val);
Expand Down Expand Up @@ -154,8 +165,7 @@ impl WinitViewController {
this.set_preferred_status_bar_style(
window_attributes
.platform_specific
.preferred_status_bar_style
.into(),
.preferred_status_bar_style,
);

this.set_supported_interface_orientations(
Expand All @@ -172,8 +182,7 @@ impl WinitViewController {
this.set_preferred_screen_edges_deferring_system_gestures(
window_attributes
.platform_specific
.preferred_screen_edges_deferring_system_gestures
.into(),
.preferred_screen_edges_deferring_system_gestures,
);

this.setView(Some(view));
Expand Down
4 changes: 2 additions & 2 deletions src/platform_impl/ios/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ impl Inner {

pub fn set_preferred_screen_edges_deferring_system_gestures(&self, edges: ScreenEdge) {
self.view_controller
.set_preferred_screen_edges_deferring_system_gestures(edges.into());
.set_preferred_screen_edges_deferring_system_gestures(edges);
}

pub fn set_prefers_status_bar_hidden(&self, hidden: bool) {
Expand All @@ -637,7 +637,7 @@ impl Inner {

pub fn set_preferred_status_bar_style(&self, status_bar_style: StatusBarStyle) {
self.view_controller
.set_preferred_status_bar_style(status_bar_style.into());
.set_preferred_status_bar_style(status_bar_style);
}

pub fn recognize_pinch_gesture(&self, should_recognize: bool) {
Expand Down

0 comments on commit a4480a0

Please sign in to comment.