Skip to content

Commit

Permalink
Remove cocoa dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Aug 31, 2022
1 parent 046f13a commit 4fa8fea
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 14 deletions.
6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ ndk-glue = "0.7.0"
objc = { version = "=0.3.0-beta.2", package = "objc2" }

[target.'cfg(target_os = "macos")'.dependencies]
# Branch: objc2
cocoa = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" }
core-foundation = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" }
core-graphics = { git = "https://github.com/madsmtm/core-foundation-rs.git", rev = "8e6c4854de2408ed1e3e614dbe6f9f2762bde7c3" }
core-foundation = "0.9.3"
core-graphics = "0.22.3"
dispatch = "0.2.0"

[target.'cfg(target_os = "windows")'.dependencies]
Expand Down
50 changes: 47 additions & 3 deletions src/platform_impl/macos/appkit/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use objc2::foundation::{MainThreadMarker, NSArray, NSInteger, NSObject, NSUInteg
use objc2::rc::{Id, Shared};
use objc2::runtime::Object;
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};
use objc2::{Encode, Encoding};

use super::{NSEvent, NSMenu, NSResponder, NSWindow};
pub use cocoa::appkit::{
NSApplicationActivationPolicy, NSApplicationPresentationOptions, NSRequestUserAttentionType,
};

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -88,3 +86,49 @@ extern_methods!(
pub unsafe fn run(&self);
}
);

#[repr(isize)] // NSInteger
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NSApplicationActivationPolicy {
NSApplicationActivationPolicyRegular = 0,
NSApplicationActivationPolicyAccessory = 1,
NSApplicationActivationPolicyProhibited = 2,
NSApplicationActivationPolicyERROR = -1,
}

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

bitflags! {
pub struct NSApplicationPresentationOptions: NSUInteger {
const NSApplicationPresentationDefault = 0;
const NSApplicationPresentationAutoHideDock = 1 << 0;
const NSApplicationPresentationHideDock = 1 << 1;
const NSApplicationPresentationAutoHideMenuBar = 1 << 2;
const NSApplicationPresentationHideMenuBar = 1 << 3;
const NSApplicationPresentationDisableAppleMenu = 1 << 4;
const NSApplicationPresentationDisableProcessSwitching = 1 << 5;
const NSApplicationPresentationDisableForceQuit = 1 << 6;
const NSApplicationPresentationDisableSessionTermination = 1 << 7;
const NSApplicationPresentationDisableHideApplication = 1 << 8;
const NSApplicationPresentationDisableMenuBarTransparency = 1 << 9;
const NSApplicationPresentationFullScreen = 1 << 10;
const NSApplicationPresentationAutoHideToolbar = 1 << 11;
}
}

