Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop): position recording controls relative to capture target

Richie McIlroy2d06e498ec8b
diff --git a/apps/desktop/src-tauri/src/fake_window.rs b/apps/desktop/src-tauri/src/fake_window.rs
index 31d35d6f5..3ed756fc8 100644
--- a/apps/desktop/src-tauri/src/fake_window.rs
+++ b/apps/desktop/src-tauri/src/fake_window.rs
@@ -1,9 +1,12 @@
-use scap_targets::{Display, DisplayId, bounds::LogicalBounds};
+use cap_recording::sources::screen_capture::ScreenCaptureTarget;
+use scap_targets::{Display, DisplayId, Window as ScapWindow, bounds::LogicalBounds};
use std::{collections::HashMap, sync::Arc, time::Duration};
use tauri::{AppHandle, Manager, WebviewWindow};
use tokio::{sync::RwLock, time::sleep};
use tracing::instrument;
+use crate::{App, ArcLock, RecordingState};
+
const RECORDING_CONTROLS_LABEL: &str = "in-progress-recording";
const RECORDING_CONTROLS_WIDTH: f64 = 320.0;
const RECORDING_CONTROLS_HEIGHT: f64 = 150.0;
@@ -70,6 +73,37 @@ fn calculate_bottom_center_position(display: &Display) -> Option<(f64, f64)> {
Some((pos_x, pos_y))
}
+const TARGET_CONTROLS_OFFSET_Y: f64 = 48.0;
+
+pub fn calculate_recording_controls_position_for_target(
+ capture_target: &ScreenCaptureTarget,
+) -> Option<(f64, f64)> {
+ match capture_target {
+ ScreenCaptureTarget::Window { id } => {
+ let window = ScapWindow::from_id(id)?;
+ let bounds = window.raw_handle().logical_bounds()?;
+ let pos_x =
+ bounds.position().x() + (bounds.size().width() - RECORDING_CONTROLS_WIDTH) / 2.0;
+ let pos_y = bounds.position().y() + bounds.size().height()
+ - RECORDING_CONTROLS_HEIGHT
+ - TARGET_CONTROLS_OFFSET_Y;
+ Some((pos_x, pos_y))
+ }
+ ScreenCaptureTarget::Area { screen, bounds } => {
+ let display = Display::from_id(screen)?;
+ let display_bounds = display.raw_handle().logical_bounds()?;
+ let abs_x = display_bounds.position().x() + bounds.position().x();
+ let abs_y = display_bounds.position().y() + bounds.position().y();
+ let pos_x = abs_x + (bounds.size().width() - RECORDING_CONTROLS_WIDTH) / 2.0;
+ let pos_y = abs_y + bounds.size().height()
+ - RECORDING_CONTROLS_HEIGHT
+ - TARGET_CONTROLS_OFFSET_Y;
+ Some((pos_x, pos_y))
+ }
+ _ => None,
+ }
+}
+
pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
window.set_ignore_cursor_events(true).ok();
@@ -78,19 +112,59 @@ pub fn spawn_fake_window_listener(app: AppHandle, window: WebviewWindow) {
tokio::spawn(async move {
let state = app.state::<FakeWindowBounds>();
let mut current_display_id: Option<DisplayId> = get_display_id_for_cursor();
+ let mut last_target_pos: Option<(f64, f64)> = None;
loop {
sleep(Duration::from_millis(1000 / 20)).await;
- if is_recording_controls && let Some(cursor_display_id) = get_display_id_for_cursor() {
- let display_changed = current_display_id.as_ref() != Some(&cursor_display_id);
+ if is_recording_controls {
+ let capture_target = app.state::<ArcLock<App>>().try_read().ok().and_then(|s| {
+ match &s.recording_state {
+ RecordingState::Pending { target, .. } => Some(target.clone()),
+ RecordingState::Active(inner) => Some(inner.capture_target().clone()),
+ RecordingState::None => None,
+ }
+ });
+
+ let mut handled = false;
+
+ if let Some(ref target) = capture_target {
+ match target {
+ ScreenCaptureTarget::Window { .. } => {
+ if let Some((px, py)) =
+ calculate_recording_controls_position_for_target(target)
+ {
+ let changed = match last_target_pos {
+ Some((lx, ly)) => {
+ (px - lx).abs() > 0.5 || (py - ly).abs() > 0.5
+ }
+ None => true,
+ };
+ if changed {
+ let _ =
+ window.set_position(tauri::LogicalPosition::new(px, py));
+ last_target_pos = Some((px, py));
+ }
+ handled = true;
+ }
+ }
+ ScreenCaptureTarget::Area { .. } => {
+ handled = true;
+ }
+ _ => {}
+ }
+ }
+
+ if !handled && let Some(cursor_display_id) = get_display_id_for_cursor() {
+ let display_changed = current_display_id.as_ref() != Some(&cursor_display_id);
- if display_changed
- && let Some(display) = get_display_by_id(&cursor_display_id)
- && let Some((pos_x, pos_y)) = calculate_bottom_center_position(&display)
- {
- let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
- current_display_id = Some(cursor_display_id);
+ if display_changed
+ && let Some(display) = get_display_by_id(&cursor_display_id)
+ && let Some((pos_x, pos_y)) = calculate_bottom_center_position(&display)
+ {
+ let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
+ current_display_id = Some(cursor_display_id);
+ }
}
}
diff --git a/apps/desktop/src-tauri/src/recording.rs b/apps/desktop/src-tauri/src/recording.rs
index c933ab89c..a2ce29ed7 100644
--- a/apps/desktop/src-tauri/src/recording.rs
+++ b/apps/desktop/src-tauri/src/recording.rs
@@ -663,7 +663,7 @@ pub async fn start_recording(
Some(project_name.clone()),
None,
inputs.organization_id.clone(),
- "desktopSegments",
+ upload_mode,
)
.await
{
diff --git a/apps/desktop/src-tauri/src/windows.rs b/apps/desktop/src-tauri/src/windows.rs
index 6afe58324..7e9298f81 100644
--- a/apps/desktop/src-tauri/src/windows.rs
+++ b/apps/desktop/src-tauri/src/windows.rs
@@ -39,7 +39,7 @@ use crate::{
target_select_overlay::WindowFocusManager,
window_exclusion::WindowExclusion,
};
-use cap_recording::feeds;
+use cap_recording::{feeds, sources::screen_capture::ScreenCaptureTarget};
#[cfg(target_os = "macos")]
const DEFAULT_TRAFFIC_LIGHTS_INSET: LogicalPosition<f64> = LogicalPosition::new(12.0, 12.0);
@@ -629,6 +629,8 @@ pub enum ShowCapWindow {
},
InProgressRecording {
countdown: Option<u32>,
+ #[serde(default)]
+ capture_target: Option<ScreenCaptureTarget>,
},
Upgrade,
ModeSelect,
@@ -906,7 +908,7 @@ impl ShowCapWindow {
}
#[cfg(target_os = "macos")]
- if let Self::InProgressRecording { .. } = self
+ if let Self::InProgressRecording { capture_target, .. } = self
&& let Some(window) = self.id(app).get(app)
{
use crate::panel_manager::is_window_handle_valid;
@@ -915,8 +917,12 @@ impl ShowCapWindow {
debug!("InProgressRecording: reusing existing window");
let width = 320.0;
let height = 150.0;
- let recording_monitor = CursorMonitorInfo::get();
- let (pos_x, pos_y) = recording_monitor.bottom_center_position(width, height, 120.0);
+ let (pos_x, pos_y) = capture_target
+ .as_ref()
+ .and_then(fake_window::calculate_recording_controls_position_for_target)
+ .unwrap_or_else(|| {
+ CursorMonitorInfo::get().bottom_center_position(width, height, 120.0)
+ });
let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
let label = window.label().to_string();
@@ -960,13 +966,17 @@ impl ShowCapWindow {
}
#[cfg(not(target_os = "macos"))]
- if let Self::InProgressRecording { .. } = self
+ if let Self::InProgressRecording { capture_target, .. } = self
&& let Some(window) = self.id(app).get(app)
{
let width = 320.0;
let height = 150.0;
- let recording_monitor = CursorMonitorInfo::get();
- let (pos_x, pos_y) = recording_monitor.bottom_center_position(width, height, 120.0);
+ let (pos_x, pos_y) = capture_target
+ .as_ref()
+ .and_then(fake_window::calculate_recording_controls_position_for_target)
+ .unwrap_or_else(|| {
+ CursorMonitorInfo::get().bottom_center_position(width, height, 120.0)
+ });
let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
window.show().ok();
window.set_focus().ok();
@@ -1894,13 +1904,20 @@ impl ShowCapWindow {
window
}
- Self::InProgressRecording { countdown } => {
+ Self::InProgressRecording {
+ countdown,
+ capture_target,
+ } => {
let width = 320.0;
let height = 150.0;
let title = CapWindowId::RecordingControls.title();
let should_protect = should_protect_window(app, &title);
+ #[cfg(target_os = "macos")]
+ app.set_activation_policy(tauri::ActivationPolicy::Accessory)
+ .ok();
+
#[cfg(target_os = "macos")]
let window = {
self.window_builder(app, "/in-progress-recording")
@@ -1944,7 +1961,10 @@ impl ShowCapWindow {
#[cfg(target_os = "windows")]
log_window_content_protection(&window, should_protect, &title);
- let (pos_x, pos_y) = cursor_monitor.bottom_center_position(width, height, 120.0);
+ let (pos_x, pos_y) = capture_target
+ .as_ref()
+ .and_then(fake_window::calculate_recording_controls_position_for_target)
+ .unwrap_or_else(|| cursor_monitor.bottom_center_position(width, height, 120.0));
let _ = window.set_position(tauri::LogicalPosition::new(pos_x, pos_y));
debug!(
diff --git a/apps/desktop/src/utils/tauri.ts b/apps/desktop/src/utils/tauri.ts
index 8cf4c2533..efdc6dee6 100644
--- a/apps/desktop/src/utils/tauri.ts
+++ b/apps/desktop/src/utils/tauri.ts
@@ -573,7 +573,7 @@ export type SerializedScreenshotEditorInstance = { framesSocketUrl: string; path
export type SetCaptureAreaPending = boolean
export type ShadowConfiguration = { size: number; opacity: number; blur: number }
export type SharingMeta = { id: string; link: string }
-export type ShowCapWindow = { Main: { init_target_mode: RecordingTargetMode | null } } | { Settings: { page: string | null } } | { Editor: { project_path: string } } | "RecordingsOverlay" | { WindowCaptureOccluder: { screen_id: DisplayId } } | { TargetSelectOverlay: { display_id: DisplayId; target_mode: RecordingTargetMode | null } } | { CaptureArea: { screen_id: DisplayId } } | { Camera: { centered: boolean } } | { InProgressRecording: { countdown: number | null } } | "Upgrade" | "ModeSelect" | { ScreenshotEditor: { path: string } } | "Onboarding"
+export type ShowCapWindow = { Main: { init_target_mode: RecordingTargetMode | null } } | { Settings: { page: string | null } } | { Editor: { project_path: string } } | "RecordingsOverlay" | { WindowCaptureOccluder: { screen_id: DisplayId } } | { TargetSelectOverlay: { display_id: DisplayId; target_mode: RecordingTargetMode | null } } | { CaptureArea: { screen_id: DisplayId } } | { Camera: { centered: boolean } } | { InProgressRecording: { countdown: number | null; capture_target?: ScreenCaptureTarget | null } } | "Upgrade" | "ModeSelect" | { ScreenshotEditor: { path: string } } | "Onboarding"
export type SingleSegment = { display: VideoMeta; camera?: VideoMeta | null; audio?: AudioMeta | null; cursor?: string | null }
export type StartRecordingInputs = { capture_target: ScreenCaptureTarget; capture_system_audio?: boolean; mode: RecordingMode; organization_id?: string | null }
export type StereoMode = "stereo" | "monoL" | "monoR"