From c206dbf40a6eecda6aedd1a8fcf682339f4a0497 Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Mon, 29 Jul 2024 15:05:07 +0300 Subject: [PATCH] feat: state, config, setup --- src-tauri/src/main.rs | 69 +++++++++++++++++++++++++++++++++++++++++- src-tauri/src/types.rs | 41 +++++++++++++++++++++++++ src-tauri/src/utils.rs | 42 +++++++++++++++++++++++++ src/app.css | 2 +- 4 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 src-tauri/src/utils.rs diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index aee3b2f..7c847ec 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -1,10 +1,20 @@ // Prevents additional console window on Windows in release, DO NOT REMOVE!! #![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] +use std::{ + fs::{self, File}, + io::Write, + sync::Mutex, +}; + use dalet::{Argument, Body, Tag}; mod types; -use types::VigiError; +mod utils; + +use tauri::Manager; +use types::{State, VigiError}; +use utils::{read_jsonl_tabs, read_or_create_current_tab_index, read_or_create_jsonl}; // Learn more about Tauri commands at https://tauri.app/v1/guides/features/command #[tauri::command] @@ -21,8 +31,65 @@ async fn process_input(input: String) -> Result, VigiError> { } } +fn setup_handler(app: &mut tauri::App) -> Result<(), Box> { + println!("---Setup---"); + + let app_handle = app.handle(); + + let state = app.state::>(); + let mut state = state.lock().unwrap(); + + let config_dir = app_handle + .path_resolver() + .app_config_dir() + .unwrap_or(std::path::PathBuf::new().join("config")); + + let local_data_dir = app_handle + .path_resolver() + .app_local_data_dir() + .unwrap_or(std::path::PathBuf::new().join("data")); + + state.cache_dir = app_handle + .path_resolver() + .app_cache_dir() + .unwrap_or(std::path::PathBuf::new().join("cache")); + + println!("Config dir: {}", config_dir.to_string_lossy()); + println!("Local data dir: {}", local_data_dir.to_string_lossy()); + println!("Cache dir: {}", state.cache_dir.to_string_lossy()); + + println!("Checking config dir"); + + // check if config/favorites.jsonl exists + if !config_dir.exists() { + println!(" Creating config dir"); + fs::create_dir_all(&config_dir)?; + } + + state.favorites_tabs_path = config_dir.join("favorites.jsonl"); + state.favorites_tabs = read_or_create_jsonl(&state.favorites_tabs_path); + + println!("Checking local data dir"); + if !local_data_dir.exists() { + println!(" Creating local data dir"); + fs::create_dir_all(&local_data_dir)?; + } + + state.local_tabs_path = local_data_dir.join("tabs.jsonl"); + state.tabs = read_or_create_jsonl(&state.local_tabs_path); + + state.current_tab_index_path = local_data_dir.join("current_tab_index"); + state.current_tab_index = read_or_create_current_tab_index(&state.current_tab_index_path); + + println!("---Setup done---"); + + Ok(()) +} + fn main() { tauri::Builder::default() + .manage(Mutex::new(State::null())) + .setup(setup_handler) .invoke_handler(tauri::generate_handler![process_input]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src-tauri/src/types.rs b/src-tauri/src/types.rs index 2ef05f7..6224715 100644 --- a/src-tauri/src/types.rs +++ b/src-tauri/src/types.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug)] @@ -5,3 +7,42 @@ pub enum VigiError { NetworkError, ParseError, } + +pub struct State { + pub current_tab_index_path: PathBuf, + pub local_tabs_path: PathBuf, + pub favorites_tabs_path: PathBuf, + + pub cache_dir: PathBuf, + + pub current_tab_index: usize, + pub tabs: Vec, + pub favorites_tabs: Vec, +} + +impl State { + pub fn null() -> Self { + Self { + current_tab_index_path: PathBuf::new(), + local_tabs_path: PathBuf::new(), + favorites_tabs_path: PathBuf::new(), + cache_dir: PathBuf::new(), + current_tab_index: 0, + tabs: Vec::new(), + favorites_tabs: Vec::new(), + } + } +} + +#[derive(Serialize, Deserialize, Debug)] +pub struct Tab { + ty: TabType, + name: String, + url: String, +} + +#[derive(Serialize, Deserialize, Debug)] +pub enum TabType { + HomePage, + Text, +} diff --git a/src-tauri/src/utils.rs b/src-tauri/src/utils.rs new file mode 100644 index 0000000..312c412 --- /dev/null +++ b/src-tauri/src/utils.rs @@ -0,0 +1,42 @@ +use std::{ + fs::{self, File}, + path::PathBuf, +}; + +use crate::types::Tab; + +pub fn read_jsonl_tabs(path: &PathBuf) -> Vec { + fs::read_to_string(&path) + .unwrap_or_default() + .lines() + .map(|line| serde_json::from_str(line).unwrap()) + .collect() +} + +pub fn read_or_create_jsonl(path: &PathBuf) -> Vec { + println!(" Getting tabs from {}", path.to_string_lossy()); + if path.exists() { + read_jsonl_tabs(path) + } else { + println!(" Creating {}", path.to_string_lossy()); + File::create(path).unwrap(); + Vec::new() + } +} + +pub fn read_or_create_current_tab_index(path: &PathBuf) -> usize { + println!( + " Getting current tab index from {}", + path.to_string_lossy() + ); + if path.exists() { + fs::read_to_string(path) + .unwrap() + .parse::() + .unwrap_or(0) + } else { + println!(" Creating {}", path.to_string_lossy()); + fs::write(path, "0").unwrap(); + 0 + } +} diff --git a/src/app.css b/src/app.css index 9896513..c932f42 100644 --- a/src/app.css +++ b/src/app.css @@ -93,7 +93,7 @@ input::placeholder { /* Track */ ::-webkit-scrollbar-track { - background: transparent; + @apply bg-transparent; } /* Handle */