Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop-gpui): movable manual captions, area aspect ratio lock, playhead clamp after shortening edits (#3230)

Richie McIlroya8e23af2f08d
diff --git a/apps/desktop-gpui/README.md b/apps/desktop-gpui/README.md
index e3c2c0853..fc9fadcad 100644
--- a/apps/desktop-gpui/README.md
+++ b/apps/desktop-gpui/README.md
@@ -697,7 +697,7 @@ Recording-specific deviations:
| **Settings button is inert** | The bar's recording-settings popover menu is not built; the same applies to the gear button in the overlay's start cluster. |
| **No overlay blur** | The recording overlay is `bg-gray-1/80` without `backdrop-blur-xs`; this gpui rev has no per-element backdrop blur hook. The target-select overlay's liquid-glass surfaces drop their `backdrop-blur-xl` for the same reason. |
| **Escape is a focused key handler** | The Tauri app registers Escape as a *global* shortcut while the overlays are up; here it is a key handler on the overlay that holds focus (the one on the cursor's display). Escape pressed while another app is active does not dismiss. |
-| **Overlay cluster is start-only** | The device row (camera/microphone selects under the start pill), the mode dropdown behind the caret, and the "What is X Mode?" link are deferred — device pickers live in the main window. The area toolbar shows the size readout but not the aspect-ratio/reset/fill/lock controls. |
+| **Overlay cluster is start-only** | The device row (camera/microphone selects under the start pill), the mode dropdown behind the caret, and the "What is X Mode?" link are deferred — device pickers live in the main window. The area toolbar shows the size readout and an aspect-ratio control, but not the reset/fill/lock controls. |
| **Camera-only keeps the bubble** | The TSX inlines a camera preview into the camera-only overlay; ours keeps the separate camera preview window it already has. |
| **Camera id is DeviceID-only** | The Tauri app persists `ModelID` when a camera advertises one, so the same camera survives re-plugging into a different port. |
| **Defaults are the builder's** | `desktop_recording_defaults` (studio quality, fps caps, custom cursor) is not applied yet — it reads the Tauri settings store. |
diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rs
index 9ab09bb06..1e999966a 100644
--- a/apps/desktop-gpui/src/editor_tabs.rs
+++ b/apps/desktop-gpui/src/editor_tabs.rs
@@ -454,6 +454,62 @@ pub enum CaptionSlider {
Size,
BackgroundOpacity,
FadeDuration,
+ ManualX,
+ ManualY,
+}
+
+/// Where Manual placement starts for a preset no caption was drawn at yet, as
+/// fractions of the frame (`captionPositionCenter`, `CaptionsTab.tsx:362-378`).
+pub fn caption_position_center(position: &str) -> (f32, f32) {
+ match position {
+ "top-left" => (0.05, 0.08),
+ "top-center" | "top" => (0.5, 0.08),
+ "top-right" => (0.95, 0.08),
+ "bottom-left" => (0.05, 0.85),
+ "bottom-right" => (0.95, 0.85),
+ _ => (0.5, 0.85),
+ }
+}
+
+pub fn caption_manual_center(settings: &CaptionSettings) -> (f32, f32) {
+ settings.manual_position.map_or_else(
+ || caption_position_center(&settings.position),
+ |xy| (xy.x, xy.y),
+ )
+}
+
+pub fn set_caption_manual_center(settings: &mut CaptionSettings, x: Option<f32>, y: Option<f32>) {
+ let (current_x, current_y) = caption_manual_center(settings);
+ let fraction = |value: f32| (value / 100.).clamp(0., 1.);
+ settings.position = "manual".to_string();
+ settings.manual_position = Some(cap_project::XY::new(
+ x.map_or(current_x, fraction),
+ y.map_or(current_y, fraction),
+ ));
+}
+
+/// Switching the caption position. Entering Manual from a preset seeds the
+/// manual centre from that preset (`updateCaptionPosition`,
+/// `CaptionsTab.tsx:380-394`); without it Manual drew bottom-centre and had
+/// nothing to adjust.
+pub fn set_caption_position(settings: &mut CaptionSettings, position: &str) {
+ set_caption_position_from(settings, position, cap_rendering::last_caption_center);
+}
+
+/// [`set_caption_position`] with the renderer's last drawn caption centre
+/// supplied: left and right presets align the box's edge to a margin, so
+/// only the drawn box says where its centre is.
+fn set_caption_position_from(
+ settings: &mut CaptionSettings,
+ position: &str,
+ drawn_center: impl FnOnce(&str) -> Option<(f32, f32)>,
+) {
+ if position == "manual" && settings.position != "manual" {
+ let (x, y) = drawn_center(&settings.position)
+ .unwrap_or_else(|| caption_position_center(&settings.position));
+ settings.manual_position = Some(cap_project::XY::new(x, y));
+ }
+ settings.position = position.to_string();
}
impl CaptionSlider {
@@ -463,6 +519,7 @@ impl CaptionSlider {
Self::BackgroundOpacity => (0., 100., 1.),
// `value={[getSetting("fadeDuration") * 100]}` over 0..50.
Self::FadeDuration => (0., 50., 1.),
+ Self::ManualX | Self::ManualY => (0., 100., 1.),
}
}
@@ -472,6 +529,8 @@ impl CaptionSlider {
Self::Size => settings.size as f32,
Self::BackgroundOpacity => settings.background_opacity as f32,
Self::FadeDuration => settings.fade_duration * 100.,
+ Self::ManualX => caption_manual_center(&settings).0 * 100.,
+ Self::ManualY => caption_manual_center(&settings).1 * 100.,
}
}
}
@@ -894,9 +953,8 @@ impl EditorWindow {
let Some((value, _)) = CAPTION_POSITIONS.get(index) else {
return;
};
- let value = value.to_string();
self.set_caption_setting("caption-position", window, cx, move |settings| {
- settings.position = value
+ set_caption_position(settings, value)
});
}
SidebarMenu::CaptionAnimation => {
@@ -1087,6 +1145,8 @@ impl EditorWindow {
CaptionSlider::Size => settings.size = value.round() as u32,
CaptionSlider::BackgroundOpacity => settings.background_opacity = value.round() as u32,
CaptionSlider::FadeDuration => settings.fade_duration = value / 100.,
+ CaptionSlider::ManualX => set_caption_manual_center(settings, Some(value), None),
+ CaptionSlider::ManualY => set_caption_manual_center(settings, None, Some(value)),
});
}
@@ -2847,6 +2907,30 @@ impl EditorWindow {
cx,
)),
)
+ .children((settings.position == "manual").then(|| {
+ div()
+ .flex()
+ .flex_col()
+ .gap(px(12.))
+ .child(
+ self.slider_field(
+ "Horizontal Position",
+ SliderKey::Caption(CaptionSlider::ManualX),
+ "%",
+ cx,
+ )
+ .into_any_element(),
+ )
+ .child(
+ self.slider_field(
+ "Vertical Position",
+ SliderKey::Caption(CaptionSlider::ManualY),
+ "%",
+ cx,
+ )
+ .into_any_element(),
+ )
+ }))
.child(
ui::Field::section(&theme, "Animation").child(
div()
@@ -3477,6 +3561,85 @@ impl EditorWindow {
mod tests {
use super::*;
+ /// A left-aligned caption sits a margin in from the edge, its centre a
+ /// half box width further in. Manual starts at that drawn centre, so
+ /// switching does not move it.
+ #[test]
+ fn manual_caption_position_starts_at_the_drawn_caption() {
+ let mut settings = CaptionSettings {
+ position: "bottom-left".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |position| {
+ (position == "bottom-left").then_some((0.16, 0.86))
+ });
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.16, 0.86))
+ );
+
+ // Nothing drawn yet: the preset's table entry.
+ let mut settings = CaptionSettings {
+ position: "top-right".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |_| None);
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.95, 0.08))
+ );
+ }
+
+ /// A 0.6.1 report: with the caption position set to Manual, the captions
+ /// stayed bottom-centre and nothing could move them. Manual now starts
+ /// where the preset drew them and the sliders move it.
+ #[test]
+ fn manual_caption_position_starts_at_the_preset_and_moves() {
+ let mut settings = CaptionSettings {
+ position: "top-center".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_position_from(&mut settings, "manual", |_| None);
+ assert_eq!(settings.position, "manual");
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.5, 0.08))
+ );
+
+ set_caption_manual_center(&mut settings, Some(20.), None);
+ set_caption_manual_center(&mut settings, None, Some(130.));
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.2, 1.0))
+ );
+ let mut project = ProjectConfiguration::default();
+ project.captions = Some(CaptionsData {
+ settings: settings.clone(),
+ ..CaptionsData::default()
+ });
+ assert_eq!(CaptionSlider::ManualX.read(&project), 20.);
+ assert_eq!(CaptionSlider::ManualY.read(&project), 100.);
+
+ // Picking Manual again keeps the adjusted spot.
+ set_caption_position(&mut settings, "manual");
+ assert_eq!(
+ settings.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.2, 1.0))
+ );
+
+ // A slider on a preset switches it to Manual from the preset's spot.
+ let mut preset = CaptionSettings {
+ position: "bottom-right".into(),
+ ..CaptionSettings::default()
+ };
+ set_caption_manual_center(&mut preset, None, Some(50.));
+ assert_eq!(preset.position, "manual");
+ assert_eq!(
+ preset.manual_position.map(|xy| (xy.x, xy.y)),
+ Some((0.95, 0.5))
+ );
+ }
+
#[test]
fn every_tab_slider_has_the_range_its_call_site_declares() {
assert_eq!(CameraSlider::Size.limits(), (20., 80., 0.1));
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index 164b8bdaf..95d5ce1e2 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -289,6 +289,11 @@ fn frame_seconds(frame: u32) -> f64 {
frame as f64 / EDITOR_PREVIEW_FPS as f64
}
+/// The new end of a timeline that an edit shortened past the playhead.
+fn playhead_past_end(playhead: f64, total: f64) -> Option<f64> {
+ (total > 0.0 && playhead > total).then_some(total)
+}
+
/// Where a seek leaves the playhead. Paused, the engine echoes the seek back
/// as `playhead_changed(frame)`; storing the frame's own time makes that echo
/// a no-op instead of a second whole-editor rebuild for every scrub step.
@@ -2292,6 +2297,12 @@ impl EditorWindow {
self.timeline.waveform_paths = waveform_paths;
if self.timeline.total_duration > 0.0 {
self.total = self.timeline.total_duration;
+ // Deleting or trimming the clip under the playhead shortens the
+ // timeline past it; bring the playhead back to the new end the way
+ // `setPlayhead` clamps it (`ED/context.ts:2424`).
+ if let Some(end) = playhead_past_end(self.playhead, self.total) {
+ self.apply_seek(end);
+ }
}
}
@@ -12278,6 +12289,17 @@ fn hex_to_color(rgba: [u8; 4]) -> cap_project::Color {
mod tests {
use super::*;
+ /// A 0.6.1 log: the user deleted the clip under the playhead, the
+ /// timeline shrank to before it, and every preview request past the new
+ /// end found no segment. The playhead goes back to the end instead.
+ #[test]
+ fn a_shortened_timeline_brings_the_playhead_back_to_its_end() {
+ assert_eq!(playhead_past_end(10.0, 4.0), Some(4.0));
+ assert_eq!(playhead_past_end(2.0, 4.0), None);
+ assert_eq!(playhead_past_end(4.0, 4.0), None);
+ assert_eq!(playhead_past_end(3.0, 0.0), None);
+ }
+
#[cfg(target_os = "linux")]
mod boundary_navigation {
use super::*;
diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs
index 3afd73bc7..430a049bf 100644
--- a/apps/desktop-gpui/src/target_overlay.rs
+++ b/apps/desktop-gpui/src/target_overlay.rs
@@ -481,6 +481,180 @@ impl TargetSelect {
}
}
+/// The aspect ratios the area toolbar cycles through, `None` first for a free
+/// selection. The 0.6.0 picker offered the same set (`COMMON_RATIOS`).
+const AREA_RATIOS: [Option<(u32, u32)>; 9] = [
+ None,
+ Some((16, 9)),
+ Some((4, 3)),
+ Some((1, 1)),
+ Some((9, 16)),
+ Some((16, 10)),
+ Some((21, 9)),
+ Some((3, 2)),
+ Some((2, 1)),
+];
+
+const AREA_SELECTION_STORE: &str = "gpui_area_selection";
+
+fn stored_area_ratio() -> Option<(u32, u32)> {
+ let section = crate::store::store_section(AREA_SELECTION_STORE);
+ let ratio = section.get("aspectRatio")?.as_array()?;
+ let [width, height] = ratio.as_slice() else {
+ return None;
+ };
+ let ratio = (width.as_u64()? as u32, height.as_u64()? as u32);
+ AREA_RATIOS.contains(&Some(ratio)).then_some(ratio)
+}
+
+fn store_area_ratio(ratio: Option<(u32, u32)>) {
+ crate::store::update_store_section(AREA_SELECTION_STORE, |section| {
+ section.insert(
+ "aspectRatio".into(),
+ ratio.map_or(serde_json::Value::Null, |(width, height)| {
+ serde_json::json!([width, height])
+ }),
+ );
+ true
+ });
+}
+
+fn next_area_ratio(current: Option<(u32, u32)>) -> Option<(u32, u32)> {
+ let index = AREA_RATIOS
+ .iter()
+ .position(|ratio| *ratio == current)
+ .unwrap_or(0);
+ AREA_RATIOS[(index + 1) % AREA_RATIOS.len()]
+}
+
+fn ratio_value((width, height): (u32, u32)) -> f32 {
+ width as f32 / height.max(1) as f32
+}
+
+/// An area at `ratio` (width over height) spanning from the fixed `anchor`
+/// towards `pointer`: the longer span wins, and the area shrinks to stay on
+/// the display.
+fn area_from_anchor_with_ratio(
+ anchor: (f32, f32),
+ pointer: (f32, f32),
+ ratio: f32,
+ display: (f32, f32),
+) -> AreaRect {
+ let (dx, dy) = (pointer.0 - anchor.0, pointer.1 - anchor.1);
+ let (mut width, mut height) = (dx.abs(), dy.abs());
+ if width / ratio >= height {
+ height = width / ratio;
+ } else {
+ width = height * ratio;
+ }
+ let room_x = if dx >= 0. {
+ display.0 - anchor.0
+ } else {
+ anchor.0
+ };
+ let room_y = if dy >= 0. {
+ display.1 - anchor.1
+ } else {
+ anchor.1
+ };
+ let fit = [
+ 1.,
+ room_x / width.max(f32::EPSILON),
+ room_y / height.max(f32::EPSILON),
+ ]
+ .into_iter()
+ .fold(f32::INFINITY, f32::min)
+ .max(0.);
+ width *= fit;
+ height *= fit;
+ AreaRect {
+ x: if dx >= 0. { anchor.0 } else { anchor.0 - width },
+ y: if dy >= 0. {
+ anchor.1
+ } else {
+ anchor.1 - height
+ },
+ width,
+ height,
+ }
+}
+
+/// An edge-handle resize at `ratio`: the opposite edge stays put, the dragged
+/// edge follows the pointer to whichever side of it the pointer is, and the
+/// other span grows around the area's centre line, shrinking to stay on the
+/// display.
+fn area_edge_resize_with_ratio(
+ handle: AreaHandle,
+ start: AreaRect,
+ pointer: (f32, f32),
+ ratio: f32,
+ display: (f32, f32),
+) -> AreaRect {
+ let (center_x, center_y) = (start.x + start.width / 2., start.y + start.height / 2.);
+ if handle.east() || handle.west() {
+ let anchor = if handle.west() {
+ start.right()
+ } else {
+ start.x
+ };
+ let before = pointer.0 < anchor;
+ let room_x = if before { anchor } else { display.0 - anchor };
+ let room_y = 2. * center_y.min(display.1 - center_y);
+ let width = (pointer.0 - anchor)
+ .abs()
+ .min(room_x)
+ .min(room_y * ratio)
+ .max(0.);
+ let height = width / ratio;
+ AreaRect {
+ x: if before { anchor - width } else { anchor },
+ y: center_y - height / 2.,
+ width,
+ height,
+ }
+ } else {
+ let anchor = if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ };
+ let before = pointer.1 < anchor;
+ let room_y = if before { anchor } else { display.1 - anchor };
+ let room_x = 2. * center_x.min(display.0 - center_x);
+ let height = (pointer.1 - anchor)
+ .abs()
+ .min(room_y)
+ .min(room_x / ratio)
+ .max(0.);
+ let width = height * ratio;
+ AreaRect {
+ x: center_x - width / 2.,
+ y: if before { anchor - height } else { anchor },
+ width,
+ height,
+ }
+ }
+}
+
+/// An existing area brought to `ratio` around its centre, keeping its width
+/// unless that would not fit the display.
+fn area_with_ratio(area: AreaRect, ratio: f32, display: (f32, f32)) -> AreaRect {
+ let (center_x, center_y) = (area.x + area.width / 2., area.y + area.height / 2.);
+ let mut width = area.width.min(display.0);
+ let mut height = width / ratio;
+ if height > display.1 {
+ height = display.1;
+ width = height * ratio;
+ }
+ AreaRect {
+ x: center_x - width / 2.,
+ y: center_y - height / 2.,
+ width,
+ height,
+ }
+ .clamped(display)
+}
+
/// A rectangle in the overlay's own coordinate space: display-relative logical
/// points, which is also what `ScreenCaptureTarget::Area` wants.
#[derive(Clone, Copy, Debug, PartialEq)]
@@ -654,6 +828,8 @@ pub struct OverlayWindow {
crop: Option<AreaRect>,
drag: Option<AreaDrag>,
snap_guides: Vec<SnapGuide>,
+ /// The aspect ratio a drawn or resized area keeps, `None` for free.
+ area_ratio: Option<(u32, u32)>,
recording_area: bool,
/// The mode menu the start pill's caret drops down -- `menuModes()`
/// popped up at the event location (`target-select-overlay.tsx:2064-2090,
@@ -799,6 +975,7 @@ impl OverlayWindow {
crop: None,
drag: None,
snap_guides: Vec::new(),
+ area_ratio: stored_area_ratio(),
recording_area: false,
mode_menu: None,
countdown_menu: None,
@@ -1786,7 +1963,7 @@ impl OverlayWindow {
if crop.is_none() && !interacting {
root = root.child(self.render_selection_hint(hint_started, window, cx));
}
- root = root.child(self.render_area_toolbar(crop, valid));
+ root = root.child(self.render_area_toolbar(crop, valid, cx));
if crop.is_some() {
root.child(self.render_area_controls(crop, valid, cx))
} else {
@@ -2033,21 +2210,52 @@ impl OverlayWindow {
}
/// The floating readout: `top-12 left-1/2 -translate-x-1/2` over the
- /// liquid-glass surface, `min-w-28 px-2 text-base tabular-nums`. The
- /// aspect-ratio, reset, fill and lock controls that share this bar in the
- /// TSX are deferred (README).
- fn render_area_toolbar(&self, crop: Option<AreaRect>, valid: bool) -> impl IntoElement {
+ /// liquid-glass surface, `min-w-28 px-2 text-base tabular-nums`, and the
+ /// aspect-ratio control beside it. The reset, fill and lock controls that
+ /// share this bar in the TSX are deferred (README).
+ fn render_area_toolbar(
+ &self,
+ crop: Option<AreaRect>,
+ valid: bool,
+ cx: &mut Context<Self>,
+ ) -> impl IntoElement {
let label = match crop.filter(|_| valid) {
Some(crop) => format!("{} × {}", crop.width.round(), crop.height.round()),
None => "Draw an area".to_string(),
};
+ let ratio_label = match self.area_ratio {
+ Some((width, height)) => format!("{width}:{height}"),
+ None => "Free".to_string(),
+ };
+ let ratio = div()
+ .id("area-aspect-ratio")
+ .h(px(36.))
+ .min_w(px(56.))
+ .px(px(10.))
+ .flex()
+ .items_center()
+ .justify_center()
+ .rounded(px(10.))
+ .cursor_pointer()
+ .text_size(px(14.))
+ .font_weight(FontWeight::MEDIUM)
+ .bg(self.theme.gray_3)
+ .hover({
+ let hover = self.theme.gray_4;
+ move |style| style.bg(hover)
+ })
+ .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation())
+ .on_click(cx.listener(|this, _, _, cx| {
+ this.set_area_ratio(next_area_ratio(this.area_ratio), cx);
+ }))
+ .child(ratio_label);
div()
.absolute()
// `top-12` on macOS, centred by hand: gpui has no translate.
.top(px(48.))
- .left(px((self.display_size.0 - 200.) / 2.))
- .w(px(200.))
+ .left(px((self.display_size.0 - 280.) / 2.))
+ .w(px(280.))
.flex()
.justify_center()
.child(
@@ -2070,10 +2278,23 @@ impl OverlayWindow {
// that the window root carries Medium.
.font_weight(FontWeight::NORMAL)
.child(label),
- ),
+ )
+ .child(ratio),
)
}
+ /// Locks drawn and resized areas to `ratio`, reshaping the current one
+ /// around its centre, and remembers the choice for the next picker.
+ fn set_area_ratio(&mut self, ratio: Option<(u32, u32)>, cx: &mut Context<Self>) {
+ self.area_ratio = ratio;
+ if let (Some(ratio), Some(crop)) = (ratio, self.crop) {
+ self.crop = Some(area_with_ratio(crop, ratio_value(ratio), self.display_size));
+ self.sync_area_camera(cx);
+ }
+ store_area_ratio(ratio);
+ cx.notify();
+ }
+
/// The controls cluster, placed against the crop the way `controlsStyle`
/// places it: below when it fits, above when it does not, inside the crop
/// otherwise.
@@ -2545,8 +2766,33 @@ impl OverlayWindow {
let x = f32::from(position.x).clamp(0., self.display_size.0);
let y = f32::from(position.y).clamp(0., self.display_size.1);
- let (next, guides) = match drag {
- AreaDrag::Draw { anchor } => {
+ let ratio = self.area_ratio.map(ratio_value);
+ let (next, guides) = match (drag, ratio) {
+ (AreaDrag::Draw { anchor }, Some(ratio)) => (
+ area_from_anchor_with_ratio(*anchor, (x, y), ratio, self.display_size),
+ Vec::new(),
+ ),
+ (AreaDrag::Resize { handle, start }, Some(ratio)) => (
+ if handle.east() != handle.west() && handle.north() != handle.south() {
+ let anchor = (
+ if handle.west() {
+ start.right()
+ } else {
+ start.x
+ },
+ if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ },
+ );
+ area_from_anchor_with_ratio(anchor, (x, y), ratio, self.display_size)
+ } else {
+ area_edge_resize_with_ratio(*handle, *start, (x, y), ratio, self.display_size)
+ },
+ Vec::new(),
+ ),
+ (AreaDrag::Draw { anchor }, None) => {
let ((x, y), guides) = snap_area_point((x, y), self.display_size);
(
AreaRect {
@@ -2558,7 +2804,7 @@ impl OverlayWindow {
guides,
)
}
- AreaDrag::Move { grab, start } => snap_area_rect(
+ (AreaDrag::Move { grab, start }, _) => snap_area_rect(
AreaRect {
x: start.x + (x - grab.0),
y: start.y + (y - grab.1),
@@ -2566,7 +2812,7 @@ impl OverlayWindow {
},
self.display_size,
),
- AreaDrag::Resize { handle, start } => {
+ (AreaDrag::Resize { handle, start }, None) => {
let mut left = start.x;
let mut top = start.y;
let mut right = start.right();
@@ -2658,6 +2904,114 @@ fn request_media_access(permission: crate::permissions::OSPermission, cx: &mut A
mod tests {
use super::*;
+ fn ratio_of(area: AreaRect) -> f32 {
+ area.width / area.height
+ }
+
+ fn inside(area: AreaRect, display: (f32, f32)) -> bool {
+ area.x >= -0.01
+ && area.y >= -0.01
+ && area.right() <= display.0 + 0.01
+ && area.bottom() <= display.1 + 0.01
+ }
+
+ /// A 0.6.1 report: the 0.6.0 picker could hold an area at 16:9, and the
+ /// new one could not. Drawing with a ratio keeps it from any direction.
+ #[test]
+ fn a_drawn_area_keeps_the_chosen_aspect_ratio() {
+ let display = (1920., 1080.);
+ let ratio = ratio_value((16, 9));
+ for pointer in [(900., 300.), (100., 900.), (1500., 1000.), (120., 140.)] {
+ let area = area_from_anchor_with_ratio((800., 500.), pointer, ratio, display);
+ assert!(
+ (ratio_of(area) - ratio).abs() < 0.001,
+ "{pointer:?}: {area:?}"
+ );
+ assert!(inside(area, display), "{pointer:?}: {area:?}");
+ }
+ // The longer span wins: a wide drag sets the width.
+ let wide = area_from_anchor_with_ratio((100., 100.), (900., 300.), ratio, display);
+ assert_eq!((wide.x, wide.y, wide.width), (100., 100., 800.));
+ assert!((wide.height - 450.).abs() < 0.01);
+ // A drag that would leave the display shrinks to fit instead.
+ let edge = area_from_anchor_with_ratio((1800., 100.), (1900., 1000.), ratio, display);
+ assert!(inside(edge, display) && (ratio_of(edge) - ratio).abs() < 0.001);
+ }
+
+ #[test]
+ fn resizing_an_area_keeps_the_chosen_aspect_ratio() {
+ let display = (1920., 1080.);
+ let ratio = ratio_value((1, 1));
+ let start = AreaRect {
+ x: 400.,
+ y: 300.,
+ width: 400.,
+ height: 400.,
+ };
+ for handle in AreaHandle::ALL {
+ for pointer in [(1000., 900.), (100., 50.), (650., 520.)] {
+ let area = if handle.east() != handle.west() && handle.north() != handle.south() {
+ let anchor = (
+ if handle.west() {
+ start.right()
+ } else {
+ start.x
+ },
+ if handle.north() {
+ start.bottom()
+ } else {
+ start.y
+ },
+ );
+ area_from_anchor_with_ratio(anchor, pointer, ratio, display)
+ } else {
+ area_edge_resize_with_ratio(handle, start, pointer, ratio, display)
+ };
+ assert!(
+ area.height == 0. || (ratio_of(area) - ratio).abs() < 0.001,
+ "{handle:?} {pointer:?}: {area:?}"
+ );
+ assert!(inside(area, display), "{handle:?} {pointer:?}: {area:?}");
+ }
+ }
+ // An east edge drag keeps the west edge and the centre line.
+ let east =
+ area_edge_resize_with_ratio(AreaHandle::East, start, (1000., 0.), ratio, display);
+ assert_eq!((east.x, east.width), (400., 600.));
+ assert_eq!(east.y + east.height / 2., 500.);
+ // Past the opposite edge, the selection follows the pointer over it.
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::East, start, (300., 0.), ratio, display);
+ assert_eq!((crossed.x, crossed.width), (300., 100.));
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::South, start, (0., 200.), ratio, display);
+ assert_eq!((crossed.y, crossed.height), (200., 100.));
+ let crossed =
+ area_edge_resize_with_ratio(AreaHandle::West, start, (900., 0.), ratio, display);
+ assert_eq!((crossed.x, crossed.width), (800., 100.));
+ }
+
+ #[test]
+ fn choosing_a_ratio_reshapes_the_area_around_its_centre() {
+ let display = (1920., 1080.);
+ let area = area_with_ratio(
+ AreaRect {
+ x: 100.,
+ y: 300.,
+ width: 800.,
+ height: 200.,
+ },
+ ratio_value((16, 9)),
+ display,
+ );
+ assert_eq!((area.x, area.width), (100., 800.));
+ assert!((area.height - 450.).abs() < 0.01);
+ assert!((area.y + area.height / 2. - 400.).abs() < 0.01);
+ assert!(inside(area, display));
+ assert_eq!(next_area_ratio(None), Some((16, 9)));
+ assert_eq!(next_area_ratio(Some((2, 1))), None);
+ }
+
#[test]
fn a_single_overlay_remains_interactive_without_a_global_cursor_probe() {
let display = "1".parse::<DisplayId>().unwrap();
diff --git a/crates/rendering/src/layers/captions.rs b/crates/rendering/src/layers/captions.rs
index 988988060..5a6d10bbe 100644
--- a/crates/rendering/src/layers/captions.rs
+++ b/crates/rendering/src/layers/captions.rs
@@ -101,6 +101,40 @@ pub struct CaptionOverlayLayout {
pub position: CaptionPosition,
}
+/// The centre of the caption box last drawn, as fractions of the frame, with
+/// the position setting it was drawn for. Left and right presets align the
+/// box's edge to the margin, so where its centre lands depends on the box
+/// width, which only this layer measures. The editor starts Manual placement
+/// here so switching to it leaves the captions where they were.
+static LAST_CAPTION_CENTER: std::sync::Mutex<Option<(CaptionPosition, [f32; 2])>> =
+ std::sync::Mutex::new(None);
+
+/// [`LAST_CAPTION_CENTER`] when the last caption was drawn at `position`.
+pub fn last_caption_center(position: &str) -> Option<(f32, f32)> {
+ let wanted = CaptionPosition::from_str(position);
+ let last = *LAST_CAPTION_CENTER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner);
+ last.and_then(|(drawn, [x, y])| (drawn == wanted).then_some((x, y)))
+}
+
+fn caption_center(rect: [f32; 4], frame: (u32, u32)) -> Option<[f32; 2]> {
+ (frame.0 > 0 && frame.1 > 0).then(|| {
+ [
+ ((rect[0] + rect[2] / 2.0) / frame.0 as f32).clamp(0.0, 1.0),
+ ((rect[1] + rect[3] / 2.0) / frame.1 as f32).clamp(0.0, 1.0),
+ ]
+ })
+}
+
+fn record_caption_center(position: CaptionPosition, rect: [f32; 4], frame: (u32, u32)) {
+ if let Some(center) = caption_center(rect, frame) {
+ *LAST_CAPTION_CENTER
+ .lock()
+ .unwrap_or_else(std::sync::PoisonError::into_inner) = Some((position, center));
+ }
+}
+
const BASE_TEXT_OPACITY: f32 = 0.8;
const BOUNCE_OFFSET_PIXELS: f32 = 8.0;
// Safety net for caption segments whose trailing word end was stretched across a
@@ -980,6 +1014,7 @@ impl CaptionsLayer {
];
self.active_layout = Some(CaptionOverlayLayout { rect, position });
+ record_caption_center(position, rect, (width, height));
let background_uniforms = CaptionBackgroundUniforms {
rect,
@@ -1223,8 +1258,8 @@ fn calculate_caption_bounce(current_time: f64, start: f64, end: f64, fade_durati
#[cfg(test)]
mod tests {
use super::{
- caption_segment_effective_end, find_active_caption_segment, find_active_word_index,
- word_byte_range,
+ caption_center, caption_segment_effective_end, find_active_caption_segment,
+ find_active_word_index, word_byte_range,
};
use cap_project::{CaptionTrackSegment, CaptionWord};
@@ -1252,6 +1287,20 @@ mod tests {
}
}
+ /// A 400 px box against the 96 px left margin of a 1920 px frame is
+ /// centred at 296 px, not at the 5% the preset table names.
+ #[test]
+ fn the_drawn_caption_centre_is_a_fraction_of_the_frame() {
+ let center = caption_center([96.0, 900.0, 400.0, 80.0], (1920, 1080)).unwrap();
+ assert!((center[0] - 296.0 / 1920.0).abs() < 1e-6);
+ assert!((center[1] - 940.0 / 1080.0).abs() < 1e-6);
+ assert!(caption_center([0.0, 0.0, 10.0, 10.0], (0, 1080)).is_none());
+ assert_eq!(
+ caption_center([1900.0, 0.0, 400.0, 10.0], (1920, 1080)).map(|c| c[0]),
+ Some(1.0)
+ );
+ }
+
#[test]
fn effective_end_clamps_inflated_trailing_word() {
let seg = segment(36.1, 42.31, vec![word(36.1, 42.31)]);
diff --git a/crates/rendering/src/lib.rs b/crates/rendering/src/lib.rs
index 56a1020e3..e5c2d0ec4 100644
--- a/crates/rendering/src/lib.rs
+++ b/crates/rendering/src/lib.rs
@@ -75,7 +75,9 @@ pub use frame_pipeline::{GpuOutputFormat, Nv12RenderedFrame, RenderedFrame, Shar
#[cfg(target_os = "macos")]
pub use frame_pipeline::{PendingSurface, RgbaToBgraSurfaceConverter};
pub use frame_windows::FrameWindows;
-pub use layers::{BackgroundTextureCache, clean_background_path, new_font_system};
+pub use layers::{
+ BackgroundTextureCache, clean_background_path, last_caption_center, new_font_system,
+};
pub use managed_segment::{
ManagedRecordingSegmentDecoders, ManagedSegmentDecoderStatus, ManagedSegmentStopHandles,
ManagedSegmentVideoError, ManagedSegmentVideoExit, ManagedSegmentVideoInput,