Skip to content

Commit

Permalink
feat: support for listening to macos ns_panel window move and resize …
Browse files Browse the repository at this point in the history
…events (#181)
  • Loading branch information
ayangweb authored Feb 24, 2025
1 parent aba3c92 commit c52a473
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions src-tauri/src/setup/mac.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
//credits to: https://github.com/ayangweb/ayangweb-EcoPaste/blob/169323dbe6365ffe4abb64d867439ed2ea84c6d1/src-tauri/src/core/setup/mac.rs
use tauri::{ActivationPolicy, App, Emitter, Manager, WebviewWindow};
use tauri::{ActivationPolicy, App, Emitter, EventTarget, WebviewWindow};
use tauri_nspanel::{
cocoa::appkit::{NSMainMenuWindowLevel, NSWindowCollectionBehavior},
panel_delegate, WebviewWindowExt,
};

use crate::common::MAIN_WINDOW_LABEL;

#[allow(non_upper_case_globals)]
const NSWindowStyleMaskNonActivatingPanel: i32 = 1 << 7;
#[allow(non_upper_case_globals)]
const NSResizableWindowMask: i32 = 1 << 3;
const MACOS_PANEL_FOCUS: &str = "macos-panel-focus";
const WINDOW_FOCUS_EVENT: &str = "tauri://focus";
const WINDOW_BLUR_EVENT: &str = "tauri://blur";
const WINDOW_MOVED_EVENT: &str = "tauri://move";
const WINDOW_RESIZED_EVENT: &str = "tauri://resize";

pub fn platform(app: &mut App, main_window: WebviewWindow, _settings_window: WebviewWindow) {
let app_handle = app.app_handle().clone();

app.set_activation_policy(ActivationPolicy::Accessory);

// Convert ns_window to ns_panel
Expand All @@ -35,24 +38,44 @@ pub fn platform(app: &mut App, main_window: WebviewWindow, _settings_window: Web
// Define the panel's delegate to listen to panel window events
let delegate = panel_delegate!(EcoPanelDelegate {
window_did_become_key,
window_did_resign_key
window_did_resign_key,
window_did_resize,
window_did_move
});

// Set event listeners for the delegate
delegate.set_listener(Box::new(move |delegate_name: String| {
let target = EventTarget::labeled(MAIN_WINDOW_LABEL);

let window_move_event = || {
if let Ok(position) = main_window.outer_position() {
let _ = main_window.emit_to(target.clone(), WINDOW_MOVED_EVENT, position);
}
};

match delegate_name.as_str() {
// Called when the window gains keyboard focus
// Called when the window gets keyboard focus
"window_did_become_key" => {
app_handle.emit(MACOS_PANEL_FOCUS, true).unwrap();
let _ = main_window.emit_to(target, WINDOW_FOCUS_EVENT, true);
}
// Called when the window loses keyboard focus
"window_did_resign_key" => {
app_handle.emit(MACOS_PANEL_FOCUS, false).unwrap();
let _ = main_window.emit_to(target, WINDOW_BLUR_EVENT, true);
}
// Called when the window size changes
"window_did_resize" => {
window_move_event();

if let Ok(size) = main_window.inner_size() {
let _ = main_window.emit_to(target, WINDOW_RESIZED_EVENT, size);
}
}
// Called when the window position changes
"window_did_move" => window_move_event(),
_ => (),
}
}));

// Set the delegate object for the window to handle window events
panel.set_delegate(delegate);
}
}

0 comments on commit c52a473

Please sign in to comment.