diff options
| author | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-06-27 21:18:09 +0200 |
|---|---|---|
| committer | Ruben Beltran del Rio <git@r.bdr.sh> | 2025-06-27 21:18:09 +0200 |
| commit | b3964aefbcab8ff098afabf64befa04a4eebd993 (patch) | |
| tree | 6065486ff980edf04ab1844ad6ad424dde690d59 /src/sources | |
| parent | 7728f7e1349cc40f8a1bc5c09f0e2ad6b2739c95 (diff) | |
Update mac version to use objc2
Diffstat (limited to 'src/sources')
| -rw-r--r-- | src/sources/apple_music.rs | 58 | ||||
| -rw-r--r-- | src/sources/spotify.rs | 58 |
2 files changed, 40 insertions, 76 deletions
diff --git a/src/sources/apple_music.rs b/src/sources/apple_music.rs index 6279a50..572fe6b 100644 --- a/src/sources/apple_music.rs +++ b/src/sources/apple_music.rs @@ -1,16 +1,12 @@ -use std::ffi::CStr; use std::io::Result; -use cocoa::{ - base::{id, nil}, - foundation::NSString, +use objc2::{ + msg_send, + rc::{Retained, autoreleasepool}, + runtime::AnyObject, }; -use objc::{ - class, msg_send, - runtime::{Class, Object}, - sel, sel_impl, -}; -use objc_id::Id; +use objc2_foundation::NSString; +use objc2_scripting_bridge::SBApplication; use crate::Track; @@ -31,36 +27,22 @@ impl LyricsSource for AppleMusic { fn current_track(&self) -> Option<Track> { unsafe { - let app: Id<Object> = { - let cls = class!(SBApplication); - let bundle_identifier = NSString::alloc(nil).init_str("com.apple.Music"); - - let workspace_class = Class::get("NSWorkspace").unwrap(); - let shared_workspace: id = msg_send![workspace_class, sharedWorkspace]; - let app_url: id = msg_send![shared_workspace, URLForApplicationWithBundleIdentifier:bundle_identifier]; - - if app_url != nil { - let app: *mut Object = - msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; - Id::from_ptr(app) - } else { - return None; - } - }; - - if msg_send![app, isRunning] { - let current_track: *mut Object = msg_send![app, currentTrack]; - if !current_track.is_null() { - let name_raw: *mut Object = msg_send![current_track, name]; - let artist_raw: *mut Object = msg_send![current_track, artist]; - - let name_ptr: *const i8 = msg_send![name_raw, UTF8String]; - let artist_ptr: *const i8 = msg_send![artist_raw, UTF8String]; + let bundle_identifier = NSString::from_str("com.apple.Music"); + let app = SBApplication::applicationWithBundleIdentifier(&bundle_identifier); + if let Some(app) = app + && app.isRunning() + { + let current_track: Option<Retained<AnyObject>> = msg_send![&app, currentTrack]; + if let Some(current_track) = current_track { + let name_raw: Option<Retained<NSString>> = msg_send![¤t_track, name]; + let artist_raw: Option<Retained<NSString>> = msg_send![¤t_track, artist]; - let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned(); - let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned(); + if let (Some(name_raw), Some(artist_raw)) = (name_raw, artist_raw) { + let name = autoreleasepool(|pool| name_raw.to_str(pool).to_string()); + let artist = autoreleasepool(|pool| artist_raw.to_str(pool).to_string()); - return Some(Track { name, artist }); + return Some(Track { name, artist }); + } } } } diff --git a/src/sources/spotify.rs b/src/sources/spotify.rs index bd627db..cd37491 100644 --- a/src/sources/spotify.rs +++ b/src/sources/spotify.rs @@ -1,16 +1,12 @@ -use std::ffi::CStr; use std::io::Result; -use cocoa::{ - base::{id, nil}, - foundation::NSString, +use objc2::{ + msg_send, + rc::{Retained, autoreleasepool}, + runtime::AnyObject, }; -use objc::{ - class, msg_send, - runtime::{Class, Object}, - sel, sel_impl, -}; -use objc_id::Id; +use objc2_foundation::NSString; +use objc2_scripting_bridge::SBApplication; use crate::Track; @@ -31,36 +27,22 @@ impl LyricsSource for Spotify { fn current_track(&self) -> Option<Track> { unsafe { - let app: Id<Object> = { - let cls = class!(SBApplication); - let bundle_identifier = NSString::alloc(nil).init_str("com.spotify.Client"); - - let workspace_class = Class::get("NSWorkspace").unwrap(); - let shared_workspace: id = msg_send![workspace_class, sharedWorkspace]; - let app_url: id = msg_send![shared_workspace, URLForApplicationWithBundleIdentifier:bundle_identifier]; - - if app_url != nil { - let app: *mut Object = - msg_send![cls, applicationWithBundleIdentifier:bundle_identifier]; - Id::from_ptr(app) - } else { - return None; - } - }; - - if msg_send![app, isRunning] { - let current_track: *mut Object = msg_send![app, currentTrack]; - if !current_track.is_null() { - let name_raw: *mut Object = msg_send![current_track, name]; - let artist_raw: *mut Object = msg_send![current_track, artist]; - - let name_ptr: *const i8 = msg_send![name_raw, UTF8String]; - let artist_ptr: *const i8 = msg_send![artist_raw, UTF8String]; + let bundle_identifier = NSString::from_str("com.spotify.Client"); + let app = SBApplication::applicationWithBundleIdentifier(&bundle_identifier); + if let Some(app) = app + && app.isRunning() + { + let current_track: Option<Retained<AnyObject>> = msg_send![&app, currentTrack]; + if let Some(current_track) = current_track { + let name_raw: Option<Retained<NSString>> = msg_send![¤t_track, name]; + let artist_raw: Option<Retained<NSString>> = msg_send![¤t_track, artist]; - let name = CStr::from_ptr(name_ptr).to_string_lossy().into_owned(); - let artist = CStr::from_ptr(artist_ptr).to_string_lossy().into_owned(); + if let (Some(name_raw), Some(artist_raw)) = (name_raw, artist_raw) { + let name = autoreleasepool(|pool| name_raw.to_str(pool).to_string()); + let artist = autoreleasepool(|pool| artist_raw.to_str(pool).to_string()); - return Some(Track { name, artist }); + return Some(Track { name, artist }); + } } } } |