Skip to content

Commit

Permalink
chore(deps): Use objc2 v0.6 (#226)
Browse files Browse the repository at this point in the history
This uses the new crates objc2-core-graphics and objc2-core-foundation.
  • Loading branch information
madsmtm authored Jan 26, 2025
1 parent f1c4490 commit d6fee6f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changes/objc2-v6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tray-icon": patch
---

Update `objc2` to v0.6.
23 changes: 16 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,30 @@ dirs = "5"
gtk = "0.18"

[target."cfg(target_os = \"macos\")".dependencies]
objc2 = "0.5.2"
objc2-foundation = { version = "0.2.2", features = [
objc2 = "0.6.0"
objc2-core-graphics = { version = "0.3.0", default-features = false, features = [
"std",
"CGDirectDisplay",
] }
objc2-core-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"CFCGTypes",
"CFRunLoop",
] }
objc2-foundation = { version = "0.3.0", default-features = false, features = [
"std",
"block2",
"objc2-core-foundation",
"NSArray",
"NSData",
"NSEnumerator",
"NSGeometry",
"NSString",
"NSThread",
] }
objc2-app-kit = { version = "0.2.2", features = [
objc2-app-kit = { version = "0.3.0", default-features = false, features = [
"std",
"objc2-core-foundation",
"NSButton",
"NSCell",
"NSControl",
Expand All @@ -64,10 +77,6 @@ objc2-app-kit = { version = "0.2.2", features = [
"NSView",
"NSWindow",
] }
core-graphics = "0.24"

[target."cfg(target_os = \"macos\")".dev-dependencies]
core-foundation = "0.10"

[target."cfg(any(target_os = \"linux\", target_os = \"macos\"))".dependencies]
png = "0.17"
Expand Down
6 changes: 3 additions & 3 deletions examples/tao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ fn main() {
// Tao only exposes a redraw method on the Window so we use core-foundation directly.
#[cfg(target_os = "macos")]
unsafe {
use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};
use objc2_core_foundation::{CFRunLoopGetMain, CFRunLoopWakeUp};

let rl = CFRunLoopGetMain();
CFRunLoopWakeUp(rl);
let rl = CFRunLoopGetMain().unwrap();
CFRunLoopWakeUp(&rl);
}
}

Expand Down
6 changes: 3 additions & 3 deletions examples/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ impl ApplicationHandler<UserEvent> for Application {
// Winit only exposes a redraw method on the Window so we use core-foundation directly.
#[cfg(target_os = "macos")]
unsafe {
use core_foundation::runloop::{CFRunLoopGetMain, CFRunLoopWakeUp};
use objc2_core_foundation::{CFRunLoopGetMain, CFRunLoopWakeUp};

let rl = CFRunLoopGetMain();
CFRunLoopWakeUp(rl);
let rl = CFRunLoopGetMain().unwrap();
CFRunLoopWakeUp(&rl);
}
}
}
Expand Down
69 changes: 31 additions & 38 deletions src/platform_impl/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
mod icon;
use std::cell::{Cell, RefCell};

use core_graphics::display::CGDisplay;
use objc2::rc::Retained;
use objc2::{declare_class, msg_send, msg_send_id, mutability, ClassType, DeclaredClass};
use objc2::{define_class, msg_send, AllocAnyThread, DeclaredClass, Message};
use objc2_app_kit::{
NSCellImagePosition, NSEvent, NSImage, NSMenu, NSStatusBar, NSStatusItem, NSTrackingArea,
NSTrackingAreaOptions, NSVariableStatusItemLength, NSView, NSWindow,
};
use objc2_foundation::{CGPoint, CGRect, CGSize, MainThreadMarker, NSData, NSSize, NSString};
use objc2_core_foundation::{CGPoint, CGRect, CGSize};
use objc2_core_graphics::{CGDisplayPixelsHigh, CGMainDisplayID};
use objc2_foundation::{MainThreadMarker, NSData, NSSize, NSString};

pub(crate) use self::icon::PlatformIcon;
use crate::Error;
Expand Down Expand Up @@ -86,8 +87,7 @@ impl TrayIcon {
status_item: ns_status_item.retain(),
menu_on_left_click: Cell::new(attrs.menu_on_left_click),
});
let tray_target: Retained<TrayTarget> =
msg_send_id![super(target), initWithFrame: frame];
let tray_target: Retained<TrayTarget> = msg_send![super(target), initWithFrame: frame];
tray_target.setWantsLayer(true);

button.addSubview(&tray_target);
Expand Down Expand Up @@ -285,7 +285,7 @@ fn set_icon_for_ns_status_item_button(
button.setImage(Some(&nsimage));
nsimage.setSize(new_size);
// The image is to the right of the title
button.setImagePosition(NSCellImagePosition::NSImageLeft);
button.setImagePosition(NSCellImagePosition::ImageLeft);
nsimage.setTemplate(icon_is_template);
}
} else {
Expand All @@ -303,22 +303,15 @@ struct TrayTargetIvars {
menu_on_left_click: Cell<bool>,
}

declare_class!(
define_class!(
#[unsafe(super(NSView))]
#[name = "TaoTrayTarget"]
#[ivars = TrayTargetIvars]
struct TrayTarget;

unsafe impl ClassType for TrayTarget {
type Super = NSView;
type Mutability = mutability::MainThreadOnly;
const NAME: &'static str = "TaoTrayTarget";
}

impl DeclaredClass for TrayTarget {
type Ivars = TrayTargetIvars;
}

// Mouse events on NSResponder
unsafe impl TrayTarget {
#[method(mouseDown:)]
/// Mouse events on NSResponder
impl TrayTarget {
#[unsafe(method(mouseDown:))]
fn on_mouse_down(&self, event: &NSEvent) {
send_mouse_event(
self,
Expand All @@ -332,7 +325,7 @@ declare_class!(
on_tray_click(self, MouseButton::Left);
}

#[method(mouseUp:)]
#[unsafe(method(mouseUp:))]
fn on_mouse_up(&self, event: &NSEvent) {
let mtm = MainThreadMarker::from(self);
unsafe {
Expand All @@ -350,7 +343,7 @@ declare_class!(
);
}

#[method(rightMouseDown:)]
#[unsafe(method(rightMouseDown:))]
fn on_right_mouse_down(&self, event: &NSEvent) {
send_mouse_event(
self,
Expand All @@ -364,7 +357,7 @@ declare_class!(
on_tray_click(self, MouseButton::Right);
}

#[method(rightMouseUp:)]
#[unsafe(method(rightMouseUp:))]
fn on_right_mouse_up(&self, event: &NSEvent) {
send_mouse_event(
self,
Expand All @@ -377,7 +370,7 @@ declare_class!(
);
}

#[method(otherMouseDown:)]
#[unsafe(method(otherMouseDown:))]
fn on_other_mouse_down(&self, event: &NSEvent) {
let button_number = unsafe { event.buttonNumber() };
if button_number == 2 {
Expand All @@ -393,7 +386,7 @@ declare_class!(
}
}

#[method(otherMouseUp:)]
#[unsafe(method(otherMouseUp:))]
fn on_other_mouse_up(&self, event: &NSEvent) {
let button_number = unsafe { event.buttonNumber() };
if button_number == 2 {
Expand All @@ -409,38 +402,38 @@ declare_class!(
}
}

#[method(mouseEntered:)]
#[unsafe(method(mouseEntered:))]
fn on_mouse_entered(&self, event: &NSEvent) {
send_mouse_event(self, event, MouseEventType::Enter, None);
}

#[method(mouseExited:)]
#[unsafe(method(mouseExited:))]
fn on_mouse_exited(&self, event: &NSEvent) {
send_mouse_event(self, event, MouseEventType::Leave, None);
}

#[method(mouseMoved:)]
#[unsafe(method(mouseMoved:))]
fn on_mouse_moved(&self, event: &NSEvent) {
send_mouse_event(self, event, MouseEventType::Move, None);
}
}

// Tracking mouse enter/exit/move events
unsafe impl TrayTarget {
#[method(updateTrackingAreas)]
/// Tracking mouse enter/exit/move events
impl TrayTarget {
#[unsafe(method(updateTrackingAreas))]
fn update_tracking_areas(&self) {
unsafe {
let areas = self.trackingAreas();
for area in &areas {
self.removeTrackingArea(area);
for area in areas {
self.removeTrackingArea(&area);
}

let _: () = msg_send![super(self), updateTrackingAreas];

let options = NSTrackingAreaOptions::NSTrackingMouseEnteredAndExited
| NSTrackingAreaOptions::NSTrackingMouseMoved
| NSTrackingAreaOptions::NSTrackingActiveAlways
| NSTrackingAreaOptions::NSTrackingInVisibleRect;
let options = NSTrackingAreaOptions::MouseEnteredAndExited
| NSTrackingAreaOptions::MouseMoved
| NSTrackingAreaOptions::ActiveAlways
| NSTrackingAreaOptions::InVisibleRect;
let rect = CGRect {
origin: CGPoint { x: 0.0, y: 0.0 },
size: CGSize {
Expand Down Expand Up @@ -590,5 +583,5 @@ struct MouseClickEvent {
/// This conversion happens to be symmetric, so we only need this one function
/// to convert between the two coordinate systems.
fn flip_window_screen_coordinates(y: f64) -> f64 {
CGDisplay::main().pixels_high() as f64 - y
unsafe { CGDisplayPixelsHigh(CGMainDisplayID()) as f64 - y }
}

0 comments on commit d6fee6f

Please sign in to comment.