20 fixes: mutex poisoning recovery, clickthrough shutdown, audio cleanup, config corruption, GIF aspect ratio, ffmpeg crash detection, cancel export, discard confirm, thumbnail cache, keyboard nav, right-click tracking, multi-monitor overlay, click injection fix, update script robustness, effect cleanup, concat guard, dead code removal
This commit is contained in:
+97
-73
@@ -9,7 +9,8 @@ mod window_picker;
|
||||
mod windows_list;
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
@@ -20,6 +21,10 @@ use std::str::FromStr;
|
||||
use crate::bootstrap::FfmpegStatus;
|
||||
use crate::config::{Config, Format};
|
||||
|
||||
fn lock<T>(m: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
|
||||
m.lock().unwrap_or_else(|e| e.into_inner())
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct CaptureRect {
|
||||
pub x: i32,
|
||||
@@ -37,6 +42,7 @@ pub struct AppState {
|
||||
pub last_clip: Mutex<Option<RecordedClip>>,
|
||||
pub session: Mutex<Option<SessionState>>,
|
||||
pub picker: Mutex<Option<window_picker::PickerSession>>,
|
||||
pub overlay_poll_shutdown: Arc<AtomicBool>,
|
||||
}
|
||||
|
||||
pub struct SessionState {
|
||||
@@ -116,7 +122,7 @@ struct AppStatus {
|
||||
|
||||
#[tauri::command]
|
||||
fn get_status(state: State<'_, AppState>) -> AppStatus {
|
||||
let cfg = state.config.lock().expect("config mutex");
|
||||
let cfg = lock(&state.config);
|
||||
AppStatus {
|
||||
ffmpeg: bootstrap::check(&state.app_dir),
|
||||
needs_first_run: cfg.needs_first_run(),
|
||||
@@ -125,7 +131,7 @@ fn get_status(state: State<'_, AppState>) -> AppStatus {
|
||||
|
||||
#[tauri::command]
|
||||
fn get_config(state: State<'_, AppState>) -> Config {
|
||||
state.config.lock().expect("config mutex").clone()
|
||||
lock(&state.config).clone()
|
||||
}
|
||||
|
||||
fn parse_shortcut(s: &str) -> Option<Shortcut> {
|
||||
@@ -184,7 +190,7 @@ fn save_config(
|
||||
) -> Result<SaveConfigResult, String> {
|
||||
let path = config::config_path(&state.app_dir);
|
||||
config.save(&path).map_err(|e| format!("{e:#}"))?;
|
||||
*state.config.lock().expect("config mutex") = config.clone();
|
||||
*lock(&state.config) = config.clone();
|
||||
let hotkey_result = register_hotkeys(&app, &config);
|
||||
Ok(SaveConfigResult { hotkey_result })
|
||||
}
|
||||
@@ -324,12 +330,12 @@ fn start_capture(
|
||||
request: Option<StartCaptureRequest>,
|
||||
) -> Result<(), String> {
|
||||
{
|
||||
let slot = state.capture.lock().expect("capture mutex");
|
||||
let slot = lock(&state.capture);
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
}
|
||||
}
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
let cfg = lock(&state.config).clone();
|
||||
let temp = temp_dir(&state.app_dir);
|
||||
std::fs::create_dir_all(&temp).map_err(|e| format!("creating temp dir: {e}"))?;
|
||||
clear_temp_segments(&state.app_dir);
|
||||
@@ -399,40 +405,34 @@ fn start_capture(
|
||||
None
|
||||
};
|
||||
|
||||
*state.session.lock().expect("session mutex") = Some(session_state);
|
||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||
*state.audio.lock().expect("audio mutex") = audio_session;
|
||||
*state.click_recorder.lock().expect("click mutex") = click_rec;
|
||||
*lock(&state.session) = Some(session_state);
|
||||
*lock(&state.capture) = Some(session);
|
||||
*lock(&state.audio) = audio_session;
|
||||
*lock(&state.click_recorder) = click_rec;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
let session = state
|
||||
.capture
|
||||
.lock()
|
||||
.expect("capture mutex")
|
||||
let session = lock(&state.capture)
|
||||
.take()
|
||||
.ok_or_else(|| "no capture in progress".to_string())?;
|
||||
if session.has_crashed() {
|
||||
return Err("recording failed - ffmpeg encoder crashed".to_string());
|
||||
}
|
||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
|
||||
let audio_path = state
|
||||
.audio
|
||||
.lock()
|
||||
.expect("audio mutex")
|
||||
let audio_path = lock(&state.audio)
|
||||
.take()
|
||||
.and_then(|a| a.stop().ok());
|
||||
|
||||
let new_clicks: Vec<clicks::Click> = state
|
||||
.click_recorder
|
||||
.lock()
|
||||
.expect("click mutex")
|
||||
let new_clicks: Vec<clicks::Click> = lock(&state.click_recorder)
|
||||
.take()
|
||||
.map(|r| r.stop())
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut ss = state.session.lock().expect("session mutex");
|
||||
let mut ss = lock(&state.session);
|
||||
let ss = ss
|
||||
.as_mut()
|
||||
.ok_or_else(|| "no session state".to_string())?;
|
||||
@@ -455,13 +455,13 @@ fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
#[tauri::command]
|
||||
fn resume_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), String> {
|
||||
{
|
||||
let slot = state.capture.lock().expect("capture mutex");
|
||||
let slot = lock(&state.capture);
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
}
|
||||
}
|
||||
let (capture_cursor, audio, crop, next_idx) = {
|
||||
let ss = state.session.lock().expect("session mutex");
|
||||
let ss = lock(&state.session);
|
||||
let ss = ss
|
||||
.as_ref()
|
||||
.ok_or_else(|| "no session to resume".to_string())?;
|
||||
@@ -473,7 +473,7 @@ fn resume_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), Stri
|
||||
)
|
||||
};
|
||||
|
||||
let fps = state.config.lock().expect("config mutex").capture.default_fps;
|
||||
let fps = lock(&state.config).capture.default_fps;
|
||||
let output = segment_path(&state.app_dir, next_idx);
|
||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||
if !ffmpeg.is_file() {
|
||||
@@ -502,9 +502,9 @@ fn resume_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), Stri
|
||||
None
|
||||
};
|
||||
|
||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||
*state.audio.lock().expect("audio mutex") = audio_session;
|
||||
*state.click_recorder.lock().expect("click mutex") = click_rec;
|
||||
*lock(&state.capture) = Some(session);
|
||||
*lock(&state.audio) = audio_session;
|
||||
*lock(&state.click_recorder) = click_rec;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -512,32 +512,29 @@ fn resume_capture(app: AppHandle, state: State<'_, AppState>) -> Result<(), Stri
|
||||
fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
set_overlay_clickthrough(&app, false);
|
||||
|
||||
let mut ss = state
|
||||
.session
|
||||
.lock()
|
||||
.expect("session mutex")
|
||||
let mut ss = lock(&state.session)
|
||||
.take()
|
||||
.ok_or_else(|| "no session to stop".to_string())?;
|
||||
|
||||
let live = state.capture.lock().expect("capture mutex").take();
|
||||
let live = lock(&state.capture).take();
|
||||
if let Some(session) = live {
|
||||
if session.has_crashed() {
|
||||
return Err("recording failed - ffmpeg encoder crashed".to_string());
|
||||
}
|
||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
ss.segments.push(path);
|
||||
ss.total_recorded_secs += elapsed;
|
||||
}
|
||||
|
||||
let live_audio = state.audio.lock().expect("audio mutex").take();
|
||||
let live_audio = lock(&state.audio).take();
|
||||
if let Some(a) = live_audio {
|
||||
if let Ok(p) = a.stop() {
|
||||
ss.audio_segments.push(p);
|
||||
}
|
||||
}
|
||||
|
||||
let live_clicks = state
|
||||
.click_recorder
|
||||
.lock()
|
||||
.expect("click mutex")
|
||||
let live_clicks = lock(&state.click_recorder)
|
||||
.take()
|
||||
.map(|r| r.stop())
|
||||
.unwrap_or_default();
|
||||
@@ -623,7 +620,7 @@ fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedCl
|
||||
crop: ss.crop,
|
||||
clicks: ss.clicks.clone(),
|
||||
};
|
||||
*state.last_clip.lock().expect("clip mutex") = Some(clip.clone());
|
||||
*lock(&state.last_clip) = Some(clip.clone());
|
||||
Ok(clip)
|
||||
}
|
||||
|
||||
@@ -647,7 +644,7 @@ pub struct ExportRequest {
|
||||
|
||||
#[tauri::command]
|
||||
fn default_output_path(state: State<'_, AppState>, format: Format) -> String {
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
let cfg = lock(&state.config).clone();
|
||||
let dir = output_dir(&state.app_dir, &cfg);
|
||||
dir.join(format!(
|
||||
"bytesize-{}.{}",
|
||||
@@ -670,10 +667,7 @@ fn render_preview(
|
||||
state: State<'_, AppState>,
|
||||
request: PreviewRequest,
|
||||
) -> Result<PathBuf, String> {
|
||||
let clip = state
|
||||
.last_clip
|
||||
.lock()
|
||||
.expect("clip mutex")
|
||||
let clip = lock(&state.last_clip)
|
||||
.clone()
|
||||
.ok_or_else(|| "no recorded clip available".to_string())?;
|
||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||
@@ -709,10 +703,7 @@ fn extract_thumbnails(
|
||||
state: State<'_, AppState>,
|
||||
fps: u32,
|
||||
) -> Result<Vec<PathBuf>, String> {
|
||||
let clip = state
|
||||
.last_clip
|
||||
.lock()
|
||||
.expect("clip mutex")
|
||||
let clip = lock(&state.last_clip)
|
||||
.clone()
|
||||
.ok_or_else(|| "no recorded clip available".to_string())?;
|
||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||
@@ -733,11 +724,8 @@ pub struct ExportResult {
|
||||
const OVERLAY_BORDER_LOGICAL_PX: f64 = 4.0;
|
||||
const OVERLAY_TITLEBAR_LOGICAL_PX: f64 = 22.0;
|
||||
|
||||
fn apply_overlay_region(_win: &tauri::WebviewWindow) -> Result<(), String> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn start_overlay_clickthrough_poll(app: AppHandle) {
|
||||
fn start_overlay_clickthrough_poll(app: AppHandle, shutdown: Arc<AtomicBool>) {
|
||||
use std::time::Duration;
|
||||
use windows::Win32::Foundation::{HWND, POINT, RECT};
|
||||
use windows::Win32::Graphics::Dwm::{DwmGetWindowAttribute, DWMWA_EXTENDED_FRAME_BOUNDS};
|
||||
@@ -746,7 +734,7 @@ fn start_overlay_clickthrough_poll(app: AppHandle) {
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let mut last_inner: Option<bool> = None;
|
||||
loop {
|
||||
while !shutdown.load(Ordering::Relaxed) {
|
||||
std::thread::sleep(Duration::from_millis(30));
|
||||
|
||||
let overlay = match app.get_webview_window("overlay") {
|
||||
@@ -762,7 +750,7 @@ fn start_overlay_clickthrough_poll(app: AppHandle) {
|
||||
// start_capture already sets ignore_cursor_events(true). Skip polling
|
||||
// so we don't fight it.
|
||||
let state = app.state::<AppState>();
|
||||
let is_recording = state.capture.lock().expect("capture mutex").is_some();
|
||||
let is_recording = lock(&state.capture).is_some();
|
||||
if is_recording {
|
||||
last_inner = Some(true);
|
||||
continue;
|
||||
@@ -843,7 +831,39 @@ fn show_overlay(app: AppHandle) -> Result<(), String> {
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
win.show().map_err(|e| e.to_string())?;
|
||||
let _ = mark_overlay_excluded_from_capture(&win);
|
||||
let _ = apply_overlay_region(&win);
|
||||
|
||||
// If the overlay is off-screen (monitor disconnected), center on primary
|
||||
use windows::Win32::Foundation::{HWND, POINT, RECT};
|
||||
use windows::Win32::Graphics::Gdi::{
|
||||
GetMonitorInfoW, MonitorFromPoint, MONITOR_DEFAULTTOPRIMARY, MONITOR_DEFAULTTONULL, MONITORINFO,
|
||||
};
|
||||
use windows::Win32::UI::WindowsAndMessaging::{GetWindowRect, SetWindowPos, HWND_TOP, SWP_NOACTIVATE, SWP_NOSIZE, SWP_NOZORDER};
|
||||
if let Ok(raw) = win.hwnd() {
|
||||
let hwnd = HWND(raw.0 as *mut _);
|
||||
let mut rect = RECT::default();
|
||||
if unsafe { GetWindowRect(hwnd, &mut rect) }.is_ok() {
|
||||
let center = POINT {
|
||||
x: (rect.left + rect.right) / 2,
|
||||
y: (rect.top + rect.bottom) / 2,
|
||||
};
|
||||
let hmon = unsafe { MonitorFromPoint(center, MONITOR_DEFAULTTONULL) };
|
||||
if hmon.0.is_null() {
|
||||
let primary = unsafe { MonitorFromPoint(POINT { x: 0, y: 0 }, MONITOR_DEFAULTTOPRIMARY) };
|
||||
let mut mi = MONITORINFO {
|
||||
cbSize: std::mem::size_of::<MONITORINFO>() as u32,
|
||||
..Default::default()
|
||||
};
|
||||
if unsafe { GetMonitorInfoW(primary, &mut mi) }.as_bool() {
|
||||
let work = mi.rcWork;
|
||||
let w = rect.right - rect.left;
|
||||
let h = rect.bottom - rect.top;
|
||||
let cx = (work.left + work.right) / 2 - w / 2;
|
||||
let cy = (work.top + work.bottom) / 2 - h / 2;
|
||||
let _ = unsafe { SetWindowPos(hwnd, Some(HWND_TOP), cx, cy, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE) };
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
win.set_focus().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
@@ -868,7 +888,6 @@ fn set_overlay_size(app: AppHandle, width: u32, height: u32) -> Result<(), Strin
|
||||
height as f64 + OVERLAY_TITLEBAR_LOGICAL_PX + (OVERLAY_BORDER_LOGICAL_PX * 2.0);
|
||||
win.set_size(Size::Logical(LogicalSize::new(final_w, final_h)))
|
||||
.map_err(|e| e.to_string())?;
|
||||
let _ = apply_overlay_region(&win);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -937,7 +956,7 @@ fn open_window_picker(
|
||||
}
|
||||
|
||||
{
|
||||
let mut slot = state.picker.lock().expect("picker mutex");
|
||||
let mut slot = lock(&state.picker);
|
||||
if let Some(existing) = slot.take() {
|
||||
existing.stop();
|
||||
}
|
||||
@@ -951,7 +970,7 @@ fn close_window_picker(
|
||||
app: AppHandle,
|
||||
state: State<'_, AppState>,
|
||||
) -> Result<(), String> {
|
||||
if let Some(session) = state.picker.lock().expect("picker mutex").take() {
|
||||
if let Some(session) = lock(&state.picker).take() {
|
||||
session.stop();
|
||||
}
|
||||
if let Some(picker) = app.get_webview_window("picker") {
|
||||
@@ -1078,8 +1097,6 @@ fn set_overlay_to_window(
|
||||
}
|
||||
.map_err(|e| format!("SetWindowPos: {e}"))?;
|
||||
|
||||
let _ = apply_overlay_region(&win);
|
||||
let _ = (visible_left, visible_top, visible_w, visible_h, scale, border_phys, titlebar_phys);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1170,9 +1187,12 @@ fn reveal_in_explorer(path: String) -> Result<(), String> {
|
||||
|
||||
#[tauri::command]
|
||||
fn open_url(url: String) -> Result<(), String> {
|
||||
use std::process::Command;
|
||||
Command::new("cmd")
|
||||
.args(["/C", "start", "", &url])
|
||||
if !url.starts_with("http://") && !url.starts_with("https://") {
|
||||
return Err("only http/https URLs are supported".to_string());
|
||||
}
|
||||
use std::os::windows::process::CommandExt;
|
||||
std::process::Command::new("cmd")
|
||||
.raw_arg(format!("/C start \"\" \"{url}\""))
|
||||
.spawn()
|
||||
.map_err(|e| format!("opening url: {e}"))?;
|
||||
Ok(())
|
||||
@@ -1180,11 +1200,8 @@ fn open_url(url: String) -> Result<(), String> {
|
||||
|
||||
#[tauri::command]
|
||||
fn export(app: AppHandle, state: State<'_, AppState>, request: ExportRequest) -> Result<ExportResult, String> {
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
let clip = state
|
||||
.last_clip
|
||||
.lock()
|
||||
.expect("clip mutex")
|
||||
let cfg = lock(&state.config).clone();
|
||||
let clip = lock(&state.last_clip)
|
||||
.clone()
|
||||
.ok_or_else(|| "no recorded clip available".to_string())?;
|
||||
|
||||
@@ -1238,6 +1255,7 @@ fn export(app: AppHandle, state: State<'_, AppState>, request: ExportRequest) ->
|
||||
time_secs: c.time_secs,
|
||||
x: (((c.x - src_origin.0) as f64) * scale).round() as i32,
|
||||
y: (((c.y - src_origin.1) as f64) * scale).round() as i32,
|
||||
button: c.button,
|
||||
})
|
||||
.collect();
|
||||
|
||||
@@ -1258,6 +1276,8 @@ fn export(app: AppHandle, state: State<'_, AppState>, request: ExportRequest) ->
|
||||
trim_start_secs: trim_start,
|
||||
trim_end_secs: trim_end,
|
||||
clicks: transformed_clicks,
|
||||
clip_width: clip.width,
|
||||
clip_height: clip.height,
|
||||
progress_tx: None,
|
||||
};
|
||||
|
||||
@@ -1322,6 +1342,7 @@ pub fn run() {
|
||||
last_clip: Mutex::new(None),
|
||||
session: Mutex::new(None),
|
||||
picker: Mutex::new(None),
|
||||
overlay_poll_shutdown: Arc::new(AtomicBool::new(false)),
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
@@ -1334,8 +1355,8 @@ pub fn run() {
|
||||
}
|
||||
let cfg = {
|
||||
let st = app.state::<AppState>();
|
||||
let guard = st.config.lock().expect("config mutex");
|
||||
guard.clone()
|
||||
let cfg = lock(&st.config).clone();
|
||||
cfg
|
||||
};
|
||||
let kind = if cfg
|
||||
.hotkeys
|
||||
@@ -1377,11 +1398,14 @@ pub fn run() {
|
||||
.setup(|app| {
|
||||
let (check_on_launch, cfg) = {
|
||||
let st = app.state::<AppState>();
|
||||
let cfg = st.config.lock().expect("config mutex").clone();
|
||||
let cfg = lock(&st.config).clone();
|
||||
(cfg.updates.check_on_launch, cfg)
|
||||
};
|
||||
let _ = register_hotkeys(app.handle(), &cfg);
|
||||
start_overlay_clickthrough_poll(app.handle().clone());
|
||||
{
|
||||
let st = app.state::<AppState>();
|
||||
start_overlay_clickthrough_poll(app.handle().clone(), st.overlay_poll_shutdown.clone());
|
||||
}
|
||||
if check_on_launch {
|
||||
let handle = app.handle().clone();
|
||||
tauri::async_runtime::spawn(async move {
|
||||
|
||||
Reference in New Issue
Block a user