Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: settings page #109

Merged
merged 12 commits into from
Dec 15, 2023
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

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

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

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

57 changes: 56 additions & 1 deletion src-tauri/Cargo.lock

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

4 changes: 3 additions & 1 deletion src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ x25519-dalek = { version = "2", features = [
"getrandom",
"static_secrets",
] }
strum = { version = "0.25", features = ["derive"] }

tauri = { version = "1.5", features = [ "http-all", "window-all", "system-tray", "native-tls-vendored"] }
tauri = { version = "1.5", features = [ "http-all", "window-all", "system-tray", "native-tls-vendored", "icon-png"] }
tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tauri-plugin-log = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
lazy_static = "1.4"
struct-patch = "0.4"

[target.'cfg(target_os = "macos")'.dependencies]
nix = { version = "0.27", features = ["net"] }
Expand Down
6 changes: 6 additions & 0 deletions src-tauri/migrations/20231212110739_add_settings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE settings(
id INTEGER PRIMARY KEY AUTOINCREMENT,
theme TEXT DEFAULT 'light' NOT NULL,
log_level TEXT DEFAULT 'info' NOT NULL,
tray_icon_theme TEXT DEFAULT 'color' NOT NULL
);
Binary file added src-tauri/resources/icons/tray-32x32-black.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/resources/icons/tray-32x32-color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/resources/icons/tray-32x32-gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src-tauri/resources/icons/tray-32x32-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 13 additions & 7 deletions src-tauri/src/bin/defguard-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ use tauri_plugin_log::LogTarget;

use defguard_client::{
__cmd__active_connection, __cmd__all_connections, __cmd__all_instances, __cmd__all_locations,
__cmd__connect, __cmd__disconnect, __cmd__last_connection, __cmd__location_interface_details,
__cmd__location_stats, __cmd__save_device_config, __cmd__update_instance,
__cmd__update_location_routing,
__cmd__connect, __cmd__disconnect, __cmd__get_settings, __cmd__last_connection,
__cmd__location_interface_details, __cmd__location_stats, __cmd__save_device_config,
__cmd__update_instance, __cmd__update_location_routing, __cmd__update_settings,
appstate::AppState,
commands::{
active_connection, all_connections, all_instances, all_locations, connect, disconnect,
last_connection, location_interface_details, location_stats, save_device_config,
update_instance, update_location_routing,
get_settings, last_connection, location_interface_details, location_stats,
save_device_config, update_instance, update_location_routing, update_settings,
},
database,
tray::create_tray_menu,
database::{self, models::settings::Settings},
tray::{configure_tray_icon, create_tray_menu},
utils::load_log_targets,
};
use std::{env, str::FromStr};
Expand Down Expand Up @@ -85,6 +85,8 @@ async fn main() {
last_connection,
active_connection,
update_location_routing,
get_settings,
update_settings,
])
.on_window_event(|event| match event.event() {
tauri::WindowEvent::CloseRequested { api, .. } => {
Expand Down Expand Up @@ -174,6 +176,10 @@ async fn main() {
info!("Starting main app thread.");
let result = database::info(&app_state.get_pool()).await;
info!("Database info result: {:#?}", result);
// configure tray
if let Ok(settings) = Settings::get(&app_state.get_pool()).await {
configure_tray_icon(&handle, &settings.tray_icon_theme).unwrap();
}
});
Ok(())
})
Expand Down
25 changes: 23 additions & 2 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
use crate::{
appstate::AppState,
database::{
models::instance::InstanceInfo, ActiveConnection, Connection, ConnectionInfo, Instance,
Location, LocationStats, WireguardKeys,
models::{instance::InstanceInfo, settings::SettingsPatch},
ActiveConnection, Connection, ConnectionInfo, Instance, Location, LocationStats, Settings,
WireguardKeys,
},
error::Error,
service::proto::RemoveInterfaceRequest,
tray::configure_tray_icon,
utils::{get_interface_name, setup_interface, spawn_stats_thread},
};
use chrono::{DateTime, Duration, NaiveDateTime, Utc};
use local_ip_address::local_ip;
use serde::{Deserialize, Serialize};
use sqlx::query;
use std::str::FromStr;
use struct_patch::Patch;
use tauri::{AppHandle, Manager, State};

#[derive(Clone, serde::Serialize)]
Expand Down Expand Up @@ -511,3 +514,21 @@ pub async fn update_location_routing(
Err(Error::NotFound)
}
}

#[tauri::command]
pub async fn get_settings(handle: AppHandle) -> Result<Settings, Error> {
let app_state = handle.state::<AppState>();
let settings = Settings::get(&app_state.get_pool()).await?;
Ok(settings)
}

#[tauri::command]
pub async fn update_settings(data: SettingsPatch, handle: AppHandle) -> Result<Settings, Error> {
let app_state = handle.state::<AppState>();
let pool = &app_state.get_pool();
let mut settings = Settings::get(pool).await?;
settings.apply(data);
settings.save(pool).await?;
configure_tray_icon(&handle, &settings.tray_icon_theme)?;
Ok(settings)
}
2 changes: 2 additions & 0 deletions src-tauri/src/database/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ pub async fn init_db(app_handle: &AppHandle) -> Result<DbPool, Error> {
let pool = DbPool::connect(&format!("sqlite://{}", db_path.to_str().unwrap())).await?;
debug!("Running migrations.");
sqlx::migrate!().run(&pool).await?;
Settings::init_defaults(&pool).await?;
info!("Applied migrations.");
Ok(pool)
}
Expand All @@ -62,5 +63,6 @@ pub use models::{
connection::{ActiveConnection, Connection, ConnectionInfo},
instance::{Instance, InstanceInfo},
location::{Location, LocationStats},
settings::{Settings, SettingsLogLevel, SettingsTheme, TrayIconTheme},
wireguard_keys::WireguardKeys,
};
1 change: 1 addition & 0 deletions src-tauri/src/database/models/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod connection;
pub mod instance;
pub mod location;
pub mod settings;
pub mod wireguard_keys;
Loading