Skip to content

Commit

Permalink
feat: add tauri_nspanel
Browse files Browse the repository at this point in the history
feat: add window.set_float_panel

feat: make main window always draw over other fullscreen apps
  • Loading branch information
ahkohd committed Jul 6, 2024
1 parent b856eab commit d83da2b
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 55 deletions.
17 changes: 17 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 @@ -46,6 +46,7 @@ tokio = { version = "1.38.0", features = [
tauri-plugin-websocket = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-window-state = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-nspanel = { git = "https://github.com/ahkohd/tauri-nspanel", branch = "v1" }
anyhow = "1.0.86"

# macos dependencies
Expand Down
8 changes: 0 additions & 8 deletions apps/desktop/src-tauri/Info.plist

This file was deleted.

52 changes: 31 additions & 21 deletions apps/desktop/src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ pub struct Pinned(AtomicBool);

#[cfg(target_os = "macos")]
fn apply_macos_specifics(window: &Window) {
window.set_visisble_on_all_workspaces(true);
window.set_transparent_titlebar(true, true);

window.set_float_panel();
}

fn main() {
Expand All @@ -41,13 +42,21 @@ fn main() {
flags.remove(StateFlags::VISIBLE);

let window_state_plugin = tauri_plugin_window_state::Builder::default().with_state_flags(flags);
let app = tauri::Builder::default()

let mut app = tauri::Builder::default()
// TODO: this should work on windows
.plugin(window_state_plugin.build())
.plugin(tauri_plugin_websocket::init())
.plugin(tauri_plugin_single_instance::init(|app, argv, cwd| {
println!("{}, {argv:?}, {cwd}", app.package_info().name);
}))
}));

#[cfg(target_os = "macos")]
{
app = app.plugin(tauri_nspanel::init());
}

app = app
.manage(Pinned(AtomicBool::new(false)))
.setup(|app| {
let window = app.get_window(MAIN_WINDOW_NAME).unwrap();
Expand Down Expand Up @@ -95,24 +104,25 @@ fn main() {
open_devtools,
close_settings,
open_settings
])
]);

app
.build(tauri::generate_context!())
.expect("An error occured while running the app!");

app.run(|app, event| match event {
tauri::RunEvent::WindowEvent {
label,
event: win_event,
..
} => match win_event {
// NOTE: prevent destroying the window
tauri::WindowEvent::CloseRequested { api, .. } => {
let win = app.get_window(label.as_str()).unwrap();
win.hide().unwrap();
api.prevent_close();
}
.expect("An error occured while running the app!")
.run(|app, event| match event {
tauri::RunEvent::WindowEvent {
label,
event: win_event,
..
} => match win_event {
// NOTE: prevent destroying the window
tauri::WindowEvent::CloseRequested { api, .. } => {
let win = app.get_window(label.as_str()).unwrap();
win.hide().unwrap();
api.prevent_close();
}
_ => {}
},
_ => {}
},
_ => {}
})
});
}
48 changes: 22 additions & 26 deletions apps/desktop/src-tauri/src/window_custom.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,19 @@
#[cfg(target_os = "macos")]
pub mod macos {
use cocoa::{
appkit::{
NSMainMenuWindowLevel, NSWindow, NSWindowButton, NSWindowCollectionBehavior,
NSWindowStyleMask, NSWindowTitleVisibility,
},
base::id,
foundation::NSInteger,
use cocoa::appkit::{
NSMainMenuWindowLevel, NSWindow, NSWindowButton, NSWindowCollectionBehavior, NSWindowStyleMask,
NSWindowTitleVisibility,
};
use objc::{msg_send, runtime::YES};
use tauri::{Runtime, Window};
use tauri_nspanel::WindowExt;

pub trait WindowExtMacos {
fn set_transparent_titlebar(&self, title_transparent: bool, remove_toolbar: bool);
fn set_visisble_on_all_workspaces(&self, enabled: bool);
fn set_float_panel(&self);
}

impl<R: Runtime> WindowExtMacos for Window<R> {
fn set_visisble_on_all_workspaces(&self, enabled: bool) {
const HIGHER_LEVEL_THAN_LEAGUE: NSInteger = 1001;
unsafe {
let ns_win = self.ns_window().unwrap() as id;

if enabled {
ns_win.setLevel_(HIGHER_LEVEL_THAN_LEAGUE);
ns_win.setCollectionBehavior_(
NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces,
);
} else {
ns_win.setLevel_(((NSMainMenuWindowLevel - 1) as u64).try_into().unwrap());
ns_win
.setCollectionBehavior_(NSWindowCollectionBehavior::NSWindowCollectionBehaviorDefault);
}
}
}

fn set_transparent_titlebar(&self, title_transparent: bool, remove_tool_bar: bool) {
unsafe {
let id = self.ns_window().unwrap() as cocoa::base::id;
Expand Down Expand Up @@ -72,6 +51,23 @@ pub mod macos {
});
}
}

fn set_float_panel(&self) {
let panel = self.to_panel().unwrap();

panel.set_level(NSMainMenuWindowLevel + 1);

#[allow(non_upper_case_globals)]
const NSWindowStyleMaskNonActivatingPanel: i32 = 1 << 7;

panel.set_style_mask(NSWindowStyleMaskNonActivatingPanel);

panel.set_collection_behaviour(
NSWindowCollectionBehavior::NSWindowCollectionBehaviorCanJoinAllSpaces
| NSWindowCollectionBehavior::NSWindowCollectionBehaviorStationary
| NSWindowCollectionBehavior::NSWindowCollectionBehaviorFullScreenAuxiliary,
);
}
}
}

Expand Down

0 comments on commit d83da2b

Please sign in to comment.