Skip to content

Commit eba33e1

Browse files
authored
Add experimental macOS notch (#1541)
1 parent d023265 commit eba33e1

File tree

13 files changed

+337
-0
lines changed

13 files changed

+337
-0
lines changed

.swift-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
6.1.2

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ hypr-loops = { path = "crates/loops", package = "loops" }
5454
hypr-moonshine = { path = "crates/moonshine", package = "moonshine" }
5555
hypr-nango = { path = "crates/nango", package = "nango" }
5656
hypr-network = { path = "crates/network", package = "network" }
57+
hypr-notch = { path = "crates/notch", package = "notch" }
5758
hypr-notification = { path = "crates/notification", package = "notification" }
5859
hypr-notification-interface = { path = "crates/notification-interface", package = "notification-interface" }
5960
hypr-notification-macos = { path = "crates/notification-macos", package = "notification-macos" }

crates/notch/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "notch"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[target.'cfg(target_os = "macos")'.build-dependencies]
7+
swift-rs = { workspace = true, features = ["build"] }
8+
9+
[target.'cfg(target_os = "macos")'.dependencies]
10+
swift-rs = { workspace = true }
11+
12+
[dev-dependencies]
13+
objc2 = { workspace = true }
14+
objc2-app-kit = { workspace = true }
15+
objc2-foundation = { workspace = true }

crates/notch/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```bash
2+
cargo run --example test_notch -p notch
3+
```

crates/notch/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
fn main() {
2+
#[cfg(target_os = "macos")]
3+
{
4+
swift_rs::SwiftLinker::new("14.2")
5+
.with_package("swift-lib", "./swift-lib/")
6+
.link();
7+
}
8+
9+
#[cfg(not(target_os = "macos"))]
10+
{
11+
println!("cargo:warning=Swift linking is only available on macOS");
12+
}
13+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use notch::*;
2+
3+
use std::time::Duration;
4+
5+
use objc2::rc::Retained;
6+
use objc2::runtime::ProtocolObject;
7+
use objc2::{define_class, msg_send, MainThreadOnly};
8+
use objc2_app_kit::{
9+
NSAppearance, NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate,
10+
};
11+
use objc2_foundation::{ns_string, MainThreadMarker, NSObject, NSObjectProtocol};
12+
13+
#[derive(Debug, Default)]
14+
struct AppDelegateIvars {}
15+
16+
define_class! {
17+
#[unsafe(super = NSObject)]
18+
#[thread_kind = MainThreadOnly]
19+
#[name = "AppDelegate"]
20+
#[ivars = AppDelegateIvars]
21+
struct AppDelegate;
22+
23+
unsafe impl NSObjectProtocol for AppDelegate {}
24+
unsafe impl NSApplicationDelegate for AppDelegate {}
25+
}
26+
27+
impl AppDelegate {
28+
fn new(mtm: MainThreadMarker) -> Retained<Self> {
29+
let this = Self::alloc(mtm).set_ivars(AppDelegateIvars::default());
30+
unsafe { msg_send![super(this), init] }
31+
}
32+
}
33+
34+
fn main() {
35+
let mtm = MainThreadMarker::new().unwrap();
36+
37+
let app = NSApplication::sharedApplication(mtm);
38+
app.setActivationPolicy(NSApplicationActivationPolicy::Regular);
39+
40+
if let Some(appearance) = NSAppearance::appearanceNamed(ns_string!("NSAppearanceNameAqua")) {
41+
app.setAppearance(Some(&appearance));
42+
}
43+
44+
let delegate = AppDelegate::new(mtm);
45+
app.setDelegate(Some(&ProtocolObject::from_ref(&*delegate)));
46+
47+
std::thread::spawn(|| {
48+
std::thread::sleep(Duration::from_millis(200));
49+
50+
show_notch(
51+
"Hello from Notch!",
52+
"This is a test notification in the dynamic notch",
53+
"bell.fill",
54+
);
55+
56+
std::thread::sleep(Duration::from_secs(10));
57+
hide_notch();
58+
59+
std::thread::sleep(Duration::from_secs(1));
60+
std::process::exit(0);
61+
});
62+
63+
app.run();
64+
}

crates/notch/src/lib.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#[cfg(target_os = "macos")]
2+
use swift_rs::{swift, SRString};
3+
4+
#[cfg(target_os = "macos")]
5+
swift!(fn notch_show_info(title: SRString, description: SRString, icon_name: SRString));
6+
7+
#[cfg(target_os = "macos")]
8+
swift!(fn notch_hide());
9+
10+
#[cfg(target_os = "macos")]
11+
swift!(fn notch_compact());
12+
13+
#[cfg(target_os = "macos")]
14+
pub fn show_notch(title: &str, description: &str, icon_name: &str) {
15+
unsafe {
16+
notch_show_info(
17+
SRString::from(title),
18+
SRString::from(description),
19+
SRString::from(icon_name),
20+
);
21+
}
22+
}
23+
24+
#[cfg(target_os = "macos")]
25+
pub fn hide_notch() {
26+
unsafe {
27+
notch_hide();
28+
}
29+
}
30+
31+
#[cfg(target_os = "macos")]
32+
pub fn compact_notch() {
33+
unsafe {
34+
notch_compact();
35+
}
36+
}
37+
38+
#[cfg(test)]
39+
mod tests {
40+
use super::*;
41+
}

crates/notch/swift-lib/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
xcuserdata/
6+
DerivedData/
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

crates/notch/swift-lib/Package.resolved

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)