unsafe impl Encode for NSApplicationPresentationOptions {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

#[repr(usize)] // NSUInteger
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NSRequestUserAttentionType {
NSCriticalRequest = 0,
NSInformationalRequest = 10,
}

unsafe impl Encode for NSRequestUserAttentionType {
const ENCODING: Encoding = NSUInteger::ENCODING;
}
100 changes: 96 additions & 4 deletions src/platform_impl/macos/appkit/event.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::os::raw::c_ushort;
use std::os::raw::{c_double, c_ushort};

use cocoa::foundation::NSTimeInterval;
use objc2::encode::{Encode, Encoding};
use objc2::foundation::{CGFloat, NSCopying, NSInteger, NSObject, NSPoint, NSString, NSUInteger};
use objc2::rc::{Id, Shared};
use objc2::{extern_class, extern_methods, msg_send_id, ClassType};

// TODO
pub use cocoa::appkit::{NSEventModifierFlags, NSEventPhase, NSEventSubtype, NSEventType};
// TODO: Move this to objc2::foundation
type NSTimeInterval = c_double;

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -138,3 +138,95 @@ unsafe impl NSCopying for NSEvent {
type Ownership = Shared;
type Output = NSEvent;
}

bitflags! {
pub struct NSEventModifierFlags: NSUInteger {
const NSAlphaShiftKeyMask = 1 << 16;
const NSShiftKeyMask = 1 << 17;
const NSControlKeyMask = 1 << 18;
const NSAlternateKeyMask = 1 << 19;
const NSCommandKeyMask = 1 << 20;
const NSNumericPadKeyMask = 1 << 21;
const NSHelpKeyMask = 1 << 22;
const NSFunctionKeyMask = 1 << 23;
const NSDeviceIndependentModifierFlagsMask = 0xffff0000;
}
}

unsafe impl Encode for NSEventModifierFlags {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

bitflags! {
pub struct NSEventPhase: NSUInteger {
const NSEventPhaseNone = 0;
const NSEventPhaseBegan = 0x1 << 0;
const NSEventPhaseStationary = 0x1 << 1;
const NSEventPhaseChanged = 0x1 << 2;
const NSEventPhaseEnded = 0x1 << 3;
const NSEventPhaseCancelled = 0x1 << 4;
const NSEventPhaseMayBegin = 0x1 << 5;
}
}

unsafe impl Encode for NSEventPhase {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

#[repr(i16)] // short
pub enum NSEventSubtype {
// TODO: Not sure what these values are
// NSMouseEventSubtype = NX_SUBTYPE_DEFAULT,
// NSTabletPointEventSubtype = NX_SUBTYPE_TABLET_POINT,
// NSTabletProximityEventSubtype = NX_SUBTYPE_TABLET_PROXIMITY
// NSTouchEventSubtype = NX_SUBTYPE_MOUSE_TOUCH,
NSWindowExposedEventType = 0,
NSApplicationActivatedEventType = 1,
NSApplicationDeactivatedEventType = 2,
NSWindowMovedEventType = 4,
NSScreenChangedEventType = 8,
NSAWTEventType = 16,
}

unsafe impl Encode for NSEventSubtype {
const ENCODING: Encoding = i16::ENCODING;
}

#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(usize)] // NSUInteger
pub enum NSEventType {
NSLeftMouseDown = 1,
NSLeftMouseUp = 2,
NSRightMouseDown = 3,
NSRightMouseUp = 4,
NSMouseMoved = 5,
NSLeftMouseDragged = 6,
NSRightMouseDragged = 7,
NSMouseEntered = 8,
NSMouseExited = 9,
NSKeyDown = 10,
NSKeyUp = 11,
NSFlagsChanged = 12,
NSAppKitDefined = 13,
NSSystemDefined = 14,
NSApplicationDefined = 15,
NSPeriodic = 16,
NSCursorUpdate = 17,
NSScrollWheel = 22,
NSTabletPoint = 23,
NSTabletProximity = 24,
NSOtherMouseDown = 25,
NSOtherMouseUp = 26,
NSOtherMouseDragged = 27,
NSEventTypeGesture = 29,
NSEventTypeMagnify = 30,
NSEventTypeSwipe = 31,
NSEventTypeRotate = 18,
NSEventTypeBeginGesture = 19,
NSEventTypeEndGesture = 20,
NSEventTypePressure = 34,
}

unsafe impl Encode for NSEventType {
const ENCODING: Encoding = NSUInteger::ENCODING;
}
2 changes: 2 additions & 0 deletions src/platform_impl/macos/appkit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![allow(unused_imports)] // TMP
#![allow(dead_code)] // TMP
#![allow(non_snake_case)]
#![allow(clippy::enum_variant_names)]
#![allow(non_upper_case_globals)]

mod application;
mod button;
Expand Down
46 changes: 43 additions & 3 deletions src/platform_impl/macos/appkit/window.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use objc2::encode::{Encode, Encoding, RefEncode};
use objc2::foundation::{CGFloat, NSArray, NSObject, NSPoint, NSRect, NSSize, NSString};
use objc2::foundation::{
CGFloat, NSArray, NSObject, NSPoint, NSRect, NSSize, NSString, NSUInteger,
};
use objc2::rc::{Id, Shared};
use objc2::runtime::Object;
use objc2::{extern_class, extern_methods, msg_send, msg_send_id, ClassType};

use super::{NSButton, NSColor, NSEvent, NSPasteboardType, NSResponder, NSScreen, NSView};

pub use cocoa::appkit::{NSBackingStoreType, NSWindowOcclusionState, NSWindowStyleMask};

extern_class!(
/// Main-Thread-Only!
#[derive(Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -282,3 +282,43 @@ pub enum NSWindowLevel {
unsafe impl Encode for NSWindowLevel {
const ENCODING: Encoding = isize::ENCODING;
}

bitflags! {
pub struct NSWindowOcclusionState: NSUInteger {
const NSWindowOcclusionStateVisible = 1 << 1;
}
}

unsafe impl Encode for NSWindowOcclusionState {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

bitflags! {
pub struct NSWindowStyleMask: NSUInteger {
const NSBorderlessWindowMask = 0;
const NSTitledWindowMask = 1 << 0;
const NSClosableWindowMask = 1 << 1;
const NSMiniaturizableWindowMask = 1 << 2;
const NSResizableWindowMask = 1 << 3;
const NSTexturedBackgroundWindowMask = 1 << 8;
const NSUnifiedTitleAndToolbarWindowMask = 1 << 12;
const NSFullScreenWindowMask = 1 << 14;
const NSFullSizeContentViewWindowMask = 1 << 15;
}
}

unsafe impl Encode for NSWindowStyleMask {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

#[repr(usize)] // NSUInteger
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NSBackingStoreType {
NSBackingStoreRetained = 0,
NSBackingStoreNonretained = 1,
NSBackingStoreBuffered = 2,
}

unsafe impl Encode for NSBackingStoreType {
const ENCODING: Encoding = NSUInteger::ENCODING;
}

0 comments on commit 4fa8fea

Please sign in to comment.