Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ features = [
"Foundation_NSProcessInfo",
"Foundation_NSThread",
"Foundation_NSNumber",
"AppKit",
"AppKit_NSAppearance",
"AppKit_NSApplication",
"AppKit_NSBitmapImageRep",
"AppKit_NSButton",
"AppKit_NSColor",
"AppKit_NSControl",
"AppKit_NSCursor",
"AppKit_NSEvent",
"AppKit_NSGraphicsContext",
"AppKit_NSImage",
"AppKit_NSImageRep",
"AppKit_NSMenu",
"AppKit_NSMenuItem",
"AppKit_NSPasteboard",
"AppKit_NSResponder",
"AppKit_NSScreen",
"AppKit_NSTextInputContext",
"AppKit_NSView",
"AppKit_NSWindow",
"AppKit_NSWindowTabGroup",
]

[target.'cfg(target_os = "ios")'.dependencies.icrate]
Expand Down
5 changes: 4 additions & 1 deletion src/platform/macos.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::os::raw::c_void;

use icrate::Foundation::MainThreadMarker;
use objc2::rc::Id;

use crate::{
Expand Down Expand Up @@ -365,7 +366,9 @@ impl MonitorHandleExtMacOS for MonitorHandle {
}

fn ns_screen(&self) -> Option<*mut c_void> {
self.inner.ns_screen().map(|s| Id::as_ptr(&s) as _)
// SAFETY: We only use the marker to get a pointer
let mtm = unsafe { MainThreadMarker::new_unchecked() };
self.inner.ns_screen(mtm).map(|s| Id::as_ptr(&s) as _)
}
}

Expand Down
44 changes: 25 additions & 19 deletions src/platform_impl/macos/app.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
#![allow(clippy::unnecessary_cast)]

use icrate::AppKit::{
NSEvent, NSEventModifierFlagCommand, NSEventTypeKeyUp, NSEventTypeLeftMouseDown,
NSEventTypeLeftMouseDragged, NSEventTypeLeftMouseUp, NSEventTypeMouseMoved,
NSEventTypeOtherMouseDown, NSEventTypeOtherMouseDragged, NSEventTypeOtherMouseUp,
NSEventTypeRightMouseDown, NSEventTypeRightMouseDragged, NSEventTypeRightMouseUp, NSResponder,
};
use icrate::Foundation::NSObject;
use objc2::{declare_class, msg_send, mutability, ClassType, DeclaredClass};

use super::appkit::{NSApplication, NSEvent, NSEventModifierFlags, NSEventType, NSResponder};
use super::appkit::NSApplication;
use super::event::flags_contains;
use super::{app_state::AppState, DEVICE_ID};
use crate::event::{DeviceEvent, ElementState, Event};

