[desktop] Minor tauri fixes and improvments

This commit is contained in:
Vladimir Stoilov
2024-08-02 12:39:57 +03:00
parent cfd877757d
commit 500256759e
9 changed files with 267 additions and 141 deletions

View File

@@ -0,0 +1,107 @@
use log::LevelFilter;
#[cfg(not(debug_assertions))]
const DEFAULT_LOG_LEVEL: log::LevelFilter = log::LevelFilter::Warn;
#[cfg(debug_assertions)]
const DEFAULT_LOG_LEVEL: log::LevelFilter = log::LevelFilter::Debug;
#[derive(Debug)]
pub struct CliArguments {
// Path to the installation directory
pub data: Option<String>,
// Log level to use: off, error, warn, info, debug, trace
pub log_level: log::LevelFilter,
// Start in the background without opening a window
pub background: bool,
// Enable experimental notifications via Tauri. Replaces the notifier app.
pub with_prompts: bool,
// Enable experimental prompt support via Tauri. Replaces the notifier app.
pub with_notifications: bool,
}
impl CliArguments {
fn parse_log(&mut self, level: String) {
self.log_level = match level.as_ref() {
"off" => LevelFilter::Off,
"error" => LevelFilter::Error,
"warn" => LevelFilter::Warn,
"info" => LevelFilter::Info,
"debug" => LevelFilter::Debug,
"trace" => LevelFilter::Trace,
_ => DEFAULT_LOG_LEVEL,
}
}
}
pub fn parse(raw: impl IntoIterator<Item = impl Into<std::ffi::OsString>>) -> CliArguments {
let mut cli = CliArguments {
data: None,
log_level: DEFAULT_LOG_LEVEL,
background: false,
with_prompts: false,
with_notifications: false,
};
let raw = clap_lex::RawArgs::new(raw);
let mut cursor = raw.cursor();
raw.next(&mut cursor); // Skip the bin
while let Some(arg) = raw.next(&mut cursor) {
if let Some((long, value)) = arg.to_long() {
match long {
Ok("data") => {
if let Some(value) = value {
cli.data = Some(value.to_string_lossy().into_owned());
}
}
Ok("log") => {
if let Some(value) = value {
cli.parse_log(value.to_string_lossy().into_owned());
}
}
Ok("background") => {
cli.background = true;
}
Ok("with_prompts") => {
cli.with_prompts = true;
}
Ok("with_notifications") => {
cli.with_notifications = true;
}
_ => {
// Ignore unexpected flags
}
}
} else if let Some(mut shorts) = arg.to_short() {
while let Some(short) = shorts.next() {
match short {
Ok('l') => {
if let Some(value) = shorts.next_value_os() {
let mut str = value.to_string_lossy().into_owned();
_ = str.remove(0); // remove first "=" from value (in -l=warn value will be "=warn")
cli.parse_log(str);
}
}
Ok('d') => {
if let Some(value) = shorts.next_value_os() {
let mut str = value.to_string_lossy().into_owned();
_ = str.remove(0); // remove first "=" from value (in -d=/data value will be "=/data")
cli.data = Some(str);
}
}
Ok('b') => cli.background = true,
_ => {
// Ignore unexpected flags
}
}
}
}
}
cli
}

View File

@@ -3,7 +3,6 @@
use std::{env, path::Path, time::Duration};
use clap::{Arg, Command};
use tauri::{AppHandle, Emitter, Listener, Manager, RunEvent, WindowEvent};
// Library crates
@@ -15,11 +14,12 @@ mod xdg;
// App modules
mod config;
mod cli;
mod portmaster;
mod traymenu;
mod window;
use log::{debug, error, info, LevelFilter};
use log::{debug, error, info};
use portmaster::PortmasterExt;
use tauri_plugin_log::RotationStrategy;
use traymenu::setup_tray_menu;
@@ -30,12 +30,6 @@ extern crate lazy_static;
const FALLBACK_TO_OLD_UI_EXIT_CODE: i32 = 77;
#[cfg(not(debug_assertions))]
const LOG_LEVEL: LevelFilter = LevelFilter::Warn;
#[cfg(debug_assertions)]
const LOG_LEVEL: LevelFilter = LevelFilter::Debug;
#[derive(Clone, serde::Serialize)]
struct Payload {
args: Vec<String>,
@@ -49,23 +43,6 @@ struct WsHandler {
is_first_connect: bool,
}
struct CliArguments {
// Path to the installation directory
data: Option<String>,
// Log level to use: off, error, warn, info, debug, trace
log: String,
// Start in the background without opening a window
background: bool,
// Enable experimental notifications via Tauri. Replaces the notifier app.
with_prompts: bool,
// Enable experimental prompt support via Tauri. Replaces the notifier app.
with_notifications: bool,
}
impl portmaster::Handler for WsHandler {
fn name(&self) -> String {
"main-handler".to_string()
@@ -147,44 +124,7 @@ fn main() {
std::process::exit(show_webview_not_installed_dialog());
}
let matches = Command::new("Portmaster")
.ignore_errors(true)
.arg(
Arg::new("data")
.short('d')
.long("data")
.required(false)
.help("Path to the installation directory."),
)
.arg(
Arg::new("log")
.short('l')
.long("log")
.required(false)
.help("Log level to use: off, error, warn, info, debug, trace."),
)
.arg(
Arg::new("background")
.short('b')
.long("background")
.required(false)
.help("Start in the background without opening a window."),
)
.arg(
Arg::new("with_prompts")
.long("with_prompts")
.required(false)
.action(clap::ArgAction::SetTrue)
.help("Enable experimental notifications via Tauri. Replaces the notifier app."),
)
.arg(
Arg::new("with_notifications")
.long("with_notifications")
.required(false)
.action(clap::ArgAction::SetTrue)
.help("Enable experimental prompt support via Tauri. Replaces the notifier app."),
)
.get_matches();
let cli_args = cli::parse(std::env::args());
let mut cli = CliArguments {
data: None,
@@ -245,7 +185,7 @@ fn main() {
// Initialize Logging plugin.
.plugin(
tauri_plugin_log::Builder::default()
.level(log_level)
.level(cli_args.log_level)
.rotation_strategy(RotationStrategy::KeepAll)
.clear_targets()
.target(log_target)
@@ -287,16 +227,18 @@ fn main() {
});
// Handle cli flags:
app.portmaster().set_show_after_bootstrap(!cli.background);
app.portmaster()
.with_notification_support(cli.with_notifications);
app.portmaster().with_connection_prompts(cli.with_prompts);
.set_show_after_bootstrap(!cli_args.background);
app.portmaster()
.with_notification_support(cli_args.with_notifications);
app.portmaster()
.with_connection_prompts(cli_args.with_prompts);
// prepare a custom portmaster plugin handler that will show the splash-screen
// (if not in --background) and launch the tray-icon handler.
let handler = WsHandler {
handle: app.handle().clone(),
background: cli.background,
background: cli_args.background,
is_first_connect: true,
};