Skip to content

Commit

Permalink
fix(startup): tray icon creation panic crash
Browse files Browse the repository at this point in the history
  • Loading branch information
eythaann committed Jul 26, 2024
1 parent 62789bd commit cb7f388
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 20 deletions.
5 changes: 4 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog

## [Unreleased]
### fix
- app not running on startup

## [1.8.7]
### Fix
### fix
- no updating themes on changes saved.

## [1.8.6]
Expand Down
17 changes: 6 additions & 11 deletions src/background/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ use modules::{
};
use plugins::register_plugins;
use seelen::SEELEN;

use tray::handle_tray_icon;
use tray::try_register_tray_icon;

fn register_panic_hook() {
std::panic::set_hook(Box::new(|info| {
Expand Down Expand Up @@ -76,19 +75,18 @@ fn setup(app: &mut tauri::App<tauri::Wry>) -> Result<(), Box<dyn std::error::Err
let mut seelen = unsafe { SEELEN.make_guard_unchecked() };
seelen.init(app.handle().clone())?;

handle_tray_icon(app)?;

if !tauri::is_dev() {
seelen.create_update_modal()?;
log_error!(seelen.create_update_modal());

let command = SEELEN_COMMAND_LINE.lock().clone();
let matches = command.get_matches();
if !matches.get_flag("silent") {
seelen.show_settings()?;
log_error!(seelen.show_settings());
}
}

seelen.start()?;
log_error!(try_register_tray_icon(app));
std::mem::forget(seelen);
Ok(())
}
Expand All @@ -102,11 +100,8 @@ fn app_callback(_: &tauri::AppHandle<tauri::Wry>, event: tauri::RunEvent) {
}
}
tauri::RunEvent::Exit => {
let seelen = trace_lock!(SEELEN);
if seelen.initialized {
log::info!("───────────────────── Exiting Seelen ─────────────────────");
seelen.stop()
}
log::info!("───────────────────── Exiting Seelen ─────────────────────");
trace_lock!(SEELEN).stop()
}
_ => {}
}
Expand Down
10 changes: 3 additions & 7 deletions src/background/seelen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ pub struct Seelen {
shell: Option<SeelenShell>,
#[getset(get = "pub")]
state: State,
pub initialized: bool,
}

/* ============== Getters ============== */
Expand Down Expand Up @@ -108,20 +107,18 @@ impl Seelen {
if self.state.is_shell_enabled() {
self.shell = Some(SeelenShell::new(app.clone()));
}

self.initialized = true;
Ok(())
}

pub fn start(&mut self) -> Result<()> {
if self.state.is_weg_enabled() {
SeelenWeg::hide_taskbar(true)?;
//SeelenWeg::hide_taskbar(true)?;
}

self.start_ahk_shortcuts()?;
declare_system_events_handlers()?;

std::thread::spawn(|| -> Result<()> {
std::thread::spawn(|| {
log::trace!("Enumerating Monitors");
WindowsApi::enum_display_monitors(Some(Self::enum_monitors_proc), 0)
.expect("Failed to enum monitors");
Expand All @@ -136,8 +133,7 @@ impl Seelen {
WindowsApi::enum_windows(Some(Self::enum_windows_proc), 0)
.expect("Failed to enum windows");

register_win_hook()?;
Ok(())
register_win_hook().expect("Failed to register windows hook");
});

if WindowsApi::is_elevated()? {
Expand Down
20 changes: 19 additions & 1 deletion src/background/tray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ use tauri::{

use crate::error_handler::Result;
use crate::seelen::SEELEN;
use crate::utils::sleep_millis;
use crate::{log_error, trace_lock};

pub fn handle_tray_icon(app: &mut App) -> Result<()> {
pub fn try_register_tray_icon(app: &mut App) -> Result<()> {
log::trace!("registering tray icon");
let mut attempts = 0;

// normally tray icon creation not fails but on windows startup
// it could fail until some processes are started
while let Err(e) = register_tray_icon(app) {
if attempts >= 10 {
return Err(e);
}
attempts += 1;
sleep_millis(100);
}

Ok(())
}

fn register_tray_icon(app: &mut App) -> Result<()> {
log::trace!("registering tray icon");
let settings = MenuItemBuilder::with_id("settings", "Open Settings").build(app)?;

Expand Down

0 comments on commit cb7f388

Please sign in to comment.