feat: 拖拽排序。
This commit is contained in:
@ -1,5 +1,6 @@
|
||||
use std::{env::current_dir, fmt::format};
|
||||
use std::env::current_dir;
|
||||
|
||||
use display_info::DisplayInfo;
|
||||
use paris::{error, info};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::api::path::config_dir;
|
||||
@ -41,6 +42,8 @@ pub struct LedStripConfigGroup {
|
||||
|
||||
impl LedStripConfigGroup {
|
||||
pub async fn read_config() -> anyhow::Result<Self> {
|
||||
let displays = DisplayInfo::all()?;
|
||||
|
||||
// config path
|
||||
let path = config_dir()
|
||||
.unwrap_or(current_dir().unwrap())
|
||||
@ -53,10 +56,14 @@ impl LedStripConfigGroup {
|
||||
if exists {
|
||||
let config = tokio::fs::read_to_string(path).await?;
|
||||
|
||||
let config: LedStripConfigGroup = toml::from_str(&config)
|
||||
let mut config: LedStripConfigGroup = toml::from_str(&config)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to parse config file: {}", e))?;
|
||||
|
||||
log::info!("config loaded: {:?}", config.strips.iter().map(|c| format!("{}#{}", c.index, c.display_id)).collect::<Vec<_>>());
|
||||
for strip in config.strips.iter_mut() {
|
||||
strip.display_id = displays[strip.index / 4].id;
|
||||
}
|
||||
|
||||
// log::info!("config loaded: {:?}", config);
|
||||
|
||||
Ok(config)
|
||||
} else {
|
||||
@ -76,7 +83,7 @@ impl LedStripConfigGroup {
|
||||
anyhow::anyhow!("Failed to parse config file: {}. configs: {:?}", e, configs)
|
||||
})?;
|
||||
|
||||
tokio::fs::write (&path, config_text).await.map_err(|e| {
|
||||
tokio::fs::write(&path, config_text).await.map_err(|e| {
|
||||
anyhow::anyhow!("Failed to write config file: {}. path: {:?}", e, &path)
|
||||
})?;
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
use std::{borrow::BorrowMut, sync::Arc};
|
||||
|
||||
use tauri::async_runtime::RwLock;
|
||||
use tokio::sync::OnceCell;
|
||||
@ -45,7 +45,7 @@ impl ConfigManager {
|
||||
.send(configs.clone())
|
||||
.map_err(|e| anyhow::anyhow!("Failed to send config update: {}", e))?;
|
||||
|
||||
log::info!("config updated: {:?}", configs);
|
||||
// log::info!("config updated: {:?}", configs);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@ -90,6 +90,53 @@ impl ConfigManager {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn move_strip_part(
|
||||
&self,
|
||||
display_id: u32,
|
||||
border: Border,
|
||||
target_start: usize,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut config = self.config.write().await;
|
||||
|
||||
for (index, strip) in config.clone().strips.iter().enumerate() {
|
||||
if strip.display_id == display_id && strip.border == border {
|
||||
let mut mapper = config.mappers[index].borrow_mut();
|
||||
|
||||
if target_start == mapper.start {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let target_end = mapper.end + target_start - mapper.start;
|
||||
|
||||
if target_start > 1000 || target_end > 1000 {
|
||||
return Err(anyhow::anyhow!(
|
||||
"Overflow. range: 0-1000, current: {}-{}",
|
||||
target_start,
|
||||
target_end
|
||||
));
|
||||
}
|
||||
|
||||
mapper.start = target_start as usize;
|
||||
mapper.end = target_end as usize;
|
||||
|
||||
log::info!("mapper: {:?}", mapper);
|
||||
}
|
||||
}
|
||||
log::info!("mapper: {:?}", config.mappers[4]);
|
||||
|
||||
let cloned_config = config.clone();
|
||||
|
||||
drop(config);
|
||||
|
||||
self.update(&cloned_config).await?;
|
||||
|
||||
self.config_update_sender
|
||||
.send(cloned_config)
|
||||
.map_err(|e| anyhow::anyhow!("Failed to send config update: {}", e))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn rebuild_mappers(config: &mut LedStripConfigGroup) {
|
||||
let mut prev_end = 0;
|
||||
let mappers: Vec<SamplePointMapper> = config
|
||||
|
@ -1,8 +1,8 @@
|
||||
use std::sync::Arc;
|
||||
use std::{sync::Arc, time::Duration};
|
||||
|
||||
use paris::warn;
|
||||
use tauri::async_runtime::{Mutex, RwLock};
|
||||
use tokio::sync::watch;
|
||||
use tokio::{sync::watch, time::sleep};
|
||||
|
||||
use crate::{
|
||||
ambient_light::{config, ConfigManager},
|
||||
@ -71,7 +71,7 @@ impl LedColorsPublisher {
|
||||
warn!("rx changed error: {}", err);
|
||||
continue;
|
||||
}
|
||||
log::info!("screenshot updated");
|
||||
// log::info!("screenshot updated");
|
||||
|
||||
let screenshot = rx.borrow().clone();
|
||||
|
||||
@ -97,30 +97,12 @@ impl LedColorsPublisher {
|
||||
if some_screenshot_receiver_is_none
|
||||
|| config_receiver.has_changed().unwrap_or(true)
|
||||
{
|
||||
sleep(Duration::from_millis(1000)).await;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
tokio::spawn(async move {
|
||||
let config_manager = ConfigManager::global().await;
|
||||
|
||||
let mut config_receiver = config_manager.clone_config_update_receiver();
|
||||
|
||||
loop {
|
||||
if let Err(err) = config_receiver.changed().await {
|
||||
warn!("config receiver changed error: {}", err);
|
||||
continue;
|
||||
}
|
||||
|
||||
let configs = config_receiver.borrow().clone();
|
||||
let configs = Self::get_colors_configs(&configs).await;
|
||||
|
||||
handler.abort();
|
||||
break;
|
||||
}
|
||||
});
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -159,6 +159,22 @@ async fn send_colors(buffer: Vec<u8>) -> Result<(), String> {
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
async fn move_strip_part(display_id: u32, border: Border, target_start: usize) -> Result<(), String> {
|
||||
let config_manager = ambient_light::ConfigManager::global().await;
|
||||
config_manager
|
||||
.move_strip_part(
|
||||
display_id,
|
||||
border,
|
||||
target_start,
|
||||
)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
error!("can not move strip part: {}", e);
|
||||
e.to_string()
|
||||
})
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
env_logger::init();
|
||||
@ -180,6 +196,7 @@ async fn main() {
|
||||
get_one_edge_colors,
|
||||
patch_led_strip_len,
|
||||
send_colors,
|
||||
move_strip_part,
|
||||
])
|
||||
.register_uri_scheme_protocol("ambient-light", move |_app, request| {
|
||||
let response = ResponseBuilder::new().header("Access-Control-Allow-Origin", "*");
|
||||
|
@ -113,7 +113,7 @@ impl ScreenshotManager {
|
||||
) -> anyhow::Result<()> {
|
||||
let channels = self.channels.to_owned();
|
||||
let encode_listeners = self.encode_listeners.to_owned();
|
||||
log::info!("subscribe_encoded_screenshot_updated. {}", display_id);
|
||||
// log::info!("subscribe_encoded_screenshot_updated. {}", display_id);
|
||||
|
||||
{
|
||||
let encode_listeners = encode_listeners.read().await;
|
||||
@ -209,7 +209,7 @@ impl ScreenshotManager {
|
||||
let screenshot = take_screenshot(display_id, scale_factor);
|
||||
if let Ok(screenshot) = screenshot {
|
||||
tx.send(screenshot).unwrap();
|
||||
log::info!("take_screenshot_loop: send success. display#{}", display_id)
|
||||
// log::info!("take_screenshot_loop: send success. display#{}", display_id)
|
||||
} else {
|
||||
warn!("take_screenshot_loop: {}", screenshot.err().unwrap());
|
||||
}
|
||||
|
Reference in New Issue
Block a user