support set interval
This commit is contained in:
@@ -10,9 +10,15 @@ use tauri_plugin_store::StoreExt;
|
||||
|
||||
pub struct AppState {
|
||||
pub is_paused: Arc<AtomicBool>,
|
||||
pub capture_interval_secs: std::sync::atomic::AtomicU64,
|
||||
pub toggle_menu_item: std::sync::Mutex<Option<tauri::menu::MenuItem<tauri::Wry>>>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn update_interval(state: tauri::State<'_, AppState>, seconds: u64) {
|
||||
state.capture_interval_secs.store(seconds, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
pub fn toggle_pause(app: tauri::AppHandle, state: tauri::State<'_, AppState>) -> bool {
|
||||
let current = state.is_paused.load(Ordering::SeqCst);
|
||||
@@ -78,26 +84,27 @@ pub fn get_timeline(date: String, base_dir: String) -> Vec<serde_json::Value> {
|
||||
}
|
||||
|
||||
pub fn start_engine(app: AppHandle) {
|
||||
let state = app.state::<AppState>();
|
||||
let is_paused = state.is_paused.clone();
|
||||
|
||||
// Start Cleanup routine
|
||||
let app_clone = app.clone();
|
||||
let app_cleanup = app.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
// Run once on startup, then every 24 hours
|
||||
let mut ticker = interval(Duration::from_secs(60 * 60 * 24));
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
if let Err(e) = run_cleanup(&app_clone).await {
|
||||
if let Err(e) = run_cleanup(&app_cleanup).await {
|
||||
eprintln!("Cleanup error: {}", e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
let app_capture = app.clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
// Check every 500ms to be precise about the 0th second
|
||||
let state = app_capture.state::<AppState>();
|
||||
let is_paused = state.is_paused.clone();
|
||||
|
||||
// Check frequently to catch the aligned second
|
||||
let mut ticker = interval(Duration::from_millis(500));
|
||||
let mut last_minute = -1;
|
||||
let mut last_capture_time = 0;
|
||||
|
||||
loop {
|
||||
ticker.tick().await;
|
||||
@@ -106,16 +113,20 @@ pub fn start_engine(app: AppHandle) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let now = Local::now();
|
||||
let current_minute = now.format("%M").to_string().parse::<i32>().unwrap_or(0);
|
||||
let current_second = now.format("%S").to_string().parse::<i32>().unwrap_or(0);
|
||||
let interval_secs = state.capture_interval_secs.load(Ordering::SeqCst);
|
||||
if interval_secs == 0 { continue; }
|
||||
|
||||
// Trigger only if it's the 0th second and we haven't captured this minute yet
|
||||
if current_second == 0 && current_minute != last_minute {
|
||||
last_minute = current_minute;
|
||||
println!("Tick: capturing screen at {:02}:{:02}:00...", now.format("%H"), current_minute);
|
||||
if let Err(e) = capture_screens(&app).await {
|
||||
let now = Local::now();
|
||||
let timestamp = now.timestamp() as u64;
|
||||
|
||||
// Trigger if aligned to interval and we haven't captured this exact second yet
|
||||
if timestamp % interval_secs == 0 && timestamp != last_capture_time {
|
||||
last_capture_time = timestamp;
|
||||
println!("Tick: capturing screen at {}...", now.format("%H:%M:%S"));
|
||||
if let Err(e) = capture_screens(&app_capture).await {
|
||||
eprintln!("Failed to capture screens: {}", e);
|
||||
} else {
|
||||
let _ = app_capture.emit("refresh-timeline", ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user