feat: 屏幕跟随改为多线程执行。
性能没有提升。
This commit is contained in:
@ -3,7 +3,10 @@ use paris::info;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{sync::Arc, time::Duration};
|
||||
use tauri::async_runtime::RwLock;
|
||||
use tokio::{sync::Mutex, time::sleep};
|
||||
use tokio::{
|
||||
task,
|
||||
time::{sleep, Instant},
|
||||
};
|
||||
use tracing::warn;
|
||||
|
||||
use crate::{
|
||||
@ -82,44 +85,50 @@ impl CoreManager {
|
||||
|
||||
pub async fn play_follow(&self) -> anyhow::Result<()> {
|
||||
{
|
||||
info!("Following A");
|
||||
let lock = self.ambient_light_mode.read().await;
|
||||
if let AmbientLightMode::Follow = *lock {
|
||||
Picker::global().refresh_displays().await?;
|
||||
info!("Following B");
|
||||
} else {
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
loop {
|
||||
let start = Instant::now();
|
||||
let next_tick = start + Duration::from_millis(16);
|
||||
info!("Following");
|
||||
let lock = self.ambient_light_mode.read().await;
|
||||
if let AmbientLightMode::Follow = *lock {
|
||||
match Picker::global().take_screenshots_for_all().await {
|
||||
Ok(_) => {
|
||||
let colors = Picker::global().get_led_strip_colors().await;
|
||||
match colors {
|
||||
Ok(colors) => {
|
||||
let colors = colors.into_iter().rev().collect();
|
||||
rpc::manager::Manager::global()
|
||||
.publish_led_colors(&colors)
|
||||
.await;
|
||||
}
|
||||
Err(error) => {
|
||||
warn!("get strip colors failed. {}", error);
|
||||
}
|
||||
task::spawn(async {
|
||||
let start = Instant::now();
|
||||
match Self::follow_once().await {
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
warn!("take screenshots failed. {}", error);
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
warn!("take screenshots failed. {}", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
println!(
|
||||
"Time elapsed in expensive_function() is: {:?}",
|
||||
start.elapsed()
|
||||
);
|
||||
});
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
tokio::time::sleep_until(next_tick).await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn follow_once() -> anyhow::Result<()> {
|
||||
Picker::global().take_screenshots_for_all().await?;
|
||||
let colors = Picker::global().get_led_strip_colors().await?;
|
||||
|
||||
let colors = colors.into_iter().rev().collect();
|
||||
rpc::manager::Manager::global()
|
||||
.publish_led_colors(&colors)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
@ -6,10 +6,7 @@ use tokio::sync::Mutex;
|
||||
|
||||
use crate::picker::screen::Screen;
|
||||
|
||||
use super::{
|
||||
led_color::LedColor,
|
||||
screenshot::{Screenshot},
|
||||
};
|
||||
use super::{led_color::LedColor, screenshot::Screenshot};
|
||||
|
||||
pub struct Picker {
|
||||
pub screens: Arc<Mutex<Vec<Screen>>>,
|
||||
|
@ -53,11 +53,11 @@ impl Screenshot {
|
||||
match bitmap.as_ref() {
|
||||
Some(bitmap) => {
|
||||
let cell_size_x = self.width / self.led_number_of_x;
|
||||
let cell_size_y = self.height / 5;
|
||||
let cell_size_y = self.height / 8;
|
||||
let cell_size = cell_size_x * cell_size_y;
|
||||
let y_range = match position {
|
||||
XPosition::Top => 0..cell_size_y,
|
||||
XPosition::Bottom => self.height - cell_size_y..self.height,
|
||||
XPosition::Top => 20..cell_size_y + 20,
|
||||
XPosition::Bottom => self.height - 20 - cell_size_y..self.height - 20,
|
||||
};
|
||||
|
||||
let mut colors = Vec::new();
|
||||
|
Reference in New Issue
Block a user