feat(gui, ambient_light): 鼠标滚轮修改 LED 灯条的灯珠数。

This commit is contained in:
2023-03-26 22:39:47 +08:00
parent 3e54d30498
commit 58e8c30fe2
14 changed files with 452 additions and 127 deletions

View File

@@ -3,8 +3,9 @@ use std::env::current_dir;
use paris::{error, info};
use serde::{Deserialize, Serialize};
use tauri::api::path::config_dir;
use tokio::sync::OnceCell;
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
#[derive(Clone, Copy, Serialize, Deserialize, Debug, PartialEq)]
pub enum Border {
Top,
Bottom,
@@ -29,7 +30,20 @@ pub struct LedStripConfig {
pub len: usize,
}
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct LedStripConfigGroup {
pub items: Vec<LedStripConfig>,
}
impl LedStripConfig {
pub async fn global() -> &'static Vec<LedStripConfig> {
static LED_STRIP_CONFIGS_GLOBAL: OnceCell<Vec<LedStripConfig>> = OnceCell::const_new();
LED_STRIP_CONFIGS_GLOBAL
.get_or_init(|| async { Self::read_config().await.unwrap() })
.await
}
pub async fn read_config() -> anyhow::Result<Vec<Self>> {
// config path
let path = config_dir()
@@ -43,10 +57,10 @@ impl LedStripConfig {
if exists {
let config = tokio::fs::read_to_string(path).await?;
let config: Vec<Self> = toml::from_str(&config)
let config: LedStripConfigGroup = toml::from_str(&config)
.map_err(|e| anyhow::anyhow!("Failed to parse config file: {}", e))?;
Ok(config)
Ok(config.items)
} else {
info!("config file not exist, fallback to default config");
Ok(Self::get_default_config().await?)
@@ -58,12 +72,15 @@ impl LedStripConfig {
.unwrap_or(current_dir().unwrap())
.join("led_strip_config.toml");
let config = toml::to_string(configs)
.map_err(|e| anyhow::anyhow!("Failed to parse config file: {}", e))?;
let configs = LedStripConfigGroup { items: configs.clone() };
tokio::fs::write(path, config)
.await
.map_err(|e| anyhow::anyhow!("Failed to write config file: {}", e))?;
let config = toml::to_string(&configs).map_err(|e| {
anyhow::anyhow!("Failed to parse config file: {}. configs: {:?}", e, configs)
})?;
tokio::fs::write(&path, config).await.map_err(|e| {
anyhow::anyhow!("Failed to write config file: {}. path: {:?}", e, &path)
})?;
Ok(())
}
@@ -99,13 +116,10 @@ impl LedStripConfig {
}
#[derive(Clone, Copy, Serialize, Deserialize, Debug)]
pub struct DisplayConfig {
pub struct LedStripConfigOfDisplays {
pub id: u32,
pub index_of_display: usize,
pub display_width: usize,
pub display_height: usize,
pub led_strip_of_borders: LedStripConfigOfBorders,
pub scale_factor: f32,
}
impl LedStripConfigOfBorders {
@@ -119,21 +133,98 @@ impl LedStripConfigOfBorders {
}
}
impl DisplayConfig {
pub fn default(
id: u32,
index_of_display: usize,
display_width: usize,
display_height: usize,
scale_factor: f32,
) -> Self {
impl LedStripConfigOfDisplays {
pub fn default(id: u32, index_of_display: usize) -> Self {
Self {
id,
index_of_display,
display_width,
display_height,
led_strip_of_borders: LedStripConfigOfBorders::default(),
scale_factor,
}
}
pub async fn read_from_disk() -> anyhow::Result<Self> {
let path = config_dir()
.unwrap_or(current_dir().unwrap())
.join("led_strip_config_of_displays.toml");
let exists = tokio::fs::try_exists(path.clone())
.await
.map_err(|e| anyhow::anyhow!("Failed to check config file exists: {}", e))?;
if exists {
let config = tokio::fs::read_to_string(path).await?;
let config: Self = toml::from_str(&config)
.map_err(|e| anyhow::anyhow!("Failed to parse config file: {}", e))?;
Ok(config)
} else {
info!("config file not exist, fallback to default config");
Ok(Self::get_default_config().await?)
}
}
pub async fn write_to_disk(&self) -> anyhow::Result<()> {
let path = config_dir()
.unwrap_or(current_dir().unwrap())
.join("led_strip_config_of_displays.toml");
let config = toml::to_string(self).map_err(|e| {
anyhow::anyhow!("Failed to parse config file: {}. config: {:?}", e, self)
})?;
tokio::fs::write(&path, config).await.map_err(|e| {
anyhow::anyhow!("Failed to write config file: {}. path: {:?}", e, &path)
})?;
Ok(())
}
pub async fn get_default_config() -> anyhow::Result<Self> {
let displays = display_info::DisplayInfo::all().map_err(|e| {
error!("can not list display info: {}", e);
anyhow::anyhow!("can not list display info: {}", e)
})?;
let mut configs = Vec::new();
for (i, display) in displays.iter().enumerate() {
let config = Self {
id: display.id,
index_of_display: i,
led_strip_of_borders: LedStripConfigOfBorders {
top: Some(LedStripConfig {
index: i * 4 * 30,
display_id: display.id,
border: Border::Top,
start_pos: i * 4 * 30,
len: 30,
}),
bottom: Some(LedStripConfig {
index: i * 4 * 30 + 30,
display_id: display.id,
border: Border::Bottom,
start_pos: i * 4 * 30 + 30,
len: 30,
}),
left: Some(LedStripConfig {
index: i * 4 * 30 + 60,
display_id: display.id,
border: Border::Left,
start_pos: i * 4 * 30 + 60,
len: 30,
}),
right: Some(LedStripConfig {
index: i * 4 * 30 + 90,
display_id: display.id,
border: Border::Right,
start_pos: i * 4 * 30 + 90,
len: 30,
}),
}
};
configs.push(config);
}
Ok(configs[0])
}
}

View File

@@ -0,0 +1,88 @@
use std::{sync::Arc, borrow::Borrow};
use tauri::async_runtime::RwLock;
use tokio::sync::OnceCell;
use crate::ambient_light::{config, LedStripConfig};
use super::Border;
pub struct ConfigManager {
configs: Arc<RwLock<Vec<LedStripConfig>>>,
config_update_receiver: tokio::sync::watch::Receiver<Vec<LedStripConfig>>,
config_update_sender: tokio::sync::watch::Sender<Vec<LedStripConfig>>,
}
impl ConfigManager {
pub async fn global() -> &'static Self {
static CONFIG_MANAGER_GLOBAL: OnceCell<ConfigManager> = OnceCell::const_new();
CONFIG_MANAGER_GLOBAL
.get_or_init(|| async {
let configs = LedStripConfig::read_config().await.unwrap();
let (config_update_sender, config_update_receiver) =
tokio::sync::watch::channel(configs.clone());
ConfigManager {
configs: Arc::new(RwLock::new(configs)),
config_update_receiver,
config_update_sender,
}
})
.await
}
pub async fn reload(&self) -> anyhow::Result<()> {
let mut configs = self.configs.write().await;
*configs = LedStripConfig::read_config().await?;
Ok(())
}
pub async fn update(&self, configs: &Vec<LedStripConfig>) -> anyhow::Result<()> {
LedStripConfig::write_config(configs).await?;
self.reload().await?;
self.config_update_sender.send(configs.clone()).map_err(|e| {
anyhow::anyhow!("Failed to send config update: {}", e)
})?;
log::info!("config updated: {:?}", configs);
Ok(())
}
pub async fn configs(&self) -> Vec<LedStripConfig> {
self.configs.read().await.clone()
}
pub async fn patch_led_strip_len(&self, display_id: u32, border: Border, delta_len: i8) -> anyhow::Result<()> {
let mut configs = self.configs.write().await;
for config in configs.iter_mut() {
if config.display_id == display_id && config.border == border {
let target = config.len as i64 + delta_len as i64;
if target < 0 || target > 1000 {
return Err(anyhow::anyhow!("Overflow. range: 0-1000, current: {}", target));
}
config.len = target as usize;
}
}
let cloned_config = configs.clone();
drop(configs);
self.update(&cloned_config).await?;
self.config_update_sender
.send(cloned_config)
.map_err(|e| anyhow::anyhow!("Failed to send config update: {}", e))?;
Ok(())
}
pub fn clone_config_update_receiver(
&self,
) -> tokio::sync::watch::Receiver<Vec<LedStripConfig>> {
self.config_update_receiver.clone()
}
}

View File

@@ -1,3 +1,5 @@
mod config;
mod config_manager;
pub use config::*;
pub use config_manager::*;