fix error popup on startup

This commit is contained in:
2024-06-03 01:21:43 -04:00
parent 816bd7db00
commit 0491cb5790
9 changed files with 74 additions and 103 deletions

View File

@ -2,6 +2,10 @@ use std::error::Error;
use std::time::Duration;
use once_cell::sync::OnceCell;
use rfd::{
MessageDialog,
MessageLevel,
};
use sqlx::{
SqlitePool,
sqlite::SqlitePoolOptions,
@ -10,9 +14,12 @@ use sqlx::{
use tauri::{
App,
AppHandle,
Manager,
async_runtime as rt,
Manager,
RunEvent,
WindowEvent,
};
use tauri::menu::MenuItem;
use crate::{
config::{self, AppConfig},
@ -33,7 +40,7 @@ pub fn run() -> tauri::Result<()> {
tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
show_main_window(app)
.blocking_error_popup("Failed to show main window")
.error_popup("Failed to show main window")
}))
.plugin(tauri_plugin_global_shortcut::Builder::default().build())
.invoke_handler(tauri::generate_handler![
@ -47,17 +54,25 @@ pub fn run() -> tauri::Result<()> {
ipc::launch_terminal,
ipc::get_setup_errors,
])
.setup(|app| rt::block_on(setup(app)))
.setup(|app| {
let res = rt::block_on(setup(app));
if let Err(ref e) = res {
MessageDialog::new()
.set_level(MessageLevel::Error)
.set_title("Creddy failed to start")
.set_description(format!("{e}"))
.show();
}
res
})
.build(tauri::generate_context!())?
.run(|app, run_event| match run_event {
tauri::RunEvent::WindowEvent { event, .. } => match event {
tauri::WindowEvent::CloseRequested { api, .. } => {
.run(|app, run_event| {
if let RunEvent::WindowEvent { event, .. } = run_event {
if let WindowEvent::CloseRequested { api, .. } = event {
let _ = hide_main_window(app);
api.prevent_close();
}
_ => ()
}
_ => ()
});
Ok(())
@ -137,7 +152,7 @@ fn start_auto_locker(app: AppHandle) {
tokio::time::sleep(delay).await;
if state.should_auto_lock().await {
state.lock().await.error_popup("Failed to lock Creddy").await;
state.lock().await.error_popup("Failed to lock Creddy");
}
}
});
@ -147,9 +162,8 @@ fn start_auto_locker(app: AppHandle) {
pub fn show_main_window(app: &AppHandle) -> Result<(), WindowError> {
let w = app.get_webview_window("main").ok_or(WindowError::NoMainWindow)?;
w.show()?;
// app.tray_handle()
// .get_item("show_hide")
// .set_title("Hide")?;
let show_hide = app.state::<MenuItem<tauri::Wry>>();
show_hide.set_text("Hide")?;
Ok(())
}
@ -157,9 +171,8 @@ pub fn show_main_window(app: &AppHandle) -> Result<(), WindowError> {
pub fn hide_main_window(app: &AppHandle) -> Result<(), WindowError> {
let w = app.get_webview_window("main").ok_or(WindowError::NoMainWindow)?;
w.hide()?;
// app.tray_handle()
// .get_item("show_hide")
// .set_title("Show")?;
let show_hide = app.state::<MenuItem<tauri::Wry>>();
show_hide.set_text("Show")?;
Ok(())
}