region capture overlay window
This commit is contained in:
+82
-4
@@ -9,11 +9,19 @@ use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tauri::{AppHandle, Emitter, State};
|
||||
use tauri::{AppHandle, Emitter, Manager, State};
|
||||
|
||||
use crate::bootstrap::FfmpegStatus;
|
||||
use crate::config::{Config, Format, GifDither, Mp4Codec, WebmCodec};
|
||||
|
||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
||||
pub struct CaptureRect {
|
||||
pub x: i32,
|
||||
pub y: i32,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
pub app_dir: PathBuf,
|
||||
pub config: Mutex<Config>,
|
||||
@@ -27,6 +35,7 @@ pub struct RecordedClip {
|
||||
pub duration_secs: f64,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub crop: Option<CaptureRect>,
|
||||
}
|
||||
|
||||
fn exe_dir() -> Result<PathBuf> {
|
||||
@@ -174,8 +183,16 @@ async fn download_ffmpeg(app: AppHandle, state: State<'_, AppState>) -> Result<(
|
||||
.map_err(|e| format!("{e:#}"))
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct StartCaptureRequest {
|
||||
pub crop: Option<CaptureRect>,
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn start_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
fn start_capture(
|
||||
state: State<'_, AppState>,
|
||||
request: Option<StartCaptureRequest>,
|
||||
) -> Result<(), String> {
|
||||
let mut slot = state.capture.lock().expect("capture mutex");
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
@@ -186,8 +203,14 @@ fn start_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
let output = temp.join("recording.mp4");
|
||||
let _ = std::fs::remove_file(&output);
|
||||
|
||||
let session = capture::start(output, cfg.capture.capture_cursor, cfg.capture.capture_audio)
|
||||
.map_err(|e| format!("{e:#}"))?;
|
||||
let crop = request.and_then(|r| r.crop);
|
||||
let session = capture::start(
|
||||
output,
|
||||
cfg.capture.capture_cursor,
|
||||
cfg.capture.capture_audio,
|
||||
crop,
|
||||
)
|
||||
.map_err(|e| format!("{e:#}"))?;
|
||||
*slot = Some(session);
|
||||
Ok(())
|
||||
}
|
||||
@@ -203,6 +226,7 @@ fn stop_capture(state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
let duration_secs = session.started_at.elapsed().as_secs_f64();
|
||||
let width = session.width;
|
||||
let height = session.height;
|
||||
let crop = session.crop;
|
||||
let temp_path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
|
||||
let clip = RecordedClip {
|
||||
@@ -210,6 +234,7 @@ fn stop_capture(state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
duration_secs,
|
||||
width,
|
||||
height,
|
||||
crop,
|
||||
};
|
||||
*state.last_clip.lock().expect("clip mutex") = Some(clip.clone());
|
||||
Ok(clip)
|
||||
@@ -229,6 +254,55 @@ pub struct ExportResult {
|
||||
pub size_bytes: u64,
|
||||
}
|
||||
|
||||
const OVERLAY_BORDER_LOGICAL_PX: f64 = 2.0;
|
||||
const OVERLAY_TITLEBAR_LOGICAL_PX: f64 = 22.0;
|
||||
|
||||
#[tauri::command]
|
||||
fn show_overlay(app: AppHandle) -> Result<(), String> {
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
win.show().map_err(|e| e.to_string())?;
|
||||
win.set_focus().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn hide_overlay(app: AppHandle) -> Result<(), String> {
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
win.hide().map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn get_overlay_rect(app: AppHandle) -> Result<CaptureRect, String> {
|
||||
let win = app
|
||||
.get_webview_window("overlay")
|
||||
.ok_or_else(|| "overlay window not found".to_string())?;
|
||||
let pos = win.outer_position().map_err(|e| e.to_string())?;
|
||||
let size = win.outer_size().map_err(|e| e.to_string())?;
|
||||
let scale = win.scale_factor().map_err(|e| e.to_string())?;
|
||||
|
||||
let border_phys = (OVERLAY_BORDER_LOGICAL_PX * scale).round() as i32;
|
||||
let titlebar_phys = (OVERLAY_TITLEBAR_LOGICAL_PX * scale).round() as i32;
|
||||
|
||||
let inset_top = border_phys + titlebar_phys;
|
||||
let inset_sides = border_phys;
|
||||
let inset_bottom = border_phys;
|
||||
|
||||
let width = (size.width as i32 - inset_sides * 2).max(2) as u32;
|
||||
let height = (size.height as i32 - inset_top - inset_bottom).max(2) as u32;
|
||||
let width = (width / 2) * 2;
|
||||
let height = (height / 2) * 2;
|
||||
|
||||
Ok(CaptureRect {
|
||||
x: pos.x + inset_sides,
|
||||
y: pos.y + inset_top,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn reveal_in_explorer(path: String) -> Result<(), String> {
|
||||
use std::process::Command;
|
||||
@@ -274,6 +348,7 @@ fn export(state: State<'_, AppState>, request: ExportRequest) -> Result<ExportRe
|
||||
webm_codec: cfg.export.webm_codec,
|
||||
mp4_codec: cfg.export.mp4_codec,
|
||||
scratch_dir: temp_dir(&state.app_dir),
|
||||
crop: clip.crop,
|
||||
};
|
||||
|
||||
encode::transcode(&ffmpeg, &req).map_err(|e| format!("{e:#}"))?;
|
||||
@@ -321,6 +396,9 @@ pub fn run() {
|
||||
stop_capture,
|
||||
export,
|
||||
reveal_in_explorer,
|
||||
show_overlay,
|
||||
hide_overlay,
|
||||
get_overlay_rect,
|
||||
])
|
||||
.run(tauri::generate_context!())
|
||||
.expect("error while running ByteSize")
|
||||
|
||||
Reference in New Issue
Block a user