move error dialog to trait

This commit is contained in:
Joseph Montanaro 2023-04-30 14:10:21 -07:00
parent 871dedf0a3
commit 55775b6b05
4 changed files with 677 additions and 292 deletions

905
src-tauri/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -17,7 +17,8 @@ tauri-build = { version = "1.0.4", features = [] }
[dependencies] [dependencies]
serde_json = "1.0" serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.0.5", features = ["api-all", "dialog", "system-tray"] } tauri = { version = "1.2", features = ["api-all", "dialog", "system-tray"] }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" }
sodiumoxide = "0.2.7" sodiumoxide = "0.2.7"
tokio = { version = ">=1.19", features = ["full"] } tokio = { version = ">=1.19", features = ["full"] }
sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls"] } sqlx = { version = "0.6.2", features = ["sqlite", "runtime-tokio-rustls"] }

View File

@ -1,9 +1,9 @@
use std::error::Error; use std::error::Error;
use std::convert::AsRef; use std::convert::AsRef;
use std::sync::mpsc;
use strum_macros::AsRefStr; use strum_macros::AsRefStr;
use thiserror::Error as ThisError; use thiserror::Error as ThisError;
use aws_sdk_sts::{ use aws_sdk_sts::{
types::SdkError as AwsSdkError, types::SdkError as AwsSdkError,
error::GetSessionTokenError, error::GetSessionTokenError,
@ -12,32 +12,29 @@ use sqlx::{
error::Error as SqlxError, error::Error as SqlxError,
migrate::MigrateError, migrate::MigrateError,
}; };
use tauri::api::dialog::{
MessageDialogBuilder,
MessageDialogKind,
};
use serde::{Serialize, Serializer, ser::SerializeMap}; use serde::{Serialize, Serializer, ser::SerializeMap};
// pub struct SerializeError<E> { pub trait ErrorPopup {
// pub err: E, fn error_popup(self, title: &str);
// } }
// impl<E: std::error::Error> Serialize for SerializeError<E> impl<E: Error> ErrorPopup for Result<(), E> {
// { fn error_popup(self, title: &str) {
// fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { if let Err(e) = self {
// let mut map = serializer.serialize_map(None)?; let (tx, rx) = mpsc::channel();
// map.serialize_entry("msg", &format!("{}", self.err))?; MessageDialogBuilder::new(title, format!("{e}"))
// if let Some(src) = self.err.source() { .kind(MessageDialogKind::Error)
// let ser_src = SerializeError { err: src }; .show(move |_| tx.send(true).unwrap());
// map.serialize_entry("source", &ser_src)?;
// }
// map.end()
// }
// }
// impl<E: std::error::Error> From<E> for SerializeError<E> { rx.recv().unwrap();
// fn from(err: E) -> Self { }
// SerializeError { err } }
// } }
// }
fn serialize_basic_err<E, S>(err: &E, serializer: S) -> Result<S::Ok, S::Error> fn serialize_basic_err<E, S>(err: &E, serializer: S) -> Result<S::Ok, S::Error>

View File

@ -3,7 +3,6 @@
windows_subsystem = "windows" windows_subsystem = "windows"
)] )]
use std::error::Error; use std::error::Error;
use std::sync::mpsc::channel;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use sqlx::{ use sqlx::{
@ -17,10 +16,6 @@ use tauri::{
Manager, Manager,
async_runtime as rt, async_runtime as rt,
}; };
use tauri::api::dialog::{
MessageDialogBuilder,
MessageDialogKind,
};
mod config; mod config;
mod errors; mod errors;
@ -68,6 +63,10 @@ async fn setup(app: &mut App) -> Result<(), Box<dyn Error>> {
fn run() -> tauri::Result<()> { fn run() -> tauri::Result<()> {
tauri::Builder::default() tauri::Builder::default()
.plugin(tauri_plugin_single_instance::init(|app, _argv, _cwd| {
app.get_window("main")
.map(|w| w.show().error_popup("Failed to show main window"));
}))
.system_tray(tray::create()) .system_tray(tray::create())
.on_system_tray_event(tray::handle_event) .on_system_tray_event(tray::handle_event)
.invoke_handler(tauri::generate_handler![ .invoke_handler(tauri::generate_handler![
@ -96,16 +95,7 @@ fn run() -> tauri::Result<()> {
fn main() { fn main() {
if let Err(e) = run() { run().error_popup("Creddy failed to start");
let (tx, rx) = channel();
MessageDialogBuilder::new(
"Creddy failed to start",
format!("{e}")
)
.kind(MessageDialogKind::Error)
.show(move |_| tx.send(true).unwrap());
rx.recv().unwrap();
}
} }