Skip to content

Commit

Permalink
Add safe abstraction over UIView
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Dec 23, 2022
1 parent 567271a commit 90be49d
Show file tree
Hide file tree
Showing 12 changed files with 415 additions and 324 deletions.
41 changes: 0 additions & 41 deletions src/platform_impl/ios/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,47 +33,6 @@ unsafe impl Encode for NSOperatingSystemVersion {
);
}

#[derive(Debug)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchPhase {
Began = 0,
Moved,
Stationary,
Ended,
Cancelled,
}

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

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UIForceTouchCapability {
Unknown = 0,
Unavailable,
Available,
}

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

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchType {
Direct = 0,
Indirect,
Pencil,
}

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

#[repr(transparent)]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct UIUserInterfaceIdiom(NSInteger);
Expand Down
5 changes: 3 additions & 2 deletions src/platform_impl/ios/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,19 @@ pub(crate) use self::{
window::{PlatformSpecificWindowBuilderAttributes, Window, WindowId},
};

use self::uikit::UIScreen;
pub(crate) use crate::icon::NoIcon as PlatformIcon;
pub(self) use crate::platform_impl::Fullscreen;

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceId {
uiscreen: ffi::id,
uiscreen: *const UIScreen,
}

impl DeviceId {
pub const unsafe fn dummy() -> Self {
DeviceId {
uiscreen: std::ptr::null_mut(),
uiscreen: std::ptr::null(),
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/platform_impl/ios/uikit/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use objc2::foundation::NSObject;
use objc2::{extern_class, ClassType};

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UIEvent;

unsafe impl ClassType for UIEvent {
type Super = NSObject;
}
);
6 changes: 6 additions & 0 deletions src/platform_impl/ios/uikit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,25 @@
mod application;
mod coordinate_space;
mod device;
mod event;
mod responder;
mod screen;
mod screen_mode;
mod touch;
mod trait_collection;
mod view;
mod view_controller;
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::event::UIEvent;
pub(crate) use self::responder::UIResponder;
pub(crate) use self::screen::{UIScreen, UIScreenOverscanCompensation};
pub(crate) use self::screen_mode::UIScreenMode;
pub(crate) use self::touch::{UITouch, UITouchPhase, UITouchType};
pub(crate) use self::trait_collection::{UIForceTouchCapability, UITraitCollection};
#[allow(unused_imports)]
pub(crate) use self::view::{UIEdgeInsets, UIView};
pub(crate) use self::view_controller::{UIInterfaceOrientationMask, UIViewController};
Expand Down
5 changes: 1 addition & 4 deletions src/platform_impl/ios/uikit/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ extern_methods!(
}

#[sel(setOverscanCompensation:)]
pub fn setOverscanCompensation(
&self,
overscanCompensation: UIScreenOverscanCompensation,
);
pub fn setOverscanCompensation(&self, overscanCompensation: UIScreenOverscanCompensation);

pub fn coordinateSpace(&self) -> Id<UICoordinateSpace, Shared> {
unsafe { msg_send_id![self, coordinateSpace] }
Expand Down
64 changes: 64 additions & 0 deletions src/platform_impl/ios/uikit/touch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use objc2::encode::{Encode, Encoding};
use objc2::foundation::{CGFloat, CGPoint, NSInteger, NSObject};
use objc2::{extern_class, extern_methods, ClassType};

use super::UIView;

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UITouch;

unsafe impl ClassType for UITouch {
type Super = NSObject;
}
);

extern_methods!(
unsafe impl UITouch {
#[sel(locationInView:)]
pub fn locationInView(&self, view: Option<&UIView>) -> CGPoint;

#[sel(type)]
pub fn type_(&self) -> UITouchType;

#[sel(force)]
pub fn force(&self) -> CGFloat;

#[sel(maximumPossibleForce)]
pub fn maximumPossibleForce(&self) -> CGFloat;

#[sel(altitudeAngle)]
pub fn altitudeAngle(&self) -> CGFloat;

#[sel(phase)]
pub fn phase(&self) -> UITouchPhase;
}
);

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchType {
Direct = 0,
Indirect,
Pencil,
}

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

#[derive(Debug)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UITouchPhase {
Began = 0,
Moved,
Stationary,
Ended,
Cancelled,
}

unsafe impl Encode for UITouchPhase {
const ENCODING: Encoding = NSInteger::ENCODING;
}
32 changes: 32 additions & 0 deletions src/platform_impl/ios/uikit/trait_collection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use objc2::encode::{Encode, Encoding};
use objc2::foundation::{NSInteger, NSObject};
use objc2::{extern_class, extern_methods, ClassType};

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
pub(crate) struct UITraitCollection;

unsafe impl ClassType for UITraitCollection {
type Super = NSObject;
}
);

extern_methods!(
unsafe impl UITraitCollection {
#[sel(forceTouchCapability)]
pub fn forceTouchCapability(&self) -> UIForceTouchCapability;
}
);

#[derive(Debug, PartialEq, Eq)]
#[allow(dead_code)]
#[repr(isize)]
pub enum UIForceTouchCapability {
Unknown = 0,
Unavailable,
Available,
}

unsafe impl Encode for UIForceTouchCapability {
const ENCODING: Encoding = NSInteger::ENCODING;
}
20 changes: 19 additions & 1 deletion src/platform_impl/ios/uikit/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use objc2::foundation::{CGFloat, CGRect, NSObject};
use objc2::rc::{Id, Shared};
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};

use super::{UIResponder, UIViewController, UICoordinateSpace};
use super::{UICoordinateSpace, UIResponder, UIViewController};

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -23,6 +23,21 @@ extern_methods!(
#[sel(setBounds:)]
pub fn setBounds(&self, value: CGRect);

#[sel(frame)]
pub fn frame(&self) -> CGRect;

#[sel(setFrame:)]
pub fn setFrame(&self, value: CGRect);

#[sel(contentScaleFactor)]
pub fn contentScaleFactor(&self) -> CGFloat;

#[sel(setContentScaleFactor:)]
pub fn setContentScaleFactor(&self, val: CGFloat);

#[sel(setMultipleTouchEnabled:)]
pub fn setMultipleTouchEnabled(&self, val: bool);

pub fn rootViewController(&self) -> Option<Id<UIViewController, Shared>> {
unsafe { msg_send_id![self, rootViewController] }
}
Expand All @@ -46,6 +61,9 @@ extern_methods!(

#[sel(safeAreaInsets)]
pub fn safeAreaInsets(&self) -> UIEdgeInsets;

#[sel(setNeedsDisplay)]
pub fn setNeedsDisplay(&self);
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/platform_impl/ios/uikit/view_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use objc2::encode::{Encode, Encoding};
use objc2::foundation::{NSObject, NSUInteger};
use objc2::{extern_class, extern_methods, ClassType};

use super::{UIView, UIResponder};
use super::{UIResponder, UIView};

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down
5 changes: 1 addition & 4 deletions src/platform_impl/ios/uikit/window.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use objc2::foundation::{CGRect, NSObject};
use objc2::foundation::NSObject;
use objc2::rc::{Id, Shared};
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};

Expand Down Expand Up @@ -26,9 +26,6 @@ extern_methods!(
#[sel(setHidden:)]
pub fn setHidden(&self, flag: bool);

#[sel(setFrame:)]
pub fn setFrame(&self, value: CGRect);

#[sel(makeKeyAndVisible)]
pub fn makeKeyAndVisible(&self);
}
Expand Down
Loading

0 comments on commit 90be49d

Please sign in to comment.