Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.lock

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

16 changes: 11 additions & 5 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use ext::*;
use store::*;

use tauri::Manager;

Check warning on line 11 in apps/desktop/src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / desktop_ci (linux, depot-ubuntu-22.04-8)

unused import: `tauri::Manager`

Check warning on line 11 in apps/desktop/src-tauri/src/lib.rs

View workflow job for this annotation

GitHub Actions / desktop_ci (linux, depot-ubuntu-22.04-8)

unused import: `tauri::Manager`
use tauri_plugin_permissions::{Permission, PermissionsPluginExt};
use tauri_plugin_windows::{AppWindow, WindowsPluginExt};

Expand Down Expand Up @@ -241,6 +241,17 @@
#[cfg(target_os = "macos")]
hypr_intercept::setup_force_quit_handler();

#[cfg(target_os = "macos")]
{
let handle = app.handle().clone();
hypr_intercept::set_close_handler(move || {
for (_, window) in handle.webview_windows() {
let _ = window.close();
}
let _ = handle.set_activation_policy(tauri::ActivationPolicy::Accessory);
});
}

#[allow(unused_variables)]
app.run(move |app, event| match event {
#[cfg(target_os = "macos")]
Expand All @@ -260,11 +271,6 @@
}

api.prevent_exit();

for (_, window) in app.webview_windows() {
let _ = window.close();
}

let _ = app.set_activation_policy(tauri::ActivationPolicy::Accessory);
}
tauri::RunEvent::Exit => {
Expand Down
12 changes: 12 additions & 0 deletions crates/intercept/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,20 @@ name = "intercept"
version = "0.1.0"
edition = "2024"

[features]
examples = []

[[example]]
name = "test_force_quit"
required-features = ["examples"]

[target.'cfg(target_os = "macos")'.build-dependencies]
swift-rs = { workspace = true, features = ["build"] }

[target.'cfg(target_os = "macos")'.dependencies]
swift-rs = { workspace = true }

[target.'cfg(target_os = "macos")'.dev-dependencies]
objc2 = { workspace = true }
objc2-app-kit = { workspace = true }
objc2-foundation = { workspace = true }
46 changes: 46 additions & 0 deletions crates/intercept/examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use objc2::rc::Retained;
use objc2::runtime::ProtocolObject;
use objc2::{MainThreadOnly, define_class, msg_send};
use objc2_app_kit::{
NSAppearance, NSApplication, NSApplicationActivationPolicy, NSApplicationDelegate,
};
use objc2_foundation::{MainThreadMarker, NSObject, NSObjectProtocol, ns_string};

#[derive(Debug, Default)]
struct AppDelegateIvars {}

define_class! {
#[unsafe(super = NSObject)]
#[thread_kind = MainThreadOnly]
#[name = "AppDelegate"]
#[ivars = AppDelegateIvars]
struct AppDelegate;

unsafe impl NSObjectProtocol for AppDelegate {}
unsafe impl NSApplicationDelegate for AppDelegate {}
}

impl AppDelegate {
fn new(mtm: MainThreadMarker) -> Retained<Self> {
let this = Self::alloc(mtm).set_ivars(AppDelegateIvars::default());
unsafe { msg_send![super(this), init] }
}
}

pub fn run_app(f: impl FnOnce() + Send + 'static) {
let mtm = MainThreadMarker::new().unwrap();

let app = NSApplication::sharedApplication(mtm);
app.setActivationPolicy(NSApplicationActivationPolicy::Regular);

if let Some(appearance) = NSAppearance::appearanceNamed(ns_string!("NSAppearanceNameAqua")) {
app.setAppearance(Some(&appearance));
}

let delegate = AppDelegate::new(mtm);
app.setDelegate(Some(&ProtocolObject::from_ref(&*delegate)));

std::thread::spawn(f);

app.run();
}
13 changes: 13 additions & 0 deletions crates/intercept/examples/test_force_quit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod common;

use intercept::*;

fn main() {
common::run_app(|| {
std::thread::sleep(std::time::Duration::from_millis(200));
show_quit_overlay();

std::thread::sleep(std::time::Duration::from_secs(10));
std::process::exit(0);
});
}
31 changes: 30 additions & 1 deletion crates/intercept/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
#[cfg(target_os = "macos")]
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{
OnceLock,
atomic::{AtomicBool, Ordering},
};

#[cfg(target_os = "macos")]
use swift_rs::swift;

#[cfg(target_os = "macos")]
swift!(fn _setup_force_quit_handler());

#[cfg(target_os = "macos")]
swift!(fn _show_quit_overlay());

#[cfg(target_os = "macos")]
static HANDLER_INITIALIZED: AtomicBool = AtomicBool::new(false);

#[cfg(target_os = "macos")]
static FORCE_QUIT: AtomicBool = AtomicBool::new(false);

#[cfg(target_os = "macos")]
static CLOSE_HANDLER: OnceLock<Box<dyn Fn() + Send + Sync>> = OnceLock::new();

#[cfg(target_os = "macos")]
pub fn setup_force_quit_handler() {
if !HANDLER_INITIALIZED.swap(true, Ordering::SeqCst) {
Expand All @@ -22,6 +31,11 @@ pub fn setup_force_quit_handler() {
}
}

#[cfg(target_os = "macos")]
pub fn set_close_handler(f: impl Fn() + Send + Sync + 'static) {
let _ = CLOSE_HANDLER.set(Box::new(f));
}

#[cfg(target_os = "macos")]
pub fn should_force_quit() -> bool {
FORCE_QUIT.load(Ordering::SeqCst)
Expand All @@ -32,8 +46,23 @@ pub fn set_force_quit() {
FORCE_QUIT.store(true, Ordering::SeqCst);
}

#[cfg(target_os = "macos")]
pub fn show_quit_overlay() {
unsafe {
_show_quit_overlay();
}
}

#[unsafe(no_mangle)]
#[cfg(target_os = "macos")]
pub extern "C" fn rust_set_force_quit() {
FORCE_QUIT.store(true, Ordering::SeqCst);
}

#[unsafe(no_mangle)]
#[cfg(target_os = "macos")]
pub extern "C" fn rust_perform_close() {
if let Some(handler) = CLOSE_HANDLER.get() {
handler();
}
}
Loading
Loading