fix the behaviour bugs in capture, encode and the overlay poller
This commit is contained in:
+97
-65
@@ -64,6 +64,10 @@ pub struct AppState {
|
||||
pub picker: Mutex<Option<window_picker::PickerSession>>,
|
||||
pub overlay_poll_shutdown: Arc<AtomicBool>,
|
||||
pub export_cancel: Arc<AtomicBool>,
|
||||
/// Held for the duration of an export. Without it a second export could
|
||||
/// start while the first was still encoding, and its reset of the shared
|
||||
/// cancel flag would revive the run the user had just cancelled.
|
||||
pub exporting: Arc<AtomicBool>,
|
||||
pub overlay_aspect_ratio: Mutex<Option<f64>>,
|
||||
/// (active flag, condvar) so the overlay click-through poll thread can park
|
||||
/// itself when no overlay is on screen instead of waking every 80ms.
|
||||
@@ -466,12 +470,6 @@ fn pad_audio_head(path: &Path, sample_rate: u32, channels: u16, head_secs: f64)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn set_overlay_clickthrough(app: &AppHandle, enabled: bool) {
|
||||
if let Some(win) = app.get_webview_window("overlay") {
|
||||
drop(win.set_ignore_cursor_events(enabled));
|
||||
}
|
||||
}
|
||||
|
||||
fn try_start_audio(
|
||||
app: &AppHandle,
|
||||
enabled: bool,
|
||||
@@ -524,9 +522,18 @@ fn watch_audio_death(app: AppHandle, died: Arc<AtomicBool>) {
|
||||
}
|
||||
};
|
||||
if let Some(session) = take {
|
||||
// Drain the dead thread so its handle doesn't leak. The pcm
|
||||
// it already wrote stays in place for stop_capture to mux.
|
||||
drop(session.stop());
|
||||
// Drain the dead thread so its handle doesn't leak, and keep the
|
||||
// pcm it already wrote: stop_capture muxes whatever is in
|
||||
// audio_segments, so discarding the path here threw away
|
||||
// everything captured before the device went away.
|
||||
let path = session.stop();
|
||||
{
|
||||
let st = app.state::<AppState>();
|
||||
let mut slot = lock(&st.session);
|
||||
if let Some(ss) = slot.as_mut() {
|
||||
ss.audio_segments.push(path);
|
||||
}
|
||||
}
|
||||
drop(app.emit(
|
||||
"audio-failed",
|
||||
"audio device lost - endpoint changed or was unplugged".to_string(),
|
||||
@@ -577,8 +584,6 @@ fn start_capture(
|
||||
)
|
||||
.map_err(|e| format!("{e:#}"))?;
|
||||
|
||||
set_overlay_clickthrough(&app, true);
|
||||
|
||||
let audio_path = audio_segment_path(&state.app_dir, 1);
|
||||
let audio_session = try_start_audio(&app, cfg.capture.capture_audio, audio_path);
|
||||
|
||||
@@ -756,9 +761,7 @@ fn restart_audio_capture(
|
||||
reason = "one recording-finalisation transaction; the steps share session state and temp paths"
|
||||
)]
|
||||
#[tauri::command]
|
||||
fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
set_overlay_clickthrough(&app, false);
|
||||
|
||||
fn stop_capture(state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
let mut ss = lock(&state.session)
|
||||
.take()
|
||||
.ok_or_else(|| "no session to stop".to_string())?;
|
||||
@@ -1245,18 +1248,66 @@ fn cleanup_temp_on_exit(app: &AppHandle) {
|
||||
clear_temp_segments(&st.app_dir, false);
|
||||
}
|
||||
|
||||
/// Whether the overlay should currently ignore the cursor. `None` means there is
|
||||
/// nothing to decide: no overlay window, it is hidden, or a query failed.
|
||||
///
|
||||
/// This is the only writer of the overlay's click-through state. `start_capture`
|
||||
/// and `stop_capture` used to flip the same property directly, which desynced it
|
||||
/// from the value tracked in the poll loop: after a recording stopped, the poller
|
||||
/// believed the frame was already click-through and never put it back.
|
||||
fn overlay_should_ignore_cursor(app: &AppHandle) -> Option<bool> {
|
||||
use windows::Win32::Foundation::{HWND, POINT};
|
||||
use windows::Win32::UI::HiDpi::GetDpiForWindow;
|
||||
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;
|
||||
|
||||
let overlay = app.get_webview_window("overlay")?;
|
||||
if !overlay.is_visible().unwrap_or(false) {
|
||||
return None;
|
||||
}
|
||||
let state = app.state::<AppState>();
|
||||
if lock(&state.capture).is_some() {
|
||||
// Mid-recording the frame is inert, so clicks reach the app below it.
|
||||
return Some(true);
|
||||
}
|
||||
|
||||
let mut pt = POINT::default();
|
||||
// SAFETY: `pt` is a live local that GetCursorPos writes through.
|
||||
if unsafe { GetCursorPos(&raw mut pt) }.is_err() {
|
||||
return None;
|
||||
}
|
||||
let raw = overlay.hwnd().ok()?;
|
||||
let (vl, vt, vr, vb) = visible_window_bounds(raw.0 as isize)?;
|
||||
|
||||
// SAFETY: `raw` is the overlay's own live HWND from Tauri.
|
||||
let dpi = unsafe { GetDpiForWindow(HWND(raw.0.cast::<std::ffi::c_void>())) };
|
||||
let scale = if dpi > 0 { f64::from(dpi) / 96.0 } else { 1.0 };
|
||||
let border = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
|
||||
let titlebar = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
|
||||
|
||||
// The border and titlebar stay interactive so the frame can still be dragged
|
||||
// and resized; only the capture area itself passes clicks through.
|
||||
let inner_left = vl + border;
|
||||
let inner_top = vt + border + titlebar;
|
||||
let inner_right = vr - border;
|
||||
let inner_bottom = vb - border;
|
||||
|
||||
Some(
|
||||
pt.x >= inner_left
|
||||
&& pt.x < inner_right
|
||||
&& pt.y >= inner_top
|
||||
&& pt.y < inner_bottom,
|
||||
)
|
||||
}
|
||||
|
||||
fn start_overlay_clickthrough_poll(
|
||||
app: AppHandle,
|
||||
shutdown: Arc<AtomicBool>,
|
||||
live: Arc<(Mutex<bool>, std::sync::Condvar)>,
|
||||
) {
|
||||
use std::time::Duration;
|
||||
use windows::Win32::Foundation::{HWND, POINT};
|
||||
use windows::Win32::UI::HiDpi::GetDpiForWindow;
|
||||
use windows::Win32::UI::WindowsAndMessaging::GetCursorPos;
|
||||
|
||||
std::thread::spawn(move || {
|
||||
let mut last_inner: Option<bool> = None;
|
||||
let mut last: Option<bool> = None;
|
||||
while !shutdown.load(Ordering::Relaxed) {
|
||||
// Park until the overlay is shown. Without this the poller woke
|
||||
// every 80ms for the whole process lifetime just to re-check
|
||||
@@ -1273,52 +1324,18 @@ fn start_overlay_clickthrough_poll(
|
||||
}
|
||||
}
|
||||
|
||||
let Some(overlay) = app.get_webview_window("overlay") else {
|
||||
continue;
|
||||
};
|
||||
if !overlay.is_visible().unwrap_or(false) {
|
||||
last_inner = None;
|
||||
continue;
|
||||
let target = overlay_should_ignore_cursor(&app);
|
||||
if target != last {
|
||||
if let (Some(overlay), Some(value)) = (app.get_webview_window("overlay"), target)
|
||||
{
|
||||
drop(overlay.set_ignore_cursor_events(value));
|
||||
}
|
||||
last = target;
|
||||
}
|
||||
|
||||
let state = app.state::<AppState>();
|
||||
let is_recording = lock(&state.capture).is_some();
|
||||
if is_recording {
|
||||
last_inner = Some(true);
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut pt = POINT::default();
|
||||
// SAFETY: `pt` is a live local that GetCursorPos writes through.
|
||||
if unsafe { GetCursorPos(&raw mut pt) }.is_err() {
|
||||
continue;
|
||||
}
|
||||
let Ok(raw) = overlay.hwnd() else { continue };
|
||||
|
||||
let Some((vl, vt, vr, vb)) = visible_window_bounds(raw.0 as isize) else {
|
||||
continue;
|
||||
};
|
||||
|
||||
// SAFETY: `raw` is the overlay's own live HWND from Tauri.
|
||||
let dpi = unsafe { GetDpiForWindow(HWND(raw.0.cast::<std::ffi::c_void>())) };
|
||||
let scale = if dpi > 0 { f64::from(dpi) / 96.0 } else { 1.0 };
|
||||
let border = encode::round_i32(OVERLAY_BORDER_LOGICAL_PX * scale);
|
||||
let titlebar = encode::round_i32(OVERLAY_TITLEBAR_LOGICAL_PX * scale);
|
||||
|
||||
let inner_left = vl + border;
|
||||
let inner_top = vt + border + titlebar;
|
||||
let inner_right = vr - border;
|
||||
let inner_bottom = vb - border;
|
||||
|
||||
let in_inner = pt.x >= inner_left
|
||||
&& pt.x < inner_right
|
||||
&& pt.y >= inner_top
|
||||
&& pt.y < inner_bottom;
|
||||
|
||||
if last_inner != Some(in_inner) {
|
||||
drop(overlay.set_ignore_cursor_events(in_inner));
|
||||
last_inner = Some(in_inner);
|
||||
}
|
||||
// The only sleep in the loop, and deliberately unconditional: every
|
||||
// branch above has to pace itself, or the thread spins at full speed
|
||||
// for the whole recording.
|
||||
std::thread::sleep(Duration::from_millis(80));
|
||||
}
|
||||
});
|
||||
@@ -1404,9 +1421,9 @@ fn hide_overlay(app: AppHandle) -> Result<(), String> {
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
// Reset click-through so the next show starts from a known state, and park
|
||||
// the poll thread until show_overlay wakes it again.
|
||||
drop(win.set_ignore_cursor_events(false));
|
||||
// Leave the click-through state alone - it is the poll thread's to own, and
|
||||
// it stays consistent with the window because nothing else writes it. Just
|
||||
// park the poller until show_overlay wakes it again.
|
||||
{
|
||||
let st = app.state::<AppState>();
|
||||
let (lock, cvar) = &*st.overlay_poll_live;
|
||||
@@ -1934,6 +1951,15 @@ fn codec_availability(state: State<'_, AppState>) -> Result<encode::CodecAvailab
|
||||
Ok(encode::codec_availability(&ffmpeg))
|
||||
}
|
||||
|
||||
/// Clears the export flag on every exit path, including the `?` returns below.
|
||||
struct ExportGuard(Arc<AtomicBool>);
|
||||
|
||||
impl Drop for ExportGuard {
|
||||
fn drop(&mut self) {
|
||||
self.0.store(false, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
|
||||
// One export is one transaction: resolve the output path, translate the trim
|
||||
// and crop into encoder terms, run the encode, and report the size. Splitting
|
||||
// it would mean threading the same resolved state through the pieces.
|
||||
@@ -1943,6 +1969,11 @@ fn codec_availability(state: State<'_, AppState>) -> Result<encode::CodecAvailab
|
||||
)]
|
||||
#[tauri::command]
|
||||
async fn export(app: AppHandle, state: State<'_, AppState>, request: ExportRequest) -> Result<ExportResult, String> {
|
||||
if state.exporting.swap(true, Ordering::SeqCst) {
|
||||
return Err("an export is already running".to_string());
|
||||
}
|
||||
let _export = ExportGuard(state.exporting.clone());
|
||||
|
||||
let (cfg, clip, app_dir, export_cancel) = {
|
||||
let cfg = lock(&state.config).clone();
|
||||
let clip = lock(&state.last_clip)
|
||||
@@ -2148,6 +2179,7 @@ pub fn run() {
|
||||
picker: Mutex::new(None),
|
||||
overlay_poll_shutdown: Arc::new(AtomicBool::new(false)),
|
||||
export_cancel: Arc::new(AtomicBool::new(false)),
|
||||
exporting: Arc::new(AtomicBool::new(false)),
|
||||
overlay_aspect_ratio: Mutex::new(None),
|
||||
overlay_poll_live: Arc::new((Mutex::new(false), std::sync::Condvar::new())),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user