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

Migrate to Tauri v2 #52

Merged
merged 7 commits into from
Oct 26, 2024
1,789 changes: 1,191 additions & 598 deletions dataans/Cargo.lock

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions dataans/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@ edition = "2021"
repository = "https://github.com/TheBestTvarynka/dataans"

[build-dependencies]
tauri-build = { version = "1", features = [] }
tauri-build = { version = "2", features = [] }

[dependencies]
tauri = { version = "1", features = [
"protocol-asset",
"shell-open",
"fs-all",
"system-tray",
"global-shortcut",
tauri = { version = "2", features = [
"protocol-asset", "tray-icon",
] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand All @@ -39,7 +35,12 @@ syntect = { version = "5.2", default-features = false, features = [
"default-fancy",
"metadata",
] }
tauri-plugin-fs = "2"
tauri-plugin-shell = "2"

[features]
# This feature is used for production builds or when a dev server is not specified, DO NOT REMOVE!!
custom-protocol = ["tauri/custom-protocol"]

[target.'cfg(not(any(target_os = "android", target_os = "ios")))'.dependencies]
tauri-plugin-global-shortcut = "2"
17 changes: 16 additions & 1 deletion dataans/src-tauri/build.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
fn main() {
tauri_build::build()
tauri_build::try_build(tauri_build::Attributes::new().plugin(
"dataans",
tauri_build::InlinedPlugin::new().commands(&[
"list_spaces",
"create_space",
"update_space",
"delete_space",
"list_notes",
"create_note",
"update_note",
"delete_note",
"search_notes_in_space",
"search_notes",
]),
))
.expect("Tauri app build should not fail")
}
27 changes: 27 additions & 0 deletions dataans/src-tauri/capabilities/desktop.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"identifier": "desktop-capability",
"platforms": [
"macOS",
"windows",
"linux"
],
"windows": [
"main"
],
"permissions": [
"dataans:allow-list-spaces",
"dataans:allow-create-space",
"dataans:allow-update-space",
"dataans:allow-delete-space",
"dataans:allow-list-notes",
"dataans:allow-create-note",
"dataans:allow-update-note",
"dataans:allow-delete-note",
"dataans:allow-search-notes-in-space",
"dataans:allow-search-notes",
"global-shortcut:default",
"global-shortcut:allow-is-registered",
"global-shortcut:allow-register",
"global-shortcut:allow-unregister"
]
}
31 changes: 31 additions & 0 deletions dataans/src-tauri/capabilities/migrated.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"identifier": "migrated",
"description": "permissions that were migrated from v1",
"local": true,
"windows": [
"main"
],
"permissions": [
"core:default",
"fs:allow-read-file",
"fs:allow-write-file",
"fs:allow-read-dir",
"fs:allow-copy-file",
"fs:allow-mkdir",
"fs:allow-remove",
"fs:allow-remove",
"fs:allow-rename",
"fs:allow-exists",
{
"identifier": "fs:scope",
"allow": [
"$HOME/.config/dataans/*",
"$APPCACHE/**",
"$RESOURCE/**"
]
},
"shell:allow-open",
"fs:default",
"shell:default"
]
}
32 changes: 6 additions & 26 deletions dataans/src-tauri/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fs::read_to_string;
use std::path::{Path, PathBuf};

use common::{Config, Theme};
use tauri::AppHandle;
use tauri::{AppHandle, Manager};

use super::{CONFIGS_DIR, CONFIG_FILE_NAME};

Expand All @@ -23,11 +23,7 @@ pub fn read_config(path: impl AsRef<Path>) -> Config {
}

pub fn load_config_inner(app_handle: &AppHandle) -> Config {
let configs_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_default()
.join(CONFIGS_DIR);
let configs_dir = app_handle.path().app_data_dir().unwrap_or_default().join(CONFIGS_DIR);

let config_file_path = configs_dir.join(CONFIG_FILE_NAME);
info!(?config_file_path, "Config file path");
Expand All @@ -44,11 +40,7 @@ pub fn config(app_handle: AppHandle) -> Config {
#[instrument(level = "trace", ret, skip(app_handle))]
#[tauri::command]
pub fn theme(app_handle: AppHandle, file_path: PathBuf) -> Theme {
let configs_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_default()
.join(CONFIGS_DIR);
let configs_dir = app_handle.path().app_data_dir().unwrap_or_default().join(CONFIGS_DIR);

let theme_file_path = configs_dir.join(file_path);
info!(?theme_file_path, "Theme file path");
Expand All @@ -69,11 +61,7 @@ pub fn theme(app_handle: AppHandle, file_path: PathBuf) -> Theme {

#[tauri::command]
pub fn open_config_file(app_handle: AppHandle) {
let configs_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_default()
.join(CONFIGS_DIR);
let configs_dir = app_handle.path().app_data_dir().unwrap_or_default().join(CONFIGS_DIR);

let config_file_path = configs_dir.join(CONFIG_FILE_NAME);

Expand All @@ -83,11 +71,7 @@ pub fn open_config_file(app_handle: AppHandle) {

#[tauri::command]
pub fn open_theme_file(app_handle: AppHandle, file_path: PathBuf) {
let configs_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_default()
.join(CONFIGS_DIR);
let configs_dir = app_handle.path().app_data_dir().unwrap_or_default().join(CONFIGS_DIR);

let theme_file_path = configs_dir.join(file_path);

Expand All @@ -97,11 +81,7 @@ pub fn open_theme_file(app_handle: AppHandle, file_path: PathBuf) {

#[tauri::command]
pub fn open_config_file_folder(app_handle: AppHandle) {
let configs_dir = app_handle
.path_resolver()
.app_data_dir()
.unwrap_or_default()
.join(CONFIGS_DIR);
let configs_dir = app_handle.path().app_data_dir().unwrap_or_default().join(CONFIGS_DIR);

let config_file_path = configs_dir.join(CONFIG_FILE_NAME);

Expand Down
55 changes: 26 additions & 29 deletions dataans/src-tauri/src/dataans/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl DataansState {
pub fn init(db_dir: PathBuf) -> Self {
let db_file = db_dir.join("dataans.db");

info!(?db_file, "database file");
info!(?db_file, "Database file");

Self {
db: Database::open_file(db_file).expect("Database opening should not fail."),
Expand All @@ -31,6 +31,9 @@ impl DataansState {
}

pub fn init_dataans_plugin<R: Runtime>() -> TauriPlugin<R> {
warn!("init_dataans_plugin");
debug!("init_dataans_plugin");

Builder::new(APP_PLUGIN_NAME)
.invoke_handler(tauri::generate_handler![
space::list_spaces,
Expand All @@ -44,10 +47,10 @@ pub fn init_dataans_plugin<R: Runtime>() -> TauriPlugin<R> {
note::search_notes_in_space,
note::search_notes,
])
.setup(|app_handle| {
.setup(|app_handle, _api| {
info!("Starting app setup...");

let path_resolver = app_handle.path_resolver();
let path_resolver = app_handle.path();
let app_data = path_resolver.app_data_dir().unwrap_or_default();
debug!(?app_data);
if !app_data.exists() {
Expand Down Expand Up @@ -92,42 +95,36 @@ pub fn init_dataans_plugin<R: Runtime>() -> TauriPlugin<R> {

let config_file = configs_dir.join(CONFIG_FILE_NAME);
if !config_file.exists() {
if let Some(default_config) = path_resolver.resolve_resource("resources/configs/config.toml") {
if let Err(err) = fs::copy(&default_config, &config_file) {
error!(
?err,
?default_config,
?config_file,
"Cannot create the default config file"
);
} else {
info!(?config_file, "Successfully created default config file");
}
} else {
let resource_dir = path_resolver.resource_dir()?.join("resources");
let default_config = resource_dir.join("configs").join("config.toml");

if let Err(err) = fs::copy(&default_config, &config_file) {
error!(
"Cannot to resolve the default config file. You need to fix it manually or reinstall the app"
?err,
?default_config,
?config_file,
"Cannot create the default config file"
);
} else {
info!(?config_file, "Successfully created default config file");
}
}

let config = crate::config::read_config(config_file);
let theme_file = configs_dir.join(&config.appearance.theme);
if !theme_file.exists() {
if let Some(default_theme) = path_resolver.resolve_resource("resources/configs/theme_dark.toml") {
if let Err(err) = fs::copy(&default_theme, &theme_file) {
error!(
?err,
?default_theme,
?theme_file,
"Cannot create the default theme file"
);
} else {
info!(?theme_file, "Successfully created default theme file");
}
} else {
let resource_dir = path_resolver.resource_dir()?.join("resources");
let default_theme = resource_dir.join("configs").join("theme_dark.toml");

if let Err(err) = fs::copy(&default_theme, &theme_file) {
error!(
"Cannot to resolve the default theme file. You need to fix it manually or reinstall the app"
?err,
?default_theme,
?theme_file,
"Cannot create the default theme file"
);
} else {
info!(?theme_file, "Successfully created default theme file");
}
}

Expand Down
4 changes: 2 additions & 2 deletions dataans/src-tauri/src/file.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fs;
use std::path::PathBuf;

use tauri::AppHandle;
use tauri::{AppHandle, Manager};
use uuid::Uuid;

use crate::FILES_DIR;
Expand All @@ -12,7 +12,7 @@ pub fn upload_file(app_handle: AppHandle, id: Uuid, name: String, data: Vec<u8>)
let file_name = format!("{}_{}", id, name);

let file_path = app_handle
.path_resolver()
.path()
.app_data_dir()
.unwrap_or_default()
.join(FILES_DIR)
Expand Down
6 changes: 3 additions & 3 deletions dataans/src-tauri/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;

use arboard::Clipboard;
use image::{ImageBuffer, Rgba};
use tauri::AppHandle;
use tauri::{AppHandle, Manager};
use uuid::Uuid;

use crate::IMAGED_DIR;
Expand All @@ -14,7 +14,7 @@ pub fn gen_random_avatar(app_handle: AppHandle) -> PathBuf {
let avatar_name = format!("{}.png", Uuid::new_v4());

let avatar_path = app_handle
.path_resolver()
.path()
.app_data_dir()
.unwrap_or_default()
.join(IMAGED_DIR)
Expand All @@ -33,7 +33,7 @@ pub fn handle_clipboard_image(app_handle: AppHandle) -> PathBuf {
let image_data = clipboard.get_image().expect("Image data");

let image_path = app_handle
.path_resolver()
.path()
.app_data_dir()
.unwrap_or_default()
.join(IMAGED_DIR)
Expand Down
Loading