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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"scripts": {
"ng": "ng",
"start": "npm run tauri:serve",
"web:serve": "ng serve -o",
"web:serve": "ng serve",
"web:build": "ng build --base-href ./",
"web:dev": "npm run web:build",
"web:prod": "npm run web:build -- -c production",
Expand Down
66 changes: 66 additions & 0 deletions src-tauri/Cargo.lock

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

3 changes: 3 additions & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ tauri-plugin-single-instance = "^2.0"
tauri-plugin-autostart = "^2.0"
urlencoding = "^2.1"

[target.'cfg(target_os = "macos")'.dependencies]
objc2-app-kit = "0.3.2"

[features]
# by default Tauri runs in production mode
# when `tauri dev` runs it is executed with `cargo run --no-default-features` if `devPath` is an URL
Expand Down
51 changes: 14 additions & 37 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ mod updater;
use config::load_config;
use state::AppState;
use tray::generate_tray_icon;
use window::{position_window_near_tray, handle_window_focus_lost, handle_window_show};
use window::{position_window_near_tray, set_macos_window_level};

fn main() {
// Load environment variables from .env file
Expand Down Expand Up @@ -65,12 +65,14 @@ fn main() {
))
.manage(AppState {
alert_count: Mutex::new(0),
last_shown: Mutex::new(None),
last_focus_lost: Mutex::new(None),
auto_hide_paused: Mutex::new(false),
config: Mutex::new(config),
})
.setup(|app| {
// On macOS, use Accessory policy so the window floats over all Spaces
// and is not bound to a single Space like a regular app window.
#[cfg(target_os = "macos")]
app.set_activation_policy(tauri::ActivationPolicy::Accessory);

// Enable autostart on first run
use tauri_plugin_autostart::ManagerExt;
let autostart_manager = app.autolaunch();
Expand All @@ -96,26 +98,13 @@ fn main() {
let icon_data = generate_tray_icon(None, has_repos);
let icon = Image::from_bytes(&icon_data)?;

// Check if user is authenticated
let is_authenticated = {
let state = app.state::<AppState>();
let config = state.config.lock().unwrap();
config.access_token.as_ref()
.map(|t| !t.trim().is_empty())
.unwrap_or(false)
};

// Show window if not authenticated, hide if already logged in
// Always show the window on startup — Angular handles the correct
// view (login vs alerts) based on auth status via checkAuthStatus()
if let Some(window) = app.get_webview_window("main") {
if is_authenticated {
let _ = window.hide();
} else {
// First time - show window for login
handle_window_show(app.handle());
position_window_near_tray(&window);
let _ = window.show();
let _ = window.set_focus();
}
set_macos_window_level(&window);
position_window_near_tray(&window);
let _ = window.show();
let _ = window.set_focus();
}


Expand Down Expand Up @@ -165,7 +154,7 @@ fn main() {
if window.is_visible().unwrap_or(false) {
let _ = window.hide();
} else {
handle_window_show(&app);
set_macos_window_level(&window);
position_window_near_tray(&window);
let _ = window.show();
let _ = window.set_focus();
Expand All @@ -179,21 +168,11 @@ fn main() {
Ok(())
})
.on_window_event(|window, event| {
// Intercept close — hide instead of quit (tray app)
if let tauri::WindowEvent::CloseRequested { api, .. } = event {
let _ = window.hide();
api.prevent_close();
}
if let tauri::WindowEvent::Focused(focused) = event {
if !*focused {
handle_window_focus_lost(window);
} else {
// Window regained focus - clear the focus lost timestamp
if let Some(state) = window.app_handle().try_state::<AppState>() {
let mut last_focus_lost = state.last_focus_lost.lock().unwrap();
*last_focus_lost = None;
}
}
}
})
.invoke_handler(tauri::generate_handler![
auth::set_token,
Expand All @@ -209,8 +188,6 @@ fn main() {
alerts::get_github_security_alerts,
tray::update_tray_icon,
system::open_taskbar_settings,
window::pause_auto_hide,
window::resume_auto_hide,
updater::check_for_updates,
updater::install_update,
updater::get_current_version,
Expand Down
4 changes: 0 additions & 4 deletions src-tauri/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@
*/

use std::sync::Mutex;
use std::time::Instant;
use crate::models::AppConfig;

pub struct AppState {
pub alert_count: Mutex<usize>,
pub last_shown: Mutex<Option<Instant>>,
pub last_focus_lost: Mutex<Option<Instant>>,
pub auto_hide_paused: Mutex<bool>,
pub config: Mutex<AppConfig>,
}
144 changes: 25 additions & 119 deletions src-tauri/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
* Software description: A modern desktop application that monitors security vulnerabilities across your GitHub repositories in real-time.
*/

use tauri::{PhysicalPosition, Manager, LogicalSize};
use std::time::Instant;
use crate::state::AppState;
use tauri::{PhysicalPosition, LogicalSize};

// ============================================================================
// Window Management
Expand Down Expand Up @@ -51,125 +49,33 @@ pub fn position_window_near_tray(window: &tauri::WebviewWindow) {
}
}

pub fn handle_window_focus_lost(window: &tauri::Window) {
let app = window.app_handle();

// Check if auto-hide is paused (for dropdown interactions)
let is_paused = if let Some(state) = app.try_state::<AppState>() {
let auto_hide_paused = state.auto_hide_paused.lock().unwrap();
*auto_hide_paused
} else {
false
};

if is_paused {
println!("[WINDOW] Auto-hide paused - ignoring focus loss");
return;
}

// On Linux, use a delayed hide approach to handle dropdown interactions
#[cfg(target_os = "linux")]
{
let should_hide = if let Some(state) = app.try_state::<AppState>() {
let last_shown = state.last_shown.lock().unwrap();
if let Some(instant) = *last_shown {
instant.elapsed().as_millis() > 1000 // 1 second minimum on Linux
} else {
true
}
} else {
true
};

if should_hide {
let window_clone = window.clone();
let app_clone = app.clone();

// Store the focus lost time
if let Some(state) = app.try_state::<AppState>() {
let mut last_focus_lost = state.last_focus_lost.lock().unwrap();
*last_focus_lost = Some(Instant::now());
}

std::thread::spawn(move || {
std::thread::sleep(std::time::Duration::from_millis(300)); // Wait 300ms

// Check if auto-hide is still not paused and focus wasn't regained
let should_still_hide = if let Some(state) = app_clone.try_state::<AppState>() {
let auto_hide_paused = state.auto_hide_paused.lock().unwrap();
if *auto_hide_paused {
return; // Auto-hide was paused during the delay
}

let last_focus_lost = state.last_focus_lost.lock().unwrap();
if let Some(focus_lost_time) = *last_focus_lost {
// If more than 300ms have passed since focus lost and focus wasn't regained, hide
focus_lost_time.elapsed().as_millis() >= 300
} else {
false // Focus was regained
}
} else {
true
};

if should_still_hide {
if let Ok(is_focused) = window_clone.is_focused() {
if !is_focused {
let _ = window_clone.hide();
}
}
}
});
}
}

// On other platforms, use the original logic
#[cfg(not(target_os = "linux"))]
{
let should_hide = if let Some(state) = app.try_state::<AppState>() {
let last_shown = state.last_shown.lock().unwrap();
if let Some(instant) = *last_shown {
instant.elapsed().as_millis() > 500
} else {
true
}
} else {
true
};

if should_hide {
let _ = window.hide();
}
}
}

pub fn handle_window_show(app: &tauri::AppHandle) {
if let Some(state) = app.try_state::<AppState>() {
let mut last_shown = state.last_shown.lock().unwrap();
*last_shown = Some(Instant::now());
}
}

// ============================================================================
// Focus Management Commands (Linux dropdown fix)
// macOS Window Configuration
// ============================================================================

#[tauri::command]
pub fn pause_auto_hide(app: tauri::AppHandle) -> Result<(), String> {
if let Some(state) = app.try_state::<AppState>() {
let mut auto_hide_paused = state.auto_hide_paused.lock().unwrap();
*auto_hide_paused = true;
println!("[WINDOW] Auto-hide paused");
/// Configure the window for macOS tray-app behavior:
/// - Visible on all Spaces (never swept away by swipe gestures)
/// - Does not auto-hide when the app loses focus
#[cfg(target_os = "macos")]
pub fn set_macos_window_level(window: &tauri::WebviewWindow) {
use objc2_app_kit::NSWindow;

// Visible on all Spaces via Tauri native API
// (sets NSWindowCollectionBehaviorCanJoinAllSpaces under the hood)
let _ = window.set_visible_on_all_workspaces(true);

// setHidesOnDeactivate is not exposed by Tauri — call via objc2-app-kit.
// Prevents macOS from auto-hiding the window when the app loses focus.
unsafe {
let ns_window: &NSWindow = &*window
.ns_window()
.expect("Failed to get NSWindow handle")
.cast();
ns_window.setHidesOnDeactivate(false);
}
Ok(())
}

#[tauri::command]
pub fn resume_auto_hide(app: tauri::AppHandle) -> Result<(), String> {
if let Some(state) = app.try_state::<AppState>() {
let mut auto_hide_paused = state.auto_hide_paused.lock().unwrap();
*auto_hide_paused = false;
println!("[WINDOW] Auto-hide resumed");
}
Ok(())
}
#[cfg(not(target_os = "macos"))]
pub fn set_macos_window_level(_window: &tauri::WebviewWindow) {
// No-op on non-macOS platforms
}
3 changes: 2 additions & 1 deletion src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"visible": false,
"skipTaskbar": true,
"center": false,
"devtools": true
"devtools": true,
"visibleOnAllWorkspaces": true
}
],
"security": {
Expand Down
Loading