Skip to content

feat: dynamic window level elevation #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2024
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
12 changes: 12 additions & 0 deletions apps/desktop/src-tauri/Cargo.lock

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

1 change: 1 addition & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ anyhow = "1.0.86"
objc = "0.2.7"
cocoa = "0.25.0"
tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v1" }
system-notification = { git = "https://github.com/ahkohd/tauri-toolkit", branch = "v1" }

[features]
# this feature is used for production builds or when `devPath` points to the filesystem
Expand Down
44 changes: 44 additions & 0 deletions apps/desktop/src-tauri/src/app_handle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use tauri::{AppHandle, Runtime};

#[cfg(target_os = "macos")]
use tauri_nspanel::{
cocoa::base::id,
objc::{class, msg_send, sel, sel_impl},
};

#[cfg(target_os = "macos")]
fn nsstring_to_string(ns_string: id) -> Option<String> {
use std::ffi::{c_char, CStr};

let utf8: id = unsafe { msg_send![ns_string, UTF8String] };

if !utf8.is_null() {
Some(unsafe {
{
CStr::from_ptr(utf8 as *const c_char)
.to_string_lossy()
.into_owned()
}
})
} else {
None
}
}

#[cfg(target_os = "macos")]
pub trait AppHandleExt {
fn frontmost_application_bundle_id() -> Option<String>;
}

#[cfg(target_os = "macos")]
impl<R: Runtime> AppHandleExt for AppHandle<R> {
fn frontmost_application_bundle_id() -> Option<String> {
let workspace: id = unsafe { msg_send![class!(NSWorkspace), sharedWorkspace] };

let frontmost_application: id = unsafe { msg_send![workspace, frontmostApplication] };

let bundle_id: id = unsafe { msg_send![frontmost_application, bundleIdentifier] };

nsstring_to_string(bundle_id)
}
}
4 changes: 3 additions & 1 deletion apps/desktop/src-tauri/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ pub const SHOW_UPDATE_MODAL: &str = "show_update_modal";

/// window levels
// NOTE: league sets it's window to 1000 so we go one higher
pub const HIGHER_LEVEL_THAN_LEAGUE: i32 = 1001;
pub static HIGHER_LEVEL_THAN_LEAGUE: i32 = 1001;
/// Float panel window level
pub static OVERLAYED_NORMAL_LEVEL: i32 = 8;
33 changes: 32 additions & 1 deletion apps/desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#[macro_use]
extern crate objc;

mod app_handle;
mod commands;
mod constants;
mod tray;
Expand All @@ -21,19 +22,49 @@ use tauri_plugin_window_state::StateFlags;
use tray::Tray;
use window_custom::WindowExt;

#[cfg(target_os = "macos")]
use app_handle::AppHandleExt;

#[cfg(target_os = "macos")]
use window_custom::macos::WindowExtMacos;

#[cfg(target_os = "macos")]
use system_notification::WorkspaceListener;

#[cfg(target_os = "macos")]
use tauri::Window;

pub struct Pinned(AtomicBool);

#[cfg(target_os = "macos")]
fn apply_macos_specifics(window: &Window) {
use tauri::{AppHandle, Wry};
use tauri_nspanel::ManagerExt;

window.remove_shadow();

window.set_float_panel(constants::HIGHER_LEVEL_THAN_LEAGUE);
window.set_float_panel(constants::OVERLAYED_NORMAL_LEVEL);

let app_handle = window.app_handle();

app_handle.listen_workspace(
"NSWorkspaceDidActivateApplicationNotification",
|app_handle| {
let bundle_id = AppHandle::<Wry>::frontmost_application_bundle_id();

if let Some(bundle_id) = bundle_id {
let is_league_of_legends = bundle_id == "com.riotgames.leagueoflegends";

let panel = app_handle.get_panel("main").unwrap();

panel.set_level(if is_league_of_legends {
constants::HIGHER_LEVEL_THAN_LEAGUE
} else {
constants::OVERLAYED_NORMAL_LEVEL
});
}
},
);
}

fn main() {
Expand Down