feat: 引入异步运行时。
This commit is contained in:
@@ -6,14 +6,11 @@
|
||||
mod screen_color_picker;
|
||||
|
||||
use paris::*;
|
||||
use screen_color_picker::{Screen, ScreenColorPicker};
|
||||
use std::time::Instant;
|
||||
|
||||
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
|
||||
#[tauri::command]
|
||||
fn greet(name: &str) -> String {
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
}
|
||||
use screen_color_picker::ScreenColorPicker;
|
||||
use std::{
|
||||
sync::{Arc, Mutex},
|
||||
time::Instant,
|
||||
};
|
||||
|
||||
#[tauri::command]
|
||||
fn refresh_displays() {
|
||||
@@ -26,41 +23,55 @@ fn refresh_displays() {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn take_snapshot() -> Vec<String> {
|
||||
async fn take_snapshot() -> Arc<Mutex<Vec<String>>> {
|
||||
let start = Instant::now();
|
||||
|
||||
let screenshots = match ScreenColorPicker::global().take_screenshots_for_all() {
|
||||
let screenshot_futures = match ScreenColorPicker::global().take_screenshots_for_all() {
|
||||
Ok(bitmaps) => {
|
||||
info!("bitmaps len: {}", bitmaps.len());
|
||||
match ScreenColorPicker::global().screens.lock() {
|
||||
Ok(screens) => Vec::from_iter(
|
||||
screens
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(index, screen)| bitmap_to_webp_base64(screen, &bitmaps[index])),
|
||||
),
|
||||
Ok(screens) => {
|
||||
Some(Vec::from_iter(screens.iter().enumerate().map(
|
||||
|(index, screen)| bitmap_to_webp_base64(screen.width, screen.height, bitmaps[index].to_vec()),
|
||||
)))
|
||||
}
|
||||
Err(error) => {
|
||||
error!("can not lock screens. {}", error);
|
||||
Vec::<String>::new()
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => {
|
||||
error!("can not take screenshots for all. {}", error);
|
||||
Vec::<String>::new()
|
||||
None
|
||||
}
|
||||
};
|
||||
println!("运行耗时: {:?}", start.elapsed());
|
||||
screenshots
|
||||
match screenshot_futures {
|
||||
Some(futures) => {
|
||||
let mut handles = Vec::with_capacity(futures.len());
|
||||
|
||||
for fut in futures {
|
||||
handles.push(tokio::spawn(fut));
|
||||
}
|
||||
|
||||
let mut results = Vec::with_capacity(handles.len());
|
||||
for handle in handles {
|
||||
results.push(handle.await.unwrap());
|
||||
}
|
||||
println!("运行耗时: {:?}", start.elapsed());
|
||||
Arc::new(Mutex::new(results))
|
||||
}
|
||||
None => Arc::new(Mutex::new(Vec::new())),
|
||||
}
|
||||
}
|
||||
|
||||
fn bitmap_to_webp_base64(screen: &Screen, bitmap: &Vec<u8>) -> String {
|
||||
let mut bitflipped = Vec::with_capacity(screen.width * screen.height * 3);
|
||||
let stride = bitmap.len() / screen.height;
|
||||
async fn bitmap_to_webp_base64(width: usize, height: usize, bitmap: Vec<u8>) -> String {
|
||||
let mut bitflipped = Vec::with_capacity(width * height * 3);
|
||||
let stride = bitmap.len() / height;
|
||||
|
||||
// let mut img = Image::new(w as u32, h as u32);
|
||||
for y in 0..screen.height {
|
||||
for x in 0..screen.width {
|
||||
for y in 0..height {
|
||||
for x in 0..width {
|
||||
let i = stride * y + 4 * x;
|
||||
bitflipped.extend_from_slice(&[bitmap[i + 2], bitmap[i + 1], bitmap[i]]);
|
||||
// img.set_pixel(
|
||||
@@ -71,22 +82,14 @@ fn bitmap_to_webp_base64(screen: &Screen, bitmap: &Vec<u8>) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
let webp_memory = webp::Encoder::from_rgb(
|
||||
bitflipped.as_slice(),
|
||||
screen.width as u32,
|
||||
screen.height as u32,
|
||||
)
|
||||
.encode(100.0);
|
||||
let webp_memory =
|
||||
webp::Encoder::from_rgb(bitflipped.as_slice(), width as u32, height as u32).encode(100.0);
|
||||
return base64::encode(&*webp_memory);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
tauri::Builder::default()
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
greet,
|
||||
take_snapshot,
|
||||
refresh_displays
|
||||
])
|
||||
.invoke_handler(tauri::generate_handler![take_snapshot, refresh_displays])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running tauri application");
|
||||
}
|
||||
|
Reference in New Issue
Block a user