diff --git a/README.md b/README.md index e0eb475b0b..72974b48f7 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ A raw capture goes in and comes out ready to post — on X, Reddit, YouTube, a d ## Core Features - Record a specific window, or your whole screen. - Record microphone and system audio. -- Webcam overlay with picture-in-picture, eight snap positions, adjustable roundness, mirroring, and shape options. +- Webcam overlay with picture-in-picture, eight snap positions, adjustable roundness, mirroring, and shape options, plus a drop shadow: six presets (Close, Soft, Lifted, Floating, Ambient, or none) that you can customize by dragging where the shadow falls and setting its blur, spread, opacity, and color. - Auto or manual zooms with adjustable depth, duration, easing, and pixel-precise position; auto-zoom follows your cursor as you work. - Custom cursor size, smoothing, and click effects, with post-recording path smoothing. - Automatic captions for voiceovers, transcribed on-device with no upload (works offline), with an editable transcript you can cut from and optional subtitle translation. diff --git a/crates/compositor/src/compositor_linux.rs b/crates/compositor/src/compositor_linux.rs index 501eb9df02..865f55f915 100644 --- a/crates/compositor/src/compositor_linux.rs +++ b/crates/compositor/src/compositor_linux.rs @@ -1637,6 +1637,7 @@ impl Compositor { spread: f32, offset_px: [f32; 2], opacity: f32, + color: [f32; 3], ) -> LayerCB { let (rw, rh) = (self.render_w as f32, self.render_h as f32); let (sx, sy) = (spread / rw, spread / rh); @@ -1646,7 +1647,7 @@ impl Compositor { quad_px: [size_px[0] + 2.0 * spread, size_px[1] + 2.0 * spread], radius_px: radius, mode: 2.0, - color: [0.0, 0.0, 0.0, opacity], + color: [color[0], color[1], color[2], opacity], fx: [spread, 0.0, 0.0, 0.0], mb: [0.0, 1.0, 1.0, 0.0], ..Default::default() @@ -2740,7 +2741,7 @@ impl Compositor { Some(cb) => cb, None => match g.shadow_caster([rw, rh]) { ShadowCaster::Upright { dst, size_px, radius } => { - self.shadow_cb(dst, size_px, radius, spread, offset, opacity) + self.shadow_cb(dst, size_px, radius, spread, offset, opacity, [0.0; 3]) } ShadowCaster::Tilted { corners, center_px, radius, mask } => self .quad_shadow_cb(&corners, center_px, radius, mask, spread, offset, opacity), @@ -2931,13 +2932,21 @@ impl Compositor { && !is_cutout && g.webcam_shadow > 0.0) .then(|| { - let cb = self.shadow_cb( + let style = g.webcam_shadow_style; + let (caster, caster_px, caster_radius) = crate::frame_geometry::spread_shadow_caster( g.w_dst, g.w_px, g.w_radius, - crate::frame_geometry::WEBCAM_SHADOW_SPREAD_FRAC * g.frame_min_px, - [0.0, crate::frame_geometry::WEBCAM_SHADOW_OFFSET_FRAC * g.frame_min_px], + style.spread_frac * g.frame_min_px, + ); + let cb = self.shadow_cb( + caster, + caster_px, + caster_radius, + style.blur_frac * g.frame_min_px, + [style.offset_frac[0] * g.frame_min_px, style.offset_frac[1] * g.frame_min_px], crate::frame_geometry::WEBCAM_SHADOW_OPACITY * g.shape_fade * g.webcam_shadow, + style.color, ); self.make_bind(&cb, None, &dummy) }); diff --git a/crates/compositor/src/compositor_macos.rs b/crates/compositor/src/compositor_macos.rs index 4e7e98cd6c..bb3265d5d1 100644 --- a/crates/compositor/src/compositor_macos.rs +++ b/crates/compositor/src/compositor_macos.rs @@ -38,8 +38,7 @@ pub use crate::frame_geometry::{ live_params_from_scene, webcam_shape_code, FIXTURE_FRAMES, LayerCB, LiveParams, OUT_H, OUT_W, }; use crate::frame_geometry::{parse_hex, FrameGeometryInput, ShadowCaster, - SCREEN_SHADOW_SPREAD_FRAC, WEBCAM_SHADOW_OFFSET_FRAC, WEBCAM_SHADOW_OPACITY, - WEBCAM_SHADOW_SPREAD_FRAC}; + spread_shadow_caster, SCREEN_SHADOW_SPREAD_FRAC, WEBCAM_SHADOW_OPACITY}; use crate::scene::{Scene, SceneBackground, SceneShaderEffect, ShaderEffectTarget, WallpaperMotion}; use anyhow::{anyhow, Result}; use metal::foreign_types::ForeignType; @@ -1320,6 +1319,7 @@ impl Compositor { spread: f32, offset_px: [f32; 2], opacity: f32, + color: [f32; 3], ) { let (rw, rh) = (self.render_w as f32, self.render_h as f32); let (sx, sy) = (spread / rw, spread / rh); @@ -1329,7 +1329,7 @@ impl Compositor { quad_px: [size_px[0] + 2.0 * spread, size_px[1] + 2.0 * spread], radius_px: radius, mode: 2.0, - color: [0.0, 0.0, 0.0, opacity], + color: [color[0], color[1], color[2], opacity], fx: [spread, 0.0, 0.0, 0.0], mb: [0.0, 1.0, 1.0, 0.0], ..Default::default() @@ -2849,7 +2849,7 @@ impl Compositor { } else { match g.shadow_caster([rw, rh]) { ShadowCaster::Upright { dst, size_px, radius } => { - self.draw_shadow(enc, dst, size_px, radius, spread, offset, opacity) + self.draw_shadow(enc, dst, size_px, radius, spread, offset, opacity, [0.0; 3]) } ShadowCaster::Tilted { corners, center_px, radius, mask } => self.draw_quad_shadow( enc, &corners, center_px, radius, mask, spread, offset, opacity, @@ -3078,14 +3078,22 @@ impl Compositor { // garde donc son ombre. let is_cutout = matches!(effect, Some((code, _)) if code == 1.0); if cfg.shadow && g.webcam_shadow > 0.0 && !is_cutout && g.shape_fade > 0.0 { - self.draw_shadow( - enc, + let style = g.webcam_shadow_style; + let (caster, caster_px, caster_radius) = spread_shadow_caster( g.w_dst, g.w_px, g.w_radius, - WEBCAM_SHADOW_SPREAD_FRAC * g.frame_min_px, - [0.0, WEBCAM_SHADOW_OFFSET_FRAC * g.frame_min_px], + style.spread_frac * g.frame_min_px, + ); + self.draw_shadow( + enc, + caster, + caster_px, + caster_radius, + style.blur_frac * g.frame_min_px, + [style.offset_frac[0] * g.frame_min_px, style.offset_frac[1] * g.frame_min_px], WEBCAM_SHADOW_OPACITY * g.shape_fade * g.webcam_shadow, + style.color, ); } diff --git a/crates/compositor/src/compositor_windows.rs b/crates/compositor/src/compositor_windows.rs index 3231bf5d6a..2a3b4d7cea 100644 --- a/crates/compositor/src/compositor_windows.rs +++ b/crates/compositor/src/compositor_windows.rs @@ -12,8 +12,8 @@ use crate::frame_geometry::{ cover_crop_uv, cover_uv_rect, decode_data_uri, ease_in_out_cubic, lerp, lerp4, parse_hex, preset_placements, remap_box, screen_source_rect, timeline, CursorPlacement, FrameParams, Placement, ShadowCaster, SpriteShape, CURSOR_BASE_SIZE_FRAC, FPS, - SCREEN_SHADOW_SPREAD_FRAC, SHADOW_TUNING_REF_PX, WEBCAM_SHADOW_OFFSET_FRAC, - WEBCAM_SHADOW_OPACITY, WEBCAM_SHADOW_SPREAD_FRAC, + spread_shadow_caster, SCREEN_SHADOW_SPREAD_FRAC, SHADOW_TUNING_REF_PX, + WEBCAM_SHADOW_OPACITY, }; use crate::cursor::CursorTrack; use crate::scene::{ @@ -2284,6 +2284,7 @@ impl Compositor { spread: f32, offset_px: [f32; 2], opacity: f32, + color: [f32; 3], ) { let sx = spread / self.rw(); let sy = spread / self.rh(); @@ -2294,7 +2295,7 @@ impl Compositor { quad_px: [size_px[0] + 2.0 * spread, size_px[1] + 2.0 * spread], radius_px: radius, mode: 2.0, - color: [0.0, 0.0, 0.0, opacity], + color: [color[0], color[1], color[2], opacity], fx: [spread, 0.0, 0.0, 0.0], mb: [0.0, 1.0, 1.0, 0.0], ..Default::default() @@ -2601,7 +2602,7 @@ impl Compositor { } else { match g.shadow_caster(render_px) { ShadowCaster::Upright { dst, size_px, radius } => { - self.draw_shadow(dst, size_px, radius, spread, offset, opacity) + self.draw_shadow(dst, size_px, radius, spread, offset, opacity, [0.0; 3]) } // Même rayon que le plan incliné lui-même (cf. le dessin du mode 8). ShadowCaster::Tilted { corners, center_px, radius, mask } => self @@ -2854,13 +2855,17 @@ impl Compositor { ) && self.webcam_mask.borrow().is_some(); if cfg.shadow && g.webcam_shadow > 0.0 && !is_cutout && shape_fade > 0.0 { let strength = WEBCAM_SHADOW_OPACITY * shape_fade * g.webcam_shadow; + let style = g.webcam_shadow_style; + let (caster, caster_px, caster_radius) = + spread_shadow_caster(w_dst, w_px, w_radius, style.spread_frac * frame_min_px); self.draw_shadow( - w_dst, - w_px, - w_radius, - WEBCAM_SHADOW_SPREAD_FRAC * frame_min_px, - [0.0, WEBCAM_SHADOW_OFFSET_FRAC * frame_min_px], + caster, + caster_px, + caster_radius, + style.blur_frac * frame_min_px, + [style.offset_frac[0] * frame_min_px, style.offset_frac[1] * frame_min_px], strength, + style.color, ); } // Effet d'arrière-plan : le mode vient de la scène, le masque par pixel de diff --git a/crates/compositor/src/frame_geometry.rs b/crates/compositor/src/frame_geometry.rs index 82a67f955c..d73865171f 100644 --- a/crates/compositor/src/frame_geometry.rs +++ b/crates/compositor/src/frame_geometry.rs @@ -457,6 +457,85 @@ pub(crate) const WEBCAM_SHADOW_OFFSET_FRAC: f32 = 12.0 / SHADOW_TUNING_REF_PX; /// la position du slider. Parité avec le preset PiP côté web (`compositeLayout.ts`, /// `rgba(0,0,0,0.35)`), dont l'ombre est elle aussi un forfait fixe et PiP-only. pub(crate) const WEBCAM_SHADOW_OPACITY: f32 = 0.35; +/// Bounds of the camera shadow, as fractions of the 1080 px reference. They mirror +/// `WEBCAM_SHADOW_BOUNDS` in src/lib/webcamShadowStyles.ts, so a hand-edited project cannot push +/// the shadow off the frame or past what the pane offers. +pub(crate) const WEBCAM_SHADOW_OFFSET_RANGE: (f32, f32) = (-80.0 / SHADOW_TUNING_REF_PX, 80.0 / SHADOW_TUNING_REF_PX); +/// The blur floor is half a pixel AT THE 1080 REFERENCE, not in render pixels: the shader fades +/// with `smoothstep(0, blur, d)`, undefined at a zero width, and a floor taken after scaling +/// would collapse every blur under it in a small preview (1 px and 2 px both reaching the +/// floor in a 270 px tall preview) while the export still told them apart. +pub(crate) const WEBCAM_SHADOW_BLUR_RANGE: (f32, f32) = (0.5 / SHADOW_TUNING_REF_PX, 120.0 / SHADOW_TUNING_REF_PX); +pub(crate) const WEBCAM_SHADOW_SPREAD_RANGE: (f32, f32) = (-40.0 / SHADOW_TUNING_REF_PX, 40.0 / SHADOW_TUNING_REF_PX); + +/// The camera shadow as `plan_frame` hands it to the backends: lengths as fractions of the +/// frame's short side, and `strength` as the scene's opacity over `WEBCAM_SHADOW_OPACITY`, the +/// factor the backends already multiply by. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct WebcamShadowPlan { + pub blur_frac: f32, + pub offset_frac: [f32; 2], + pub spread_frac: f32, + pub color: [f32; 3], + pub strength: f32, +} + +impl WebcamShadowPlan { + /// The tuned 35% black shadow every scene had before the presets, at `strength`. + pub(crate) fn tuned(strength: f32) -> Self { + Self { + blur_frac: WEBCAM_SHADOW_SPREAD_FRAC, + offset_frac: [0.0, WEBCAM_SHADOW_OFFSET_FRAC], + spread_frac: 0.0, + color: [0.0, 0.0, 0.0], + strength, + } + } +} + +fn webcam_shadow_plan(effects: Option<&crate::scene::SceneEffects>) -> WebcamShadowPlan { + let finite = |v: f32, fallback: f32| if v.is_finite() { v } else { fallback }; + let clamp = |v: f32, (lo, hi): (f32, f32)| finite(v, 0.0).clamp(lo, hi); + match effects.and_then(|e| e.webcam_shadow_style) { + Some(style) => WebcamShadowPlan { + blur_frac: clamp(style.blur_frac, WEBCAM_SHADOW_BLUR_RANGE), + offset_frac: [ + clamp(style.offset_frac[0], WEBCAM_SHADOW_OFFSET_RANGE), + clamp(style.offset_frac[1], WEBCAM_SHADOW_OFFSET_RANGE), + ], + spread_frac: clamp(style.spread_frac, WEBCAM_SHADOW_SPREAD_RANGE), + color: style.color.map(|c| clamp(c, (0.0, 1.0))), + strength: clamp(style.opacity, (0.0, 1.0)) / WEBCAM_SHADOW_OPACITY, + }, + // `webcam_shadow` was a strength where 0.6 drew the tuned shadow. + None => WebcamShadowPlan::tuned( + effects.and_then(|e| e.webcam_shadow).unwrap_or(0.6).clamp(0.0, 1.0) / 0.6, + ), + } +} + +/// The rectangle a camera shadow is cast from: the camera's own, grown (or shrunk, with a +/// negative `spread_px`) on every side, its corners following so a round camera stays round. +/// `dst` is in the render target's normalised units, so its scale per pixel comes from `size_px`. +pub(crate) fn spread_shadow_caster( + dst: [f32; 4], + size_px: [f32; 2], + radius_px: f32, + spread_px: f32, +) -> ([f32; 4], [f32; 2], f32) { + if spread_px == 0.0 || size_px[0] <= 0.0 || size_px[1] <= 0.0 { + return (dst, size_px, radius_px); + } + // Never shrink past a pixel: a negative size would turn the rect inside out. + let spread = spread_px.max(0.5 - size_px[0].min(size_px[1]) / 2.0); + let (ux, uy) = (dst[2] / size_px[0], dst[3] / size_px[1]); + ( + [dst[0] - spread * ux, dst[1] - spread * uy, dst[2] + 2.0 * spread * ux, dst[3] + 2.0 * spread * uy], + [size_px[0] + 2.0 * spread, size_px[1] + 2.0 * spread], + (radius_px + spread).max(0.0), + ) +} + /// Taille de base du curseur, même convention (34 px réglés contre un cadre 1080). pub(crate) const CURSOR_BASE_SIZE_FRAC: f32 = 34.0 / SHADOW_TUNING_REF_PX; @@ -1910,6 +1989,8 @@ pub struct FrameGeometry { pub show_screen: bool, /// Continuous PiP shadow weight through a layout change. pub webcam_shadow: f32, + /// The camera shadow's look; `webcam_shadow` above carries its strength through layouts. + pub webcam_shadow_style: WebcamShadowPlan, pub mb_taps: f32, pub mb_amount: f32, pub source_t: f32, @@ -3711,11 +3792,13 @@ pub fn plan_frame(input: &FrameGeometryInput) -> FrameGeometry { Some(sample) => sample.preset != "camera-only" || sample.progress < 1.0, None => scene_preset.as_deref() != Some("camera-only"), }); + let webcam_shadow_style = webcam_shadow_plan(input.scene.map(|scene| &scene.effects)); FrameGeometry { show_screen, webcam_shadow: sampled_layout.as_ref().map(|s| s.webcam_shadow) .unwrap_or_else(|| if matches!(scene_preset.as_deref(), Some("dual-frame") | Some("vertical-stack") | Some("camera-only")) { 0.0 } else { 1.0 }) - * input.scene.and_then(|scene| scene.effects.webcam_shadow).unwrap_or(0.6).clamp(0.0, 1.0) / 0.6, + * webcam_shadow_style.strength, + webcam_shadow_style, scene_preset, mb_taps, mb_amount, @@ -5256,6 +5339,63 @@ mod tests { } } + #[test] + fn webcam_shadow_style_reaches_the_plan_and_old_scenes_keep_the_tuned_shadow() { + let mut scene = golden_scene(); + let cfg = crate::config::all().pop().expect("compositor config"); + // A scene from before the presets: the tuned shadow, exactly. + let baseline = plan_frame(&golden_input(&scene, &cfg)); + assert_eq!(baseline.webcam_shadow_style, WebcamShadowPlan::tuned(1.0)); + + scene.effects.webcam_shadow_style = Some(crate::scene::SceneWebcamShadow { + offset_frac: [-0.01, 0.03], + blur_frac: 0.08, + spread_frac: -0.01, + opacity: WEBCAM_SHADOW_OPACITY * 2.0, + color: [0.2, 0.4, 0.6], + }); + let styled = plan_frame(&golden_input(&scene, &cfg)); + let s = styled.webcam_shadow_style; + assert_eq!((s.offset_frac, s.blur_frac, s.spread_frac, s.color), ([-0.01, 0.03], 0.08, -0.01, [0.2, 0.4, 0.6])); + // Opacity is a strength over the tuned 35%, which every backend already multiplies by. + // Absolute weights, so a planner that suppressed the shadow (0 and 0) cannot pass. + assert!((baseline.webcam_shadow - 1.0).abs() < 1e-5, "{}", baseline.webcam_shadow); + assert!((styled.webcam_shadow - 2.0).abs() < 1e-5, "{}", styled.webcam_shadow); + // The shadow never moves the camera itself. + assert_eq!(styled.w_dst, baseline.w_dst); + + // A hand-edited scene is held to the pane's ranges, and a NaN to nothing. + scene.effects.webcam_shadow_style = Some(crate::scene::SceneWebcamShadow { + offset_frac: [5.0, f32::NAN], + blur_frac: -1.0, + spread_frac: 9.0, + opacity: 7.0, + color: [2.0, -1.0, 0.5], + }); + let s = plan_frame(&golden_input(&scene, &cfg)).webcam_shadow_style; + assert_eq!(s.offset_frac, [WEBCAM_SHADOW_OFFSET_RANGE.1, 0.0]); + assert_eq!(s.blur_frac, WEBCAM_SHADOW_BLUR_RANGE.0); + assert_eq!(s.spread_frac, WEBCAM_SHADOW_SPREAD_RANGE.1); + assert_eq!(s.color, [1.0, 0.0, 0.5]); + assert!((s.strength - 1.0 / WEBCAM_SHADOW_OPACITY).abs() < 1e-5); + } + + #[test] + fn spread_grows_the_caster_on_every_side_and_never_inverts_it() { + // 200x100 px at 0.002 normalised units per pixel. + let dst = [0.5, 0.5, 0.4, 0.2]; + let (d, px, r) = spread_shadow_caster(dst, [200.0, 100.0], 20.0, 10.0); + assert_eq!(px, [220.0, 120.0]); + assert_eq!(r, 30.0); + for (got, want) in d.iter().zip([0.48, 0.48, 0.44, 0.24]) { + assert!((got - want).abs() < 1e-6, "{d:?}"); + } + let (_, px, r) = spread_shadow_caster(dst, [200.0, 100.0], 20.0, -500.0); + assert!(px[1] >= 1.0 - 1e-6 && px[0] > px[1], "{px:?}"); + assert_eq!(r, 0.0); + assert_eq!(spread_shadow_caster(dst, [200.0, 100.0], 20.0, 0.0), (dst, [200.0, 100.0], 20.0)); + } + /// Le JSON de la scène zoomée, brut : `tilted_golden_scene` n'en change QUE la /// rotation, et le faire par substitution garantit que les deux scènes ne diffèrent /// pas ailleurs sans qu'on s'en aperçoive. @@ -8674,6 +8814,7 @@ mod tests { show_screen: true, scene_preset: None, webcam_shadow: 1.0, + webcam_shadow_style: WebcamShadowPlan::tuned(1.0), mb_taps: 1.0, mb_amount: 0.0, source_t: 0.0, diff --git a/crates/compositor/src/scene.rs b/crates/compositor/src/scene.rs index b4a5fb11d9..63e2835b6b 100644 --- a/crates/compositor/src/scene.rs +++ b/crates/compositor/src/scene.rs @@ -270,6 +270,19 @@ fn blur_amount<'de, D: serde::Deserializer<'de>>(d: D) -> Result }) } +/// The camera's drop shadow. Lengths are fractions of a 1080 px reference, like +/// `roundness_frac`: the native side multiplies them by the frame it draws. +#[derive(Debug, Clone, Copy, Deserialize, PartialEq)] +#[serde(rename_all = "camelCase")] +pub struct SceneWebcamShadow { + pub offset_frac: [f32; 2], + pub blur_frac: f32, + pub spread_frac: f32, + pub opacity: f32, + /// sRGB, 0..1 per channel. + pub color: [f32; 3], +} + /// Effets de cadre (padding, blur, ombre, coins, motion blur). #[derive(Debug, Clone, Copy, Deserialize)] #[serde(rename_all = "camelCase")] @@ -284,6 +297,10 @@ pub struct SceneEffects { pub shadow: f32, #[serde(default)] pub webcam_shadow: Option, + /// The PiP camera's drop shadow (`SceneWebcamShadow` in sceneDescription.ts). Absent in + /// scenes from before the presets, which keep the tuned shadow scaled by `webcam_shadow`. + #[serde(default)] + pub webcam_shadow_style: Option, /// Slider Roundness, en FRACTION du petit côté du cadre de sortie. /// /// Toute longueur qui traverse ce contrat est une fraction, jamais un nombre de pixels, et diff --git a/src/components/ai-edition/CameraShadowControls.module.css b/src/components/ai-edition/CameraShadowControls.module.css new file mode 100644 index 0000000000..da7c2b2756 --- /dev/null +++ b/src/components/ai-edition/CameraShadowControls.module.css @@ -0,0 +1,333 @@ +/* The camera shadow section of the Layout pane. Tokens from design/tokens/v4.css; the one fixed + colour is the stage the previews sit on, a stand-in for the wallpaper: it stays light in both + themes, because a shadow on the editor's own dark surface would show nothing. */ + +.root { + --shadow-stage: #e7ecf2; + --shadow-stage-line: rgb(15 23 42 / 0.08); + --shadow-camera: linear-gradient(160deg, #f8fafc 0%, #cbd5e1 100%); + padding-bottom: 4px; +} +/* In the dark editor a light stage glares; a mid grey still shows a dark shadow plainly. */ +:global(:root[data-theme="dark"]) .root { + --shadow-stage: #9ba5b3; + --shadow-stage-line: rgb(15 23 42 / 0.14); + --shadow-camera: linear-gradient(160deg, #e2e8f0 0%, #aab4c2 100%); +} + +/* ─── preset tiles ──────────────────────────────────────────────────────── */ + +.tileStage { + position: relative; + display: grid; + place-items: center; + width: 52px; + height: 34px; + margin-top: 2px; + border-radius: 7px; + background: var(--shadow-stage); + /* Concentric with the tile's 10px radius across its 3px inset. */ + box-shadow: inset 0 0 0 1px var(--shadow-stage-line); + overflow: hidden; +} + +.tileCamera { + width: 20px; + height: 20px; + border-radius: 6px; + background: var(--shadow-camera); + outline: 1px solid rgb(0 0 0 / 0.1); + outline-offset: -1px; + transition: box-shadow var(--motion-base) var(--ease); +} + +/* Marks the preset an edited shadow was tuned from: a static cue, never motion alone. */ +.editedDot { + position: absolute; + top: 4px; + right: 4px; + width: 6px; + height: 6px; + border-radius: var(--r-pill); + background: var(--accent); + box-shadow: 0 0 0 2px var(--shadow-stage); +} + +/* ─── Customize disclosure ──────────────────────────────────────────────── */ + +.disclosureRow { + display: flex; + align-items: center; + gap: 8px; + margin: 10px var(--sp-4) 0; +} + +.disclosure { + display: inline-flex; + align-items: center; + gap: 6px; + min-height: 32px; + margin-left: -6px; + padding: 0 8px 0 6px; + border: 0; + border-radius: var(--r-sm); + background: transparent; + color: var(--fg-2); + font: 500 13px/1.3 var(--font-body); + cursor: pointer; + transition: background var(--motion-fast) var(--ease), color var(--motion-fast) var(--ease); +} +.disclosure:hover:not(:disabled) { + background: color-mix(in oklab, var(--fg) 6%, transparent); + color: var(--fg); +} +.disclosure:focus-visible { + outline: 2px solid var(--brand); + outline-offset: 1px; +} +.disclosure:disabled { + opacity: 0.5; + cursor: not-allowed; +} + +.chevron { + color: var(--muted); + transition: rotate var(--motion-base) var(--ease); +} +.disclosure[aria-expanded="true"] .chevron { + rotate: 90deg; +} + +.badge { + padding: 1px 6px; + border-radius: var(--r-pill); + background: var(--accent-soft); + color: var(--fg-2); + font: 500 11px/16px var(--font-body); +} + +.reset { + display: inline-flex; + align-items: center; + gap: 4px; + min-height: 28px; + margin-left: auto; + padding: 0 8px; + border: 0; + border-radius: var(--r-sm); + background: transparent; + color: var(--muted); + font: 500 12px/1 var(--font-body); + cursor: pointer; + transition: background var(--motion-fast) var(--ease), color var(--motion-fast) var(--ease), + scale var(--motion-fast) var(--ease); +} +.reset:hover:not(:disabled) { + background: color-mix(in oklab, var(--fg) 6%, transparent); + color: var(--fg); +} +.reset:active:not(:disabled) { + scale: 0.96; +} +.reset:focus-visible { + outline: 2px solid var(--brand); + outline-offset: 1px; +} + +/* Height animates through the grid track, which can go to its content's size without a + measured height; the content fades a beat behind so it never shows clipped mid-way. */ +.collapse { + display: grid; + grid-template-rows: 0fr; + opacity: 0; + transition: grid-template-rows var(--motion-base) var(--ease), opacity var(--motion-fast) var(--ease); +} +.collapse[data-open="true"] { + grid-template-rows: 1fr; + opacity: 1; + transition: grid-template-rows var(--motion-base) var(--ease), + opacity var(--motion-base) var(--ease) 60ms; +} +.collapseInner { + min-height: 0; + overflow: hidden; +} + +/* ─── position pad ──────────────────────────────────────────────────────── */ + +.position { + margin: 8px var(--sp-4) 4px; +} + +.positionHead { + display: flex; + align-items: center; + gap: 6px; + min-height: 20px; + margin-bottom: 6px; +} +.positionLabel { + margin-right: auto; + font: 500 13px/1.3 var(--font-body); + color: var(--fg-2); +} +.readout { + font: 400 12px/1 var(--font-mono); + font-variant-numeric: tabular-nums; + color: var(--muted); +} +.iconButton { + display: grid; + place-items: center; + width: 20px; + height: 20px; + border: 0; + border-radius: var(--r-xs); + background: transparent; + color: var(--muted); + cursor: pointer; +} +.iconButton:hover { + color: var(--fg); + background: var(--border-hi); +} +.iconButton:focus-visible { + outline: 2px solid var(--brand); + outline-offset: 1px; +} + +.pad { + position: relative; + height: 132px; + border-radius: var(--r-md); + background: + radial-gradient(circle, var(--shadow-stage-line) 1px, transparent 1.5px) center / 12px 12px, + var(--shadow-stage); + box-shadow: inset 0 0 0 1px var(--shadow-stage-line); + overflow: hidden; + cursor: grab; + touch-action: none; + user-select: none; +} +.pad[data-dragging="true"] { + cursor: grabbing; +} +.pad[data-disabled="true"] { + opacity: 0.5; + cursor: not-allowed; +} + +/* The axes the position snaps to; one lights up while the drag sits on it. */ +.axisX, +.axisY { + position: absolute; + background: rgb(15 23 42 / 0.1); + transition: background var(--motion-fast) var(--ease); +} +.axisX { + left: 0; + right: 0; + top: 50%; + height: 1px; +} +.axisY { + top: 0; + bottom: 0; + left: 50%; + width: 1px; +} +.axisX[data-snapped="true"], +.axisY[data-snapped="true"] { + background: var(--accent); +} + +.padCamera { + position: absolute; + left: 50%; + top: 50%; + width: 56px; + height: 56px; + margin: -28px 0 0 -28px; + border-radius: 14px; + background: var(--shadow-camera); + outline: 1px solid rgb(0 0 0 / 0.1); + outline-offset: -1px; + /* Glides between presets; follows the pointer exactly while dragged. */ + transition: box-shadow var(--motion-base) var(--ease); +} + +.vector { + position: absolute; + left: 50%; + top: 50%; + overflow: visible; + pointer-events: none; +} +.vector line { + stroke: rgb(15 23 42 / 0.35); + stroke-width: 1; + stroke-dasharray: 2 3; +} + +.handle { + position: absolute; + left: 50%; + top: 50%; + width: 16px; + height: 16px; + margin: -8px 0 0 -8px; + border-radius: var(--r-pill); + background: rgb(255 255 255 / 0.85); + box-shadow: + 0 0 0 1.5px rgb(15 23 42 / 0.55), + 0 1px 3px rgb(15 23 42 / 0.25); + transition: transform var(--motion-base) var(--ease), box-shadow var(--motion-fast) var(--ease); + cursor: inherit; +} +/* A bigger target than the dot, without moving it. */ +.handle::before { + content: ""; + position: absolute; + inset: -12px; + border-radius: inherit; +} +.handle:focus-visible { + outline: none; + box-shadow: + 0 0 0 1.5px rgb(15 23 42 / 0.55), + 0 0 0 4px color-mix(in oklab, var(--brand) 45%, transparent); +} +.pad[data-dragging="true"] .handle, +.pad[data-dragging="true"] .padCamera { + transition: none; +} + +/* ─── colour ────────────────────────────────────────────────────────────── */ + +.colorRow { + margin-top: 0; + padding-top: 4px; +} + +/* Forced colours drop box-shadow, which is the handle's whole focus ring: give it a system + colour outline there, and the dot a border so it stays visible on the pad. */ +@media (forced-colors: active) { + .handle { + border: 2px solid CanvasText; + } + .handle:focus-visible { + outline: 2px solid Highlight; + outline-offset: 2px; + } +} + +@media (prefers-reduced-motion: reduce) { + .collapse, + .collapse[data-open="true"], + .chevron, + .tileCamera, + .padCamera, + .handle { + transition: none; + } +} diff --git a/src/components/ai-edition/CameraShadowControls.tsx b/src/components/ai-edition/CameraShadowControls.tsx new file mode 100644 index 0000000000..31e9b03253 --- /dev/null +++ b/src/components/ai-edition/CameraShadowControls.tsx @@ -0,0 +1,460 @@ +import { ChevronRight, RotateCcw } from "lucide-react"; +import { + type KeyboardEvent, + type PointerEvent, + useCallback, + useEffect, + useId, + useRef, + useState, +} from "react"; +import { Tooltip, TooltipProvider } from "@/components/ui/tooltip"; +import { useScopedT } from "@/contexts/I18nContext"; +import type { + EditorSettingsPatch, + EditorSettingsSnapshot, +} from "@/lib/ai-edition/store/editorSettings"; +import { + describeWebcamShadow, + WEBCAM_SHADOW_BOUNDS, + WEBCAM_SHADOW_COLORS, + WEBCAM_SHADOW_PRESETS, + type WebcamShadowPresetId, + type WebcamShadowValues, + webcamShadowCss, + webcamShadowPreset, + webcamShadowPresetPatch, +} from "@/lib/webcamShadowStyles"; +import css from "./CameraShadowControls.module.css"; +import { ColorField } from "./ColorField"; +import styles from "./NewEditorShell.module.css"; +import { ChoiceRow, SliderCell } from "./RightPanes"; + +/** Preview scales: what a 1080 px shadow becomes in the tiles and in the position pad. */ +const TILE_SCALE = 0.11; +const PAD_SCALE = 0.42; +/** Within this many pixels of an axis, the position snaps onto it. */ +const SNAP_PX = 3; +const OPEN_KEY = "os-camera-shadow-customize-open"; + +type ShadowSettings = Pick< + EditorSettingsSnapshot, + | "webcamShadowPreset" + | "webcamShadowOffsetX" + | "webcamShadowOffsetY" + | "webcamShadowBlur" + | "webcamShadowSpread" + | "webcamShadowOpacity" + | "webcamShadowColor" +>; + +function valuesOf(settings: ShadowSettings): WebcamShadowValues { + return { + offsetX: settings.webcamShadowOffsetX, + offsetY: settings.webcamShadowOffsetY, + blur: settings.webcamShadowBlur, + spread: settings.webcamShadowSpread, + opacity: settings.webcamShadowOpacity, + color: settings.webcamShadowColor, + }; +} + +function readOpen(): boolean { + try { + return window.localStorage.getItem(OPEN_KEY) === "1"; + } catch { + return false; + } +} + +function writeOpen(open: boolean) { + try { + window.localStorage.setItem(OPEN_KEY, open ? "1" : "0"); + } catch { + // A per-viewer convenience: without storage the section just starts closed. + } +} + +/** + * The picture-in-picture camera's shadow: six presets, then a "Customize" section that tunes + * position, blur, spread, opacity and colour from the one picked. An edited preset stays + * pressed and says so, with a way back to it; a shadow from before the presets that matches + * none of them presses none. + * + * Every drag writes live and records one undo step on release (`setLive` + `commit`); a preset + * or a colour preset is one `set`. + */ +export function CameraShadowControls({ + settings, + disabled, + set, + setLive, + commit, +}: { + settings: ShadowSettings; + disabled: boolean; + set: (patch: EditorSettingsPatch) => unknown; + setLive: (patch: EditorSettingsPatch) => void; + commit: () => unknown; +}) { + const ts = useScopedT("settings"); + const values = valuesOf(settings); + const { preset, edited } = describeWebcamShadow(values, settings.webcamShadowPreset); + const [open, setOpen] = useState(readOpen); + const bodyId = useId(); + + const toggle = () => { + setOpen((was) => { + writeOpen(!was); + return !was; + }); + }; + + /** The patch an edit writes. It keeps the preset the shadow was tuned from, whatever its + * values. The one exception is None: tuning a shadow that is not there would show nothing, + * so any edit but a 0% opacity starts from Soft's whole shadow and becomes an edited Soft. + * A preset tuned down to 0% is not None, and its other values are kept. */ + const editPatch = (change: Partial): EditorSettingsPatch => { + const fromNone = preset === "none" && !(change.opacity !== undefined && change.opacity <= 0); + const start: WebcamShadowValues = fromNone ? webcamShadowPreset("soft") : values; + const next = { ...start, ...change }; + return { + webcamShadowPreset: fromNone ? "soft" : (preset ?? null), + webcamShadowOffsetX: next.offsetX, + webcamShadowOffsetY: next.offsetY, + webcamShadowBlur: next.blur, + webcamShadowSpread: next.spread, + webcamShadowOpacity: next.opacity, + webcamShadowColor: next.color, + }; + }; + const live = (change: Partial) => setLive(editPatch(change)); + + const label = (id: WebcamShadowPresetId) => ts(`layout.cameraShadow.${id}`); + const base = preset ? webcamShadowPreset(preset) : null; + + return ( +
+
{ts("effects.shadow")}
+
+ + label={ts("effects.shadow")} + display="both" + tiles + columns={3} + options={WEBCAM_SHADOW_PRESETS.map((p) => ({ + value: p.id, + label: label(p.id), + title: ts(`layout.cameraShadow.${p.id}Tip`), + icon: , + }))} + value={preset ?? ""} + disabled={disabled} + onChange={(id) => { + if (id !== "") void set(webcamShadowPresetPatch(id)); + }} + /> +
+ +
+ + {edited && base ? ( + + + + + + ) : null} +
+ +
+
+ live({ offsetX, offsetY })} + onCommit={() => void commit()} + onReset={(offsetX, offsetY) => void set(editPatch({ offsetX, offsetY }))} + /> +
+ live({ blur })} + onCommit={() => void commit()} + /> + live({ spread })} + onCommit={() => void commit()} + /> + live({ opacity: pct / 100 })} + onCommit={() => void commit()} + /> +
+
+ {ts("layout.cameraShadow.color")} + { + if (/^#[0-9a-f]{6}$/i.test(hex)) live({ color: hex.toLowerCase() }); + }} + onCommit={() => void commit()} + /> +
+
+
+
+ ); +} + +/** A preset's picture: a camera on a small stage, casting that preset's shadow. */ +function ShadowTile({ values, edited }: { values: WebcamShadowValues; edited: boolean }) { + return ( +