Skip to content

Commit

Permalink
Use objc2 wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Berrysoft committed Sep 22, 2024
1 parent 9fc82e7 commit a48ef68
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 51 deletions.
15 changes: 13 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ serde = "1"
serde_json = "1"
serde_with = "3"

objc = "0.2"
core-foundation = "0.9"
system-configuration = "0.6"

Expand Down
2 changes: 1 addition & 1 deletion color-theme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ windows = { workspace = true, features = ["UI_ViewManagement"] }
log = { workspace = true }

[target.'cfg(target_os = "macos")'.dependencies]
objc = { workspace = true }
objc2-app-kit = { version = "0.2", features = ["NSColor", "NSColorSpace"] }
40 changes: 15 additions & 25 deletions color-theme/src/mac.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,22 @@
use super::*;
use objc::{
runtime::{Class, Object},
*,
};
use std::ptr::{addr_of_mut, null_mut};

#[link(name = "AppKit", kind = "framework")]
extern "C" {
#[link_name = "OBJC_CLASS_$_NSColor"]
static OBJC_CLASS__NSColor: Class;

#[link_name = "OBJC_CLASS_$_NSColorSpace"]
static OBJC_CLASS__NSColorSpace: Class;
}
use objc2_app_kit::{NSColor, NSColorSpace};
use std::ptr::null_mut;

pub fn accent() -> Option<Color> {
unsafe {
let accent: *mut Object = msg_send![&OBJC_CLASS__NSColor, controlAccentColor];
let color_space: *mut Object = msg_send![&OBJC_CLASS__NSColorSpace, genericRGBColorSpace];
let accent: *mut Object = msg_send![accent, colorUsingColorSpace: color_space];
let mut r: f64 = 0.0;
let mut g: f64 = 0.0;
let mut b: f64 = 0.0;
let _: () = msg_send![accent, getRed:addr_of_mut!(r) green:addr_of_mut!(g) blue:addr_of_mut!(b) alpha:null_mut::<f64>()];
Some(Color {
r: (r * 255.0) as u8,
g: (g * 255.0) as u8,
b: (b * 255.0) as u8,
let accent = NSColor::controlAccentColor();
let color_space = NSColorSpace::genericRGBColorSpace();
let accent = accent.colorUsingColorSpace(&color_space);
accent.map(|accent| {
let mut r: f64 = 0.0;
let mut g: f64 = 0.0;
let mut b: f64 = 0.0;
accent.getRed_green_blue_alpha(&mut r, &mut g, &mut b, null_mut());
Color {
r: (r * 255.0) as u8,
g: (g * 255.0) as u8,
b: (b * 255.0) as u8,
}
})
}
}
5 changes: 4 additions & 1 deletion netstatus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ windows = { workspace = true, features = [
] }

[target.'cfg(target_os = "macos")'.dependencies]
objc = { workspace = true }
objc2-core-wlan = { version = "0.2", features = [
"CWInterface",
"CWWiFiClient",
] }
system-configuration = { workspace = true }
core-foundation = { workspace = true }

Expand Down
30 changes: 9 additions & 21 deletions netstatus/src/sc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ use core_foundation::{
runloop::{kCFRunLoopDefaultMode, CFRunLoop, CFRunLoopRef},
};
use flume::{r#async::RecvStream, unbounded};
use objc::{
runtime::{Class, Object},
*,
};
use objc2_core_wlan::CWWiFiClient;
use pin_project::pin_project;
use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
Expand All @@ -18,24 +15,15 @@ use std::{
};
use system_configuration::network_reachability::{ReachabilityFlags, SCNetworkReachability};

#[link(name = "CoreWLAN", kind = "framework")]
extern "C" {
#[link_name = "OBJC_CLASS_$_CWWiFiClient"]
static OBJC_CLASS__CWWiFiClient: Class;
}

unsafe fn get_ssid() -> Option<String> {
let client: *mut Object = msg_send![&OBJC_CLASS__CWWiFiClient, sharedWiFiClient];
let interface: *mut Object = msg_send![client, interface];
let name: *mut Object = msg_send![interface, ssid];
if !name.is_null() {
let name = std::ffi::CStr::from_ptr(msg_send![name, UTF8String])
.to_string_lossy()
.into_owned();
Some(name)
} else {
None
}
CWWiFiClient::sharedWiFiClient()
.interface()
.and_then(|interface| interface.ssid())
.map(|name| {
std::ffi::CStr::from_ptr(name.UTF8String())
.to_string_lossy()
.into_owned()
})
}

pub fn current() -> NetStatus {
Expand Down

0 comments on commit a48ef68

Please sign in to comment.