feature/gui-configuration:支持从 GUI 配置程序。 #4

Merged
Ivan merged 19 commits from feature/gui-configuration into master 2023-01-26 23:44:19 +08:00
2 changed files with 75 additions and 86 deletions
Showing only changes of commit a12c503e81 - Show all commits

View File

@ -1,14 +1,12 @@
use futures::future::join_all; use futures::future::join_all;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use paris::info; use paris::{error, info};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{collections::HashMap, sync::Arc, time::Duration}; use std::{collections::HashMap, sync::Arc, time::Duration};
use tauri::async_runtime::RwLock; use tauri::async_runtime::RwLock;
use tokio::{ use tokio::{
join,
sync::mpsc, sync::mpsc,
task,
time::{sleep, Instant}, time::{sleep, Instant},
}; };
use tracing::warn; use tracing::warn;
@ -21,7 +19,7 @@ use crate::{
rpc, rpc,
}; };
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize, Clone, Copy)]
pub enum AmbientLightMode { pub enum AmbientLightMode {
None, None,
Follow, Follow,
@ -40,25 +38,25 @@ impl CoreManager {
ambient_light_mode: Arc::new(RwLock::new(AmbientLightMode::None)), ambient_light_mode: Arc::new(RwLock::new(AmbientLightMode::None)),
}); });
tokio::spawn(async {
loop {
core.play_flowing_light().await;
match core.play_follow().await {
Ok(_) => {}
Err(error) => {
warn!("Can not following displays. {}", error);
sleep(Duration::from_millis(1000)).await;
}
};
sleep(Duration::from_millis(10)).await;
}
});
core core
} }
pub async fn set_ambient_light(&self, target_mode: AmbientLightMode) { pub async fn set_ambient_light(&self, target_mode: AmbientLightMode) {
let mut mode = self.ambient_light_mode.write().await; let mut mode = self.ambient_light_mode.write().await;
*mode = target_mode; *mode = target_mode;
drop(mode);
match target_mode {
AmbientLightMode::Flowing => self.play_flowing_light().await,
AmbientLightMode::None => {}
AmbientLightMode::Follow => match self.play_follow().await {
Ok(_) => {}
Err(error) => {
warn!("Can not following displays. {}", error);
}
},
};
} }
pub async fn play_flowing_light(&self) { pub async fn play_flowing_light(&self) {
@ -91,11 +89,9 @@ impl CoreManager {
} }
pub async fn play_follow(&self) -> anyhow::Result<()> { pub async fn play_follow(&self) -> anyhow::Result<()> {
let lock = self.ambient_light_mode.read().await;
let mut futs = vec![]; let mut futs = vec![];
if let AmbientLightMode::Follow = *lock {
drop(lock);
let configs = Picker::global().await.display_configs.lock().await; let configs = Picker::global().await.display_configs.lock().await;
info!("piker display configs: {:?}", configs);
let (tx, mut rx) = mpsc::channel(10); let (tx, mut rx) = mpsc::channel(10);
@ -112,8 +108,6 @@ impl CoreManager {
futs.push(fut); futs.push(fut);
} }
let configs = configs.clone();
tokio::spawn(async move { tokio::spawn(async move {
let mut global_colors = HashMap::new(); let mut global_colors = HashMap::new();
while let Some(screenshot) = rx.recv().await { while let Some(screenshot) = rx.recv().await {
@ -123,7 +117,6 @@ impl CoreManager {
.get_top_of_led_start_at() .get_top_of_led_start_at()
.min(screenshot.get_top_of_led_end_at()); .min(screenshot.get_top_of_led_end_at());
let colors_len = colors.len();
for (index, color) in colors.into_iter().enumerate() { for (index, color) in colors.into_iter().enumerate() {
global_colors.insert(index + start, color); global_colors.insert(index + start, color);
} }
@ -156,10 +149,6 @@ impl CoreManager {
}); });
join_all(futs).await; join_all(futs).await;
} else {
drop(lock);
return Ok(());
}
Ok(()) Ok(())
} }
@ -179,17 +168,17 @@ impl CoreManager {
drop(lock); drop(lock);
let screenshot = picker.take_screenshot()?; let screenshot = picker.take_screenshot()?;
info!("Take Screenshot Spend: {:?}", start.elapsed()); info!("Take Screenshot Spend: {:?}", start.elapsed());
tx.send(screenshot).await; match tx.send(screenshot).await {
Ok(_) => {}
Err(err) => {
error!("send screenshot to main thread was failed. {:?}", err);
}
};
} else { } else {
break; break;
} }
tokio::time::sleep_until(next_tick).await; tokio::time::sleep_until(next_tick).await;
} }
// // Picker::global().take_screenshots_for_all().await?;
// // let colors = Picker::global().get_led_strip_colors().await?;
// // let colors = colors.into_iter().rev().collect();
Ok(()) Ok(())
} }
} }

View File

@ -78,7 +78,7 @@ async fn write_picker_config(config: picker::config::Configuration) -> Result<()
async fn play_mode(target_mode: AmbientLightMode) { async fn play_mode(target_mode: AmbientLightMode) {
info!("target mode: {:?}", target_mode); info!("target mode: {:?}", target_mode);
CoreManager::global().set_ambient_light(target_mode).await; tokio::spawn(async move { CoreManager::global().set_ambient_light(target_mode).await });
} }
#[tokio::main] #[tokio::main]