Expand All @@ -13,7 +20,7 @@ declare_class!(
unsafe impl ClassType for WinitApplication {
#[inherits(NSResponder, NSObject)]
type Super = NSApplication;
type Mutability = mutability::InteriorMutable;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "WinitApplication";
}

Expand All @@ -28,10 +35,10 @@ declare_class!(
// For posterity, there are some undocumented event types
// (https://github.com/servo/cocoa-rs/issues/155)
// but that doesn't really matter here.
let event_type = event.type_();
let modifier_flags = event.modifierFlags();
if event_type == NSEventType::NSKeyUp
&& modifier_flags.contains(NSEventModifierFlags::NSCommandKeyMask)
let event_type = unsafe { event.r#type() };
let modifier_flags = unsafe { event.modifierFlags() };
if event_type == NSEventTypeKeyUp
&& flags_contains(modifier_flags, NSEventModifierFlagCommand)
{
if let Some(key_window) = self.keyWindow() {
unsafe { key_window.sendEvent(event) };
Expand All @@ -45,14 +52,15 @@ declare_class!(
);

fn maybe_dispatch_device_event(event: &NSEvent) {
let event_type = event.type_();
let event_type = unsafe { event.r#type() };
#[allow(non_upper_case_globals)]
match event_type {
NSEventType::NSMouseMoved
| NSEventType::NSLeftMouseDragged
| NSEventType::NSOtherMouseDragged
| NSEventType::NSRightMouseDragged => {
let delta_x = event.deltaX() as f64;
let delta_y = event.deltaY() as f64;
NSEventTypeMouseMoved
| NSEventTypeLeftMouseDragged
| NSEventTypeOtherMouseDragged
| NSEventTypeRightMouseDragged => {
let delta_x = unsafe { event.deltaX() } as f64;
let delta_y = unsafe { event.deltaY() } as f64;

if delta_x != 0.0 {
queue_device_event(DeviceEvent::Motion {
Expand All @@ -74,17 +82,15 @@ fn maybe_dispatch_device_event(event: &NSEvent) {
});
}
}
NSEventType::NSLeftMouseDown
| NSEventType::NSRightMouseDown
| NSEventType::NSOtherMouseDown => {
NSEventTypeLeftMouseDown | NSEventTypeRightMouseDown | NSEventTypeOtherMouseDown => {
queue_device_event(DeviceEvent::Button {
button: event.buttonNumber() as u32,
button: unsafe { event.buttonNumber() } as u32,
state: ElementState::Pressed,
});
}
NSEventType::NSLeftMouseUp | NSEventType::NSRightMouseUp | NSEventType::NSOtherMouseUp => {
NSEventTypeLeftMouseUp | NSEventTypeRightMouseUp | NSEventTypeOtherMouseUp => {
queue_device_event(DeviceEvent::Button {
button: event.buttonNumber() as u32,
button: unsafe { event.buttonNumber() } as u32,
state: ElementState::Released,
});
}
Expand Down
12 changes: 7 additions & 5 deletions src/platform_impl/macos/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ use std::{
};

use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};
use icrate::Foundation::{is_main_thread, NSSize};
use icrate::Foundation::{is_main_thread, MainThreadMarker, NSSize};
use objc2::rc::{autoreleasepool, Id};
use once_cell::sync::Lazy;

use super::appkit::{NSApp, NSApplication, NSApplicationActivationPolicy, NSEvent};
use super::appkit::{NSApp, NSApplication, NSApplicationActivationPolicy};
use super::{
event_loop::PanicInfo, menu, observer::EventLoopWaker, util::Never, window::WinitWindow,
event::dummy_event, event_loop::PanicInfo, menu, observer::EventLoopWaker, util::Never,
window::WinitWindow,
};
use crate::{
dpi::PhysicalSize,
Expand Down Expand Up @@ -459,6 +460,7 @@ impl AppState {
create_default_menu: bool,
activate_ignoring_other_apps: bool,
) {
let mtm = MainThreadMarker::new().unwrap();
let app = NSApp();
// We need to delay setting the activation policy and activating the app
// until `applicationDidFinishLaunching` has been called. Otherwise the
Expand All @@ -473,7 +475,7 @@ impl AppState {
if create_default_menu {
// The menubar initialization should be before the `NewEvents` event, to allow
// overriding of the default menu even if it's created
menu::initialize();
menu::initialize(mtm);
}

Self::start_running();
Expand Down Expand Up @@ -593,7 +595,7 @@ impl AppState {
autoreleasepool(|_| {
app.stop(None);
// To stop event loop immediately, we need to post some event here.
app.postEvent_atStart(&NSEvent::dummy(), true);
app.postEvent_atStart(&dummy_event().unwrap(), true);
});
}

Expand Down
28 changes: 0 additions & 28 deletions src/platform_impl/macos/appkit/appearance.rs

This file was deleted.

5 changes: 3 additions & 2 deletions src/platform_impl/macos/appkit/application.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use icrate::AppKit::{NSAppearance, NSEvent, NSMenu, NSResponder};
use icrate::Foundation::{MainThreadMarker, NSArray, NSInteger, NSObject, NSUInteger};
use objc2::rc::Id;
use objc2::runtime::AnyObject;
use objc2::{extern_class, extern_methods, msg_send_id, mutability, ClassType};
use objc2::{Encode, Encoding};

use super::{NSAppearance, NSEvent, NSMenu, NSResponder, NSWindow};
use super::NSWindow;

extern_class!(
#[derive(Debug, PartialEq, Eq, Hash)]
Expand All @@ -13,7 +14,7 @@ extern_class!(
unsafe impl ClassType for NSApplication {
#[inherits(NSObject)]
type Super = NSResponder;
type Mutability = mutability::InteriorMutable;
type Mutability = mutability::MainThreadOnly;
}
);

Expand Down
56 changes: 0 additions & 56 deletions src/platform_impl/macos/appkit/bitmap_image_rep.rs

This file was deleted.

15 changes: 0 additions & 15 deletions src/platform_impl/macos/appkit/button.rs

This file was deleted.

28 changes: 0 additions & 28 deletions src/platform_impl/macos/appkit/color.rs

This file was deleted.

25 changes: 0 additions & 25 deletions src/platform_impl/macos/appkit/control.rs

This file was deleted.

Loading