Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

improve(recording): position area controls outside capture

Richie McIlroyfaa2b2a1155f
diff --git a/apps/desktop/src-tauri/src/fake_window.rs b/apps/desktop/src-tauri/src/fake_window.rs
index c00429e47..ec5b011cb 100644
--- a/apps/desktop/src-tauri/src/fake_window.rs
+++ b/apps/desktop/src-tauri/src/fake_window.rs
@@ -18,6 +18,8 @@ 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;
+const RECORDING_CONTROLS_BAR_HEIGHT: f64 = 40.0;
+const RECORDING_CONTROLS_BOTTOM_PADDING: f64 = 12.0;
const RECORDING_CONTROLS_OFFSET_Y: f64 = 120.0;
const TICK_INTERVAL: Duration = Duration::from_millis(50);
const DEAD_WINDOW_ERROR_THRESHOLD: u8 = 5;
@@ -254,6 +256,43 @@ fn calculate_bottom_center_position(display: &Display) -> Option<(f64, f64)> {
}
const TARGET_CONTROLS_OFFSET_Y: f64 = 48.0;
+const AREA_CONTROLS_MARGIN: f64 = 16.0;
+
+fn calculate_area_recording_controls_position(
+ display_bounds: LogicalBounds,
+ area_bounds: LogicalBounds,
+) -> (f64, f64) {
+ let display_left = display_bounds.position().x();
+ let display_top = display_bounds.position().y();
+ let display_right = display_left + display_bounds.size().width();
+ let display_bottom = display_top + display_bounds.size().height();
+ let area_left = display_left + area_bounds.position().x();
+ let area_top = display_top + area_bounds.position().y();
+ let area_bottom = area_top + area_bounds.size().height();
+
+ let max_x = (display_right - RECORDING_CONTROLS_WIDTH).max(display_left);
+ let pos_x = (area_left + (area_bounds.size().width() - RECORDING_CONTROLS_WIDTH) / 2.0)
+ .clamp(display_left, max_x);
+ let bar_top_offset = RECORDING_CONTROLS_HEIGHT
+ - RECORDING_CONTROLS_BOTTOM_PADDING
+ - RECORDING_CONTROLS_BAR_HEIGHT;
+ let bar_bottom_offset = RECORDING_CONTROLS_HEIGHT - RECORDING_CONTROLS_BOTTOM_PADDING;
+
+ let below_y = area_bottom + AREA_CONTROLS_MARGIN - bar_top_offset;
+ if below_y >= display_top && below_y + RECORDING_CONTROLS_HEIGHT <= display_bottom {
+ return (pos_x, below_y);
+ }
+
+ let above_y = area_top - AREA_CONTROLS_MARGIN - bar_bottom_offset;
+ if above_y >= display_top && above_y + RECORDING_CONTROLS_HEIGHT <= display_bottom {
+ return (pos_x, above_y);
+ }
+
+ let max_y = (display_bottom - RECORDING_CONTROLS_HEIGHT).max(display_top);
+ let fallback_y = (area_bottom - RECORDING_CONTROLS_HEIGHT - TARGET_CONTROLS_OFFSET_Y)
+ .clamp(display_top, max_y);
+ (pos_x, fallback_y)
+}
pub fn calculate_recording_controls_position_for_target(
capture_target: &ScreenCaptureTarget,
@@ -272,13 +311,10 @@ pub fn calculate_recording_controls_position_for_target(
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))
+ Some(calculate_area_recording_controls_position(
+ display_bounds,
+ *bounds,
+ ))
}
_ => None,
}
@@ -604,4 +640,47 @@ mod tests {
2.0,
));
}
+
+ #[test]
+ fn area_recording_controls_prefer_below_the_capture() {
+ let display = bounds(100.0, 200.0, 1920.0, 1080.0);
+ let area = bounds(200.0, 100.0, 800.0, 500.0);
+
+ let position = calculate_area_recording_controls_position(display, area);
+
+ assert_eq!(position, (540.0, 718.0));
+ }
+
+ #[test]
+ fn area_recording_controls_move_above_when_below_does_not_fit() {
+ let display = bounds(0.0, 0.0, 1920.0, 1080.0);
+ let area = bounds(300.0, 600.0, 800.0, 440.0);
+
+ let position = calculate_area_recording_controls_position(display, area);
+
+ assert_eq!(position, (540.0, 446.0));
+ }
+
+ #[test]
+ fn area_recording_controls_keep_the_existing_inside_fallback() {
+ let display = bounds(0.0, 0.0, 1920.0, 1080.0);
+ let area = bounds(300.0, 40.0, 800.0, 1000.0);
+
+ let position = calculate_area_recording_controls_position(display, area);
+
+ assert_eq!(position, (540.0, 842.0));
+ }
+
+ #[test]
+ fn area_recording_controls_stay_within_horizontal_display_edges() {
+ let display = bounds(100.0, 200.0, 1920.0, 1080.0);
+ let left_area = bounds(0.0, 100.0, 150.0, 300.0);
+ let right_area = bounds(1770.0, 100.0, 150.0, 300.0);
+
+ let left_position = calculate_area_recording_controls_position(display, left_area);
+ let right_position = calculate_area_recording_controls_position(display, right_area);
+
+ assert_eq!(left_position.0, 100.0);
+ assert_eq!(right_position.0, 1700.0);
+ }
}