Skip to content

Commit

Permalink
use objc2 instead of objc for more convenient to call objectc (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
tmts2233 authored Jan 16, 2025
1 parent 1b00814 commit e7fabfd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ x11-clipboard = "0.9"
x11rb = { version = "0.13", features = ["xfixes"] }

[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
objc_id = "0.1"
objc-foundation = "0.1"
objc2 = { version = "0.5.2" }
objc2-app-kit = { version = "0.2.2", features = ["NSPasteboard"] }

[features]
cargo-clippy = []
21 changes: 7 additions & 14 deletions src/master/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::io;
use std::sync::mpsc::{self, SyncSender, Receiver, sync_channel};
use crate::{ClipboardHandler, CallbackResult};

use objc::runtime::{Object, Class};
use objc_id::Id;
use objc2::{msg_send_id, rc::Id, ClassType};
use objc2_app_kit::NSPasteboard;

#[link(name = "AppKit", kind = "framework")]
extern "C" {}
Expand Down Expand Up @@ -58,25 +58,18 @@ impl<H: ClipboardHandler> Master<H> {

///Starts Master by polling clipboard for change
pub fn run(&mut self) -> io::Result<()> {
use objc::{msg_send, sel, sel_impl};
let pasteboard: Option<Id<NSPasteboard>> = unsafe { msg_send_id![NSPasteboard::class(), generalPasteboard] };

let cls = match Class::get("NSPasteboard") {
Some(cls) => cls,
let pasteboard = match pasteboard {
Some(pasteboard) => pasteboard,
None => return Err(io::Error::new(io::ErrorKind::Other, "Unable to create mac pasteboard")),
};
let pasteboard: *mut Object = unsafe { msg_send![cls, generalPasteboard] };

if pasteboard.is_null() {
return Err(io::Error::new(io::ErrorKind::Other, "Unable to create mac pasteboard"));
}

let pasteboard: Id<Object> = unsafe { Id::from_ptr(pasteboard) };

let mut prev_count = unsafe { msg_send![pasteboard, changeCount] };
let mut prev_count = unsafe { pasteboard.changeCount() };
let mut result = Ok(());

loop {
let count: isize = unsafe { msg_send![pasteboard, changeCount] };
let count: isize = unsafe { pasteboard.changeCount() };

if count == prev_count {
match self.recv.recv_timeout(self.handler.sleep_interval()) {
Expand Down

0 comments on commit e7fabfd

Please sign in to comment.