feat: 支持实时保存配置。

This commit is contained in:
2023-01-12 13:44:54 +08:00
parent 9030d48e21
commit 5293ed52ff
6 changed files with 87 additions and 44 deletions

View File

@@ -2,12 +2,13 @@ use std::{
env::current_dir,
fs::{self, File},
io::Read,
path::PathBuf,
path::PathBuf, sync::Arc,
};
use once_cell::sync::OnceCell;
use paris::info;
use serde::{Deserialize, Serialize};
use tauri::api::path::config_dir;
use tauri::{api::path::config_dir, async_runtime::Mutex};
use super::DisplayConfig;
@@ -26,9 +27,8 @@ impl Configuration {
}
}
#[derive(Serialize, Deserialize)]
pub struct Manager {
config: Configuration,
config: Arc<Mutex<Configuration>>,
}
impl Manager {
@@ -43,7 +43,7 @@ impl Manager {
}
pub fn new(config: Configuration) -> Self {
Self { config }
Self { config: Arc::new(Mutex::new(config)) }
}
pub fn get_config_file_path() -> PathBuf {
@@ -70,16 +70,21 @@ impl Manager {
.map_err(|error| anyhow::anyhow!("can not parse config file contents. {}", error))
}
pub fn write_config_to_disk(&self, config_file_path: PathBuf) -> anyhow::Result<()> {
let contents = serde_json::to_string(&self.config)
pub fn write_config_to_disk(config_file_path: PathBuf, config: &Configuration) -> anyhow::Result<()> {
let contents = serde_json::to_string(config)
.map_err(|error| anyhow::anyhow!("can not serialize config. {}", error))?;
info!("contents: {}", contents);
fs::write(config_file_path, contents.as_bytes())
.map_err(|error| anyhow::anyhow!("can not write config file. {}", error))?;
Ok(())
}
pub fn get_config(&self) -> Configuration {
self.config.clone()
pub async fn get_config(&self) -> Configuration {
self.config.lock().await.clone()
}
pub async fn set_config(&self, new_config: &Configuration) {
let mut config = self.config.lock().await;
*config = new_config.clone();
}
}
@@ -93,13 +98,13 @@ mod tests {
use crate::picker::config::Configuration;
#[test]
fn write_config_to_disk_should_be_successful() {
#[tokio::test]
async fn write_config_to_disk_should_be_successful() {
let temp = TestDir::temp().create("config_dir", test_dir::FileType::Dir);
let config_file_path = temp.path("config_dir").join("picker.config.json");
let manager = crate::picker::config::manger::Manager::default();
manager
.write_config_to_disk(config_file_path.clone())
crate::picker::config::manger::Manager
::write_config_to_disk(config_file_path.clone(), &Configuration::default())
.unwrap();
let contents = fs::read_to_string(config_file_path.clone()).unwrap();

View File

@@ -1,9 +1,11 @@
use futures::{stream::FuturesUnordered, StreamExt};
use once_cell::sync::OnceCell;
use paris::info;
use scrap::Display;
use std::sync::Arc;
use tokio::{sync::Mutex, task};
use tokio::{
sync::{Mutex, OnceCell},
task,
};
use crate::picker::{config, screen::Screen};
@@ -20,16 +22,20 @@ pub struct Picker {
}
impl Picker {
pub fn global() -> &'static Picker {
static SCREEN_COLOR_PICKER: OnceCell<Picker> = OnceCell::new();
pub async fn global() -> &'static Picker {
static SCREEN_COLOR_PICKER: OnceCell<Picker> = OnceCell::const_new();
SCREEN_COLOR_PICKER.get_or_init(|| Picker {
screens: Arc::new(Mutex::new(vec![])),
screenshots: Arc::new(Mutex::new(vec![])),
display_configs: Arc::new(Mutex::new(
config::Manager::global().get_config().display_configs,
)),
})
SCREEN_COLOR_PICKER
.get_or_init(|| async {
Picker {
screens: Arc::new(Mutex::new(vec![])),
screenshots: Arc::new(Mutex::new(vec![])),
display_configs: Arc::new(Mutex::new(
config::Manager::global().get_config().await.display_configs,
)),
}
})
.await
}
pub async fn list_displays(&self) -> anyhow::Result<Vec<ScreenshotDto>> {