diff --git a/apps/cli/src/selftest/playback.rs b/apps/cli/src/selftest/playback.rs index 866c22617d4..496c6b845aa 100644 --- a/apps/cli/src/selftest/playback.rs +++ b/apps/cli/src/selftest/playback.rs @@ -884,6 +884,7 @@ mod fixture { status: Some(StudioRecordingStatus::Complete), cursor_shape_version: 0, audio_only: false, + blank: false, }, }; diff --git a/apps/desktop-gpui/assets/icons/gem.svg b/apps/desktop-gpui/assets/icons/gem.svg deleted file mode 100644 index a69fafa6503..00000000000 --- a/apps/desktop-gpui/assets/icons/gem.svg +++ /dev/null @@ -1 +0,0 @@ - diff --git a/apps/desktop-gpui/locales/de.toml b/apps/desktop-gpui/locales/de.toml index 5dfc0b08922..20c2ce40a6a 100644 --- a/apps/desktop-gpui/locales/de.toml +++ b/apps/desktop-gpui/locales/de.toml @@ -30,6 +30,7 @@ website = "Website" commit = "Commit" [menu.file] +new_blank_project = "Neues leeres Projekt" open_prompt = "Aufnahme öffnen" close_window = "Fenster schließen" title = "Ablage" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "Bildschirmfoto konnte nicht kopiert werden" import_recordings = "Importieren" import_screenshots = "Bild importieren" importing = "Import läuft" +new_project = "Neu" no_matching_recordings = "Keine passenden Aufnahmen" no_matching_screenshots = "Keine passenden Bildschirmfotos" no_recordings = "Noch keine Aufnahmen" @@ -1165,6 +1167,7 @@ no_displays = "Keine Bildschirme gefunden" no_matching_displays = "Keine passenden Bildschirme" no_matching_windows = "Keine passenden Fenster" no_windows = "Keine Fenster gefunden" +solo_title = "Nur Kamera oder Audio" window = "Fenster" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm} Wörter/Min." [import] audio_failed = "Audio konnte nicht importiert werden: {error}" +blank_failed = "Projekt konnte nicht erstellt werden: {error}" +blank_name = "Unbenannt" error_title = "Importfehler" image_failed = "Bild konnte nicht importiert werden: {error}" unsupported = "Nicht unterstützte Mediendatei." @@ -1854,9 +1859,9 @@ done = "Fertig" import = "Datei importieren" [editor.audio_only] -add_visuals = "Visuals hinzufügen" body = "Diese Aufnahme hat kein Video. Exportieren Sie den Ton als MP3 oder WAV, oder fügen Sie Visuals hinzu, um daraus ein Video zu machen." export_audio = "Audio exportieren" +make_video = "Ein Video daraus machen" show_visuals = "Visuals anzeigen" size_large = "Groß" size_medium = "Mittel" @@ -2310,12 +2315,16 @@ save_failed = "Dieser Verlauf konnte nicht gespeichert werden. Versuchen Sie es save_tooltip = "Aktuellen Verlauf speichern" saved = "Gespeichert" still = "Still" +style = "Stil" +style_dither = "Dither" +style_smooth = "Weich" [editor.sidebar.animated_gradient.control] contrast = "Kontrast" curvature = "Krümmung" detail = "Detail" direction = "Richtung" +dither_size = "Dither-Größe" exposure = "Belichtung" flow_scale = "Flussgröße" flow_strength = "Flussstärke" @@ -2381,6 +2390,23 @@ to = "Bis" [editor.sidebar.image] prompt = "Klicken Sie zum Auswählen oder ziehen Sie ein Bild hierher" +[editor.sidebar.input_sounds] +click_sounds = "Klickgeräusche" +deep = "Tief" +deep_description = "Lineare Schalter mit tiefem, rundem Klang." +key_sounds = "Tastengeräusche" +laptop = "Laptop" +laptop_description = "Weiche, flache Laptoptasten." +mechanical = "Mechanisch" +mechanical_description = "Knackige Schalter mit hellem, taktilem Klacken." +mouse = "Maus" +mouse_description = "Drücken und Loslassen einer Maustaste." +trackpad = "Trackpad" +trackpad_description = "Der leisere Klick eines Laptop-Trackpads." +typewriter = "Schreibmaschine" +typewriter_description = "Anschlagende Typenhebel, mit Wagenrücklauf bei Eingabe." +volume = "Lautstärke" + [editor.sidebar.layout] background_blur = "Hintergrundunschärfe" custom_position = "Eigene Bildschirmposition (auf der Leinwand verschoben)" @@ -2498,6 +2524,7 @@ frames = { one = "{rendered} / {count} Frame", other = "{rendered} / {count} Fra generating_preview = "Vorschau wird erstellt…" link_unavailable_cursor = "Exporte nur mit Cursor können nur als Datei oder in die Zwischenablage gespeichert werden." link_unavailable_transparent = "Transparente Exporte können nur als Datei oder in die Zwischenablage gespeichert werden." +more_formats = "Mehr" organization = "Organisation" preview = "Vorschau" preview_info = "Dies ist ein gerendertes Bild aus Ihrem Video. Passen Sie die Einstellungen an, um die Qualität des fertigen Exports zu sehen." @@ -2545,7 +2572,9 @@ file = "Datei" link = "Link zum Teilen" [editor.export.done] +copy_file = "In Zwischenablage kopieren" copy_link = "Link kopieren" +file_copied = "Kopiert" instant_hint = "Verwenden Sie für Ihre nächste Aufnahme den Instant-Modus, wenn Sie direkt nach dem Stoppen einen Link möchten." link_copied = "Link kopiert" open_file = "Datei öffnen" @@ -2640,14 +2669,16 @@ format = "Spuren" microphone = "Mikrofon" open_folder = "Ordner öffnen" screen = "Bildschirm" +separate = "Separate Spuren" +separate_description = "Bildschirm, Kamera und Audio jeweils als eigene Datei" studio_only = "Separate Spuren werden nur im Studio-Modus aufgenommen." system_audio = "Systemaudio" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "Turbo-Export" -description = "Exportiert deutlich schneller mit allen CPU-Kernen und der GPU" -warning = "Nutzt bis zum Ende des Exports die gesamte CPU, die GPU und ein Vielfaches an Arbeitsspeicher. Andere Apps können langsamer werden, und Ihr Computer kann warm und laut werden." +description = "Schließt Exporte deutlich schneller ab, indem mehr Leistung Ihres Computers genutzt wird" +warning = "Andere Apps können sich bis zum Ende des Exports etwas langsamer anfühlen." [editor.modal] cancel_export = "Export abbrechen" @@ -2714,6 +2745,8 @@ advanced_shadow = "Erweiterte Schatteneinstellungen" background = "Hintergrund" corner_style = "Eckenstil" custom_position = "Eigene Position (auf der Leinwand verschoben)" +empty_description = "Wählen Sie vor der Aufnahme eine Kamera, dann können Sie sie hier gestalten und platzieren." +empty_title = "Keine Kamera in dieser Aufnahme" hide = "Kamera ausblenden" keep_size_during_zoom = "Originalgröße beim Zoomen beibehalten" mirror = "Kamera spiegeln" @@ -2840,12 +2873,16 @@ minimal = "Minimal" minimal_description = "Dezenter, umrandeter Text ohne Hintergrund." pop = "Pop" pop_description = "Verspielte Großbuchstaben, die mit einem kräftigen Akzent aufpoppen." +soft = "Weich" +soft_description = "Abgerundete anthrazitfarbene Karte passend zu den Tasten." [editor.tabs.corner] rounded = "Abgerundet" squircle = "Squircle" [editor.tabs.cursor] +empty_description = "Diese Aufnahme hat den Cursor nicht erfasst, daher gibt es hier noch nichts zu gestalten." +empty_title = "Kein Cursor in dieser Aufnahme" follow_hover = "Hover-Hervorhebungen folgen" friction = "Reibung" hide_when_idle = "Bei Inaktivität ausblenden" @@ -2871,6 +2908,7 @@ smooth = "Weich" smooth_description = "Weiche, schwebende Bewegung" [editor.tabs.keyboard] +add_to_timeline = "Zur Timeline hinzufügen" animation = "Animation" background_color = "Hintergrundfarbe" background_opacity = "Hintergrunddeckkraft" @@ -2887,18 +2925,38 @@ font_weight = "Schriftstärke" generate = "Tastatursegmente erstellen" generating = "Tastatursegmente werden erstellt..." grouping_threshold = "Gruppierungsschwelle" +key_style = "Tastenstil" +key_theme = "Tastenfarben" linger_duration = "Anzeigedauer" load_failed = "Aufnahmedaten konnten nicht geladen werden: {error}" no_presses = "Es wurden keine aufgezeichneten Tastendrücke gefunden." position = "Position" regenerate = "Tastatursegmente neu erstellen" +show_as = "Anzeigen als" show_modifiers = "Modifikatortasten anzeigen" show_special_keys = "Spezialtasten anzeigen" +show_typing = "Getippten Text zeigen" size_label = "Größe" +split_keys = "Tasten" +split_sentence = "Satz" +split_words = "Wörter" +style_classic = "Klassisch" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "Mechanisch" +style_windows = "Windows" text_color = "Textfarbe" +theme_dark = "Dunkel" +theme_light = "Hell" +theme_retro = "Retro" timeline_unavailable = "Die Projektzeitleiste ist nicht verfügbar." timeline_unreadable = "Die Projektzeitleiste konnte nicht gelesen werden." title = "Tastatur anzeigen" +type_keys = "Tasten eingeben" +type_keys_button = "Tasten eingeben…" +type_keys_hint = "Füge einen Satz, ein Skript oder ein Transkript ein, oder ein Kürzel wie cmd+shift+p. Es wird ab hier als Tastenanschläge getippt." +type_keys_placeholder = "Ein Satz oder ein Kürzel wie cmd+k" +type_keys_summary = "Etwa {seconds} s Tippen" uppercase = "Großbuchstaben" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "Proportional" bottom_center = "Unten mittig" bottom_left = "Unten links" bottom_right = "Unten rechts" +center = "Mitte" manual = "Manuell" +middle_left = "Mitte links" +middle_right = "Mitte rechts" top_center = "Oben mittig" top_left = "Oben links" top_right = "Oben rechts" @@ -2979,6 +3040,7 @@ title = "Zeitleiste" [editor.timeline.empty] add_audio = "Audio hinzufügen" +add_key = "Tastendruck hinzufügen" add_scene = "Szene hinzufügen" audio = "Fügen Sie Hintergrundmusik hinzu oder importieren Sie eigenes Audio" captions = "Keine Untertitel" @@ -3117,6 +3179,7 @@ crop = "Zuschneiden" crop_tooltip = "Video zuschneiden" no_room_for_3d_shot = "Kein Platz für eine weitere 3D-Einstellung" no_room_for_caption = "Hier ist kein Platz für einen weiteren Untertitel" +no_room_for_key = "Hier ist kein Platz für einen weiteren Tastendruck" redo = "Wiederholen" save_failed = "{path} konnte nicht gespeichert werden. Lassen Sie den Editor geöffnet und versuchen Sie es erneut: {error}" undo = "Rückgängig" @@ -3231,6 +3294,26 @@ no_segments = "Die Aufnahme hat keine Segmente. Möglicherweise muss sie zuerst not_found = "Videopfad {path} nicht gefunden!" not_studio = "Nur Studio-Aufnahmen können bearbeitet werden" +[editor.window.looks] +launch_day = "Launch-Tag" +launch_day_description = "Ein kräftiger Verlauf, schnelle Zooms und auffällige Untertitel." +mac_native = "Mac pur" +mac_native_description = "Ein macOS-Fenster auf Tahoe mit weißen Mac-Tasten." +midnight = "Mitternacht" +midnight_description = "Ein tiefer, dunkler Hintergrund mit klarer Kante." +minimal = "Minimal" +minimal_description = "Eine ruhige, cremeweiße Fläche und nichts weiter." +retro_desk = "Retro-Schreibtisch" +retro_desk_description = "Warmes Beige und kräftige mechanische Tasten." +saved = "Ihre Vorlagen" +smooth_demo = "Sanfte Demo" +smooth_demo_description = "Weiche Wolken, sanfte Zooms und ein ruhiger Cursor." +title = "Looks" +tutorial = "Tutorial" +tutorial_description = "Langsame, ruhige Zooms und Tasten, denen jeder folgen kann." +web_product = "Webprodukt" +web_product_description = "Ein Browserfenster für Website- und App-Demos." + [editor.window.presets] apply = "Anwenden" create = "Erstellen" @@ -3445,4 +3528,4 @@ no_resumable_state = "Für diese Aufnahme gibt es keinen fortsetzbaren Upload" verification_failed = "Die Prüfung der Aufnahme ist fehlgeschlagen: {status}" [message.cli] -binary_missing = "Mitgelieferte CLI unter {path} nicht gefunden" \ No newline at end of file +binary_missing = "Mitgelieferte CLI unter {path} nicht gefunden" diff --git a/apps/desktop-gpui/locales/en.toml b/apps/desktop-gpui/locales/en.toml index 530c312d80c..ee592ce4047 100644 --- a/apps/desktop-gpui/locales/en.toml +++ b/apps/desktop-gpui/locales/en.toml @@ -30,6 +30,7 @@ website = "Website" commit = "Commit" [menu.file] +new_blank_project = "New Blank Project" open_prompt = "Open Recording" close_window = "Close Window" title = "File" @@ -1067,6 +1068,7 @@ copy_screenshot_failed = "Could not copy screenshot" import_recordings = "Import" import_screenshots = "Import image" importing = "Importing" +new_project = { text = "New", context = "Button that starts a new blank project, with no recording, to build a video from backgrounds, music, keys and text." } no_matching_recordings = "No matching recordings" no_matching_screenshots = "No matching screenshots" no_recordings = "No recordings yet" @@ -1167,6 +1169,7 @@ no_displays = "No displays found" no_matching_displays = "No matching displays" no_matching_windows = "No matching windows" no_windows = "No windows found" +solo_title = { text = "Camera or audio only", context = "Title of the panel choosing between camera-only and audio-only recording, and the tooltip on its chevron." } window = { text = "Window", max_px = 100, size = 12 } [recordings_overlay] @@ -1541,6 +1544,8 @@ words_per_minute = { text = "{wpm} wpm", context = "Teleprompter scroll speed in [import] audio_failed = { text = "Failed to import audio: {error}", context = "{error} is a technical error message, usually in English." } +blank_failed = { text = "Failed to create a project: {error}", context = "{error} is a technical error message, usually in English." } +blank_name = { text = "Untitled", context = "The name of a new blank project, made without recording anything." } error_title = "Import Error" image_failed = { text = "Failed to import image: {error}", context = "{error} is a technical error message, usually in English." } unsupported = "Unsupported media file." @@ -1856,9 +1861,9 @@ done = "Done" import = "Import file" [editor.audio_only] -add_visuals = "Add Visuals" body = "This recording has no video. Export the sound as MP3 or WAV, or add visuals to turn it into a video." export_audio = "Export Audio" +make_video = { text = "Make it a video", context = "Heading over visualiser choices that turn an audio-only recording into a video." } show_visuals = "Show visuals" size_large = "Large" size_medium = "Medium" @@ -2312,12 +2317,16 @@ save_failed = "Could not save this gradient. Try again." save_tooltip = "Save the current gradient" saved = "Saved" still = { text = "Still", context = "Motion speed readout when the speed is zero." } +style = "Style" +style_dither = { text = "Dither", context = "A background style drawn as a fine ordered dither of a few colours, like an old print." } +style_smooth = "Smooth" [editor.sidebar.animated_gradient.control] contrast = "Contrast" curvature = "Curvature" detail = "Detail" direction = "Direction" +dither_size = "Dither Size" exposure = "Exposure" flow_scale = "Flow Scale" flow_strength = "Flow Strength" @@ -2383,6 +2392,23 @@ to = "To" [editor.sidebar.image] prompt = "Click to select or drag and drop image" +[editor.sidebar.input_sounds] +click_sounds = "Click Sounds" +deep = "Deep" +deep_description = "Linear switches with a low, rounded thock." +key_sounds = "Key Sounds" +laptop = "Laptop" +laptop_description = "Soft, shallow laptop keys." +mechanical = "Mechanical" +mechanical_description = "Crisp switches with a bright, tactile clack." +mouse = "Mouse" +mouse_description = "The press and release of a mouse button." +trackpad = "Trackpad" +trackpad_description = "The softer click of a laptop trackpad." +typewriter = "Typewriter" +typewriter_description = "Type bars striking, with a carriage return on enter." +volume = "Volume" + [editor.sidebar.layout] background_blur = "Background Blur" custom_position = "Custom screen position (dragged on canvas)" @@ -2500,6 +2526,7 @@ frames = { one = "{rendered} / {count} frame", other = "{rendered} / {count} fra generating_preview = "Generating preview…" link_unavailable_cursor = "Cursor-only exports can only be saved to a file or clipboard." link_unavailable_transparent = "Transparent exports can only be saved to a file or clipboard." +more_formats = { text = "More", context = "Export format option that reveals less common formats, such as separate tracks." } organization = "Organization" preview = "Preview" preview_info = "This is a rendered frame from your video. Adjust the settings to see the quality of the final export." @@ -2547,7 +2574,9 @@ file = { text = "File", context = "Export destination." } link = { text = "Shareable Link", context = "Export destination." } [editor.export.done] +copy_file = "Copy to Clipboard" copy_link = "Copy link" +file_copied = "Copied" instant_hint = "Use Instant Mode for your next recording if you want a link the moment you stop." link_copied = "Link copied" open_file = "Open File" @@ -2642,14 +2671,16 @@ format = { text = "Tracks", context = "Export format: separate screen, camera an microphone = "Microphone" open_folder = "Open Folder" screen = "Screen" +separate = "Separate tracks" +separate_description = "Screen, camera and audio, each as its own file" studio_only = "Separate tracks are only recorded in Studio Mode." system_audio = "System audio" video_detail = { text = "MP4 · {width}×{height}", context = "Video track file type and size, for example MP4 · 1920×1080." } [editor.export.turbo] title = "Turbo export" -description = "Exports much faster using every CPU core and the GPU" -warning = "Uses all of your CPU, the GPU and several times more memory until the export finishes. Other apps may slow down, and your computer may get warm and loud." +description = "Finishes exports much faster by using more of your computer's power" +warning = "Other apps may feel a little slower until the export finishes." [editor.modal] cancel_export = "Cancel export" @@ -2716,6 +2747,8 @@ advanced_shadow = "Advanced shadow settings" background = "Background" corner_style = "Corner Style" custom_position = "Custom position (dragged on canvas)" +empty_description = "Choose a camera before you record and you can shape and place it here." +empty_title = "No camera in this recording" hide = "Hide Camera" keep_size_during_zoom = "Keep original size during zoom" mirror = "Mirror Camera" @@ -2842,12 +2875,16 @@ minimal = "Minimal" minimal_description = "Subtle outlined text with no background." pop = "Pop" pop_description = "Playful caps that pop in with a vibrant accent." +soft = "Soft" +soft_description = "Rounded charcoal card that matches the keys." [editor.tabs.corner] rounded = "Rounded" squircle = "Squircle" [editor.tabs.cursor] +empty_description = "This recording didn't capture the cursor, so there's nothing to style yet." +empty_title = "No cursor in this recording" follow_hover = "Follow Hover Highlights" friction = "Friction" hide_when_idle = "Hide When Idle" @@ -2873,6 +2910,7 @@ smooth = "Smooth" smooth_description = "Soft, floaty movement" [editor.tabs.keyboard] +add_to_timeline = "Add to Timeline" animation = "Animation" background_color = "Background Color" background_opacity = "Background Opacity" @@ -2889,18 +2927,38 @@ font_weight = "Font Weight" generate = "Generate Keyboard Segments" generating = "Generating Keyboard Segments..." grouping_threshold = "Grouping Threshold" +key_style = "Key Style" +key_theme = "Key Theme" linger_duration = "Linger Duration" load_failed = "Failed to load recording data: {error}" no_presses = "No recorded keyboard presses were found." position = "Position" regenerate = "Regenerate Keyboard Segments" +show_as = "Show As" show_modifiers = "Show Modifiers" show_special_keys = "Show Special Keys" +show_typing = "Show Typing" size_label = "Size" +split_keys = { text = "Keys", context = "How typed text shows: one key per keystroke." } +split_sentence = { text = "Sentence", context = "How typed text shows: the whole sentence on one key." } +split_words = { text = "Words", context = "How typed text shows: one key per word." } +style_classic = "Classic" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "Mechanical" +style_windows = "Windows" text_color = "Text Color" +theme_dark = "Dark" +theme_light = "Light" +theme_retro = "Retro" timeline_unavailable = "The project timeline is unavailable." timeline_unreadable = "The project timeline could not be read." title = "Show keyboard" +type_keys = "Type Keys" +type_keys_button = "Type Keys…" +type_keys_hint = { text = "Paste a sentence, a script or a transcript, or a shortcut like cmd+shift+p. It types out as key presses from where you opened this.", context = "cmd+shift+p is typed as is and must stay in English." } +type_keys_placeholder = { text = "A sentence, or a shortcut like cmd+k", context = "Placeholder in a text box; cmd+k is typed as is and must stay in English." } +type_keys_summary = { text = "About {seconds} s of typing", context = "{seconds} is a number of seconds, like 4.2." } uppercase = "Uppercase" [editor.tabs.padding] @@ -2911,7 +2969,10 @@ proportional = "Proportional" bottom_center = "Bottom Center" bottom_left = "Bottom Left" bottom_right = "Bottom Right" +center = "Center" manual = "Manual" +middle_left = "Middle Left" +middle_right = "Middle Right" top_center = "Top Center" top_left = "Top Left" top_right = "Top Right" @@ -2981,6 +3042,7 @@ title = { text = "Timeline", context = "Label above the timeline while the edito [editor.timeline.empty] add_audio = "Add audio" +add_key = "Add a key press" add_scene = "Add scene" audio = "Add background music or import your own audio" captions = "No captions" @@ -3119,6 +3181,7 @@ crop = "Crop" crop_tooltip = "Crop Video" no_room_for_3d_shot = "No room for another 3D shot" no_room_for_caption = "No room for another caption here" +no_room_for_key = "No room for another key press here" redo = "Redo" save_failed = { text = "Could not save {path}. Keep the editor open and try again: {error}", context = "{path} is the recording's folder; {error} is a technical message that stays in English." } undo = "Undo" @@ -3233,6 +3296,26 @@ no_segments = "Recording has no segments. It may need to be recovered first." not_found = "Video path {path} not found!" not_studio = "Cannot edit non-studio recordings" +[editor.window.looks] +launch_day = "Launch Day" +launch_day_description = "A vivid gradient, snappy zooms and bold captions." +mac_native = "Mac Native" +mac_native_description = "A macOS window on Tahoe with white Mac keys." +midnight = "Midnight" +midnight_description = "A deep, dark backdrop with a crisp edge." +minimal = "Minimal" +minimal_description = "A quiet off-white canvas and nothing extra." +retro_desk = "Retro Desk" +retro_desk_description = "Warm beige and chunky mechanical keys." +saved = "Your Presets" +smooth_demo = "Smooth Demo" +smooth_demo_description = "Soft clouds, gentle zooms and a calm cursor." +title = "Looks" +tutorial = "Tutorial" +tutorial_description = "Slow, steady zooms and keys anyone can follow." +web_product = "Web Product" +web_product_description = "A browser window for site and app demos." + [editor.window.presets] apply = "Apply" create = "Create" @@ -3447,4 +3530,4 @@ no_resumable_state = "This recording has no resumable upload state" verification_failed = { text = "Recording verification failed: {status}", context = "{status} is a technical status, usually in English." } [message.cli] -binary_missing = { text = "Bundled CLI binary not found at {path}", context = "{path} is a file path." } \ No newline at end of file +binary_missing = { text = "Bundled CLI binary not found at {path}", context = "{path} is a file path." } diff --git a/apps/desktop-gpui/locales/es.toml b/apps/desktop-gpui/locales/es.toml index 920cd1dd872..ea649bde39b 100644 --- a/apps/desktop-gpui/locales/es.toml +++ b/apps/desktop-gpui/locales/es.toml @@ -30,6 +30,7 @@ website = "Sitio web" commit = "Commit" [menu.file] +new_blank_project = "Nuevo proyecto en blanco" open_prompt = "Abrir grabación" close_window = "Cerrar ventana" title = "Archivo" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "No se pudo copiar la captura de pantalla" import_recordings = "Importar" import_screenshots = "Importar imagen" importing = "Importando" +new_project = "Nuevo" no_matching_recordings = "No hay grabaciones que coincidan" no_matching_screenshots = "No hay capturas que coincidan" no_recordings = "Aún no hay grabaciones" @@ -1165,6 +1167,7 @@ no_displays = "No se encontraron pantallas" no_matching_displays = "No hay pantallas que coincidan" no_matching_windows = "No hay ventanas que coincidan" no_windows = "No se encontraron ventanas" +solo_title = "Solo cámara o audio" window = "Ventana" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm} ppm" [import] audio_failed = "No se pudo importar el audio: {error}" +blank_failed = "No se pudo crear el proyecto: {error}" +blank_name = "Sin título" error_title = "Error de importación" image_failed = "No se pudo importar la imagen: {error}" unsupported = "Archivo multimedia no compatible." @@ -1854,9 +1859,9 @@ done = "Listo" import = "Importar archivo" [editor.audio_only] -add_visuals = "Añadir visuales" body = "Esta grabación no tiene vídeo. Exporta el sonido como MP3 o WAV, o añade elementos visuales para convertirla en un vídeo." export_audio = "Exportar audio" +make_video = "Convertirlo en vídeo" show_visuals = "Mostrar visuales" size_large = "Grande" size_medium = "Mediano" @@ -2310,12 +2315,16 @@ save_failed = "No se pudo guardar este degradado. Inténtalo de nuevo." save_tooltip = "Guardar el degradado actual" saved = "Guardados" still = "Quieto" +style = "Estilo" +style_dither = "Tramado" +style_smooth = "Suave" [editor.sidebar.animated_gradient.control] contrast = "Contraste" curvature = "Curvatura" detail = "Detalle" direction = "Dirección" +dither_size = "Tamaño del tramado" exposure = "Exposición" flow_scale = "Escala del flujo" flow_strength = "Fuerza del flujo" @@ -2381,6 +2390,23 @@ to = "Hasta" [editor.sidebar.image] prompt = "Haz clic para elegir o arrastra y suelta una imagen" +[editor.sidebar.input_sounds] +click_sounds = "Sonidos de clic" +deep = "Grave" +deep_description = "Interruptores lineales con un golpe grave y redondo." +key_sounds = "Sonidos de teclas" +laptop = "Portátil" +laptop_description = "Teclas de portátil suaves y de poco recorrido." +mechanical = "Mecánico" +mechanical_description = "Interruptores nítidos con un clac brillante y táctil." +mouse = "Ratón" +mouse_description = "Pulsar y soltar un botón del ratón." +trackpad = "Trackpad" +trackpad_description = "El clic más suave de un trackpad de portátil." +typewriter = "Máquina de escribir" +typewriter_description = "Tipos que golpean, con retorno de carro al pulsar Intro." +volume = "Volumen" + [editor.sidebar.layout] background_blur = "Desenfoque del fondo" custom_position = "Posición de pantalla personalizada (arrastrada en el lienzo)" @@ -2498,6 +2524,7 @@ frames = { one = "{rendered} / {count} fotograma", other = "{rendered} / {count} generating_preview = "Generando vista previa…" link_unavailable_cursor = "Las exportaciones solo del cursor solo se pueden guardar en un archivo o en el portapapeles." link_unavailable_transparent = "Las exportaciones transparentes solo se pueden guardar en un archivo o en el portapapeles." +more_formats = "Más" organization = "Organización" preview = "Vista previa" preview_info = "Este es un fotograma renderizado de tu vídeo. Ajusta la configuración para ver la calidad de la exportación final." @@ -2545,7 +2572,9 @@ file = "Archivo" link = "Enlace para compartir" [editor.export.done] +copy_file = "Copiar al portapapeles" copy_link = "Copiar enlace" +file_copied = "Copiado" instant_hint = "Usa el modo Instant en tu próxima grabación si quieres un enlace en cuanto te detengas." link_copied = "Enlace copiado" open_file = "Abrir archivo" @@ -2640,14 +2669,16 @@ format = "Pistas" microphone = "Micrófono" open_folder = "Abrir carpeta" screen = "Pantalla" +separate = "Pistas separadas" +separate_description = "Pantalla, cámara y audio, cada uno en su propio archivo" studio_only = "Las pistas separadas solo se graban en el modo Studio." system_audio = "Audio del sistema" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "Exportación turbo" -description = "Exporta mucho más rápido usando todos los núcleos de la CPU y la GPU" -warning = "Usa toda tu CPU, la GPU y varias veces más memoria hasta que termina la exportación. Otras apps pueden ir más lentas, y tu ordenador puede calentarse y hacer ruido." +description = "Termina las exportaciones mucho más rápido usando más potencia de tu ordenador" +warning = "Otras apps pueden ir un poco más lentas hasta que termine la exportación." [editor.modal] cancel_export = "Cancelar exportación" @@ -2714,6 +2745,8 @@ advanced_shadow = "Ajustes avanzados de sombra" background = "Fondo" corner_style = "Estilo de esquinas" custom_position = "Posición personalizada (arrastrada en el lienzo)" +empty_description = "Elige una cámara antes de grabar y podrás darle forma y colocarla aquí." +empty_title = "No hay cámara en esta grabación" hide = "Ocultar cámara" keep_size_during_zoom = "Mantener el tamaño original durante el zoom" mirror = "Reflejar cámara" @@ -2840,12 +2873,16 @@ minimal = "Mínimo" minimal_description = "Texto sutil con contorno y sin fondo." pop = "Pop" pop_description = "Mayúsculas divertidas que aparecen con un acento vibrante." +soft = "Suave" +soft_description = "Tarjeta redondeada en gris carbón a juego con las teclas." [editor.tabs.corner] rounded = "Redondeadas" squircle = "Squircle" [editor.tabs.cursor] +empty_description = "Esta grabación no capturó el cursor, así que todavía no hay nada que ajustar." +empty_title = "No hay cursor en esta grabación" follow_hover = "Seguir resaltados al pasar" friction = "Fricción" hide_when_idle = "Ocultar en reposo" @@ -2871,6 +2908,7 @@ smooth = "Suave" smooth_description = "Movimiento suave y flotante" [editor.tabs.keyboard] +add_to_timeline = "Añadir a la línea de tiempo" animation = "Animación" background_color = "Color de fondo" background_opacity = "Opacidad del fondo" @@ -2887,18 +2925,38 @@ font_weight = "Grosor de fuente" generate = "Generar segmentos de teclado" generating = "Generando segmentos de teclado..." grouping_threshold = "Umbral de agrupación" +key_style = "Estilo de teclas" +key_theme = "Tema de teclas" linger_duration = "Tiempo en pantalla" load_failed = "No se pudieron cargar los datos de la grabación: {error}" no_presses = "No se encontraron pulsaciones de teclado grabadas." position = "Posición" regenerate = "Regenerar segmentos de teclado" +show_as = "Mostrar como" show_modifiers = "Mostrar modificadores" show_special_keys = "Mostrar teclas especiales" +show_typing = "Mostrar lo que escribes" size_label = "Tamaño" +split_keys = "Teclas" +split_sentence = "Frase" +split_words = "Palabras" +style_classic = "Clásico" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "Mecánico" +style_windows = "Windows" text_color = "Color del texto" +theme_dark = "Oscuro" +theme_light = "Claro" +theme_retro = "Retro" timeline_unavailable = "La línea de tiempo del proyecto no está disponible." timeline_unreadable = "No se pudo leer la línea de tiempo del proyecto." title = "Mostrar teclado" +type_keys = "Escribir teclas" +type_keys_button = "Escribir teclas…" +type_keys_hint = "Pega una frase, un guion o una transcripción, o un atajo como cmd+shift+p. Se escribe como pulsaciones desde donde abriste esto." +type_keys_placeholder = "Una frase o un atajo como cmd+k" +type_keys_summary = "Unos {seconds} s de escritura" uppercase = "Mayúsculas" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "Proporcional" bottom_center = "Abajo al centro" bottom_left = "Abajo a la izquierda" bottom_right = "Abajo a la derecha" +center = "Centro" manual = "Manual" +middle_left = "Centro izquierda" +middle_right = "Centro derecha" top_center = "Arriba al centro" top_left = "Arriba a la izquierda" top_right = "Arriba a la derecha" @@ -2979,6 +3040,7 @@ title = "Línea de tiempo" [editor.timeline.empty] add_audio = "Añadir audio" +add_key = "Añadir una pulsación" add_scene = "Añadir escena" audio = "Añade música de fondo o importa tu propio audio" captions = "Sin subtítulos" @@ -3117,6 +3179,7 @@ crop = "Recortar" crop_tooltip = "Recortar vídeo" no_room_for_3d_shot = "No hay espacio para otra toma 3D" no_room_for_caption = "No hay espacio para otro subtítulo aquí" +no_room_for_key = "No hay espacio para otra pulsación aquí" redo = "Rehacer" save_failed = "No se pudo guardar {path}. Mantén el editor abierto e inténtalo de nuevo: {error}" undo = "Deshacer" @@ -3231,6 +3294,26 @@ no_segments = "La grabación no tiene segmentos. Puede que primero haya que recu not_found = "¡No se encontró la ruta de vídeo {path}!" not_studio = "Solo se pueden editar grabaciones Studio" +[editor.window.looks] +launch_day = "Día de lanzamiento" +launch_day_description = "Un degradado vivo, zooms ágiles y subtítulos llamativos." +mac_native = "Mac nativo" +mac_native_description = "Una ventana de macOS sobre Tahoe con teclas blancas de Mac." +midnight = "Medianoche" +midnight_description = "Un fondo oscuro y profundo con un borde nítido." +minimal = "Minimalista" +minimal_description = "Un lienzo blanco roto, tranquilo y sin nada más." +retro_desk = "Escritorio retro" +retro_desk_description = "Beige cálido y teclas mecánicas robustas." +saved = "Tus preajustes" +smooth_demo = "Demo suave" +smooth_demo_description = "Nubes suaves, zooms delicados y un cursor tranquilo." +title = "Estilos" +tutorial = "Tutorial" +tutorial_description = "Zooms lentos y estables, y teclas que cualquiera puede seguir." +web_product = "Producto web" +web_product_description = "Una ventana de navegador para demos de webs y apps." + [editor.window.presets] apply = "Aplicar" create = "Crear" @@ -3445,4 +3528,4 @@ no_resumable_state = "Esta grabación no tiene una subida que se pueda reanudar" verification_failed = "La verificación de la grabación ha fallado: {status}" [message.cli] -binary_missing = "No se encontró la CLI incluida en {path}" \ No newline at end of file +binary_missing = "No se encontró la CLI incluida en {path}" diff --git a/apps/desktop-gpui/locales/fr.toml b/apps/desktop-gpui/locales/fr.toml index cab06798301..4b3e957ad44 100644 --- a/apps/desktop-gpui/locales/fr.toml +++ b/apps/desktop-gpui/locales/fr.toml @@ -30,6 +30,7 @@ website = "Site web" commit = "Commit" [menu.file] +new_blank_project = "Nouveau projet vierge" open_prompt = "Ouvrir l'enregistrement" close_window = "Fermer la fenêtre" title = "Fichier" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "Impossible de copier la capture d'écran" import_recordings = "Importer" import_screenshots = "Importer une image" importing = "Importation" +new_project = "Nouveau" no_matching_recordings = "Aucun enregistrement correspondant" no_matching_screenshots = "Aucune capture d'écran correspondante" no_recordings = "Aucun enregistrement pour l'instant" @@ -1165,6 +1167,7 @@ no_displays = "Aucun écran trouvé" no_matching_displays = "Aucun écran correspondant" no_matching_windows = "Aucune fenêtre correspondante" no_windows = "Aucune fenêtre trouvée" +solo_title = "Caméra ou audio seul" window = "Fenêtre" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm} mots/min" [import] audio_failed = "Impossible d'importer l'audio : {error}" +blank_failed = "Impossible de créer le projet : {error}" +blank_name = "Sans titre" error_title = "Erreur d'importation" image_failed = "Impossible d'importer l'image : {error}" unsupported = "Fichier multimédia non pris en charge." @@ -1854,9 +1859,9 @@ done = "Terminé" import = "Importer un fichier" [editor.audio_only] -add_visuals = "Ajouter des visuels" body = "Cet enregistrement n'a pas de vidéo. Exportez le son en MP3 ou WAV, ou ajoutez des visuels pour en faire une vidéo." export_audio = "Exporter l'audio" +make_video = "En faire une vidéo" show_visuals = "Afficher les visuels" size_large = "Grand" size_medium = "Moyen" @@ -2310,12 +2315,16 @@ save_failed = "Impossible d'enregistrer ce dégradé. Réessayez." save_tooltip = "Enregistrer le dégradé actuel" saved = "Enregistrés" still = "Immobile" +style = "Style" +style_dither = "Tramé" +style_smooth = "Lisse" [editor.sidebar.animated_gradient.control] contrast = "Contraste" curvature = "Courbure" detail = "Détail" direction = "Direction" +dither_size = "Taille du tramé" exposure = "Exposition" flow_scale = "Échelle du flux" flow_strength = "Force du flux" @@ -2381,6 +2390,23 @@ to = "À" [editor.sidebar.image] prompt = "Cliquez pour choisir ou glissez-déposez une image" +[editor.sidebar.input_sounds] +click_sounds = "Sons des clics" +deep = "Grave" +deep_description = "Interrupteurs linéaires au son grave et rond." +key_sounds = "Sons des touches" +laptop = "Portable" +laptop_description = "Touches de portable douces et peu profondes." +mechanical = "Mécanique" +mechanical_description = "Interrupteurs nets au clac vif et tactile." +mouse = "Souris" +mouse_description = "L'appui et le relâchement d'un bouton de souris." +trackpad = "Trackpad" +trackpad_description = "Le clic plus doux d'un trackpad de portable." +typewriter = "Machine à écrire" +typewriter_description = "Des barres qui frappent, avec un retour chariot sur Entrée." +volume = "Volume" + [editor.sidebar.layout] background_blur = "Flou de l'arrière-plan" custom_position = "Position d'écran personnalisée (déplacée sur le canevas)" @@ -2498,6 +2524,7 @@ frames = { one = "{rendered} / {count} image", other = "{rendered} / {count} ima generating_preview = "Génération de l'aperçu…" link_unavailable_cursor = "Les exportations du curseur seul ne peuvent être enregistrées que dans un fichier ou le presse-papiers." link_unavailable_transparent = "Les exportations transparentes ne peuvent être enregistrées que dans un fichier ou le presse-papiers." +more_formats = "Plus" organization = "Organisation" preview = "Aperçu" preview_info = "Ceci est une image rendue de votre vidéo. Ajustez les paramètres pour voir la qualité de l'exportation finale." @@ -2545,7 +2572,9 @@ file = "Fichier" link = "Lien de partage" [editor.export.done] +copy_file = "Copier dans le presse-papiers" copy_link = "Copier le lien" +file_copied = "Copié" instant_hint = "Utilisez le mode Instant pour votre prochain enregistrement si vous voulez un lien dès que vous arrêtez." link_copied = "Lien copié" open_file = "Ouvrir le fichier" @@ -2640,14 +2669,16 @@ format = "Pistes" microphone = "Micro" open_folder = "Ouvrir le dossier" screen = "Écran" +separate = "Pistes séparées" +separate_description = "Écran, caméra et audio, chacun dans son propre fichier" studio_only = "Les pistes séparées ne sont enregistrées qu'en mode Studio." system_audio = "Audio système" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "Exportation turbo" -description = "Exporte beaucoup plus vite en utilisant tous les cœurs du processeur et le GPU" -warning = "Utilise tout votre processeur, le GPU et plusieurs fois plus de mémoire jusqu'à la fin de l'exportation. Les autres apps peuvent ralentir, et votre ordinateur peut chauffer et devenir bruyant." +description = "Termine les exportations beaucoup plus vite en utilisant davantage la puissance de votre ordinateur" +warning = "Les autres apps peuvent sembler un peu plus lentes jusqu'à la fin de l'exportation." [editor.modal] cancel_export = "Annuler l'exportation" @@ -2714,6 +2745,8 @@ advanced_shadow = "Réglages avancés de l'ombre" background = "Arrière-plan" corner_style = "Style des coins" custom_position = "Position personnalisée (déplacée sur le canevas)" +empty_description = "Choisissez une caméra avant d'enregistrer pour pouvoir la mettre en forme et la placer ici." +empty_title = "Aucune caméra dans cet enregistrement" hide = "Masquer la caméra" keep_size_during_zoom = "Conserver la taille d'origine pendant le zoom" mirror = "Effet miroir" @@ -2840,12 +2873,16 @@ minimal = "Minimal" minimal_description = "Texte discret avec contour, sans arrière-plan." pop = "Pop" pop_description = "Majuscules ludiques qui apparaissent avec une touche de couleur vive." +soft = "Doux" +soft_description = "Carte arrondie anthracite assortie aux touches." [editor.tabs.corner] rounded = "Arrondi" squircle = "Squircle" [editor.tabs.cursor] +empty_description = "Cet enregistrement n'a pas capturé le curseur, il n'y a donc encore rien à styliser." +empty_title = "Aucun curseur dans cet enregistrement" follow_hover = "Suivre les survols" friction = "Friction" hide_when_idle = "Masquer en cas d'inactivité" @@ -2871,6 +2908,7 @@ smooth = "Fluide" smooth_description = "Mouvement souple et flottant" [editor.tabs.keyboard] +add_to_timeline = "Ajouter à la timeline" animation = "Animation" background_color = "Couleur d'arrière-plan" background_opacity = "Opacité de l'arrière-plan" @@ -2887,18 +2925,38 @@ font_weight = "Graisse" generate = "Générer les segments clavier" generating = "Génération des segments clavier..." grouping_threshold = "Seuil de regroupement" +key_style = "Style des touches" +key_theme = "Thème des touches" linger_duration = "Durée d'affichage" load_failed = "Impossible de charger les données d'enregistrement : {error}" no_presses = "Aucune frappe enregistrée n'a été trouvée." position = "Position" regenerate = "Régénérer les segments clavier" +show_as = "Afficher en" show_modifiers = "Afficher les modificateurs" show_special_keys = "Afficher les touches spéciales" +show_typing = "Afficher la saisie" size_label = "Taille" +split_keys = "Touches" +split_sentence = "Phrase" +split_words = "Mots" +style_classic = "Classique" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "Mécanique" +style_windows = "Windows" text_color = "Couleur du texte" +theme_dark = "Sombre" +theme_light = "Clair" +theme_retro = "Rétro" timeline_unavailable = "La timeline du projet n'est pas disponible." timeline_unreadable = "Impossible de lire la timeline du projet." title = "Afficher le clavier" +type_keys = "Taper des touches" +type_keys_button = "Taper des touches…" +type_keys_hint = "Collez une phrase, un script ou une transcription, ou un raccourci comme cmd+shift+p. Le texte est tapé en touches à partir d’ici." +type_keys_placeholder = "Une phrase ou un raccourci comme cmd+k" +type_keys_summary = "Environ {seconds} s de frappe" uppercase = "Majuscules" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "Proportionnelle" bottom_center = "En bas au centre" bottom_left = "En bas à gauche" bottom_right = "En bas à droite" +center = "Centre" manual = "Manuelle" +middle_left = "Milieu gauche" +middle_right = "Milieu droite" top_center = "En haut au centre" top_left = "En haut à gauche" top_right = "En haut à droite" @@ -2979,6 +3040,7 @@ title = "Timeline" [editor.timeline.empty] add_audio = "Ajouter de l'audio" +add_key = "Ajouter une touche" add_scene = "Ajouter une scène" audio = "Ajoutez une musique de fond ou importez votre propre audio" captions = "Aucun sous-titre" @@ -3117,6 +3179,7 @@ crop = "Recadrer" crop_tooltip = "Recadrer la vidéo" no_room_for_3d_shot = "Plus de place pour un autre plan 3D" no_room_for_caption = "Pas de place pour un autre sous-titre ici" +no_room_for_key = "Pas de place pour une autre touche ici" redo = "Rétablir" save_failed = "Impossible d'enregistrer {path}. Gardez l'éditeur ouvert et réessayez : {error}" undo = "Annuler" @@ -3231,6 +3294,26 @@ no_segments = "L'enregistrement n'a aucun segment. Il faut peut-être d'abord le not_found = "Chemin vidéo {path} introuvable !" not_studio = "Seuls les enregistrements Studio peuvent être modifiés" +[editor.window.looks] +launch_day = "Jour de lancement" +launch_day_description = "Un dégradé éclatant, des zooms vifs et des sous-titres marquants." +mac_native = "Mac natif" +mac_native_description = "Une fenêtre macOS sur Tahoe avec des touches Mac blanches." +midnight = "Minuit" +midnight_description = "Un fond sombre et profond au contour net." +minimal = "Minimal" +minimal_description = "Une toile blanc cassé, calme, sans rien de plus." +retro_desk = "Bureau rétro" +retro_desk_description = "Un beige chaleureux et de grosses touches mécaniques." +saved = "Vos préréglages" +smooth_demo = "Démo fluide" +smooth_demo_description = "Des nuages doux, des zooms délicats et un curseur posé." +title = "Styles" +tutorial = "Tutoriel" +tutorial_description = "Des zooms lents et réguliers, et des touches faciles à suivre." +web_product = "Produit web" +web_product_description = "Une fenêtre de navigateur pour les démos de sites et d'apps." + [editor.window.presets] apply = "Appliquer" create = "Créer" @@ -3445,4 +3528,4 @@ no_resumable_state = "Cet enregistrement n'a pas d'envoi pouvant reprendre" verification_failed = "La vérification de l'enregistrement a échoué : {status}" [message.cli] -binary_missing = "CLI intégrée introuvable dans {path}" \ No newline at end of file +binary_missing = "CLI intégrée introuvable dans {path}" diff --git a/apps/desktop-gpui/locales/hi.toml b/apps/desktop-gpui/locales/hi.toml index 539cb62f991..0a8b538ba50 100644 --- a/apps/desktop-gpui/locales/hi.toml +++ b/apps/desktop-gpui/locales/hi.toml @@ -30,6 +30,7 @@ website = "वेबसाइट" commit = "कमिट" [menu.file] +new_blank_project = "नया खाली प्रोजेक्ट" open_prompt = "रिकॉर्डिंग खोलें" close_window = "विंडो बंद करें" title = "फ़ाइल" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "स्क्रीनशॉट कॉपी नही import_recordings = "इम्पोर्ट करें" import_screenshots = "इमेज इम्पोर्ट करें" importing = "इम्पोर्ट हो रहा है" +new_project = "नया" no_matching_recordings = "कोई मिलती-जुलती रिकॉर्डिंग नहीं" no_matching_screenshots = "कोई मिलता-जुलता स्क्रीनशॉट नहीं" no_recordings = "अभी कोई रिकॉर्डिंग नहीं" @@ -1165,6 +1167,7 @@ no_displays = "कोई डिस्प्ले नहीं मिला" no_matching_displays = "कोई मिलता-जुलता डिस्प्ले नहीं" no_matching_windows = "कोई मिलती-जुलती विंडो नहीं" no_windows = "कोई विंडो नहीं मिली" +solo_title = "सिर्फ़ कैमरा या ऑडियो" window = "विंडो" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm} शब्द/मिनट" [import] audio_failed = "ऑडियो इंपोर्ट नहीं हो सका: {error}" +blank_failed = "प्रोजेक्ट नहीं बन सका: {error}" +blank_name = "बिना शीर्षक" error_title = "इम्पोर्ट त्रुटि" image_failed = "इमेज इम्पोर्ट नहीं हो सकी: {error}" unsupported = "यह मीडिया फ़ाइल समर्थित नहीं है।" @@ -1854,9 +1859,9 @@ done = "हो गया" import = "फ़ाइल इम्पोर्ट करें" [editor.audio_only] -add_visuals = "विज़ुअल जोड़ें" body = "इस रिकॉर्डिंग में वीडियो नहीं है। आवाज़ को MP3 या WAV के रूप में एक्सपोर्ट करें, या इसे वीडियो बनाने के लिए विज़ुअल जोड़ें।" export_audio = "ऑडियो एक्सपोर्ट करें" +make_video = "इसे वीडियो बनाएँ" show_visuals = "विज़ुअल दिखाएं" size_large = "बड़ा" size_medium = "मध्यम" @@ -2310,12 +2315,16 @@ save_failed = "यह ग्रेडिएंट सेव नहीं हो save_tooltip = "मौजूदा ग्रेडिएंट सेव करें" saved = "सेव किए गए" still = "स्थिर" +style = "स्टाइल" +style_dither = "डिथर" +style_smooth = "स्मूद" [editor.sidebar.animated_gradient.control] contrast = "कंट्रास्ट" curvature = "वक्रता" detail = "डिटेल" direction = "दिशा" +dither_size = "डिथर आकार" exposure = "एक्सपोज़र" flow_scale = "फ़्लो स्केल" flow_strength = "फ़्लो की ताकत" @@ -2381,6 +2390,23 @@ to = "तक" [editor.sidebar.image] prompt = "चुनने के लिए क्लिक करें या इमेज ड्रैग करके छोड़ें" +[editor.sidebar.input_sounds] +click_sounds = "क्लिक ध्वनियाँ" +deep = "गहरी" +deep_description = "गहरी, गोल आवाज़ वाले लीनियर स्विच।" +key_sounds = "कुंजी ध्वनियाँ" +laptop = "लैपटॉप" +laptop_description = "नरम, उथली लैपटॉप कुंजियाँ।" +mechanical = "मैकेनिकल" +mechanical_description = "साफ़, स्पर्शनीय खटखट वाले कुरकुरे स्विच।" +mouse = "माउस" +mouse_description = "माउस बटन को दबाना और छोड़ना।" +trackpad = "ट्रैकपैड" +trackpad_description = "लैपटॉप ट्रैकपैड का हल्का क्लिक।" +typewriter = "टाइपराइटर" +typewriter_description = "टकराती टाइप बार, एंटर पर कैरिज रिटर्न के साथ।" +volume = "वॉल्यूम" + [editor.sidebar.layout] background_blur = "बैकग्राउंड ब्लर" custom_position = "कस्टम स्क्रीन पोज़िशन (कैनवास पर ड्रैग की गई)" @@ -2498,6 +2524,7 @@ frames = { one = "{rendered} / {count} फ़्रेम", other = "{rendered} generating_preview = "प्रीव्यू बनाया जा रहा है…" link_unavailable_cursor = "सिर्फ़ कर्सर वाले एक्सपोर्ट को फ़ाइल या क्लिपबोर्ड में ही सेव किया जा सकता है।" link_unavailable_transparent = "पारदर्शी एक्सपोर्ट को फ़ाइल या क्लिपबोर्ड में ही सेव किया जा सकता है।" +more_formats = "और" organization = "संगठन" preview = "प्रीव्यू" preview_info = "यह आपके वीडियो का रेंडर किया गया फ़्रेम है। फ़ाइनल एक्सपोर्ट की क्वालिटी देखने के लिए सेटिंग्स बदलें।" @@ -2545,7 +2572,9 @@ file = "फ़ाइल" link = "शेयर लिंक" [editor.export.done] +copy_file = "क्लिपबोर्ड पर कॉपी करें" copy_link = "लिंक कॉपी करें" +file_copied = "कॉपी हो गया" instant_hint = "अगर आप रोकते ही लिंक चाहते हैं, तो अगली रिकॉर्डिंग के लिए Instant मोड इस्तेमाल करें।" link_copied = "लिंक कॉपी हो गया" open_file = "फ़ाइल खोलें" @@ -2640,14 +2669,16 @@ format = "ट्रैक" microphone = "माइक्रोफ़ोन" open_folder = "फ़ोल्डर खोलें" screen = "स्क्रीन" +separate = "अलग-अलग ट्रैक" +separate_description = "स्क्रीन, कैमरा और ऑडियो, हर एक अपनी अलग फ़ाइल में" studio_only = "अलग-अलग ट्रैक सिर्फ़ Studio मोड में रिकॉर्ड होते हैं।" system_audio = "सिस्टम ऑडियो" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "टर्बो एक्सपोर्ट" -description = "हर CPU कोर और GPU का इस्तेमाल करके कहीं तेज़ी से एक्सपोर्ट करता है" -warning = "एक्सपोर्ट पूरा होने तक आपका पूरा CPU, GPU और कई गुना ज़्यादा मेमोरी इस्तेमाल करता है। दूसरे ऐप्स धीमे हो सकते हैं, और आपका कंप्यूटर गर्म होकर ज़्यादा आवाज़ कर सकता है।" +description = "आपके कंप्यूटर की ज़्यादा ताक़त इस्तेमाल करके एक्सपोर्ट कहीं तेज़ी से पूरा करता है" +warning = "एक्सपोर्ट पूरा होने तक दूसरे ऐप्स थोड़े धीमे लग सकते हैं।" [editor.modal] cancel_export = "एक्सपोर्ट रद्द करें" @@ -2714,6 +2745,8 @@ advanced_shadow = "छाया की एडवांस्ड सेटिं background = "बैकग्राउंड" corner_style = "कोने की स्टाइल" custom_position = "कस्टम पोज़िशन (कैनवास पर ड्रैग की गई)" +empty_description = "रिकॉर्ड करने से पहले कैमरा चुनें, फिर आप उसे यहाँ आकार दे सकते हैं और जगह तय कर सकते हैं।" +empty_title = "इस रिकॉर्डिंग में कैमरा नहीं है" hide = "कैमरा छिपाएँ" keep_size_during_zoom = "ज़ूम के दौरान मूल आकार बनाए रखें" mirror = "कैमरा मिरर करें" @@ -2840,12 +2873,16 @@ minimal = "मिनिमल" minimal_description = "बिना बैकग्राउंड के हल्का आउटलाइन टेक्स्ट।" pop = "पॉप" pop_description = "चटक रंग के साथ उभरते मज़ेदार बड़े अक्षर।" +soft = "सॉफ़्ट" +soft_description = "कुंजियों से मेल खाता गोल चारकोल कार्ड।" [editor.tabs.corner] rounded = "गोल" squircle = "स्क्वर्कल" [editor.tabs.cursor] +empty_description = "इस रिकॉर्डिंग में कर्सर कैप्चर नहीं हुआ, इसलिए अभी स्टाइल करने को कुछ नहीं है।" +empty_title = "इस रिकॉर्डिंग में कर्सर नहीं है" follow_hover = "होवर हाइलाइट का अनुसरण करें" friction = "घर्षण" hide_when_idle = "निष्क्रिय होने पर छिपाएँ" @@ -2871,6 +2908,7 @@ smooth = "स्मूथ" smooth_description = "मुलायम, तैरता हुआ मूवमेंट" [editor.tabs.keyboard] +add_to_timeline = "टाइमलाइन में जोड़ें" animation = "एनिमेशन" background_color = "बैकग्राउंड रंग" background_opacity = "बैकग्राउंड अपारदर्शिता" @@ -2887,18 +2925,38 @@ font_weight = "फ़ॉन्ट वेट" generate = "कीबोर्ड सेगमेंट बनाएँ" generating = "कीबोर्ड सेगमेंट बनाए जा रहे हैं..." grouping_threshold = "ग्रुपिंग सीमा" +key_style = "की स्टाइल" +key_theme = "की थीम" linger_duration = "दिखने की अवधि" load_failed = "रिकॉर्डिंग डेटा लोड नहीं हो सका: {error}" no_presses = "कोई रिकॉर्ड किया गया की-प्रेस नहीं मिला।" position = "पोज़िशन" regenerate = "कीबोर्ड सेगमेंट दोबारा बनाएँ" +show_as = "ऐसे दिखाएँ" show_modifiers = "मॉडिफ़ायर दिखाएँ" show_special_keys = "विशेष कुंजियाँ दिखाएँ" +show_typing = "टाइपिंग दिखाएँ" size_label = "आकार" +split_keys = "की" +split_sentence = "वाक्य" +split_words = "शब्द" +style_classic = "क्लासिक" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "मैकेनिकल" +style_windows = "Windows" text_color = "टेक्स्ट रंग" +theme_dark = "डार्क" +theme_light = "लाइट" +theme_retro = "रेट्रो" timeline_unavailable = "प्रोजेक्ट टाइमलाइन उपलब्ध नहीं है।" timeline_unreadable = "प्रोजेक्ट टाइमलाइन पढ़ी नहीं जा सकी।" title = "कीबोर्ड दिखाएँ" +type_keys = "की टाइप करें" +type_keys_button = "की टाइप करें…" +type_keys_hint = "कोई वाक्य, स्क्रिप्ट या ट्रांसक्रिप्ट पेस्ट करें, या cmd+shift+p जैसा शॉर्टकट। यह यहीं से की प्रेस के रूप में टाइप होगा।" +type_keys_placeholder = "कोई वाक्य, या cmd+k जैसा शॉर्टकट" +type_keys_summary = "लगभग {seconds} से. की टाइपिंग" uppercase = "बड़े अक्षर" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "आनुपातिक" bottom_center = "नीचे बीच में" bottom_left = "नीचे बाएँ" bottom_right = "नीचे दाएँ" +center = "बीच" manual = "मैनुअल" +middle_left = "बीच बाएँ" +middle_right = "बीच दाएँ" top_center = "ऊपर बीच में" top_left = "ऊपर बाएँ" top_right = "ऊपर दाएँ" @@ -2979,6 +3040,7 @@ title = "टाइमलाइन" [editor.timeline.empty] add_audio = "ऑडियो जोड़ें" +add_key = "की प्रेस जोड़ें" add_scene = "सीन जोड़ें" audio = "बैकग्राउंड म्यूज़िक जोड़ें या अपना ऑडियो इम्पोर्ट करें" captions = "कोई कैप्शन नहीं" @@ -3117,6 +3179,7 @@ crop = "क्रॉप" crop_tooltip = "वीडियो क्रॉप करें" no_room_for_3d_shot = "एक और 3D शॉट के लिए जगह नहीं है" no_room_for_caption = "यहाँ एक और कैप्शन के लिए जगह नहीं है" +no_room_for_key = "यहाँ एक और की प्रेस के लिए जगह नहीं है" redo = "फिर करें" save_failed = "{path} सेव नहीं हो सका। एडिटर खुला रखें और फिर से कोशिश करें: {error}" undo = "पहले जैसा करें" @@ -3231,6 +3294,26 @@ no_segments = "रिकॉर्डिंग में कोई सेगम not_found = "वीडियो पाथ {path} नहीं मिला!" not_studio = "Studio के अलावा दूसरी रिकॉर्डिंग एडिट नहीं की जा सकतीं" +[editor.window.looks] +launch_day = "लॉन्च डे" +launch_day_description = "चटक ग्रेडिएंट, तेज़ ज़ूम और बोल्ड कैप्शन।" +mac_native = "मैक नेटिव" +mac_native_description = "Tahoe पर macOS विंडो, सफ़ेद Mac कीज़ के साथ।" +midnight = "मिडनाइट" +midnight_description = "गहरा, डार्क बैकड्रॉप और साफ़ किनारा।" +minimal = "मिनिमल" +minimal_description = "शांत ऑफ़-व्हाइट कैनवस, और कुछ नहीं।" +retro_desk = "रेट्रो डेस्क" +retro_desk_description = "गर्म बेज रंग और मोटी मैकेनिकल कीज़।" +saved = "आपके प्रीसेट" +smooth_demo = "स्मूद डेमो" +smooth_demo_description = "मुलायम बादल, हल्के ज़ूम और शांत कर्सर।" +title = "लुक्स" +tutorial = "ट्यूटोरियल" +tutorial_description = "धीमे, स्थिर ज़ूम और ऐसी कीज़ जिन्हें हर कोई समझ सके।" +web_product = "वेब प्रोडक्ट" +web_product_description = "साइट और ऐप डेमो के लिए ब्राउज़र विंडो।" + [editor.window.presets] apply = "लागू करें" create = "बनाएँ" @@ -3445,4 +3528,4 @@ no_resumable_state = "इस रिकॉर्डिंग का कोई ऐ verification_failed = "रिकॉर्डिंग की जाँच विफल हुई: {status}" [message.cli] -binary_missing = "{path} पर बंडल की गई CLI नहीं मिली" \ No newline at end of file +binary_missing = "{path} पर बंडल की गई CLI नहीं मिली" diff --git a/apps/desktop-gpui/locales/ja.toml b/apps/desktop-gpui/locales/ja.toml index 22a68894023..93a51688dab 100644 --- a/apps/desktop-gpui/locales/ja.toml +++ b/apps/desktop-gpui/locales/ja.toml @@ -30,6 +30,7 @@ website = "ウェブサイト" commit = "コミット" [menu.file] +new_blank_project = "新しい空のプロジェクト" open_prompt = "録画を開く" close_window = "ウインドウを閉じる" title = "ファイル" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "スクリーンショットをコピーできません import_recordings = "読み込む" import_screenshots = "画像を読み込む" importing = "読み込み中" +new_project = "新規" no_matching_recordings = "一致する録画はありません" no_matching_screenshots = "一致するスクリーンショットはありません" no_recordings = "録画はまだありません" @@ -1165,6 +1167,7 @@ no_displays = "ディスプレイが見つかりません" no_matching_displays = "一致するディスプレイはありません" no_matching_windows = "一致するウィンドウはありません" no_windows = "ウィンドウが見つかりません" +solo_title = "カメラまたは音声のみ" window = "ウィンドウ" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm}語/分" [import] audio_failed = "音声を読み込めませんでした: {error}" +blank_failed = "プロジェクトを作成できませんでした: {error}" +blank_name = "名称未設定" error_title = "読み込みエラー" image_failed = "画像を読み込めませんでした:{error}" unsupported = "サポートされていないメディアファイルです。" @@ -1854,9 +1859,9 @@ done = "完了" import = "ファイルを読み込む" [editor.audio_only] -add_visuals = "ビジュアルを追加" body = "この録音には映像がありません。音声をMP3またはWAVで書き出すか、ビジュアルを追加して動画にできます。" export_audio = "音声を書き出す" +make_video = "動画にする" show_visuals = "ビジュアルを表示" size_large = "大" size_medium = "中" @@ -2310,12 +2315,16 @@ save_failed = "このグラデーションを保存できませんでした。 save_tooltip = "現在のグラデーションを保存" saved = "保存済み" still = "静止" +style = "スタイル" +style_dither = "ディザ" +style_smooth = "なめらか" [editor.sidebar.animated_gradient.control] contrast = "コントラスト" curvature = "曲率" detail = "ディテール" direction = "方向" +dither_size = "ディザのサイズ" exposure = "露出" flow_scale = "流れの大きさ" flow_strength = "流れの強さ" @@ -2381,6 +2390,23 @@ to = "終了" [editor.sidebar.image] prompt = "クリックして選択するか、画像をドラッグ&ドロップ" +[editor.sidebar.input_sounds] +click_sounds = "クリック音" +deep = "ディープ" +deep_description = "低く丸みのある打鍵音のリニアスイッチ。" +key_sounds = "キー音" +laptop = "ノートパソコン" +laptop_description = "柔らかく浅いノートパソコンのキー。" +mechanical = "メカニカル" +mechanical_description = "明るく歯切れのよいタクタイルスイッチ。" +mouse = "マウス" +mouse_description = "マウスボタンを押して離す音。" +trackpad = "トラックパッド" +trackpad_description = "ノートパソコンのトラックパッドの柔らかいクリック。" +typewriter = "タイプライター" +typewriter_description = "活字が打たれ、Enterでキャリッジが戻ります。" +volume = "音量" + [editor.sidebar.layout] background_blur = "背景のぼかし" custom_position = "カスタムの画面位置(キャンバスでドラッグ)" @@ -2498,6 +2524,7 @@ frames = { other = "{rendered} / {count}フレーム" } generating_preview = "プレビューを生成中…" link_unavailable_cursor = "カーソルのみの書き出しは、ファイルかクリップボードにのみ保存できます。" link_unavailable_transparent = "透過の書き出しは、ファイルかクリップボードにのみ保存できます。" +more_formats = "その他" organization = "組織" preview = "プレビュー" preview_info = "動画からレンダリングしたフレームです。設定を調整すると、最終的な書き出しの品質を確認できます。" @@ -2545,7 +2572,9 @@ file = "ファイル" link = "共有リンク" [editor.export.done] +copy_file = "クリップボードにコピー" copy_link = "リンクをコピー" +file_copied = "コピーしました" instant_hint = "停止した瞬間にリンクが欲しい場合は、次の録画でInstantモードを使ってください。" link_copied = "リンクをコピーしました" open_file = "ファイルを開く" @@ -2640,14 +2669,16 @@ format = "トラック" microphone = "マイク" open_folder = "フォルダを開く" screen = "画面" +separate = "個別のトラック" +separate_description = "画面、カメラ、音声をそれぞれ別のファイルに" studio_only = "個別のトラックはStudioモードでのみ録画されます。" system_audio = "システム音声" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "ターボ書き出し" -description = "すべてのCPUコアとGPUを使って、はるかに高速に書き出します" -warning = "書き出しが終わるまで、CPUとGPUをすべて使い、メモリも数倍多く消費します。ほかのアプリの動作が遅くなったり、コンピュータが熱くなってファンの音が大きくなったりすることがあります。" +description = "コンピュータの性能をより多く使って、書き出しをはるかに速く終えます" +warning = "書き出しが終わるまで、ほかのアプリが少し遅く感じられることがあります。" [editor.modal] cancel_export = "書き出しをキャンセル" @@ -2714,6 +2745,8 @@ advanced_shadow = "影の詳細設定" background = "背景" corner_style = "角のスタイル" custom_position = "カスタム位置(キャンバス上でドラッグ)" +empty_description = "録画の前にカメラを選ぶと、ここで形や位置を調整できます。" +empty_title = "この録画にはカメラがありません" hide = "カメラを非表示" keep_size_during_zoom = "ズーム中も元のサイズを保つ" mirror = "カメラを反転" @@ -2840,12 +2873,16 @@ minimal = "ミニマル" minimal_description = "背景なしの控えめな縁取りテキスト。" pop = "ポップ" pop_description = "鮮やかなアクセントで飛び出す遊び心のある大文字。" +soft = "ソフト" +soft_description = "キーと揃えた、角の丸いチャコールのカード。" [editor.tabs.corner] rounded = "角丸" squircle = "スクワークル" [editor.tabs.cursor] +empty_description = "この録画ではカーソルが記録されなかったため、調整できる項目はまだありません。" +empty_title = "この録画にはカーソルがありません" follow_hover = "ホバー表示に合わせる" friction = "摩擦" hide_when_idle = "操作がないときは非表示" @@ -2871,6 +2908,7 @@ smooth = "なめらか" smooth_description = "柔らかく浮遊するような動き" [editor.tabs.keyboard] +add_to_timeline = "タイムラインに追加" animation = "アニメーション" background_color = "背景色" background_opacity = "背景の不透明度" @@ -2887,18 +2925,38 @@ font_weight = "フォントの太さ" generate = "キーボードセグメントを生成" generating = "キーボードセグメントを生成中..." grouping_threshold = "グループ化のしきい値" +key_style = "キーのスタイル" +key_theme = "キーのテーマ" linger_duration = "表示を残す時間" load_failed = "録画データを読み込めませんでした:{error}" no_presses = "記録されたキー入力が見つかりませんでした。" position = "位置" regenerate = "キーボードセグメントを再生成" +show_as = "表示方法" show_modifiers = "修飾キーを表示" show_special_keys = "特殊キーを表示" +show_typing = "入力した文字を表示" size_label = "サイズ" +split_keys = "キー" +split_sentence = "文章" +split_words = "単語" +style_classic = "クラシック" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "メカニカル" +style_windows = "Windows" text_color = "テキストの色" +theme_dark = "ダーク" +theme_light = "ライト" +theme_retro = "レトロ" timeline_unavailable = "プロジェクトのタイムラインを利用できません。" timeline_unreadable = "プロジェクトのタイムラインを読み込めませんでした。" title = "キーボードを表示" +type_keys = "キーを入力" +type_keys_button = "キーを入力…" +type_keys_hint = "文章、台本、文字起こし、または cmd+shift+p のようなショートカットを貼り付けます。ここからキー入力として打ち込まれます。" +type_keys_placeholder = "文章、または cmd+k のようなショートカット" +type_keys_summary = "入力は約 {seconds} 秒" uppercase = "大文字" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "比例" bottom_center = "下中央" bottom_left = "左下" bottom_right = "右下" +center = "中央" manual = "手動" +middle_left = "中央左" +middle_right = "中央右" top_center = "上中央" top_left = "左上" top_right = "右上" @@ -2979,6 +3040,7 @@ title = "タイムライン" [editor.timeline.empty] add_audio = "音声を追加" +add_key = "キー入力を追加" add_scene = "シーンを追加" audio = "BGMを追加するか、自分の音声を読み込みます" captions = "字幕なし" @@ -3117,6 +3179,7 @@ crop = "切り抜き" crop_tooltip = "動画を切り抜く" no_room_for_3d_shot = "別の3Dショットを入れる余地がありません" no_room_for_caption = "ここには字幕を追加する余地がありません" +no_room_for_key = "ここには別のキー入力を置く場所がありません" redo = "やり直す" save_failed = "{path}を保存できませんでした。エディタを開いたまま、もう一度お試しください:{error}" undo = "取り消す" @@ -3231,6 +3294,26 @@ no_segments = "録画にセグメントがありません。先に復元が必 not_found = "動画のパス{path}が見つかりません!" not_studio = "Studio以外の録画は編集できません" +[editor.window.looks] +launch_day = "ローンチデー" +launch_day_description = "鮮やかなグラデーション、きびきびしたズーム、目立つ字幕。" +mac_native = "Macネイティブ" +mac_native_description = "Tahoe上のmacOSウインドウと白いMacのキー。" +midnight = "ミッドナイト" +midnight_description = "深く暗い背景に、くっきりした縁取り。" +minimal = "ミニマル" +minimal_description = "静かなオフホワイトのキャンバスだけ。" +retro_desk = "レトロデスク" +retro_desk_description = "温かみのあるベージュと厚みのあるメカニカルキー。" +saved = "保存したプリセット" +smooth_demo = "スムーズデモ" +smooth_demo_description = "やわらかな雲、穏やかなズーム、落ち着いたカーソル。" +title = "ルック" +tutorial = "チュートリアル" +tutorial_description = "ゆっくり安定したズームと、誰でも追えるキー表示。" +web_product = "Webプロダクト" +web_product_description = "サイトやアプリのデモ向けのブラウザウインドウ。" + [editor.window.presets] apply = "適用" create = "作成" @@ -3445,4 +3528,4 @@ no_resumable_state = "この録画には再開できるアップロード状態 verification_failed = "録画の確認に失敗しました:{status}" [message.cli] -binary_missing = "{path}に同梱のCLIが見つかりません" \ No newline at end of file +binary_missing = "{path}に同梱のCLIが見つかりません" diff --git a/apps/desktop-gpui/locales/zh-CN.toml b/apps/desktop-gpui/locales/zh-CN.toml index 562853b82cb..6e2b74a582f 100644 --- a/apps/desktop-gpui/locales/zh-CN.toml +++ b/apps/desktop-gpui/locales/zh-CN.toml @@ -30,6 +30,7 @@ website = "网站" commit = "提交" [menu.file] +new_blank_project = "新建空白项目" open_prompt = "打开录制" close_window = "关闭窗口" title = "文件" @@ -1065,6 +1066,7 @@ copy_screenshot_failed = "无法复制截图" import_recordings = "导入" import_screenshots = "导入图片" importing = "正在导入" +new_project = "新建" no_matching_recordings = "没有匹配的录制" no_matching_screenshots = "没有匹配的截图" no_recordings = "还没有录制" @@ -1165,6 +1167,7 @@ no_displays = "未找到屏幕" no_matching_displays = "没有匹配的屏幕" no_matching_windows = "没有匹配的窗口" no_windows = "未找到窗口" +solo_title = "仅摄像头或音频" window = "窗口" [recordings_overlay] @@ -1539,6 +1542,8 @@ words_per_minute = "{wpm} 字/分钟" [import] audio_failed = "无法导入音频:{error}" +blank_failed = "无法创建项目:{error}" +blank_name = "未命名" error_title = "导入错误" image_failed = "无法导入图像:{error}" unsupported = "不支持的媒体文件。" @@ -1854,9 +1859,9 @@ done = "完成" import = "导入文件" [editor.audio_only] -add_visuals = "添加视觉效果" body = "此录制没有视频。可将声音导出为 MP3 或 WAV,或添加视觉效果把它变成视频。" export_audio = "导出音频" +make_video = "做成视频" show_visuals = "显示视觉效果" size_large = "大" size_medium = "中" @@ -2310,12 +2315,16 @@ save_failed = "无法保存此渐变。请重试。" save_tooltip = "保存当前渐变" saved = "已保存" still = "静止" +style = "样式" +style_dither = "抖动" +style_smooth = "平滑" [editor.sidebar.animated_gradient.control] contrast = "对比度" curvature = "弯曲度" detail = "细节" direction = "方向" +dither_size = "抖动大小" exposure = "曝光" flow_scale = "流动尺度" flow_strength = "流动强度" @@ -2381,6 +2390,23 @@ to = "结束" [editor.sidebar.image] prompt = "点击选择或拖放图片" +[editor.sidebar.input_sounds] +click_sounds = "点击音效" +deep = "低沉" +deep_description = "声音低沉圆润的线性轴。" +key_sounds = "按键音效" +laptop = "笔记本" +laptop_description = "柔和、键程短的笔记本键盘。" +mechanical = "机械" +mechanical_description = "清脆明亮、段落感强的机械轴。" +mouse = "鼠标" +mouse_description = "按下并松开鼠标按键的声音。" +trackpad = "触控板" +trackpad_description = "笔记本触控板更柔和的点击声。" +typewriter = "打字机" +typewriter_description = "字锤敲击,按回车时托架复位。" +volume = "音量" + [editor.sidebar.layout] background_blur = "背景模糊" custom_position = "自定义屏幕位置(在画布上拖动)" @@ -2498,6 +2524,7 @@ frames = { other = "{rendered} / {count} 帧" } generating_preview = "正在生成预览…" link_unavailable_cursor = "仅导出光标时,只能保存到文件或剪贴板。" link_unavailable_transparent = "透明导出只能保存到文件或剪贴板。" +more_formats = "更多" organization = "组织" preview = "预览" preview_info = "这是从你的视频渲染出的一帧。调整设置即可查看最终导出的画质。" @@ -2545,7 +2572,9 @@ file = "文件" link = "分享链接" [editor.export.done] +copy_file = "复制到剪贴板" copy_link = "复制链接" +file_copied = "已复制" instant_hint = "如果想在停止录制时立即获得链接,下次录制请使用 Instant 模式。" link_copied = "链接已复制" open_file = "打开文件" @@ -2640,14 +2669,16 @@ format = "轨道" microphone = "麦克风" open_folder = "打开文件夹" screen = "屏幕" +separate = "单独的轨道" +separate_description = "屏幕、摄像头和音频分别保存为单独的文件" studio_only = "只有 Studio 模式会录制单独的轨道。" system_audio = "系统音频" video_detail = "MP4 · {width}×{height}" [editor.export.turbo] title = "极速导出" -description = "使用所有 CPU 核心和 GPU,大幅加快导出" -warning = "导出完成前会占用全部 CPU、GPU 以及数倍的内存。其他应用可能会变慢,电脑也可能发热、风扇声变大。" +description = "调用更多电脑性能,大幅加快导出" +warning = "导出完成前,其他应用可能会稍慢一些。" [editor.modal] cancel_export = "取消导出" @@ -2714,6 +2745,8 @@ advanced_shadow = "高级阴影设置" background = "背景" corner_style = "圆角样式" custom_position = "自定义位置(在画布上拖动)" +empty_description = "录制前选择摄像头,即可在这里调整它的形状和位置。" +empty_title = "此录制中没有摄像头" hide = "隐藏摄像头" keep_size_during_zoom = "缩放时保持原始大小" mirror = "镜像摄像头" @@ -2840,12 +2873,16 @@ minimal = "极简" minimal_description = "无背景的低调描边文字。" pop = "跳跃" pop_description = "活泼的大写文字,以鲜艳的强调色弹出。" +soft = "柔和" +soft_description = "与按键相配的圆角炭灰卡片。" [editor.tabs.corner] rounded = "圆角" squircle = "超椭圆" [editor.tabs.cursor] +empty_description = "此录制没有捕获光标,所以暂时没有可调整的内容。" +empty_title = "此录制中没有光标" follow_hover = "跟随悬停高亮" friction = "摩擦" hide_when_idle = "空闲时隐藏" @@ -2871,6 +2908,7 @@ smooth = "顺滑" smooth_description = "柔软、轻盈的移动" [editor.tabs.keyboard] +add_to_timeline = "添加到时间线" animation = "动画" background_color = "背景颜色" background_opacity = "背景不透明度" @@ -2887,18 +2925,38 @@ font_weight = "字重" generate = "生成键盘分段" generating = "正在生成键盘分段..." grouping_threshold = "分组阈值" +key_style = "按键样式" +key_theme = "按键主题" linger_duration = "停留时长" load_failed = "无法加载录制数据:{error}" no_presses = "未找到录制的按键。" position = "位置" regenerate = "重新生成键盘分段" +show_as = "显示方式" show_modifiers = "显示修饰键" show_special_keys = "显示特殊键" +show_typing = "显示输入内容" size_label = "大小" +split_keys = "按键" +split_sentence = "整句" +split_words = "单词" +style_classic = "经典" +style_linux = "Linux" +style_macos = "macOS" +style_mechanical = "机械" +style_windows = "Windows" text_color = "文字颜色" +theme_dark = "深色" +theme_light = "浅色" +theme_retro = "复古" timeline_unavailable = "项目时间线不可用。" timeline_unreadable = "无法读取项目时间线。" title = "显示键盘" +type_keys = "输入按键" +type_keys_button = "输入按键…" +type_keys_hint = "粘贴一句话、脚本或文字稿,或 cmd+shift+p 这样的快捷键。将从这里开始以按键形式打出。" +type_keys_placeholder = "一句话,或 cmd+k 这样的快捷键" +type_keys_summary = "约 {seconds} 秒的输入" uppercase = "大写" [editor.tabs.padding] @@ -2909,7 +2967,10 @@ proportional = "按比例" bottom_center = "底部居中" bottom_left = "左下" bottom_right = "右下" +center = "居中" manual = "手动" +middle_left = "中左" +middle_right = "中右" top_center = "顶部居中" top_left = "左上" top_right = "右上" @@ -2979,6 +3040,7 @@ title = "时间线" [editor.timeline.empty] add_audio = "添加音频" +add_key = "添加按键" add_scene = "添加场景" audio = "添加背景音乐或导入你自己的音频" captions = "没有字幕" @@ -3117,6 +3179,7 @@ crop = "裁剪" crop_tooltip = "裁剪视频" no_room_for_3d_shot = "没有空间再添加 3D 镜头" no_room_for_caption = "这里没有空间再添加字幕" +no_room_for_key = "这里放不下另一个按键" redo = "重做" save_failed = "无法保存 {path}。请保持编辑器打开并重试:{error}" undo = "撤销" @@ -3231,6 +3294,26 @@ no_segments = "录制没有分段。可能需要先恢复。" not_found = "找不到视频路径 {path}!" not_studio = "无法编辑非 Studio 录制" +[editor.window.looks] +launch_day = "发布日" +launch_day_description = "鲜明的渐变、利落的缩放和醒目的字幕。" +mac_native = "原生 Mac" +mac_native_description = "Tahoe 壁纸上的 macOS 窗口,配白色 Mac 键帽。" +midnight = "午夜" +midnight_description = "深邃的暗色背景,边缘清晰。" +minimal = "极简" +minimal_description = "安静的米白画布,别无其他。" +retro_desk = "复古桌面" +retro_desk_description = "温暖的米色和厚实的机械键帽。" +saved = "你的预设" +smooth_demo = "流畅演示" +smooth_demo_description = "柔和的云朵、轻缓的缩放和平稳的光标。" +title = "风格" +tutorial = "教程" +tutorial_description = "缓慢平稳的缩放,按键一目了然。" +web_product = "网页产品" +web_product_description = "适合网站和应用演示的浏览器窗口。" + [editor.window.presets] apply = "应用" create = "创建" @@ -3445,4 +3528,4 @@ no_resumable_state = "此录制没有可继续的上传状态" verification_failed = "录制验证失败:{status}" [message.cli] -binary_missing = "未在 {path} 找到内置的 CLI 程序" \ No newline at end of file +binary_missing = "未在 {path} 找到内置的 CLI 程序" diff --git a/apps/desktop-gpui/src/assets.rs b/apps/desktop-gpui/src/assets.rs index eb091cc85e5..4bc02c91b45 100644 --- a/apps/desktop-gpui/src/assets.rs +++ b/apps/desktop-gpui/src/assets.rs @@ -183,7 +183,6 @@ const ICONS: &[(&str, &[u8])] = assets!("icons": "refresh-cw.svg", "rotate-ccw.svg", "volume-x.svg", - "gem.svg", "sliders-horizontal.svg", "zap.svg", "zoom-in.svg", diff --git a/apps/desktop-gpui/src/audio_project.rs b/apps/desktop-gpui/src/audio_project.rs index e939373198a..d1b502ef5ec 100644 --- a/apps/desktop-gpui/src/audio_project.rs +++ b/apps/desktop-gpui/src/audio_project.rs @@ -5,7 +5,9 @@ use std::{ use cap_export::{ audio::AudioExportFormat, - audio_project::{AudioProjectError, AudioProjectSources, create_audio_only_project}, + audio_project::{ + AudioProjectError, AudioProjectSources, create_audio_only_project, create_blank_project, + }, }; use gpui::{App, Window}; @@ -49,6 +51,43 @@ pub fn open_audio_in_editor(sources: AudioProjectSources, pretty_name: String, c .detach(); } +/// Creates an empty Studio project in the recordings folder, with nothing +/// recorded, and opens it in the editor to build a video from a background, +/// music, keys, text and images. A failure is reported in a dialog. +pub fn open_blank_in_editor(cx: &mut App) { + let pretty_name = tr::import::blank_name().to_string(); + let created = cx.background_executor().spawn(async move { + let recordings_dir = crate::recording::recordings_dir(); + std::fs::create_dir_all(&recordings_dir).map_err(|error| error.to_string())?; + let folder = sanitize_filename::sanitize(&pretty_name); + let mut attempts = 0; + loop { + let name = + cap_utils::ensure_unique_filename(&format!("{folder}.cap"), &recordings_dir)?; + let project_path = recordings_dir.join(name); + match create_blank_project(&project_path, &pretty_name) { + Ok(_) => break Ok(project_path), + Err(AudioProjectError::Exists(_)) if attempts < 8 => attempts += 1, + Err(error) => break Err(error.to_string()), + } + } + }); + cx.spawn(async move |cx| match created.await { + Ok(project_path) => { + tracing::info!(path = %project_path.display(), "created a blank project"); + cx.update(|cx| { + crate::import::refresh_libraries(cx); + app_windows::open_editor(project_path, cx); + }); + } + Err(error) => { + tracing::error!(error, "could not create a blank project"); + crate::import::show_error_dialog(tr::import::blank_failed(&error)); + } + }) + .detach(); +} + /// Asks where to save, then writes the project's edited sound there as MP3, /// or WAV when the chosen name ends in `.wav`. pub fn save_audio_as(project_path: PathBuf, window: &mut Window, cx: &mut App) { diff --git a/apps/desktop-gpui/src/editor_audio_only.rs b/apps/desktop-gpui/src/editor_audio_only.rs index e1b9bb84ff8..dfcdb86029f 100644 --- a/apps/desktop-gpui/src/editor_audio_only.rs +++ b/apps/desktop-gpui/src/editor_audio_only.rs @@ -1,12 +1,15 @@ use std::sync::Arc; -use cap_project::{AudioVisualizerStyle, AudioVisualsConfiguration}; +use cap_project::{ + AnimatedGradientConfig, AudioVisualizerStyle, AudioVisualsConfiguration, BackgroundSource, +}; use cap_rendering::AudioLevels; use gpui::{ AnyElement, Context, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, - StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px, svg, + StatefulInteractiveElement, Styled, Window, div, prelude::FluentBuilder, px, }; +use crate::editor_sidebar::SelectionRing as _; use crate::editor_window::EditorWindow; use crate::i18n::tr; use crate::ui; @@ -71,7 +74,7 @@ impl EditorWindow { } /// `CAP_GPUI_AUTO_AUDIO_VISUALS=bars|wave|circle`: turn visuals on the way - /// Add Visuals does, for the automated export checks. + /// a visualiser tile does, for the automated export checks. pub(crate) fn enable_audio_visuals_for_automation( &mut self, style: &str, @@ -90,6 +93,105 @@ impl EditorWindow { }); } + /// Turns visuals on in `style` in one step. A project still on the plain + /// background a recording starts with gets the animated one, so the + /// first look is already a video worth exporting. + fn show_visuals( + &mut self, + style: AudioVisualizerStyle, + window: &mut Window, + cx: &mut Context, + ) { + let plain = matches!( + self.project.background.source, + BackgroundSource::Color { + value: [255, 255, 255], + alpha: 255 + } + ); + self.edit_project_without_timeline("audio-visuals-style", window, cx, move |project| { + let before = project.audio_visuals.clone(); + if plain { + project.background.source = BackgroundSource::AnimatedGradient { + config: AnimatedGradientConfig::default(), + }; + } + let background = &project.background.source; + let visuals = project.audio_visuals.get_or_insert_with(|| { + Box::new(AudioVisualsConfiguration::for_background(background)) + }); + visuals.enabled = true; + visuals.style = style; + plain || project.audio_visuals != before + }); + if plain { + self.sidebar.source_tab = crate::editor_sidebar::SourceTab::AnimatedGradient; + } + } + + /// One tile per visualiser, each drawing its look in small so the choice + /// is seen before it is made. `selected` rings the one showing. + fn render_visualizer_tiles( + &self, + selected: Option, + tile_height: f32, + cx: &mut Context, + ) -> AnyElement { + let theme = self.theme; + let editor = theme.editor; + div() + .flex() + .flex_row() + .gap(px(8.)) + .w_full() + .children(AudioVisualizerStyle::ALL.iter().map(|&style| { + let chosen = selected == Some(style); + div() + .id(gpui::SharedString::from(format!( + "audio-visualizer-{style:?}" + ))) + .flex_1() + .min_w_0() + .flex() + .flex_col() + .gap(px(8.)) + .p(px(6.)) + .pb(px(10.)) + .rounded(px(12.)) + .map(|this| { + if chosen { + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .cursor_pointer() + .hover(move |this| this.bg(Hsla::from(editor.ctl_hover))) + } + }) + .child(visualizer_preview(style, tile_height)) + .child( + div() + .w_full() + .text_center() + .text_size(px(12.)) + .text_color(Hsla::from(if chosen { + editor.text_1 + } else { + editor.text_2 + })) + .child(style_label(style)), + ) + .on_click(cx.listener(move |this, _, window, cx| { + this.show_visuals(style, window, cx); + })) + })) + .into_any_element() + } + + /// Shown in place of the preview while an audio-only project has no + /// visuals: its sound, a way out as audio, and the visualisers it can + /// become, each one click away. pub(crate) fn render_audio_card(&self, cx: &mut Context) -> AnyElement { let theme = self.theme; let editor = theme.editor; @@ -100,27 +202,26 @@ impl EditorWindow { 0.0 }; let bars = card_bars(self.audio_levels.as_deref(), &self.project, CARD_BARS); - let played = Hsla::from(editor.accent); + let played = Hsla::from(editor.text_1); let unplayed = Hsla::from(editor.text_3); - let waveform = - div() - .h(px(96.)) - .w_full() - .flex() - .flex_row() - .items_center() - .justify_between() - .children(bars.into_iter().enumerate().map(move |(index, level)| { - let position = (index as f32 + 0.5) / CARD_BARS as f32; - div().w(px(3.)).h(px(4. + 88. * level)).rounded_full().bg( - if position <= progress { - played - } else { - unplayed - }, - ) - })); + let waveform = div() + .h(px(88.)) + .w_full() + .flex() + .flex_row() + .items_center() + .justify_between() + .children(bars.into_iter().enumerate().map(move |(index, level)| { + let position = (index as f32 + 0.5) / CARD_BARS as f32; + div().w(px(2.5)).h(px(3. + 82. * level)).rounded_full().bg( + if position <= progress { + played + } else { + unplayed + }, + ) + })); div() .debug_selector(|| "editor-audio-card".into()) @@ -132,62 +233,43 @@ impl EditorWindow { .child( div() .w_full() - .max_w(px(640.)) + .max_w(px(560.)) .flex() .flex_col() - .items_center() - .gap(px(22.)) + .gap(px(28.)) .child( div() .flex() - .flex_col() - .items_center() - .gap(px(6.)) + .flex_row() + .items_end() + .justify_between() + .gap(px(16.)) .child( div() - .size(px(44.)) - .rounded(px(14.)) .flex() - .items_center() - .justify_center() - .bg(Hsla::from(editor.seg_fill(gpui::rgb( - crate::editor_timeline::track_color::AUDIO, - )))) + .flex_col() + .gap(px(4.)) + .min_w_0() .child( - svg() - .path("icons/microphone.svg") - .size(px(20.)) - .text_color(Hsla::from(editor.text_1)), + div() + .text_size(px(17.)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .child(tr::editor::audio_only::title()), + ) + .child( + div() + .text_size(px(13.)) + .text_color(Hsla::from(editor.text_2)) + .child(tr::editor::audio_only::body()), ), ) - .child( - div() - .text_size(px(17.)) - .font_weight(FontWeight::MEDIUM) - .text_color(Hsla::from(editor.text_1)) - .child(tr::editor::audio_only::title()), - ) - .child( - div() - .max_w(px(420.)) - .text_center() - .text_size(px(13.)) - .text_color(Hsla::from(editor.text_2)) - .child(tr::editor::audio_only::body()), - ), - ) - .child(waveform) - .child( - div() - .flex() - .flex_row() - .gap(px(10.)) .child( ui::Button::plain( &theme, "audio-only-export", ui::ButtonVariant::Blue, - ui::ButtonSize::Lg, + ui::ButtonSize::Md, ) .icon("icons/music.svg") .label(tr::editor::audio_only::export_audio()) @@ -196,32 +278,30 @@ impl EditorWindow { this.open_export(window, cx); }, )), - ) - .child( - ui::Button::plain( - &theme, - "audio-only-add-visuals", - ui::ButtonVariant::Outline, - ui::ButtonSize::Lg, - ) - .icon("icons/image.svg") - .label(tr::editor::audio_only::add_visuals()) - .on_click(cx.listener( - |this, _, window, cx| { - this.edit_audio_visuals( - "audio-visuals-on", - window, - cx, - |visuals| visuals.enabled = true, - ); - }, - )), ), + ) + .child(waveform) + .child( + div() + .flex() + .flex_col() + .gap(px(10.)) + .child( + div() + .text_size(px(13.)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .child(tr::editor::audio_only::make_video()), + ) + .child(self.render_visualizer_tiles(None, 64., cx)), ), ) .into_any_element() } + /// The visualiser, at the top of the Background tab so the background it + /// stands on is right below: a switch in the header, the looks as tiles, + /// then its size and colour once it shows. pub(crate) fn render_audio_visuals_card(&self, cx: &mut Context) -> Option { if !self.is_audio_only() { return None; @@ -230,7 +310,6 @@ impl EditorWindow { let editor = theme.editor; let visuals = self.project.audio_visuals.clone().unwrap_or_default(); let enabled = visuals.enabled; - let styles = AudioVisualizerStyle::ALL; let size_index = VISUALIZER_SIZES .iter() .enumerate() @@ -249,16 +328,17 @@ impl EditorWindow { .id(gpui::SharedString::from(format!( "audio-visualizer-color-{color}" ))) - .size(px(24.)) .rounded_full() - .border_2() - .border_color(if selected { - Hsla::from(editor.accent) - } else { - Hsla::from(editor.line) - }) - .bg(fill) + .selection_ring(Hsla::from(editor.card), editor.accent, selected) .cursor_pointer() + .child( + div() + .size(px(20.)) + .rounded_full() + .bg(fill) + .border_1() + .border_color(Hsla::from(editor.line)), + ) .on_click(cx.listener({ let color = color.to_string(); move |this, _, window, cx| { @@ -275,92 +355,143 @@ impl EditorWindow { })) .into_any_element() }); + let swatches: Vec = swatches.collect(); + let tiles = self.render_visualizer_tiles(enabled.then_some(visuals.style), 48., cx); Some( ui::Field::section(&theme, tr::editor::audio_only::visuals()) - .child( - ui::Subfield::plain(&theme, tr::editor::audio_only::show_visuals()).child( - ui::Toggle::plain(&theme, "audio-visuals-enabled", enabled).on_click( - cx.listener(move |this, _, window, cx| { + .value( + ui::Toggle::plain(&theme, "audio-visuals-enabled", enabled) + .label(tr::editor::audio_only::show_visuals()) + .on_click(cx.listener(move |this, _, window, cx| { + if enabled { this.edit_audio_visuals( "audio-visuals-enabled", window, cx, - move |visuals| visuals.enabled = !enabled, + |visuals| visuals.enabled = false, ); - }), - ), - ), + } else { + let style = this + .project + .audio_visuals + .as_ref() + .map_or(AudioVisualizerStyle::Bars, |visuals| visuals.style); + this.show_visuals(style, window, cx); + } + })) + .into_any_element(), ) - .when(enabled, |field| { - field - .child( - ui::SegmentedControl::editor( - &theme, - "audio-visualizer-style", - styles - .iter() - .map(|style| { - ui::SegmentOption::new( - style_label(*style), - *style == visuals.style, - ) + .child( + div() + .flex() + .flex_col() + .gap(px(12.)) + .child(tiles) + .when(enabled, |this| { + this.child( + ui::SegmentedControl::editor( + &theme, + "audio-visualizer-size", + [ + tr::editor::audio_only::size_small(), + tr::editor::audio_only::size_medium(), + tr::editor::audio_only::size_large(), + ] + .into_iter() + .enumerate() + .map(|(index, label)| { + ui::SegmentOption::new(label, index == size_index) }) .collect(), + ) + .stretch() + .on_select(cx.listener( + |this, index: &usize, window, cx| { + let Some(size) = VISUALIZER_SIZES.get(*index).copied() + else { + return; + }; + this.edit_audio_visuals( + "audio-visuals-size", + window, + cx, + move |visuals| visuals.size = size, + ); + }, + )), ) - .stretch() - .on_select(cx.listener( - move |this, index: &usize, window, cx| { - let Some(style) = styles.get(*index).copied() else { - return; - }; - this.edit_audio_visuals( - "audio-visuals-style", - window, - cx, - move |visuals| visuals.style = style, - ); - }, - )), - ) - .child( - ui::SegmentedControl::editor( - &theme, - "audio-visualizer-size", - [ - tr::editor::audio_only::size_small(), - tr::editor::audio_only::size_medium(), - tr::editor::audio_only::size_large(), - ] - .into_iter() - .enumerate() - .map(|(index, label)| { - ui::SegmentOption::new(label, index == size_index) - }) - .collect(), + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(10.)) + .px(px(2.)) + .children(swatches), ) - .stretch() - .on_select(cx.listener( - |this, index: &usize, window, cx| { - let Some(size) = VISUALIZER_SIZES.get(*index).copied() else { - return; - }; - this.edit_audio_visuals( - "audio-visuals-size", - window, - cx, - move |visuals| visuals.size = size, - ); - }, - )), - ) - .child(div().flex().flex_row().gap(px(8.)).children(swatches)) - }) + }), + ) .into_any_element(), ) } } +/// A visualiser's look in small, white over the animated background's own +/// palette, so a tile reads as the video it makes. +fn visualizer_preview(style: AudioVisualizerStyle, height: f32) -> gpui::Div { + let surface = div() + .relative() + .w_full() + .h(px(height)) + .rounded(px(8.)) + .overflow_hidden() + .bg(gpui::linear_gradient( + 135., + gpui::linear_color_stop(gpui::rgb(0x6f9bf5), 0.), + gpui::linear_color_stop(gpui::rgb(0x9a7ff5), 1.), + )); + let ink = gpui::hsla(0., 0., 1., 0.95); + match style { + AudioVisualizerStyle::Bars => surface.flex().items_center().justify_center().child( + div().flex().flex_row().items_center().gap(px(3.)).children( + [0.35, 0.6, 0.9, 0.55, 1.0, 0.7, 0.45, 0.8, 0.5, 0.3] + .into_iter() + .map(move |level: f32| { + div() + .w(px(3.)) + .h(px(4. + (height * 0.55) * level)) + .rounded_full() + .bg(ink) + }), + ), + ), + AudioVisualizerStyle::Wave => surface.children((0..22).map(move |index| { + let t = index as f32 / 21.; + let y = height * 0.5 + (t * std::f32::consts::TAU * 1.5).sin() * height * 0.22; + div() + .absolute() + .left(gpui::relative(0.08 + t * 0.84)) + .top(px(y - 1.5)) + .size(px(3.)) + .rounded_full() + .bg(ink) + })), + AudioVisualizerStyle::Circle => surface.children((0..18).map(move |index| { + let angle = index as f32 / 18. * std::f32::consts::TAU; + let radius = height * (0.26 + 0.05 * ((index * 5 % 7) as f32 / 6.)); + div() + .absolute() + .left(gpui::relative(0.5)) + .ml(px(angle.cos() * radius - 1.5)) + .top(px(height * 0.5 + angle.sin() * radius - 1.5)) + .size(px(3.)) + .rounded_full() + .bg(ink) + })), + } +} + fn peak_slices(waveforms: &[Arc>]) -> Vec<&[f32]> { waveforms.iter().map(|peaks| peaks.as_slice()).collect() } diff --git a/apps/desktop-gpui/src/editor_canvas.rs b/apps/desktop-gpui/src/editor_canvas.rs index 5ba52dca8aa..d5a0e5143dc 100644 --- a/apps/desktop-gpui/src/editor_canvas.rs +++ b/apps/desktop-gpui/src/editor_canvas.rs @@ -46,7 +46,7 @@ use std::rc::Rc; use crate::i18n::tr; use crate::{editor_timeline::TrackKind, editor_window::EditorWindow}; use cap_project::{CameraXPosition, CameraYPosition, OverlayTrackKind, SceneMode, XY}; -use cap_rendering::{CaptionFrameBox, FrameLayout}; +use cap_rendering::FrameLayout; use gpui::{ AnyElement, Bounds, Context, CursorStyle, FontWeight, Hsla, InteractiveElement, IntoElement, MouseButton, MouseDownEvent, MouseMoveEvent, ParentElement, Pixels, Point, SharedString, @@ -481,23 +481,91 @@ pub fn caption_nudge_center( camera_nudge_center(rect, canvas, direction, shift) } +/// The caption font size a corner drag asks for: the box grows about its +/// centre, so the pointer's travel counts twice, and the size scales with the +/// average of the two sides' growth. A box pinned to the left or right margin +/// (`edge_pinned`) grows sideways from that edge only, so there the travel +/// counts once across. `start_px` is the box in canvas pixels. +pub fn caption_resize_size( + start_size: u32, + start_px: (f64, f64), + delta: (f64, f64), + dir: (i8, i8), + edge_pinned: bool, +) -> u32 { + let (w, h) = (start_px.0.max(1.), start_px.1.max(1.)); + let across = if edge_pinned { 1. } else { 2. }; + let grow_w = (w + across * f64::from(dir.0) * delta.0) / w; + let grow_h = (h + 2. * f64::from(dir.1) * delta.1) / h; + let factor = ((grow_w + grow_h) / 2.).max(0.05); + (f64::from(start_size) * factor) + .round() + .clamp(CAPTION_SIZE_RANGE.0, CAPTION_SIZE_RANGE.1) as u32 +} + +/// Switches the caption on screen at `time` to Manual placement, centred on +/// `drawn`: the setting, and the caption's own override when it has a +/// preset one, since the renderer reads the override first. Returns whether +/// anything changed. +pub fn switch_captions_to_manual( + project: &mut cap_project::ProjectConfiguration, + time: f64, + drawn: NormRect, +) -> bool { + let mut changed = false; + if let Some(timeline) = project.timeline.as_mut() { + for segment in timeline + .caption_segments + .iter_mut() + .filter(|segment| segment.start <= time && time < segment.end) + { + if segment + .position_override + .as_deref() + .is_some_and(|position| position != "manual") + { + segment.position_override = Some("manual".into()); + changed = true; + } + } + } + if let Some(captions) = project.captions.as_mut() + && (captions.settings.position != "manual" || changed) + { + captions.settings.position = "manual".into(); + captions.settings.manual_position = Some(XY::new( + (drawn.x + drawn.w / 2.) as f32, + (drawn.y + drawn.h / 2.) as f32, + )); + changed = true; + } + changed +} + +/// The caption size slider's range, which a canvas resize keeps to. +pub const CAPTION_SIZE_RANGE: (f64, f64) = (12., 100.); + /// Where Manual placement centres the captions when no position is stored: /// the bottom-centre spot the renderer falls back to. pub const CAPTION_DEFAULT_CENTER: (f64, f64) = (0.5, 0.85); -/// The caption box to hit-test on the canvas. Only a caption the renderer -/// placed manually gets one, so preset captions never take a click from the -/// elements under them. The size is the renderer's; the position comes from -/// the stored centre so a drag never waits on a frame. +/// The caption box to hit-test on the canvas, in every placement, so the +/// caption can be hovered, moved and resized like a text. The size is the +/// renderer's. A manual caption's position comes from the stored centre so a +/// drag never waits on a frame; a preset caption sits where it is drawn until +/// a drag moves it to Manual. pub fn caption_canvas_rect( layout: &FrameLayout, manual_position: Option>, ) -> Option { - let caption = layout.caption.filter(CaptionFrameBox::is_manual)?; + let caption = layout.caption?; let drawn = norm_rect(caption.bounds, layout); if drawn.w <= 0. || drawn.h <= 0. { return None; } + if !caption.is_manual() { + return Some(drawn); + } let center = manual_position.map_or(CAPTION_DEFAULT_CENTER, |position| { (f64::from(position.x), f64::from(position.y)) }); @@ -580,6 +648,8 @@ pub struct CanvasResize { /// Set when the display pads evenly, which the proportional /// `max_width` / `padding_scale` model cannot express. pub even: Option, + /// The caption font size when a caption resize began. + pub caption_size: u32, } pub struct CanvasDrag { @@ -841,8 +911,8 @@ impl EditorWindow { CanvasSelection::Mask(_) | CanvasSelection::Text(_) | CanvasSelection::Image(_) - | CanvasSelection::Shape(_) => true, - CanvasSelection::Captions => false, + | CanvasSelection::Shape(_) + | CanvasSelection::Captions => true, }; if !resizable { return; @@ -909,6 +979,11 @@ impl EditorWindow { max_width, padding_scale, even, + caption_size: self + .project + .captions + .as_ref() + .map_or(0, |captions| captions.settings.size), }), }); cx.notify(); @@ -966,6 +1041,7 @@ impl EditorWindow { resize.max_width, resize.padding_scale, resize.even, + resize.caption_size, ) }); let size = ( @@ -983,6 +1059,7 @@ impl EditorWindow { max_width, padding_scale, even, + caption_size, )) = resize { match element { @@ -1073,7 +1150,23 @@ impl EditorWindow { self.canvas_overlay_rect = Some(rect); self.write_overlay_rect(element, rect, cx); } - CanvasSelection::Captions => {} + CanvasSelection::Captions => { + // Where the renderer drew this caption, its own + // position override included. + let edge_pinned = self + .frame_layout + .as_ref() + .and_then(|layout| layout.caption) + .is_some_and(|caption| caption.is_side_pinned()); + let next = caption_resize_size( + caption_size, + (start.w * size.0, start.h * size.1), + delta, + (dir_x, dir_y), + edge_pinned, + ); + self.write_caption_size(next, cx); + } } let _ = (window, output_w, output_h); return; @@ -1098,7 +1191,10 @@ impl EditorWindow { }; overlay_drag_center(bounds, size, delta, &targets, shift) } - CanvasSelection::Captions => caption_drag_center(start, size, delta, &targets, shift), + CanvasSelection::Captions => { + self.make_captions_manual(start, cx); + caption_drag_center(start, size, delta, &targets, shift) + } }; self.snap_guides = guides; let optimistic = NormRect { @@ -1237,7 +1333,10 @@ impl EditorWindow { | CanvasSelection::Text(_) | CanvasSelection::Image(_) | CanvasSelection::Shape(_) => overlay_nudge_center(rect, direction, shift), - CanvasSelection::Captions => caption_nudge_center(rect, size, direction, shift), + CanvasSelection::Captions => { + self.make_captions_manual(rect, cx); + caption_nudge_center(rect, size, direction, shift) + } }; let optimistic = NormRect { x: center.x - rect.w / 2., @@ -1294,9 +1393,29 @@ impl EditorWindow { ); } - /// The manual caption centre. The placement setting itself is left alone: - /// the box only exists while the caption on screen is already manual, - /// whether through the setting or its segment's override. + /// Moving a caption that sits at a preset spot switches it to Manual, + /// centred where it is drawn, so the drag carries on from there. The + /// setting changes, and so does the caption's own placement override + /// when it has one, since the renderer reads the override first. + fn make_captions_manual(&mut self, drawn: NormRect, cx: &mut Context) { + let time = self.preview_or_playhead(); + if switch_captions_to_manual(&mut self.project, time, drawn) { + self.project_changed_live(cx); + } + } + + fn write_caption_size(&mut self, size: u32, cx: &mut Context) { + let Some(captions) = self.project.captions.as_mut() else { + return; + }; + if captions.settings.size == size { + return; + } + captions.settings.size = size; + self.project_changed_live(cx); + } + + /// The manual caption centre. fn write_caption_center(&mut self, center: XY, cx: &mut Context) { let Some(captions) = self.project.captions.as_mut() else { return; @@ -1703,9 +1822,9 @@ impl EditorWindow { | CanvasSelection::Text(_) | CanvasSelection::Image(_) | CanvasSelection::Shape(_) => (true, true, None), - CanvasSelection::Captions => (true, false, None), + CanvasSelection::Captions => (true, true, None), }; - let has_handles = element != CanvasSelection::Captions; + let has_handles = resizable; let left = rect.x as f32 * canvas.0; let top = rect.y as f32 * canvas.1; @@ -2655,7 +2774,7 @@ mod tests { fn caption_layout(width: u32, height: u32, bounds: [f32; 4], position: &str) -> FrameLayout { FrameLayout { - caption: Some(CaptionFrameBox::new(bounds, position)), + caption: Some(cap_rendering::CaptionFrameBox::new(bounds, position)), ..layout(width, height, [0., 0., width as f32, height as f32]) } } @@ -2664,10 +2783,10 @@ mod tests { (a - b).abs() < 1e-6 } - /// A preset caption gets no hit box at all, so a click on it reaches the - /// text, mask or screen underneath exactly as before. + /// A caption at a preset spot takes clicks where it is drawn, whatever + /// manual centre is stored, so it can be picked up and moved. #[test] - fn only_a_manual_caption_takes_clicks() { + fn a_preset_caption_takes_clicks_where_it_is_drawn() { let bounds = [700., 870., 1220., 930.]; for preset in [ "top-left", @@ -2678,7 +2797,9 @@ mod tests { "bottom-right", ] { let layout = caption_layout(1920, 1080, bounds, preset); - assert_eq!(caption_canvas_rect(&layout, Some(XY::new(0.5, 0.5))), None); + let rect = caption_canvas_rect(&layout, Some(XY::new(0.5, 0.5))).unwrap(); + assert!(near(rect.x, 700. / 1920.)); + assert!(near(rect.y, 870. / 1080.)); } assert_eq!(caption_canvas_rect(&layout(1920, 1080, bounds), None), None); let manual = caption_layout(1920, 1080, bounds, "manual"); @@ -2713,6 +2834,108 @@ mod tests { )); } + /// Picking up a preset caption makes it Manual where it is drawn, and a + /// caption with its own preset override gets that override switched too, + /// so the one on screen is the one that moves. + #[test] + fn picking_up_a_caption_switches_it_and_its_override_to_manual() { + let drawn = NormRect { + x: 0.4, + y: 0.8, + w: 0.2, + h: 0.1, + }; + let mut project = cap_project::ProjectConfiguration { + captions: Some(cap_project::CaptionsData::default()), + ..Default::default() + }; + project.captions.as_mut().unwrap().settings.position = "bottom-center".into(); + assert!(switch_captions_to_manual(&mut project, 1., drawn)); + let settings = &project.captions.as_ref().unwrap().settings; + assert_eq!(settings.position, "manual"); + let centre = settings.manual_position.unwrap(); + assert!((centre.x - 0.5).abs() < 1e-6 && (centre.y - 0.85).abs() < 1e-6); + // Already manual with nothing overridden: nothing to do. + assert!(!switch_captions_to_manual(&mut project, 1., drawn)); + + let mut timeline = cap_project::TimelineConfiguration { + segments: Vec::new(), + transitions: Vec::new(), + zoom_segments: Vec::new(), + scene_segments: Vec::new(), + mask_segments: Vec::new(), + text_segments: Vec::new(), + caption_segments: Vec::new(), + keyboard_segments: Vec::new(), + audio_segments: Vec::new(), + camera3d_segments: Vec::new(), + style_segments: Vec::new(), + image_segments: Vec::new(), + shape_segments: Vec::new(), + }; + timeline + .caption_segments + .push(cap_project::CaptionTrackSegment { + id: "a".into(), + start: 0., + end: 2., + text: "Hi".into(), + words: Vec::new(), + fade_duration_override: None, + linger_duration_override: None, + position_override: Some("top".into()), + color_override: None, + background_color_override: None, + font_size_override: None, + }); + project.timeline = Some(timeline); + assert!(switch_captions_to_manual(&mut project, 1., drawn)); + let segment = &project.timeline.as_ref().unwrap().caption_segments[0]; + assert_eq!(segment.position_override.as_deref(), Some("manual")); + // A caption outside the playhead keeps its override. + project.timeline.as_mut().unwrap().caption_segments[0].position_override = + Some("top".into()); + assert!(!switch_captions_to_manual(&mut project, 5., drawn)); + assert_eq!( + project.timeline.as_ref().unwrap().caption_segments[0] + .position_override + .as_deref(), + Some("top") + ); + } + + /// A corner drag grows the caption about its centre and keeps to the + /// size slider's range. + #[test] + fn a_caption_corner_drag_scales_its_font_size() { + // Dragging the bottom-right corner out by a quarter of each side + // grows the box by half, as it grows about its centre: 40 becomes 60. + assert_eq!( + caption_resize_size(40, (400., 40.), (100., 10.), (1, 1), false), + 60 + ); + // The top-left corner pulled inward shrinks it. + assert_eq!( + caption_resize_size(40, (400., 40.), (100., 10.), (-1, -1), false), + 20 + ); + assert_eq!( + caption_resize_size(40, (400., 40.), (-400., -40.), (1, 1), false), + 12 + ); + assert_eq!( + caption_resize_size(90, (400., 40.), (400., 40.), (1, 1), false), + 100 + ); + // Pinned to the left margin, the box widens from that edge only: + // its width grows by a quarter, its height (about its centre) by + // half, so 40 becomes 55, where an unpinned box would reach 60. + assert_eq!( + caption_resize_size(40, (400., 40.), (100., 10.), (1, 1), true), + 55 + ); + } + /// No stored centre is the renderer's bottom-centre fallback, and a /// centre near an edge is drawn pushed back inside the frame, in every /// aspect. diff --git a/apps/desktop-gpui/src/editor_clips.rs b/apps/desktop-gpui/src/editor_clips.rs index 43210c1566a..6702de86188 100644 --- a/apps/desktop-gpui/src/editor_clips.rs +++ b/apps/desktop-gpui/src/editor_clips.rs @@ -2498,6 +2498,7 @@ fn ensure_multiple_segments(meta: &mut RecordingMeta) -> Result<&mut MultipleSeg status: Some(StudioRecordingStatus::Complete), cursor_shape_version: 0, audio_only: false, + blank: false, }, }; } @@ -3732,6 +3733,7 @@ mod tests { status: Some(StudioRecordingStatus::Complete), cursor_shape_version: 0, audio_only: false, + blank: false, }, }, )), diff --git a/apps/desktop-gpui/src/editor_edits.rs b/apps/desktop-gpui/src/editor_edits.rs index a9583b1d671..b79fb527286 100644 --- a/apps/desktop-gpui/src/editor_edits.rs +++ b/apps/desktop-gpui/src/editor_edits.rs @@ -821,7 +821,12 @@ impl TrackSegmentOps for KeyboardTrackSegment { fn split_tail(&self, at: f64) -> Self { let mut tail = self.clone(); tail.start = self.start + at; - tail.id = split_id("kb-edit"); + // Half of a key added by hand is still one added by hand. + tail.id = split_id(if cap_project::is_manual_keyboard_segment(self) { + "kb-manual" + } else { + "kb-edit" + }); tail } } @@ -1913,7 +1918,12 @@ pub fn split_segment( let Some((head, mut tail)) = segment.split_at(segment.start + at) else { return false; }; - tail.id = split_id("kb-edit"); + // Half of a key added by hand is still one added by hand. + tail.id = split_id(if cap_project::is_manual_keyboard_segment(&head) { + "kb-manual" + } else { + "kb-edit" + }); timeline.keyboard_segments[index] = head; timeline.keyboard_segments.insert(index + 1, tail); return true; @@ -3041,11 +3051,7 @@ pub fn set_clip_segment_timescale( } pub fn clip_is_muted(segment: &TimelineSegment) -> bool { - if (segment.timescale - 1.0).abs() < f64::EPSILON { - segment.speed_audio_mode == Some(ClipSpeedAudioMode::Mute) - } else { - segment.speed_audio_mode.unwrap_or(ClipSpeedAudioMode::Mute) == ClipSpeedAudioMode::Mute - } + segment.speed_audio_muted() } pub fn set_clip_volume(timeline: &mut TimelineConfiguration, index: usize, volume: f64) -> bool { @@ -4617,8 +4623,21 @@ mod tests { ); assert_eq!(left.keys[0].time_offset, 500.0); assert_eq!(right.keys[0].time_offset, 500.0); + assert!(!cap_project::is_manual_keyboard_segment(right)); assert_ne!(left.id, right.id); assert!(right.id.starts_with("kb-edit-split-")); + + // Half of a key added by hand stays one added by hand, so it keeps + // its sounds and survives regenerating from the recording. + let manual = &mut timeline.keyboard_segments[1]; + manual.id = "kb-manual-0".into(); + manual.display_text = "hi".into(); + manual.keys = cap_project::manual_key_presses("hi"); + manual.keys[1].time_offset = 1500.0; + assert!(split_segment(timeline, TrackKind::Keyboard, 1, 1.0)); + assert!(cap_project::is_manual_keyboard_segment( + &timeline.keyboard_segments[2] + )); } #[test] diff --git a/apps/desktop-gpui/src/editor_export.rs b/apps/desktop-gpui/src/editor_export.rs index 43b8c6dfddf..c4a9355421f 100644 --- a/apps/desktop-gpui/src/editor_export.rs +++ b/apps/desktop-gpui/src/editor_export.rs @@ -34,12 +34,13 @@ use crate::ui; use crate::{library, platform}; const SIDEBAR_WIDTH: f32 = 400.; +const EXPORT_PANEL_INSET: f32 = 12.; const HEADER_HEIGHT: f32 = 52.; const STAGE_PADDING_X: f32 = 24.; /// Stage top padding, caption row, preview padding, stats card and bottom /// padding: everything on the stage that is not the preview itself. const STAGE_VERTICAL_CHROME: f32 = 16. + 22. + 14. + 18. + STATS_HEIGHT + 20.; -const STATS_HEIGHT: f32 = 44.; +const STATS_HEIGHT: f32 = 50.; const SEGMENT_HEIGHT: f32 = 26.; const COMPRESSION_PRESETS: [(ExportCompression, fn() -> &'static str); 4] = [ ( @@ -109,8 +110,6 @@ pub enum ExportFormatKind { } impl ExportFormatKind { - const ALL: [Self; 3] = [Self::Mp4, Self::Gif, Self::Tracks]; - fn label(self) -> &'static str { match self { Self::Mp4 => "MP4", @@ -122,7 +121,7 @@ impl ExportFormatKind { fn options(audio_only: bool, visuals: bool) -> Vec { match (audio_only, visuals) { - (false, _) => Self::ALL.to_vec(), + (false, _) => vec![Self::Mp4, Self::Gif, Self::Tracks], (true, false) => vec![Self::Audio], (true, true) => vec![Self::Mp4, Self::Gif, Self::Audio], } @@ -397,6 +396,8 @@ pub struct ExportUi { pub custom_bpp: Option, pub force_ffmpeg: bool, pub advanced_open: bool, + /// Whether Format's More reveal is open; not saved. + pub more_formats_open: bool, pub preview: Option>, pub preview_stats: Option, pub preview_error: Option, @@ -423,6 +424,8 @@ pub struct ExportUi { pub reuploading: bool, pub upload_progress: f32, pub copy_link_pressed: bool, + /// The finished file was just copied; the button says so for a moment. + pub file_copied: bool, bpp_track: ui::SliderTrack, bpp_dragging: bool, pub track_options: Vec, @@ -485,7 +488,7 @@ impl ExportUi { force_ffmpeg_decoder: false, advanced_open: false, organization_id: None, - turbo: false, + turbo: true, gif_quality: None, excluded_tracks: None, resolution_from_source: true, @@ -516,6 +519,7 @@ impl ExportUi { custom_bpp: prefs.custom_bpp, force_ffmpeg: prefs.force_ffmpeg_decoder, advanced_open: prefs.advanced_open, + more_formats_open: false, preview: None, preview_stats: None, preview_error: None, @@ -539,6 +543,7 @@ impl ExportUi { reuploading: false, upload_progress: 0.0, copy_link_pressed: false, + file_copied: false, bpp_track: ui::SliderTrack::default(), bpp_dragging: false, track_options: Vec::new(), @@ -967,6 +972,38 @@ impl EditorWindow { } } + /// Puts the finished file on the clipboard, the way the Clipboard + /// destination does, and confirms it on the button for two seconds. + fn copy_exported_file(&mut self, window: &mut Window, cx: &mut Context) { + let Some(path) = self.export.as_ref().and_then(|ui| ui.output_path.clone()) else { + return; + }; + let result = platform::copy_file_to_clipboard(&path, cx); + if let Some(ui) = self.export.as_mut() { + match result { + Ok(()) => { + ui.file_copied = true; + ui.error = None; + } + Err(error) => { + tracing::warn!(error, "copying the exported file failed"); + ui.error = Some(error); + } + } + } + cx.notify(); + cx.spawn_in(window, async move |this, cx| { + cx.background_executor().timer(Duration::from_secs(2)).await; + let _ = this.update(cx, |this, cx| { + if let Some(ui) = this.export.as_mut() { + ui.file_copied = false; + } + cx.notify(); + }); + }) + .detach(); + } + fn copy_share_link(&mut self, window: &mut Window, cx: &mut Context) { let Some(link) = self.export.as_ref().and_then(|ui| ui.share_link.clone()) else { return; @@ -1836,7 +1873,7 @@ impl EditorWindow { let options = AudioExportFormat::ALL; let selected = ui.audio_format; let size = selected.estimated_bytes(self.total_duration()) as f64 / (1024.0 * 1024.0); - self.export_section(tr::editor::export::audio::file_type(), "icons/music.svg") + self.export_section(tr::editor::export::audio::file_type()) .child( ui::SegmentedControl::editor( &theme, @@ -2309,8 +2346,6 @@ impl EditorWindow { .items_center() .pl(px(if cfg!(target_os = "macos") { 92. } else { 12. })) .pr(px(12.)) - .border_b_1() - .border_color(Hsla::from(editor.line)) .when(cfg!(target_os = "windows"), |header| { header.window_control_area(gpui::WindowControlArea::Drag) }) @@ -2338,8 +2373,7 @@ impl EditorWindow { .pr(px(10.)) .rounded(px(8.)) .cursor_pointer() - .text_size(px(12.)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(13.)) .text_color(Hsla::from(editor.text_2)) .hover(move |style| { style @@ -2459,7 +2493,14 @@ impl EditorWindow { .rounded(px(10.)) .overflow_hidden() .shadow(crate::theme::preview_shadow()) - .child(img(image).object_fit(gpui::ObjectFit::Contain).size_full()) + // The image paints its own corners; the box's clip alone + // leaves them square. + .child( + img(image) + .object_fit(gpui::ObjectFit::Contain) + .size_full() + .rounded(px(10.)), + ) .into_any_element() } None => div() @@ -2487,7 +2528,7 @@ impl EditorWindow { .flex_col() .flex_1() .min_w_0() - .bg(Hsla::from(editor.stage)) + .bg(Hsla::from(editor.window)) .pt(px(16.)) .px(px(STAGE_PADDING_X)) .pb(px(20.)) @@ -2537,11 +2578,14 @@ impl EditorWindow { div().flex().flex_row().justify_center().child( div() .h(px(STATS_HEIGHT)) - .min_w(px(520.)) .flex() .flex_row() - .rounded(px(10.)) - .bg(Hsla::from(editor.card)) + .items_center() + .px(px(4.)) + .rounded(px(12.)) + .border_1() + .border_color(self.card_line()) + .bg(self.panel_bg()) .shadow(editor.card_shadow()) .child(export_stat( &theme, @@ -2733,7 +2777,7 @@ impl EditorWindow { .flex_col() .flex_1() .min_w_0() - .bg(Hsla::from(editor.stage)) + .bg(Hsla::from(editor.window)) .pt(px(16.)) .px(px(STAGE_PADDING_X)) .pb(px(20.)) @@ -2845,15 +2889,16 @@ impl EditorWindow { let resolutions = self.export_resolutions(ui); let fps_options = self.export_fps_options(ui); - div() - .w(px(SIDEBAR_WIDTH)) - .flex_shrink_0() - .h_full() + let panel = div() + .size_full() .flex() .flex_col() - .bg(Hsla::from(editor.card)) - .border_l_1() - .border_color(Hsla::from(editor.line)) + .rounded(px(crate::theme::EDITOR_CARD_RADIUS)) + .border_1() + .border_color(self.card_line()) + .bg(self.panel_bg()) + .shadow(editor.card_shadow()) + .overflow_hidden() .child( div() .id("export-settings") @@ -2862,8 +2907,10 @@ impl EditorWindow { .flex_1() .min_h_0() .overflow_y_scroll() - .p(px(16.)) - .gap(px(20.)) + .px(px(20.)) + .pt(px(18.)) + .pb(px(20.)) + .gap(px(22.)) .when(!audio_mode, |this| { this.child(self.export_destination_field(ui, link_disabled, cx)) }) @@ -2895,13 +2942,22 @@ impl EditorWindow { ) .child( div() - .px(px(16.)) - .pt(px(12.)) - .pb(px(16.)) + .px(px(20.)) + .pt(px(14.)) + .pb(px(20.)) .border_t_1() .border_color(Hsla::from(editor.line)) .child(self.export_cta(ui, cx)), - ) + ); + // The same raised card as the editor's side panel, inset from the + // window's right and bottom edges. + div() + .w(px(SIDEBAR_WIDTH)) + .flex_shrink_0() + .h_full() + .pr(px(EXPORT_PANEL_INSET)) + .pb(px(EXPORT_PANEL_INSET)) + .child(panel) } fn export_cta(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { @@ -2964,6 +3020,19 @@ impl EditorWindow { ) }; + let gradient = |top: gpui::Rgba, bottom: gpui::Rgba| { + gpui::linear_gradient( + 180., + gpui::linear_color_stop(top, 0.), + gpui::linear_color_stop(bottom, 1.), + ) + }; + let tint = move |amount: f32, toward: u32| { + gradient( + crate::theme::mix(editor.accent_2, gpui::rgb(toward), amount), + crate::theme::mix(editor.accent, gpui::rgb(toward), amount), + ) + }; div() .id("export-cta") .tab_index(0) @@ -2975,34 +3044,72 @@ impl EditorWindow { .justify_center() .gap(px(8.)) .rounded(px(10.)) - .bg(bg) .text_size(px(13.)) .font_weight(FontWeight::MEDIUM) .text_color(text) - .when(primary, |this| { - this.shadow(vec![gpui::BoxShadow { - color: gpui::hsla(0., 0., 1., 0.18), - offset: gpui::point(px(0.), px(1.)), - blur_radius: px(0.), - spread_radius: px(0.), - inset: true, - }]) + // Primary matches the editor's Export button: an accent gradient + // with a lit top edge, lighter on hover and deeper when pressed. + .map(|this| { + if primary { + this.bg(gradient(editor.accent_2, editor.accent)) + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.22), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }]) + } else { + this.bg(bg) + } }) .when(busy, |this| this.opacity(0.5)) .when(!busy, |this| { this.cursor_pointer() - .hover(move |style| style.bg(hover_bg)) + .map(|this| { + if primary { + this.hover(move |style| style.bg(tint(0.10, 0xffffff))) + .active(move |style| style.bg(tint(0.06, 0x000000))) + } else { + this.hover(move |style| style.bg(hover_bg)) + } + }) .on_click(cx.listener(|this, _, window, cx| this.start_export(window, cx))) }) .children(icon.map(|icon| svg().path(icon).size(px(16.)).text_color(text))) .child(label) } - fn export_section(&self, name: &'static str, icon: &'static str) -> ui::Field { - ui::Field::section(&self.theme, name) - .icon(icon) - .icon_size(px(14.)) - .gap(px(8.)) + fn export_section(&self, name: &'static str) -> ui::Field { + ui::Field::section(&self.theme, name).gap(px(10.)) + } + + fn select_export_destination( + &mut self, + dest: ExportDestination, + window: &mut Window, + cx: &mut Context, + ) { + if let Some(ui) = self.export.as_mut() { + if ui.format == ExportFormatKind::Tracks && dest != ExportDestination::File { + return; + } + if dest == ExportDestination::Link + && (has_transparent_background(&self.project) || ui.cursor_only) + { + return; + } + ui.destination = dest; + if dest == ExportDestination::Link && ui.format == ExportFormatKind::Gif { + ui.format = ExportFormatKind::Mp4; + } + } + self.normalize_loaded_export_options(); + if let Some(ui) = self.export.as_mut() { + ui.persist(); + } + self.refresh_export_preview(window, cx); + cx.notify(); } fn export_destination_field( @@ -3013,194 +3120,243 @@ impl EditorWindow { ) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; - self.export_section( - tr::editor::export::section::destination(), - "icons/upload-arrow.svg", - ) - .child( - ui::SegmentedControl::editor( - &theme, - "export-destination", - ExportDestination::ALL - .iter() - .map(|dest| { - let label = if *dest == ExportDestination::Link && self.sharing.is_some() { + self.export_section(tr::editor::export::section::destination()) + .child( + div() + .flex() + .flex_row() + .gap(px(8.)) + .children(ExportDestination::ALL.iter().map(|dest| { + let dest = *dest; + let label = if dest == ExportDestination::Link && self.sharing.is_some() { tr::editor::export::reupload() } else { dest.label() }; - let mut option = ui::SegmentOption::new(label, ui.destination == *dest) - .disabled( - (*dest == ExportDestination::Link && link_disabled) - || (*dest == ExportDestination::Clipboard - && ui.format == ExportFormatKind::Tracks), - ); - option.icon = Some(dest.icon().into()); - option - }) - .collect(), - ) - .stretch() - .item_height(px(30.)) - .on_select(cx.listener(|this, index: &usize, window, cx| { - let Some(dest) = ui::option_at(ExportDestination::ALL, *index) else { - return; - }; - if let Some(ui) = this.export.as_mut() { - if ui.format == ExportFormatKind::Tracks && dest != ExportDestination::File { - return; - } - if dest == ExportDestination::Link - && (has_transparent_background(&this.project) || ui.cursor_only) - { - return; - } - ui.destination = dest; - if dest == ExportDestination::Link && ui.format == ExportFormatKind::Gif { - ui.format = ExportFormatKind::Mp4; - } - } - this.normalize_loaded_export_options(); - if let Some(ui) = this.export.as_mut() { - ui.persist(); - } - this.refresh_export_preview(window, cx); - cx.notify(); - })), - ) - .when(link_disabled, |field| { - field.child( - div() - .text_size(px(11.)) - .text_color(Hsla::from(editor.text_3)) - .child(if ui.format == ExportFormatKind::Tracks { - tr::editor::export::tracks::file_only() - } else if ui.cursor_only { - tr::editor::export::link_unavailable_cursor() - } else { - tr::editor::export::link_unavailable_transparent() - }), + let selected = ui.destination == dest; + let disabled = (dest == ExportDestination::Link && link_disabled) + || (dest == ExportDestination::Clipboard + && ui.format == ExportFormatKind::Tracks); + let ink = Hsla::from(if selected { + editor.text_1 + } else { + editor.text_2 + }); + // A tile per destination, the cursor panel's language: + // room for "Shareable link" on one line under its icon. + div() + .id(gpui::SharedString::from(format!( + "export-destination-{dest:?}" + ))) + .flex_1() + .min_w_0() + .h(px(64.)) + .flex() + .flex_col() + .items_center() + .justify_center() + .gap(px(7.)) + .px(px(6.)) + .rounded(px(12.)) + .text_size(px(12.5)) + .line_height(px(15.)) + .text_color(ink) + .map(|this| { + if selected { + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)).shadow(editor.trough_ring()) + } + }) + .when(disabled, |this| this.opacity(0.4)) + .when(!disabled && !selected, |this| { + this.cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + }) + .when(!disabled, |this| { + this.on_click(cx.listener(move |this, _, window, cx| { + this.select_export_destination(dest, window, cx); + })) + }) + .child( + svg() + .path(dest.icon()) + .size(px(18.)) + .flex_none() + .text_color(ink), + ) + .child(div().max_w_full().truncate().child(label)) + })), ) - }) - .when( - ui.destination == ExportDestination::Link && self.sharing.is_some(), - |field| { - let link = self - .sharing - .as_ref() - .map(|sharing| sharing.link.clone()) - .unwrap_or_default(); + .when(link_disabled, |field| { field.child( div() - .p(px(12.)) - .rounded(px(10.)) - .bg(Hsla::from(editor.card_2)) - .flex() - .flex_col() - .gap(px(4.)) - .child( - div() - .text_size(px(12.5)) - .font_weight(FontWeight::MEDIUM) - .text_color(Hsla::from(editor.text_1)) - .child(tr::editor::export::existing_link_title()), - ) - .child( - div() - .text_size(px(11.5)) - .line_height(px(15.)) - .text_color(Hsla::from(editor.text_3)) - .child(tr::editor::export::existing_link_description()), - ) - .child( - div() - .id("reupload-existing-link") - .text_size(px(11.5)) - .text_color(Hsla::from(editor.accent)) - .truncate() - .cursor_pointer() - .child(link.clone()) - .on_click(move |_, _, cx| cx.open_url(&link)), - ), - ) - }, - ) - .when( - ui.destination == ExportDestination::Link - && self.sharing.is_none() - && store::auth_snapshot().organizations.len() > 1, - |this| { - let orgs = store::auth_snapshot().organizations; - let selected = ui - .organization_id - .clone() - .or_else(|| orgs.first().map(|org| org.id.clone())); - let label = orgs - .iter() - .find(|org| Some(org.id.as_str()) == selected.as_deref()) - .or(orgs.first()) - .map(|org| org.name.clone()) - .unwrap_or_else(|| tr::editor::export::organization().into()); - this.child( - div() - .id("export-organization") - .flex() - .flex_row() - .items_center() - .justify_between() - .h(px(30.)) - .px(px(10.)) - .rounded(px(7.)) - .bg(Hsla::from(editor.ctl)) - .cursor_pointer() - .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) - .on_click(cx.listener(move |this, _, _, cx| { - let orgs = store::auth_snapshot().organizations; - if orgs.is_empty() { - return; - } - if let Some(ui) = this.export.as_mut() { - let current = ui - .organization_id - .as_deref() - .or(orgs.first().map(|org| org.id.as_str())); - let index = orgs - .iter() - .position(|org| Some(org.id.as_str()) == current) - .unwrap_or(0); - let next = orgs[(index + 1) % orgs.len()].id.clone(); - ui.organization_id = Some(next); - ui.persist(); - } - cx.notify(); - })) - .child( - div() - .text_size(px(12.)) - .text_color(Hsla::from(editor.text_2)) - .child(tr::editor::export::organization()), - ) - .child( - div() - .flex() - .flex_row() - .items_center() - .gap(px(4.)) - .text_size(px(12.)) - .text_color(Hsla::from(editor.text_1)) - .child(label) - .child( - svg() - .path("icons/caret-down.svg") - .size(px(14.)) - .text_color(Hsla::from(editor.text_3)), - ), - ), + .text_size(px(11.)) + .text_color(Hsla::from(editor.text_3)) + .child(if ui.format == ExportFormatKind::Tracks { + tr::editor::export::tracks::file_only() + } else if ui.cursor_only { + tr::editor::export::link_unavailable_cursor() + } else { + tr::editor::export::link_unavailable_transparent() + }), ) - }, - ) + }) + .when( + ui.destination == ExportDestination::Link && self.sharing.is_some(), + |field| { + let link = self + .sharing + .as_ref() + .map(|sharing| sharing.link.clone()) + .unwrap_or_default(); + field.child( + div() + .p(px(12.)) + .rounded(px(10.)) + .bg(Hsla::from(editor.card_2)) + .flex() + .flex_col() + .gap(px(4.)) + .child( + div() + .text_size(px(12.5)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(editor.text_1)) + .child(tr::editor::export::existing_link_title()), + ) + .child( + div() + .text_size(px(11.5)) + .line_height(px(15.)) + .text_color(Hsla::from(editor.text_3)) + .child(tr::editor::export::existing_link_description()), + ) + .child( + div() + .id("reupload-existing-link") + .text_size(px(11.5)) + .text_color(Hsla::from(editor.accent)) + .truncate() + .cursor_pointer() + .child(link.clone()) + .on_click(move |_, _, cx| cx.open_url(&link)), + ), + ) + }, + ) + .when( + ui.destination == ExportDestination::Link + && self.sharing.is_none() + && store::auth_snapshot().organizations.len() > 1, + |this| { + let orgs = store::auth_snapshot().organizations; + let selected = ui + .organization_id + .clone() + .or_else(|| orgs.first().map(|org| org.id.clone())); + let label = orgs + .iter() + .find(|org| Some(org.id.as_str()) == selected.as_deref()) + .or(orgs.first()) + .map(|org| org.name.clone()) + .unwrap_or_else(|| tr::editor::export::organization().into()); + this.child( + div() + .id("export-organization") + .flex() + .flex_row() + .items_center() + .justify_between() + .h(px(30.)) + .px(px(10.)) + .rounded(px(7.)) + .bg(Hsla::from(editor.ctl)) + .cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + .on_click(cx.listener(move |this, _, _, cx| { + let orgs = store::auth_snapshot().organizations; + if orgs.is_empty() { + return; + } + if let Some(ui) = this.export.as_mut() { + let current = ui + .organization_id + .as_deref() + .or(orgs.first().map(|org| org.id.as_str())); + let index = orgs + .iter() + .position(|org| Some(org.id.as_str()) == current) + .unwrap_or(0); + let next = orgs[(index + 1) % orgs.len()].id.clone(); + ui.organization_id = Some(next); + ui.persist(); + } + cx.notify(); + })) + .child( + div() + .text_size(px(12.)) + .text_color(Hsla::from(editor.text_2)) + .child(tr::editor::export::organization()), + ) + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(4.)) + .text_size(px(12.)) + .text_color(Hsla::from(editor.text_1)) + .child(label) + .child( + svg() + .path("icons/caret-down.svg") + .size(px(14.)) + .text_color(Hsla::from(editor.text_3)), + ), + ), + ) + }, + ) + } + + fn choose_export_format( + &mut self, + format: ExportFormatKind, + window: &mut Window, + cx: &mut Context, + ) { + if let Some(ui) = self.export.as_mut() { + ui.format = format; + // Tracks and audio are files, and have no cursor-only video. + if !format.writes_video() { + ui.destination = ExportDestination::File; + ui.cursor_only = false; + } + if format != ExportFormatKind::Tracks { + ui.more_formats_open = false; + } + if format == ExportFormatKind::Gif { + if ui.destination == ExportDestination::Link { + ui.destination = ExportDestination::File; + } + if ui.resolution == ExportResolution::P4k { + ui.resolution = ExportResolution::P1080; + } + } + } + self.normalize_loaded_export_options(); + if let Some(ui) = self.export.as_mut() { + ui.persist(); + } + self.refresh_export_preview(window, cx); + cx.notify(); } + /// MP4 and GIF, then More: the less common formats (separate tracks) + /// open as a card underneath, so the everyday choice stays two options. fn export_format_field( &self, ui: &ExportUi, @@ -3213,58 +3369,85 @@ impl EditorWindow { let cursor_only = ui.cursor_only; let no_tracks = ui.track_options.is_empty(); let offers_tracks = options.contains(&ExportFormatKind::Tracks); + let tracks = ui.format == ExportFormatKind::Tracks; + let open = offers_tracks && (tracks || ui.more_formats_open); + // The everyday formats side by side; separate tracks wait under More. + let everyday: Vec = options + .iter() + .copied() + .filter(|format| *format != ExportFormatKind::Tracks) + .collect(); let disabled = move |format: ExportFormatKind| match format { ExportFormatKind::Mp4 => transparent || cursor_only, ExportFormatKind::Gif => cursor_only, ExportFormatKind::Tracks => no_tracks, ExportFormatKind::Audio => false, }; - self.export_section(tr::editor::export::section::format(), "icons/video.svg") - .child( - ui::SegmentedControl::editor( - &theme, - "export-format", - options + let mut more = ui::SegmentOption::new( + if tracks { + ExportFormatKind::Tracks.label() + } else { + tr::editor::export::more_formats() + }, + tracks, + ); + more.icon = Some(if open { + "icons/chevron-up.svg".into() + } else { + "icons/chevron-down.svg".into() + }); + self.export_section(tr::editor::export::section::format()) + .child( + ui::SegmentedControl::editor( + &theme, + "export-format", + everyday .iter() .map(|format| { ui::SegmentOption::new(format.label(), ui.format == *format) .disabled(disabled(*format)) }) + .chain(offers_tracks.then_some(more)) .collect(), ) .stretch() .item_height(px(SEGMENT_HEIGHT)) .on_select(cx.listener(move |this, index: &usize, window, cx| { - let Some(format) = options.get(*index).copied() else { + let Some(format) = everyday.get(*index).copied() else { + if let Some(ui) = this.export.as_mut() + && ui.format != ExportFormatKind::Tracks + { + ui.more_formats_open = !ui.more_formats_open; + cx.notify(); + } return; }; - if disabled(format) { - return; - } - if let Some(ui) = this.export.as_mut() { - ui.format = format; - if !format.writes_video() { - ui.destination = ExportDestination::File; - ui.cursor_only = false; - } - if format == ExportFormatKind::Gif { - if ui.destination == ExportDestination::Link { - ui.destination = ExportDestination::File; - } - if ui.resolution == ExportResolution::P4k { - ui.resolution = ExportResolution::P1080; - } - } - } - this.normalize_loaded_export_options(); - if let Some(ui) = this.export.as_mut() { - ui.persist(); + if !disabled(format) { + this.choose_export_format(format, window, cx); } - this.refresh_export_preview(window, cx); - cx.notify(); })), ) - .when(no_tracks && offers_tracks, |field| { + .when(open, |field| { + field.child(self.export_choice_card( + "export-format-tracks", + "icons/layers.svg", + tr::editor::export::tracks::separate(), + tr::editor::export::tracks::separate_description(), + tracks, + no_tracks, + cx.listener(move |this, _, window, cx| { + let format = if tracks { + ExportFormatKind::Mp4 + } else { + ExportFormatKind::Tracks + }; + if !disabled(format) { + this.choose_export_format(format, window, cx); + } + }), + )) + }) + .when(open && no_tracks, |field| { field.child( div() .text_size(px(11.)) @@ -3274,57 +3457,69 @@ impl EditorWindow { }) } - fn export_tracks_field(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { - let editor = self.theme.editor; - self.export_section(tr::editor::export::section::tracks(), "icons/layers.svg") - .children(ui.track_options.iter().map(|option| { - self.export_track_row(option, ui.tracks.contains(&option.kind), cx) - .into_any_element() - })) - .child( - div() - .text_size(px(11.)) - .line_height(px(14.)) - .text_color(Hsla::from(editor.text_3)) - .child(if ui.tracks.is_empty() { - tr::editor::export::tracks::choose_one() - } else { - tr::editor::export::tracks::aligned() - }), - ) - } - - fn export_track_row( + /// A row-sized choice: an icon tile, a title and a line under it, and a + /// check circle. Selected rows sit on the raised pill with the accent + /// ring; the rest are recessed and fill on hover. + #[allow(clippy::too_many_arguments)] + fn export_choice_card( &self, - option: &TrackOption, - checked: bool, - cx: &mut Context, + id: &'static str, + icon: &'static str, + title: impl Into, + detail: impl Into, + selected: bool, + disabled: bool, + on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static, ) -> impl IntoElement { let editor = self.theme.editor; - let kind = option.kind; + let accent = Hsla::from(editor.accent); div() + .id(id) .flex() .flex_row() .items_center() - .gap(px(10.)) - .min_h(px(36.)) + .gap(px(12.)) + .min_h(px(54.)) + .px(px(12.)) + .py(px(9.)) + .rounded(px(12.)) + .map(|this| { + if selected { + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)).shadow(editor.trough_ring()) + } + }) + .when(disabled, |this| this.opacity(0.4)) + .when(!disabled, |this| { + this.cursor_pointer() + .when(!selected, |this| { + this.hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + }) + .on_click(on_click) + }) .child( div() .flex_none() - .size(px(28.)) - .rounded(px(7.)) - .bg(Hsla::from(editor.ctl)) + .size(px(30.)) + .rounded(px(8.)) .flex() .items_center() .justify_center() + .bg(Hsla::from(if selected { + editor.ctl_active + } else { + editor.ctl + })) .child( svg() - .path(track_icon(kind)) - .size(px(14.)) - .text_color(Hsla::from(if checked { + .path(icon) + .size(px(15.)) + .text_color(Hsla::from(if selected { editor.text_1 } else { - editor.text_3 + editor.text_2 })), ), ) @@ -3334,36 +3529,90 @@ impl EditorWindow { .flex_col() .flex_1() .min_w_0() - .gap(px(1.)) + .gap(px(2.)) .child( div() - .text_size(px(12.5)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(13.)) + .line_height(px(16.)) .text_color(Hsla::from(editor.text_1)) - .child(track_label(kind)), + .truncate() + .child(title.into()), ) .child( div() - .text_size(px(11.)) - .line_height(px(14.)) + .text_size(px(11.5)) + .line_height(px(15.)) .text_color(Hsla::from(editor.text_3)) - .truncate() - .child(track_detail(option)), + .child(detail.into()), ), ) .child( - ui::Toggle::plain(&self.theme, track_toggle_id(kind), checked).on_click( - cx.listener(move |this, _, _, cx| { - if let Some(ui) = this.export.as_mut() { - ui.toggle_track(kind); - ui.persist(); + div() + .flex_none() + .size(px(20.)) + .rounded_full() + .flex() + .items_center() + .justify_center() + .map(|this| { + if selected { + this.bg(accent).child( + svg() + .path("icons/check.svg") + .size(px(12.)) + .text_color(gpui::white()), + ) + } else { + this.border_1().border_color(Hsla::from(editor.line_strong)) } - cx.notify(); }), - ), ) } + fn export_tracks_field(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { + let editor = self.theme.editor; + self.export_section(tr::editor::export::section::tracks()) + .children(ui.track_options.iter().map(|option| { + self.export_track_row(option, ui.tracks.contains(&option.kind), cx) + .into_any_element() + })) + .child( + div() + .text_size(px(11.)) + .line_height(px(14.)) + .text_color(Hsla::from(editor.text_3)) + .child(if ui.tracks.is_empty() { + tr::editor::export::tracks::choose_one() + } else { + tr::editor::export::tracks::aligned() + }), + ) + } + + fn export_track_row( + &self, + option: &TrackOption, + checked: bool, + cx: &mut Context, + ) -> impl IntoElement { + let kind = option.kind; + self.export_choice_card( + track_toggle_id(kind), + track_icon(kind), + track_label(kind), + track_detail(option), + checked, + false, + cx.listener(move |this, _, _, cx| { + if let Some(ui) = this.export.as_mut() { + ui.toggle_track(kind); + ui.persist(); + } + cx.notify(); + }), + ) + } + fn export_resolution_field( &self, ui: &ExportUi, @@ -3372,33 +3621,30 @@ impl EditorWindow { ) -> impl IntoElement { let theme = self.theme; let resolutions = resolutions.to_vec(); - self.export_section( - tr::editor::export::section::resolution(), - "icons/monitor-outline.svg", - ) - .child( - ui::SegmentedControl::editor( - &theme, - "export-resolution", - resolutions - .iter() - .map(|res| ui::SegmentOption::new(res.label(), ui.resolution == *res)) - .collect(), + self.export_section(tr::editor::export::section::resolution()) + .child( + ui::SegmentedControl::editor( + &theme, + "export-resolution", + resolutions + .iter() + .map(|res| ui::SegmentOption::new(res.label(), ui.resolution == *res)) + .collect(), + ) + .stretch() + .item_height(px(SEGMENT_HEIGHT)) + .on_select(cx.listener(move |this, index: &usize, window, cx| { + let Some(resolution) = resolutions.get(*index).copied() else { + return; + }; + if let Some(ui) = this.export.as_mut() { + ui.choose_resolution(resolution); + ui.persist(); + } + this.refresh_export_preview(window, cx); + cx.notify(); + })), ) - .stretch() - .item_height(px(SEGMENT_HEIGHT)) - .on_select(cx.listener(move |this, index: &usize, window, cx| { - let Some(resolution) = resolutions.get(*index).copied() else { - return; - }; - if let Some(ui) = this.export.as_mut() { - ui.choose_resolution(resolution); - ui.persist(); - } - this.refresh_export_preview(window, cx); - cx.notify(); - })), - ) } fn export_fps_field( @@ -3409,7 +3655,7 @@ impl EditorWindow { ) -> impl IntoElement { let theme = self.theme; let options = options.to_vec(); - self.export_section(tr::editor::export::section::frame_rate(), "icons/gauge.svg") + self.export_section(tr::editor::export::section::frame_rate()) .child( ui::SegmentedControl::editor( &theme, @@ -3440,7 +3686,7 @@ impl EditorWindow { fn export_quality_field(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; - self.export_section(tr::editor::export::section::quality(), "icons/gem.svg") + self.export_section(tr::editor::export::section::quality()) .child( ui::SegmentedControl::editor( &theme, @@ -3491,7 +3737,7 @@ impl EditorWindow { fn export_gif_quality_field(&self, ui: &ExportUi, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let editor = theme.editor; - self.export_section(tr::editor::export::section::quality(), "icons/gem.svg") + self.export_section(tr::editor::export::section::quality()) .child( ui::SegmentedControl::editor( &theme, @@ -3625,33 +3871,14 @@ impl EditorWindow { cx.notify(); }), )) + // A quiet note, not an alert: Turbo is on by default. .when(ui.turbo, |this| { this.child( div() - .p(px(12.)) - .rounded(px(10.)) - .bg(Hsla::from(editor.card_2)) - .flex() - .flex_row() - .items_start() - .gap(px(8.)) - .child( - svg() - .path("icons/triangle-alert.svg") - .size(px(14.)) - .flex_shrink_0() - .mt(px(1.)) - .text_color(Hsla::from(editor.text_2)), - ) - .child( - div() - .flex_1() - .min_w_0() - .text_size(px(11.5)) - .line_height(px(15.)) - .text_color(Hsla::from(editor.text_2)) - .child(tr::editor::export::turbo::warning()), - ), + .text_size(px(11.5)) + .line_height(px(15.)) + .text_color(Hsla::from(editor.text_3)) + .child(tr::editor::export::turbo::warning()), ) }) } @@ -3676,14 +3903,11 @@ impl EditorWindow { .px(px(6.)) .rounded(px(8.)) .cursor_pointer() - .text_size(px(12.)) + // Reads as a section title, like the ones above it. + .text_size(px(13.)) .font_weight(FontWeight::MEDIUM) - .text_color(Hsla::from(editor.text_2)) - .hover(move |style| { - style - .bg(Hsla::from(editor.ctl)) - .text_color(Hsla::from(editor.text_1)) - }) + .text_color(Hsla::from(editor.text_1)) + .hover(move |style| style.bg(Hsla::from(editor.ctl))) .on_click(cx.listener(|this, _, _window, cx| { if let Some(ui) = this.export.as_mut() { ui.advanced_open = !ui.advanced_open; @@ -3691,12 +3915,6 @@ impl EditorWindow { } cx.notify(); })) - .child( - svg() - .path("icons/sliders-horizontal.svg") - .size(px(14.)) - .text_color(Hsla::from(editor.text_2)), - ) .child(tr::editor::export::advanced::title()) .child( svg() @@ -3794,14 +4012,7 @@ impl EditorWindow { ) .flex() .row_height(px(28.)) - .track(px(3.), Hsla::from(editor.ctl_active)) - .fill(Hsla::from(editor.accent)) - .thumb( - px(14.), - Hsla::from(editor.thumb), - Some(gpui::hsla(0., 0., 0., 0.12)), - ) - .thumb_shadow() + .editor(&self.theme, px(14.), false) .on_drag_start( cx.listener( |this, event: &MouseDownEvent, _window, cx| { @@ -3953,24 +4164,30 @@ impl EditorWindow { ExportPhase::Idle | ExportPhase::ChoosingFile => "", }; - let mut wash: Hsla = editor.window.into(); - wash.a = 0.94; + // A dark scrim in both themes, so the card reads as raised over the + // page rather than the page fading to white. + let wash = gpui::hsla(0., 0., 0., if theme.is_dark() { 0.55 } else { 0.32 }); - div() - .absolute() - .inset_0() - .occlude() + // Everything sits on one raised card, the popovers' surface. + let card = div() + .w(px(380.)) .flex() .flex_col() .items_center() - .justify_center() - .gap(px(16.)) - .bg(wash) + .gap(px(14.)) + .px(px(28.)) + .pt(px(28.)) + .pb(px(24.)) + .rounded(px(16.)) + .border_1() + .border_color(editor.pop_line()) + .bg(editor.pop_bg()) + .shadow(editor.pop_shadow()) .text_color(Hsla::from(editor.text_1)) .child({ let ring = ui::CircularProgress::new( - px(80.), - px(6.), + px(64.), + px(5.), Hsla::from(editor.ctl_active), Hsla::from(editor.accent), ) @@ -3989,8 +4206,9 @@ impl EditorWindow { .gap(px(4.)) .child( div() - .text_size(px(16.)) + .text_size(px(15.)) .font_weight(FontWeight::MEDIUM) + .text_center() .child(heading), ) .when( @@ -4017,6 +4235,7 @@ impl EditorWindow { this.child( div() .text_size(px(12.)) + .font_features(crate::editor_timeline::tabular_numerals()) .text_color(Hsla::from(editor.text_2)) .child(tr::editor::export::frames( ui.total_frames as usize, @@ -4090,9 +4309,11 @@ impl EditorWindow { .when(ui.format.writes_video(), |this| { this.child( div() - .max_w(px(320.)) + .max_w(px(300.)) + .pt(px(2.)) .text_size(px(12.)) - .text_color(Hsla::from(editor.text_2)) + .line_height(px(17.)) + .text_color(Hsla::from(editor.text_3)) .text_center() .child(tr::editor::export::done::instant_hint()), ) @@ -4101,106 +4322,148 @@ impl EditorWindow { .when( ui.phase == ExportPhase::Done || ui.phase == ExportPhase::Failed, |this| { - this.when(ui.clipboard_retry_path().is_some(), |this| { - this.child( - ui::Button::plain( - &theme, - "export-retry-copy", - ui::ButtonVariant::Gray, - ui::ButtonSize::Md, - ) - .icon("icons/copy.svg") - .label(tr::editor::export::done::retry_copy()) - .on_click(cx.listener( - |this, _, window, cx| { - this.retry_clipboard_copy(window, cx); - }, - )), + // One accent action, a quieter second one beside it, and + // the way back as a plain link underneath. + let button = |id: &'static str, primary: bool| { + ui::Button::plain( + &theme, + id, + if primary { + ui::ButtonVariant::Blue + } else { + ui::ButtonVariant::Gray + }, + ui::ButtonSize::Md, ) - }) - .when( - (ui.phase == ExportPhase::Done - && ui.destination == ExportDestination::File) - || ui.clipboard_retry_path().is_some(), - |this| { - let path = ui.output_path.clone(); + }; + let retry = ui.clipboard_retry_path().is_some(); + let file_done = + ui.phase == ExportPhase::Done && ui.destination == ExportDestination::File; + let tracks = ui.format == ExportFormatKind::Tracks; + let link_done = ui.phase == ExportPhase::Done + && ui.destination == ExportDestination::Link + && ui.share_link.is_some(); + let path = ui.output_path.clone(); + let open_file = move |primary: bool| { + let path = path.clone(); + button("export-open-file", primary) + .icon("icons/folder-open.svg") + .label(if tracks { + tr::editor::export::tracks::open_folder() + } else { + tr::editor::export::done::open_file() + }) + .on_click(move |_, _, cx| { + if let Some(path) = &path { + library::open_with_system(path, cx); + } + }) + }; + let actions = div() + .flex() + .flex_row() + .flex_wrap() + .justify_center() + .gap(px(8.)) + .pt(px(4.)) + .when(retry, |this| { this.child( - ui::Button::plain( - &theme, - "export-open-file", - ui::ButtonVariant::Gray, - ui::ButtonSize::Md, - ) - .label(if ui.format == ExportFormatKind::Tracks { - tr::editor::export::tracks::open_folder() - } else { - tr::editor::export::done::open_file() - }) - .on_click(move |_, _, cx| { - if let Some(path) = &path { - library::open_with_system(path, cx); - } - }), + button("export-retry-copy", true) + .icon("icons/copy.svg") + .label(tr::editor::export::done::retry_copy()) + .on_click(cx.listener(|this, _, window, cx| { + this.retry_clipboard_copy(window, cx); + })), ) - }, - ) - .when( - ui.phase == ExportPhase::Done - && ui.destination == ExportDestination::Link - && ui.share_link.is_some(), - |this| { + .child(open_file(false)) + }) + .when(file_done && !retry, |this| { + let copied = ui.file_copied; + this.child(open_file(true)).when(!tracks, |this| { + this.child( + button("export-copy-file", false) + .icon(if copied { + "icons/check.svg" + } else { + "icons/copy.svg" + }) + .label(if copied { + tr::editor::export::done::file_copied() + } else { + tr::editor::export::done::copy_file() + }) + .on_click(cx.listener(|this, _, window, cx| { + this.copy_exported_file(window, cx); + })), + ) + }) + }) + .when(link_done, |this| { let copied = ui.copy_link_pressed; let open = ui.share_link.clone().unwrap_or_default(); this.child( - ui::Button::plain( - &theme, - "export-copy-link", - ui::ButtonVariant::Gray, - ui::ButtonSize::Md, - ) - .icon(if copied { - "icons/check.svg" - } else { - "icons/copy.svg" - }) - .label(if copied { - tr::editor::export::done::link_copied() - } else { - tr::editor::export::done::copy_link() - }) - .on_click(cx.listener( - |this, _, window, cx| { + button("export-copy-link", true) + .icon(if copied { + "icons/check.svg" + } else { + "icons/copy.svg" + }) + .label(if copied { + tr::editor::export::done::link_copied() + } else { + tr::editor::export::done::copy_link() + }) + .on_click(cx.listener(|this, _, window, cx| { this.copy_share_link(window, cx); - }, - )), + })), ) .child( - ui::Button::plain( - &theme, - "export-open-link", - ui::ButtonVariant::Primary, - ui::ButtonSize::Md, - ) - .icon("icons/link.svg") - .label(tr::editor::export::done::open_link()) - .on_click(move |_, _, cx| { - cx.open_url(&open); - }), + button("export-open-link", false) + .icon("icons/link.svg") + .label(tr::editor::export::done::open_link()) + .on_click(move |_, _, cx| { + cx.open_url(&open); + }), ) - }, - ) - .child( - ui::Button::plain( - &theme, - "export-back-done", - ui::ButtonVariant::Gray, - ui::ButtonSize::Md, + }); + let back = div() + .id("export-back-done") + .flex() + .flex_row() + .items_center() + .gap(px(6.)) + .h(px(28.)) + .px(px(10.)) + .rounded(px(8.)) + .cursor_pointer() + .text_size(px(13.)) + .text_color(Hsla::from(editor.text_2)) + .hover(move |style| { + style + .bg(Hsla::from(editor.ctl)) + .text_color(Hsla::from(editor.text_1)) + }) + .child( + svg() + .path("icons/move-left.svg") + .size(px(14.)) + .text_color(Hsla::from(editor.text_2)), ) - .label(tr::editor::export::back()) - .on_click(cx.listener(|this, _, window, cx| this.close_export(window, cx))), - ) + .child(tr::editor::export::back()) + .on_click(cx.listener(|this, _, window, cx| this.close_export(window, cx))); + this.child(actions).child(back) }, - ) + ); + + div() + .absolute() + .inset_0() + .occlude() + .flex() + .items_center() + .justify_center() + .bg(wash) + .child(card) } } @@ -4364,35 +4627,48 @@ fn export_stat( ) -> impl IntoElement { let editor = theme.editor; div() + .relative() .flex() - .flex_1() - .min_w_0() + .flex_none() .flex_col() .justify_center() - .px(px(16.)) - .gap(px(1.)) + .h_full() + .min_w(px(96.)) + .px(px(18.)) + .gap(px(2.)) + // An inset rule rather than a full-height border, so the card reads + // as one surface. .when(divided, |this| { - this.border_l_1().border_color(Hsla::from(editor.line)) + this.child( + div() + .absolute() + .left_0() + .top(px(12.)) + .bottom(px(12.)) + .w(px(1.)) + .bg(Hsla::from(editor.line)), + ) }) .child( div() - .text_size(px(10.5)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(11.)) + .line_height(px(14.)) .text_color(Hsla::from(editor.text_3)) - .truncate() + .whitespace_nowrap() .child(label), ) .child(match value { Some(value) => div() - .text_size(px(12.5)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(13.)) + .line_height(px(17.)) + .font_features(crate::editor_timeline::tabular_numerals()) .text_color(Hsla::from(editor.text_1)) .whitespace_nowrap() .child(value) .into_any_element(), None => div() .my(px(3.)) - .h(px(12.)) + .h(px(11.)) .w(px(56.)) .rounded(px(4.)) .bg(Hsla::from(editor.ctl_active)) @@ -4411,7 +4687,7 @@ fn export_fps_options(format: ExportFormatKind, cursor_only: bool) -> &'static [ if format == ExportFormatKind::Tracks { &[15, 30, 60] } else if format == ExportFormatKind::Gif && !cursor_only { - &[5, 10, 15, 20, 25, 30] + &[10, 15, 30] } else { &[15, 30, 60] } @@ -4463,7 +4739,18 @@ fn normalize_export_options(ui: &mut ExportUi, transparent: bool) { ui.resolution = source_resolution(ui.source.size, resolutions, largest); } if !fps_options.contains(&ui.fps) { - ui.fps = if ui.format == ExportFormatKind::Gif { + // A GIF rate this version no longer offers keeps the nearest one + // (a saved 25 stays smooth at 30), ties going to the lower rate. A + // rate past what GIFs offer, such as 60 carried over from MP4, takes + // the light default. + let gif_max = fps_options.iter().copied().max().unwrap_or(15); + ui.fps = if ui.format == ExportFormatKind::Gif && ui.fps <= gif_max { + fps_options + .iter() + .copied() + .min_by_key(|option| (option.abs_diff(ui.fps), *option)) + .unwrap_or(15) + } else if ui.format == ExportFormatKind::Gif { 15 } else { 30 @@ -4745,7 +5032,7 @@ mod tests { fn gif_exports_offer_smaller_options_that_other_formats_do_not() { assert_eq!( export_fps_options(ExportFormatKind::Gif, false), - [5, 10, 15, 20, 25, 30] + [10, 15, 30] ); assert_eq!( export_fps_options(ExportFormatKind::Mp4, false), @@ -4883,6 +5170,25 @@ mod tests { assert_eq!(chosen(&ui), (ExportResolution::P1080, 60)); } + /// Turbo is on for someone exporting for the first time; whoever saved + /// export settings before the toggle existed keeps it off, and a choice + /// either way is kept. + #[test] + fn turbo_starts_on_only_for_new_users() { + let saved_before_turbo: ExportPrefs = serde_json::from_str( + r#"{"format":"Mp4","fps":30,"export_to":"file","resolution":"720p","compression":"Maximum","custom_bpp":null}"#, + ) + .unwrap(); + assert!(!saved_before_turbo.turbo); + for chosen in [false, true] { + let mut prefs = saved_before_turbo.clone(); + prefs.turbo = chosen; + let reread: ExportPrefs = + serde_json::from_str(&serde_json::to_string(&prefs).unwrap()).unwrap(); + assert_eq!(reread.turbo, chosen); + } + } + #[test] fn a_remembered_choice_wins_over_the_source() { let saved_before_this_change: ExportPrefs = serde_json::from_str( @@ -4950,9 +5256,9 @@ mod tests { let mut ui = clipboard_export(); ui.format = ExportFormatKind::Gif; ui.resolution = ExportResolution::P480; - ui.fps = 5; + ui.fps = 10; normalize_export_options(&mut ui, false); - assert_eq!((ui.resolution, ui.fps), (ExportResolution::P480, 5)); + assert_eq!((ui.resolution, ui.fps), (ExportResolution::P480, 10)); ui.format = ExportFormatKind::Mp4; normalize_export_options(&mut ui, false); @@ -4970,6 +5276,13 @@ mod tests { ui.fps = 60; normalize_export_options(&mut ui, false); assert_eq!((ui.resolution, ui.fps), (ExportResolution::P1080, 15)); + + // A GIF rate saved by an older version keeps the nearest one offered. + for (saved, kept) in [(25, 30), (20, 15), (5, 10)] { + ui.fps = saved; + normalize_export_options(&mut ui, false); + assert_eq!(ui.fps, kept, "{saved}"); + } } #[test] diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rs index fd9e336ac0f..c14f40f9234 100644 --- a/apps/desktop-gpui/src/editor_panels.rs +++ b/apps/desktop-gpui/src/editor_panels.rs @@ -2790,6 +2790,9 @@ pub enum FieldKey { /// Single-line names and labels. AudioName(usize), KeyboardText(usize), + /// The Keyboard tab's composer: a draft of keys to add, never written + /// from the project. + KeyComposer, /// `` boxes. CaptionStart(usize), CaptionEnd(usize), @@ -2822,7 +2825,10 @@ impl FieldKey { } fn multi_line(self) -> bool { - matches!(self, Self::TextContent(_) | Self::CaptionText(_)) + matches!( + self, + Self::TextContent(_) | Self::CaptionText(_) | Self::KeyComposer + ) } } @@ -2842,11 +2848,15 @@ impl EditorWindow { return; } let input = cx.new(|cx| { - if key.multi_line() { + let mut input = if key.multi_line() { ui::TextInputState::multi_line(window, cx) } else { ui::TextInputState::single_line(window, cx) + }; + if key == FieldKey::KeyComposer { + input.set_placeholder(tr::editor::tabs::keyboard::type_keys_placeholder()); } + input }); self.push_text_subscription(cx.subscribe_in( &input, @@ -2920,6 +2930,10 @@ impl EditorWindow { fn write_field_value(&self, key: FieldKey, out: &mut String) -> Option<()> { use std::fmt::Write as _; + if key == FieldKey::KeyComposer { + return None; + } + // The crop boxes read the open cropper, not the project: their value // is `crop()[props.field]`, i.e. `realBounds` (`Editor.tsx:1182`). if let FieldKey::Crop(field) = key { @@ -3029,6 +3043,25 @@ impl EditorWindow { window: &mut Window, cx: &mut Context, ) { + if key == FieldKey::KeyComposer { + // Enter adds what was typed and clears the box for the next + // entry, keeping it focused; Escape clears it. + let Some(input) = self.field(key).cloned() else { + return; + }; + match event { + // A script can run over lines, so Enter is a new line and + // the panel's Add button adds it; Escape closes the panel. + ui::TextInputEvent::Confirmed => {} + ui::TextInputEvent::Cancelled => { + self.close_key_prompt(cx); + } + // The Add button follows whether there is anything to add. + ui::TextInputEvent::Changed => cx.notify(), + ui::TextInputEvent::Blurred => {} + } + return; + } match event { ui::TextInputEvent::Changed => { self.begin_field_edit(key); @@ -3085,6 +3118,8 @@ impl EditorWindow { }; let text = input.read(cx).text().to_string(); match key { + // The composer adds its keys on Enter only (`on_field_event`). + FieldKey::KeyComposer => {} FieldKey::StyleName(index) | FieldKey::ImageName(index) => { let style = matches!(key, FieldKey::StyleName(_)); self.edit_project("segment-name", window, cx, move |project| { @@ -3191,10 +3226,43 @@ impl EditorWindow { }); } FieldKey::KeyboardText(index) => { + // Typed text a key added by hand now carries may run past its + // end; it grows to fit, up to the next key or the end. + let room = self.project.timeline.as_ref().map(|timeline| { + timeline + .keyboard_segments + .iter() + .enumerate() + .filter(|(other, _)| *other != index) + .map(|(_, other)| other.start) + .filter(|start| { + timeline + .keyboard_segments + .get(index) + .is_some_and(|segment| *start >= segment.start) + }) + .fold(timeline.duration(), f64::min) + }); self.edit_keyboard_segment("keyboard-text", index, window, cx, move |segment| { + // A key added by hand takes a shortcut in words + // (`cmd+shift+p`) and strikes whatever its text says. + let text = if cap_project::is_manual_keyboard_segment(segment) { + cap_project::manual_key_text(&text) + } else { + text + }; if segment.display_text == text { return false; } + if cap_project::is_manual_keyboard_segment(segment) { + segment.keys = cap_project::manual_key_presses(&text); + let needed = cap_project::manual_key_length(&segment.keys); + if let Some(room) = room + && segment.end - segment.start < needed + { + segment.end = (segment.start + needed).min(room).max(segment.end); + } + } segment.display_text = text; true }); @@ -4680,15 +4748,23 @@ impl EditorWindow { .flex_1() .flex() .justify_center() - .py(px(10.)) - .rounded(px(9.6)) + .py(px(7.)) + .rounded(px(8.)) + .font_weight(FontWeight::MEDIUM) .text_color(if selected { - Hsla::from(theme.gray_12) + Hsla::from(theme.editor.text_1) } else { - Hsla::from(theme.gray_11) + Hsla::from(theme.editor.text_2) }) .when(locked, |this| this.opacity(0.5)) - .when(selected, |this| this.bg(Hsla::from(theme.gray_3))) + .when(selected, |this| { + this.bg(theme.editor.pill_fill()) + .shadow(theme.editor.pill_shadow()) + }) + .when(!selected && !locked, |this| { + this.cursor_pointer() + .hover(|style| style.bg(Hsla::from(theme.editor.ctl_hover))) + }) .child(label) .when(!locked, |this| { this.on_click(cx.listener(move |this, _, window, cx| { @@ -4706,10 +4782,10 @@ impl EditorWindow { .flex() .flex_row() .items_center() - .rounded(px(8.)) - .border_1() - .border_color(Hsla::from(theme.gray_12)) - .p(px(1.)) + .rounded(px(10.)) + .bg(Hsla::from(theme.editor.ctl)) + .shadow(theme.editor.trough_ring()) + .p(px(2.)) .child(tab( tr::editor::panel::zoom::auto(), "zoom-mode-auto", @@ -5181,7 +5257,8 @@ impl EditorWindow { .text_size(px(11.)) .font_weight(FontWeight::MEDIUM) .when(selected, |this| { - this.bg(Hsla::from(theme.editor.ctl_active)) + this.bg(theme.editor.pill_fill()) + .shadow(theme.editor.pill_shadow()) .text_color(Hsla::from(theme.editor.text_1)) }) .when(!selected, |this| { @@ -6013,6 +6090,27 @@ impl EditorWindow { &theme, SharedString::from(tr::editor::panel::keyboard::title(index + 1)), ) + // How every key looks lives in the Keyboard tab, one click + // away from any key. + .value( + ui::Button::plain( + &theme, + SharedString::from(format!("keyboard-style-{index}")), + ui::ButtonVariant::Gray, + ui::ButtonSize::Sm, + ) + .icon("icons/keyboard.svg") + .label(tr::editor::tabs::keyboard::key_style()) + .on_click(cx.listener(|this, _, window, cx| { + if let Some(index) = crate::editor_sidebar::SidebarTab::ALL + .iter() + .position(|tab| *tab == crate::editor_sidebar::SidebarTab::Keyboard) + { + this.select_sidebar_tab(index, window, cx); + } + })) + .into_any_element(), + ) .child(self.render_field_input(FieldKey::KeyboardText(index), None)), ) .child(self.timing_field( @@ -8454,7 +8552,8 @@ impl EditorWindow { } } crate::editor_sidebar::SidebarTab::Keyboard => { - colors.extend([ColorTarget::KeyboardColor, ColorTarget::KeyboardBackground]) + colors.extend([ColorTarget::KeyboardColor, ColorTarget::KeyboardBackground]); + fields.push(FieldKey::KeyComposer); } crate::editor_sidebar::SidebarTab::Audio => { if let Some(summary) = self.summary() { diff --git a/apps/desktop-gpui/src/editor_shapes.rs b/apps/desktop-gpui/src/editor_shapes.rs index 032bbef5aab..96ca615b8c9 100644 --- a/apps/desktop-gpui/src/editor_shapes.rs +++ b/apps/desktop-gpui/src/editor_shapes.rs @@ -1862,8 +1862,8 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child(div().flex().flex_col().gap(px(2.)).p(px(6.)).children( diff --git a/apps/desktop-gpui/src/editor_side_panel.rs b/apps/desktop-gpui/src/editor_side_panel.rs index 0adfd524fd9..9bd34fe1a3d 100644 --- a/apps/desktop-gpui/src/editor_side_panel.rs +++ b/apps/desktop-gpui/src/editor_side_panel.rs @@ -11,9 +11,9 @@ //! them and every switch goes through [`EditorWindow::set_panel_tab`]. use gpui::{ - AnyElement, AnyWindowHandle, Context, CursorStyle, Div, FontWeight, Hsla, InteractiveElement, - IntoElement, MouseButton, MouseDownEvent, ParentElement, SharedString, StyleRefinement, Styled, - Window, div, prelude::FluentBuilder, px, svg, + AnyElement, AnyWindowHandle, App, Context, CursorStyle, Div, FontWeight, Hsla, + InteractiveElement, IntoElement, MouseButton, MouseDownEvent, ParentElement, SharedString, + StyleRefinement, Styled, Window, div, prelude::FluentBuilder, px, svg, }; use crate::editor_edits::Selection; @@ -93,6 +93,9 @@ pub(crate) struct PanelState { /// The editor's own window, for the tab switch a selection asks for from /// a path that was handed no window. window: AnyWindowHandle, + /// A tab switch's fade: when it began, and how far down the card the + /// new content starts. + pub(crate) fade: Option<(std::time::Instant, f32)>, } impl PanelState { @@ -108,6 +111,7 @@ impl PanelState { Self { width, resize: None, + fade: None, window, } } @@ -210,34 +214,89 @@ pub(crate) fn panel_empty_state( .gap(px(8.)) .px(px(32.)) .pb(px(SIDEBAR_TAB_BAR_HEIGHT)) + // The icon sits in a small raised tile, the same lit edge and ring + // as the panel's pills. .child( - svg() - .path(icon) - .size(px(22.)) - .mb(px(2.)) - .text_color(Hsla::from(editor.text_3)), + div() + .size(px(48.)) + .mb(px(6.)) + .flex() + .items_center() + .justify_center() + .rounded(px(14.)) + .bg(gpui::linear_gradient( + 180., + gpui::linear_color_stop(Hsla::from(editor.ctl_active), 0.), + gpui::linear_color_stop(Hsla::from(editor.ctl), 1.), + )) + .shadow(editor.pill_shadow()) + .child( + svg() + .path(icon) + .size(px(20.)) + .text_color(Hsla::from(editor.text_2)), + ), ) .child( div() - .text_size(px(13.)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(14.)) + .line_height(px(18.)) .text_color(Hsla::from(editor.text_1)) + .text_center() .child(title.into()), ) .when(!description.is_empty(), |empty| { empty.child( div() - .max_w(px(280.)) - .text_size(px(12.)) + .max_w(px(260.)) + .text_size(px(12.5)) .line_height(px(18.)) .text_center() - .text_color(Hsla::from(editor.text_2)) + .text_color(Hsla::from(editor.text_3)) .child(description), ) }) } +/// How long a tab switch takes to fade its new content in. +const PANEL_FADE: std::time::Duration = std::time::Duration::from_millis(180); + +/// How opaque the fade's cover is `elapsed` into it: fully at the start, +/// clear by the end, easing out so the content arrives quickly and settles. +pub(crate) fn panel_fade_cover(elapsed: std::time::Duration) -> f32 { + let t = (elapsed.as_secs_f32() / PANEL_FADE.as_secs_f32()).clamp(0., 1.); + (1. - t).powi(3) +} + impl EditorWindow { + /// The tab-switch fade: a cover in the card's own colour over the new + /// content, clearing over [`PANEL_FADE`]. The content underneath is the + /// cached view, painted once; only this rectangle repaints per frame. + fn render_panel_fade(&self, window: &Window, cx: &App) -> Option { + let (since, top) = self.panel.fade?; + if cx.reduce_motion() { + return None; + } + let cover = panel_fade_cover(since.elapsed()); + if cover <= 0.001 { + return None; + } + window.request_animation_frame(); + let radius = px(crate::theme::EDITOR_CARD_RADIUS - 1.); + Some( + div() + .absolute() + .left_0() + .right_0() + .bottom_0() + .top(px(top)) + .rounded_bl(radius) + .rounded_br(radius) + .bg(self.panel_bg().opacity(cover)) + .into_any_element(), + ) + } + pub(crate) fn panel_tab(&self) -> PanelTab { PanelTab::from_flags(self.transcript.open, self.clips.open) } @@ -290,6 +349,7 @@ impl EditorWindow { PanelTab::Style => {} } tracing::debug!(from = ?current, to = ?tab, "editor panel tab"); + self.panel.fade = Some((std::time::Instant::now(), SWITCHER_HEIGHT)); cx.notify(); } @@ -474,7 +534,7 @@ impl EditorWindow { .h_full() .min_h_0() .w(px(self.panel_width(viewport_width))) - .rounded(px(12.)) + .rounded(px(crate::theme::EDITOR_CARD_RADIUS)) .border_1() .border_color(if focused { Hsla::from(theme.editor.line_strong) @@ -484,11 +544,13 @@ impl EditorWindow { .bg(self.panel_bg()) .shadow(theme.editor.card_shadow()) .overflow_hidden() + .relative() .child(self.render_panel_switcher(cx)) .child(self.cached_panel_body( tab == PanelTab::Transcript, StyleRefinement::default().w_full().flex_1().min_h_0(), )) + .children(self.render_panel_fade(window, cx)) .into_any_element() } } @@ -497,6 +559,16 @@ impl EditorWindow { mod tests { use super::*; + #[test] + fn a_tab_switch_fade_clears_quickly_and_settles() { + use std::time::Duration; + assert_eq!(panel_fade_cover(Duration::ZERO), 1.); + let early = panel_fade_cover(PANEL_FADE / 4); + assert!(early > 0. && early < 0.5, "{early}"); + assert_eq!(panel_fade_cover(PANEL_FADE), 0.); + assert_eq!(panel_fade_cover(PANEL_FADE * 3), 0.); + } + #[test] fn the_tab_follows_the_mode_flags() { assert_eq!(PanelTab::from_flags(false, false), PanelTab::Style); diff --git a/apps/desktop-gpui/src/editor_sidebar.rs b/apps/desktop-gpui/src/editor_sidebar.rs index 86b1a64a12e..fb99d4cca00 100644 --- a/apps/desktop-gpui/src/editor_sidebar.rs +++ b/apps/desktop-gpui/src/editor_sidebar.rs @@ -38,7 +38,7 @@ use std::{ collections::{BTreeMap, HashMap}, path::{Path, PathBuf}, sync::Arc, - time::Instant, + time::{Duration, Instant}, }; use cap_project::{ @@ -46,10 +46,10 @@ use cap_project::{ DisplayNotch, NotchConfiguration, PaddingMode, ProjectConfiguration, ShadowConfiguration, }; use gpui::{ - AnyElement, Bounds, Context, FontWeight, Hsla, InteractiveElement, IntoElement, MouseDownEvent, - MouseMoveEvent, ParentElement, Pixels, RenderImage, SharedString, StatefulInteractiveElement, - Styled, StyledImage, Window, canvas, div, img, linear_color_stop, linear_gradient, - prelude::FluentBuilder, px, svg, + AnyElement, Bounds, Context, DispatchPhase, FontWeight, Hsla, InteractiveElement, IntoElement, + MouseDownEvent, MouseMoveEvent, ParentElement, Pixels, RenderImage, ScrollWheelEvent, + SharedString, StatefulInteractiveElement, Styled, StyledImage, Window, canvas, div, img, + linear_color_stop, linear_gradient, prelude::FluentBuilder, px, svg, }; use crate::{ @@ -65,6 +65,9 @@ use crate::{ mod animated_gradient; mod cursor; +mod input_sounds; + +pub(crate) use input_sounds::InputSoundSlider; // --------------------------------------------------------------------------- // The catalogue: every constant the background section reads @@ -333,12 +336,12 @@ impl SourceTab { /// `BACKGROUND_SOURCES_ROW_ONE` / `_TWO` (`:236-246`), now one segmented /// row. `None` is not in it: it is the section header's own action. pub const PICKER: [SourceTab; 6] = [ - Self::Desktop, Self::Wallpaper, - Self::Image, - Self::Color, - Self::Gradient, Self::AnimatedGradient, + Self::Gradient, + Self::Color, + Self::Image, + Self::Desktop, ]; pub fn label(self) -> &'static str { @@ -730,6 +733,7 @@ pub enum SliderKey { Keyboard(KeyboardSlider), Panel(PanelSlider, usize), VoiceEq(crate::editor_voice_eq::VoiceEqSlider), + InputSound(InputSoundSlider), } /// Which colour the open `NSColorPanel` is editing. @@ -813,6 +817,13 @@ pub struct SidebarState { /// The scroll body's offset, so a tab switch can put it back to the top /// (`:647-649`). pub scroll: gpui::ScrollHandle, + pub source_scroll: gpui::ScrollHandle, + /// The source whose pill the row last scrolled into view, so the + /// selection is revealed once and the user's own scroll is kept after. + pub source_revealed: std::rc::Rc>>, + /// Which surface the current wheel or trackpad gesture over the source + /// row moves, and when it last moved, so one gesture never moves both. + pub source_wheel: std::rc::Rc>>, /// `KCollapsible open={...}` for Border and MacBook notch (both driven by /// their own toggle) and `ShadowSettings`' own `isOpen` signal. @@ -927,6 +938,9 @@ impl SidebarState { source_tab: initial_source_tab(config), wallpaper_theme: 0, scroll: gpui::ScrollHandle::new(), + source_scroll: gpui::ScrollHandle::new(), + source_revealed: Default::default(), + source_wheel: Default::default(), border_open: CollapsibleState::new( config.background.border.as_ref().is_some_and(|b| b.enabled), ), @@ -1078,6 +1092,13 @@ impl EditorWindow { // `KSelect` menu, and a field's history bracket. self.sidebar.menu = None; self.end_field_edit(cx); + if self.sidebar.tab != tab { + self.panel.fade = Some(( + std::time::Instant::now(), + crate::editor_side_panel::SWITCHER_HEIGHT + + crate::editor_window::SIDEBAR_TAB_BAR_HEIGHT, + )); + } self.sidebar.tab = tab; self.sidebar.scroll.set_offset(gpui::point(px(0.), px(0.))); cx.notify(); @@ -1306,6 +1327,7 @@ impl EditorWindow { SliderKey::Keyboard(slider) => slider.limits(), SliderKey::Panel(slider, index) => self.panel_slider_limits(slider, index), SliderKey::VoiceEq(slider) => slider.limits(), + SliderKey::InputSound(slider) => slider.limits(), } } @@ -1336,6 +1358,7 @@ impl EditorWindow { SliderKey::Keyboard(slider) => slider.read(&project), SliderKey::Panel(slider, index) => self.panel_slider_value(slider, index), SliderKey::VoiceEq(slider) => self.voice_eq_slider_value(slider), + SliderKey::InputSound(slider) => slider.read(&self.project), } } @@ -1370,6 +1393,9 @@ impl EditorWindow { self.apply_panel_slider(slider, index, value, window, cx) } SliderKey::VoiceEq(slider) => self.apply_voice_eq_slider(slider, value, window, cx), + SliderKey::InputSound(slider) => { + self.apply_input_sound_slider(slider, value, window, cx) + } } } @@ -2643,11 +2669,71 @@ impl EditorWindow { /// The sidebar card's inner content width: 416 minus its 1px borders minus the /// panel's `p-4`. Fixed, because the column is `w-104 min-w-104 flex-none`. -const CONTENT_WIDTH: f32 = 416. - 2. - 32.; +const SIDEBAR_INSET: f32 = 20.; +const CONTENT_WIDTH: f32 = 416. - 2. - SIDEBAR_INSET * 2.; /// Six 34px-tall tiles across the pane. -const WALLPAPER_COLUMNS: f32 = 6.; -const WALLPAPER_GAP: f32 = 6.; -const WALLPAPER_TILE_HEIGHT: f32 = 34.; +const SOURCE_FADE_WIDTH: f32 = 28.; + +/// The row offset that centres a pill, so the selection sits with its +/// neighbours on both sides. Item edges are unscrolled positions. +fn source_reveal_offset(view: (f32, f32), item: (f32, f32), max: f32) -> f32 { + let centre = (view.0 + view.1) / 2.; + (centre - (item.0 + item.1) / 2.).clamp(-max.max(0.), 0.) +} + +/// A gap this long between wheel events starts a new gesture. +const WHEEL_GESTURE_GAP: Duration = Duration::from_millis(220); + +/// The surface a gesture over the source row scrolls. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum WheelAxis { + Row, + Panel, +} + +/// Picks the surface for a new gesture over the source row. A trackpad +/// (or any smooth input) goes the way it mostly moves; a notched wheel, a +/// tilt or Shift moves the row while the row can still move that way, and +/// the panel otherwise. `row_can_move` takes a horizontal delta. +fn latch_wheel_axis( + delta: (f32, f32), + smooth: bool, + shift: bool, + row_can_move: impl Fn(f32) -> bool, +) -> Option { + let (dx, dy) = delta; + if dx == 0. && dy == 0. { + return None; + } + if smooth && !shift { + return Some(if dx.abs() > dy.abs() { + WheelAxis::Row + } else { + WheelAxis::Panel + }); + } + let horizontal = if dx != 0. { dx } else { dy }; + Some(if row_can_move(horizontal) { + WheelAxis::Row + } else { + WheelAxis::Panel + }) +} + +/// How far a wheel event moves a surface on `axis`: the row takes the +/// horizontal part (or the vertical one from a plain wheel or Shift), the +/// panel only the vertical part. +fn wheel_step(axis: WheelAxis, delta: (f32, f32)) -> f32 { + match axis { + WheelAxis::Row if delta.0 != 0. => delta.0, + WheelAxis::Row => delta.1, + WheelAxis::Panel => delta.1, + } +} + +const WALLPAPER_COLUMNS: f32 = 4.; +const WALLPAPER_GAP: f32 = 8.; +const WALLPAPER_TILE_HEIGHT: f32 = 50.; /// The gradient preview: `h-28` over the panel's full content width. const GRADIENT_PREVIEW_WIDTH: f32 = CONTENT_WIDTH; const GRADIENT_PREVIEW_HEIGHT: f32 = 112.; @@ -2655,19 +2741,15 @@ const GRADIENT_PREVIEW_HEIGHT: f32 = 112.; impl EditorWindow { pub(crate) fn render_sidebar(&self, cx: &mut Context) -> impl IntoElement { let theme = self.theme; - let summary = self.summary(); let selection = self.sidebar_selection().cloned(); let selected_tab = self.sidebar.tab; - // The two data-driven disabled states (`:602-604`, `:610`). let items = SidebarTab::ALL .into_iter() .map(|tab| { - let disabled = match tab { - SidebarTab::Camera => !summary.is_some_and(|summary| summary.has_camera), - SidebarTab::Cursor => !summary.is_some_and(|summary| summary.has_cursor_data), - _ => false, - }; + // Camera and Cursor stay reachable without their data; the tab + // explains what is missing instead of refusing the click. + let disabled = false; // While a selection panel is up the rail shows no selected // item at all: `KTabs`'s value is forced to `undefined` and // the indicator is hidden (`:586-592, 667-677`). @@ -2713,6 +2795,8 @@ impl EditorWindow { self.render_style_group(index, group, cx) } else if self.audio_picker.is_some() { self.render_audio_library(cx) + } else if self.key_prompt.is_some() { + self.render_key_prompt(cx) } else { match selection { Some(selection) => self.render_selection_panel(&selection, cx), @@ -2760,9 +2844,9 @@ impl EditorWindow { .flex() .flex_col() .gap(px(14.)) - .pt(px(14.)) - .px(px(16.)) - .pb(px(16.)) + .pt(px(18.)) + .px(px(SIDEBAR_INSET)) + .pb(px(20.)) .child(content), ) .into_any_element() @@ -2796,7 +2880,7 @@ impl EditorWindow { div() .flex() .flex_col() - .gap(px(14.)) + .gap(px(20.)) .child(self.render_source_field(cx)) .child(dashed_divider(Hsla::from(theme.editor.line))) .child( @@ -2893,11 +2977,6 @@ impl EditorWindow { let theme = self.theme; let tab = self.sidebar.source_tab; - let options = SourceTab::PICKER - .into_iter() - .map(|item| ui::SegmentOption::new(item.label(), tab == item)) - .collect(); - ui::Field::section(&theme, tr::editor::sidebar::background::title()) .value(section_action( &theme, @@ -2909,18 +2988,7 @@ impl EditorWindow { this.select_source_tab(SourceTab::None, window, cx); }), )) - .child( - ui::SegmentedControl::editor(&theme, "background-source", options) - .stretch() - .item_height(px(26.)) - .item_padding(px(4.), px(0.)) - .text_size(px(11.5)) - .on_select(cx.listener(|this, index: &usize, window, cx| { - if let Some(item) = ui::option_at(&SourceTab::PICKER, *index) { - this.select_source_tab(item, window, cx); - } - })), - ) + .child(self.render_source_tiles(tab, cx)) .child(match tab { SourceTab::Desktop => self.render_desktop_pane(cx).into_any_element(), SourceTab::Wallpaper => self.render_wallpaper_pane(cx).into_any_element(), @@ -2934,6 +3002,220 @@ impl EditorWindow { }) } + fn render_source_tiles(&self, tab: SourceTab, cx: &mut Context) -> impl IntoElement { + let theme = self.theme; + let editor = theme.editor; + let scroll = self.sidebar.source_scroll.clone(); + let revealed = self.sidebar.source_revealed.clone(); + let offset = f32::from(scroll.offset().x); + let max = f32::from(scroll.max_offset().x); + let fade_left = offset < -1.; + let fade_right = max > 1. && offset > -(max - 1.); + let panel = self.panel_bg(); + let fade = |angle: f32| { + div() + .absolute() + .top_0() + .bottom_0() + .w(px(SOURCE_FADE_WIDTH)) + .bg(gpui::linear_gradient( + angle, + gpui::linear_color_stop(panel, 0.), + gpui::linear_color_stop(panel.opacity(0.), 1.), + )) + }; + + div() + .relative() + .child( + div() + .when(revealed.get() != Some(tab), |this| { + let scroll = scroll.clone(); + let revealed = revealed.clone(); + let index = SourceTab::PICKER.iter().position(|item| *item == tab); + let editor = cx.entity().downgrade(); + this.on_children_prepainted(move |_, _, cx| { + revealed.set(Some(tab)); + // A scrolling row keeps its children's bounds on the handle; + // the listener's own list is empty. + let Some(item) = index.and_then(|index| scroll.bounds_for_item(index)) + else { + return; + }; + let view = scroll.bounds(); + let offset = scroll.offset(); + let next = source_reveal_offset( + (f32::from(view.left()), f32::from(view.right())), + (f32::from(item.left()), f32::from(item.right())), + f32::from(scroll.max_offset().x), + ); + if (next - f32::from(offset.x)).abs() > 0.5 { + scroll.set_offset(gpui::point(px(next), offset.y)); + // The sidebar view is cached, so it repaints + // the new offset and edge fades only when told. + editor + .update(cx, |this, cx| this.invalidate_sidebar(cx)) + .ok(); + } + }) + }) + .id("background-sources") + .flex() + .flex_row() + .gap(px(6.)) + .overflow_x_scroll() + .track_scroll(&scroll) + .children(SourceTab::PICKER.into_iter().map(|item| { + let selected = tab == item; + let ink = Hsla::from(if selected { + editor.text_1 + } else { + editor.text_2 + }); + let glyph = match item { + SourceTab::Gradient => div() + .size(px(13.)) + .rounded(px(3.)) + .bg(gpui::linear_gradient( + 135., + gpui::linear_color_stop(ink, 0.), + gpui::linear_color_stop(ink.opacity(0.15), 1.), + )) + .into_any_element(), + _ => svg() + .path(match item { + SourceTab::Desktop => "icons/monitor-outline.svg", + SourceTab::Wallpaper => "icons/image.svg", + SourceTab::Image => "icons/folder-open.svg", + SourceTab::Color => "icons/palette.svg", + _ => "icons/square-play.svg", + }) + .size(px(14.)) + .text_color(ink) + .into_any_element(), + }; + div() + .id(SharedString::from(format!("background-source-{item:?}"))) + .flex() + .flex_none() + .flex_row() + .items_center() + .gap(px(7.)) + .h(px(32.)) + .px(px(13.)) + .rounded(px(10.)) + .text_size(px(13.)) + .text_color(ink) + .map(|this| { + if selected { + this.bg(editor.pill_fill()).shadow(editor.pill_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .cursor_pointer() + .hover(|this| this.bg(Hsla::from(editor.ctl_hover))) + } + }) + .child(glyph) + .child(item.label()) + .on_click(cx.listener(move |this, _, window, cx| { + this.select_source_tab(item, window, cx); + })) + })), + ) + .when(fade_left, |this| this.child(fade(90.).left_0())) + .when(fade_right, |this| this.child(fade(270.).right_0())) + .child(self.source_wheel_owner(cx)) + } + + /// Owns wheel and trackpad input over the source row before either + /// scroll area sees it, so a gesture moves the row or the panel and + /// never both. Its hitbox is transparent to clicks and hovers. + fn source_wheel_owner(&self, cx: &mut Context) -> impl IntoElement { + let row = self.sidebar.source_scroll.clone(); + let panel = self.sidebar.scroll.clone(); + let latch = self.sidebar.source_wheel.clone(); + let editor = cx.entity().downgrade(); + gpui::canvas( + |bounds, window, _| window.insert_hitbox(bounds, gpui::HitboxBehavior::Normal), + move |_, hitbox, window, _| { + window.on_mouse_event(move |event: &ScrollWheelEvent, phase, window, cx| { + if phase != DispatchPhase::Capture || !hitbox.is_hovered(window) { + return; + } + let (delta, smooth) = match event.delta { + gpui::ScrollDelta::Pixels(delta) => { + ((f32::from(delta.x), f32::from(delta.y)), true) + } + gpui::ScrollDelta::Lines(lines) => { + let line = f32::from(window.line_height()); + // Precision touchpads on Windows report fractions + // of a line; a notched wheel reports whole lines. + let smooth = lines.x.fract() != 0. || lines.y.fract() != 0.; + ((lines.x * line, lines.y * line), smooth) + } + }; + let now = Instant::now(); + let row_offset = row.offset(); + let row_max = f32::from(row.max_offset().x); + let held = latch.get().filter(|(_, at)| { + event.touch_phase != gpui::TouchPhase::Started + && now.duration_since(*at) < WHEEL_GESTURE_GAP + }); + // A notched wheel that ran the row to its end hands the + // rest of its spin to the panel, rather than stalling + // there until the wheel pauses. + let x = f32::from(row_offset.x); + let row_can_move = |dx: f32| (dx > 0. && x < 0.) || (dx < 0. && x > -row_max); + let held = held.filter(|(axis, _)| { + smooth + || *axis != WheelAxis::Row + || row_can_move(wheel_step(WheelAxis::Row, delta)) + }); + let axis = match held { + Some((axis, _)) => axis, + None => { + let Some(axis) = latch_wheel_axis( + delta, + smooth, + event.modifiers.shift, + row_can_move, + ) else { + return; + }; + axis + } + }; + latch.set(Some((axis, now))); + let step = px(wheel_step(axis, delta)); + let handle = match axis { + WheelAxis::Row => &row, + WheelAxis::Panel => &panel, + }; + let offset = handle.offset(); + let max = handle.max_offset(); + let next = match axis { + WheelAxis::Row => { + gpui::point((offset.x + step).clamp(-max.x, px(0.)), offset.y) + } + WheelAxis::Panel => { + gpui::point(offset.x, (offset.y + step).clamp(-max.y, px(0.))) + } + }; + cx.stop_propagation(); + if next != offset { + handle.set_offset(next); + editor + .update(cx, |this, cx| this.invalidate_sidebar(cx)) + .ok(); + } + }); + }, + ) + .absolute() + .inset_0() + } + /// The decoded preview for a path, if it is the one already decoded. fn tile_image(&self, path: &str) -> Option> { self.sidebar @@ -3086,9 +3368,9 @@ impl EditorWindow { .flex() .flex_row() .items_center() - .gap(px(4.)) - .mb(px(10.)) - .text_size(px(12.)) + .gap(px(16.)) + .mb(px(12.)) + .text_size(px(12.5)) .overflow_x_scroll() .children( BACKGROUND_THEMES @@ -3099,21 +3381,18 @@ impl EditorWindow { div() .id(SharedString::from(format!("wallpaper-theme-{index}"))) .flex() - .flex_1() - .justify_center() + .flex_none() .items_center() - .h(px(24.)) - .px(px(8.)) - .rounded(px(6.)) - .font_weight(FontWeight::MEDIUM) + .h(px(22.)) .when(selected, |this| { - this.bg(Hsla::from(theme.editor.ctl_hover)) - .text_color(Hsla::from(theme.editor.text_1)) + this.text_color(Hsla::from(theme.editor.text_1)) }) .when(!selected, |this| { - this.text_color(Hsla::from(theme.editor.text_2)) + this.text_color(Hsla::from(theme.editor.text_3)) .cursor_pointer() - .hover(|this| this.bg(Hsla::from(theme.editor.ctl))) + .hover(|this| { + this.text_color(Hsla::from(theme.editor.text_2)) + }) }) .child(background_theme_label(id)) .on_click(cx.listener(move |this, _, window, cx| { @@ -3137,13 +3416,19 @@ impl EditorWindow { .id(SharedString::from(format!("wallpaper-{id}"))) .w(px(cell)) .h(px(WALLPAPER_TILE_HEIGHT + 2. * (RING_WIDTH + RING_GAP))) - .rounded(px(8.)) + .rounded(px(12.)) .cursor_pointer() .selection_ring(self.panel_bg(), theme.editor.accent, selected) + .when(!selected, |this| { + this.hover(|this| { + this.border_color(Hsla::from(theme.editor.line_strong)) + }) + }) .child( div() + .relative() .size_full() - .rounded(px(6.)) + .rounded(px(8.)) .overflow_hidden() .bg(Hsla::from(theme.editor.ctl_hover)) .children(image.map(|image| { @@ -3153,8 +3438,15 @@ impl EditorWindow { img(image) .size_full() .object_fit(gpui::ObjectFit::Cover) - .rounded(px(6.)) - })), + .rounded(px(8.)) + })) + .child( + div() + .absolute() + .inset_0() + .rounded(px(8.)) + .shadow(theme.editor.trough_ring()), + ), ) .on_click(cx.listener(move |this, _, window, cx| { let Some(path) = wallpaper_path(id) else { @@ -3956,11 +4248,19 @@ impl EditorWindow { disabled: bool, cx: &mut Context, ) -> ui::Field { + let theme = self.theme; let value = format_slider_value(self.slider_value(slider), unit); - ui::Field::inline(&self.theme, name) + ui::Field::stacked(&theme, name) .disabled(disabled) - .value_text(value) - .child(self.slider_sized_state(slider, unit, true, disabled, cx)) + .value( + div() + .text_size(px(12.)) + .font_features(crate::editor_timeline::tabular_numerals()) + .text_color(Hsla::from(theme.editor.text_2)) + .child(value) + .into_any_element(), + ) + .child(self.slider_sized_state(slider, unit, false, disabled, cx)) } fn slider_sized( @@ -3995,8 +4295,9 @@ impl EditorWindow { div() .id(SharedString::from(format!("bg-slider-row-{slider:?}"))) // The Kobalte root is `relative px-1 h-8 flex items-center`. - .px(px(4.)) - .h(px(32.)) + .px(px(8.)) + .h(px(22.)) + .when(!flex, |this| this.mb(px(10.))) .flex() .flex_row() .items_center() @@ -4009,19 +4310,8 @@ impl EditorWindow { track, ) .flex() - .row_height(px(28.)) - .track(px(3.), Hsla::from(theme.editor.ctl_active)) - .fill(Hsla::from(if disabled { - theme.editor.text_3 - } else { - theme.editor.accent - })) - .thumb( - px(14.), - Hsla::from(theme.editor.thumb), - Some(gpui::hsla(0., 0., 0., 0.12)), - ) - .thumb_shadow() + .row_height(px(22.)) + .editor(&theme, px(18.), disabled) .when(!disabled, |this| { this.on_drag_start(cx.listener( move |this, event: &MouseDownEvent, window, cx| { @@ -4368,6 +4658,55 @@ mod tests { assert!(!ColorTarget::AnimatedGradientStop(0).is_hex_string()); } + #[test] + fn revealing_a_source_pill_centres_it_within_the_row() { + let view = (0., 300.); + // A pill in the middle of the content scrolls to the row's centre. + assert_eq!(source_reveal_offset(view, (250., 330.), 200.), -140.); + // The first pill rests against the start. + assert_eq!(source_reveal_offset(view, (0., 80.), 200.), 0.); + // The last pill can only go as far as the row scrolls. + assert_eq!(source_reveal_offset(view, (420., 500.), 200.), -200.); + } + + #[test] + fn a_trackpad_gesture_moves_the_way_it_mostly_goes() { + let free = |_: f32| true; + assert_eq!( + latch_wheel_axis((-8., 2.), true, false, free), + Some(WheelAxis::Row) + ); + assert_eq!( + latch_wheel_axis((2., -8.), true, false, free), + Some(WheelAxis::Panel) + ); + assert_eq!(latch_wheel_axis((0., 0.), true, false, free), None); + } + + #[test] + fn a_notched_wheel_moves_the_row_until_it_runs_out() { + assert_eq!( + latch_wheel_axis((0., -40.), false, false, |_| true), + Some(WheelAxis::Row) + ); + assert_eq!( + latch_wheel_axis((0., -40.), false, false, |_| false), + Some(WheelAxis::Panel) + ); + // Shift turns a smooth vertical scroll sideways too. + assert_eq!( + latch_wheel_axis((0., -6.), true, true, |_| true), + Some(WheelAxis::Row) + ); + } + + #[test] + fn each_surface_takes_only_its_own_part_of_a_step() { + assert_eq!(wheel_step(WheelAxis::Row, (-12., 3.)), -12.); + assert_eq!(wheel_step(WheelAxis::Row, (0., -40.)), -40.); + assert_eq!(wheel_step(WheelAxis::Panel, (-12., 3.)), 3.); + } + #[test] fn built_in_wallpapers_remain_available_from_the_development_checkout() { assert!(wallpaper_path("macOS/sequoia-dark").is_some()); diff --git a/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs b/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs index 5f6662ddd9b..d59e7eb61ce 100644 --- a/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs +++ b/apps/desktop-gpui/src/editor_sidebar/animated_gradient.rs @@ -1,6 +1,6 @@ use cap_project::{ AnimatedGradientCatalog, AnimatedGradientConfig, AnimatedGradientControl, - AnimatedGradientLibrary, AnimatedGradientPreset, AnimatedGradientStop, + AnimatedGradientLibrary, AnimatedGradientPreset, AnimatedGradientStop, AnimatedGradientStyle, animated_gradient_catalog, }; use gpui::{AppContext as _, MouseButton}; @@ -155,6 +155,9 @@ fn control_label(control: &AnimatedGradientControl) -> SharedString { AnimatedGradientParameter::MotionSpeed => { tr::editor::sidebar::animated_gradient::control::motion_speed() } + AnimatedGradientParameter::DitherSize => { + tr::editor::sidebar::animated_gradient::control::dither_size() + } }) } @@ -177,10 +180,25 @@ fn group_label(name: &str) -> SharedString { } } -fn control_groups(catalog: &AnimatedGradientCatalog) -> Vec<(&str, Vec<&AnimatedGradientControl>)> { +/// The fine-tune groups for a gradient drawn in `style`: a dither draws only +/// its palette, so it has no lighting or grain to tune, but it has a cell +/// size; a smooth wash is the other way round. +fn control_groups( + catalog: &AnimatedGradientCatalog, + style: AnimatedGradientStyle, +) -> Vec<(&str, Vec<&AnimatedGradientControl>)> { let mut groups: Vec<(&str, Vec<&AnimatedGradientControl>)> = Vec::new(); for control in &catalog.controls { - if control.key == AnimatedGradientParameter::MotionSpeed { + let dither = style == AnimatedGradientStyle::Dither; + let shown = match control.key { + AnimatedGradientParameter::MotionSpeed => false, + AnimatedGradientParameter::DitherSize => dither, + AnimatedGradientParameter::GrainAmount | AnimatedGradientParameter::GrainSize => { + !dither + } + _ => !(dither && control.group == "Lighting"), + }; + if !shown { continue; } match groups.iter_mut().find(|(name, _)| *name == control.group) { @@ -814,6 +832,7 @@ impl EditorWindow { .text_color(Hsla::from(theme.red_11)) .child(error) })) + .child(self.render_animated_gradient_style(config, cx)) .child(self.render_animated_gradient_presets(cx)) .child(dashed_divider(Hsla::from(theme.editor.line))) .child(self.render_animated_gradient_colours(config, cx)) @@ -824,6 +843,65 @@ impl EditorWindow { .into_any_element() } + /// Dither or smooth: the first choice, since it changes the whole look. + fn render_animated_gradient_style( + &self, + config: &AnimatedGradientConfig, + cx: &mut Context, + ) -> AnyElement { + let theme = self.theme; + let styles = [AnimatedGradientStyle::Dither, AnimatedGradientStyle::Smooth]; + ui::Field::inline(&theme, tr::editor::sidebar::animated_gradient::style()) + .value( + ui::SegmentedControl::editor( + &theme, + "animated-gradient-style", + styles + .iter() + .map(|style| { + ui::SegmentOption::new( + match style { + AnimatedGradientStyle::Dither => { + tr::editor::sidebar::animated_gradient::style_dither() + } + AnimatedGradientStyle::Smooth => { + tr::editor::sidebar::animated_gradient::style_smooth() + } + }, + *style == config.style, + ) + }) + .collect(), + ) + .on_select(cx.listener(move |this, index: &usize, window, cx| { + let Some(&style) = styles.get(*index) else { + return; + }; + this.edit_animated_gradient( + |config| { + let changed = config.style != style; + config.style = style; + changed + }, + window, + cx, + ); + })) + .into_any_element(), + ) + .into_any_element() + } + + /// A template or a shuffle keeps the style already chosen; only a saved + /// gradient brings its own. + fn with_current_style(&self, mut config: AnimatedGradientConfig) -> AnimatedGradientConfig { + if let Some(current) = self.animated_gradient_config() { + config.style = current.style; + config.dither_size = current.dither_size; + } + config + } + fn render_animated_gradient_presets(&self, cx: &mut Context) -> AnyElement { let theme = self.theme; let state = &self.sidebar.animated_gradient; @@ -855,7 +933,8 @@ impl EditorWindow { .text_color(Hsla::from(theme.editor.text_3)), ) .on_click(cx.listener(|this, _, window, cx| { - this.select_animated_gradient(AnimatedGradientConfig::random(), window, cx); + let config = this.with_current_style(AnimatedGradientConfig::random()); + this.select_animated_gradient(config, window, cx); })); let mut swatches = vec![shuffle.into_any_element()]; @@ -979,8 +1058,12 @@ impl EditorWindow { cx: &mut Context, ) -> gpui::Div { let theme = self.theme; - let selected = self.animated_gradient_config() == Some(&preset.config); - let config = preset.config.clone(); + let config = if saved { + preset.config.clone() + } else { + self.with_current_style(preset.config.clone()) + }; + let selected = self.animated_gradient_config() == Some(&config); let name = SharedString::from(preset.name.clone()); let id = preset.id.clone(); let swatch = div() @@ -1222,7 +1305,7 @@ impl EditorWindow { let theme = self.theme; let state = &self.sidebar.animated_gradient; let open = state.fine_tune_open.is_open(); - let groups = control_groups(&state.catalog); + let groups = control_groups(&state.catalog, config.style); let active = state.fine_tune_group.min(groups.len().saturating_sub(1)); let tabs = ui::SegmentedControl::icons( @@ -1483,8 +1566,23 @@ mod tests { #[test] fn fine_tune_groups_exclude_motion() { let catalog = animated_gradient_catalog(); - let groups = control_groups(&catalog); + let groups = control_groups(&catalog, AnimatedGradientStyle::Smooth); assert_eq!(groups.len(), 4); + // A dither has a cell size and no lighting or grain to tune. + let dither = control_groups(&catalog, AnimatedGradientStyle::Dither); + let keys: Vec<_> = dither + .iter() + .flat_map(|(_, controls)| controls.iter().map(|control| control.key)) + .collect(); + assert!(keys.contains(&AnimatedGradientParameter::DitherSize)); + assert!(!keys.contains(&AnimatedGradientParameter::GrainAmount)); + assert!(!keys.contains(&AnimatedGradientParameter::Relief)); + assert!( + !groups + .iter() + .flat_map(|(_, controls)| controls) + .any(|control| control.key == AnimatedGradientParameter::DitherSize) + ); assert!(groups.iter().all(|(name, _)| *name != "Animation")); assert!( groups diff --git a/apps/desktop-gpui/src/editor_sidebar/cursor.rs b/apps/desktop-gpui/src/editor_sidebar/cursor.rs index 910310e9b18..5e9168adb9e 100644 --- a/apps/desktop-gpui/src/editor_sidebar/cursor.rs +++ b/apps/desktop-gpui/src/editor_sidebar/cursor.rs @@ -5,10 +5,10 @@ use super::*; use crate::editor_tabs::CursorSlider; const CARD_GAP: f32 = 8.; -const TILE_HEIGHT: f32 = 60.; +const TILE_HEIGHT: f32 = 52.; const TILE_RADIUS: f32 = 10.; -const ARROW_BOX: f32 = 34.; -const CIRCLE_DISC: f32 = 28.; +const ARROW_BOX: f32 = 28.; +const CIRCLE_DISC: f32 = 22.; const CARD_GROUP: &str = "cursor-style-card"; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -23,7 +23,7 @@ impl CursorCard { match self { Self::Default => tr::editor::sidebar::cursor::default(), Self::Family(CursorFamily::MacOS) => "macOS", - Self::Family(CursorFamily::MacOSTahoe) => "macOS Tahoe", + Self::Family(CursorFamily::MacOSTahoe) => "Tahoe", Self::Family(CursorFamily::Windows) => "Windows", Self::Circle => tr::editor::sidebar::cursor::circle(), } @@ -188,37 +188,21 @@ impl EditorWindow { fn render_cursor_tile(&self, card: CursorCard, selected: bool, recorded: bool) -> AnyElement { let theme = self.theme; + let editor = theme.editor; let art = match card { - CursorCard::Default => div() - .flex() - .items_center() - .gap(px(12.)) - .child( - self.cursor_art( - self.recorded_cursor_family - .unwrap_or(host_cursor_family()) - .arrow(), - ARROW_BOX, - ), - ) - .child( - div() - .text_size(px(12.)) - .child(tr::editor::sidebar::cursor::default()), - ) - .child( - div() - .text_size(px(11.)) - .text_color(Hsla::from(theme.gray_11)) - .child(tr::editor::sidebar::cursor::recorded()), - ) - .into_any_element(), + CursorCard::Default => self.cursor_art( + self.recorded_cursor_family + .unwrap_or(host_cursor_family()) + .arrow(), + ARROW_BOX, + ), CursorCard::Family(family) => self.cursor_art(family.arrow(), ARROW_BOX), CursorCard::Circle => circle_art(), }; div() .id(SharedString::from(format!("cursor-tile-{}", card.key()))) + .relative() .w_full() .h(px(TILE_HEIGHT)) .rounded(px(TILE_RADIUS)) @@ -227,23 +211,32 @@ impl EditorWindow { .justify_center() .map(|this| { if selected { - this.border_2() - .border_color(Hsla::from(theme.blue_8)) - .bg(with_alpha(theme.blue_3, 0.4)) + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) } else { - this.border_1() - .border_color(Hsla::from(theme.gray_3)) - .bg(Hsla::from(theme.gray_2)) - .group_hover(CARD_GROUP, |this| { - this.border_color(Hsla::from(theme.gray_5)) - }) + this.bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .group_hover(CARD_GROUP, |this| this.bg(Hsla::from(editor.ctl_hover))) } }) - .when(recorded, |this| { - this.tooltip(move |_window, cx| { - ui::Tooltip::new(&theme, tr::editor::sidebar::cursor::recorded_tooltip()) - .view(cx) - }) + .when(recorded || card == CursorCard::Default, |this| { + let label = if card == CursorCard::Default { + tr::editor::sidebar::cursor::recorded() + } else { + tr::editor::sidebar::cursor::recorded_tooltip() + }; + this.tooltip(move |_window, cx| ui::Tooltip::new(&theme, label).view(cx)) + }) + .when(card == CursorCard::Default, |this| { + this.child( + div() + .absolute() + .top(px(5.)) + .right(px(5.)) + .size(px(6.)) + .rounded_full() + .bg(Hsla::from(editor.accent)), + ) }) .child(art) .into_any_element() @@ -271,29 +264,27 @@ impl EditorWindow { .gap(px(6.)) .cursor_pointer() .child(self.render_cursor_tile(card, selected, is_recorded)) - .when(card != CursorCard::Default, |this| { - this.child( - div() - .max_w_full() - .whitespace_nowrap() - .overflow_hidden() - .text_ellipsis() - .text_size(px(11.)) - .line_height(px(11.)) - .font_weight(FontWeight::MEDIUM) - .text_color(Hsla::from(if selected { - theme.gray_12 - } else { - theme.gray_11 - })) - .when(!selected, |this| { - this.group_hover(CARD_GROUP, |this| { - this.text_color(Hsla::from(theme.gray_12)) - }) + .child( + div() + .max_w_full() + .whitespace_nowrap() + .overflow_hidden() + .text_ellipsis() + .text_size(px(11.)) + .line_height(px(13.)) + .font_weight(FontWeight::MEDIUM) + .text_color(Hsla::from(if selected { + theme.editor.text_1 + } else { + theme.editor.text_2 + })) + .when(!selected, |this| { + this.group_hover(CARD_GROUP, |this| { + this.text_color(Hsla::from(theme.editor.text_1)) }) - .child(card.label()), - ) - }) + }) + .child(card.label()), + ) .on_click(cx.listener(move |this, _, window, cx| { let cursor_type = cursor_type.clone(); this.edit_project("cursor-type", window, cx, move |project| { @@ -328,24 +319,18 @@ impl EditorWindow { .flex() .flex_col() .gap(px(CARD_GAP)) - .child(self.render_cursor_card( - CursorCard::Default, - selected == CursorCard::Default, - recorded, - cx, - )) .child( div().flex().flex_row().gap(px(CARD_GAP)).children( cursor_cards() .into_iter() - .filter(|card| *card != CursorCard::Default) .map(|card| self.render_cursor_card(card, selected == card, recorded, cx)), ), ) .child( div() - .text_size(px(11.)) - .text_color(Hsla::from(self.theme.gray_11)) + .text_size(px(12.)) + .line_height(px(16.)) + .text_color(Hsla::from(self.theme.editor.text_2)) .child(description), ) .into_any_element() diff --git a/apps/desktop-gpui/src/editor_sidebar/input_sounds.rs b/apps/desktop-gpui/src/editor_sidebar/input_sounds.rs new file mode 100644 index 00000000000..f4ab5a5c838 --- /dev/null +++ b/apps/desktop-gpui/src/editor_sidebar/input_sounds.rs @@ -0,0 +1,367 @@ +use cap_editor::InputSoundPreview; +use cap_project::{ClickSoundType, KeySoundType, KeyboardData}; + +use super::*; + +const TILE_GAP: f32 = 8.; +const TILE_HEIGHT: f32 = 34.; +const TILE_RADIUS: f32 = 10.; +const TILE_GROUP: &str = "input-sound-tile"; + +const KEY_TYPES: [KeySoundType; 4] = [ + KeySoundType::Mechanical, + KeySoundType::Deep, + KeySoundType::Laptop, + KeySoundType::Typewriter, +]; +const CLICK_TYPES: [ClickSoundType; 2] = [ClickSoundType::Mouse, ClickSoundType::Trackpad]; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum InputSoundSlider { + Keys, + Clicks, +} + +impl InputSoundSlider { + pub fn limits(self) -> (f32, f32, f32) { + (0., 100., 1.) + } + + pub fn read(self, project: &ProjectConfiguration) -> f32 { + let volume = match self { + Self::Keys => project + .keyboard + .as_ref() + .map_or(cap_project::DEFAULT_INPUT_SOUND_VOLUME, |keyboard| { + keyboard.settings.key_sound_volume + }), + Self::Clicks => project.cursor.click_sound_volume, + }; + (volume * 100.).round() + } +} + +fn key_label(kind: KeySoundType) -> &'static str { + match kind { + KeySoundType::Mechanical => tr::editor::sidebar::input_sounds::mechanical(), + KeySoundType::Deep => tr::editor::sidebar::input_sounds::deep(), + KeySoundType::Laptop => tr::editor::sidebar::input_sounds::laptop(), + KeySoundType::Typewriter => tr::editor::sidebar::input_sounds::typewriter(), + } +} + +fn key_description(kind: KeySoundType) -> &'static str { + match kind { + KeySoundType::Mechanical => tr::editor::sidebar::input_sounds::mechanical_description(), + KeySoundType::Deep => tr::editor::sidebar::input_sounds::deep_description(), + KeySoundType::Laptop => tr::editor::sidebar::input_sounds::laptop_description(), + KeySoundType::Typewriter => tr::editor::sidebar::input_sounds::typewriter_description(), + } +} + +fn click_label(kind: ClickSoundType) -> &'static str { + match kind { + ClickSoundType::Mouse => tr::editor::sidebar::input_sounds::mouse(), + ClickSoundType::Trackpad => tr::editor::sidebar::input_sounds::trackpad(), + } +} + +fn click_description(kind: ClickSoundType) -> &'static str { + match kind { + ClickSoundType::Mouse => tr::editor::sidebar::input_sounds::mouse_description(), + ClickSoundType::Trackpad => tr::editor::sidebar::input_sounds::trackpad_description(), + } +} + +/// Plays a short phrase in the chosen sound off the UI thread, through the +/// default output, so a choice can be heard without starting playback. +fn audition(preview: InputSoundPreview, volume: f32) { + let spawned = std::thread::Builder::new() + .name("input-sound-preview".into()) + .spawn(move || { + let Some(samples) = cap_editor::render_input_sound_preview(preview, volume) else { + return; + }; + let played = rodio::OutputStream::try_default() + .map_err(|error| error.to_string()) + .and_then(|(_stream, handle)| { + let sink = rodio::Sink::try_new(&handle).map_err(|error| error.to_string())?; + sink.append(rodio::buffer::SamplesBuffer::new( + 2, + cap_audio::AudioData::SAMPLE_RATE, + samples, + )); + sink.sleep_until_end(); + Ok(()) + }); + if let Err(error) = played { + tracing::warn!(%error, "Could not preview the input sound"); + } + }); + if let Err(error) = spawned { + tracing::warn!(%error, "Could not start the input sound preview"); + } +} + +impl EditorWindow { + pub(crate) fn apply_input_sound_slider( + &mut self, + slider: InputSoundSlider, + value: f32, + window: &mut Window, + cx: &mut Context, + ) { + let volume = (value / 100.).clamp(0., 1.); + self.edit_project_without_timeline("input-sound-volume", window, cx, move |project| { + match slider { + InputSoundSlider::Keys => { + project + .keyboard + .get_or_insert_with(KeyboardData::default) + .settings + .key_sound_volume = volume + } + InputSoundSlider::Clicks => project.cursor.click_sound_volume = volume, + } + true + }); + } + + fn render_sound_tile( + &self, + id: SharedString, + label: &'static str, + selected: bool, + on_click: impl Fn(&mut Self, &mut Window, &mut Context) + 'static, + cx: &mut Context, + ) -> AnyElement { + // Painted like the key style and theme tiles above it. + let editor = self.theme.editor; + div() + .id(id) + .group(TILE_GROUP) + .flex_1() + .min_w_0() + .h(px(TILE_HEIGHT)) + .rounded(px(TILE_RADIUS)) + .flex() + .items_center() + .justify_center() + .px(px(8.)) + .map(|this| { + if selected { + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + } + }) + .child( + div() + .max_w_full() + .whitespace_nowrap() + .overflow_hidden() + .text_ellipsis() + .text_size(px(12.)) + .text_color(Hsla::from(if selected { + editor.text_1 + } else { + editor.text_2 + })) + .child(label), + ) + .on_click(cx.listener(move |this, _, window, cx| on_click(this, window, cx))) + .into_any_element() + } + + fn render_sound_options( + &self, + rows: Vec>, + description: &'static str, + volume: SliderKey, + cx: &mut Context, + ) -> AnyElement { + div() + .flex() + .flex_col() + .gap(px(TILE_GAP)) + .pt(px(4.)) + .pb(px(8.)) + .children( + rows.into_iter() + .map(|row| div().flex().flex_row().gap(px(TILE_GAP)).children(row)), + ) + .child( + div() + .text_size(px(11.)) + .text_color(Hsla::from(self.theme.gray_11)) + .child(description), + ) + .child(self.slider_field(tr::editor::sidebar::input_sounds::volume(), volume, "%", cx)) + .into_any_element() + } + + /// Key sounds follow the keyboard track: they play for presses while their + /// keys are on screen, so they live with the overlay's own settings. + pub(crate) fn render_key_sounds(&self, cx: &mut Context) -> AnyElement { + let theme = self.theme; + let settings = crate::editor_tabs::keyboard_settings(&self.project); + let enabled = settings.key_sounds; + let selected = settings.key_sound_type; + let mut section = div().flex().flex_col().child( + ui::Field::inline(&theme, tr::editor::sidebar::input_sounds::key_sounds()).value( + ui::Toggle::plain(&theme, "key-sounds", enabled) + .on_click(cx.listener(move |this, _, window, cx| { + this.edit_project_without_timeline("key-sounds", window, cx, |project| { + let settings = &mut project + .keyboard + .get_or_insert_with(KeyboardData::default) + .settings; + settings.key_sounds = !enabled; + true + }); + })) + .into_any_element(), + ), + ); + if enabled { + let tiles = KEY_TYPES + .iter() + .map(|&kind| { + self.render_sound_tile( + SharedString::from(format!("key-sound-{kind:?}")), + key_label(kind), + kind == selected, + move |this, window, cx| { + this.edit_project_without_timeline( + "key-sound-type", + window, + cx, + |project| { + let settings = &mut project + .keyboard + .get_or_insert_with(KeyboardData::default) + .settings; + let changed = settings.key_sound_type != kind; + settings.key_sound_type = kind; + changed + }, + ); + let volume = crate::editor_tabs::keyboard_settings(&this.project) + .key_sound_volume; + audition(InputSoundPreview::Keys(kind), volume); + }, + cx, + ) + }) + .collect::>(); + let mut tiles = tiles.into_iter(); + let rows = vec![tiles.by_ref().take(2).collect(), tiles.collect()]; + section = section.child(self.render_sound_options( + rows, + key_description(selected), + SliderKey::InputSound(InputSoundSlider::Keys), + cx, + )); + } + section.into_any_element() + } + + /// Click sounds sit with the cursor, which has to be visible for them to + /// play. Inside a style segment the cursor tab edits that segment's look, + /// and sounds are not part of it, so the section is left out there. + pub(crate) fn render_click_sounds(&self, cx: &mut Context) -> AnyElement { + if self.sidebar.style_target.is_some() { + return div().into_any_element(); + } + let theme = self.theme; + let cursor = &self.project.cursor; + let enabled = cursor.click_sounds; + let selected = cursor.click_sound_type; + let mut section = div().flex().flex_col().child( + ui::Field::inline(&theme, tr::editor::sidebar::input_sounds::click_sounds()).value( + ui::Toggle::plain(&theme, "click-sounds", enabled) + .on_click(cx.listener(move |this, _, window, cx| { + this.edit_project_without_timeline("click-sounds", window, cx, |project| { + project.cursor.click_sounds = !enabled; + true + }); + })) + .into_any_element(), + ), + ); + if enabled { + let tiles = CLICK_TYPES + .iter() + .map(|&kind| { + self.render_sound_tile( + SharedString::from(format!("click-sound-{kind:?}")), + click_label(kind), + kind == selected, + move |this, window, cx| { + this.edit_project_without_timeline( + "click-sound-type", + window, + cx, + |project| { + let changed = project.cursor.click_sound_type != kind; + project.cursor.click_sound_type = kind; + changed + }, + ); + audition( + InputSoundPreview::Clicks(kind), + this.project.cursor.click_sound_volume, + ); + }, + cx, + ) + }) + .collect(); + section = section.child(self.render_sound_options( + vec![tiles], + click_description(selected), + SliderKey::InputSound(InputSoundSlider::Clicks), + cx, + )); + } + section.into_any_element() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn volume_sliders_read_the_saved_fraction_as_a_percent() { + let mut project = ProjectConfiguration::default(); + assert_eq!(InputSoundSlider::Keys.read(&project), 60.); + assert_eq!(InputSoundSlider::Clicks.read(&project), 60.); + project.keyboard = Some(KeyboardData::default()); + project.keyboard.as_mut().unwrap().settings.key_sound_volume = 0.25; + project.cursor.click_sound_volume = 1.; + assert_eq!(InputSoundSlider::Keys.read(&project), 25.); + assert_eq!(InputSoundSlider::Clicks.read(&project), 100.); + assert_eq!(InputSoundSlider::Keys.limits(), (0., 100., 1.)); + } + + #[test] + fn every_sound_type_has_its_own_label_and_description() { + let labels: std::collections::HashSet<&str> = KEY_TYPES + .iter() + .map(|kind| key_label(*kind)) + .chain(CLICK_TYPES.iter().map(|kind| click_label(*kind))) + .collect(); + assert_eq!(labels.len(), KEY_TYPES.len() + CLICK_TYPES.len()); + let descriptions: std::collections::HashSet<&str> = KEY_TYPES + .iter() + .map(|kind| key_description(*kind)) + .chain(CLICK_TYPES.iter().map(|kind| click_description(*kind))) + .collect(); + assert_eq!(descriptions.len(), labels.len()); + } +} diff --git a/apps/desktop-gpui/src/editor_tabs.rs b/apps/desktop-gpui/src/editor_tabs.rs index 282d779d97a..640f57e4124 100644 --- a/apps/desktop-gpui/src/editor_tabs.rs +++ b/apps/desktop-gpui/src/editor_tabs.rs @@ -126,7 +126,12 @@ pub const CURSOR_STYLES: [(CursorAnimationStyle, Label, Label); 4] = [ ]; /// `FONT_OPTIONS` (`text-style.tsx:12-16`). -pub const FONT_OPTIONS: [&str; 3] = ["System Sans-Serif", "System Serif", "System Monospace"]; +pub const FONT_OPTIONS: [&str; 4] = [ + "Inter", + "System Sans-Serif", + "System Serif", + "System Monospace", +]; /// The project keeps the English `FONT_OPTIONS` value; menus show it translated. fn font_option_label(font: &str) -> SharedString { @@ -146,10 +151,14 @@ pub const TEXT_WEIGHTS: [(u32, Label); 3] = [ ]; /// `KEYBOARD_POSITION_OPTIONS` (`text-style.tsx:28-35`). -pub const KEYBOARD_POSITIONS: [(&str, Label); 6] = [ +/// Where the keys can sit, row by row as the position picker lays them out. +pub const KEYBOARD_POSITIONS: [(&str, Label); 9] = [ ("top-left", tr::editor::tabs::position::top_left), ("top-center", tr::editor::tabs::position::top_center), ("top-right", tr::editor::tabs::position::top_right), + ("middle-left", tr::editor::tabs::position::middle_left), + ("center", tr::editor::tabs::position::center), + ("middle-right", tr::editor::tabs::position::middle_right), ("bottom-left", tr::editor::tabs::position::bottom_left), ("bottom-center", tr::editor::tabs::position::bottom_center), ("bottom-right", tr::editor::tabs::position::bottom_right), @@ -269,6 +278,7 @@ pub struct CaptionPreset { pub id: &'static str, pub label: Label, pub description: Label, + pub font: &'static str, pub font_weight: u32, pub size: u32, pub color: &'static str, @@ -284,11 +294,272 @@ pub struct CaptionPreset { pub fade_duration: f32, } +/// Writes a caption preset's styling into `settings`. +pub(crate) fn apply_caption_preset_settings( + settings: &mut CaptionSettings, + preset: &CaptionPreset, +) { + settings.preset = preset.id.to_string(); + // A font the user picked themselves stays; only a preset's own face is + // swapped for the new preset's. + if CAPTION_PRESETS + .iter() + .any(|other| other.font == settings.font) + { + settings.font = preset.font.to_string(); + } + settings.font_weight = preset.font_weight; + settings.size = preset.size; + settings.color = preset.color.to_string(); + settings.background_color = preset.background_color.to_string(); + settings.background_opacity = preset.background_opacity; + settings.outline = preset.outline; + settings.outline_color = preset.outline_color.to_string(); + settings.highlight_color = preset.highlight_color.to_string(); + settings.active_word_highlight = preset.active_word_highlight; + settings.highlight_style = preset.highlight_style.to_string(); + settings.animation = preset.animation.to_string(); + settings.uppercase = preset.uppercase; + settings.fade_duration = preset.fade_duration; +} + +/// The key styles, each with the keys its tile shows. +static KEY_STYLES: [(&str, fn() -> &'static str, &[&str]); 5] = [ + ( + "classic", + tr::editor::tabs::keyboard::style_classic, + &["⌘", "K"], + ), + ( + "macos", + tr::editor::tabs::keyboard::style_macos, + &["⌘", "K"], + ), + ( + "windows", + tr::editor::tabs::keyboard::style_windows, + &["Ctrl", "K"], + ), + ( + "linux", + tr::editor::tabs::keyboard::style_linux, + &["Super", "K"], + ), + ( + "mechanical", + tr::editor::tabs::keyboard::style_mechanical, + &["⌘", "K"], + ), +]; + +/// How the style tiles are set out: the three platform keyboards side by +/// side, then the desk keyboard and the plain label. +const KEY_STYLE_ROWS: [&[&str]; 2] = [&["macos", "windows", "linux"], &["mechanical", "classic"]]; + +static KEY_THEMES: [(&str, fn() -> &'static str); 3] = [ + ("light", tr::editor::tabs::keyboard::theme_light), + ("dark", tr::editor::tabs::keyboard::theme_dark), + ("retro", tr::editor::tabs::keyboard::theme_retro), +]; + +fn rgba(color: [f32; 4]) -> Hsla { + gpui::Rgba { + r: color[0], + g: color[1], + b: color[2], + a: color[3], + } + .into() +} + +/// A small key, painted like the renderer paints it: the gradient top, the +/// side as a solid lip under it, a fine border and a soft shadow. +pub(crate) fn key_chip( + style: cap_rendering::keycaps::KeycapStyle, + theme: cap_rendering::keycaps::KeycapTheme, + label: &str, + modifier: bool, +) -> gpui::Div { + key_chip_sized(style, theme, label, modifier, 24.) +} + +pub(crate) fn key_chip_sized( + style: cap_rendering::keycaps::KeycapStyle, + theme: cap_rendering::keycaps::KeycapTheme, + label: &str, + modifier: bool, + height: f32, +) -> gpui::Div { + #[allow(non_snake_case)] + let HEIGHT = height; + let geometry = style.geometry(); + let depth = (geometry.depth * HEIGHT).max(2.); + if style.is_mechanical() { + return mechanical_chip(theme.mechanical_palette(modifier), label, HEIGHT, depth); + } + let palette = theme.palette(modifier); + div() + .h(px(HEIGHT - depth)) + .min_w(px(HEIGHT - depth)) + .px(px(HEIGHT * 0.25)) + .mb(px(depth)) + .flex() + .items_center() + .justify_center() + .rounded(px(geometry.radius * HEIGHT)) + .bg(gpui::linear_gradient( + 180., + gpui::linear_color_stop(rgba(palette.top[0]), 0.), + gpui::linear_color_stop(rgba(palette.top[1]), 1.), + )) + .border_1() + .border_color(rgba(palette.border)) + .shadow(vec![ + gpui::BoxShadow { + color: rgba(palette.side[1]), + offset: gpui::point(px(0.), px(depth)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: false, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., palette.shadow * 0.7), + offset: gpui::point(px(0.), px(depth + 1.)), + blur_radius: px(3.), + spread_radius: px(0.), + inset: false, + }, + ]) + .text_size(px( + HEIGHT * if label.chars().count() > 1 { 0.4 } else { 0.46 } + )) + .text_color(rgba(palette.label)) + .child(label.to_string()) +} + +/// A mechanical key in small: the walls as the body, lit lighter at the back +/// than the deep front, and the top face set in from them. +fn mechanical_chip( + palette: cap_rendering::keycaps::KeycapPalette, + label: &str, + height: f32, + depth: f32, +) -> gpui::Div { + let inset = (height * 0.1).max(2.); + div() + .h(px(height)) + .min_w(px(height)) + .pt(px((inset * 0.4).max(1.))) + .px(px(inset)) + .pb(px(depth + inset * 0.45)) + .flex() + .rounded(px(height * 0.11)) + .bg(gpui::linear_gradient( + 180., + gpui::linear_color_stop(rgba(palette.side[0]), 0.), + gpui::linear_color_stop(rgba(palette.side[1]), 1.), + )) + .border_1() + .border_color(rgba(palette.border)) + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., palette.shadow * 0.7), + offset: gpui::point(px(0.), px(1.5)), + blur_radius: px(3.), + spread_radius: px(0.), + inset: false, + }]) + .child( + div() + .flex_1() + .px(px(height * 0.16)) + .flex() + .items_center() + .justify_center() + .rounded(px(height * 0.08)) + .bg(gpui::linear_gradient( + 180., + gpui::linear_color_stop(rgba(palette.top[0]), 0.), + gpui::linear_color_stop(rgba(palette.top[1]), 1.), + )) + .shadow(vec![gpui::BoxShadow { + color: rgba(palette.highlight), + offset: gpui::point(px(0.), px(0.5)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }]) + .text_size(px( + height * if label.chars().count() > 1 { 0.34 } else { 0.4 } + )) + .text_color(rgba(palette.label)) + .child(label.to_string()), + ) +} + +/// A tile's preview: its keys as keycaps, or the classic label. +fn key_preview( + style: cap_rendering::keycaps::KeycapStyle, + theme: cap_rendering::keycaps::KeycapTheme, + keys: &[&str], +) -> gpui::AnyElement { + if style == cap_rendering::keycaps::KeycapStyle::Classic { + return div() + .h(px(22.)) + .px(px(9.)) + .flex() + .items_center() + .rounded(px(11.)) + .bg(gpui::hsla(0., 0., 0., 0.9)) + .text_size(px(11.)) + .text_color(gpui::white()) + .child(keys.concat()) + .into_any_element(); + } + div() + .flex() + .flex_row() + .items_end() + .gap(px(4.)) + .children( + keys.iter() + .enumerate() + .map(|(index, key)| key_chip(style, theme, key, index + 1 < keys.len())), + ) + .into_any_element() +} + +/// What was pasted into Type Keys as one line: a script's line breaks and +/// runs of spaces become single spaces. +pub(crate) fn key_prompt_text(input: &str) -> String { + input.split_whitespace().collect::>().join(" ") +} + pub static CAPTION_PRESETS: &[CaptionPreset] = &[ + // New captions start here: see `CaptionSettings::default`. + CaptionPreset { + id: "soft", + label: tr::editor::tabs::captions::preset::soft, + description: tr::editor::tabs::captions::preset::soft_description, + font: "Inter", + font_weight: 600, + size: 46, + color: "#FFFFFF", + background_color: "#1C1C1F", + background_opacity: 86, + outline: false, + outline_color: "#000000", + highlight_color: "#FFFFFF", + active_word_highlight: true, + highlight_style: "color", + animation: "pop", + uppercase: false, + fade_duration: 0.15, + }, CaptionPreset { id: "classic", label: tr::editor::tabs::captions::preset::classic, description: tr::editor::tabs::captions::preset::classic_description, + font: "System Sans-Serif", font_weight: 700, size: 50, color: "#FFFFFF", @@ -297,7 +568,7 @@ pub static CAPTION_PRESETS: &[CaptionPreset] = &[ outline: false, outline_color: "#000000", highlight_color: "#FFFFFF", - active_word_highlight: false, + active_word_highlight: true, highlight_style: "color", animation: "bounce", uppercase: false, @@ -307,6 +578,7 @@ pub static CAPTION_PRESETS: &[CaptionPreset] = &[ id: "karaoke", label: tr::editor::tabs::captions::preset::karaoke, description: tr::editor::tabs::captions::preset::karaoke_description, + font: "System Sans-Serif", font_weight: 700, size: 52, color: "#FFFFFF", @@ -325,6 +597,7 @@ pub static CAPTION_PRESETS: &[CaptionPreset] = &[ id: "highlight", label: tr::editor::tabs::captions::preset::highlight, description: tr::editor::tabs::captions::preset::highlight_description, + font: "System Sans-Serif", font_weight: 700, size: 54, color: "#FFFFFF", @@ -343,6 +616,7 @@ pub static CAPTION_PRESETS: &[CaptionPreset] = &[ id: "pop", label: tr::editor::tabs::captions::preset::pop, description: tr::editor::tabs::captions::preset::pop_description, + font: "System Sans-Serif", font_weight: 700, size: 56, color: "#FFFFFF", @@ -361,6 +635,7 @@ pub static CAPTION_PRESETS: &[CaptionPreset] = &[ id: "minimal", label: tr::editor::tabs::captions::preset::minimal, description: tr::editor::tabs::captions::preset::minimal_description, + font: "System Sans-Serif", font_weight: 600, size: 46, color: "#FFFFFF", @@ -747,12 +1022,15 @@ pub fn keyboard_settings(project: &ProjectConfiguration) -> KeyboardSettings { .unwrap_or_default() } -fn keyboard_generation_settings_fingerprint(settings: &KeyboardSettings) -> (u64, u32, bool, bool) { +fn keyboard_generation_settings_fingerprint( + settings: &KeyboardSettings, +) -> (u64, u32, bool, bool, bool) { ( settings.grouping_threshold_ms.to_bits(), settings.linger_duration.to_bits(), settings.show_modifiers, settings.show_special_keys, + settings.show_typing, ) } @@ -801,7 +1079,6 @@ pub enum SidebarMenu { CaptionAnimation, CaptionWeight, KeyboardFont, - KeyboardPosition, KeyboardWeight, /// The segment panels' own selects carry their segment index. /// @@ -916,10 +1193,6 @@ impl EditorWindow { .iter() .map(|font| ui::MenuItem::new(font_option_label(font), *font == keyboard.font)) .collect(), - SidebarMenu::KeyboardPosition => KEYBOARD_POSITIONS - .iter() - .map(|(value, label)| ui::MenuItem::new(label(), *value == keyboard.position)) - .collect(), SidebarMenu::KeyboardWeight => TEXT_WEIGHTS .iter() .map(|(weight, label)| ui::MenuItem::new(label(), *weight == keyboard.font_weight)) @@ -1167,15 +1440,6 @@ impl EditorWindow { settings.font = font }); } - SidebarMenu::KeyboardPosition => { - let Some((value, _)) = KEYBOARD_POSITIONS.get(index) else { - return; - }; - let value = value.to_string(); - self.set_keyboard_setting("keyboard-position", window, cx, move |settings| { - settings.position = value - }); - } SidebarMenu::KeyboardWeight => { let Some((weight, _)) = TEXT_WEIGHTS.get(index) else { return; @@ -1245,6 +1509,20 @@ impl EditorWindow { self.set_keyboard_setting_inner(true, reason, window, cx, change); } + /// The keycap style the renderer draws for `settings`: an older project's + /// `auto` resolves for the platform it was recorded on, and stays on its + /// label when any key was coloured on its own. + pub(crate) fn effective_key_style<'a>(&self, settings: &'a KeyboardSettings) -> &'a str { + let platform = self.summary().and_then(|summary| summary.platform.as_ref()); + let styled_keys = settings.style == "auto" + && self.project.timeline.as_ref().is_some_and(|timeline| { + timeline.keyboard_segments.iter().any(|segment| { + segment.color_override.is_some() || segment.background_color_override.is_some() + }) + }); + settings.effective_style_for(platform, styled_keys) + } + fn set_keyboard_setting_inner( &mut self, rebuild_timeline: bool, @@ -1253,7 +1531,20 @@ impl EditorWindow { cx: &mut Context, change: impl FnOnce(&mut KeyboardSettings), ) { - let apply = |project: &mut ProjectConfiguration| with_keyboard_settings(project, change); + // The first edit settles an older project's `auto` style on what it + // draws now, so editing its label (say, back to the default colours) + // can never flip it to keys mid-edit. + let settled = keyboard_settings(&self.project); + let settled = + (settled.style == "auto").then(|| self.effective_key_style(&settled).to_string()); + let apply = |project: &mut ProjectConfiguration| { + with_keyboard_settings(project, |settings| { + if let Some(style) = settled { + settings.style = style; + } + change(settings); + }) + }; if rebuild_timeline { self.edit_project(reason, window, cx, apply); } else { @@ -1519,6 +1810,16 @@ impl EditorWindow { /// `CameraConfig` (`ConfigSidebar.tsx:2978-3330`). pub(crate) fn render_camera_tab(&self, cx: &mut Context) -> AnyElement { let theme = self.theme; + if !self.summary().is_some_and(|summary| summary.has_camera) { + return crate::editor_side_panel::panel_empty_state( + &theme, + "icons/camera.svg", + tr::editor::tabs::camera::empty_title(), + tr::editor::tabs::camera::empty_description(), + ) + .pt(px(40.)) + .into_any_element(); + } let camera = &self.project.camera; let manual = camera.manual_position; @@ -2148,6 +2449,19 @@ impl EditorWindow { /// The cursor tab (`:820-1000`). pub(crate) fn render_cursor_tab(&self, cx: &mut Context) -> AnyElement { let theme = self.theme; + if !self + .summary() + .is_some_and(|summary| summary.has_cursor_data) + { + return crate::editor_side_panel::panel_empty_state( + &theme, + "icons/cursor.svg", + tr::editor::tabs::cursor::empty_title(), + tr::editor::tabs::cursor::empty_description(), + ) + .pt(px(40.)) + .into_any_element(); + } let cursor = &self.project.cursor; let hidden = cursor.hide; @@ -2192,6 +2506,7 @@ impl EditorWindow { cx, )) .child(self.render_cursor_ripple(cx)) + .child(self.render_click_sounds(cx)) .child( ui::Field::inline(&theme, tr::editor::tabs::cursor::hide_when_idle()).value( ui::Toggle::plain(&theme, "cursor-idle", idle) @@ -2238,20 +2553,22 @@ impl EditorWindow { let smooth = !cursor.raw; let mut body = body .child( - ui::Field::section(&theme, tr::editor::tabs::cursor::movement_style()).child( - ui::RadioCards::plain( - &theme, - "cursor-style", - CURSOR_STYLES - .iter() - .map(|(_, label, description)| { - ui::RadioCard::new(label(), Some(description())) - }) - .collect(), - style_index, - ) - .on_select(cx.listener( - |this, index: &usize, window, cx| { + ui::Field::section(&theme, tr::editor::tabs::cursor::movement_style()) + .child( + ui::SegmentedControl::editor( + &theme, + "cursor-style", + CURSOR_STYLES + .iter() + .enumerate() + .map(|(index, (_, label, _))| { + ui::SegmentOption::new(label(), style_index == Some(index)) + }) + .collect(), + ) + .stretch() + .item_height(px(28.)) + .on_select(cx.listener(|this, index: &usize, window, cx| { let Some((style, _, _)) = CURSOR_STYLES.get(*index) else { return; }; @@ -2267,9 +2584,19 @@ impl EditorWindow { } true }); + })) + .into_any_element(), + ) + .children(style_index.and_then(|index| CURSOR_STYLES.get(index)).map( + |(_, _, description)| { + div() + .text_size(px(12.)) + .line_height(px(16.)) + .text_color(Hsla::from(theme.editor.text_2)) + .child(description()) + .into_any_element() }, )), - ), ) .child( div() @@ -2453,6 +2780,16 @@ impl EditorWindow { let Some(timeline) = project.timeline.as_mut() else { return false; }; + // Keys added by hand are not the recording's to replace. + let mut segments = segments; + segments.extend( + timeline + .keyboard_segments + .iter() + .filter(|segment| cap_project::is_manual_keyboard_segment(segment)) + .cloned(), + ); + segments.sort_by(|a, b| a.start.total_cmp(&b.start)); timeline.keyboard_segments = segments; true }); @@ -2476,8 +2813,135 @@ impl EditorWindow { } /// `KeyboardTab` (`KeyboardTab.tsx:128-553`). + /// The key style tiles, then, for keycaps, the theme tiles. Each tile + /// shows a small key in the renderer's own colours. + fn render_key_style_picker( + &self, + settings: &KeyboardSettings, + cx: &mut Context, + ) -> impl IntoElement { + use cap_rendering::keycaps::{KeycapStyle, KeycapTheme}; + let theme = self.theme; + let effective = self.effective_key_style(settings).to_string(); + let style = KeycapStyle::from_setting_for( + &effective, + self.summary().and_then(|summary| summary.platform.as_ref()), + ); + let key_theme = KeycapTheme::from_setting(&settings.theme); + let tile_for = |&(value, label, keys): &(&'static str, fn() -> &'static str, &[&str])| { + let tile_style = KeycapStyle::from_setting(value); + self.key_choice_tile( + SharedString::from(format!("keyboard-style-{value}")), + label(), + effective == value, + key_preview(tile_style, key_theme, keys), + // The timeline's key labels follow the style, so it rebuilds. + cx.listener(move |this, _, window, cx| { + this.set_keyboard_setting("keyboard-style", window, cx, move |settings| { + settings.style = value.to_string() + }); + }), + ) + }; + let row = |values: &[&str]| { + div().flex().flex_row().gap(px(8.)).children( + values + .iter() + .filter_map(|value| KEY_STYLES.iter().find(|(v, ..)| v == value)) + .map(&tile_for), + ) + }; + let keys = KEY_STYLES + .iter() + .find(|(value, ..)| *value == effective) + .map_or(&["⌘", "K"][..], |(_, _, keys)| keys); + let themes = KEY_THEMES.iter().map(|(value, label)| { + let value = *value; + self.key_choice_tile( + SharedString::from(format!("keyboard-theme-{value}")), + label(), + settings.theme == value, + key_preview(style, KeycapTheme::from_setting(value), keys), + cx.listener(move |this, _, window, cx| { + this.set_keyboard_setting_inner( + false, + "keyboard-theme", + window, + cx, + move |settings| settings.theme = value.to_string(), + ); + }), + ) + }); + div() + .flex() + .flex_col() + .gap(px(16.)) + .child( + ui::Field::section(&theme, tr::editor::tabs::keyboard::key_style()).child( + div() + .flex() + .flex_col() + .gap(px(8.)) + .children(KEY_STYLE_ROWS.iter().map(|values| row(values))), + ), + ) + .when(style != KeycapStyle::Classic, |this| { + this.child( + ui::Field::section(&theme, tr::editor::tabs::keyboard::key_theme()) + .child(div().flex().flex_row().gap(px(8.)).children(themes)), + ) + }) + } + + fn key_choice_tile( + &self, + id: SharedString, + label: &'static str, + selected: bool, + preview: impl IntoElement, + on_click: impl Fn(&gpui::ClickEvent, &mut Window, &mut gpui::App) + 'static, + ) -> impl IntoElement { + let editor = self.theme.editor; + div() + .id(id) + .flex_1() + .min_w_0() + .h(px(78.)) + .flex() + .flex_col() + .items_center() + .justify_center() + .gap(px(10.)) + .rounded(px(12.)) + .map(|this| { + if selected { + this.bg(editor.pill_fill()) + .shadow(editor.choice_selected_shadow()) + } else { + this.bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl_hover))) + } + }) + .on_click(on_click) + .child(preview) + .child( + div() + .text_size(px(12.)) + .text_color(Hsla::from(if selected { + editor.text_1 + } else { + editor.text_2 + })) + .child(label), + ) + } + pub(crate) fn render_keyboard_tab(&self, cx: &mut Context) -> AnyElement { let theme = self.theme; + let blank = self.summary().is_some_and(|summary| summary.blank); let settings = keyboard_settings(&self.project); let enabled = settings.enabled; let has_segments = self @@ -2497,27 +2961,35 @@ impl EditorWindow { .find(|(weight, _)| *weight == settings.font_weight) .map_or(tr::editor::tabs::weight::normal(), |(_, label)| label()); + let classic = self.effective_key_style(&settings) == "classic"; + // `class={cx("space-y-4", !enabled && "opacity-50 pointer-events-none")}` let body = div() .flex() .flex_col() .gap(px(16.)) .when(!enabled, |this| this.opacity(0.5)) + .child(self.render_key_style_picker(&settings, cx)) + .child(self.render_key_sounds(cx)) .child( ui::Field::section(&theme, tr::editor::tabs::keyboard::font_settings()).child( div() .flex() .flex_col() .gap(px(12.)) - .child(self.labelled_inline( - tr::editor::tabs::keyboard::font_family(), - self.menu_select( - SidebarMenu::KeyboardFont, - "keyboard-font", - font_label, - cx, - ), - )) + // Keycaps are set in each keyboard's own face, so the + // family and weight only apply to the classic label. + .when(classic, |this| { + this.child(self.labelled_inline( + tr::editor::tabs::keyboard::font_family(), + self.menu_select( + SidebarMenu::KeyboardFont, + "keyboard-font", + font_label, + cx, + ), + )) + }) .child( self.slider_field( tr::editor::tabs::keyboard::size_label(), @@ -2527,61 +2999,67 @@ impl EditorWindow { ) .into_any_element(), ) - .child(self.labelled( - tr::editor::tabs::keyboard::text_color(), - self.render_hex_field( - crate::editor_panels::FieldKey::KeyboardColor, - &settings.color, - cx, - ), - )), - ), - ) - .child( - ui::Field::section(&theme, tr::editor::tabs::keyboard::background_settings()) - .child( - div() - .flex() - .flex_col() - .gap(px(12.)) - .child(self.labelled( - tr::editor::tabs::keyboard::background_color(), + .when(classic, |this| { + this.child(self.labelled( + tr::editor::tabs::keyboard::text_color(), self.render_hex_field( - crate::editor_panels::FieldKey::KeyboardBackground, - &settings.background_color, + crate::editor_panels::FieldKey::KeyboardColor, + &settings.color, cx, ), )) - .child( - self.slider_field( - tr::editor::tabs::keyboard::background_opacity(), - SliderKey::Keyboard(KeyboardSlider::BackgroundOpacity), - "", - cx, - ) - .into_any_element(), - ), - ), - ) - .child( - ui::Field::inline(&theme, tr::editor::tabs::keyboard::position()).child( - self.menu_select( - SidebarMenu::KeyboardPosition, - "keyboard-position", - position_label, - cx, - ), + }), ), ) + // Keycaps take their colours from the theme; the label's own + // background only applies to the classic look. + .when(classic, |this| { + this.child( + ui::Field::section(&theme, tr::editor::tabs::keyboard::background_settings()) + .child( + div() + .flex() + .flex_col() + .gap(px(12.)) + .child(self.labelled( + tr::editor::tabs::keyboard::background_color(), + self.render_hex_field( + crate::editor_panels::FieldKey::KeyboardBackground, + &settings.background_color, + cx, + ), + )) + .child( + self.slider_field( + tr::editor::tabs::keyboard::background_opacity(), + SliderKey::Keyboard(KeyboardSlider::BackgroundOpacity), + "", + cx, + ) + .into_any_element(), + ), + ), + ) + }) .child( - ui::Field::inline(&theme, tr::editor::tabs::keyboard::font_weight()).child( - self.menu_select( - SidebarMenu::KeyboardWeight, - "keyboard-weight", - weight_label, - cx, - ), - ), + ui::Field::section(&theme, tr::editor::tabs::keyboard::position()) + .value_text(position_label) + .child(self.render_key_position_picker(&settings.position, cx)), + ) + .when( + classic && settings.font != cap_rendering::keycaps::OVERLAY_TEXT_FONT, + |this| { + this.child( + ui::Field::inline(&theme, tr::editor::tabs::keyboard::font_weight()).child( + self.menu_select( + SidebarMenu::KeyboardWeight, + "keyboard-weight", + weight_label, + cx, + ), + ), + ) + }, ) .child( ui::Field::section(&theme, tr::editor::tabs::keyboard::animation()).child( @@ -2601,12 +3079,15 @@ impl EditorWindow { format!("{:.1}s", settings.linger_duration), cx, )) - .child(self.labelled_readout( - tr::editor::tabs::keyboard::grouping_threshold(), - SliderKey::Keyboard(KeyboardSlider::GroupingThreshold), - format!("{:.0}ms", settings.grouping_threshold_ms), - cx, - )), + // Grouping only joins typed letters into words. + .when(settings.show_typing, |this| { + this.child(self.labelled_readout( + tr::editor::tabs::keyboard::grouping_threshold(), + SliderKey::Keyboard(KeyboardSlider::GroupingThreshold), + format!("{:.0}ms", settings.grouping_threshold_ms), + cx, + )) + }), ), ) .child( @@ -2616,29 +3097,54 @@ impl EditorWindow { .flex_col() .gap(px(12.)) .child( - ui::Subfield::plain( - &theme, - tr::editor::tabs::keyboard::show_modifiers(), - ) - .child( - ui::Toggle::plain( + ui::Subfield::plain(&theme, tr::editor::tabs::keyboard::show_typing()) + .child( + ui::Toggle::plain( + &theme, + "keyboard-typing", + settings.show_typing, + ) + .on_click(cx.listener( + |this, _, window, cx| { + let next = + !keyboard_settings(&this.project).show_typing; + this.set_keyboard_setting( + "keyboard-typing", + window, + cx, + move |settings| settings.show_typing = next, + ); + }, + )), + ), + ) + .when(settings.show_typing, |this| { + this.child( + ui::Subfield::plain( &theme, - "keyboard-modifiers", - settings.show_modifiers, + tr::editor::tabs::keyboard::show_modifiers(), ) - .on_click(cx.listener( - |this, _, window, cx| { - let next = !keyboard_settings(&this.project).show_modifiers; - this.set_keyboard_setting( - "keyboard-modifiers", - window, - cx, - move |settings| settings.show_modifiers = next, - ); - }, - )), - ), - ) + .child( + ui::Toggle::plain( + &theme, + "keyboard-modifiers", + settings.show_modifiers, + ) + .on_click(cx.listener( + |this, _, window, cx| { + let next = + !keyboard_settings(&this.project).show_modifiers; + this.set_keyboard_setting( + "keyboard-modifiers", + window, + cx, + move |settings| settings.show_modifiers = next, + ); + }, + )), + ), + ) + }) .child( ui::Subfield::plain( &theme, @@ -2687,28 +3193,32 @@ impl EditorWindow { ), ), ) - .child( - div().pt(px(8.)).child( - ui::Button::plain( - &theme, - "keyboard-generate", - ui::ButtonVariant::Primary, - ui::ButtonSize::Md, - ) - .label(if self.generating_keyboard { - tr::editor::tabs::keyboard::generating() - } else if has_segments { - tr::editor::tabs::keyboard::regenerate() - } else { - tr::editor::tabs::keyboard::generate() - }) - .full_width() - .disabled(self.generating_keyboard || self.project.timeline.is_none()) - .on_click(cx.listener(|this, _, window, cx| { - this.generate_keyboard_segments_clicked(window, cx); - })), - ), - ) + // A blank project has no recording to take key presses from; + // its keys come from the composer and the track. + .when(!blank, |this| { + this.child( + div().pt(px(8.)).child( + ui::Button::plain( + &theme, + "keyboard-generate", + ui::ButtonVariant::Primary, + ui::ButtonSize::Md, + ) + .label(if self.generating_keyboard { + tr::editor::tabs::keyboard::generating() + } else if has_segments { + tr::editor::tabs::keyboard::regenerate() + } else { + tr::editor::tabs::keyboard::generate() + }) + .full_width() + .disabled(self.generating_keyboard || self.project.timeline.is_none()) + .on_click(cx.listener(|this, _, window, cx| { + this.generate_keyboard_segments_clicked(window, cx); + })), + ), + ) + }) .children(self.keyboard_generation_message.as_ref().map(|message| { div() .text_size(px(12.)) @@ -2716,7 +3226,7 @@ impl EditorWindow { .child(message.clone()) .into_any_element() })) - .children((!has_segments).then(|| { + .children((!has_segments && !blank).then(|| { div() .py(px(16.)) .flex() @@ -2763,10 +3273,271 @@ impl EditorWindow { })) .into_any_element(), ) + // Typing keys onto the video opens its own panel. + .child( + ui::EditorButton::plain(&theme, "keyboard-type-keys") + .left_icon("icons/keyboard.svg") + .label(tr::editor::tabs::keyboard::type_keys_button()) + .on_click(cx.listener(|this, _, window, cx| { + let at = this.playhead; + this.open_key_prompt(at, window, cx); + })), + ) .child(body) .into_any_element() } + /// Where the keys sit, picked on a little frame: nine spots, the chosen + /// one holding a key, so the choice reads as the video will. + fn render_key_position_picker(&self, current: &str, cx: &mut Context) -> AnyElement { + // The frame, at the video's own shape, as wide as the panel. + const FRAME_HEIGHT: f32 = 132.; + let theme = self.theme; + let editor = theme.editor; + // An older project's "above captions" sits where bottom-center does, + // and lights that spot; clicking it still writes bottom-center. + let stored = current.to_string(); + let current = match current { + "top" => "top-center", + "bottom" | "above-captions" => "bottom-center", + "middle-center" => "center", + other => other, + }; + let rows = KEYBOARD_POSITIONS.chunks(3).map(|row| { + div() + .flex() + .flex_1() + .flex_row() + .children(row.iter().map(|&(value, label)| { + let selected = value == current; + let align = |this: gpui::Div| match value.rsplit('-').next() { + Some("left") => this.justify_start(), + Some("right") => this.justify_end(), + _ => this.justify_center(), + }; + let vertical = |this: gpui::Div| match value.split('-').next() { + Some("top") => this.items_start(), + Some("bottom") => this.items_end(), + _ => this.items_center(), + }; + vertical(align( + div() + .flex() + .flex_1() + .p(px(7.)) + .cursor_pointer() + .group(value), + )) + .id(SharedString::from(format!("keyboard-position-{value}"))) + .tooltip(move |_window, cx| ui::Tooltip::new(&theme, label()).view(cx)) + .child(if selected { + // The chosen spot holds a key, as the frame will. + div() + .h(px(12.)) + .w(px(22.)) + .rounded(px(3.)) + .bg(Hsla::from(editor.accent)) + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.35), + offset: gpui::point(px(0.), px(1.5)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: false, + }]) + .into_any_element() + } else { + div() + .size(px(5.)) + .rounded_full() + .bg(Hsla::from(editor.text_3)) + .opacity(0.45) + .group_hover(value, |style| style.opacity(1.)) + .into_any_element() + }) + .when(stored != value, |this| { + let value = value.to_string(); + this.on_click(cx.listener(move |this, _, window, cx| { + let value = value.clone(); + this.set_keyboard_setting( + "keyboard-position", + window, + cx, + move |settings| settings.position = value, + ); + })) + }) + })) + }); + div() + .w_full() + .h(px(FRAME_HEIGHT)) + .p(px(4.)) + .flex() + .flex_col() + .rounded(px(10.)) + .bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) + .children(rows) + .into_any_element() + } + + /// How a typed sentence shows: a key per word (the default, and the most + /// like watching someone type), a key per keystroke, or whole. + fn render_typing_split(&self, cx: &mut Context) -> AnyElement { + let theme = self.theme; + let current = keyboard_settings(&self.project).typing_split; + let splits: [(&'static str, Label); 3] = [ + ("words", tr::editor::tabs::keyboard::split_words), + ("keys", tr::editor::tabs::keyboard::split_keys), + ("sentence", tr::editor::tabs::keyboard::split_sentence), + ]; + ui::Field::inline(&theme, tr::editor::tabs::keyboard::show_as()) + .value( + ui::SegmentedControl::editor( + &theme, + "keyboard-typing-split", + splits + .iter() + .map(|(value, label)| ui::SegmentOption::new(label(), *value == current)) + .collect(), + ) + .on_select(cx.listener(move |this, index: &usize, window, cx| { + let Some((value, _)) = splits.get(*index) else { + return; + }; + let value = value.to_string(); + this.set_keyboard_setting_inner( + false, + "keyboard-typing-split", + window, + cx, + move |settings| settings.typing_split = value, + ); + })) + .into_any_element(), + ) + .into_any_element() + } + + /// The Type Keys panel, in place of the sidebar while it is open: paste a + /// sentence, a script or a transcript, or a shortcut like cmd+shift+p, + /// and Add turns it into key presses on the timeline, typed out from + /// where the panel was opened. + pub(crate) fn render_key_prompt(&self, cx: &mut Context) -> AnyElement { + let theme = self.theme; + let Some(input) = self + .field(crate::editor_panels::FieldKey::KeyComposer) + .cloned() + else { + return div().into_any_element(); + }; + let typed = cap_project::manual_key_text(&key_prompt_text(input.read(cx).text())); + let has_text = !typed.trim().is_empty(); + let split = keyboard_settings(&self.project).typing_split; + let runs = cap_project::manual_key_runs(&typed, &split); + let seconds = runs.last().map_or(0.0, |run| run.offset + run.length); + let add_input = input.clone(); + div() + .id("key-prompt") + .flex() + .flex_col() + .flex_1() + .min_h_0() + .overflow_y_scroll() + .p(px(16.)) + .gap(px(16.)) + .child( + div() + .flex() + .flex_row() + .items_center() + .gap(px(8.)) + .child( + ui::EditorButton::plain(&theme, "key-prompt-close") + .left_icon("icons/x.svg") + .icon_size(px(14.)) + .label(tr::common::cancel()) + .on_click(cx.listener(|this, _, _, cx| { + this.close_key_prompt(cx); + })), + ) + .child( + div() + .text_size(px(14.)) + .text_color(Hsla::from(theme.editor.text_1)) + .child(tr::editor::tabs::keyboard::type_keys()), + ), + ) + .child( + div() + .text_size(px(12.)) + .line_height(px(17.)) + .text_color(Hsla::from(theme.gray_10)) + .child(tr::editor::tabs::keyboard::type_keys_hint()), + ) + .child( + ui::TextInput::plain(&theme, "key-prompt-text", &input) + .flex(true) + .padding_x(px(12.)) + .padding_y(px(10.)) + .text_size(px(14.)) + .height(px(150.)) + .bg(Hsla::from(theme.gray_2)) + .border(Hsla::from(theme.editor.line)), + ) + .child(self.render_typing_split(cx)) + .when(has_text, |this| { + this.child( + div() + .text_size(px(12.)) + .text_color(Hsla::from(theme.gray_10)) + .child(tr::editor::tabs::keyboard::type_keys_summary(&format!( + "{seconds:.1}" + ))), + ) + }) + .child( + ui::Button::plain( + &theme, + "key-prompt-add", + ui::ButtonVariant::Blue, + ui::ButtonSize::Md, + ) + .label(tr::editor::tabs::keyboard::add_to_timeline()) + .full_width() + .disabled(!has_text) + .on_click(cx.listener(move |this, _, window, cx| { + let text = key_prompt_text(add_input.read(cx).text()); + let at = this.key_prompt.unwrap_or(this.playhead); + if this.add_typed_keys(&text, at, window, cx) { + add_input.update(cx, |input, cx| input.set_text("", cx)); + this.close_key_prompt(cx); + } + })), + ) + .into_any_element() + } + + /// Opens Type Keys, to add key presses from `at` on. + pub(crate) fn open_key_prompt(&mut self, at: f64, window: &mut Window, cx: &mut Context) { + self.set_selection(None, cx); + self.audio_picker = None; + self.key_prompt = Some(at); + self.ensure_field(crate::editor_panels::FieldKey::KeyComposer, window, cx); + if let Some(input) = self + .field(crate::editor_panels::FieldKey::KeyComposer) + .cloned() + { + input.update(cx, |input, cx| input.focus_and_select_all(window, cx)); + } + cx.notify(); + } + + pub(crate) fn close_key_prompt(&mut self, cx: &mut Context) { + self.key_prompt = None; + cx.notify(); + } + /// A slider with a `text-xs text-right` readout underneath -- the shape the /// keyboard and caption tabs use for every duration. fn labelled_readout( @@ -3409,15 +4180,21 @@ impl EditorWindow { )), ), ) - .child( - ui::Field::inline(&theme, tr::editor::tabs::captions::font_weight()).child( - self.menu_select( - SidebarMenu::CaptionWeight, - "caption-weight", - weight_label, - cx, - ), - ), + // Inter ships at one weight, so it has no weight to pick. + .when( + settings.font != cap_rendering::keycaps::OVERLAY_TEXT_FONT, + |this| { + this.child( + ui::Field::inline(&theme, tr::editor::tabs::captions::font_weight()).child( + self.menu_select( + SidebarMenu::CaptionWeight, + "caption-weight", + weight_label, + cx, + ), + ), + ) + }, ); // An extra flex ancestor here repeats intrinsic layout while scrolling. @@ -3544,6 +4321,19 @@ impl EditorWindow { .px(px(8.)) .py(px(4.)) .bg(background) + // The Soft card's shadow and lit top edge. + .when(preset.id == "soft", |this| { + this.rounded(px(5.)) + .border_t_1() + .border_color(gpui::hsla(0., 0., 1., 0.12)) + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.45), + offset: gpui::point(px(0.), px(2.)), + blur_radius: px(6.), + spread_radius: px(0.), + inset: false, + }]) + }) .children(words.into_iter().enumerate().map(|(index, word)| { let emphasised = preset.active_word_highlight && index == 2; let pill = emphasised && preset.highlight_style == "pill"; @@ -3581,21 +4371,7 @@ impl EditorWindow { let Some(captions) = project.captions.as_mut() else { return false; }; - let settings = &mut captions.settings; - settings.preset = preset.id.to_string(); - settings.font_weight = preset.font_weight; - settings.size = preset.size; - settings.color = preset.color.to_string(); - settings.background_color = preset.background_color.to_string(); - settings.background_opacity = preset.background_opacity; - settings.outline = preset.outline; - settings.outline_color = preset.outline_color.to_string(); - settings.highlight_color = preset.highlight_color.to_string(); - settings.active_word_highlight = preset.active_word_highlight; - settings.highlight_style = preset.highlight_style.to_string(); - settings.animation = preset.animation.to_string(); - settings.uppercase = preset.uppercase; - settings.fade_duration = preset.fade_duration; + apply_caption_preset_settings(&mut captions.settings, preset); true }); } @@ -4178,6 +4954,27 @@ mod tests { /// 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 every_key_style_has_one_tile() { + let mut laid_out: Vec<&str> = KEY_STYLE_ROWS + .iter() + .flat_map(|row| row.iter().copied()) + .collect(); + laid_out.sort_unstable(); + let mut styles: Vec<&str> = KEY_STYLES.iter().map(|(value, ..)| *value).collect(); + styles.sort_unstable(); + assert_eq!(laid_out, styles); + // Every tile names a style the renderer draws as keys or the label. + for value in styles { + let style = cap_rendering::keycaps::KeycapStyle::from_setting(value); + assert_eq!( + style == cap_rendering::keycaps::KeycapStyle::Classic, + value == "classic", + "{value}" + ); + } + } + #[test] fn manual_caption_position_starts_at_the_drawn_caption() { let mut settings = CaptionSettings { @@ -4384,12 +5181,14 @@ mod tests { #[test] fn the_catalogues_match_the_source() { - assert_eq!(CAPTION_PRESETS.len(), 5); - assert_eq!(CAPTION_PRESETS[0].id, "classic"); - assert_eq!(CAPTION_PRESETS[2].highlight_style, "pill"); + assert_eq!(CAPTION_PRESETS.len(), 6); + assert_eq!(CAPTION_PRESETS[0].id, "soft"); + assert_eq!(CAPTION_PRESETS[1].id, "classic"); + assert_eq!(CAPTION_PRESETS[3].highlight_style, "pill"); assert_eq!(CAPTION_POSITIONS[0].0, "manual"); - assert_eq!(KEYBOARD_POSITIONS.len(), 6); - assert_eq!(FONT_OPTIONS.len(), 3); + assert_eq!(KEYBOARD_POSITIONS.len(), 9); + assert_eq!(KEYBOARD_POSITIONS[4].0, "center"); + assert_eq!(FONT_OPTIONS.len(), 4); for font in FONT_OPTIONS { assert_eq!(font_option_label(font).as_ref(), font); } @@ -4437,7 +5236,7 @@ mod tests { })); assert_eq!(keyboard_settings(&project).size, 72); // The rest of the defaults come along, not just the written key. - assert_eq!(keyboard_settings(&project).position, "bottom-center"); + assert_eq!(keyboard_settings(&project).position, "top-center"); } fn slider_model_project(timeline_segments: usize) -> ProjectConfiguration { diff --git a/apps/desktop-gpui/src/editor_timeline.rs b/apps/desktop-gpui/src/editor_timeline.rs index e83f801185a..19b0865cb95 100644 --- a/apps/desktop-gpui/src/editor_timeline.rs +++ b/apps/desktop-gpui/src/editor_timeline.rs @@ -36,6 +36,8 @@ use std::{ sync::{Arc, Mutex}, }; +use cap_rendering::keycaps::{self, KeycapStyle}; + mod playback_follow; pub use playback_follow::PlaybackFollow; @@ -67,7 +69,7 @@ pub const TIMELINE_PADDING: f32 = 11.; pub const TIMELINE_TOP_PADDING: f32 = 9.; pub const TIMELINE_BOTTOM_PADDING: f32 = 11.; pub const TRACK_GUTTER_GAP: f32 = 8.; -pub const TRACK_GUTTER: f32 = 104.; +pub const TRACK_GUTTER: f32 = 136.; pub const TRACK_ICON_WIDTH: f32 = TRACK_GUTTER - TRACK_GUTTER_GAP; pub const TIMELINE_HEADER_HEIGHT: f32 = 26.; /// The playhead hangs from the ruler's baseline, which is the header's bottom @@ -84,8 +86,8 @@ pub const TIMELINE_SLOT_PADDING: f32 = 8.; /// the timeline is computed over a column four pixels narrower than the ruler's. pub const SCROLL_BODY_PADDING_RIGHT: f32 = 4.; -pub const TRACK_HEIGHT: f32 = 44.; -pub const TRACK_ROW_GAP: f32 = 6.; +pub const TRACK_HEIGHT: f32 = 48.; +pub const TRACK_ROW_GAP: f32 = 8.; /// The radius of the band a row draws across its gutter and lane. pub const TRACK_BAND_RADIUS: f32 = 8.; /// The gap between the ruler's baseline and the first track row. It is not the @@ -116,13 +118,17 @@ pub fn hug_height(rows: usize) -> f32 { const RULER_MAJOR_TICK: f32 = 8.; const RULER_MINOR_TICK: f32 = 5.; /// The gutter cell: a 22px tile at 4px of left padding, then the label. -const TRACK_TILE_SIZE: f32 = 22.; +const TRACK_TILE_SIZE: f32 = 26.; +/// Room kept right of the label for the lane's delete button, which sits +/// 24px in from the gutter's edge, so a label never runs under it. +const TRACK_DELETE_CLEARANCE: f32 = 28.; const TRACK_TILE_INSET: f32 = 4.; +/// How far a paused hold's well sits in from the clip's edges and its own. +const HOLD_INSET_X: f32 = 3.; +const HOLD_INSET_Y: f32 = 6.; /// The segment's radius, its left accent bar and the content inset that clears /// it (`0 10px 0 13px`). -const SEGMENT_RADIUS: f32 = 8.; -const SEGMENT_ACCENT_BAR: f32 = 3.; -const SEGMENT_PADDING_LEFT: f32 = 13.; +const SEGMENT_RADIUS: f32 = 10.; const SEGMENT_PADDING_RIGHT: f32 = 10.; /// The selected 3D shot's pose dots: 6px, 5px inside each edge, so the resize /// handles still own the outer pixels. @@ -174,40 +180,96 @@ pub const MINIMAP_MAX_WIDTH: f32 = 78.; // Colours // --------------------------------------------------------------------------- -/// The single source of truth for track colour is eleven CSS custom properties -/// with one definition each -- not per-appearance values -- so they are literal -/// in both themes exactly as they are there -/// (`apps/desktop/src/styles/theme.css`). pub mod track_color { - pub const CLIP: u32 = 0x3b82f6; - pub const ZOOM: u32 = 0x64748b; - pub const CAPTION: u32 = 0x0ea5e9; - pub const KEYBOARD: u32 = 0xf97316; - pub const STYLE: u32 = 0xec4899; - pub const IMAGE: u32 = 0xf59e0b; - pub const TEXT: u32 = 0x14b8a6; - pub const MASK: u32 = 0xef4444; - pub const SHAPE: u32 = 0xd946ef; - pub const SCENE: u32 = 0x8b5cf6; - pub const AUDIO: u32 = 0x22c55e; - pub const THREE_D: u32 = 0x6366f1; + pub const CLIP: u32 = 0x3065c8; + pub const ZOOM: u32 = 0x6c50de; + pub const CAPTION: u32 = 0x0a6ea9; + pub const KEYBOARD: u32 = 0xb44b0b; + pub const STYLE: u32 = 0xa33fba; + pub const IMAGE: u32 = 0x945c07; + pub const TEXT: u32 = 0x0e7768; + pub const MASK: u32 = 0xc63838; + pub const SHAPE: u32 = 0x5a6787; + pub const SCENE: u32 = 0x5e6f12; + pub const AUDIO: u32 = 0x1b7940; + pub const THREE_D: u32 = 0xbe3b69; + pub const MUTED: u32 = 0x5f606a; } /// A muted clip keeps the clip lane's shape but drops its hue, so it reads as /// the row's own disabled state rather than as a different track. -fn muted_clip_color(theme: &Theme) -> Hsla { - Hsla::from(theme.editor.text_3) +fn muted_clip_color(_theme: &Theme) -> Hsla { + gpui::rgb(track_color::MUTED).into() } /// The `EditorPalette` mixers work in `Rgba`, which is what the `--ed-*` /// tokens are; every track colour in this file is an `Hsla`. -fn seg_fill(theme: &Theme, color: Hsla, selected: bool) -> Hsla { - let color = gpui::Rgba::from(color); - if selected { - theme.editor.seg_fill_selected(color).into() - } else { - theme.editor.seg_fill(color).into() - } +fn seg_fill(theme: &Theme, color: Hsla) -> Hsla { + theme.editor.seg_fill(gpui::Rgba::from(color)).into() +} + +fn seg_fill_sheen(theme: &Theme, color: Hsla) -> gpui::Background { + let (top, bottom) = theme.editor.seg_gradient(gpui::Rgba::from(color)); + gpui::linear_gradient( + 180., + gpui::linear_color_stop(top, 0.), + gpui::linear_color_stop(bottom, 1.), + ) +} + +fn seg_gloss() -> Vec { + vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.2), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.1), + offset: gpui::point(px(0.), px(-10.)), + blur_radius: px(14.), + spread_radius: px(-6.), + inset: true, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.12), + offset: gpui::point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.), + inset: true, + }, + ] +} + +/// The segment's own colour glowing under it: soft on hover, fuller once +/// selected, so selection reads as lit rather than boxed in. +fn seg_glow(color: Hsla, dark: bool, selected: bool) -> Vec { + let alpha = match (dark, selected) { + (true, true) => 0.7, + (false, true) => 0.55, + (true, false) => 0.6, + (false, false) => 0.46, + }; + // A tight glow that hugs the segment, then a wide one that lights the + // lane around it. + vec![ + gpui::BoxShadow { + color: color.opacity(alpha), + offset: gpui::point(px(0.), px(2.)), + blur_radius: px(if selected { 16. } else { 14. }), + spread_radius: px(if selected { 0. } else { -1. }), + inset: false, + }, + gpui::BoxShadow { + color: color.opacity(alpha * 0.45), + offset: gpui::point(px(0.), px(4.)), + blur_radius: px(30.), + spread_radius: px(2.), + inset: false, + }, + ] } fn seg_border(theme: &Theme, color: Hsla, extra_alpha: f32) -> Hsla { @@ -955,10 +1017,8 @@ pub fn waveform_sample_count(canvas_width: f64) -> usize { ((canvas_width * WAVEFORM_SAMPLES_PER_PIXEL).ceil() as usize).min(MAX_WAVEFORM_SAMPLES) } -/// Both waveforms are the lane's own colour at 55 %: over a tinted segment a -/// white or orange fill (`TL/ClipTrack.tsx:293-302`) has nothing to sit on. -pub fn waveform_color(color: Hsla) -> Hsla { - with_alpha(color, 0.55) +pub fn waveform_color(_color: Hsla) -> Hsla { + gpui::hsla(0., 0., 1., 0.42) } // --------------------------------------------------------------------------- @@ -1281,7 +1341,6 @@ pub enum SegmentDetail { /// `TL/TextTrack.tsx:428-450`. Text { content: SharedString, - color: Hsla, italic: bool, bold: bool, fullscreen: bool, @@ -1314,9 +1373,36 @@ pub enum SegmentDetail { /// `TL/KeyboardTrack.tsx:168-266`. Keyboard { text: SharedString, + /// Typed words, which read as text; a shortcut or a lone special key + /// reads as keys. + typed: bool, + /// The keys the overlay draws: what a shortcut shows as small + /// keycaps. + keys: Arc<[TimelineKey]>, }, } +/// One key a keyboard segment shows on the timeline, and the width its +/// keycap takes, worked out once when the timeline is built so drawing a +/// segment only adds widths. +#[derive(Debug, Clone, PartialEq)] +pub struct TimelineKey { + pub label: SharedString, + pub modifier: bool, + pub width: f32, +} + +impl TimelineKey { + fn new(label: SharedString, modifier: bool) -> Self { + let width = key_chip_width(&label); + Self { + label, + modifier, + width, + } + } +} + impl SegmentDetail { fn shows_waveform(&self) -> bool { matches!(self, Self::Clip { muted: false, .. }) @@ -1416,6 +1502,17 @@ impl TimelineModel { /// whether more than one recording clip exists (which decides `"Clip"` vs /// `"Clip N"`, `TL/ClipTrack.tsx:617-620`). pub fn build(config: &ProjectConfiguration, has_camera: bool, multiple_clips: bool) -> Self { + Self::build_for(config, has_camera, multiple_clips, None) + } + + /// [`TimelineModel::build`] for a recording made on `platform`, whose + /// keyboard the keys print as the renderer draws them. + pub fn build_for( + config: &ProjectConfiguration, + has_camera: bool, + multiple_clips: bool, + platform: Option<&cap_project::Platform>, + ) -> Self { let Some(timeline) = config.timeline.as_ref() else { return Self::default(); }; @@ -1477,7 +1574,6 @@ impl TimelineModel { } else { SharedString::from(segment.content.clone()) }, - color: parse_hex_color(&segment.color).unwrap_or_else(gpui::white), italic: segment.italic, // `font-weight: segment.fontWeight ?? 700` -- gpui has one // weight per family here, so anything at or above 600 @@ -1565,6 +1661,27 @@ impl TimelineModel { }) .collect(); + // The keys print the way the chosen keyboard prints them; the classic + // label reads like a Mac's. + let styled_keys = timeline.keyboard_segments.iter().any(|segment| { + segment.color_override.is_some() || segment.background_color_override.is_some() + }); + let key_style = config + .keyboard + .as_ref() + .map(|keyboard| { + KeycapStyle::from_setting_for( + keyboard.settings.effective_style_for(platform, styled_keys), + platform, + ) + }) + .filter(|style| *style != KeycapStyle::Classic) + .unwrap_or(KeycapStyle::MacOs); + // As the overlay decides: with typing hidden, a lone key is a key. + let show_typing = config.keyboard.as_ref().map_or_else( + || cap_project::KeyboardSettings::default().show_typing, + |keyboard| keyboard.settings.show_typing, + ); let keyboard = timeline .keyboard_segments .iter() @@ -1578,6 +1695,21 @@ impl TimelineModel { } else { SharedString::from(segment.display_text.clone()) }, + typed: (show_typing || cap_project::is_manual_keyboard_segment(segment)) + && is_typed_text(segment), + keys: keycaps::keycaps_for(&segment.display_text, key_style) + .into_iter() + .map(|cap| { + // Apple's named keys carry only their word; the + // space bar carries nothing and reads as ␣. + let label = match (cap.label.is_empty(), cap.legend) { + (false, _) => SharedString::from(cap.label), + (true, Some(word)) => SharedString::from(word), + (true, None) => SharedString::new_static("\u{2423}"), + }; + TimelineKey::new(label, cap.modifier) + }) + .collect(), }, }) .collect(); @@ -1647,7 +1779,17 @@ impl TimelineModel { multiple_clips: bool, lanes: &TrackLanes, ) -> Self { - let mut model = Self::build(config, has_camera, multiple_clips); + Self::build_with_lanes_for(config, has_camera, multiple_clips, lanes, None) + } + + pub fn build_with_lanes_for( + config: &ProjectConfiguration, + has_camera: bool, + multiple_clips: bool, + lanes: &TrackLanes, + platform: Option<&cap_project::Platform>, + ) -> Self { + let mut model = Self::build_for(config, has_camera, multiple_clips, platform); model.rows = build_rows(config, &model, has_camera, lanes); model } @@ -1985,31 +2127,48 @@ pub fn track_chip(theme: &Theme, kind: TrackKind) -> impl IntoElement { .items_center() .gap(px(8.)) .pl(px(TRACK_TILE_INSET)) - .pr(px(6.)) + .pr(px(TRACK_DELETE_CLEARANCE)) .child( div() .flex_none() .size(px(TRACK_TILE_SIZE)) - .rounded(px(6.)) + .rounded(px(7.)) .flex() .items_center() .justify_center() - .bg(tile_bg(theme, color)) + // A small piece of the track's own segment: the same + // gradient, lit edge and ring, with a white glyph. + .bg(seg_fill_sheen(theme, color)) + .shadow(vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.22), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.1), + offset: gpui::point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.), + inset: true, + }, + ]) .child( svg() .path(kind.icon()) - .size(px(12.)) + .size(px(14.)) .flex_none() - .text_color(tile_fg(theme, color)), + .text_color(gpui::hsla(0., 0., 1., 0.95)), ), ) .child( div() .min_w_0() .truncate() - .text_size(px(11.)) - .line_height(px(14.)) - .font_weight(FontWeight::MEDIUM) + .text_size(px(13.)) + .line_height(px(16.)) .child(kind.label()), ) } @@ -2281,7 +2440,7 @@ pub fn render_row( ) -> AnyElement { let height = model.track_height(); let selected = ui.row_selected(model, row); - let active_bg = Hsla::from(theme.editor.ctl_hover); + let active_bg = Hsla::from(theme.editor.ctl); let active_text = Hsla::from(theme.editor.text_1); div() .relative() @@ -2291,15 +2450,11 @@ pub fn render_row( .h(px(height)) .flex_none() .rounded(px(TRACK_BAND_RADIUS)) - .bg(if selected { - active_bg - } else { - Hsla::from(theme.editor.ctl) - }) + .when(selected, |this| this.bg(active_bg)) .text_color(if selected { active_text } else { - Hsla::from(theme.editor.text_2) + Hsla::from(theme.editor.text_2).blend(Hsla::from(theme.editor.text_1).opacity(0.5)) }) .hover(|style| style.bg(active_bg).text_color(active_text)) .child( @@ -2357,6 +2512,12 @@ fn render_track_content( content = content.child(empty); } + let visible_ends = if row.kind == TrackKind::Keyboard { + keyboard_visible_ends(segments, secs_per_pixel) + } else { + Vec::new() + }; + // Enumerated before the lane filter so the index stays the **config** // index -- what the selection and every mutator address segments by. for (index, segment) in segments @@ -2369,6 +2530,18 @@ fn render_track_content( if !view.transform.segment_visible(segment.start, segment.end) { continue; } + // Key presses linger after the key goes up, so one often runs past + // the next press; the overlay then shows the newer one. Each is drawn + // only until the next begins, so the row reads like the video. + let clipped = visible_ends + .get(index) + .copied() + .flatten() + .map(|end| Segment { + end, + ..segment.clone() + }); + let segment = clipped.as_ref().unwrap_or(segment); content = content.child(render_segment( theme, model, @@ -2567,6 +2740,56 @@ pub fn new_gap_segment( Some((start, start + min_duration)) } +/// A track's segments as the row draws them: key presses end where the next +/// begins; every other track as stored. Hit-testing uses this so a handle is +/// where it is drawn. Indices are unchanged. +pub fn drawn_segments( + kind: TrackKind, + segments: &[Segment], + secs_per_pixel: f64, +) -> std::borrow::Cow<'_, [Segment]> { + if kind != TrackKind::Keyboard { + return std::borrow::Cow::Borrowed(segments); + } + let ends = keyboard_visible_ends(segments, secs_per_pixel); + std::borrow::Cow::Owned( + segments + .iter() + .zip(ends) + .map(|(segment, end)| Segment { + end: end.unwrap_or(segment.end), + ..segment.clone() + }) + .collect(), + ) +} + +/// Where each key press stops being the one on screen: just before the next +/// press to start, when that comes before its own end. Never shorter than a +/// few pixels, so a burst of presses stays clickable. One sort, so hovering +/// a long track stays cheap. +pub fn keyboard_visible_ends(segments: &[Segment], secs_per_pixel: f64) -> Vec> { + let mut order: Vec = (0..segments.len()).collect(); + order.sort_by(|a, b| segments[*a].start.total_cmp(&segments[*b].start)); + let mut ends = vec![None; segments.len()]; + let mut next = 0; + for (position, &index) in order.iter().enumerate() { + let segment = &segments[index]; + next = next.max(position + 1); + while next < order.len() && segments[order[next]].start <= segment.start { + next += 1; + } + if let Some(&following) = order.get(next) { + let start = segments[following].start; + if start < segment.end { + ends[index] = + Some((start - 2.0 * secs_per_pixel).max(segment.start + 6.0 * secs_per_pixel)); + } + } + } + ends +} + /// The 3D lane's create ghost: where a click would put a shot, and what it /// would be. Pointer-events-none, like the zoom lane's -- the press is the /// row's own. @@ -2628,7 +2851,7 @@ fn render_camera3d_ghost( .border_1() .border_dashed() .border_color(seg_border(theme, color, 0.)) - .bg(seg_fill(theme, color, false)) + .bg(seg_fill(theme, color)) .child( div() .h(px(height)) @@ -2688,7 +2911,7 @@ fn render_gap_ghost( .justify_center() .rounded(px(SEGMENT_RADIUS)) .overflow_hidden() - .bg(seg_fill(theme, color, false)) + .bg(seg_fill(theme, color)) .border_1() .border_dashed() .border_color(seg_border(theme, color, 0.)) @@ -2720,7 +2943,7 @@ fn render_empty_prompt( .gap(px(5.)) .px(px(10.)) .rounded(px(SEGMENT_RADIUS)) - .when(active, |this| this.bg(seg_fill(theme, color, false))) + .when(active, |this| this.bg(seg_fill(theme, color))) .text_size(px(12.)) .line_height(px(16.)) .text_color(Hsla::from(if hovered { @@ -2759,7 +2982,10 @@ fn render_empty_track( // `TL/CaptionsTrack.tsx:146-160`. TrackKind::Caption => (tr::editor::timeline::empty::captions(), None), // `TL/KeyboardTrack.tsx:146-153`. - TrackKind::Keyboard => (tr::editor::timeline::empty::keyboard(), None), + TrackKind::Keyboard => ( + tr::editor::timeline::empty::keyboard(), + Some(tr::editor::timeline::empty::add_key()), + ), // `TL/AudioTrack.tsx:400-427`, `TL/ThreeDTrack.tsx:319-337`, and the // scene lane, which had no empty state at all. TrackKind::Audio => ( @@ -2853,17 +3079,8 @@ fn render_segment( .flex_row() .rounded(px(SEGMENT_RADIUS)) .overflow_hidden() - .bg(seg_fill(theme, color, selected)) - .child( - div() - .absolute() - .top(px(SEGMENT_RADIUS)) - .bottom(px(SEGMENT_RADIUS)) - .left(px(2.)) - .w(px(SEGMENT_ACCENT_BAR)) - .rounded_full() - .bg(color), - ); + .bg(seg_fill_sheen(theme, color)) + .shadow(seg_gloss()); // The clip track's waveform and its per-second markings, both under the // label (`TL/ClipTrack.tsx:943-957`). @@ -2898,7 +3115,7 @@ fn render_segment( for (hold_start, hold_end) in holds.iter() { let hold_x = ((hold_start - segment.start) / secs_per_pixel) as f32; let hold_width = ((hold_end - hold_start) / secs_per_pixel) as f32; - fill = fill.child(render_hold(theme, color, hold_x, hold_width, width, height)); + fill = fill.child(render_hold(hold_x, hold_width)); } } @@ -2933,12 +3150,27 @@ fn render_segment( )); } + let (label_width, label_center) = match &segment.detail { + SegmentDetail::Clip { holds, .. } if !holds.is_empty() => { + let spans: Vec<(f64, f64)> = holds + .iter() + .map(|(start, end)| { + ( + (start - segment.start) / secs_per_pixel, + (end - segment.start) / secs_per_pixel, + ) + }) + .collect(); + label_box_clear_of(visible_width, center_x, &spans) + } + _ => (visible_width, center_x), + }; fill = fill.child(render_label( theme, color, segment, - visible_width, - center_x, + label_width, + label_center, kind, )); @@ -2998,7 +3230,9 @@ fn render_segment( // visible bar, half-overhanging each edge. A *compact* handle carries no // `group-hover` class over there, so it stays visible with the pointer on // it (`TL/Track.tsx:250`). - let compact = (width as f64) < 40.; + // A key press is often a sliver; its handles wait for the pointer, so a + // run of presses reads as keys rather than a fence of bars. + let compact = (width as f64) < 40. && kind != TrackKind::Keyboard; let handle_opacity = if compact { 0.55 } else if hovered { @@ -3014,6 +3248,9 @@ fn render_segment( .left(px(x)) .w(px(width)) .rounded(px(SEGMENT_RADIUS)) + .when(hovered || selected, |this| { + this.shadow(seg_glow(color, theme.is_dark(), selected)) + }) .when_some(dim, |this, opacity| this.opacity(opacity)) // `interactMode === "split" && "timeline-scissors-cursor"` // (`TL/Track.tsx:107-108`). That cursor is an inline SVG data-URI; @@ -3029,7 +3266,7 @@ fn render_segment( if selected || (split_mode && hovered) { border .border(px(1.5)) - .border_color(Hsla::from(theme.editor.accent)) + .border_color(gpui::hsla(0., 0., 1., 0.9)) } else { border.border_1().border_color(seg_border( theme, @@ -3212,7 +3449,7 @@ fn render_fade( ) -> impl IntoElement { let span = (fraction * width).max(0.); let shade_x = if edge_in { 0. } else { width - span }; - let shade = with_alpha(Hsla::from(theme.editor.card), 0.55); + let shade = with_alpha(Hsla::from(theme.editor.window), 0.55); let curve = with_alpha(seg_label(theme, color), 0.85); let corner = with_alpha(seg_handle(theme, color), 0.8); @@ -3348,70 +3585,56 @@ fn rounded_segment_overlay( } /// The paused window a fullscreen text segment inserts inside a clip -/// (`TL/ClipTrack.tsx:959-1002`): the clip's tint washed back to the card, -/// ruled off at both edges, with a pause glyph. -fn render_hold( - theme: &Theme, - color: Hsla, - x: f32, - width: f32, - segment_width: f32, - height: f32, -) -> impl IntoElement { - let ink = with_alpha(seg_label(theme, color), 0.75); +/// (`TL/ClipTrack.tsx:959-1002`): a rounded well cut into the clip, shaded +/// along its top and lit along its lower lip, with a pause glyph and, when it +/// fits, its name. +fn render_hold(x: f32, width: f32) -> impl IntoElement { + let ink = gpui::hsla(0., 0., 1., 0.9); + let well = (width - HOLD_INSET_X * 2.).max(0.); div() .absolute() - .top_0() - .bottom_0() - .left(px(x)) - .w(px(width)) + .left(px(x + HOLD_INSET_X)) + .top(px(HOLD_INSET_Y)) + .bottom(px(HOLD_INSET_Y)) + .w(px(well)) + .rounded(px(SEGMENT_RADIUS - HOLD_INSET_X)) + .bg(gpui::hsla(0., 0., 0., 0.26)) + .shadow(vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.3), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(3.), + spread_radius: px(0.), + inset: true, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.14), + offset: gpui::point(px(0.), px(-1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + ]) .flex() .flex_row() .items_center() .justify_center() - .gap(px(4.)) + .gap(px(5.)) .overflow_hidden() - .child( - div() - .absolute() - .top_0() - .bottom_0() - .left(px(-x)) - .w(px(segment_width)) - .rounded(px(SEGMENT_RADIUS)) - .bg(with_alpha(Hsla::from(theme.editor.card), 0.6)), - ) - .children([0., width - 1.].map(|edge| { - let radius = SEGMENT_RADIUS.min(segment_width / 2.).min(height / 2.); - let inset = rounded_corner_inset( - f64::from(x + edge), - f64::from(segment_width), - f64::from(radius), + .when(well >= 14., |this| { + this.child( + svg() + .path("icons/pause.svg") + .size(px(10.)) + .flex_none() + .text_color(ink), ) - .max(rounded_corner_inset( - f64::from(x + edge + 1.), - f64::from(segment_width), - f64::from(radius), - )) as f32; - div() - .absolute() - .left(px(edge)) - .top(px(inset)) - .bottom(px(inset)) - .w(px(1.)) - .bg(seg_border(theme, color, 0.)) - })) - .child( - svg() - .path("icons/pause.svg") - .size(px(12.)) - .flex_none() - .text_color(ink), - ) - .when(width >= 64., |this| { + }) + .when(well >= 64., |this| { this.child( div() - .text_size(px(10.)) + .text_size(px(11.)) + .line_height(px(15.)) .font_weight(FontWeight::MEDIUM) .text_color(ink) .child(tr::editor::timeline::paused()), @@ -3599,6 +3822,42 @@ fn waveform_canvas( .into_any_element() } +fn label_box_clear_of(visible_width: f64, center_x: f64, covered: &[(f64, f64)]) -> (f64, f64) { + let start = center_x - visible_width / 2.; + let end = center_x + visible_width / 2.; + let mut covered: Vec<(f64, f64)> = covered + .iter() + .map(|&(a, b)| (a.max(start), b.min(end))) + .filter(|(a, b)| b > a) + .collect(); + if covered.is_empty() { + return (visible_width, center_x); + } + covered.sort_by(|a, b| a.0.total_cmp(&b.0)); + let mut runs = Vec::new(); + let mut cursor = start; + for (a, b) in covered { + if a > cursor { + runs.push((cursor, a)); + } + cursor = cursor.max(b); + } + if end > cursor { + runs.push((cursor, end)); + } + let pick = runs + .iter() + .find(|(a, b)| b - a >= SEGMENT_LABEL_COMPACT_PX) + .or_else(|| { + runs.iter() + .max_by(|x, y| (x.1 - x.0).total_cmp(&(y.1 - y.0))) + }); + match pick { + Some(&(a, b)) => (b - a, (a + b) / 2.), + None => (0., center_x), + } +} + /// `SegmentLabel` (`TL/Track.tsx:186-220`): full, compact and glyph tiers, /// anchored to the visible box and left-aligned inside the segment's own /// `0 10px 0 13px` content padding. @@ -3610,6 +3869,9 @@ fn render_label( center_x: f64, kind: TrackKind, ) -> impl IntoElement { + if let SegmentDetail::Keyboard { text, typed, keys } = &segment.detail { + return render_key_label(theme, color, text, *typed, keys, visible_width, center_x); + } let compact_at = match kind { TrackKind::Caption | TrackKind::Keyboard | TrackKind::Audio => { SEGMENT_LABEL_COMPACT_TIGHT_PX @@ -3635,9 +3897,9 @@ fn render_label( let (pad_left, pad_right) = if tier == LabelTier::Glyph { (4., 4.) } else if visible_width >= 60. { - (SEGMENT_PADDING_LEFT as f64, SEGMENT_PADDING_RIGHT as f64) + (SEGMENT_PADDING_RIGHT as f64, SEGMENT_PADDING_RIGHT as f64) } else { - (6., 4.) + (5., 5.) }; let max_width = (visible_width - pad_left - pad_right).max(0.); let left = center_x - visible_width / 2. + pad_left; @@ -3651,7 +3913,7 @@ fn render_label( .w(px(max_width as f32)) .flex() .items_center() - .when(tier == LabelTier::Glyph, |this| this.justify_center()) + .justify_center() .overflow_hidden() .child(body), ) @@ -3693,6 +3955,241 @@ fn label_secondary(theme: &Theme, color: Hsla) -> gpui::Div { .text_color(seg_muted(theme, color)) } +/// One key on a keyboard segment: a small physical key in a deeper shade +/// of the segment, lit along its top and standing on a darker lip, with its +/// legend in white. It is exactly as wide as [`key_chip_width`] says, so the +/// keys laid out by [`fit_key_label`] never overrun the segment. Modifiers +/// sit a step quieter than the key they modify. +fn key_chip(theme: &Theme, color: Hsla, key: &TimelineKey) -> gpui::Div { + let face = theme.editor.badge_fill(gpui::Rgba::from(color)); + div() + .flex_none() + .w(px(key.width)) + .h(px(KEY_CHIP_HEIGHT)) + .px(px(KEY_CHIP_PAD_X)) + .flex() + .items_center() + .justify_center() + .rounded(px(4.5)) + .bg(Hsla::from(face)) + .shadow(vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.16), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.3), + offset: gpui::point(px(0.), px(-2.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + ]) + .text_size(px(11.)) + .line_height(px(13.)) + .font_weight(FontWeight::MEDIUM) + .text_color(gpui::hsla( + 0., + 0., + 1., + if key.modifier { 0.82 } else { 0.98 }, + )) + .truncate() + .child(key.label.clone()) +} + +/// Whether a key segment is typed words: not a shortcut, and with at least +/// one character that is not a key symbol, so a lone Shift or Return reads +/// as its key. The shortcut test matches the overlay's own. +fn is_typed_text(segment: &cap_project::KeyboardTrackSegment) -> bool { + !is_shortcut(segment) && segment.display_text.chars().any(|c| !is_key_symbol(c)) +} + +fn is_key_symbol(c: char) -> bool { + matches!( + c, + '⌘' | '⌥' + | '⇧' + | '⌃' + | '⇪' + | '⏎' + | '↵' + | '⇥' + | '⌫' + | '⌦' + | '⎋' + | '⇱' + | '⇲' + | '⇞' + | '⇟' + | '↑' + | '↓' + | '←' + | '→' + | '␣' + ) +} + +/// A shortcut, as the overlay tells one from typing: it holds ⌃, ⌥ or ⌘, +/// or one key struck again and again, unless each strike typed one more of +/// the same character. +fn is_shortcut(segment: &cap_project::KeyboardTrackSegment) -> bool { + if segment.display_text.contains(['⌃', '⌥', '⌘']) { + return true; + } + let repeated = segment.keys.len() > 1 + && segment + .keys + .iter() + .all(|key| key.key == segment.keys[0].key); + if !repeated { + return false; + } + let mut chars = segment.display_text.chars(); + let first = chars.next(); + let typed = segment.display_text.chars().count() == segment.keys.len() + && chars.all(|c| Some(c) == first); + !typed +} + +/// How far a keyboard segment's label stays in from each edge: clear of the +/// trim handle's bar, which sits 5 to 8 px inside the edge. +const KEY_LABEL_INSET: f32 = 10.; +const KEY_CHIP_HEIGHT: f32 = 18.; +const KEY_CHIP_PAD_X: f32 = 5.; +const KEY_CHIP_MIN_WIDTH: f32 = 18.; +const KEY_CHIP_GAP: f32 = 3.; +/// The narrowest box typed words show in: a letter and an ellipsis. +const KEY_TEXT_MIN_WIDTH: f32 = 16.; +/// The narrowest box the keyboard glyph shows in. +const KEY_GLYPH_MIN_WIDTH: f32 = 12.; + +/// The width of one key's keycap at the timeline's 11 px legend, from its +/// characters, erring wide so the legend fits the cap it is given. +fn key_chip_width(label: &str) -> f32 { + let ink: f32 = label + .chars() + .map(|c| match c { + ' ' => 3.5, + c if c.is_ascii_uppercase() || c.is_ascii_digit() => 7.4, + c if c.is_ascii_lowercase() => 6.4, + c if c.is_ascii() => 4.8, + // Key symbols and anything outside ASCII. + _ => 10.5, + }) + .sum(); + (ink + KEY_CHIP_PAD_X * 2.).max(KEY_CHIP_MIN_WIDTH) +} + +/// The width of the quiet `+N` that stands for keys left out. +fn more_keys_width(more: usize) -> f32 { + let digits = more.checked_ilog10().unwrap_or(0) + 1; + 7. + 6.6 * digits as f32 +} + +/// What a keyboard segment shows in `width` pixels. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum KeyLabel { + /// The first `n` keys, and a count of the rest when some are left out. + Keys { + shown: usize, + more: usize, + }, + /// The typed words, cut short with an ellipsis if they must be. + Text, + Glyph, + Nothing, +} + +/// Whole keys only: as many of a shortcut's keys as fit, with a `+N` for +/// the rest, and failing that the keyboard glyph. Typed words show as text. +fn fit_key_label(typed: bool, keys: &[TimelineKey], width: f32) -> KeyLabel { + let fallback = if width >= KEY_GLYPH_MIN_WIDTH { + KeyLabel::Glyph + } else { + KeyLabel::Nothing + }; + if typed || keys.is_empty() { + return if width >= KEY_TEXT_MIN_WIDTH { + KeyLabel::Text + } else { + fallback + }; + } + // `row[n]` is the width of the first `n` keys and the gaps between them. + let row = |n: usize| -> f32 { + keys[..n].iter().map(|key| key.width).sum::() + + KEY_CHIP_GAP * n.saturating_sub(1) as f32 + }; + if row(keys.len()) <= width { + return KeyLabel::Keys { + shown: keys.len(), + more: 0, + }; + } + (1..keys.len()) + .rev() + .find(|&shown| row(shown) + KEY_CHIP_GAP + more_keys_width(keys.len() - shown) <= width) + .map_or(fallback, |shown| KeyLabel::Keys { + shown, + more: keys.len() - shown, + }) +} + +/// A keyboard segment's label: its keys or words, centred in the box left +/// between the trim handles. +fn render_key_label( + theme: &Theme, + color: Hsla, + text: &SharedString, + typed: bool, + keys: &[TimelineKey], + visible_width: f64, + center_x: f64, +) -> gpui::Div { + let width = (visible_width as f32 - KEY_LABEL_INSET * 2.).max(0.); + let body = match fit_key_label(typed, keys, width) { + KeyLabel::Keys { shown, more } => label_row() + .gap(px(KEY_CHIP_GAP)) + .children(keys[..shown].iter().map(|key| key_chip(theme, color, key))) + .when(more > 0, |row| { + row.child( + div() + .flex_none() + .text_size(px(11.)) + .line_height(px(13.)) + .font_weight(FontWeight::MEDIUM) + .text_color(with_alpha(seg_label(theme, color), 0.78)) + .child(SharedString::from(format!("+{more}"))), + ) + }) + .into_any_element(), + KeyLabel::Text => label_row() + .child(label_primary(theme, color).child(text.clone())) + .into_any_element(), + KeyLabel::Glyph => label_glyph(theme, color, "icons/keyboard.svg", 12.), + KeyLabel::Nothing => return div(), + }; + div().child( + div() + .absolute() + .top_0() + .bottom_0() + .left(px( + center_x as f32 - visible_width as f32 / 2. + KEY_LABEL_INSET + )) + .w(px(width)) + .flex() + .items_center() + .justify_center() + .overflow_hidden() + .child(body), + ) +} + fn label_glyph(theme: &Theme, color: Hsla, path: &'static str, size: f32) -> AnyElement { svg() .path(path) @@ -3871,7 +4368,6 @@ fn label_body( ( SegmentDetail::Text { content, - color: text_color, italic, bold, fullscreen, @@ -3880,28 +4376,23 @@ fn label_body( LabelTier::Full | LabelTier::Compact, ) => label_row() .gap(px(6.)) - .child( - div() - .size(px(8.)) - .flex_none() - .rounded_full() - .bg(*text_color), - ) - .child( - label_primary(theme, color) - .when(*bold, |this| this.font_weight(FontWeight::BOLD)) - .when(*italic, |this| this.italic()) - .child(content.clone()), - ) + // A fullscreen text pauses the video: it leads with the same + // glyph as the hold it cuts into the clip. .when(*fullscreen, |this| { this.child( svg() .path("icons/pause.svg") .size(px(10.)) .flex_none() - .text_color(seg_muted(theme, color)), + .text_color(seg_label(theme, color)), ) }) + .child( + label_primary(theme, color) + .when(*bold, |this| this.font_weight(FontWeight::BOLD)) + .when(*italic, |this| this.italic()) + .child(content.clone()), + ) .into_any_element(), (SegmentDetail::Text { fullscreen, .. }, LabelTier::Glyph) => { if !*fullscreen { @@ -3965,18 +4456,8 @@ fn label_body( .into_any_element(), (SegmentDetail::Caption { .. }, LabelTier::Glyph) => return None, - // -- Keyboard (`TL/KeyboardTrack.tsx:166-173, 260-268`) ----------- - (SegmentDetail::Keyboard { text }, LabelTier::Full | LabelTier::Compact) => label_row() - .child( - label_primary(theme, color) - .font_family("monospace") - .child(text.clone()), - ) - .into_any_element(), - (SegmentDetail::Keyboard { .. }, LabelTier::Glyph) => label_primary(theme, color) - .font_family("monospace") - .child("\u{2328}") - .into_any_element(), + // Key presses fit their own label: see `render_key_label`. + (SegmentDetail::Keyboard { .. }, _) => return None, }) } @@ -4029,17 +4510,17 @@ fn clip_badge( .gap(px(3.)) .px(px(5.)) .rounded(px(4.)) - .bg(seg_border(theme, color, 0.)) + .bg(Hsla::from(theme.editor.badge_fill(gpui::Rgba::from(color)))) .text_size(px(10.)) .line_height(px(14.)) .font_weight(FontWeight::MEDIUM) - .text_color(seg_label(theme, color)) + .text_color(Hsla::from(theme.editor.badge_ink(gpui::Rgba::from(color)))) .child( svg() .path(icon) .size(px(10.)) .flex_none() - .text_color(seg_label(theme, color)), + .text_color(Hsla::from(theme.editor.badge_ink(gpui::Rgba::from(color)))), ) .child(label) } @@ -4077,14 +4558,14 @@ pub fn render_playhead(theme: &Theme, x: f32, opacity: f32) -> AnyElement { color, x, opacity, - Some((12., color, theme.editor.card.into())), + Some((12., color, theme.editor.window.into())), ) } /// The hover preview line (`TL/index.tsx:1255-1277`). pub fn render_preview_line(theme: &Theme, x: f32) -> AnyElement { let color = with_alpha(Hsla::from(theme.editor.text_3), 0.5); - render_line(color, x, 1., Some((10., color, theme.editor.card.into()))) + render_line(color, x, 1., Some((10., color, theme.editor.window.into()))) } fn render_line(color: Hsla, x: f32, opacity: f32, head: Option<(f32, Hsla, Hsla)>) -> AnyElement { @@ -4127,6 +4608,81 @@ pub fn wheel_zoom_delta(dom_delta_y: f64, zoom: f64) -> f64 { mod tests { use super::*; + #[test] + fn every_track_label_and_value_reads_at_aa_contrast_in_both_themes() { + use crate::theme::{EditorPalette, mix, relative_luminance}; + let contrast = |a: gpui::Rgba, b: gpui::Rgba| { + let (a, b) = (relative_luminance(a), relative_luminance(b)); + (a.max(b) + 0.05) / (a.min(b) + 0.05) + }; + let tracks = [ + track_color::CLIP, + track_color::ZOOM, + track_color::CAPTION, + track_color::KEYBOARD, + track_color::STYLE, + track_color::IMAGE, + track_color::TEXT, + track_color::MASK, + track_color::SHAPE, + track_color::SCENE, + track_color::AUDIO, + track_color::THREE_D, + ]; + for palette in [EditorPalette::light(), EditorPalette::dark()] { + let colors = tracks + .into_iter() + .map(gpui::rgb) + .chain([gpui::rgb(track_color::MUTED)]); + for color in colors { + let badge = palette.badge_fill(color); + assert!( + contrast(palette.badge_ink(color), badge) >= 4.5, + "badge {color:?}: {:.2}", + contrast(palette.badge_ink(color), badge) + ); + for fill in palette.seg_text_band(color) { + for text in [palette.seg_label(color), palette.seg_muted(color)] { + let ink = mix(fill, gpui::Rgba { a: 1., ..text }, text.a); + assert!( + contrast(ink, fill) >= 4.5, + "{color:?}: {:.2}", + contrast(ink, fill) + ); + } + } + } + } + } + + #[test] + fn a_clip_label_steps_past_a_hold_at_its_start() { + let (width, center) = label_box_clear_of(1000., 500., &[(0., 150.)]); + assert_eq!(width, 850.); + assert_eq!(center, 575.); + } + + #[test] + fn a_clip_label_takes_the_first_gap_wide_enough() { + let (width, center) = label_box_clear_of(1000., 500., &[(0., 150.), (170., 400.)]); + assert_eq!(width, 600.); + assert_eq!(center, 700.); + } + + #[test] + fn a_clip_label_without_holds_keeps_the_visible_box() { + assert_eq!(label_box_clear_of(320., 160., &[]), (320., 160.)); + assert_eq!( + label_box_clear_of(320., 160., &[(400., 500.)]), + (320., 160.) + ); + } + + #[test] + fn a_clip_fully_held_shows_no_label() { + assert_eq!(label_box_clear_of(200., 100., &[(0., 200.)]).0, 0.); + } + const AUDIO_SAMPLE_RATE: usize = 48_000; // -- The ruler ---------------------------------------------------------- @@ -4421,14 +4977,14 @@ mod tests { // -- Geometry ----------------------------------------------------------- /// The editor's default width: 1275 minus 16 (slot), 24 (the card's - /// hairline and padding), 4 (`pr-1`) and 104 (gutter) = 1127px of track - /// content starting at x = 124, with the ruler's own strip four pixels + /// hairline and padding), 4 (`pr-1`) and 136 (gutter) = 1095px of track + /// content starting at x = 156, with the ruler's own strip four pixels /// wider. #[test] fn the_content_column_carries_the_scroll_bodys_padding() { - assert_eq!(content_width(1275.), 1127.); - assert_eq!(ruler_width(1275.), 1131.); - assert_eq!(content_left(), 124.); + assert_eq!(content_width(1275.), 1095.); + assert_eq!(ruler_width(1275.), 1099.); + assert_eq!(content_left(), 156.); } #[test] @@ -4438,10 +4994,10 @@ mod tests { let transform = Transform::initial(total); let content = content_width(width); - assert!(time_from_x(124., width, transform, total).abs() < 1e-9); - let end = time_from_x(124. + content, width, transform, total); + assert!(time_from_x(content_left(), width, transform, total).abs() < 1e-9); + let end = time_from_x(content_left() + content, width, transform, total); assert!((end - total).abs() < 1e-6, "{end}"); - let middle = time_from_x(124. + content / 2., width, transform, total); + let middle = time_from_x(content_left() + content / 2., width, transform, total); assert!((middle - total / 2.).abs() < 1e-6, "{middle}"); } @@ -4450,8 +5006,11 @@ mod tests { let width = 1275.; let total = 60.0; let transform = Transform::initial(total); - assert_eq!(time_from_x(124. + 9., width, transform, total), 0.0); - assert!(time_from_x(124. + 11., width, transform, total) > 0.0); + assert_eq!( + time_from_x(content_left() + 9., width, transform, total), + 0.0 + ); + assert!(time_from_x(content_left() + 11., width, transform, total) > 0.0); assert_eq!(time_from_x(0., width, transform, total), 0.0); assert_eq!(time_from_x(9_000., width, transform, total), total); } @@ -4465,7 +5024,12 @@ mod tests { zoom: 10., position: 20., }; - let time = time_from_x(124. + content_width(width) / 2., width, transform, 600.); + let time = time_from_x( + content_left() + content_width(width) / 2., + width, + transform, + 600., + ); assert!((time - 25.0).abs() < 1e-6, "{time}"); } @@ -4475,18 +5039,167 @@ mod tests { fn the_hover_preview_clears_outside_the_content_column() { let width = 1275.; let transform = Transform::initial(60.); - assert_eq!(preview_time_from_x(123., width, transform), None); assert_eq!( - preview_time_from_x(124. + content_width(width) + 1., width, transform), + preview_time_from_x(content_left() - 1., width, transform), None ); - assert_eq!(preview_time_from_x(124. + 5., width, transform), Some(0.)); - let middle = preview_time_from_x(124. + content_width(width) / 2., width, transform); + assert_eq!( + preview_time_from_x(content_left() + content_width(width) + 1., width, transform), + None + ); + assert_eq!( + preview_time_from_x(content_left() + 5., width, transform), + Some(0.) + ); + let middle = + preview_time_from_x(content_left() + content_width(width) / 2., width, transform); assert!((middle.unwrap() - 30.).abs() < 1e-6); } + /// A key press that runs past the next one is drawn, and hit, only up + /// to it; the last press keeps its full length. + #[test] + fn key_presses_end_where_the_next_one_begins() { + let press = |start: f64, end: f64| Segment { + start, + end, + lane: 0, + detail: SegmentDetail::Keyboard { + text: SharedString::new_static("a"), + typed: true, + keys: Arc::from(Vec::new()), + }, + }; + let segments = [press(1., 3.), press(0., 2.), press(5., 6.), press(1., 1.5)]; + let drawn = drawn_segments(TrackKind::Keyboard, &segments, 0.01); + assert_eq!(drawn[0].end, 3.); + assert!((drawn[1].end - 0.98).abs() < 1e-9); + assert_eq!(drawn[2].end, 6.); + // Two presses starting together don't clip each other. + assert_eq!(drawn[3].end, 1.5); + assert!(matches!( + drawn_segments(TrackKind::Zoom, &segments, 0.01), + std::borrow::Cow::Borrowed(_) + )); + } + // -- Labels ------------------------------------------------------------- + fn timeline_keys(labels: &[&'static str]) -> Vec { + labels + .iter() + .map(|label| TimelineKey::new(SharedString::new_static(label), false)) + .collect() + } + + /// A shortcut shows every key it has room for, whole, then a count of + /// the keys left out, then the keyboard glyph, then nothing. + #[test] + fn key_segments_show_whole_keys_and_count_the_rest() { + let keys = timeline_keys(&["⌘", "⇧", "P"]); + let all = keys.iter().map(|key| key.width).sum::() + KEY_CHIP_GAP * 2.; + assert_eq!( + fit_key_label(false, &keys, all), + KeyLabel::Keys { shown: 3, more: 0 } + ); + let two_and_more = keys[0].width + keys[1].width + KEY_CHIP_GAP * 2. + more_keys_width(1); + assert!(two_and_more < all); + assert_eq!( + fit_key_label(false, &keys, all - 1.), + KeyLabel::Keys { shown: 2, more: 1 } + ); + assert_eq!( + fit_key_label(false, &keys, two_and_more - 1.), + KeyLabel::Keys { shown: 1, more: 2 } + ); + let one_and_more = keys[0].width + KEY_CHIP_GAP + more_keys_width(2); + assert_eq!( + fit_key_label(false, &keys, one_and_more), + KeyLabel::Keys { shown: 1, more: 2 } + ); + assert_eq!( + fit_key_label(false, &keys, one_and_more - 1.), + KeyLabel::Glyph + ); + assert_eq!( + fit_key_label(false, &keys, KEY_GLYPH_MIN_WIDTH - 1.), + KeyLabel::Nothing + ); + // Whatever the width, the keys shown and the count fit inside it. + for width in 0..200 { + let width = width as f32; + if let KeyLabel::Keys { shown, more } = fit_key_label(false, &keys, width) { + let used = keys[..shown].iter().map(|key| key.width).sum::() + + KEY_CHIP_GAP * (shown - 1) as f32 + + if more > 0 { + KEY_CHIP_GAP + more_keys_width(more) + } else { + 0. + }; + assert!(used <= width, "{width}: {used}"); + assert_eq!(shown + more, keys.len()); + } + } + } + + /// Typed words read as text down to a letter and an ellipsis. + #[test] + fn typed_key_segments_read_as_words() { + let keys = timeline_keys(&["hello"]); + assert_eq!(fit_key_label(true, &keys, 200.), KeyLabel::Text); + assert_eq!( + fit_key_label(true, &keys, KEY_TEXT_MIN_WIDTH), + KeyLabel::Text + ); + assert_eq!( + fit_key_label(true, &keys, KEY_TEXT_MIN_WIDTH - 1.), + KeyLabel::Glyph + ); + assert_eq!(fit_key_label(false, &[], 40.), KeyLabel::Text); + } + + /// A key's cap grows with its legend and never shrinks below a square. + #[test] + fn key_chips_size_to_their_legend() { + assert_eq!(key_chip_width("P"), KEY_CHIP_MIN_WIDTH); + assert!(key_chip_width("⌘") >= KEY_CHIP_MIN_WIDTH); + assert!(key_chip_width("Backspace") > key_chip_width("Tab")); + assert!(key_chip_width("Shift") > KEY_CHIP_MIN_WIDTH); + assert_eq!(more_keys_width(9), more_keys_width(1)); + assert!(more_keys_width(10) > more_keys_width(9)); + } + + /// Words are typed text; a shortcut, or a special key alone, is keys. + #[test] + fn key_segments_tell_typing_from_shortcuts() { + let segment = |text: &str, keys: &[&str]| cap_project::KeyboardTrackSegment { + display_text: text.to_string(), + keys: keys + .iter() + .map(|key| cap_project::KeyPressDisplay { + key: key.to_string(), + time_offset: 0., + }) + .collect(), + id: String::new(), + start: 0., + end: 1., + fade_duration_override: None, + position_override: None, + color_override: None, + background_color_override: None, + font_size_override: None, + uppercase_override: None, + }; + assert!(is_typed_text(&segment("hello", &["h", "e", "l", "l", "o"]))); + assert!(is_typed_text(&segment("⌫e", &["Backspace", "e"]))); + assert!(is_typed_text(&segment("aa", &["a", "a"]))); + assert!(!is_typed_text(&segment("⌘C", &["Meta", "c"]))); + assert!(!is_typed_text(&segment("⇧", &["Shift"]))); + assert!(!is_typed_text(&segment("⏎", &["Enter"]))); + assert!(!is_typed_text(&segment("⎋", &["Escape", "Escape"]))); + } + /// `useSegmentVisibleBox`: a segment wider than the viewport has its true /// centre off screen, so the label anchors to the middle of the *visible* /// slice instead. @@ -4561,6 +5274,33 @@ mod tests { /// arrives: the row order is the source's mount order, multi-lane tracks /// contribute one row per used lane with the highest-priority lane first, /// and the two locked tracks are always there. + /// The timeline's keys print for the platform the recording was made + /// on, as the preview and every export draw them. + #[test] + fn timeline_keys_print_for_the_recording_platform() { + let config: ProjectConfiguration = serde_json::from_value(serde_json::json!({ + "timeline": { + "segments": [{ "start": 0.0, "end": 4.0, "timescale": 1.0, "recordingSegment": 0 }], + "zoomSegments": [], + "keyboardSegments": [ + { "id": "k", "start": 0.5, "end": 1.5, "displayText": "⌘C" } + ] + }, + "keyboard": { "settings": { "enabled": true, "style": "auto" } } + })) + .unwrap(); + let first_key = |platform: cap_project::Platform| { + let model = TimelineModel::build_for(&config, false, false, Some(&platform)); + match &model.keyboard[0].detail { + SegmentDetail::Keyboard { keys, .. } => keys[0].label.to_string(), + _ => unreachable!(), + } + }; + assert_eq!(first_key(cap_project::Platform::MacOS), "⌘"); + assert_eq!(first_key(cap_project::Platform::Windows), "Win"); + assert_eq!(first_key(cap_project::Platform::Linux), "Super"); + } + #[test] fn the_row_order_is_the_sources_mount_order() { let config: ProjectConfiguration = serde_json::from_value(serde_json::json!({ @@ -5209,7 +5949,7 @@ mod tests { let (first, last) = path .vertices .iter() - .filter(|vertex| f32::from(vertex.xy_position.y) < 35.) + .filter(|vertex| f32::from(vertex.xy_position.y) < 43. - SEGMENT_RADIUS) .map(|vertex| { range.0 + f64::from(f32::from(vertex.xy_position.x)) / 600. @@ -6063,11 +6803,11 @@ pub(crate) fn render_preparing_timeline( ), ); } - let mut veil = theme.editor.card; + let mut veil = theme.editor.window; veil.a = 0.88; - let mut edge = theme.editor.card; + let mut edge = theme.editor.window; edge.a = 0.35; - let mut clear = theme.editor.card; + let mut clear = theme.editor.window; clear.a = 0.0; div() .size_full() diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs index 1b7aa847079..dc2ce03d2ae 100644 --- a/apps/desktop-gpui/src/editor_window.rs +++ b/apps/desktop-gpui/src/editor_window.rs @@ -233,11 +233,161 @@ impl EditorVerticalLayout { } } -const PRESETS_MENU_WIDTH: f32 = 288.; +const PRESETS_MENU_WIDTH: f32 = 520.; const PRESET_SUBMENU_WIDTH: f32 = 208.; const PRESET_SUBMENU_HEIGHT: f32 = 5. * 32. + 4. * 2. + 8.; const POPOVER_WINDOW_INSET: f32 = 8.; +/// A look in miniature: its background, the recording as a rounded card at +/// the look's padding with its shadow and frame, and its keys in the +/// corner. +fn look_preview( + theme: &crate::theme::Theme, + look: &crate::looks::Look, + image: Option>, + selected: bool, +) -> gpui::Div { + use crate::looks::LookBackground; + use cap_rendering::keycaps::{KeycapStyle, KeycapTheme}; + use gpui::StyledImage as _; + const HEIGHT: f32 = 72.; + let rgb = |c: [u16; 3]| -> Hsla { + gpui::Rgba { + r: c[0] as f32 / 255., + g: c[1] as f32 / 255., + b: c[2] as f32 / 255., + a: 1., + } + .into() + }; + let pad = (look.padding as f32 * 1.05).clamp(5., 16.); + let radius = (look.rounding as f32 * 0.55).clamp(2., 8.); + let (app, line) = if look.dark_app { + (gpui::rgb(0x1d1d21), gpui::hsla(0., 0., 1., 0.14)) + } else { + (gpui::rgb(0xffffff), gpui::hsla(0., 0., 0., 0.08)) + }; + let frame = look.frame.map(|(style, _)| style); + let window = + div() + .absolute() + .left(px(pad)) + .right(px(pad)) + .top(px(pad * 0.8)) + .bottom(px(pad * 0.8)) + .rounded(px(radius)) + .bg(app) + .overflow_hidden() + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., (look.shadow.0 / 100.) * 0.55), + offset: gpui::point(px(0.), px(3.)), + blur_radius: px(10.), + spread_radius: px(0.), + inset: false, + }]) + .when_some(look.border, |this, _| { + this.border_1().border_color(gpui::hsla(0., 0., 1., 0.35)) + }) + .flex() + .flex_col() + .when( + matches!( + frame, + Some(cap_project::FrameStyle::MacOS | cap_project::FrameStyle::Browser) + ), + |this| { + this.child( + div() + .h(px(8.)) + .flex_none() + .flex() + .flex_row() + .items_center() + .gap(px(2.)) + .px(px(4.)) + .bg(gpui::rgb(0xececee)) + .children([0xff5f57_u32, 0xfebc2e, 0x28c840].map(|color| { + div().size(px(3.)).rounded_full().bg(gpui::rgb(color)) + })) + .when(frame == Some(cap_project::FrameStyle::Browser), |bar| { + bar.child( + div() + .ml(px(6.)) + .h(px(4.)) + .w(px(26.)) + .rounded(px(2.)) + .bg(gpui::rgb(0xffffff)), + ) + }), + ) + }, + ) + .child( + div() + .flex() + .flex_col() + .gap(px(3.)) + .p(px(5.)) + .child(div().h(px(3.)).w(px(28.)).rounded(px(2.)).bg(line)) + .child(div().h(px(3.)).w(px(40.)).rounded(px(2.)).bg(line)) + .child(div().h(px(3.)).w(px(22.)).rounded(px(2.)).bg(line)), + ); + let style = KeycapStyle::from_setting(&look.keys.style.map_or_else( + cap_project::KeyboardSettings::platform_style, + str::to_string, + )); + let key_theme = KeycapTheme::from_setting(look.keys.theme); + let keys = div() + .absolute() + .right(px(5.)) + .bottom(px(4.)) + .flex() + .flex_row() + .items_end() + .gap(px(2.)) + .child(crate::editor_tabs::key_chip_sized( + style, key_theme, "⌘", true, 14., + )) + .child(crate::editor_tabs::key_chip_sized( + style, key_theme, "K", false, 14., + )); + div() + .relative() + .h(px(HEIGHT)) + .w_full() + .rounded(px(9.)) + .overflow_hidden() + .map(|this| match (look.background, image) { + (LookBackground::Wallpaper(_), Some(image)) => this.child( + gpui::img(image) + .absolute() + .size_full() + .object_fit(gpui::ObjectFit::Cover), + ), + (LookBackground::Gradient { from, to, angle }, _) => this.bg(gpui::linear_gradient( + angle as f32, + gpui::linear_color_stop(rgb(from), 0.), + gpui::linear_color_stop(rgb(to), 1.), + )), + (LookBackground::Color(color), _) => this.bg(rgb(color)), + _ => this.bg(Hsla::from(theme.editor.ctl_active)), + }) + .child(window) + .child(keys) + .child( + div() + .absolute() + .inset_0() + .rounded(px(9.)) + .border_1() + .border_color(gpui::hsla(0., 0., 0., 0.08)) + .when(selected, |this| { + this.border_2() + .border_color(Hsla::from(theme.editor.accent)) + }), + ) +} + /// The per-preset submenu sits beside the menu, aligned with the row that /// summoned it: to the right when it fits, otherwise flipped to the left, and /// never past the window's bottom edge. @@ -330,20 +480,36 @@ fn seeked_playhead(time: f64, frame: u32, playing: bool) -> f64 { if playing { time } else { frame_seconds(frame) } } -/// The part of a frame layout the canvas overlay draws from. A preset -/// caption has no box on the canvas, and the overlay only draws while the -/// player is hovered or dragged, so a new caption under the playhead only -/// rebuilds the editor when its box could be on screen. Hovering the player -/// rebuilds the editor anyway, which picks up the latest box. +/// The part of a frame layout the canvas overlay draws from. Every caption, +/// preset or manual, has a box on the canvas, and the overlay only draws +/// while the player is hovered or dragged, so a new caption under the +/// playhead only rebuilds the editor when its box could be on screen. +/// Hovering the player rebuilds the editor anyway, which picks up the latest +/// box. fn canvas_hit_layout(layout: FrameLayout, overlay_live: bool) -> FrameLayout { FrameLayout { - caption: layout - .caption - .filter(|caption| overlay_live && caption.is_manual()), + caption: layout.caption.filter(|_| overlay_live), ..layout } } +/// Where each of a run of keys added by hand lands in `start..end`: back to +/// back as laid out, squeezed in proportion when less room than they asked +/// for was free, so every one lands inside it. +fn manual_run_spans(runs: &[cap_project::ManualKeyRun], start: f64, end: f64) -> Vec<(f64, f64)> { + let requested = runs + .last() + .map_or(0.0, |run| run.offset + run.length) + .max(1e-9); + let scale = ((end - start) / requested).min(1.0); + runs.iter() + .map(|run| { + let run_start = start + run.offset * scale; + (run_start, (run_start + run.length * scale).min(end)) + }) + .collect() +} + fn frame_layout_requires_editor_invalidation( first_frame: bool, playing: bool, @@ -405,6 +571,11 @@ pub struct ProjectSummary { /// stored it in `recording-meta.json`: the rate it captured at, where the /// probed rate in `recordings` is the track's average. pub recorded_fps: Vec<(u32, Option)>, + /// The platform the recording was made on, whose keyboard an older + /// project's keys follow, as the renderer draws them. + pub platform: Option, + /// Started empty rather than recorded: no key presses to generate from. + pub blank: bool, } /// Validate a `.cap` before handing it to `EditorInstance::new`. @@ -527,10 +698,17 @@ pub fn preflight(path: &std::path::Path) -> Result { }); } - let timeline = TimelineModel::build(&config, has_camera, multiple_recording_segments); + let timeline = TimelineModel::build_for( + &config, + has_camera, + multiple_recording_segments, + meta.platform.as_ref(), + ); let duration = timeline.total_duration; Ok(ProjectSummary { + platform: meta.platform.clone(), + blank: studio.is_blank(), recordings: recordings.clone(), pretty_name: meta.pretty_name.clone(), timeline, @@ -1797,6 +1975,8 @@ pub struct EditorWindow { add_track: Option, add_track_opens: usize, pub(crate) audio_picker: Option, + /// Type Keys is open, to add key presses from this time on. + pub(crate) key_prompt: Option, pub(crate) audio_picker_generation: u64, pub(crate) audio_import_pending: Option, audio_picker_request: Option, @@ -1840,6 +2020,11 @@ struct PresetsMenu { /// An open per-preset submenu: which row, and where it was summoned. submenu: Option<(PresetRow, gpui::Point)>, scroll: gpui::ScrollHandle, + /// Each look's wallpaper thumbnail, decoded when the menu opens. + look_images: Vec>>, + /// The look the project wears, found once when the menu opens: finding + /// it compares the project against every look, too much for each frame. + current_look: Option<&'static str>, } /// A row of the presets menu: the built-in Default, or a saved preset. @@ -2475,6 +2660,7 @@ impl EditorWindow { add_track: None, add_track_opens: 0, audio_picker: None, + key_prompt: None, audio_picker_generation: 0, audio_import_pending: None, audio_picker_request: None, @@ -2639,11 +2825,13 @@ impl EditorWindow { let system = std::mem::take(&mut self.timeline.system_waveforms); let imported = std::mem::take(&mut self.timeline.imported_waveforms); let waveform_paths = self.timeline.waveform_paths.clone(); - self.timeline = TimelineModel::build_with_lanes( + let platform = self.summary().and_then(|summary| summary.platform.clone()); + self.timeline = TimelineModel::build_with_lanes_for( &self.project, self.has_camera, self.multiple_clips, &self.tracks, + platform.as_ref(), ); self.timeline.mic_waveforms = mic; self.timeline.system_waveforms = system; @@ -3211,6 +3399,14 @@ impl EditorWindow { /// `projectHistory.undo()` -- reconcile the previous snapshot back over the /// store. The playhead is not moved and the selection is not restored; /// neither is in the snapshot (`editorState` is a separate store). + /// An undo or redo while the Presets menu is open changes which look the + /// project wears; the menu's outline follows it. + fn refresh_presets_menu_look(&mut self) { + if let Some(menu) = self.presets_menu.as_mut() { + menu.current_look = crate::looks::current_look(&self.project); + } + } + pub(crate) fn undo(&mut self, window: &mut Window, cx: &mut Context) { self.close_frame_controls(window, cx); self.end_field_edit(cx); @@ -3221,6 +3417,7 @@ impl EditorWindow { return; }; self.apply_history(config, window, cx); + self.refresh_presets_menu_look(); self.note_edit("undo", None); } @@ -3234,6 +3431,7 @@ impl EditorWindow { return; }; self.apply_history(config, window, cx); + self.refresh_presets_menu_look(); self.note_edit("redo", None); } @@ -4597,6 +4795,7 @@ impl EditorWindow { || self.style_presets.menu_open() || self.clips.has_open_menu() || self.audio_picker.is_some() + || self.key_prompt.is_some() || self.camera3d_picker || self.frame_controls.is_open() { @@ -4645,6 +4844,10 @@ impl EditorWindow { cx.notify(); return; } + if self.key_prompt.is_some() { + self.close_key_prompt(cx); + return; + } if self.camera3d_picker { self.close_camera3d_picker(cx); return; @@ -5298,7 +5501,7 @@ impl EditorWindow { let position = self.view.transform.position; let total = self.total_duration(); let hit = edits::hit_test( - self.timeline.segments(kind), + &timeline::drawn_segments(kind, self.timeline.segments(kind), secs_per_pixel), lane, x, position, @@ -5366,6 +5569,12 @@ impl EditorWindow { self.seek_to_time(press_time, cx); self.view.preview_time = None; } + } else if kind == TrackKind::Keyboard { + // Type Keys opens where the track was pressed. + cx.stop_propagation(); + self.seek_to_time(press_time, cx); + self.view.preview_time = None; + self.open_key_prompt(press_time, window, cx); } else if matches!( kind, TrackKind::Text | TrackKind::Shape | TrackKind::Mask | TrackKind::Scene @@ -5431,6 +5640,13 @@ impl EditorWindow { return; } let (start, end) = (segments[index].start, segments[index].end); + // A key press's end handle sits where its drawn end is, so the trim + // starts there. + let drawn_end = if kind == TrackKind::Keyboard { + timeline::keyboard_visible_ends(segments, secs_per_pixel)[index].unwrap_or(end) + } else { + end + }; // The clip's own recording-domain edges, which its handles move. let source_edges = self @@ -5474,7 +5690,7 @@ impl EditorWindow { let min = edits::min_segment_duration(kind, secs_per_pixel); let bounds = edits::trim_end_bounds(segments, lane, index, min, total); Some(DragKind::TrimEnd { - end, + end: drawn_end, bounds: audio_segment.map_or(bounds, |segment| { edits::audio_trim_end_bounds(segment, bounds) }), @@ -7148,6 +7364,139 @@ impl EditorWindow { true } + /// Turns what was typed into Type Keys into key presses from `at`, + /// sized to strike every key and be read, then moves the playhead past + /// it so the next entry follows on. A sentence pops up a word at a time, + /// a key at a time, or whole, as the tab's "Show as" says; `cmd+shift+p` + /// always shows as its keys. + pub(crate) fn add_typed_keys( + &mut self, + input: &str, + at: f64, + window: &mut Window, + cx: &mut Context, + ) -> bool { + let text = cap_project::manual_key_text(input); + if text.trim().is_empty() { + return false; + } + let split = crate::editor_tabs::keyboard_settings(&self.project).typing_split; + let runs = cap_project::manual_key_runs(&text, &split); + let Some(length) = runs.last().map(|run| run.offset + run.length) else { + return false; + }; + let start = at; + let Some((_, end)) = self.add_manual_keys(start + length / 2.0, &runs, false, window, cx) + else { + return false; + }; + self.seek_to_time((end + 0.15).min(self.total_duration()), cx); + true + } + + /// Adds key presses by hand around `time`: `runs` back to back, each + /// striking its own text, and returns where they landed. `select` opens + /// the first one's panel. + fn add_manual_keys( + &mut self, + time: f64, + runs: &[cap_project::ManualKeyRun], + select: bool, + window: &mut Window, + cx: &mut Context, + ) -> Option<(f64, f64)> { + let requested = runs.last().map(|run| run.offset + run.length)?; + let durations = self.clip_display_durations.clone(); + if !edits::ensure_timeline(&mut self.project, &durations) { + return None; + } + let total = self.total_duration(); + let length = requested.min(total); + let placement = self.project.timeline.as_ref().and_then(|timeline| { + edits::find_placement(&timeline.keyboard_segments, time, length, total) + }); + let Some((start, end)) = placement else { + self.show_notice(tr::editor::window::no_room_for_key(), window, cx); + return None; + }; + let next = self.project.timeline.as_ref().map_or(0, |timeline| { + timeline + .keyboard_segments + .iter() + .filter_map(|segment| { + segment + .id + .strip_prefix(cap_project::MANUAL_KEYBOARD_PREFIX)? + .parse::() + .ok() + }) + .max() + .map_or(0, |last| last + 1) + }); + let id = format!("{}{next}", cap_project::MANUAL_KEYBOARD_PREFIX); + let spans = manual_run_spans(runs, start, end); + let showed_track = self.tracks.keyboard; + self.tracks.keyboard = true; + let added = self.edit_timeline_project( + |project| { + let keyboard = project + .keyboard + .get_or_insert_with(cap_project::KeyboardData::default); + keyboard.settings.enabled = true; + let Some(timeline) = project.timeline.as_mut() else { + return false; + }; + for (index, (run, &(run_start, run_end))) in runs.iter().zip(&spans).enumerate() { + let segment = cap_project::KeyboardTrackSegment { + id: if index == 0 { + id.clone() + } else { + format!( + "{}{}", + cap_project::MANUAL_KEYBOARD_PREFIX, + next + index as u64 + ) + }, + start: run_start, + end: run_end, + display_text: run.text.clone(), + keys: cap_project::manual_key_presses(&run.text), + fade_duration_override: None, + position_override: None, + color_override: None, + background_color_override: None, + font_size_override: None, + uppercase_override: None, + }; + let at = timeline + .keyboard_segments + .partition_point(|other| other.start <= run_start); + timeline.keyboard_segments.insert(at, segment); + } + true + }, + window, + cx, + ); + if !added { + self.tracks.keyboard = showed_track; + return None; + } + if select { + let index = self.project.timeline.as_ref().and_then(|timeline| { + timeline + .keyboard_segments + .iter() + .position(|segment| segment.id == id) + }); + if let Some(index) = index { + self.set_selection(Some(Selection::single(TrackKind::Keyboard, index)), cx); + } + } + self.note_edit("add-keyboard", Some(TrackKind::Keyboard)); + Some((start, end)) + } + /// `finish(e)`: resume the history, and -- if the drag never promoted -- /// select instead, which also moves the playhead /// (`props.handleUpdatePlayhead(e)`). @@ -7278,7 +7627,7 @@ impl EditorWindow { let x = self.content_x(window_x); let position = self.view.transform.position; let hit = edits::hit_test( - self.timeline.segments(kind), + &timeline::drawn_segments(kind, self.timeline.segments(kind), secs_per_pixel), lane, x, position, @@ -8667,18 +9016,114 @@ impl EditorWindow { let viewport = window.viewport_size(); // `gutter={8}`, opening downward from the header trigger; `w-72`. let x = f32::from(anchor.x) - .min(f32::from(viewport.width) - 288. - 12.) + .min(f32::from(viewport.width) - PRESETS_MENU_WIDTH - 12.) .max(12.); let y = f32::from(anchor.y) + 8.; + let look_images = crate::looks::LOOKS + .iter() + .map(|look| match look.background { + crate::looks::LookBackground::Wallpaper(id) => { + crate::editor_sidebar::decode_wallpaper_thumbnail(id) + } + _ => None, + }) + .collect(); self.presets_menu = Some(PresetsMenu { origin: point(px(x), px(y)), store: crate::presets::PresetsStore::load(), submenu: None, scroll: gpui::ScrollHandle::new(), + look_images, + current_look: crate::looks::current_look(&self.project), }); cx.notify(); } + /// A built-in look: its styling over the project, one undo entry. + fn apply_look_at(&mut self, index: usize, window: &mut Window, cx: &mut Context) { + let Some(look) = crate::looks::LOOKS.get(index) else { + return; + }; + let next = crate::looks::apply_look(look, &self.project); + tracing::info!(look = look.id, "look applied"); + self.apply_project_preset(next, window, cx); + } + + /// The looks gallery atop the presets menu: four across, each a small + /// picture of the look, its name, and its one line on hover. + fn render_looks_gallery(&self, menu: &PresetsMenu, cx: &mut Context) -> gpui::AnyElement { + let theme = self.theme; + let editor = theme.editor; + let current = menu.current_look; + let cards: Vec = crate::looks::LOOKS + .iter() + .enumerate() + .map(|(index, look)| { + let selected = current == Some(look.id); + let image = menu.look_images.get(index).cloned().flatten(); + let description = (look.description)(); + div() + .id(SharedString::from(format!("look-{}", look.id))) + .flex_1() + .min_w_0() + .flex() + .flex_col() + .gap(px(7.)) + .p(px(4.)) + .rounded(px(12.)) + .cursor_pointer() + .hover(move |style| style.bg(Hsla::from(editor.ctl))) + .tooltip(move |_, cx| crate::ui::Tooltip::new(&theme, description).view(cx)) + .on_click(cx.listener(move |this, _, window, cx| { + this.apply_look_at(index, window, cx); + })) + .child(look_preview(&theme, look, image, selected)) + .child( + div() + .px(px(2.)) + .text_size(px(12.5)) + .line_height(px(15.)) + .text_color(Hsla::from(if selected { + editor.text_1 + } else { + editor.text_2 + })) + .truncate() + .child((look.name)()), + ) + .into_any_element() + }) + .collect(); + let mut rows = Vec::new(); + let mut cards = cards.into_iter(); + loop { + let row: Vec<_> = cards.by_ref().take(4).collect(); + if row.is_empty() { + break; + } + rows.push(div().flex().flex_row().gap(px(4.)).children(row)); + } + div() + .flex() + .flex_col() + .gap(px(4.)) + .px(px(8.)) + .pt(px(10.)) + .pb(px(8.)) + .border_b_1() + .border_color(Hsla::from(editor.line)) + .child( + div() + .px(px(6.)) + .pb(px(4.)) + .text_size(px(12.)) + .text_color(Hsla::from(editor.text_2)) + .child(tr::editor::window::looks::title()), + ) + .children(rows) + .into_any_element() + } + /// Apply row `index`: the preset's config with the current timeline and /// clips kept -- one undo entry, like any other config edit. fn apply_preset_at(&mut self, index: usize, window: &mut Window, cx: &mut Context) { @@ -8929,6 +9374,12 @@ impl EditorWindow { self.add_overlay_segment(kind, window, cx); } TrackKind::Shape => self.add_shape_at_playhead(window, cx), + TrackKind::Keyboard => { + self.tracks.keyboard = true; + self.rebuild_timeline(); + let at = self.playhead; + self.open_key_prompt(at, window, cx); + } _ => {} } cx.notify(); @@ -9492,6 +9943,10 @@ impl EditorWindow { window: &mut Window, cx: &mut Context, ) { + if spec == "type-keys" { + self.open_key_prompt(0.0, window, cx); + return; + } self.open_add_track(window, cx); let kind = match spec { "style" => TrackKind::Style, @@ -9715,8 +10170,8 @@ impl EditorWindow { .gap(px(8.)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .p(px(6.)) .shadow(theme.editor.pop_shadow()) .on_mouse_down( @@ -10168,10 +10623,11 @@ impl EditorWindow { .whitespace_nowrap() .cursor_pointer() .bg(if selected { - Hsla::from(theme.editor.ctl_active) + theme.editor.pill_fill() } else { gpui::transparent_black() }) + .when(selected, |this| this.shadow(theme.editor.pill_shadow())) .text_color(Hsla::from(if selected { theme.editor.text_1 } else { @@ -10202,8 +10658,8 @@ impl EditorWindow { .gap(px(8.)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .p(px(6.)) .shadow(theme.editor.pop_shadow()) .occlude() @@ -10620,8 +11076,8 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child( @@ -10889,10 +11345,19 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .overflow_hidden() + .child(self.render_looks_gallery(menu, cx)) + .child( + div() + .px(px(14.)) + .pt(px(4.)) + .text_size(px(12.)) + .text_color(Hsla::from(theme.editor.text_2)) + .child(tr::editor::window::looks::saved()), + ) .child( div() .id("presets-list") @@ -11087,8 +11552,8 @@ impl EditorWindow { .gap(px(2.)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .child( menu_row( @@ -11301,8 +11766,8 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child( @@ -11658,17 +12123,15 @@ impl EditorWindow { .text_size(px(13.)) .font_weight(FontWeight::MEDIUM) .text_color(gpui::white()) - // gpui has no box-shadow, so the `inset 0 1px 0 rgba(255,255,255, - // .22)` highlight is a painted hairline instead. - .child( - div() - .absolute() - .top_0() - .left_0() - .right_0() - .h(px(1.)) - .bg(gpui::hsla(0., 0., 1., 0.22)), - ) + // The lit top edge is an inset shadow, so it follows the rounded + // corners instead of running past them. + .shadow(vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., 0.22), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }]) .child( svg() .path("icons/upload-arrow.svg") @@ -11899,19 +12362,16 @@ impl EditorWindow { .rev() .copied() .collect(); - let selected_bg = if theme.is_dark() { - gpui::hsla(0., 0., 1., 0.12) - } else { - Hsla::from(editor.card) - }; + let selected_bg = editor.pill_fill(); div() .id("preview-quality") .flex() .flex_row() .flex_none() .p(px(2.)) - .rounded(px(8.)) + .rounded(px(9.)) .bg(Hsla::from(editor.ctl)) + .shadow(editor.trough_ring()) .children(qualities.iter().enumerate().map(|(index, quality)| { let quality = *quality; let selected = quality == self.preview_quality; @@ -11926,12 +12386,14 @@ impl EditorWindow { .h(px(24.)) .min_w(px(24.)) .px(px(10.)) - .rounded(px(6.)) + .rounded(px(7.)) .text_size(px(12.)) .font_weight(FontWeight::MEDIUM) .map(|this| { if selected { - this.bg(selected_bg).text_color(Hsla::from(editor.text_1)) + this.bg(selected_bg) + .shadow(editor.pill_shadow()) + .text_color(Hsla::from(editor.text_1)) } else { this.text_color(Hsla::from(editor.text_2)) .cursor_pointer() @@ -11989,7 +12451,7 @@ impl EditorWindow { .flex_1() .min_h_0() .overflow_hidden() - .bg(Hsla::from(theme.editor.card)) + .bg(Hsla::from(theme.editor.window)) .capture_any_mouse_down(cx.listener(|this, event: &MouseDownEvent, window, cx| { if event.button == MouseButton::Left { this.focus_root(window, cx); @@ -12340,14 +12802,7 @@ impl EditorWindow { ) .row_width(px(72.)) .row_height(px(20.)) - .track(px(3.), Hsla::from(editor.ctl_active)) - .fill(Hsla::from(editor.accent)) - .thumb( - px(12.), - Hsla::from(editor.thumb), - Some(gpui::hsla(0., 0., 0., 0.12)), - ) - .thumb_shadow() + .editor(&theme, px(12.), false) .on_drag_start(cx.listener( |this, event: &MouseDownEvent, window, cx| { this.zoom_slider_drag = true; @@ -12749,33 +13204,68 @@ impl EditorWindow { self.view, viewport_width, )) - // `TrackManager`'s trigger (`TL/TrackManager.tsx`): a solid pill - // in the gutter (`bg-ed-text-1 text-ed-card`, so black on the - // light theme and white on the dark one). Its plus turns into an - // x while the tray is open. + // `absolute inset-y-0 right-0 z-40` at + // `left: TRACK_GUTTER - START_SNAP_PX` -- the ruler's scrub + // surface, which reaches into the snap-to-zero zone so hitting + // 0:00 is not a battle (`TL/index.tsx:1237-1244`). + .when(live, |this| { + this.child( + div() + .absolute() + .top_0() + .bottom_0() + .left(px(TRACK_GUTTER - START_SNAP_PX as f32)) + .right_0() + .cursor(gpui::CursorStyle::ResizeLeftRight) + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, event: &MouseDownEvent, window, cx| { + cx.stop_propagation(); + this.timeline_mouse_down(event, true, window, cx); + }), + ), + ) + }) + // `TrackManager`'s trigger. Drawn after the scrub surface so a + // press anywhere on it opens the tray, even where a long label + // reaches past `TRACK_GUTTER - START_SNAP_PX`. .child( div() .id("add-track") .absolute() .bottom_0() .left(px(4.)) - .h(px(24.)) + .h(px(TIMELINE_HEADER_HEIGHT)) .flex() .flex_row() .flex_none() .items_center() .justify_start() - .gap(px(5.)) - .pl(px(6.)) - .pr(px(8.)) - .rounded(px(6.)) + .gap(px(6.)) + .pl(px(9.)) + .pr(px(12.)) + .rounded(px(8.)) .cursor_pointer() - .bg(Hsla::from(theme.editor.text_1)) - .hover(|style| style.opacity(0.85)) - .active(|style| style.opacity(0.7)) - .text_size(px(12.)) + .bg(Hsla::from(theme.editor.accent).opacity(if theme.is_dark() { + 0.2 + } else { + 0.12 + })) + .hover(|style| { + style.bg(Hsla::from(theme.editor.accent).opacity(if theme.is_dark() { + 0.28 + } else { + 0.18 + })) + }) + .active(|style| style.opacity(0.8)) + .text_size(px(13.)) .font_weight(FontWeight::MEDIUM) - .text_color(Hsla::from(theme.editor.card)) + .text_color(Hsla::from(if theme.is_dark() { + theme.editor.accent_2 + } else { + theme.editor.accent + })) .child( svg() .path(if self.add_track.is_some() { @@ -12783,9 +13273,9 @@ impl EditorWindow { } else { "icons/plus.svg" }) - .size(px(12.)) + .size(px(14.)) .flex_none() - .text_color(Hsla::from(theme.editor.card)), + .text_color(Hsla::from(theme.editor.accent)), ) .child( div() @@ -12798,28 +13288,6 @@ impl EditorWindow { this.open_add_track(window, cx); })), ) - // `absolute inset-y-0 right-0 z-40` at - // `left: TRACK_GUTTER - START_SNAP_PX` -- the ruler's scrub - // surface, which reaches into the snap-to-zero zone so hitting - // 0:00 is not a battle (`TL/index.tsx:1237-1244`). - .when(live, |this| { - this.child( - div() - .absolute() - .top_0() - .bottom_0() - .left(px(TRACK_GUTTER - START_SNAP_PX as f32)) - .right_0() - .cursor(gpui::CursorStyle::ResizeLeftRight) - .on_mouse_down( - MouseButton::Left, - cx.listener(move |this, event: &MouseDownEvent, window, cx| { - cx.stop_propagation(); - this.timeline_mouse_down(event, true, window, cx); - }), - ), - ) - }) .children(self.render_marker_flags(viewport_width, cx)) } @@ -13554,7 +14022,6 @@ fn dismiss_indexed_sidebar_menu(menu: &mut Option) | SidebarMenu::CaptionAnimation | SidebarMenu::CaptionWeight | SidebarMenu::KeyboardFont - | SidebarMenu::KeyboardPosition | SidebarMenu::KeyboardWeight => false, }); if indexed { @@ -13853,11 +14320,6 @@ impl Render for EditorWindow { .flex_1() .min_w_0() .min_h_0() - .rounded(px(12.)) - .border_1() - .border_color(self.card_line()) - .bg(self.panel_bg()) - .shadow(theme.editor.card_shadow()) .overflow_hidden() .child(self.render_player(cx)), ) @@ -13881,11 +14343,11 @@ impl Render for EditorWindow { .child( div() .size_full() - .rounded(px(12.)) + // Invisible, but kept: the timeline's + // hit-testing still offsets by + // `TIMELINE_CARD_BORDER`. .border_1() - .border_color(self.card_line()) - .bg(self.panel_bg()) - .shadow(theme.editor.card_shadow()) + .border_color(gpui::transparent_black()) .overflow_hidden() .child(self.timeline_view.clone().cached( StyleRefinement::default().w_full().h_full(), @@ -14769,6 +15231,7 @@ mod tests { max_width: 1000., padding_scale: 1., even: None, + caption_size: 0, }), }); editor.canvas_drag_rect = Some(rect); @@ -15527,6 +15990,8 @@ mod tests { store: store.clone(), submenu: None, scroll: gpui::ScrollHandle::new(), + look_images: Vec::new(), + current_look: None, }); }); let mut samples = Vec::new(); @@ -16222,7 +16687,7 @@ mod tests { #[test] fn responsive_editor_hugs_its_rows_and_still_fits_a_short_viewport() { // The four-row scratch project: chrome plus four rows and their gaps. - assert_eq!(timeline::hug_height(4), 246.); + assert_eq!(timeline::hug_height(4), 268.); assert_eq!(timeline::hug_height(0), timeline::MIN_HUG_HEIGHT); for rows in 1..12usize { let hug = timeline::hug_height(rows); @@ -16351,7 +16816,6 @@ mod tests { SidebarMenu::CaptionAnimation, SidebarMenu::CaptionWeight, SidebarMenu::KeyboardFont, - SidebarMenu::KeyboardPosition, SidebarMenu::KeyboardWeight, ] { let mut menu = open_sidebar_menu_for_test(kind); @@ -16493,6 +16957,8 @@ mod tests { .unwrap(); editor.set_summary( ProjectSummary { + platform: None, + blank: false, recordings: Arc::new(ProjectRecordingsMeta { segments: Vec::new(), }), @@ -16641,6 +17107,8 @@ mod tests { .unwrap(); editor.set_summary( ProjectSummary { + platform: None, + blank: false, recordings: Arc::new(ProjectRecordingsMeta { segments: Vec::new(), }), @@ -17630,12 +18098,32 @@ mod tests { )); } - /// Scrubbing across preset captions changes only the caption box, which - /// the canvas never draws, so it costs no editor rebuild. A manual - /// caption's box is a canvas hit target, so its change does while the - /// overlay is live, and not while the pointer is elsewhere. + /// Every caption's box is a canvas hit target, so a change to it rebuilds + /// the editor while the overlay is live, and not while the pointer is + /// elsewhere. + /// A sentence longer than the room for it is squeezed to fit, every key + /// landing inside the clip with its start before its end. + #[test] + fn typed_keys_longer_than_the_room_are_squeezed_in() { + let runs = cap_project::manual_key_runs("hello world", "keys"); + let requested = runs.last().map(|run| run.offset + run.length).unwrap(); + assert!(requested > 2.0); + let spans = manual_run_spans(&runs, 0.0, 2.0); + assert_eq!(spans.len(), runs.len()); + for (start, end) in &spans { + assert!( + *start >= 0.0 && start < end && *end <= 2.0 + 1e-9, + "{start} {end}" + ); + } + assert!((spans.last().unwrap().1 - 2.0).abs() < 1e-9); + // With room to spare, nothing is stretched. + let roomy = manual_run_spans(&runs, 1.0, 100.0); + assert!((roomy[0].1 - roomy[0].0 - runs[0].length).abs() < 1e-9); + } + #[test] - fn only_a_manual_caption_box_changes_the_canvas_layout() { + fn a_caption_box_changes_the_canvas_layout_only_while_the_overlay_is_live() { let base = FrameLayout { display: [0., 0., 1920., 1080.], camera: None, @@ -17650,11 +18138,15 @@ mod tests { caption: Some(cap_rendering::CaptionFrameBox::new(bounds, "manual")), ..base }; - assert_eq!(canvas_hit_layout(preset([0., 0., 10., 10.]), true), base); - assert_eq!( + assert_ne!(canvas_hit_layout(preset([0., 0., 10., 10.]), true), base); + assert_ne!( canvas_hit_layout(preset([0., 0., 10., 10.]), true), canvas_hit_layout(preset([5., 5., 50., 20.]), true) ); + assert_eq!( + canvas_hit_layout(preset([0., 0., 10., 10.]), false), + canvas_hit_layout(preset([5., 5., 50., 20.]), false) + ); assert_ne!( canvas_hit_layout(manual([0., 0., 10., 10.]), true), canvas_hit_layout(manual([5., 5., 50., 20.]), true) diff --git a/apps/desktop-gpui/src/editor_window/frame.rs b/apps/desktop-gpui/src/editor_window/frame.rs index 7e78c983087..c4847062944 100644 --- a/apps/desktop-gpui/src/editor_window/frame.rs +++ b/apps/desktop-gpui/src/editor_window/frame.rs @@ -409,8 +409,8 @@ impl EditorWindow { .overflow_y_scroll() .rounded(px(12.)) .border_1() - .border_color(gpui::Hsla::from(theme.editor.line)) - .bg(gpui::Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child( diff --git a/apps/desktop-gpui/src/editor_window/loading.rs b/apps/desktop-gpui/src/editor_window/loading.rs index ec70ee0e483..0ef81a1a4fc 100644 --- a/apps/desktop-gpui/src/editor_window/loading.rs +++ b/apps/desktop-gpui/src/editor_window/loading.rs @@ -40,7 +40,7 @@ pub(super) fn preview(theme: &Theme, animated: bool) -> AnyElement { .items_center() .justify_center() .p(px(24.)) - .bg(Hsla::from(theme.editor.card)) + .bg(Hsla::from(theme.editor.window)) .child(pulse( "editor-preview-skeleton", div() diff --git a/apps/desktop-gpui/src/editor_window/markers.rs b/apps/desktop-gpui/src/editor_window/markers.rs index ff19a98310c..a9523bfad90 100644 --- a/apps/desktop-gpui/src/editor_window/markers.rs +++ b/apps/desktop-gpui/src/editor_window/markers.rs @@ -888,8 +888,8 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .overflow_hidden() } diff --git a/apps/desktop-gpui/src/editor_window/style_preset_ui.rs b/apps/desktop-gpui/src/editor_window/style_preset_ui.rs index a7724e245c4..a04961053f8 100644 --- a/apps/desktop-gpui/src/editor_window/style_preset_ui.rs +++ b/apps/desktop-gpui/src/editor_window/style_preset_ui.rs @@ -644,8 +644,8 @@ impl EditorWindow { .flex_col() .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .overflow_hidden() .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) @@ -751,8 +751,8 @@ impl EditorWindow { .gap(px(2.)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .on_mouse_down(MouseButton::Left, |_, _, cx| cx.stop_propagation()) .child( diff --git a/apps/desktop-gpui/src/editor_window/transitions.rs b/apps/desktop-gpui/src/editor_window/transitions.rs index 0874bda6c03..3dc83ee7f69 100644 --- a/apps/desktop-gpui/src/editor_window/transitions.rs +++ b/apps/desktop-gpui/src/editor_window/transitions.rs @@ -546,8 +546,8 @@ impl EditorWindow { .justify_center() .cursor_pointer() .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) .hover(|chip| { chip.border_color(accent) @@ -687,10 +687,11 @@ impl EditorWindow { .whitespace_nowrap() .cursor_pointer() .bg(if selected { - Hsla::from(theme.editor.ctl_active) + theme.editor.pill_fill() } else { gpui::transparent_black() }) + .when(selected, |this| this.shadow(theme.editor.pill_shadow())) .text_color(Hsla::from(if selected { theme.editor.text_1 } else { @@ -723,8 +724,8 @@ impl EditorWindow { .gap(px(10.)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .p(px(8.)) .shadow(theme.editor.pop_shadow()) .occlude() diff --git a/apps/desktop-gpui/src/i18n/dialogs_surface_tests.rs b/apps/desktop-gpui/src/i18n/dialogs_surface_tests.rs index 22dc8aeddcc..b4ddc7906ab 100644 --- a/apps/desktop-gpui/src/i18n/dialogs_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/dialogs_surface_tests.rs @@ -38,6 +38,7 @@ const FORMATTED: &[&str] = &[ "dialog.clipboard.unavailable_error", "dialog.file.failed", "import.audio_failed", + "import.blank_failed", "import.image_failed", "import.video_failed", "import.progress.converting", @@ -322,6 +323,7 @@ fn dialog_surfaces_match_the_literals_they_replaced() { ), (tr::auth::sign_in_failed(), "Sign in failed"), (tr::import::error_title(), "Import Error"), + (tr::import::blank_name(), "Untitled"), (tr::import::unsupported(), "Unsupported media file."), (tr::import::filter::image(), "Image Files"), (tr::import::filter::media(), "Media Files"), @@ -1068,6 +1070,10 @@ fn assembled_dialog_messages_match_the_code_they_replaced() { ] { assert_eq!(formatted, format!("Failed to import {media}: {message}")); } + assert_eq!( + tr::import::blank_failed(message), + format!("Failed to create a project: {message}") + ); assert_eq!( tr::dialog::capture::shortcut_hint("Ctrl+Shift+F9"), "Press and release Ctrl+Shift+F9 to start. Use the same shortcut to stop. This temporary shortcut does not change your settings." diff --git a/apps/desktop-gpui/src/i18n/editor_panels_surface_tests.rs b/apps/desktop-gpui/src/i18n/editor_panels_surface_tests.rs index c4661c14d9d..544109c3151 100644 --- a/apps/desktop-gpui/src/i18n/editor_panels_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_panels_surface_tests.rs @@ -497,7 +497,10 @@ fn editor_panels_match_the_literals_they_replaced() { "Background Blur", ), (tr::editor::sidebar::layout::padding(), "Padding"), - (tr::editor::sidebar::layout::padding_style(), "Padding Style"), + ( + tr::editor::sidebar::layout::padding_style(), + "Padding Style", + ), ( tr::editor::sidebar::layout::custom_position(), "Custom screen position (dragged on canvas)", @@ -593,6 +596,51 @@ fn editor_panels_match_the_literals_they_replaced() { (tr::editor::sidebar::cursor::strength(), "Strength"), (tr::editor::sidebar::cursor::ripple_size(), "Size"), (tr::editor::sidebar::cursor::duration(), "Duration"), + ( + tr::editor::sidebar::input_sounds::click_sounds(), + "Click Sounds", + ), + (tr::editor::sidebar::input_sounds::deep(), "Deep"), + ( + tr::editor::sidebar::input_sounds::deep_description(), + "Linear switches with a low, rounded thock.", + ), + ( + tr::editor::sidebar::input_sounds::key_sounds(), + "Key Sounds", + ), + (tr::editor::sidebar::input_sounds::laptop(), "Laptop"), + ( + tr::editor::sidebar::input_sounds::laptop_description(), + "Soft, shallow laptop keys.", + ), + ( + tr::editor::sidebar::input_sounds::mechanical(), + "Mechanical", + ), + ( + tr::editor::sidebar::input_sounds::mechanical_description(), + "Crisp switches with a bright, tactile clack.", + ), + (tr::editor::sidebar::input_sounds::mouse(), "Mouse"), + ( + tr::editor::sidebar::input_sounds::mouse_description(), + "The press and release of a mouse button.", + ), + (tr::editor::sidebar::input_sounds::trackpad(), "Trackpad"), + ( + tr::editor::sidebar::input_sounds::trackpad_description(), + "The softer click of a laptop trackpad.", + ), + ( + tr::editor::sidebar::input_sounds::typewriter(), + "Typewriter", + ), + ( + tr::editor::sidebar::input_sounds::typewriter_description(), + "Type bars striking, with a carriage return on enter.", + ), + (tr::editor::sidebar::input_sounds::volume(), "Volume"), (tr::editor::sidebar::animated_gradient::still(), "Still"), ( tr::editor::sidebar::animated_gradient::name_placeholder(), @@ -635,6 +683,19 @@ fn editor_panels_match_the_literals_they_replaced() { "Add a colour", ), (tr::editor::sidebar::animated_gradient::motion(), "Motion"), + (tr::editor::sidebar::animated_gradient::style(), "Style"), + ( + tr::editor::sidebar::animated_gradient::style_dither(), + "Dither", + ), + ( + tr::editor::sidebar::animated_gradient::style_smooth(), + "Smooth", + ), + ( + tr::editor::sidebar::animated_gradient::control::dither_size(), + "Dither Size", + ), ( tr::editor::sidebar::animated_gradient::fine_tune(), "Fine-tune", @@ -808,30 +869,15 @@ fn editor_panels_match_the_literals_they_replaced() { (tr::editor::crop::full(), "Full"), (tr::editor::crop::loading_frame(), "Loading frame…"), (tr::editor::crop::rendering_preview(), "Rendering preview…"), - ( - tr::editor::panel::style_presets::apply(), - "Apply", - ), - ( - tr::editor::panel::style_presets::create(), - "Save", - ), + (tr::editor::panel::style_presets::apply(), "Apply"), + (tr::editor::panel::style_presets::create(), "Save"), ( tr::editor::panel::style_presets::create_title(), "Save Style Preset", ), - ( - tr::editor::panel::style_presets::custom(), - "Custom style", - ), - ( - tr::editor::panel::style_presets::default_badge(), - "Default", - ), - ( - tr::editor::panel::style_presets::delete(), - "Delete", - ), + (tr::editor::panel::style_presets::custom(), "Custom style"), + (tr::editor::panel::style_presets::default_badge(), "Default"), + (tr::editor::panel::style_presets::delete(), "Delete"), ( tr::editor::panel::style_presets::delete_title(), "Delete Style Preset", @@ -844,22 +890,13 @@ fn editor_panels_match_the_literals_they_replaced() { tr::editor::panel::style_presets::empty_help(), "Save a style to reuse it in any project.", ), - ( - tr::editor::panel::style_presets::move_down(), - "Move down", - ), - ( - tr::editor::panel::style_presets::move_up(), - "Move up", - ), + (tr::editor::panel::style_presets::move_down(), "Move down"), + (tr::editor::panel::style_presets::move_up(), "Move up"), ( tr::editor::panel::style_presets::remove_default(), "Remove as default", ), - ( - tr::editor::panel::style_presets::rename(), - "Rename", - ), + (tr::editor::panel::style_presets::rename(), "Rename"), ( tr::editor::panel::style_presets::rename_title(), "Rename Style Preset", @@ -876,10 +913,7 @@ fn editor_panels_match_the_literals_they_replaced() { tr::editor::panel::style_presets::set_default(), "Set as default", ), - ( - tr::editor::panel::style_presets::tooltip(), - "Style presets", - ), + (tr::editor::panel::style_presets::tooltip(), "Style presets"), ( tr::editor::panel::style_presets::update(), "Update with current style", diff --git a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs index d514de160f6..000efad7e91 100644 --- a/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/editor_shell_surface_tests.rs @@ -20,6 +20,7 @@ pub(super) fn is_editor_shell_surface_key(key: &str) -> bool { } const FORMATTED: &[&str] = &[ + "editor.tabs.keyboard.type_keys_summary", "editor.transcript.captions_save_failed", "editor.transcript.fillers.remove_all", "editor.transcript.pauses.summary", @@ -228,6 +229,101 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { ), (tr::editor::export::done::link_copied(), "Link copied"), (tr::editor::export::done::open_file(), "Open File"), + (tr::editor::export::done::copy_file(), "Copy to Clipboard"), + (tr::editor::tabs::keyboard::show_typing(), "Show Typing"), + (tr::editor::window::looks::launch_day(), "Launch Day"), + ( + tr::editor::window::looks::launch_day_description(), + "A vivid gradient, snappy zooms and bold captions.", + ), + (tr::editor::window::looks::mac_native(), "Mac Native"), + ( + tr::editor::window::looks::mac_native_description(), + "A macOS window on Tahoe with white Mac keys.", + ), + (tr::editor::window::looks::midnight(), "Midnight"), + ( + tr::editor::window::looks::midnight_description(), + "A deep, dark backdrop with a crisp edge.", + ), + (tr::editor::window::looks::minimal(), "Minimal"), + ( + tr::editor::window::looks::minimal_description(), + "A quiet off-white canvas and nothing extra.", + ), + (tr::editor::window::looks::retro_desk(), "Retro Desk"), + ( + tr::editor::window::looks::retro_desk_description(), + "Warm beige and chunky mechanical keys.", + ), + (tr::editor::window::looks::saved(), "Your Presets"), + (tr::editor::window::looks::smooth_demo(), "Smooth Demo"), + ( + tr::editor::window::looks::smooth_demo_description(), + "Soft clouds, gentle zooms and a calm cursor.", + ), + (tr::editor::window::looks::title(), "Looks"), + (tr::editor::window::looks::tutorial(), "Tutorial"), + ( + tr::editor::window::looks::tutorial_description(), + "Slow, steady zooms and keys anyone can follow.", + ), + (tr::editor::window::looks::web_product(), "Web Product"), + ( + tr::editor::window::looks::web_product_description(), + "A browser window for site and app demos.", + ), + (tr::editor::tabs::keyboard::key_style(), "Key Style"), + (tr::editor::tabs::keyboard::key_theme(), "Key Theme"), + (tr::editor::tabs::keyboard::style_classic(), "Classic"), + (tr::editor::tabs::keyboard::style_macos(), "macOS"), + (tr::editor::tabs::keyboard::style_windows(), "Windows"), + (tr::editor::tabs::keyboard::style_linux(), "Linux"), + (tr::editor::tabs::keyboard::style_mechanical(), "Mechanical"), + (tr::editor::tabs::keyboard::type_keys(), "Type Keys"), + ( + tr::editor::tabs::keyboard::type_keys_placeholder(), + "A sentence, or a shortcut like cmd+k", + ), + (tr::editor::tabs::keyboard::show_as(), "Show As"), + (tr::editor::tabs::keyboard::type_keys_button(), "Type Keys…"), + ( + tr::editor::tabs::keyboard::add_to_timeline(), + "Add to Timeline", + ), + ( + tr::editor::tabs::keyboard::type_keys_hint(), + "Paste a sentence, a script or a transcript, or a shortcut like cmd+shift+p. It types out as key presses from where you opened this.", + ), + (tr::editor::tabs::keyboard::split_words(), "Words"), + (tr::editor::tabs::keyboard::split_keys(), "Keys"), + (tr::editor::tabs::keyboard::split_sentence(), "Sentence"), + (tr::editor::tabs::keyboard::theme_light(), "Light"), + (tr::editor::tabs::keyboard::theme_dark(), "Dark"), + (tr::editor::tabs::keyboard::theme_retro(), "Retro"), + ( + tr::editor::tabs::camera::empty_title(), + "No camera in this recording", + ), + ( + tr::editor::tabs::camera::empty_description(), + "Choose a camera before you record and you can shape and place it here.", + ), + ( + tr::editor::tabs::cursor::empty_title(), + "No cursor in this recording", + ), + ( + tr::editor::tabs::cursor::empty_description(), + "This recording didn't capture the cursor, so there's nothing to style yet.", + ), + (tr::editor::export::done::file_copied(), "Copied"), + (tr::editor::export::more_formats(), "More"), + (tr::editor::export::tracks::separate(), "Separate tracks"), + ( + tr::editor::export::tracks::separate_description(), + "Screen, camera and audio, each as its own file", + ), (tr::editor::export::done::open_link(), "Open link"), (tr::editor::export::done::retry_copy(), "Retry Copy"), ( @@ -620,6 +716,11 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { tr::editor::tabs::captions::preset::pop_description(), "Playful caps that pop in with a vibrant accent.", ), + (tr::editor::tabs::captions::preset::soft(), "Soft"), + ( + tr::editor::tabs::captions::preset::soft_description(), + "Rounded charcoal card that matches the keys.", + ), ( tr::editor::tabs::captions::refining_word_timings(), "Refining word timings…", @@ -779,6 +880,9 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { (tr::editor::tabs::position::manual(), "Manual"), (tr::editor::tabs::position::top_center(), "Top Center"), (tr::editor::tabs::position::top_left(), "Top Left"), + (tr::editor::tabs::position::middle_left(), "Middle Left"), + (tr::editor::tabs::position::center(), "Center"), + (tr::editor::tabs::position::middle_right(), "Middle Right"), (tr::editor::tabs::position::top_right(), "Top Right"), ( tr::editor::tabs::sync::auto_description(), @@ -1218,6 +1322,11 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { tr::editor::window::no_room_for_caption(), "No room for another caption here", ), + ( + tr::editor::window::no_room_for_key(), + "No room for another key press here", + ), + (tr::editor::timeline::empty::add_key(), "Add a key press"), ( tr::editor::window::open::damaged(), "This recording's video tracks could not be opened. The bundle looks damaged.", @@ -1342,11 +1451,11 @@ fn editor_shell_surfaces_match_the_literals_they_replaced() { (tr::editor::export::turbo::title(), "Turbo export"), ( tr::editor::export::turbo::description(), - "Exports much faster using every CPU core and the GPU", + "Finishes exports much faster by using more of your computer's power", ), ( tr::editor::export::turbo::warning(), - "Uses all of your CPU, the GPU and several times more memory until the export finishes. Other apps may slow down, and your computer may get warm and loud.", + "Other apps may feel a little slower until the export finishes.", ), (tr::editor::window::clip_menu::transition(), "TRANSITION"), (tr::editor::window::clip_menu::transition_none(), "None"), diff --git a/apps/desktop-gpui/src/i18n/messages.rs b/apps/desktop-gpui/src/i18n/messages.rs index 4e44dfe1624..2463628e56b 100644 --- a/apps/desktop-gpui/src/i18n/messages.rs +++ b/apps/desktop-gpui/src/i18n/messages.rs @@ -10,12 +10,30 @@ use crate::permissions::OSPermission; pub(super) const KEY_PREFIX: &str = "message."; const EXACT: &[(&str, fn() -> &'static str)] = &[ - ("Recording task failed.", tr::message::recording::task_failed), - ("Recording startup failed", tr::message::recording::startup_failed), - ("Recording ended unexpectedly.", tr::message::recording::ended_unexpectedly), - ("Stop task failed.", tr::message::recording::stop_task_failed_plain), - ("Capture stop was not acknowledged", tr::message::recording::stop_not_acknowledged), - ("Timed out confirming capture state", tr::message::recording::capture_state_timed_out), + ( + "Recording task failed.", + tr::message::recording::task_failed, + ), + ( + "Recording startup failed", + tr::message::recording::startup_failed, + ), + ( + "Recording ended unexpectedly.", + tr::message::recording::ended_unexpectedly, + ), + ( + "Stop task failed.", + tr::message::recording::stop_task_failed_plain, + ), + ( + "Capture stop was not acknowledged", + tr::message::recording::stop_not_acknowledged, + ), + ( + "Timed out confirming capture state", + tr::message::recording::capture_state_timed_out, + ), ( "Capture did not acknowledge the requested pause state", tr::message::recording::pause_not_acknowledged, @@ -65,7 +83,10 @@ const EXACT: &[(&str, fn() -> &'static str)] = &[ tr::message::recording::crashed_before_capture, ), ("offline", tr::message::upload::offline), - ("Authentication expired", tr::message::upload::authentication_expired), + ( + "Authentication expired", + tr::message::upload::authentication_expired, + ), ( "This older recording has no saved audio intent; review it before retrying", tr::message::upload::no_audio_intent_review, @@ -90,15 +111,30 @@ const EXACT: &[(&str, fn() -> &'static str)] = &[ "Sign in to the original recording account and server to retry", tr::message::upload::sign_in_original, ), - ("Sign in again to upload this recording", tr::message::upload::sign_in_again), - ("Instant recording requires an upgraded plan", tr::message::upload::needs_upgrade), - ("The local recording changed during upload", tr::message::upload::changed_during), + ( + "Sign in again to upload this recording", + tr::message::upload::sign_in_again, + ), + ( + "Instant recording requires an upgraded plan", + tr::message::upload::needs_upgrade, + ), + ( + "The local recording changed during upload", + tr::message::upload::changed_during, + ), ( "The local recording changed after upload; it has been preserved", tr::message::upload::changed_after, ), - ("This recording is already queued for upload", tr::message::upload::already_queued), - ("Upload paused; the local recording is preserved", tr::message::upload::paused), + ( + "This recording is already queued for upload", + tr::message::upload::already_queued, + ), + ( + "Upload paused; the local recording is preserved", + tr::message::upload::paused, + ), ( "The upload worker failed; the local recording is preserved", tr::message::upload::worker_failed, diff --git a/apps/desktop-gpui/src/i18n/messages/tests.rs b/apps/desktop-gpui/src/i18n/messages/tests.rs index ea2fcc5c78b..8f706348e92 100644 --- a/apps/desktop-gpui/src/i18n/messages/tests.rs +++ b/apps/desktop-gpui/src/i18n/messages/tests.rs @@ -55,7 +55,12 @@ fn every_message_is_still_written_by_the_app() { #[test] fn unknown_and_hostile_text_passes_through() { - for message in ["", "encoder died", "Stop task failed: ", ". Cleanup is unconfirmed; retry Stop."] { + for message in [ + "", + "encoder died", + "Stop task failed: ", + ". Cleanup is unconfirmed; retry Stop.", + ] { assert_eq!(localize(message), message); } let deep = format!( @@ -90,7 +95,10 @@ fn messages_follow_the_language() { } init(Some(Locale::Ja.code())); - assert_eq!(localize("Recording task failed."), "録画タスクが失敗しました。"); + assert_eq!( + localize("Recording task failed."), + "録画タスクが失敗しました。" + ); assert_eq!( localize( "Stop task failed: Recording startup cancelled; local files preserved. Capture cleanup is unconfirmed. Use Ctrl+Shift+F9 to retry Stop." @@ -106,7 +114,9 @@ fn messages_follow_the_language() { "カメラへのアクセスを利用できません。「システム設定」>「プライバシーとセキュリティ」>「カメラ」でCapを許可してから、デバイスをもう一度選択してください。" ); assert_eq!( - localize("Low storage. Your recording files are preserved at /Users/me/Cap/Stop task failed: x. Free up space, then recover the recording in Cap."), + localize( + "Low storage. Your recording files are preserved at /Users/me/Cap/Stop task failed: x. Free up space, then recover the recording in Cap." + ), "ストレージの空き容量が不足しています。録画ファイルは/Users/me/Cap/Stop task failed: xに保持されています。空き容量を確保してから、Capで録画を復元してください。" ); diff --git a/apps/desktop-gpui/src/i18n/surface_tests.rs b/apps/desktop-gpui/src/i18n/surface_tests.rs index 4ea53bc7ea1..59f219dc9f4 100644 --- a/apps/desktop-gpui/src/i18n/surface_tests.rs +++ b/apps/desktop-gpui/src/i18n/surface_tests.rs @@ -75,6 +75,7 @@ fn main_surfaces_match_the_literals_they_replaced() { (tr::main::target::area(), "Area"), (tr::main::target::camera_only(), "Camera Only"), (tr::main::target::audio_only(), "Audio Only"), + (tr::main::target::solo_title(), "Camera or audio only"), (tr::main::target::choose_display(), "Choose a display"), (tr::main::target::choose_window(), "Choose a window"), (tr::main::target::no_displays(), "No displays found"), @@ -140,6 +141,7 @@ fn main_surfaces_match_the_literals_they_replaced() { (tr::main::device::format::loading(), "Loading formats..."), (tr::main::device::format::applying(), "Applying format..."), (tr::main::library::import_recordings(), "Import"), + (tr::main::library::new_project(), "New"), (tr::main::library::import_screenshots(), "Import image"), (tr::main::library::no_recordings(), "No recordings yet"), (tr::main::library::no_screenshots(), "No screenshots yet"), diff --git a/apps/desktop-gpui/src/i18n/tests.rs b/apps/desktop-gpui/src/i18n/tests.rs index d778f2f79cf..101c269d02f 100644 --- a/apps/desktop-gpui/src/i18n/tests.rs +++ b/apps/desktop-gpui/src/i18n/tests.rs @@ -12,12 +12,12 @@ fn english_matches_the_literals_it_replaced() { let app_name = crate::menus::APP_NAME; let pairs: &[(&str, String)] = &[ (tr::common::add(), "Add".into()), - (tr::editor::audio_only::add_visuals(), "Add Visuals".into()), ( tr::editor::audio_only::body(), "This recording has no video. Export the sound as MP3 or WAV, or add visuals to turn it into a video.".into(), ), (tr::editor::audio_only::export_audio(), "Export Audio".into()), + (tr::editor::audio_only::make_video(), "Make it a video".into()), (tr::editor::audio_only::show_visuals(), "Show visuals".into()), (tr::editor::audio_only::size_large(), "Large".into()), (tr::editor::audio_only::size_medium(), "Medium".into()), @@ -43,6 +43,7 @@ fn english_matches_the_literals_it_replaced() { (tr::menu::file::open_prompt(), "Open Recording".into()), (tr::menu::file::open_recent(), "Open Recent".into()), (tr::menu::file::import_media(), "Import Media…".into()), + (tr::menu::file::new_blank_project(), "New Blank Project".into()), (tr::menu::file::title(), "File".into()), (tr::menu::edit::copy(), "Copy".into()), (tr::menu::edit::cut(), "Cut".into()), diff --git a/apps/desktop-gpui/src/import.rs b/apps/desktop-gpui/src/import.rs index 03ae8ff73ec..59a61a7b427 100644 --- a/apps/desktop-gpui/src/import.rs +++ b/apps/desktop-gpui/src/import.rs @@ -151,6 +151,7 @@ fn imported_video_meta( status: Some(status), cursor_shape_version: cap_project::CURSOR_SHAPE_VERSION, audio_only: false, + blank: false, }, })), upload: None, diff --git a/apps/desktop-gpui/src/looks.rs b/apps/desktop-gpui/src/looks.rs new file mode 100644 index 00000000000..9de3d79b727 --- /dev/null +++ b/apps/desktop-gpui/src/looks.rs @@ -0,0 +1,441 @@ +//! Built-in looks: whole styles applied in one click from the Presets menu. +//! +//! A look sets only what makes a recording look a certain way -- the +//! background, framing, shadow, border and frame, how zooms move, how the +//! cursor glides, the key-press keys and the caption style -- and leaves the +//! timeline, camera, audio and everything else in the project as it is. + +use cap_project::{ + BackgroundSource, BorderConfiguration, CornerStyle, CursorAnimationStyle, FrameConfiguration, + FrameStyle, FrameTheme, KeyboardData, KeyboardSettings, PaddingMode, ProjectConfiguration, + ScreenMovementSpring, ShadowConfiguration, +}; + +use crate::i18n::tr; + +#[derive(Debug, Clone, Copy, PartialEq)] +pub enum LookBackground { + /// A catalogue wallpaper, by id. + Wallpaper(&'static str), + Gradient { + from: [u16; 3], + to: [u16; 3], + angle: u16, + }, + Color([u16; 3]), +} + +/// The key-press keys a look uses; `None` style keeps the platform's own. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct LookKeys { + pub style: Option<&'static str>, + pub theme: &'static str, +} + +#[derive(Debug, Clone, Copy)] +pub struct Look { + pub id: &'static str, + pub name: fn() -> &'static str, + pub description: fn() -> &'static str, + pub background: LookBackground, + /// Padding and corner rounding, in the background panel's own units. + pub padding: f64, + pub rounding: f64, + /// The card's shadow: overall strength, then size, opacity and blur. + pub shadow: (f32, f32, f32, f32), + /// A fine border: width and opacity, in white. + pub border: Option<(f32, f32)>, + pub frame: Option<(FrameStyle, FrameTheme)>, + pub zoom: ScreenMovementSpring, + pub cursor: CursorAnimationStyle, + pub keys: LookKeys, + /// The caption preset the look sets when the project has captions. + pub captions: &'static str, + /// Whether the card in the preview reads as a dark app. + pub dark_app: bool, +} + +const DEFAULT_ZOOM: ScreenMovementSpring = ScreenMovementSpring::NEW_PROJECT; + +pub static LOOKS: [Look; 8] = [ + Look { + id: "smooth-demo", + name: tr::editor::window::looks::smooth_demo, + description: tr::editor::window::looks::smooth_demo_description, + background: LookBackground::Wallpaper("macOS/sonoma-clouds"), + padding: 12.0, + rounding: 9.0, + shadow: (45.0, 0.0, 55.0, 60.0), + border: None, + frame: None, + zoom: ScreenMovementSpring { + stiffness: 120.0, + damping: 28.0, + mass: 2.0, + start_zooms_early: true, + }, + cursor: CursorAnimationStyle::Smooth, + keys: LookKeys { + style: Some("macos"), + theme: "dark", + }, + captions: "soft", + dark_app: false, + }, + Look { + id: "mac-native", + name: tr::editor::window::looks::mac_native, + description: tr::editor::window::looks::mac_native_description, + background: LookBackground::Wallpaper("macOS/tahoe-day-min"), + padding: 10.0, + rounding: 7.0, + shadow: (40.0, 0.0, 50.0, 45.0), + border: None, + frame: Some((FrameStyle::MacOS, FrameTheme::Light)), + zoom: DEFAULT_ZOOM, + cursor: CursorAnimationStyle::Mellow, + keys: LookKeys { + style: Some("macos"), + theme: "light", + }, + captions: "soft", + dark_app: false, + }, + Look { + id: "midnight", + name: tr::editor::window::looks::midnight, + description: tr::editor::window::looks::midnight_description, + background: LookBackground::Wallpaper("dark/3"), + padding: 9.0, + rounding: 8.0, + shadow: (65.0, 10.0, 70.0, 60.0), + border: Some((1.5, 14.0)), + frame: None, + zoom: DEFAULT_ZOOM, + cursor: CursorAnimationStyle::Mellow, + keys: LookKeys { + style: Some("macos"), + theme: "dark", + }, + captions: "soft", + dark_app: true, + }, + Look { + id: "minimal", + name: tr::editor::window::looks::minimal, + description: tr::editor::window::looks::minimal_description, + background: LookBackground::Color([245, 245, 247]), + padding: 6.0, + rounding: 4.0, + shadow: (18.0, 0.0, 30.0, 30.0), + border: None, + frame: None, + zoom: DEFAULT_ZOOM, + cursor: CursorAnimationStyle::Mellow, + keys: LookKeys { + style: None, + theme: "light", + }, + captions: "classic", + dark_app: false, + }, + Look { + id: "launch-day", + name: tr::editor::window::looks::launch_day, + description: tr::editor::window::looks::launch_day_description, + background: LookBackground::Gradient { + from: [255, 95, 109], + to: [255, 195, 113], + angle: 135, + }, + padding: 11.0, + rounding: 12.0, + shadow: (60.0, 8.0, 60.0, 60.0), + border: None, + frame: None, + zoom: ScreenMovementSpring { + stiffness: 280.0, + damping: 26.0, + mass: 1.0, + start_zooms_early: false, + }, + cursor: CursorAnimationStyle::Fast, + keys: LookKeys { + style: Some("macos"), + theme: "dark", + }, + captions: "highlight", + dark_app: false, + }, + Look { + id: "tutorial", + name: tr::editor::window::looks::tutorial, + description: tr::editor::window::looks::tutorial_description, + background: LookBackground::Wallpaper("blue/3"), + padding: 8.0, + rounding: 7.0, + shadow: (35.0, 0.0, 50.0, 40.0), + border: None, + frame: None, + zoom: ScreenMovementSpring { + stiffness: 110.0, + damping: 30.0, + mass: 2.4, + start_zooms_early: true, + }, + cursor: CursorAnimationStyle::Slow, + keys: LookKeys { + style: None, + theme: "light", + }, + captions: "karaoke", + dark_app: false, + }, + Look { + id: "web-product", + name: tr::editor::window::looks::web_product, + description: tr::editor::window::looks::web_product_description, + background: LookBackground::Gradient { + from: [233, 238, 255], + to: [205, 219, 255], + angle: 160, + }, + padding: 9.0, + rounding: 8.0, + shadow: (35.0, 0.0, 45.0, 45.0), + border: None, + frame: Some((FrameStyle::Browser, FrameTheme::Light)), + zoom: DEFAULT_ZOOM, + cursor: CursorAnimationStyle::Mellow, + keys: LookKeys { + style: None, + theme: "light", + }, + captions: "soft", + dark_app: false, + }, + Look { + id: "retro-desk", + name: tr::editor::window::looks::retro_desk, + description: tr::editor::window::looks::retro_desk_description, + background: LookBackground::Gradient { + from: [236, 228, 208], + to: [206, 193, 160], + angle: 145, + }, + padding: 10.0, + rounding: 5.0, + shadow: (35.0, 0.0, 45.0, 35.0), + border: None, + frame: None, + zoom: DEFAULT_ZOOM, + cursor: CursorAnimationStyle::Mellow, + keys: LookKeys { + style: Some("mechanical"), + theme: "retro", + }, + captions: "classic", + dark_app: false, + }, +]; + +/// `current` with `look` applied: the look's styling, the rest of the +/// project untouched. +pub fn apply_look(look: &Look, current: &ProjectConfiguration) -> ProjectConfiguration { + let mut next = current.clone(); + let background = &mut next.background; + match look.background { + LookBackground::Wallpaper(id) => { + if let Some(path) = crate::editor_sidebar::wallpaper_path(id) { + background.source = BackgroundSource::Wallpaper { + path: Some(path.to_string_lossy().into_owned()), + }; + } + } + LookBackground::Gradient { from, to, angle } => { + background.source = BackgroundSource::Gradient { + from, + to, + angle, + noise_intensity: None, + noise_scale: None, + animated: None, + animation_speed: None, + }; + } + LookBackground::Color(value) => { + background.source = BackgroundSource::Color { value, alpha: 255 }; + } + } + background.blur = 0.0; + background.padding = look.padding; + background.padding_mode = PaddingMode::Even; + background.rounding = look.rounding; + background.rounding_type = CornerStyle::Squircle; + let (shadow, size, opacity, blur) = look.shadow; + background.shadow = shadow; + background.advanced_shadow = Some(ShadowConfiguration { + size, + opacity, + blur, + }); + background.border = look.border.map(|(width, opacity)| BorderConfiguration { + enabled: true, + width, + color: [255, 255, 255], + opacity, + }); + background.frame = match look.frame { + Some((style, theme)) => Some(FrameConfiguration { + style, + theme, + ..background.frame.clone().unwrap_or_default() + }), + None => background.frame.clone().map(|frame| FrameConfiguration { + style: FrameStyle::None, + ..frame + }), + }; + + next.screen_movement_spring = look.zoom; + + next.cursor.animation_style = look.cursor; + if let Some(preset) = look.cursor.preset() { + next.cursor.tension = preset.tension; + next.cursor.mass = preset.mass; + next.cursor.friction = preset.friction; + } + + let keyboard = next.keyboard.get_or_insert_with(|| KeyboardData { + settings: KeyboardSettings::default(), + }); + keyboard.settings.style = look + .keys + .style + .map_or_else(KeyboardSettings::platform_style, str::to_string); + keyboard.settings.theme = look.keys.theme.to_string(); + + if let Some(captions) = next.captions.as_mut() + && let Some(preset) = crate::editor_tabs::CAPTION_PRESETS + .iter() + .find(|preset| preset.id == look.captions) + { + crate::editor_tabs::apply_caption_preset_settings(&mut captions.settings, preset); + } + next +} + +/// Which look the project is wearing: the one whose styling it still has +/// in every part a look sets. +pub fn current_look(project: &ProjectConfiguration) -> Option<&'static str> { + LOOKS + .iter() + .find(|look| look_settings(&apply_look(look, project)) == look_settings(project)) + .map(|look| look.id) +} + +/// Every setting a look writes, for comparing two projects on them alone. +fn look_settings(project: &ProjectConfiguration) -> serde_json::Value { + serde_json::json!({ + "background": project.background, + "zoom": project.screen_movement_spring, + "cursor": [ + serde_json::to_value(project.cursor.animation_style).ok(), + project.cursor.tension, + project.cursor.mass, + project.cursor.friction, + ], + "keys": project.keyboard.as_ref().map(|keyboard| { + (keyboard.settings.style.clone(), keyboard.settings.theme.clone()) + }), + "captions": project.captions.as_ref().map(|captions| &captions.settings), + }) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_look_has_a_unique_id_and_a_name() { + let mut ids: Vec<&str> = LOOKS.iter().map(|look| look.id).collect(); + ids.sort_unstable(); + ids.dedup(); + assert_eq!(ids.len(), LOOKS.len()); + for look in &LOOKS { + assert!(!(look.name)().is_empty()); + assert!(!(look.description)().is_empty()); + } + } + + #[test] + fn every_look_names_a_wallpaper_and_caption_preset_that_exist() { + for look in &LOOKS { + if let LookBackground::Wallpaper(id) = look.background { + assert!( + crate::editor_sidebar::WALLPAPER_NAMES.contains(&id), + "{}: {id}", + look.id + ); + } + assert!( + crate::editor_tabs::CAPTION_PRESETS + .iter() + .any(|preset| preset.id == look.captions), + "{}: {}", + look.id, + look.captions + ); + } + } + + #[test] + fn a_look_styles_the_project_and_keeps_the_rest() { + let mut project = ProjectConfiguration::for_new_project(); + project.camera.size = 33.0; + project.cursor.size = 140; + let look = LOOKS.iter().find(|look| look.id == "launch-day").unwrap(); + let next = apply_look(look, &project); + assert_eq!(next.background.padding, 11.0); + assert!(matches!( + next.background.source, + BackgroundSource::Gradient { angle: 135, .. } + )); + assert_eq!(next.screen_movement_spring.stiffness, 280.0); + assert!(matches!( + next.cursor.animation_style, + CursorAnimationStyle::Fast + )); + assert_eq!(next.keyboard.as_ref().unwrap().settings.style, "macos"); + // Untouched: the camera, the cursor's size, whether keys show. + assert_eq!(next.camera.size, 33.0); + assert_eq!(next.cursor.size, 140); + assert!(!next.keyboard.as_ref().unwrap().settings.enabled); + assert_eq!(current_look(&next), Some("launch-day")); + assert_eq!(current_look(&project), None); + // Changing anything the look set means it is no longer the look. + let mut tweaked = next.clone(); + tweaked.keyboard.as_mut().unwrap().settings.theme = "light".into(); + assert_eq!(current_look(&tweaked), None); + let mut slower = next.clone(); + slower.cursor.animation_style = CursorAnimationStyle::Slow; + assert_eq!(current_look(&slower), None); + } + + #[test] + fn leaving_a_framed_look_takes_the_frame_away() { + let project = ProjectConfiguration::for_new_project(); + let mac = LOOKS.iter().find(|look| look.id == "mac-native").unwrap(); + let framed = apply_look(mac, &project); + assert_eq!( + FrameConfiguration::active_style(framed.background.frame.as_ref()), + FrameStyle::MacOS + ); + let minimal = LOOKS.iter().find(|look| look.id == "minimal").unwrap(); + let bare = apply_look(minimal, &framed); + assert_eq!( + FrameConfiguration::active_style(bare.background.frame.as_ref()), + FrameStyle::None + ); + } +} diff --git a/apps/desktop-gpui/src/main.rs b/apps/desktop-gpui/src/main.rs index e81ca3f396f..996ec9402b3 100644 --- a/apps/desktop-gpui/src/main.rs +++ b/apps/desktop-gpui/src/main.rs @@ -17,8 +17,8 @@ mod camera_blur; #[cfg(any(not(target_os = "macos"), test))] mod camera_blur_portable; mod camera_loading; -mod caption_export; mod camera_window; +mod caption_export; mod controls_window; mod crash_capture; mod deeplink; @@ -47,19 +47,21 @@ mod editor_shapes; mod editor_side_panel; mod editor_sidebar; mod editor_tabs; -mod editor_voice_eq; mod editor_timeline; +mod editor_voice_eq; mod editor_window; mod encoder_recovery; mod feeds; mod focus_border; +mod foreign_window; +#[cfg(debug_assertions)] +mod glass_probe; mod hotkeys; mod i18n; mod import; mod installer; mod jpeg_thumbnail; mod library; -mod live_draw; #[cfg(not(any(target_os = "macos", target_os = "windows")))] mod linux_dialog; #[cfg(target_os = "linux")] @@ -68,6 +70,8 @@ mod linux_display; mod linux_portal_id; #[cfg(target_os = "linux")] mod linux_wallpaper; +mod live_draw; +mod looks; mod main_window; mod media_access; mod menus; @@ -83,15 +87,12 @@ mod permissions_ui; #[cfg(test)] mod picker_benchmark; #[cfg(debug_assertions)] -mod glass_probe; -#[cfg(debug_assertions)] mod picker_probe; #[cfg(debug_assertions)] mod picker_ui_benchmark; mod platform; mod power; mod presets; -mod style_presets; mod project_save; mod recording; mod recordings_overlay; @@ -110,6 +111,7 @@ mod single_instance; mod snapshot; mod startup; mod store; +mod style_presets; mod target_overlay; mod target_thumbnails; mod teleprompter_window; @@ -121,7 +123,6 @@ mod ui; mod unused_media; mod updates; mod upload; -mod foreign_window; mod window_arrange; use gpui::{App, AppContext as _, Bounds, WindowBounds, WindowOptions, px, size}; @@ -381,6 +382,7 @@ fn main() { #[cfg(target_os = "linux")] runtime.block_on(linux_portal_id::register()); + platform::use_antialiased_text(); let app = gpui_platform::application().with_assets(Assets); // `RunEvent::Reopen`: the dock icon clicked while the app runs. Must be // registered on the builder -- gpui exposes it nowhere else -- with the @@ -984,7 +986,9 @@ mod logging_tests { tracing_subscriber::fmt::layer() .with_ansi(false) .with_writer(move || writer.clone()) - .with_filter(tracing_subscriber::EnvFilter::new(super::DEFAULT_LOG_FILTER)), + .with_filter(tracing_subscriber::EnvFilter::new( + super::DEFAULT_LOG_FILTER, + )), ); tracing::subscriber::with_default(subscriber, || { tracing::warn!(target: "zbus::proxy", "Failed to populate properties cache"); @@ -993,9 +997,15 @@ mod logging_tests { tracing::info!(target: "cap_gpui::session", "recording started"); }); let logged = String::from_utf8(lines.0.lock().unwrap().clone()).unwrap(); - assert!(!logged.contains("Failed to populate properties cache"), "{logged}"); + assert!( + !logged.contains("Failed to populate properties cache"), + "{logged}" + ); assert!(logged.contains("a zbus error still shows"), "{logged}"); - assert!(logged.contains("other zbus warnings still show"), "{logged}"); + assert!( + logged.contains("other zbus warnings still show"), + "{logged}" + ); assert!(logged.contains("recording started"), "{logged}"); } diff --git a/apps/desktop-gpui/src/main_window.rs b/apps/desktop-gpui/src/main_window.rs index ee78512daa6..ac55027eaf7 100644 --- a/apps/desktop-gpui/src/main_window.rs +++ b/apps/desktop-gpui/src/main_window.rs @@ -834,6 +834,8 @@ pub enum Panel { Device(DeviceMenu), /// Pick which display or which window to capture. Target(TargetType), + /// Record only the camera, or only audio. + Solo, /// What the three recording modes do. ModeInfo, /// The header recordings / screenshots library. @@ -980,6 +982,9 @@ pub struct MainWindow { mode_hover_task: Option>, mode_hover_bounds: [Rc>>>; 3], target: Option, + /// Which of Camera Only and Audio Only the shared tile offers: the last + /// one picked from its chevron. + solo_target: TargetType, devices: DeviceSnapshot, camera: Option, camera_id: Option, @@ -1298,6 +1303,7 @@ impl MainWindow { mode_hover_task: None, mode_hover_bounds: std::array::from_fn(|_| Rc::new(Cell::new(None))), target: None, + solo_target: TargetType::CameraOnly, devices: DeviceSnapshot::default(), camera: None, camera_id: None, @@ -4376,6 +4382,14 @@ impl MainWindow { .child(tr::main::mode::info::title()) .into_any_element(), Panel::Library(kind) => self.render_library_header(kind, cx).into_any_element(), + Panel::Solo => div() + .flex_1() + .min_w_0() + .text_size(px(12.)) + .font_weight(FontWeight::MEDIUM) + .text_color(theme.gray_11) + .child(tr::main::target::solo_title()) + .into_any_element(), Panel::Device(_) if self.device_format_target.is_some() => div() .flex_1() .min_w_0() @@ -4465,6 +4479,7 @@ impl MainWindow { self.render_target_grid(target, cx).into_any_element() } Panel::ModeInfo => self.render_mode_info(cx).into_any_element(), + Panel::Solo => self.render_solo_choices(cx).into_any_element(), Panel::Library(kind) => { self.render_library_grid(kind, cx).into_any_element() } @@ -4607,6 +4622,23 @@ impl MainWindow { .gap(px(8.)) .items_center() .child(self.render_search_field(cx)) + // A blank project: no recording, a background to build on. + .when(kind == LibraryKind::Recordings, |this| { + this.child( + ui::Button::plain( + &theme, + "library-new-project", + ui::ButtonVariant::Gray, + ui::ButtonSize::Sm, + ) + .label(tr::main::library::new_project()) + .icon("icons/plus.svg") + .height(px(36.)) + .on_click(cx.listener(|_, _, _window, cx| { + cx.defer(crate::audio_project::open_blank_in_editor); + })), + ) + }) .child( ui::Button::plain( &theme, @@ -7025,8 +7057,7 @@ impl MainWindow { .items_stretch() .w_full() .child(self.render_target_tile(TargetType::Area, cx)) - .child(self.render_target_tile(TargetType::CameraOnly, cx)) - .child(self.render_target_tile(TargetType::AudioOnly, cx)), + .child(self.render_split_target(self.shown_solo_target(), cx)), ) } @@ -7051,7 +7082,8 @@ impl MainWindow { .role(gpui::Role::Button) .aria_label(match target { TargetType::Display => "Choose a display", - _ => "Choose a window", + TargetType::Window => "Choose a window", + _ => "Camera or audio only", }) .flex() .w(px(28.)) @@ -7091,19 +7123,93 @@ impl MainWindow { match target { TargetType::Display => tr::main::target::choose_display(), TargetType::Window => tr::main::target::choose_window(), - _ => unreachable!(), + _ => tr::main::target::solo_title(), }, ) .view(cx) }) .tooltip_show_delay(ui::TOOLTIP_SHOW_DELAY) .on_click(cx.listener(move |this, _, window, cx| { - this.open_panel(Panel::Target(target), window, cx); + let panel = match target { + TargetType::Display | TargetType::Window => Panel::Target(target), + _ => Panel::Solo, + }; + this.open_panel(panel, window, cx); })), ) } - /// Area, Camera Only and Audio Only are plain tiles with their own border. + /// Camera Only or Audio Only, whichever the shared tile shows: the one + /// armed, else the last one picked. + fn shown_solo_target(&self) -> TargetType { + match self.target { + Some(target @ (TargetType::CameraOnly | TargetType::AudioOnly)) => target, + _ => self.solo_target, + } + } + + /// The chevron's choices: the camera alone, or audio alone, as the same + /// tiles the capture sources use. + fn render_solo_choices(&self, cx: &mut Context) -> gpui::Div { + let theme = self.theme; + div().flex().flex_row().gap(px(8.)).w_full().children( + [TargetType::CameraOnly, TargetType::AudioOnly] + .into_iter() + .map(|kind| { + let selected = self.target == Some(kind); + div() + .id(SharedString::from(format!("solo-{}", kind.label()))) + .when(self.keyboard_nav, ui::keyboard_nav::stop) + .role(gpui::Role::Button) + .aria_label(kind.label()) + .aria_selected(selected) + .flex() + .flex_1() + .flex_col() + .items_center() + .justify_center() + .gap(px(6.)) + .h(px(96.)) + .rounded(px(10.)) + .map(|this| glass_button(this, theme, selected)) + .hover(move |style| style.bg(capture_hover_fill(theme, selected))) + .active(move |style| style.bg(capture_press_fill(theme, selected))) + .child( + svg() + .path(kind.icon()) + .size(px(22.)) + .text_color(if selected { + theme.blue_10 + } else { + theme.gray_10 + }), + ) + .child( + div() + .text_size(px(12.)) + .line_height(px(16.)) + .text_color(if selected { + theme.blue_11 + } else { + theme.gray_12 + }) + .child(kind.display_label()), + ) + .on_click(cx.listener(move |this, _, _window, cx| { + // Picking one arms it, the way clicking its tile + // does. + this.solo_target = kind; + this.target = Some(kind); + this.close_panel(cx); + this.end_capture_mode(cx); + this.sync_overlays(cx); + cx.notify(); + })) + }), + ) + } + + /// Area is a plain tile with its own border. fn render_target_tile(&self, target: TargetType, cx: &mut Context) -> impl IntoElement { let theme = self.theme; let selected = self.target == Some(target); @@ -7163,6 +7269,9 @@ impl MainWindow { } else { Some(target) }; + if matches!(target, TargetType::CameraOnly | TargetType::AudioOnly) { + this.solo_target = target; + } // The tile picks by hovering: a window chosen from its // dropdown earlier would otherwise stay locked in, and the // highlight would ignore the window under the pointer. diff --git a/apps/desktop-gpui/src/menus.rs b/apps/desktop-gpui/src/menus.rs index ed1cb3e2b41..e03b1dc1481 100644 --- a/apps/desktop-gpui/src/menus.rs +++ b/apps/desktop-gpui/src/menus.rs @@ -135,6 +135,7 @@ actions!( /// Settings… -- ⌘, on macOS, Ctrl+, on Windows and Linux. OpenSettings, OpenFile, + NewBlankProject, ImportMedia, Changelog, HelpDashboard, @@ -199,6 +200,9 @@ pub fn init(cx: &mut App) { }) .detach(); }); + cx.on_action(|_: &NewBlankProject, cx: &mut App| { + crate::audio_project::open_blank_in_editor(cx); + }); cx.on_action(|_: &ImportMedia, cx: &mut App| { crate::import::pick_and_import_media(cx); }); @@ -377,6 +381,7 @@ pub fn build_menus(update_status: &updates::UpdateStatus, settings_enabled: bool name: tr::menu::file::title().into(), disabled: false, items: vec![ + MenuItem::action(tr::menu::file::new_blank_project(), NewBlankProject), MenuItem::action(tr::menu::file::open_file(), OpenFile), MenuItem::action(tr::menu::file::import_media(), ImportMedia), MenuItem::separator(), @@ -1356,6 +1361,7 @@ mod tests { .map(describe) .collect(); let expected_file_items = vec![ + "New Blank Project".into(), "Open File…".into(), "Import Media…".into(), "-".into(), diff --git a/apps/desktop-gpui/src/platform.rs b/apps/desktop-gpui/src/platform.rs index 73bec98f3d2..6df8d332da6 100644 --- a/apps/desktop-gpui/src/platform.rs +++ b/apps/desktop-gpui/src/platform.rs @@ -698,6 +698,15 @@ pub fn follow_system_reduce_motion(cx: &mut gpui::App) { let _ = cx; } +/// Text renders grayscale-antialiased with no stroke dilation, the way the +/// web app's `-webkit-font-smoothing: antialiased` draws it. gpui only +/// dilates glyphs when `AppleFontSmoothing` allows it, and reads that once, +/// so this must run before the first frame. +pub fn use_antialiased_text() { + #[cfg(target_os = "macos")] + mac::use_antialiased_text(); +} + #[cfg(target_os = "macos")] mod macos_exception_guard; @@ -706,6 +715,31 @@ mod macos_occlusion; #[cfg(target_os = "macos")] mod mac { + pub fn use_antialiased_text() { + use core_foundation::base::TCFType; + use core_foundation::number::CFNumber; + use core_foundation::string::{CFString, CFStringRef}; + + unsafe extern "C" { + static kCFPreferencesCurrentApplication: CFStringRef; + fn CFPreferencesSetAppValue( + key: CFStringRef, + value: core_foundation::base::CFTypeRef, + application: CFStringRef, + ); + } + + let key = CFString::new("AppleFontSmoothing"); + let value = CFNumber::from(0i32); + unsafe { + CFPreferencesSetAppValue( + key.as_concrete_TypeRef(), + value.as_CFTypeRef(), + kCFPreferencesCurrentApplication, + ); + } + } + pub fn follow_system_reduce_motion(cx: &mut gpui::App) { use objc2_app_kit::{ NSWorkspace, NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification, diff --git a/apps/desktop-gpui/src/store.rs b/apps/desktop-gpui/src/store.rs index 8366a3e0219..e84abef7f48 100644 --- a/apps/desktop-gpui/src/store.rs +++ b/apps/desktop-gpui/src/store.rs @@ -150,6 +150,8 @@ pub struct ExportPrefs { pub advanced_open: bool, #[serde(default)] pub organization_id: Option, + /// New users start with Turbo on (see `ExportUi::load`); prefs saved + /// before the toggle existed keep it off, as they were. #[serde(default)] pub turbo: bool, #[serde(default)] diff --git a/apps/desktop-gpui/src/target_overlay.rs b/apps/desktop-gpui/src/target_overlay.rs index c85e263b39d..4c9410cf153 100644 --- a/apps/desktop-gpui/src/target_overlay.rs +++ b/apps/desktop-gpui/src/target_overlay.rs @@ -3410,6 +3410,16 @@ impl OverlayWindow { } else { tr::overlay::audio_only::choose_source() }; + // The stage is dark in both themes, over a dimmed screen, so its ink + // is light in both. + let ink = |alpha: f32| gpui::hsla(0., 0., 1., alpha); + let status_dot = if has_microphone { + gpui::hsla(0.39, 0.62, 0.52, 1.) + } else if system_audio { + Hsla::from(self.theme.blue_9) + } else { + ink(0.3) + }; div() .size_full() .relative() @@ -3417,63 +3427,101 @@ impl OverlayWindow { .flex_col() .items_center() .justify_center() - .bg(gpui::hsla(0., 0., 0., 0.7)) + .bg(gpui::hsla(0., 0., 0., 0.62)) .child( div() .flex() .flex_col() .items_center() - .mb(px(16.)) + .gap(px(6.)) + .mb(px(20.)) .child( div() - .mb(px(8.)) - .text_size(px(30.)) - .font_weight(FontWeight::BOLD) + .text_size(px(22.)) + .font_weight(FontWeight::SEMIBOLD) + .text_color(ink(0.96)) .child(tr::main::target::audio_only()), ) .child( div() - .text_size(px(12.)) - .text_color(self.theme.gray_11) + .text_size(px(13.)) + .text_color(ink(0.6)) .child(tr::overlay::audio_only::description()), ), ) + // One stage holds the sound: a live meter that breathes with the + // microphone, and a line saying what will be recorded. .child( - self.glass_surface() + div() .id("overlay-audio-meter") .w(px(CLUSTER_WIDTH)) - .mb(px(6.)) - .px(px(20.)) - .py(px(18.)) + .mb(px(14.)) .flex() .flex_col() .items_center() - .gap(px(12.)) + .gap(px(14.)) + .pt(px(26.)) + .pb(px(18.)) + .rounded(px(20.)) + .bg(gpui::hsla(0.63, 0.08, 0.09, 0.82)) + .border_1() + .border_color(ink(0.08)) + .shadow(vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.45), + offset: gpui::point(px(0.), px(18.)), + blur_radius: px(48.), + spread_radius: px(-8.), + inset: false, + }, + gpui::BoxShadow { + color: ink(0.06), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + ]) .child(self.render_level_bars(fill, has_microphone)) .child( div() - .text_size(px(12.)) - .text_color(self.theme.gray_11) - .child(caption), + .flex() + .flex_row() + .items_center() + .gap(px(8.)) + .child(div().size(px(6.)).rounded_full().bg(status_dot)) + .child( + div() + .text_size(px(12.)) + .text_color(ink(0.72)) + .child(caption), + ), ), ) .child(self.render_controls_cluster(self.target(cx), false, cx)) } + /// The meter: bars mirrored about a centre line, tallest in the middle, + /// each swaying a little out of step with its neighbours while the + /// microphone is live, so the stage breathes with the voice rather than + /// jumping as one block. Silent, it rests as a faint outline. fn render_level_bars(&self, fill: f32, live: bool) -> impl IntoElement { - const BARS: usize = 31; - const MAX_HEIGHT: f32 = 56.; - const MIN_HEIGHT: f32 = 4.; + const BARS: usize = 41; + const MAX_HEIGHT: f32 = 72.; + const MIN_HEIGHT: f32 = 3.; + static START: std::sync::OnceLock = std::sync::OnceLock::new(); let fill = if fill.is_finite() { fill.clamp(0., 1.) } else { 0. }; - let lit = if live { - Hsla::from(self.theme.blue_9) - } else { - Hsla::from(self.theme.gray_7) - }; + // Quiet speech still reads: the level is eased up from the floor. + let fill = fill.sqrt(); + let seconds = START + .get_or_init(std::time::Instant::now) + .elapsed() + .as_secs_f32(); + let accent = Hsla::from(self.theme.blue_9); div() .h(px(MAX_HEIGHT)) .flex() @@ -3482,14 +3530,33 @@ impl OverlayWindow { .gap(px(4.)) .children((0..BARS).map(move |index| { let offset = index as f32 - (BARS - 1) as f32 / 2.; - let envelope = (std::f32::consts::PI * offset / BARS as f32).cos(); - let texture = 0.75 + 0.25 * ((index * 7 % 5) as f32 / 4.); - let height = MIN_HEIGHT + (MAX_HEIGHT - MIN_HEIGHT) * fill * envelope * texture; + let envelope = (std::f32::consts::FRAC_PI_2 * offset / ((BARS - 1) as f32 / 2.)) + .cos() + .powf(1.4); + let sway = 0.72 + + 0.28 * (seconds * 7.0 + index as f32 * 0.9).sin() + + 0.12 * (seconds * 11.0 - index as f32 * 1.7).sin(); + // At rest the bars hold a faint outline of a voice, so the + // stage reads as a meter before any sound arrives. + let resting = 0.1 * envelope * (0.7 + 0.3 * ((index * 7 % 5) as f32 / 4.)); + let level = if live { + (fill * envelope * sway.clamp(0.4, 1.0)).max(resting) + } else { + resting + }; + let height = MIN_HEIGHT + (MAX_HEIGHT - MIN_HEIGHT) * level; + // Louder bars warm from white toward the accent. + let color = if live { + let white = gpui::hsla(0., 0., 1., 0.55 + 0.4 * level); + if level > 0.55 { accent } else { white } + } else { + gpui::hsla(0., 0., 1., 0.22) + }; div() - .w(px(4.)) + .w(px(3.)) .h(px(height.max(MIN_HEIGHT))) .rounded_full() - .bg(lit) + .bg(color) })) } diff --git a/apps/desktop-gpui/src/theme.rs b/apps/desktop-gpui/src/theme.rs index 766f56b5925..48cf6fdb290 100644 --- a/apps/desktop-gpui/src/theme.rs +++ b/apps/desktop-gpui/src/theme.rs @@ -467,6 +467,8 @@ pub struct Theme { pub editor: EditorPalette, } +pub const EDITOR_CARD_RADIUS: f32 = 14.; + /// Editor surfaces, transcribed from the `--ed-*` custom properties in /// `apps/desktop/src/styles/theme.css`. A layered, low-contrast system /// (window < card < control) with one accent; change both files together. @@ -488,14 +490,7 @@ pub struct EditorPalette { pub accent_2: Rgba, pub playhead: Rgba, pub thumb: Rgba, - /// `--ed-seg-tint`: how much of a track colour a segment fill carries. - pub seg_tint: f32, - /// `--ed-seg-line`: the segment border's alpha. - pub seg_line: f32, - /// `--ed-seg-label-base` / `--ed-seg-label-mix`: segment text is the - /// track colour pulled this far toward black (light) or white (dark). pub seg_label_base: Rgba, - pub seg_label_mix: f32, /// `--ed-card-shadow` / `--ed-pop-shadow`: how black each surface's drop /// shadow is. A card lifts barely at all on white and much more on the dark /// window, where the hairline alone is invisible. @@ -508,91 +503,111 @@ pub struct EditorPalette { impl EditorPalette { pub fn light() -> Self { Self { - window: rgb(0xf1f1f3), + window: rgb(0xf7f7f9), card: rgb(0xffffff), - card_2: rgb(0xf6f6f7), - stage: rgb(0xe9e9ec), - line: rgba(0x00000013), - line_strong: rgba(0x0000001f), - text_1: rgb(0x1c1c1e), - text_2: rgb(0x6e6e75), - text_3: rgb(0xa0a0a8), - ctl: rgba(0x0000000b), - ctl_hover: rgba(0x00000013), - ctl_active: rgba(0x0000001c), - accent: rgb(0x007aff), + card_2: rgb(0xffffff), + stage: rgb(0xf7f7f9), + line: rgba(0x00000014), + line_strong: rgba(0x00000021), + text_1: rgb(0x17171a), + text_2: rgb(0x66666e), + text_3: rgb(0x9a9aa2), + ctl: rgba(0x00000008), + ctl_hover: rgba(0x0000000f), + ctl_active: rgba(0x00000018), + accent: rgb(0x0a7cff), accent_2: rgb(0x2b8cff), playhead: rgb(0xff3b30), thumb: rgb(0xffffff), - seg_tint: 0.13, - seg_line: 0.34, seg_label_base: rgb(0x000000), - seg_label_mix: 0.58, - card_shadow_alpha: 0.04, - pop_shadow_alpha: 0.25, - pop_shadow_offset: 12., - pop_shadow_blur: 32., + card_shadow_alpha: 0.05, + pop_shadow_alpha: 0.22, + pop_shadow_offset: 14., + pop_shadow_blur: 36., } } pub fn dark() -> Self { Self { - window: rgb(0x131315), - card: rgb(0x1b1b1e), - card_2: rgb(0x202024), - stage: rgb(0x121214), - line: rgba(0xffffff14), - line_strong: rgba(0xffffff21), - text_1: rgb(0xf4f4f5), - text_2: rgb(0x9a9aa3), - text_3: rgb(0x62626a), - ctl: rgba(0xffffff0f), - ctl_hover: rgba(0xffffff17), + window: rgb(0x0c0d10), + card: rgb(0x16171b), + card_2: rgb(0x1e1f24), + stage: rgb(0x0e0f12), + line: rgba(0xffffff12), + line_strong: rgba(0xffffff1f), + text_1: rgb(0xf5f5f7), + text_2: rgb(0x9d9ea8), + text_3: rgb(0x64656e), + ctl: rgba(0xffffff0d), + ctl_hover: rgba(0xffffff16), ctl_active: rgba(0xffffff21), accent: rgb(0x0a84ff), accent_2: rgb(0x3d9bff), playhead: rgb(0xff453a), - thumb: rgb(0xe8e8ea), - seg_tint: 0.20, - seg_line: 0.42, + thumb: rgb(0xf4f4f6), seg_label_base: rgb(0xffffff), - seg_label_mix: 0.55, - card_shadow_alpha: 0.30, - pop_shadow_alpha: 0.60, + card_shadow_alpha: 0.42, + pop_shadow_alpha: 0.62, pop_shadow_offset: 16., - pop_shadow_blur: 40., + pop_shadow_blur: 44., } } - /// `color-mix(in srgb, color , card)`: the segment fill. - pub fn seg_fill(&self, color: Rgba) -> Rgba { - mix(self.card, color, self.seg_tint) + fn is_dark(&self) -> bool { + self.card.r < 0.5 } - /// A selected segment: the tint plus ten points. - pub fn seg_fill_selected(&self, color: Rgba) -> Rgba { - mix(self.card, color, self.seg_tint + 0.10) + pub fn seg_fill(&self, color: Rgba) -> Rgba { + if self.is_dark() { + mix(color, rgb(0x000000), 0.10) + } else { + color + } } - /// The segment's 1px inset border. pub fn seg_border(&self, color: Rgba) -> Rgba { - rgba_alpha(color, self.seg_line) + rgba_alpha(mix(color, rgb(0x000000), 0.32), 0.9) } - /// Segment label text. pub fn seg_label(&self, color: Rgba) -> Rgba { - mix(self.seg_label_base, color, self.seg_label_mix) + self.seg_ink(color, 1.) } - /// Segment secondary text (durations, amounts): the label colour at 45% - /// toward `text_3`. pub fn seg_muted(&self, color: Rgba) -> Rgba { - mix(self.text_3, color, 0.45) + self.seg_ink(color, 1.) + } + + /// White or a deep shade of the track colour, whichever contrasts more + /// with the brightest part of the fill (its lit top), so labels stay + /// readable on the bright hues without dulling every track. + pub fn badge_fill(&self, color: Rgba) -> Rgba { + mix(self.seg_fill(color), rgb(0x000000), 0.32) + } + + pub fn badge_ink(&self, _color: Rgba) -> Rgba { + rgb(0xffffff) + } + + pub fn seg_gradient(&self, color: Rgba) -> (Rgba, Rgba) { + let fill = self.seg_fill(color); + ( + mix(fill, rgb(0x000000), 0.24), + mix(fill, rgb(0xffffff), 0.22), + ) + } + + #[cfg(test)] + pub fn seg_text_band(&self, color: Rgba) -> [Rgba; 2] { + let (top, bottom) = self.seg_gradient(color); + [mix(top, bottom, 1. / 3.), mix(top, bottom, 2. / 3.)] + } + + fn seg_ink(&self, _color: Rgba, alpha: f32) -> Rgba { + rgba_alpha(rgb(0xffffff), alpha) } - /// The track gutter's icon tile: `color 16%` over transparent. pub fn tile_bg(&self, color: Rgba) -> Rgba { - rgba_alpha(color, 0.16) + rgba_alpha(color, 0.2) } /// The tile's glyph: the track colour pulled 62% toward the label base. @@ -600,32 +615,124 @@ impl EditorPalette { mix(self.seg_label_base, color, 0.62) } - /// Trim handles: the colour at 70% toward the label base. pub fn seg_handle(&self, color: Rgba) -> Rgba { - mix(self.seg_label_base, color, 0.70) + self.seg_ink(color, 0.9) } - /// `--ed-card-shadow`: `0 1px 2px` under every editor card. pub fn card_shadow(&self) -> Vec { - vec![BoxShadow { - color: gpui::hsla(0., 0., 0., self.card_shadow_alpha), - offset: point(px(0.), px(1.)), - blur_radius: px(2.), - spread_radius: px(0.), - inset: false, - }] + let dark = self.is_dark(); + vec![ + BoxShadow { + color: gpui::hsla(0., 0., 1., if dark { 0.045 } else { 0.9 }), + offset: point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + BoxShadow { + color: gpui::hsla(0., 0., 0., self.card_shadow_alpha), + offset: point(px(0.), px(1.)), + blur_radius: px(2.), + spread_radius: px(0.), + inset: false, + }, + BoxShadow { + color: gpui::hsla(0., 0., 0., self.card_shadow_alpha * 0.8), + offset: point(px(0.), px(8.)), + blur_radius: px(28.), + spread_radius: px(-10.), + inset: false, + }, + ] } - /// `--ed-pop-shadow`: the lift under a menu, popover or dialog. pub fn pop_shadow(&self) -> Vec { + vec![ + BoxShadow { + color: gpui::hsla(0., 0., 1., if self.is_dark() { 0.06 } else { 0.9 }), + offset: point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + BoxShadow { + color: gpui::hsla(0., 0., 0., self.pop_shadow_alpha), + offset: point(px(0.), px(self.pop_shadow_offset)), + blur_radius: px(self.pop_shadow_blur), + spread_radius: px(-12.), + inset: false, + }, + ] + } + + pub fn pop_bg(&self) -> Hsla { + if self.is_dark() { + self.card_2.into() + } else { + self.card.into() + } + } + + pub fn pop_line(&self) -> Hsla { + self.line_strong.into() + } + + /// An inset shadow, so the ring adds no layout size. + pub fn trough_ring(&self) -> Vec { vec![BoxShadow { - color: gpui::hsla(0., 0., 0., self.pop_shadow_alpha), - offset: point(px(0.), px(self.pop_shadow_offset)), - blur_radius: px(self.pop_shadow_blur), - spread_radius: px(-12.), - inset: false, + color: self.line.into(), + offset: point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.), + inset: true, }] } + + pub fn pill_shadow(&self) -> Vec { + let dark = self.is_dark(); + vec![ + BoxShadow { + color: gpui::hsla(0., 0., 1., if dark { 0.05 } else { 0.8 }), + offset: point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }, + BoxShadow { + color: if dark { + gpui::hsla(0., 0., 1., 0.07) + } else { + gpui::hsla(0., 0., 0., 0.09) + }, + offset: point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.), + inset: true, + }, + ] + } + + /// Shadows only, so selecting a tile never shifts the layout. + pub fn choice_selected_shadow(&self) -> Vec { + let mut shadow = self.pill_shadow(); + shadow.push(BoxShadow { + color: self.accent.into(), + offset: point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.5), + inset: true, + }); + shadow + } + + /// Opaque, so the pill's own drop shadow never shows through it. + pub fn pill_fill(&self) -> Hsla { + if self.is_dark() { + mix(self.card_2, rgb(0xffffff), 0.08).into() + } else { + self.card.into() + } + } } /// The drop shadow under the stage's letterboxed preview. The stage is nearly @@ -663,13 +770,22 @@ pub fn raised_pill_shadow() -> Vec { /// The drop shadow under a slider knob. pub fn thumb_shadow() -> Vec { - vec![BoxShadow { - color: gpui::hsla(0., 0., 0., 0.25), - offset: point(px(0.), px(1.)), - blur_radius: px(3.), - spread_radius: px(0.), - inset: false, - }] + vec![ + BoxShadow { + color: gpui::hsla(0., 0., 0., 0.22), + offset: point(px(0.), px(1.)), + blur_radius: px(2.), + spread_radius: px(0.), + inset: false, + }, + BoxShadow { + color: gpui::hsla(0., 0., 0., 0.14), + offset: point(px(0.), px(3.)), + blur_radius: px(8.), + spread_radius: px(0.), + inset: false, + }, + ] } /// `color-mix(in srgb, b , a)`: straight sRGB interpolation, which is what @@ -684,6 +800,18 @@ pub fn mix(a: Rgba, b: Rgba, t: f32) -> Rgba { } } +#[cfg(test)] +pub fn relative_luminance(color: Rgba) -> f32 { + let channel = |value: f32| { + if value <= 0.040_45 { + value / 12.92 + } else { + ((value + 0.055) / 1.055).powf(2.4) + } + }; + 0.2126 * channel(color.r) + 0.7152 * channel(color.g) + 0.0722 * channel(color.b) +} + pub fn rgba_alpha(color: Rgba, alpha: f32) -> Rgba { Rgba { a: alpha, ..color } } @@ -732,12 +860,12 @@ impl Theme { editor: EditorPalette::light(), gray_1: rgb(0xfcfcfc), - gray_2: rgb(0xf9f9f9), - gray_3: rgb(0xf0f0f0), - gray_4: rgb(0xe8e8e8), - gray_5: rgb(0xe0e0e0), - gray_6: rgb(0xd9d9d9), - gray_7: rgb(0xcecece), + gray_2: rgb(0xf8f8fa), + gray_3: rgb(0xeff0f2), + gray_4: rgb(0xe7e8eb), + gray_5: rgb(0xdfe0e3), + gray_6: rgb(0xd8d9dd), + gray_7: rgb(0xcdced2), gray_8: rgb(0xbbbbbb), gray_9: rgb(0x8d8d8d), gray_10: rgb(0x838383), @@ -786,19 +914,18 @@ impl Theme { material: None, editor: EditorPalette::dark(), - // gray 1-6 and 11 are the theme.css overrides, not stock Radix. - gray_1: rgb(0x111111), - gray_2: rgb(0x1c1c1c), - gray_3: rgb(0x282828), - gray_4: rgb(0x323232), - gray_5: rgb(0x3a3a3a), - gray_6: rgb(0x444444), - gray_7: rgb(0x484848), - gray_8: rgb(0x606060), - gray_9: rgb(0x6e6e6e), - gray_10: rgb(0x7b7b7b), - gray_11: rgb(0xa1a1a1), - gray_12: rgb(0xeeeeee), + gray_1: rgb(0x101114), + gray_2: rgb(0x1a1b1f), + gray_3: rgb(0x26272c), + gray_4: rgb(0x303136), + gray_5: rgb(0x38393f), + gray_6: rgb(0x424349), + gray_7: rgb(0x47484e), + gray_8: rgb(0x5f6067), + gray_9: rgb(0x6d6e75), + gray_10: rgb(0x7a7b82), + gray_11: rgb(0xa1a2a9), + gray_12: rgb(0xeeeef0), blue_3: rgb(0x0d2847), blue_4: rgb(0x003362), @@ -826,10 +953,10 @@ impl Theme { red_300: rgb(0xff4766), // `theme.css:101-107`. - gray_200_legacy: rgb(0x282828), - gray_300_legacy: rgb(0x323232), + gray_200_legacy: rgb(0x26272c), + gray_300_legacy: rgb(0x303136), // `--gray-400: #a1a1a1` (`theme.css:105`). - gray_400_legacy: rgb(0xa1a1a1), + gray_400_legacy: rgb(0xa1a2a9), gray_500_legacy: rgb(0xffffff), text_primary: rgba(0xfffffff2), diff --git a/apps/desktop-gpui/src/ui/button.rs b/apps/desktop-gpui/src/ui/button.rs index 37dda34ff32..bb49ff3acb1 100644 --- a/apps/desktop-gpui/src/ui/button.rs +++ b/apps/desktop-gpui/src/ui/button.rs @@ -130,6 +130,7 @@ pub struct Button { /// Whether the disabled look is the plain surface's `opacity-50` rather /// than the settings surface's repaint. dim_disabled: bool, + dark: bool, on_click: Option, on_open: Option, } @@ -158,6 +159,7 @@ impl Button { full_width: false, height: None, dim_disabled: false, + dark: theme.is_dark(), on_click: None, on_open: None, } @@ -436,6 +438,7 @@ impl RenderOnce for Button { radius, disabled, dim_disabled, + dark, gap, font_weight, width, @@ -446,6 +449,12 @@ impl RenderOnce for Button { } = self; let icon_color = paint.text; + let finish = paint.bg.map(|bg| ButtonFinish::new(bg, dark)); + let hover_fill = paint.hover_bg.map(|bg| ButtonFinish::sheen(bg, dark)); + let pressed_fill = paint + .hover_bg + .or(paint.bg) + .map(|bg| ButtonFinish::pressed(bg, dark)); div() .id(id) @@ -470,15 +479,17 @@ impl RenderOnce for Button { .text_size(size.text_size()) .when_some(font_weight, |this, weight| this.font_weight(weight)) .text_color(paint.text) - .when_some(paint.bg, |this, bg| this.bg(bg)) + .when_some(finish, |this, finish| { + this.bg(finish.fill).shadow(finish.shadow) + }) .when_some(paint.border, |this, border| { this.border_1().border_color(border) }) .when(dim_disabled, |this| this.opacity(0.5)) .when(!disabled, |this| { this.hover(move |style| { - let style = match paint.hover_bg { - Some(bg) => style.bg(bg), + let style = match hover_fill { + Some(fill) => style.bg(fill), None => style, }; let style = match paint.hover_text { @@ -506,6 +517,12 @@ impl RenderOnce for Button { .flex_shrink_0() .text_color(icon_color) })) + .when(!disabled, |this| { + this.active(move |style| match pressed_fill { + Some(fill) => style.bg(fill), + None => style.opacity(0.8), + }) + }) .when_some(on_click.filter(|_| !disabled), |this, handler| { this.on_click(move |event, window, cx| handler(event, window, cx)) }) @@ -513,6 +530,77 @@ impl RenderOnce for Button { } } +struct ButtonFinish { + fill: gpui::Background, + shadow: Vec, +} + +impl ButtonFinish { + fn new(bg: Hsla, dark: bool) -> Self { + let opaque = bg.a > 0.9; + let saturated = opaque && bg.s > 0.35 && bg.l > 0.2 && bg.l < 0.8; + let highlight = if saturated { + 0.22 + } else if opaque && bg.l > 0.6 { + 0.7 + } else if opaque { + 0.12 + } else { + 0.07 + }; + let mut shadow = vec![gpui::BoxShadow { + color: gpui::hsla(0., 0., 1., highlight), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(0.), + spread_radius: px(0.), + inset: true, + }]; + if opaque { + shadow.push(gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., if dark { 0.35 } else { 0.12 }), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(2.), + spread_radius: px(0.), + inset: false, + }); + } + if saturated { + shadow.push(gpui::BoxShadow { + color: bg.opacity(if dark { 0.35 } else { 0.28 }), + offset: gpui::point(px(0.), px(4.)), + blur_radius: px(12.), + spread_radius: px(-4.), + inset: false, + }); + } + Self { + fill: Self::sheen(bg, dark), + shadow, + } + } + + fn sheen(bg: Hsla, dark: bool) -> gpui::Background { + let lift = if dark { 0.09 } else { 0.06 }; + let top = bg.blend(gpui::hsla(0., 0., 1., lift * bg.a.max(0.3))); + gpui::linear_gradient( + 180., + gpui::linear_color_stop(top, 0.), + gpui::linear_color_stop(bg, 1.), + ) + } + + fn pressed(bg: Hsla, dark: bool) -> gpui::Background { + let shade = if bg.l > 0.6 && bg.a > 0.9 { 0.08 } else { 0.14 }; + let pressed = bg.blend(gpui::hsla( + 0., + 0., + if dark && bg.l < 0.5 { 1. } else { 0. }, + shade * bg.a.max(0.4), + )); + pressed.into() + } +} + /// The icon-only button: a square hit area, a circular hover fill and no label. /// /// `TooltipIconButton` in the Solid app is `p-2.5 opacity-70 hover:opacity-100 @@ -689,7 +777,14 @@ impl RenderOnce for IconButton { Some(radius) => this.rounded(radius), None => this.rounded_full(), }) - .when_some(bg, |this, bg| this.bg(bg)) + .when_some(bg, |this, bg| { + if bg.a > 0.9 { + let finish = ButtonFinish::new(bg, bg.l > 0.5); + this.bg(finish.fill).shadow(finish.shadow) + } else { + this.bg(bg) + } + }) .when_some(border, |this, border| this.border_1().border_color(border)) .when(active, |this| { this.when_some(active_bg, |this, bg| this.bg(bg)) @@ -715,6 +810,9 @@ impl RenderOnce for IconButton { }) }, ) + .when(!disabled && on_click.is_some(), |this| { + this.active(|style| style.opacity(0.78)) + }) .when_some(on_click.filter(|_| !disabled), |this, handler| { this.on_click(move |event, window, cx| handler(event, window, cx)) }) diff --git a/apps/desktop-gpui/src/ui/color_picker.rs b/apps/desktop-gpui/src/ui/color_picker.rs index 36030848461..d8cec282726 100644 --- a/apps/desktop-gpui/src/ui/color_picker.rs +++ b/apps/desktop-gpui/src/ui/color_picker.rs @@ -271,8 +271,8 @@ impl RenderOnce for ColorPicker { .gap(px(GAP)) .rounded(px(12.)) .border_1() - .border_color(Hsla::from(theme.editor.line)) - .bg(Hsla::from(theme.editor.card)) + .border_color(theme.editor.pop_line()) + .bg(theme.editor.pop_bg()) .shadow(theme.editor.pop_shadow()) // A press on the panel body must not fall through to the // caller's click-away backdrop underneath. diff --git a/apps/desktop-gpui/src/ui/editor_button.rs b/apps/desktop-gpui/src/ui/editor_button.rs index 8222902e196..99d43005383 100644 --- a/apps/desktop-gpui/src/ui/editor_button.rs +++ b/apps/desktop-gpui/src/ui/editor_button.rs @@ -183,7 +183,7 @@ impl EditorButton { } impl RenderOnce for EditorButton { - fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let EditorButton { id, label, @@ -226,6 +226,8 @@ impl RenderOnce for EditorButton { other => SharedString::from(format!("eb-{other:?}")), }; let icon_hover = (!disabled && !pressed).then_some(hover_text); + let (fade, level) = + crate::ui::hover_fade(window, cx, SharedString::from(format!("{group}-fade"))); div() .id(id) @@ -250,9 +252,22 @@ impl RenderOnce for EditorButton { .when(pressed, |this| this.bg(pressed_bg)) .when(!disabled && !pressed, |this| { this.cursor_pointer() - .hover(move |this| this.bg(hover_bg).text_color(hover_text)) + .when(level > 0., |this| { + this.bg(crate::ui::hover_blend( + hover_bg.opacity(0.), + hover_bg, + level, + )) + }) + .hover(move |this| this.text_color(hover_text)) .active(move |this| this.bg(active_bg)) }) + .on_hover(move |hovered, _, cx| { + fade.update(cx, |fade, cx| { + fade.set_hovered(*hovered); + cx.notify(); + }); + }) .children(left_icon.map(|icon| { let group = group.clone(); svg() diff --git a/apps/desktop-gpui/src/ui/field.rs b/apps/desktop-gpui/src/ui/field.rs index 015f37f7028..10e48fc08b8 100644 --- a/apps/desktop-gpui/src/ui/field.rs +++ b/apps/desktop-gpui/src/ui/field.rs @@ -61,7 +61,6 @@ pub struct Field { gap: Pixels, style: FieldStyle, text: Hsla, - muted: Hsla, value_color: Hsla, disabled_text: Hsla, badge_bg: Hsla, @@ -82,7 +81,6 @@ impl Field { gap, style, text: Hsla::from(theme.editor.text_1), - muted: Hsla::from(theme.editor.text_2), value_color: Hsla::from(theme.editor.text_3), disabled_text: Hsla::from(theme.editor.text_3), badge_bg: Hsla::from(theme.editor.ctl), @@ -174,7 +172,6 @@ impl RenderOnce for Field { gap, style, text, - muted, value_color, disabled_text, badge_bg, @@ -183,7 +180,7 @@ impl RenderOnce for Field { } = self; let label_color = match (disabled, style) { (true, _) => disabled_text, - (false, FieldStyle::Section) => muted, + (false, FieldStyle::Section) => text, (false, _) => text, }; @@ -232,7 +229,7 @@ impl RenderOnce for Field { } let (label_size, label_weight) = match style { - FieldStyle::Section => (px(12.), FontWeight::MEDIUM), + FieldStyle::Section => (px(13.), FontWeight::MEDIUM), FieldStyle::Stacked => (px(13.), FontWeight::NORMAL), _ => (px(13.), FontWeight::SEMIBOLD), }; diff --git a/apps/desktop-gpui/src/ui/hover_fade.rs b/apps/desktop-gpui/src/ui/hover_fade.rs new file mode 100644 index 00000000000..b021a64bdb8 --- /dev/null +++ b/apps/desktop-gpui/src/ui/hover_fade.rs @@ -0,0 +1,109 @@ +use std::time::{Duration, Instant}; + +use gpui::{App, ElementId, Entity, Hsla, Window}; + +const FADE_IN: Duration = Duration::from_millis(90); +const FADE_OUT: Duration = Duration::from_millis(220); + +pub struct HoverFade { + hovered: bool, + from: f32, + since: Option, +} + +impl HoverFade { + fn level_at(&self, now: Instant) -> (f32, bool) { + let target = if self.hovered { 1. } else { 0. }; + let Some(since) = self.since else { + return (target, false); + }; + let duration = if self.hovered { FADE_IN } else { FADE_OUT }; + let t = (now.saturating_duration_since(since).as_secs_f32() / duration.as_secs_f32()) + .clamp(0., 1.); + let eased = 1. - (1. - t).powi(3); + (self.from + (target - self.from) * eased, t < 1.) + } + + pub fn set_hovered(&mut self, hovered: bool) { + if hovered == self.hovered { + return; + } + let now = Instant::now(); + self.from = self.level_at(now).0; + self.hovered = hovered; + self.since = Some(now); + } +} + +pub fn hover_fade( + window: &mut Window, + cx: &mut App, + id: impl Into, +) -> (Entity, f32) { + let state = window.use_keyed_state(id, cx, |_, _| HoverFade { + hovered: false, + from: 0., + since: None, + }); + if cx.reduce_motion() { + let level = if state.read(cx).hovered { 1. } else { 0. }; + return (state, level); + } + let (level, animating) = state.read(cx).level_at(Instant::now()); + if animating { + window.request_animation_frame(); + } + (state, level) +} + +pub fn blend(rest: Hsla, hover: Hsla, level: f32) -> Hsla { + let rest = gpui::Rgba::from(rest); + let hover = gpui::Rgba::from(hover); + crate::theme::mix(rest, hover, level).into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_fresh_control_rests_without_animating() { + let fade = HoverFade { + hovered: false, + from: 0., + since: None, + }; + assert_eq!(fade.level_at(Instant::now()), (0., false)); + } + + #[test] + fn hovering_fades_in_and_settles_at_full() { + let mut fade = HoverFade { + hovered: false, + from: 0., + since: None, + }; + fade.set_hovered(true); + let start = fade.since.unwrap(); + let (mid, animating) = fade.level_at(start + FADE_IN / 2); + assert!(mid > 0. && mid < 1. && animating); + assert_eq!(fade.level_at(start + FADE_IN * 2), (1., false)); + } + + #[test] + fn leaving_mid_fade_starts_from_the_current_level() { + let mut fade = HoverFade { + hovered: false, + from: 0., + since: None, + }; + fade.set_hovered(true); + let start = fade.since.unwrap(); + let level = fade.level_at(start + FADE_IN / 2).0; + fade.hovered = false; + fade.from = level; + fade.since = Some(start + FADE_IN / 2); + let (just_after, _) = fade.level_at(start + FADE_IN / 2); + assert!((just_after - level).abs() < 1e-6); + } +} diff --git a/apps/desktop-gpui/src/ui/menu.rs b/apps/desktop-gpui/src/ui/menu.rs index e57dab93e20..f01d5a37811 100644 --- a/apps/desktop-gpui/src/ui/menu.rs +++ b/apps/desktop-gpui/src/ui/menu.rs @@ -239,8 +239,12 @@ impl Menu { Self { radius: px(12.), shadow: theme.editor.pop_shadow(), - bg: Hsla::from(theme.editor.card), - border: Hsla::from(theme.editor.line), + bg: Hsla::from(if theme.is_dark() { + theme.editor.card_2 + } else { + theme.editor.card + }), + border: Hsla::from(theme.editor.line_strong), hover: Hsla::from(theme.editor.ctl_hover), text: Hsla::from(theme.editor.text_1), ..Self::settings(theme, id, items, state) diff --git a/apps/desktop-gpui/src/ui/mod.rs b/apps/desktop-gpui/src/ui/mod.rs index 478a6b0ae08..973dcfc0d35 100644 --- a/apps/desktop-gpui/src/ui/mod.rs +++ b/apps/desktop-gpui/src/ui/mod.rs @@ -48,6 +48,7 @@ mod field; pub mod glass; mod glass_switch; mod hint; +mod hover_fade; mod kbd; pub mod keyboard_nav; mod menu; @@ -91,6 +92,7 @@ pub use editor_button::{EditorButton, EditorButtonVariant}; pub use field::{Field, Subfield}; #[allow(unused_imports)] pub use hint::{Hint, Hints, Side as HintSide}; +pub use hover_fade::{blend as hover_blend, hover_fade}; pub use kbd::{KbdChip, KbdSize, kbd_symbol}; #[allow(unused_imports)] diff --git a/apps/desktop-gpui/src/ui/segmented.rs b/apps/desktop-gpui/src/ui/segmented.rs index e7626dc7908..0239c0c454e 100644 --- a/apps/desktop-gpui/src/ui/segmented.rs +++ b/apps/desktop-gpui/src/ui/segmented.rs @@ -14,15 +14,18 @@ //! ([`crate::ui::TabRail`]) because its items are `flex-1` icon boxes with a //! separate indicator layer, not padded pills. //! -//! The indicator does not slide. Kobalte positions it from the selected -//! trigger's measured rect and animates a transform; this gpui rev has no -//! transform, so the selected item paints its own fill -- which is what the -//! settings window has always done here. +//! The selected pill glides between items. Stretched controls without gaps +//! place it by fraction so it tracks a resizing row exactly; the rest place +//! it from item rects measured on the previous frame, so until those exist +//! the selected item paints its own fill. + +use std::time::Duration; use gpui::{ - App, ElementId, FontWeight, Hsla, InteractiveElement, IntoElement, ParentElement, Pixels, - RenderOnce, SharedString, StatefulInteractiveElement, Styled, Window, div, - prelude::FluentBuilder, px, svg, + Animation, AnimationExt, App, Bounds, ElementId, FontWeight, Hsla, InteractiveElement, + IntoElement, ParentElement, Pixels, Point, RenderOnce, SharedString, + StatefulInteractiveElement, Styled, Window, canvas, div, prelude::FluentBuilder, px, relative, + svg, }; use crate::theme::Theme; @@ -97,6 +100,7 @@ pub struct SegmentedControl { idle_bg: Option, hover_bg: Option, selected_shadow: Vec, + container_shadow: Vec, stretch: bool, tab_stops: bool, on_select: Option, @@ -126,6 +130,7 @@ impl SegmentedControl { idle_bg: None, hover_bg: None, selected_shadow: Vec::new(), + container_shadow: Vec::new(), stretch: false, tab_stops: false, on_select: None, @@ -176,30 +181,21 @@ impl SegmentedControl { } } - /// The editor's segmented control: an `ed-ctl` trough with a raised - /// `ed-card` pill on the selected item (a white 12% wash in dark, where a - /// lifted card would read as a hole). pub fn editor(theme: &Theme, id: impl Into, options: Vec) -> Self { let editor = theme.editor; Self { container_bg: Some(Hsla::from(editor.ctl)), - container_border: None, + container_radius: px(9.), + container_shadow: editor.trough_ring(), + item_radius: px(7.), item_padding_x: px(10.), item_padding_y: px(0.), item_height: Some(px(24.)), - selected_bg: Some(if theme.is_dark() { - gpui::hsla(0., 0., 1., 0.12) - } else { - Hsla::from(editor.card) - }), + selected_bg: Some(editor.pill_fill()), selected_text: Hsla::from(editor.text_1), idle_text: Hsla::from(editor.text_2), hover_bg: Some(Hsla::from(editor.ctl_hover)), - selected_shadow: if theme.is_dark() { - Vec::new() - } else { - crate::theme::raised_pill_shadow() - }, + selected_shadow: editor.pill_shadow(), ..Self::base(theme, id, options) } } @@ -260,7 +256,7 @@ impl SegmentedControl { } impl RenderOnce for SegmentedControl { - fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let SegmentedControl { id, options, @@ -283,6 +279,7 @@ impl RenderOnce for SegmentedControl { idle_bg, hover_bg, selected_shadow, + container_shadow, stretch, tab_stops, on_select, @@ -294,8 +291,60 @@ impl RenderOnce for SegmentedControl { other => SharedString::from(format!("{other:?}")), }; + let count = options.len(); + let selected_index = options.iter().position(|option| option.selected); + let glide_state = (selected_bg.is_some() && count > 1).then(|| { + let state = window.use_keyed_state( + SharedString::from(format!("{prefix}-glide")), + cx, + |_, _| Glide::new(selected_index, count), + ); + state.update(cx, |glide, _| glide.select(selected_index, count)); + state + }); + let fractional = stretch && f32::from(container_gap) == 0.; + let indicator = glide_state.as_ref().and_then(|state| { + let glide = state.read(cx); + let to = glide.current?; + let from = glide.from.unwrap_or(to); + let place = if fractional { + let width = 1. / count as f32; + Placement::Fraction { + from: from as f32 * width, + to: to as f32 * width, + width, + } + } else { + let origin = glide.origin?; + let start = glide.rects.get(from).copied().flatten()?; + let end = glide.rects.get(to).copied().flatten()?; + Placement::Measured { + from: ( + f32::from(start.origin.x - origin.x), + f32::from(start.size.width), + ), + to: ( + f32::from(end.origin.x - origin.x), + f32::from(end.size.width), + ), + } + }; + Some((place, glide.generation)) + }); + let glides = indicator.is_some(); + let fades: Vec<_> = (0..count) + .map(|index| { + crate::ui::hover_fade( + window, + cx, + SharedString::from(format!("{prefix}-hover-{index}")), + ) + }) + .collect(); + div() .id(id) + .relative() .flex() .flex_row() .p(container_padding) @@ -305,13 +354,73 @@ impl RenderOnce for SegmentedControl { .when_some(container_border, |this, border| { this.border_1().border_color(border) }) + .when(!container_shadow.is_empty(), |this| { + this.shadow(container_shadow) + }) + .when_some( + glide_state.clone().filter(|_| !fractional), + |this, state| { + this.child( + canvas( + move |bounds, _, cx| { + state.update(cx, |glide, _| glide.origin = Some(bounds.origin)); + }, + |_, _, _, _| {}, + ) + .absolute() + .inset_0(), + ) + }, + ) + .when_some(indicator, |this, (place, generation)| { + let pill = div() + .absolute() + .top(container_padding) + .bottom(container_padding) + .rounded(item_radius) + .when_some(selected_bg, |pill, bg| pill.bg(bg)) + .when(!selected_shadow.is_empty(), |pill| { + pill.shadow(selected_shadow.clone()) + }); + let glide_ms = if cx.reduce_motion() { 1 } else { GLIDE_MS }; + let animation = Animation::new(Duration::from_millis(glide_ms)) + .with_easing(gpui::ease_out_quint()); + let id = SharedString::from(format!("{prefix}-glide-{generation}")); + match place { + Placement::Fraction { from, to, width } => this.child( + div() + .absolute() + .top_0() + .bottom_0() + .left(container_padding) + .right(container_padding) + .child(pill.w(relative(width)).left(relative(to)).with_animation( + id, + animation, + move |pill, delta| pill.left(relative(from + (to - from) * delta)), + )), + ), + Placement::Measured { from, to } => { + this.child(pill.left(px(to.0)).w(px(to.1)).with_animation( + id, + animation, + move |pill, delta| { + pill.left(px(from.0 + (to.0 - from.0) * delta)) + .w(px(from.1 + (to.1 - from.1) * delta)) + }, + )) + } + } + }) .children(options.into_iter().enumerate().map(|(index, option)| { let handler = handler.clone(); let selected = option.selected; let disabled = option.disabled; + let measure = glide_state.clone().filter(|_| !fractional); div() .id(SharedString::from(format!("{prefix}-{index}"))) + .relative() .flex() .flex_row() .items_center() @@ -330,8 +439,22 @@ impl RenderOnce for SegmentedControl { }) .when_some(font_weight, |this, weight| this.font_weight(weight)) .when(disabled, |this| this.opacity(0.5)) + .when_some(measure, |this, state| { + this.child( + canvas( + move |bounds, _, cx| { + state.update(cx, |glide, _| glide.measure(index, bounds)); + }, + |_, _, _, _| {}, + ) + .absolute() + .inset_0(), + ) + }) .map(|this| { - if selected { + if selected && glides { + this.text_color(selected_text) + } else if selected { let this = this .text_color(selected_text) .when(!selected_shadow.is_empty(), |this| { @@ -348,7 +471,13 @@ impl RenderOnce for SegmentedControl { None => this, }; match hover_bg { - Some(bg) if !disabled => this.hover(move |style| style.bg(bg)), + Some(bg) if !disabled => { + let level = fades[index].1; + let rest = idle_bg.unwrap_or(bg.opacity(0.)); + this.when(level > 0., |this| { + this.bg(crate::ui::hover_blend(rest, bg, level)) + }) + } _ => this, } } @@ -367,6 +496,15 @@ impl RenderOnce for SegmentedControl { label.into_any_element() } })) + .on_hover({ + let fade = fades[index].0.clone(); + move |hovered, _, cx| { + fade.update(cx, |fade, cx| { + fade.set_hovered(*hovered); + cx.notify(); + }); + } + }) .when_some(handler.filter(|_| !disabled), |this, handler| { this.on_click(move |_, window, cx| handler(&index, window, cx)) }) @@ -374,10 +512,82 @@ impl RenderOnce for SegmentedControl { } } +const GLIDE_MS: u64 = 200; + +#[derive(Clone, Copy)] +enum Placement { + Fraction { from: f32, to: f32, width: f32 }, + Measured { from: (f32, f32), to: (f32, f32) }, +} + +struct Glide { + current: Option, + from: Option, + generation: u64, + origin: Option>, + rects: Vec>>, +} + +impl Glide { + fn new(selected: Option, count: usize) -> Self { + Self { + current: selected, + from: selected, + generation: 0, + origin: None, + rects: vec![None; count], + } + } + + fn select(&mut self, selected: Option, count: usize) { + if self.rects.len() != count { + self.rects = vec![None; count]; + } + if self.current != selected { + self.from = self.current.or(selected); + self.current = selected; + self.generation = self.generation.wrapping_add(1); + } + } + + fn measure(&mut self, index: usize, bounds: Bounds) { + if let Some(slot) = self.rects.get_mut(index) { + *slot = Some(bounds); + } + } +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn a_new_pick_glides_from_the_old_one() { + let mut glide = Glide::new(Some(0), 3); + glide.select(Some(2), 3); + assert_eq!( + (glide.from, glide.current, glide.generation), + (Some(0), Some(2), 1) + ); + glide.select(Some(2), 3); + assert_eq!(glide.generation, 1); + } + + #[test] + fn a_first_pick_appears_in_place() { + let mut glide = Glide::new(None, 3); + glide.select(Some(1), 3); + assert_eq!((glide.from, glide.current), (Some(1), Some(1))); + } + + #[test] + fn a_changed_item_count_forgets_stale_rects() { + let mut glide = Glide::new(Some(0), 2); + glide.measure(1, Bounds::default()); + glide.select(Some(0), 4); + assert_eq!(glide.rects, vec![None; 4]); + } + #[derive(Debug, Clone, Copy, PartialEq, Eq)] enum Quality { Low, diff --git a/apps/desktop-gpui/src/ui/slider.rs b/apps/desktop-gpui/src/ui/slider.rs index 38af646eb5f..5369fb9319b 100644 --- a/apps/desktop-gpui/src/ui/slider.rs +++ b/apps/desktop-gpui/src/ui/slider.rs @@ -162,6 +162,38 @@ impl Slider { self } + pub fn editor(self, theme: &crate::theme::Theme, knob: Pixels, disabled: bool) -> Self { + let editor = theme.editor; + let fill = if disabled { + editor.text_3 + } else { + editor.accent + }; + let mut slider = self + .track(px(4.), Hsla::from(editor.ctl_active)) + .fill(Hsla::from(fill)) + .thumb(knob, gpui::white(), None); + if let Some(thumb) = slider.thumb.as_mut() { + thumb.shadow = vec![ + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.12), + offset: gpui::point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(0.5), + inset: false, + }, + gpui::BoxShadow { + color: gpui::hsla(0., 0., 0., 0.22), + offset: gpui::point(px(0.), px(1.)), + blur_radius: px(3.), + spread_radius: px(0.), + inset: false, + }, + ]; + } + slider + } + /// A knob that has to read on a light track needs the lift the CSS one /// gets from `0 1px 3px rgba(0,0,0,.25)`. pub fn thumb_shadow(mut self) -> Self { diff --git a/apps/desktop-gpui/src/ui/tab_rail.rs b/apps/desktop-gpui/src/ui/tab_rail.rs index dfa85913d38..11ebda90d36 100644 --- a/apps/desktop-gpui/src/ui/tab_rail.rs +++ b/apps/desktop-gpui/src/ui/tab_rail.rs @@ -64,6 +64,7 @@ pub struct TabRail { bg: Hsla, border: Hsla, indicator: Hsla, + indicator_shadow: Vec, hover: Hsla, selected_icon: Hsla, idle_icon: Hsla, @@ -72,8 +73,6 @@ pub struct TabRail { } impl TabRail { - /// The editor's config sidebar rail: a 46px bar on the card surface under - /// an `ed-line` hairline, with 40x30 tab boxes and 16px icons. pub fn editor( theme: &Theme, id: impl Into, @@ -89,7 +88,8 @@ impl TabRail { icon_size: px(16.), bg: panel_bg, border: Hsla::from(theme.editor.line), - indicator: Hsla::from(theme.editor.ctl_hover), + indicator: theme.editor.pill_fill(), + indicator_shadow: theme.editor.pill_shadow(), hover: Hsla::from(theme.editor.ctl), selected_icon: Hsla::from(theme.editor.text_1), idle_icon: Hsla::from(theme.editor.text_2), @@ -112,7 +112,7 @@ impl TabRail { } impl RenderOnce for TabRail { - fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { let TabRail { id, items, @@ -123,6 +123,7 @@ impl RenderOnce for TabRail { bg, border, indicator, + indicator_shadow, hover, selected_icon, idle_icon, @@ -135,6 +136,15 @@ impl RenderOnce for TabRail { other => SharedString::from(format!("{other:?}")), }; let handler: Option> = on_select.map(std::rc::Rc::new); + let fades: Vec<_> = (0..items.len()) + .map(|index| { + crate::ui::hover_fade( + window, + cx, + SharedString::from(format!("{prefix}-hover-{index}")), + ) + }) + .collect(); div() .id(id) @@ -145,7 +155,7 @@ impl RenderOnce for TabRail { .h(height) .flex_none() .overflow_hidden() - .rounded_t(px(11.)) + .rounded_t(px(crate::theme::EDITOR_CARD_RADIUS - 1.)) .justify_around() .px(px(10.)) .border_b_1() @@ -167,9 +177,22 @@ impl RenderOnce for TabRail { .w(box_width) .rounded(px(9.)) .when(disabled, |this| this.opacity(0.6)) - .when(selected, |this| this.bg(indicator)) - .when(!selected && !disabled, |this| { - this.cursor_pointer().hover(move |style| style.bg(hover)) + .when(selected, |this| { + this.bg(indicator).shadow(indicator_shadow.clone()) + }) + .map(|this| { + let (fade, level) = fades[index].clone(); + this.when(!selected && !disabled, |this| { + this.cursor_pointer().when(level > 0., |this| { + this.bg(crate::ui::hover_blend(hover.opacity(0.), hover, level)) + }) + }) + .on_hover(move |hovered, _, cx| { + fade.update(cx, |fade, cx| { + fade.set_hovered(*hovered); + cx.notify(); + }); + }) }) .tooltip_show_delay(crate::ui::TOOLTIP_SHOW_DELAY) .tooltip(move |_, cx| crate::ui::Tooltip::new(&theme, label.clone()).view(cx)) diff --git a/apps/desktop-gpui/src/ui/toggle.rs b/apps/desktop-gpui/src/ui/toggle.rs index fa5a1a2a4d5..f2a299068a3 100644 --- a/apps/desktop-gpui/src/ui/toggle.rs +++ b/apps/desktop-gpui/src/ui/toggle.rs @@ -61,6 +61,7 @@ pub struct Toggle { on_fill: Hsla, /// `bg-white` -- the thumb is white on every surface. thumb: Hsla, + ring: Hsla, disabled: bool, label: Option, on_click: Option, @@ -76,6 +77,7 @@ impl Toggle { off_fill: Hsla::from(theme.editor.ctl_active), on_fill: Hsla::from(theme.editor.accent), thumb: gpui::white(), + ring: Hsla::from(theme.editor.line), disabled: false, label: None, on_click: None, @@ -148,6 +150,7 @@ impl RenderOnce for Toggle { off_fill, on_fill, thumb, + ring, disabled, label, on_click, @@ -179,6 +182,15 @@ impl RenderOnce for Toggle { .when(checked, |this| this.justify_end()) .when(!disabled, |this| this.cursor_pointer()) .bg(if checked { on_fill } else { off_fill }) + .when(!checked, |this| { + this.shadow(vec![gpui::BoxShadow { + color: ring, + offset: gpui::point(px(0.), px(0.)), + blur_radius: px(0.), + spread_radius: px(1.), + inset: true, + }]) + }) .child({ let (thumb_width, thumb_height) = size.thumb(); div() diff --git a/crates/audio/src/audio_data.rs b/crates/audio/src/audio_data.rs index 89d85f80285..271ad4a579b 100644 --- a/crates/audio/src/audio_data.rs +++ b/crates/audio/src/audio_data.rs @@ -4,7 +4,12 @@ use ffmpeg::{ frame::Audio as FFAudio, software::resampling, }; -use std::{ops::Range, path::Path}; +use std::{ + ffi::{c_int, c_void}, + ops::Range, + path::Path, + ptr::{self, NonNull}, +}; use crate::cast_bytes_to_f32_slice; @@ -242,6 +247,18 @@ impl AudioData { }) } + /// Decodes a small audio file held in memory, such as a bundled asset. + pub fn from_memory(bytes: &[u8]) -> Result { + let mut input = MemoryInput::open(bytes)?; + let (samples, channels) = decode_all(input.input()?)?; + Ok(AudioData { + samples, + channels, + source_start_sample: 0, + covered_source_end_sample: usize::MAX, + }) + } + pub fn channels(&self) -> u16 { self.channels } @@ -500,11 +517,204 @@ fn append_resampled_frame(samples: &mut Vec, frame: &FFAudio) -> Result<(), Ok(()) } +fn decode_all(input_ctx: &mut ffmpeg::format::context::Input) -> Result<(Vec, u16), String> { + let input_stream = input_ctx + .streams() + .best(ffmpeg::media::Type::Audio) + .ok_or_else(|| "No Stream".to_string())?; + let index = input_stream.index(); + let mut decoder = avcodec::Context::from_parameters(input_stream.parameters()) + .and_then(|context| context.decoder().audio()) + .map_err(|e| format!("Decoder / {e}"))?; + let source_channels = decoder.channels().max(1); + if decoder.channel_layout().is_empty() { + decoder.set_channel_layout(ChannelLayout::default(source_channels as i32)); + } + decoder.set_packet_time_base(input_stream.time_base()); + let channels = target_channels_for_source(source_channels); + let mut resampler = resampling::Context::get_with( + decoder.format(), + decoder.channel_layout(), + decoder.rate(), + AudioData::SAMPLE_FORMAT, + ChannelLayout::default(channels as i32), + AudioData::SAMPLE_RATE, + high_quality_resampler_options(), + ) + .map_err(|e| format!("Resampler / {e}"))?; + let mut frame = FFAudio::empty(); + let mut output = ResamplerOutput::new(); + let mut samples = Vec::new(); + for (stream, packet) in input_ctx.packets() { + if stream.index() != index { + continue; + } + decoder + .send_packet(&packet) + .map_err(|e| format!("Send Packet / {e}"))?; + while decoder.receive_frame(&mut frame).is_ok() { + run_resampler(&mut resampler, &frame, &mut output, &mut samples)?; + } + } + decoder.send_eof().map_err(|e| format!("Send EOF / {e}"))?; + while decoder.receive_frame(&mut frame).is_ok() { + run_resampler(&mut resampler, &frame, &mut output, &mut samples)?; + } + flush_resampler(&mut resampler, &mut output, &mut samples)?; + Ok((samples, channels)) +} + +struct MemoryReader<'a> { + bytes: &'a [u8], + position: usize, +} + +unsafe extern "C" fn read_memory(opaque: *mut c_void, buffer: *mut u8, size: c_int) -> c_int { + let reader = unsafe { &mut *opaque.cast::() }; + let remaining = reader.bytes.len().saturating_sub(reader.position); + if remaining == 0 { + return ffmpeg::ffi::AVERROR_EOF; + } + let count = remaining.min(usize::try_from(size).unwrap_or(0)); + unsafe { + ptr::copy_nonoverlapping(reader.bytes.as_ptr().add(reader.position), buffer, count); + } + reader.position += count; + count as c_int +} + +unsafe extern "C" fn seek_memory(opaque: *mut c_void, offset: i64, whence: c_int) -> i64 { + let reader = unsafe { &mut *opaque.cast::() }; + let len = reader.bytes.len() as i64; + let target = match whence & !ffmpeg::ffi::AVSEEK_FORCE { + ffmpeg::ffi::AVSEEK_SIZE => return len, + ffmpeg::ffi::SEEK_SET => offset, + ffmpeg::ffi::SEEK_CUR => reader.position as i64 + offset, + ffmpeg::ffi::SEEK_END => len + offset, + _ => return i64::from(ffmpeg::ffi::AVERROR(ffmpeg::ffi::EINVAL)), + }; + if !(0..=len).contains(&target) { + return i64::from(ffmpeg::ffi::AVERROR(ffmpeg::ffi::EINVAL)); + } + reader.position = target as usize; + target +} + +/// A demuxer reading from a byte slice through custom FFmpeg I/O. The input +/// is closed before the I/O context and the reader it points at are freed. +struct MemoryInput<'a> { + input: Option, + io: NonNull, + _reader: Box>, +} + +impl<'a> MemoryInput<'a> { + fn open(bytes: &'a [u8]) -> Result { + const BUFFER_SIZE: usize = 16 * 1024; + let mut reader = Box::new(MemoryReader { bytes, position: 0 }); + let buffer = unsafe { ffmpeg::ffi::av_malloc(BUFFER_SIZE) }.cast::(); + if buffer.is_null() { + return Err("Out of memory".into()); + } + let io = unsafe { + ffmpeg::ffi::avio_alloc_context( + buffer, + BUFFER_SIZE as c_int, + 0, + ptr::from_mut(reader.as_mut()).cast(), + Some(read_memory), + None, + Some(seek_memory), + ) + }; + let Some(io) = NonNull::new(io) else { + unsafe { ffmpeg::ffi::av_free(buffer.cast()) }; + return Err("Out of memory".into()); + }; + let mut opened = Self { + input: None, + io, + _reader: reader, + }; + let mut context = unsafe { ffmpeg::ffi::avformat_alloc_context() }; + if context.is_null() { + return Err("Out of memory".into()); + } + unsafe { + (*context).pb = opened.io.as_ptr(); + (*context).flags |= ffmpeg::ffi::AVFMT_FLAG_CUSTOM_IO; + let result = ffmpeg::ffi::avformat_open_input( + &mut context, + ptr::null(), + ptr::null(), + ptr::null_mut(), + ); + if result < 0 { + return Err(format!("Input Open / {}", ffmpeg::Error::from(result))); + } + let found = ffmpeg::ffi::avformat_find_stream_info(context, ptr::null_mut()); + opened.input = Some(ffmpeg::format::context::Input::wrap(context)); + if found < 0 { + return Err(format!("Stream Info / {}", ffmpeg::Error::from(found))); + } + } + Ok(opened) + } + + fn input(&mut self) -> Result<&mut ffmpeg::format::context::Input, String> { + self.input + .as_mut() + .ok_or_else(|| "Input not open".to_string()) + } +} + +impl Drop for MemoryInput<'_> { + fn drop(&mut self) { + drop(self.input.take()); + let mut io = self.io.as_ptr(); + unsafe { + ffmpeg::ffi::av_free((*io).buffer.cast()); + (*io).buffer = ptr::null_mut(); + ffmpeg::ffi::avio_context_free(&mut io); + } + } +} + #[cfg(test)] mod tests { use super::*; use std::path::Path; + #[test] + fn decodes_audio_held_in_memory_like_the_same_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("tone.ogg"); + let status = std::process::Command::new("ffmpeg") + .args([ + "-v", + "error", + "-f", + "lavfi", + "-i", + "sine=frequency=440:duration=0.5", + ]) + .args(["-ac", "1", "-c:a", "libopus", "-b:a", "64k"]) + .arg(&path) + .status(); + if !status.is_ok_and(|status| status.success()) { + eprintln!("ffmpeg CLI with libopus unavailable, skipping"); + return; + } + let bytes = std::fs::read(&path).unwrap(); + let from_memory = AudioData::from_memory(&bytes).unwrap(); + let from_file = AudioData::from_file(&path).unwrap(); + assert_eq!(from_memory.channels(), 1); + assert_eq!(from_memory.samples(), from_file.samples()); + assert!((from_memory.sample_count() as i64 - 24_000).abs() < 480); + assert!(AudioData::from_memory(b"not audio").is_err()); + let _ = AudioData::from_memory(&bytes[..bytes.len() / 3]); + } + #[cfg(unix)] struct CommaDecimalThreadLocale { locale: libc::locale_t, diff --git a/crates/editor/assets/input-sounds/README.md b/crates/editor/assets/input-sounds/README.md new file mode 100644 index 00000000000..5fff595fc5a --- /dev/null +++ b/crates/editor/assets/input-sounds/README.md @@ -0,0 +1,42 @@ +# Input sounds + +Key press and mouse click sounds the editor can mix into preview and export +(Keyboard settings > Key sounds, Cursor settings > Click sounds). + +Every recording below is CC0 1.0 (public domain dedication) on Freesound, so it +can be redistributed and modified without attribution. The credits are kept +here anyway. + +| File | Sound | Source | Author | Licence | +| --- | --- | --- | --- | --- | +| `mechanical.opus` | Mechanical keyboard, isolated presses and releases | [mechanical keyboard.wav](https://freesound.org/people/AdrianoAnjos/sounds/404298/) | AdrianoAnjos | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `deep.opus` | Linear-switch keyboard, letters | [Keychron k10 A to + individual keys](https://freesound.org/people/Sadiquecat/sounds/789628/) | Sadiquecat | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `deep.opus` | Same keyboard, space bar | [Keychron k10 space_bar](https://freesound.org/people/Sadiquecat/sounds/789630/) | Sadiquecat | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `laptop.opus` | Laptop keyboard keystrokes | [Computer keyboard typing and keystrokes - Apple MacBook Pro 2018](https://freesound.org/people/khenshom/sounds/565645/) | khenshom | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `typewriter.opus` | Typewriter keys and carriage return | [Foley_Object_Typewriter_Keys_&_Return_Mono_NTG4.wav](https://freesound.org/people/Nox_Sound/sounds/550340/) | Nox_Sound | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `mouse.opus` | Mouse button presses and releases | [Computer Mouse Clicks - Single, Double and Soft Clicks](https://freesound.org/people/ApeinCave/sounds/855467/) | ApeinCave | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | +| `trackpad.opus` | Laptop trackpad presses and releases | [trackpad computer (MacBook Pro).wav](https://freesound.org/people/seiswork/sounds/274220/) | seiswork | [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/) | + +## Processing + +Each pack is one Ogg Opus file (mono, 48 kHz, 96 kb/s VBR) holding every +sample back to back with 20 ms of silence between them. `src/input_sounds/packs.rs` +lists where each sample starts and how long it is, in 48 kHz frames from the +start of the decoded file. + +From each source recording: + +1. Downmixed to mono at 48 kHz and high-passed at 35 Hz (90 Hz for `deep`, whose + source carries desk rumble). The laptop and trackpad sources were spectrally + denoised against their own room tone. +2. Only isolated keystrokes or clicks were kept. Each is split at the release + transient into a press sample and a release sample, starting 1 ms before its + attack, trimmed where it decays 60 dB below its peak, and given a raised + cosine fade out so no tail is cut off abruptly. +3. Press samples within a pack were levelled to the same short-term loudness + (halfway toward the pack median, so natural variation survives), releases + keep their level relative to their press, and every sample peaks below + -1 dBFS. +4. The keys with the most low-frequency energy and the longest ring became the + space, enter and backspace samples, the softest presses became modifiers, + and the rest are letters. The typewriter's carriage return is its enter key. diff --git a/crates/editor/assets/input-sounds/deep.opus b/crates/editor/assets/input-sounds/deep.opus new file mode 100644 index 00000000000..b9c99ed3f2c Binary files /dev/null and b/crates/editor/assets/input-sounds/deep.opus differ diff --git a/crates/editor/assets/input-sounds/laptop.opus b/crates/editor/assets/input-sounds/laptop.opus new file mode 100644 index 00000000000..f992284cc06 Binary files /dev/null and b/crates/editor/assets/input-sounds/laptop.opus differ diff --git a/crates/editor/assets/input-sounds/mechanical.opus b/crates/editor/assets/input-sounds/mechanical.opus new file mode 100644 index 00000000000..b3d56e8f2f1 Binary files /dev/null and b/crates/editor/assets/input-sounds/mechanical.opus differ diff --git a/crates/editor/assets/input-sounds/mouse.opus b/crates/editor/assets/input-sounds/mouse.opus new file mode 100644 index 00000000000..1c21bd0848e Binary files /dev/null and b/crates/editor/assets/input-sounds/mouse.opus differ diff --git a/crates/editor/assets/input-sounds/trackpad.opus b/crates/editor/assets/input-sounds/trackpad.opus new file mode 100644 index 00000000000..28007dc5a33 Binary files /dev/null and b/crates/editor/assets/input-sounds/trackpad.opus differ diff --git a/crates/editor/assets/input-sounds/typewriter.opus b/crates/editor/assets/input-sounds/typewriter.opus new file mode 100644 index 00000000000..1130fab17c8 Binary files /dev/null and b/crates/editor/assets/input-sounds/typewriter.opus differ diff --git a/crates/editor/examples/audio-output-idle-benchmark.rs b/crates/editor/examples/audio-output-idle-benchmark.rs index 380a1d5b414..99031884510 100644 --- a/crates/editor/examples/audio-output-idle-benchmark.rs +++ b/crates/editor/examples/audio-output-idle-benchmark.rs @@ -64,6 +64,7 @@ fn spec(tone: &Arc) -> PlaySpec { SegmentAudioTimingRepair::default(), )], music: MusicTracks::new(), + input_sounds: None, project, duration_secs: CLIP_SECS, start_playhead_secs: 0.0, diff --git a/crates/editor/examples/input-sounds-benchmark.rs b/crates/editor/examples/input-sounds-benchmark.rs new file mode 100644 index 00000000000..d49f5d1b85b --- /dev/null +++ b/crates/editor/examples/input-sounds-benchmark.rs @@ -0,0 +1,198 @@ +use cap_editor::{AudioRenderer, AudioSegment, InputSounds, create_segments, get_audio_segments}; +use cap_project::{ + ClickSoundType, KeySoundType, KeyboardData, ProjectConfiguration, RecordingMeta, +}; +use std::{ + io::Write, + path::{Path, PathBuf}, + sync::Arc, + time::{Duration, Instant}, +}; + +const BLOCK: usize = 1024; + +fn render( + segments: &[AudioSegment], + project: &ProjectConfiguration, + sounds: Option>>, +) -> (Vec, Vec) { + let renderer = AudioRenderer::new(segments.to_vec()); + let mut renderer = match sounds { + Some(sounds) => renderer.with_input_sounds(sounds), + None => renderer, + }; + renderer.set_playhead(0.0, project); + let mut output = Vec::new(); + let mut timings = Vec::new(); + loop { + let started = Instant::now(); + let Some((_, block)) = renderer.render_frame_raw(BLOCK, project) else { + break; + }; + timings.push(started.elapsed()); + output.extend_from_slice(&block); + } + (output, timings) +} + +fn write_wav(path: &Path, samples: &[f32]) { + let data_bytes = (samples.len() * 4) as u32; + let mut file = std::io::BufWriter::new(std::fs::File::create(path).unwrap()); + file.write_all(b"RIFF").unwrap(); + file.write_all(&(36 + data_bytes).to_le_bytes()).unwrap(); + file.write_all(b"WAVEfmt ").unwrap(); + file.write_all(&16u32.to_le_bytes()).unwrap(); + file.write_all(&3u16.to_le_bytes()).unwrap(); + file.write_all(&2u16.to_le_bytes()).unwrap(); + file.write_all(&48_000u32.to_le_bytes()).unwrap(); + file.write_all(&(48_000u32 * 8).to_le_bytes()).unwrap(); + file.write_all(&8u16.to_le_bytes()).unwrap(); + file.write_all(&32u16.to_le_bytes()).unwrap(); + file.write_all(b"data").unwrap(); + file.write_all(&data_bytes.to_le_bytes()).unwrap(); + for sample in samples { + file.write_all(&sample.to_le_bytes()).unwrap(); + } +} + +fn stats(label: &str, timings: &[Duration]) { + let mut micros: Vec = timings + .iter() + .map(|time| time.as_secs_f64() * 1e6) + .collect(); + micros.sort_by(f64::total_cmp); + let mean = micros.iter().sum::() / micros.len() as f64; + let at = |q: f64| micros[((micros.len() - 1) as f64 * q) as usize]; + println!( + "{label:<28} blocks={:>5} mean={mean:>7.2}us p50={:>7.2}us p99={:>7.2}us max={:>7.2}us total={:>7.2}ms", + micros.len(), + at(0.5), + at(0.99), + micros.last().unwrap(), + micros.iter().sum::() / 1000.0, + ); +} + +fn with_sounds( + project: &ProjectConfiguration, + keys: Option, + clicks: Option, +) -> ProjectConfiguration { + let mut project = project.clone(); + let keyboard = project.keyboard.get_or_insert_with(KeyboardData::default); + keyboard.settings.key_sounds = keys.is_some(); + keyboard.settings.key_sound_type = keys.unwrap_or_default(); + project.cursor.click_sounds = clicks.is_some(); + project.cursor.click_sound_type = clicks.unwrap_or_default(); + project +} + +#[tokio::main] +async fn main() { + ffmpeg::init().unwrap(); + let mut args = std::env::args_os().skip(1); + let project_path = PathBuf::from(args.next().expect("project path")); + let out = PathBuf::from(args.next().expect("output directory")); + std::fs::create_dir_all(&out).unwrap(); + + let meta = RecordingMeta::load_for_project(&project_path).unwrap(); + let studio = meta.studio_meta().unwrap().clone(); + let mut base = ProjectConfiguration::load(&project_path).unwrap(); + cap_project::synchronize_legacy_keyboard(&meta, &mut base); + let media = create_segments(&meta, &studio, false).await.unwrap(); + let segments = get_audio_segments(&media).await; + let off = with_sounds(&base, None, None); + + let started = Instant::now(); + let cold = InputSounds::for_segments( + &with_sounds( + &base, + Some(KeySoundType::Mechanical), + Some(ClickSoundType::Mouse), + ), + &media, + ) + .unwrap(); + let cold_build = started.elapsed(); + let started = Instant::now(); + let warm = InputSounds::for_segments( + &with_sounds( + &base, + Some(KeySoundType::Mechanical), + Some(ClickSoundType::Mouse), + ), + &media, + ) + .unwrap(); + let warm_build = started.elapsed(); + println!( + "build: first {:.2}ms (decodes two packs), again {:.3}ms, {} sounds", + cold_build.as_secs_f64() * 1e3, + warm_build.as_secs_f64() * 1e3, + warm.len() + ); + drop(cold); + + let (main_like, main_timings) = render(&segments, &off, None); + let off_sounds = InputSounds::for_segments(&off, &media); + assert!(off_sounds.is_none()); + let (toggles_off, off_timings) = render(&segments, &off, Some(off_sounds)); + let identical = main_like.len() == toggles_off.len() + && main_like + .iter() + .zip(&toggles_off) + .all(|(a, b)| a.to_bits() == b.to_bits()); + println!( + "toggles off vs no input sounds: {} samples, byte identical: {identical}", + main_like.len() + ); + assert!(identical); + stats("render, sounds off", &main_timings); + stats("render, toggles off", &off_timings); + + let on = with_sounds( + &base, + Some(KeySoundType::Mechanical), + Some(ClickSoundType::Mouse), + ); + let (combined, on_timings) = render(&segments, &on, Some(Some(warm.clone()))); + stats("render, keys + clicks on", &on_timings); + write_wav(&out.join("keys-mechanical-and-mouse-clicks.wav"), &combined); + + let frames = combined.len() / 2; + let mut block = vec![0.0f32; BLOCK * 2]; + let mut mix_timings = Vec::new(); + for _ in 0..20 { + let mut position = 0; + while position < frames { + block.fill(0.0); + let started = Instant::now(); + warm.mix(&mut block, position); + mix_timings.push(started.elapsed()); + position += BLOCK; + } + } + stats("mix only, 1024 frames", &mix_timings); + + for kind in [ + KeySoundType::Mechanical, + KeySoundType::Deep, + KeySoundType::Laptop, + KeySoundType::Typewriter, + ] { + let project = with_sounds(&base, Some(kind), None); + let sounds = InputSounds::for_segments(&project, &media); + let (audio, _) = render(&segments, &project, Some(sounds)); + let name = format!("keys-{}.wav", format!("{kind:?}").to_lowercase()); + write_wav(&out.join(&name), &audio); + println!("wrote {name}"); + } + for kind in [ClickSoundType::Mouse, ClickSoundType::Trackpad] { + let project = with_sounds(&base, None, Some(kind)); + let sounds = InputSounds::for_segments(&project, &media); + let (audio, _) = render(&segments, &project, Some(sounds)); + let name = format!("clicks-{}.wav", format!("{kind:?}").to_lowercase()); + write_wav(&out.join(&name), &audio); + println!("wrote {name}"); + } +} diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rs index f762c72de82..932280f7aca 100644 --- a/crates/editor/src/audio.rs +++ b/crates/editor/src/audio.rs @@ -1,4 +1,5 @@ use crate::export_audio::{EXPORT_AUDIO_BLOCK_SAMPLES, ExportAudioError, ExportAudioSources}; +use crate::input_sounds::InputSounds; use cap_audio::{ AudioData, AudioRendererTrack, DeclickStrength, DecodedAudio, FromSampleBytes, StereoMode, ToneFilter, VoiceAudio, VoiceEnhancer, VoiceSource, VoiceTone, VoiceToneSettings, @@ -32,6 +33,8 @@ use tracing::{info, warn}; /// of the recording audio in output/timeline time. pub type MusicTracks = HashMap>; +type PlaybackInputs = (Vec, MusicTracks, Option>); + pub struct AudioRenderer { data: Vec, cursor: AudioRendererCursor, @@ -44,6 +47,7 @@ pub struct AudioRenderer { speed_audio_processors: [Option; 2], speed_audio_use_counter: u64, voice_enhancement: VoiceEnhancementCache, + input_sounds: Option>, } #[derive(Clone, Copy, Debug)] @@ -217,6 +221,7 @@ impl AudioRenderer { speed_audio_processors: [None, None], speed_audio_use_counter: 0, voice_enhancement: VoiceEnhancementCache::default(), + input_sounds: None, } } @@ -225,6 +230,11 @@ impl AudioRenderer { self } + pub fn with_input_sounds(mut self, input_sounds: Option>) -> Self { + self.input_sounds = input_sounds; + self + } + pub fn set_playhead(&mut self, playhead: f64, project: &ProjectConfiguration) { self.elapsed_samples = self.playhead_to_samples(playhead); self.speed_audio_processors = [None, None]; @@ -285,6 +295,9 @@ impl AudioRenderer { if !self.music.is_empty() && !timeline.audio_segments.is_empty() { mix_music(&self.music, timeline, frame_start, written, &mut buf); } + if let Some(input_sounds) = &self.input_sounds { + input_sounds.mix(&mut buf, frame_start); + } return Some((written, buf)); } @@ -437,6 +450,9 @@ impl AudioRenderer { self.elapsed_samples + span_offset, &joins, ); + if let Some(input_sounds) = &self.input_sounds { + input_sounds.mix(output, self.elapsed_samples + span_offset); + } emit(written + span_offset, output)?; span_offset += count; } @@ -2033,6 +2049,7 @@ struct CachedProgressiveRender { struct ProgressiveRenderKey { segments: Vec, music: Vec<(String, Arc)>, + input_sounds: Option, project: Vec, duration_bits: u64, sample_rate: u32, @@ -2043,6 +2060,7 @@ impl ProgressiveRenderKey { fn new( segments: Vec, music: &MusicTracks, + input_sounds: Option<&InputSounds>, project: &ProjectConfiguration, duration_secs: f64, output_info: AudioInfo, @@ -2055,6 +2073,7 @@ impl ProgressiveRenderKey { Some(Self { segments, music, + input_sounds: input_sounds.map(InputSounds::fingerprint), project: audio_project_key(project)?, duration_bits: duration_secs.to_bits(), sample_rate: output_info.sample_rate, @@ -2066,6 +2085,7 @@ impl ProgressiveRenderKey { impl PartialEq for ProgressiveRenderKey { fn eq(&self, other: &Self) -> bool { self.project == other.project + && self.input_sounds == other.input_sounds && self.duration_bits == other.duration_bits && self.sample_rate == other.sample_rate && self.channels == other.channels @@ -2275,6 +2295,7 @@ impl> PrerenderedAudioBuffer { fn new( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: &ProjectConfiguration, output_info: AudioInfo, duration_secs: f64, @@ -2283,6 +2304,7 @@ impl> PrerenderedAudioBuffer { Self::create( segments, music, + input_sounds, project, output_info, duration_secs, @@ -2298,6 +2320,7 @@ impl> PrerenderedAudioBuffer { pub fn new_cached( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: &ProjectConfiguration, output_info: AudioInfo, duration_secs: f64, @@ -2307,6 +2330,7 @@ impl> PrerenderedAudioBuffer { Self::create( segments, music, + input_sounds, project, output_info, duration_secs, @@ -2315,9 +2339,11 @@ impl> PrerenderedAudioBuffer { ) } + #[allow(clippy::too_many_arguments)] fn create( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: &ProjectConfiguration, output_info: AudioInfo, duration_secs: f64, @@ -2332,11 +2358,14 @@ impl> PrerenderedAudioBuffer { ); let estimated_memory_bytes = progressive_buffer_bytes(estimated_output_samples); - let (segments, music) = if estimated_memory_bytes <= MAX_PROGRESSIVE_BUFFER_BYTES { + let (segments, music, input_sounds) = if estimated_memory_bytes + <= MAX_PROGRESSIVE_BUFFER_BYTES + { let plan = cache.and_then(|_| { ProgressiveAudioBuffer::::cache_plan( segments.clone(), &music, + input_sounds.as_deref(), project, output_info, duration_secs, @@ -2355,6 +2384,7 @@ impl> PrerenderedAudioBuffer { match ProgressiveAudioBuffer::new( segments, music, + input_sounds, project, output_info, duration_secs, @@ -2385,7 +2415,7 @@ impl> PrerenderedAudioBuffer { max_memory_mb = MAX_PROGRESSIVE_BUFFER_BYTES / (1024 * 1024), "Using bounded streaming audio renderer for long playback" ); - (segments, music) + (segments, music, input_sounds) }; if let Some(cache) = cache { @@ -2395,6 +2425,7 @@ impl> PrerenderedAudioBuffer { mode: PrerenderedAudioBufferMode::Streaming(Box::new(StreamingAudioBuffer::new( segments, music, + input_sounds, project.clone(), output_info, start_playhead_secs, @@ -2405,6 +2436,7 @@ impl> PrerenderedAudioBuffer { pub(crate) fn bounded( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: &ProjectConfiguration, output_info: AudioInfo, start_playhead_secs: f64, @@ -2413,6 +2445,7 @@ impl> PrerenderedAudioBuffer { mode: PrerenderedAudioBufferMode::Streaming(Box::new(StreamingAudioBuffer::new( segments, music, + input_sounds, project.clone(), output_info, start_playhead_secs, @@ -2547,15 +2580,17 @@ impl> ProgressiveAudioBuffer { /// small backwards drift corrections stay inside the first rendered pass. const START_PREROLL_SECS: f64 = 1.0; + #[allow(clippy::too_many_arguments)] fn new( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: &ProjectConfiguration, output_info: AudioInfo, duration_secs: f64, start_playhead_secs: f64, cache: Option<&PlaybackAudioCache>, - ) -> Result, MusicTracks)> { + ) -> Result { // Clamp output info for FFmpeg compatibility (max 8 channels) let output_info = output_info.for_ffmpeg_output(); let estimated_output_samples = progressive_buffer_samples( @@ -2591,7 +2626,7 @@ impl> ProgressiveAudioBuffer { let Some(timeline) = ProgressiveTimeline::new(estimated_output_samples, primary_start) else { - return Err((segments, music)); + return Err((segments, music, input_sounds)); }; let timeline = Arc::new(timeline); @@ -2617,6 +2652,7 @@ impl> ProgressiveAudioBuffer { timeline.clone(), segments, music, + input_sounds, project.clone(), render_info, duration_secs, @@ -2668,6 +2704,7 @@ impl> ProgressiveAudioBuffer { fn cache_plan( segments: Vec, music: &MusicTracks, + input_sounds: Option<&InputSounds>, project: &ProjectConfiguration, output_info: AudioInfo, duration_secs: f64, @@ -2678,7 +2715,14 @@ impl> ProgressiveAudioBuffer { let shift = origin_shift(pass1_start_secs)?; let key = render_is_origin_independent(project, output_info, duration_secs) .then(|| { - ProgressiveRenderKey::new(segments, music, project, duration_secs, output_info) + ProgressiveRenderKey::new( + segments, + music, + input_sounds, + project, + duration_secs, + output_info, + ) }) .flatten()?; let (rate, channels) = (output_info.sample_rate, output_info.channels); @@ -2887,6 +2931,7 @@ impl> StreamingAudioBuffer { fn new( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: ProjectConfiguration, output_info: AudioInfo, start_playhead_secs: f64, @@ -2922,6 +2967,7 @@ impl> StreamingAudioBuffer { let mut producer = StreamingAudioProducer::::new( segments, music, + input_sounds, project, render_info, start_playhead_secs, @@ -3124,6 +3170,7 @@ impl StreamingAudioProducer { fn new( segments: Vec, music: MusicTracks, + input_sounds: Option>, project: ProjectConfiguration, output_info: AudioInfo, start_playhead_secs: f64, @@ -3135,7 +3182,9 @@ impl StreamingAudioProducer { .max(Self::PROCESSING_SAMPLES_COUNT * output_info.channels * 4); let mut buffer = Self { - renderer: AudioRenderer::new(segments).with_music(music), + renderer: AudioRenderer::new(segments) + .with_music(music) + .with_input_sounds(input_sounds), resampler: AudioResampler::new(output_info).unwrap(), resampled_buffer: HeapRb::new(capacity), project, @@ -3272,6 +3321,7 @@ fn spawn_progressive_render( timeline: Arc, segments: Vec, music: MusicTracks, + input_sounds: Option>, project: ProjectConfiguration, render_info: AudioInfo, duration_secs: f64, @@ -3288,7 +3338,9 @@ fn spawn_progressive_render( release: None, adopted: false, }; - let mut renderer = AudioRenderer::new(segments).with_music(music); + let mut renderer = AudioRenderer::new(segments) + .with_music(music) + .with_input_sounds(input_sounds); let chunk_size = 1024usize; @@ -3763,6 +3815,7 @@ mod tests { let mut buffer = PrerenderedAudioBuffer::::new( Vec::new(), MusicTracks::new(), + None, &ProjectConfiguration::default(), AudioRenderer::info(), 1.0, @@ -3789,6 +3842,7 @@ mod tests { let buffer = PrerenderedAudioBuffer::::new( Vec::new(), MusicTracks::new(), + None, &ProjectConfiguration::default(), output_info, duration_secs, @@ -3846,6 +3900,7 @@ mod tests { let mut buffer = PrerenderedAudioBuffer::::new_cached( fixture.segments.clone(), fixture.music.clone(), + None, &fixture.project, output_info, duration, @@ -4034,6 +4089,7 @@ mod tests { let mut playback = PrerenderedAudioBuffer::::new( renderer.data.clone(), MusicTracks::new(), + None, &project, AudioRenderer::info(), duration_secs, @@ -4099,6 +4155,7 @@ mod tests { let mut playback = PrerenderedAudioBuffer::::new( renderer.data.clone(), MusicTracks::new(), + None, &project, AudioRenderer::info(), duration, @@ -4454,6 +4511,7 @@ mod tests { let mut reference = StreamingAudioProducer::::new( renderer.data.clone(), MusicTracks::new(), + None, project.clone(), info, 0.0, @@ -4461,6 +4519,7 @@ mod tests { let mut candidate = StreamingAudioBuffer::::new( renderer.data.clone(), MusicTracks::new(), + None, project.clone(), info, 0.0, @@ -4497,6 +4556,7 @@ mod tests { StreamingAudioProducer::::new( renderer.data.clone(), MusicTracks::new(), + None, project.clone(), info, 0.0, @@ -4643,6 +4703,7 @@ mod tests { let mut playback_buffer = PrerenderedAudioBuffer::::new( segments, MusicTracks::new(), + None, &project, AudioRenderer::info(), 3.0, @@ -5211,6 +5272,7 @@ mod tests { PrerenderedAudioBuffer::new( self.segments.clone(), self.music.clone(), + None, &self.project, output_info, self.duration(), @@ -5227,6 +5289,7 @@ mod tests { PrerenderedAudioBuffer::new_cached( self.segments.clone(), self.music.clone(), + None, &self.project, output_info, self.duration(), @@ -5607,6 +5670,7 @@ mod tests { let mut streaming = PrerenderedAudioBuffer::::new( fixture.segments.clone(), fixture.music.clone(), + None, &fixture.project, output_info, duration, @@ -5981,6 +6045,7 @@ mod tests { let plan = ProgressiveAudioBuffer::::cache_plan( fixture.segments.clone(), &fixture.music, + None, &fixture.project, output_info, fixture.duration(), @@ -5996,6 +6061,7 @@ mod tests { let fresh = ProgressiveAudioBuffer::::new( fixture.segments.clone(), fixture.music.clone(), + None, &fixture.project, output_info, fixture.duration(), diff --git a/crates/editor/src/audio_output.rs b/crates/editor/src/audio_output.rs index 9ad05339f20..a74aa51c6b6 100644 --- a/crates/editor/src/audio_output.rs +++ b/crates/editor/src/audio_output.rs @@ -59,6 +59,7 @@ const IDLE_PAUSE_AFTER: Duration = Duration::from_secs(15); pub struct PlaySpec { pub segments: Vec, pub music: MusicTracks, + pub input_sounds: Option>, pub project: ProjectConfiguration, pub duration_secs: f64, pub start_playhead_secs: f64, @@ -1108,6 +1109,7 @@ fn install_source>( let PlaySpec { segments, music, + input_sounds, project, duration_secs, start_playhead_secs, @@ -1155,11 +1157,19 @@ fn install_source>( let start_playhead = start_playhead_secs + initial_latency_secs; let mut buffer = if matches!(ack, SourceAcknowledgement::Refresh(..)) { - PrerenderedAudioBuffer::::bounded(segments, music, &project, output_info, start_playhead) + PrerenderedAudioBuffer::::bounded( + segments, + music, + input_sounds, + &project, + output_info, + start_playhead, + ) } else { PrerenderedAudioBuffer::::new_cached( segments, music, + input_sounds, &project, output_info, duration_secs, @@ -1796,6 +1806,7 @@ mod tests { Box::new(PlaySpec { segments: Vec::new(), music: MusicTracks::new(), + input_sounds: None, project: ProjectConfiguration::default(), duration_secs: 2.0, start_playhead_secs: 0.0, @@ -1841,6 +1852,7 @@ mod tests { Box::new(PlaySpec { segments: Vec::new(), music: MusicTracks::new(), + input_sounds: None, project: ProjectConfiguration::default(), duration_secs: 60.0, start_playhead_secs: latency, @@ -2073,6 +2085,7 @@ mod tests { replacement.buffer = ActiveSourceBuffer::Ordinary(PrerenderedAudioBuffer::bounded( Vec::new(), MusicTracks::new(), + None, &ProjectConfiguration::default(), AudioInfo::new_raw(AudioData::SAMPLE_FORMAT, 48_000, 2), 3.0, diff --git a/crates/editor/src/audio_output/native_tests.rs b/crates/editor/src/audio_output/native_tests.rs index d031d740530..8f3c3cc18a4 100644 --- a/crates/editor/src/audio_output/native_tests.rs +++ b/crates/editor/src/audio_output/native_tests.rs @@ -113,6 +113,7 @@ fn audible_spec_with_duration(duration_secs: usize) -> PlaySpec { crate::SegmentAudioTimingRepair::default(), )], music: MusicTracks::new(), + input_sounds: None, project: ProjectConfiguration::default(), duration_secs: duration_secs as f64, start_playhead_secs: 0.0, diff --git a/crates/editor/src/completed_audio.rs b/crates/editor/src/completed_audio.rs index 210c590fe4c..dfc3dfad024 100644 --- a/crates/editor/src/completed_audio.rs +++ b/crates/editor/src/completed_audio.rs @@ -207,6 +207,7 @@ pub(crate) mod tests { status: Some(StudioRecordingStatus::Complete), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), } diff --git a/crates/editor/src/editor_instance.rs b/crates/editor/src/editor_instance.rs index 9b31fac4421..d61bf640473 100644 --- a/crates/editor/src/editor_instance.rs +++ b/crates/editor/src/editor_instance.rs @@ -1912,6 +1912,7 @@ pub struct SegmentMedia { pub cursor: Arc, pub hover_changes: Arc, pub keyboard: Arc, + pub keyboard_offset_secs: f64, pub decoders: RecordingSegmentDecoders, } @@ -2222,6 +2223,7 @@ async fn create_segments_with_audio( cursor, hover_changes, keyboard: Arc::new(Default::default()), + keyboard_offset_secs: 0.0, decoders, }]) } @@ -2287,6 +2289,7 @@ async fn create_segments_with_audio( cursor, hover_changes, keyboard, + keyboard_offset_secs: s.latest_start_time().unwrap_or(0.0), decoders, }) }); diff --git a/crates/editor/src/export_audio.rs b/crates/editor/src/export_audio.rs index c172ff98d9e..893080eac48 100644 --- a/crates/editor/src/export_audio.rs +++ b/crates/editor/src/export_audio.rs @@ -1,4 +1,4 @@ -use crate::{AudioRenderer, SegmentMedia}; +use crate::{AudioRenderer, InputSounds, SegmentMedia}; use cap_audio::{ AudioData, AudioRendererTrack, AudioSampleSource, AudioStream, AudioStreamError, ChunkRead, StereoMode, VOICE_PREROLL_SAMPLES, VOICE_PROFILE_SAMPLES, VOICE_TONE_LOOKAHEAD_SAMPLES, @@ -399,6 +399,11 @@ impl ExportAudioRenderer { }) } + pub fn with_input_sounds(mut self, input_sounds: Option>) -> Self { + self.renderer = self.renderer.with_input_sounds(input_sounds); + self + } + pub fn render_chunks( &mut self, samples: usize, diff --git a/crates/editor/src/input_sounds.rs b/crates/editor/src/input_sounds.rs new file mode 100644 index 00000000000..a62655d0bbb --- /dev/null +++ b/crates/editor/src/input_sounds.rs @@ -0,0 +1,617 @@ +use cap_audio::AudioData; +use cap_project::{ + ClickSoundType, KeySoundType, KeyboardEvents, ProjectConfiguration, RecordingMeta, + StudioRecordingMeta, + input_sounds::{ + InputSoundEvent, InputSoundKind, InputSoundTake, KeySoundClass, click_sounds_enabled, + input_sound_events, key_sounds_enabled, + }, +}; +use std::sync::{Arc, OnceLock}; +use tracing::warn; + +use crate::SegmentMedia; + +mod packs; + +#[derive(Clone, Copy)] +struct Stroke { + down: (u32, u32), + up: Option<(u32, u32)>, +} + +struct Pack { + bytes: &'static [u8], + frames: u32, + strokes: [&'static [Stroke]; 6], +} + +const LETTER: usize = 0; +const CLICK: usize = 5; + +#[derive(Clone, Copy, PartialEq, Eq)] +enum PackId { + Mechanical, + Deep, + Laptop, + Typewriter, + Mouse, + Trackpad, +} + +impl PackId { + #[cfg(test)] + const ALL: [Self; 6] = [ + Self::Mechanical, + Self::Deep, + Self::Laptop, + Self::Typewriter, + Self::Mouse, + Self::Trackpad, + ]; + + fn keys(kind: KeySoundType) -> Self { + match kind { + KeySoundType::Mechanical => Self::Mechanical, + KeySoundType::Deep => Self::Deep, + KeySoundType::Laptop => Self::Laptop, + KeySoundType::Typewriter => Self::Typewriter, + } + } + + fn clicks(kind: ClickSoundType) -> Self { + match kind { + ClickSoundType::Mouse => Self::Mouse, + ClickSoundType::Trackpad => Self::Trackpad, + } + } + + fn index(self) -> usize { + self as usize + } + + fn pack(self) -> &'static Pack { + match self { + Self::Mechanical => &packs::MECHANICAL, + Self::Deep => &packs::DEEP, + Self::Laptop => &packs::LAPTOP, + Self::Typewriter => &packs::TYPEWRITER, + Self::Mouse => &packs::MOUSE, + Self::Trackpad => &packs::TRACKPAD, + } + } +} + +/// Mono 48 kHz samples of one bundled pack, decoded on first use and kept for +/// the life of the process. Packs nobody turns on are never decoded. +fn decoded(id: PackId) -> Option<&'static [f32]> { + static DECODED: [OnceLock>>; 6] = [const { OnceLock::new() }; 6]; + DECODED[id.index()] + .get_or_init(|| { + let pack = id.pack(); + let audio = match AudioData::from_memory(pack.bytes) { + Ok(audio) => audio, + Err(error) => { + warn!(%error, "Could not decode an input sound pack"); + return None; + } + }; + let channels = usize::from(audio.channels().max(1)); + let samples: Vec = audio + .samples() + .chunks_exact(channels) + .map(|frame| frame.iter().sum::() / channels as f32) + .collect(); + if samples.len() < pack.frames as usize { + warn!( + decoded = samples.len(), + expected = pack.frames, + "Input sound pack decoded short" + ); + return None; + } + Some(samples) + }) + .as_deref() +} + +#[derive(Clone, Copy)] +struct Voice { + start: usize, + offset: u32, + len: u32, + frames: usize, + step: f32, + left: f32, + right: f32, + pack: PackId, +} + +/// Key and click sounds placed on the output timeline, ready to be mixed into +/// any block of rendered audio. Built once per play or export; mixing reads +/// only pre-sorted voices and never allocates. +pub struct InputSounds { + voices: Vec, + banks: [Option<&'static [f32]>; 6], + longest: usize, + fingerprint: u64, +} + +fn unit(seed: u64, shift: u32) -> f32 { + ((seed >> shift) & 0xffff) as f32 / 65_535.0 +} + +fn volume_gain(volume: f32) -> f32 { + let volume = if volume.is_finite() { + volume.clamp(0.0, 1.0) + } else { + 0.0 + }; + volume * volume +} + +fn class_slot(class: KeySoundClass) -> usize { + match class { + KeySoundClass::Letter => 0, + KeySoundClass::Space => 1, + KeySoundClass::Enter => 2, + KeySoundClass::Backspace => 3, + KeySoundClass::Modifier => 4, + } +} + +/// Stabilised keys sound lower than letters. The shift is small enough to +/// keep the recording's character while separating space from enter. +fn class_rate(class: KeySoundClass) -> f32 { + match class { + KeySoundClass::Letter => 1.0, + KeySoundClass::Space => 0.95, + KeySoundClass::Enter => 0.97, + KeySoundClass::Backspace => 0.985, + KeySoundClass::Modifier => 1.015, + } +} + +fn mix_fingerprint(hash: u64, value: u64) -> u64 { + (hash ^ value).wrapping_mul(0x0100_0000_01b3).rotate_left(5) +} + +impl InputSounds { + pub fn new(project: &ProjectConfiguration, takes: &[InputSoundTake<'_>]) -> Option> { + let events = input_sound_events(project, takes); + Self::from_events(project, &events) + } + + pub fn for_segments( + project: &ProjectConfiguration, + segments: &[SegmentMedia], + ) -> Option> { + if !key_sounds_enabled(project) && !click_sounds_enabled(project) { + return None; + } + let takes = segments + .iter() + .map(|segment| InputSoundTake { + keyboard: &segment.keyboard, + keyboard_offset_secs: segment.keyboard_offset_secs, + clicks: &segment.cursor.clicks, + }) + .collect::>(); + Self::new(project, &takes) + } + + /// Reads each take's input from disk, for renderers that never load + /// [`SegmentMedia`]. + pub fn load( + project: &ProjectConfiguration, + recording: &RecordingMeta, + studio: &StudioRecordingMeta, + ) -> Option> { + let keys = key_sounds_enabled(project); + let clicks = click_sounds_enabled(project); + if !keys && !clicks { + return None; + } + let inputs: Vec<(KeyboardEvents, f64, Vec)> = match studio { + StudioRecordingMeta::SingleSegment { segment } => { + let clicks = segment + .cursor + .as_ref() + .filter(|_| clicks) + .and_then(|path| { + cap_project::CursorEvents::load_from_file(&recording.path(path)).ok() + }) + .map(|cursor| cursor.clicks) + .unwrap_or_default(); + vec![(KeyboardEvents::default(), 0.0, clicks)] + } + StudioRecordingMeta::MultipleSegments { inner } => inner + .segments + .iter() + .map(|segment| { + ( + if keys { + segment.keyboard_events(recording) + } else { + KeyboardEvents::default() + }, + segment.latest_start_time().unwrap_or(0.0), + if clicks { + segment.cursor_events(recording).clicks + } else { + Vec::new() + }, + ) + }) + .collect(), + }; + let takes = inputs + .iter() + .map(|(keyboard, offset, clicks)| InputSoundTake { + keyboard, + keyboard_offset_secs: *offset, + clicks, + }) + .collect::>(); + Self::new(project, &takes) + } + + fn from_events( + project: &ProjectConfiguration, + events: &[InputSoundEvent], + ) -> Option> { + if events.is_empty() { + return None; + } + let key_settings = project.keyboard.as_ref().map(|keyboard| &keyboard.settings); + let key_pack = PackId::keys( + key_settings.map_or(KeySoundType::default(), |settings| settings.key_sound_type), + ); + let click_pack = PackId::clicks(project.cursor.click_sound_type); + let key_gain = volume_gain(key_settings.map_or(0.0, |settings| settings.key_sound_volume)); + let click_gain = volume_gain(project.cursor.click_sound_volume); + Self::schedule(events, key_pack, key_gain, click_pack, click_gain) + } + + fn schedule( + events: &[InputSoundEvent], + key_pack: PackId, + key_gain: f32, + click_pack: PackId, + click_gain: f32, + ) -> Option> { + let rate = AudioData::SAMPLE_RATE as f64; + let mut banks = [None; 6]; + let mut last_variant = [[[usize::MAX; 2]; 6]; 6]; + let mut voices = Vec::with_capacity(events.len()); + for event in events { + let (pack_id, slot, class, down, gain) = match event.kind { + InputSoundKind::Key { class, down } => { + (key_pack, class_slot(class), Some(class), down, key_gain) + } + InputSoundKind::Click { down } => (click_pack, CLICK, None, down, click_gain), + }; + let gain = gain * event.gain; + if gain <= 0.0 { + continue; + } + let bank = match banks[pack_id.index()] { + Some(bank) => bank, + None => { + let Some(bank) = decoded(pack_id) else { + continue; + }; + banks[pack_id.index()] = Some(bank); + bank + } + }; + let pack = pack_id.pack(); + let (pool, slot) = if pack.strokes[slot].is_empty() { + (pack.strokes[LETTER], LETTER) + } else { + (pack.strokes[slot], slot) + }; + if pool.is_empty() { + continue; + } + let seed = event.seed; + let part = usize::from(!down); + let previous = &mut last_variant[pack_id.index()][slot][part]; + let mut variant = (seed % pool.len() as u64) as usize; + if pool.len() > 1 && variant == *previous { + variant = (variant + 1 + (seed >> 20) as usize % (pool.len() - 1)) % pool.len(); + } + *previous = variant; + let stroke = pool[variant]; + let Some((offset, len)) = (if down { Some(stroke.down) } else { stroke.up }) else { + continue; + }; + if len < 4 || (offset + len) as usize > bank.len() { + continue; + } + let jitter = (unit(seed, 8) - 0.5) * 0.03; + let step = class.map_or(1.0, class_rate) * (1.0 + jitter); + let level = gain * 10f32.powf((unit(seed, 24) - 0.5) * 2.0 / 20.0); + let level = if class == Some(KeySoundClass::Modifier) { + level * 0.85 + } else { + level + }; + let pan = (event.pan + (unit(seed, 40) - 0.5) * 0.06).clamp(-1.0, 1.0); + let start = (event.time * rate).round(); + if !start.is_finite() || start < 0.0 || start > usize::MAX as f64 / 2.0 { + continue; + } + voices.push(Voice { + start: start as usize, + offset, + len, + frames: ((len - 1) as f32 / step) as usize + 1, + step, + left: level * (1.0 - pan.max(0.0)), + right: level * (1.0 + pan.min(0.0)), + pack: pack_id, + }); + } + if voices.is_empty() { + return None; + } + voices.sort_by_key(|voice| voice.start); + let longest = voices.iter().map(|voice| voice.frames).max().unwrap_or(0); + let fingerprint = voices.iter().fold(0xcbf2_9ce4_8422_2325, |hash, voice| { + let hash = mix_fingerprint(hash, voice.start as u64); + let hash = + mix_fingerprint(hash, (u64::from(voice.offset) << 32) | u64::from(voice.len)); + let hash = mix_fingerprint(hash, voice.pack as u64); + let hash = mix_fingerprint(hash, u64::from(voice.step.to_bits())); + mix_fingerprint( + hash, + (u64::from(voice.left.to_bits()) << 32) | u64::from(voice.right.to_bits()), + ) + }); + Some(Arc::new(Self { + voices, + banks, + longest, + fingerprint, + })) + } + + pub fn fingerprint(&self) -> u64 { + self.fingerprint + } + + pub fn len(&self) -> usize { + self.voices.len() + } + + pub fn is_empty(&self) -> bool { + self.voices.is_empty() + } + + /// Adds every sound overlapping `[frame_start, frame_start + frames)` to + /// interleaved stereo `output`, where `frame_start` is the output-timeline + /// frame of `output[0]`. + pub fn mix(&self, output: &mut [f32], frame_start: usize) { + let frames = output.len() / 2; + if frames == 0 { + return; + } + let frame_end = frame_start.saturating_add(frames); + let first = self + .voices + .partition_point(|voice| voice.start.saturating_add(self.longest) <= frame_start); + for voice in &self.voices[first..] { + if voice.start >= frame_end { + break; + } + let voice_end = voice.start + voice.frames; + if voice_end <= frame_start { + continue; + } + let Some(bank) = self.banks[voice.pack.index()] else { + continue; + }; + let samples = &bank[voice.offset as usize..(voice.offset + voice.len) as usize]; + let from = frame_start.max(voice.start); + let to = frame_end.min(voice_end); + let out = &mut output[(from - frame_start) * 2..(to - frame_start) * 2]; + let first_frame = from - voice.start; + if voice.step == 1.0 { + for (frame, sample) in out + .chunks_exact_mut(2) + .zip(&samples[first_frame..first_frame + (to - from)]) + { + frame[0] += sample * voice.left; + frame[1] += sample * voice.right; + } + } else { + for (index, frame) in out.chunks_exact_mut(2).enumerate() { + let sample = cubic(samples, (first_frame + index) as f32 * voice.step); + frame[0] += sample * voice.left; + frame[1] += sample * voice.right; + } + } + } + } +} + +fn input_sound_inputs(project: &ProjectConfiguration) -> Option> { + let keys = key_sounds_enabled(project); + let clicks = click_sounds_enabled(project); + if !keys && !clicks { + return None; + } + let settings = project + .keyboard + .as_ref() + .map(|keyboard| &keyboard.settings) + .filter(|_| keys); + let timeline = project.timeline.as_ref(); + serde_json::to_vec(&( + settings.map(|settings| (settings.key_sound_type, settings.key_sound_volume)), + keys.then(|| { + timeline.map(|timeline| { + timeline + .keyboard_segments + .iter() + .map(|segment| { + ( + segment.start, + segment.end, + // A key added by hand sounds the keys its text + // strikes. + cap_project::is_manual_keyboard_segment(segment).then(|| { + segment + .keys + .iter() + .map(|key| (key.key.as_str(), key.time_offset)) + .collect::>() + }), + ) + }) + .collect::>() + }) + }), + clicks.then_some(( + project.cursor.click_sound_type, + project.cursor.click_sound_volume, + project.cursor.hide, + )), + // Style segments that hide or show the cursor gate its clicks. + clicks.then(|| { + timeline.map(|timeline| { + timeline + .style_segments + .iter() + .filter_map(|segment| { + let cursor = segment.overrides.cursor.as_ref()?; + Some(( + segment.start, + segment.end, + segment.track, + segment.enabled, + cursor.hide, + )) + }) + .collect::>() + }) + }), + timeline.map(|timeline| { + ( + timeline + .segments + .iter() + .map(|segment| { + ( + segment.recording_clip, + segment.start, + segment.end, + segment.timescale, + segment.speed_audio_mode, + segment.volume(), + segment.hides_cursor(), + ) + }) + .collect::>(), + &timeline.transitions, + timeline.hold_windows(), + ) + }), + )) + .ok() +} + +/// Whether an edit moves, adds, removes or restyles any key or click sound. +pub(crate) fn input_sounds_changed( + previous: &ProjectConfiguration, + next: &ProjectConfiguration, +) -> bool { + input_sound_inputs(previous) != input_sound_inputs(next) +} + +/// Catmull-Rom interpolation, clamped at the sample's ends. Pitch variation is +/// a few percent, so this keeps transients clean without a filter bank. +fn cubic(samples: &[f32], position: f32) -> f32 { + let last = samples.len() - 1; + let position = position.clamp(0.0, last as f32); + let index = position as usize; + let t = position - index as f32; + let at = |offset: isize| samples[(index as isize + offset).clamp(0, last as isize) as usize]; + let (p0, p1, p2, p3) = (at(-1), at(0), at(1), at(2)); + p1 + 0.5 + * t + * (p2 - p0 + t * (2.0 * p0 - 5.0 * p1 + 4.0 * p2 - p3 + t * (3.0 * (p1 - p2) + p3 - p0))) +} + +#[derive(Clone, Copy, Debug)] +pub enum InputSoundPreview { + Keys(KeySoundType), + Clicks(ClickSoundType), +} + +/// A short phrase of typing or clicking with the chosen pack, as interleaved +/// 48 kHz stereo, so the settings can be auditioned without a recording. +pub fn render_input_sound_preview(preview: InputSoundPreview, volume: f32) -> Option> { + let gain = volume_gain(volume); + let mut events = Vec::new(); + let mut seed = 0x5eed_u64; + let mut push = |time: f64, kind: InputSoundKind, pan: f32| { + seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1); + events.push(InputSoundEvent { + time, + kind, + seed: seed ^ (seed >> 29), + pan, + gain: 1.0, + }); + }; + let (keys, clicks) = match preview { + InputSoundPreview::Keys(kind) => { + let typed: [(KeySoundClass, f32, f64); 12] = [ + (KeySoundClass::Letter, 0.05, 0.0), + (KeySoundClass::Letter, -0.12, 0.11), + (KeySoundClass::Letter, 0.2, 0.2), + (KeySoundClass::Letter, 0.2, 0.31), + (KeySoundClass::Letter, 0.18, 0.43), + (KeySoundClass::Space, 0.0, 0.58), + (KeySoundClass::Letter, -0.17, 0.72), + (KeySoundClass::Letter, 0.18, 0.83), + (KeySoundClass::Letter, -0.1, 0.92), + (KeySoundClass::Letter, 0.2, 1.04), + (KeySoundClass::Letter, -0.08, 1.15), + (KeySoundClass::Enter, 0.27, 1.42), + ]; + for (class, pan, time) in typed { + push(time, InputSoundKind::Key { class, down: true }, pan); + push( + time + 0.075, + InputSoundKind::Key { class, down: false }, + pan, + ); + } + (PackId::keys(kind), PackId::Mouse) + } + InputSoundPreview::Clicks(kind) => { + for time in [0.0, 0.55, 0.68] { + push(time, InputSoundKind::Click { down: true }, 0.0); + push(time + 0.07, InputSoundKind::Click { down: false }, 0.0); + } + (PackId::Mechanical, PackId::clicks(kind)) + } + }; + events.sort_by(|a, b| a.time.total_cmp(&b.time)); + let sounds = InputSounds::schedule(&events, keys, gain, clicks, gain)?; + let frames = sounds + .voices + .iter() + .map(|voice| voice.start + voice.frames) + .max() + .unwrap_or(0); + let mut output = vec![0.0; frames * 2]; + sounds.mix(&mut output, 0); + Some(output) +} + +#[cfg(test)] +mod tests; diff --git a/crates/editor/src/input_sounds/packs.rs b/crates/editor/src/input_sounds/packs.rs new file mode 100644 index 00000000000..10db8d01783 --- /dev/null +++ b/crates/editor/src/input_sounds/packs.rs @@ -0,0 +1,452 @@ +use super::{Pack, Stroke}; + +pub(super) const MECHANICAL: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/mechanical.opus"), + frames: 273264, + strokes: [ + &[ + Stroke { + down: (960, 3888), + up: Some((5808, 4848)), + }, + Stroke { + down: (11616, 3600), + up: Some((16176, 4464)), + }, + Stroke { + down: (21600, 3264), + up: Some((25824, 4368)), + }, + Stroke { + down: (31152, 3216), + up: Some((35328, 5472)), + }, + Stroke { + down: (41760, 3552), + up: Some((46272, 5376)), + }, + Stroke { + down: (52608, 3312), + up: Some((56880, 5232)), + }, + Stroke { + down: (63072, 3600), + up: Some((67632, 4272)), + }, + Stroke { + down: (72864, 3456), + up: Some((77280, 5424)), + }, + Stroke { + down: (83664, 3264), + up: Some((87888, 3600)), + }, + Stroke { + down: (92448, 3648), + up: Some((97056, 4416)), + }, + Stroke { + down: (102432, 3360), + up: Some((106752, 4608)), + }, + Stroke { + down: (112320, 3552), + up: Some((116832, 4896)), + }, + Stroke { + down: (122688, 3936), + up: Some((127584, 5088)), + }, + Stroke { + down: (133632, 3216), + up: Some((137808, 4320)), + }, + Stroke { + down: (143088, 3504), + up: Some((147552, 3840)), + }, + Stroke { + down: (152352, 3504), + up: Some((156816, 4944)), + }, + Stroke { + down: (162720, 2928), + up: Some((166608, 3888)), + }, + Stroke { + down: (171456, 2736), + up: Some((175152, 5376)), + }, + ], + &[ + Stroke { + down: (181488, 3552), + up: Some((186000, 7968)), + }, + Stroke { + down: (194928, 4320), + up: Some((200208, 8112)), + }, + ], + &[ + Stroke { + down: (209280, 4128), + up: Some((214368, 5040)), + }, + Stroke { + down: (220368, 3648), + up: Some((224976, 5472)), + }, + ], + &[ + Stroke { + down: (231408, 3312), + up: Some((235680, 5568)), + }, + Stroke { + down: (242208, 4320), + up: Some((247488, 4848)), + }, + ], + &[ + Stroke { + down: (253296, 3216), + up: Some((257472, 4992)), + }, + Stroke { + down: (263424, 2976), + up: Some((267360, 3984)), + }, + ], + &[], + ], +}; + +pub(super) const DEEP: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/deep.opus"), + frames: 92832, + strokes: [ + &[ + Stroke { + down: (960, 5856), + up: None, + }, + Stroke { + down: (7776, 5712), + up: None, + }, + Stroke { + down: (14448, 5520), + up: None, + }, + Stroke { + down: (20928, 5856), + up: None, + }, + Stroke { + down: (27744, 4368), + up: None, + }, + Stroke { + down: (33072, 5184), + up: None, + }, + Stroke { + down: (39216, 6720), + up: None, + }, + ], + &[ + Stroke { + down: (46896, 4800), + up: None, + }, + Stroke { + down: (52656, 2256), + up: None, + }, + ], + &[ + Stroke { + down: (55872, 5616), + up: None, + }, + Stroke { + down: (62448, 5472), + up: None, + }, + ], + &[ + Stroke { + down: (68880, 5616), + up: None, + }, + Stroke { + down: (75456, 5472), + up: None, + }, + ], + &[ + Stroke { + down: (81888, 4368), + up: None, + }, + Stroke { + down: (87216, 3696), + up: None, + }, + ], + &[], + ], +}; + +pub(super) const LAPTOP: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/laptop.opus"), + frames: 177504, + strokes: [ + &[ + Stroke { + down: (960, 3312), + up: Some((5232, 10848)), + }, + Stroke { + down: (17040, 2448), + up: Some((20448, 11472)), + }, + Stroke { + down: (32880, 2016), + up: Some((35856, 7728)), + }, + Stroke { + down: (44544, 2928), + up: Some((48432, 9648)), + }, + Stroke { + down: (59040, 3600), + up: Some((63600, 7152)), + }, + ], + &[ + Stroke { + down: (71712, 3024), + up: Some((75696, 12720)), + }, + Stroke { + down: (89376, 2784), + up: Some((93120, 11664)), + }, + ], + &[ + Stroke { + down: (105744, 2976), + up: Some((109680, 10224)), + }, + Stroke { + down: (120864, 1968), + up: Some((123792, 10176)), + }, + ], + &[ + Stroke { + down: (134928, 3216), + up: Some((139104, 7824)), + }, + Stroke { + down: (147888, 3696), + up: Some((152544, 10464)), + }, + ], + &[Stroke { + down: (163968, 2928), + up: Some((167856, 7728)), + }], + &[], + ], +}; + +pub(super) const TYPEWRITER: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/typewriter.opus"), + frames: 199872, + strokes: [ + &[ + Stroke { + down: (960, 8640), + up: Some((10560, 10560)), + }, + Stroke { + down: (22080, 7056), + up: Some((30096, 13920)), + }, + Stroke { + down: (44976, 8112), + up: Some((54048, 10896)), + }, + Stroke { + down: (65904, 7440), + up: Some((74304, 12048)), + }, + ], + &[Stroke { + down: (87312, 2352), + up: Some((90624, 18912)), + }], + &[Stroke { + down: (110496, 35472), + up: Some((146928, 26784)), + }], + &[Stroke { + down: (174672, 8112), + up: Some((183744, 14208)), + }], + &[], + &[], + ], +}; + +pub(super) const MOUSE: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/mouse.opus"), + frames: 65520, + strokes: [ + &[], + &[], + &[], + &[], + &[], + &[ + Stroke { + down: (960, 2064), + up: Some((3984, 1008)), + }, + Stroke { + down: (5952, 1056), + up: Some((7968, 1920)), + }, + Stroke { + down: (10848, 1152), + up: Some((12960, 480)), + }, + Stroke { + down: (14400, 2064), + up: Some((17424, 816)), + }, + Stroke { + down: (19200, 2112), + up: Some((22272, 1152)), + }, + Stroke { + down: (24384, 2112), + up: Some((27456, 864)), + }, + Stroke { + down: (29280, 1968), + up: Some((32208, 2016)), + }, + Stroke { + down: (35184, 816), + up: Some((36960, 1728)), + }, + Stroke { + down: (39648, 2064), + up: Some((42672, 576)), + }, + Stroke { + down: (44208, 2064), + up: Some((47232, 480)), + }, + Stroke { + down: (48672, 1920), + up: Some((51552, 816)), + }, + Stroke { + down: (53328, 1680), + up: Some((55968, 864)), + }, + Stroke { + down: (57792, 528), + up: Some((59280, 1008)), + }, + Stroke { + down: (61248, 864), + up: Some((63072, 528)), + }, + ], + ], +}; + +pub(super) const TRACKPAD: Pack = Pack { + bytes: include_bytes!("../../assets/input-sounds/trackpad.opus"), + frames: 100224, + strokes: [ + &[], + &[], + &[], + &[], + &[], + &[ + Stroke { + down: (960, 1344), + up: Some((3264, 4272)), + }, + Stroke { + down: (8496, 2208), + up: Some((11664, 1920)), + }, + Stroke { + down: (14544, 2304), + up: Some((17808, 2208)), + }, + Stroke { + down: (20976, 1440), + up: Some((23376, 2208)), + }, + Stroke { + down: (26544, 1440), + up: Some((28944, 2304)), + }, + Stroke { + down: (32208, 1824), + up: Some((34992, 1920)), + }, + Stroke { + down: (37872, 1296), + up: Some((40128, 2352)), + }, + Stroke { + down: (43440, 1248), + up: Some((45648, 3696)), + }, + Stroke { + down: (50304, 1440), + up: Some((52704, 2304)), + }, + Stroke { + down: (55968, 1296), + up: Some((58224, 2352)), + }, + Stroke { + down: (61536, 2352), + up: Some((64848, 1824)), + }, + Stroke { + down: (67632, 1344), + up: Some((69936, 2880)), + }, + Stroke { + down: (73776, 2208), + up: Some((76944, 1920)), + }, + Stroke { + down: (79824, 2160), + up: Some((82944, 2112)), + }, + Stroke { + down: (86016, 2784), + up: Some((89760, 2208)), + }, + Stroke { + down: (92928, 1344), + up: Some((95232, 3072)), + }, + ], + ], +}; diff --git a/crates/editor/src/input_sounds/tests.rs b/crates/editor/src/input_sounds/tests.rs new file mode 100644 index 00000000000..5fcfa1a351d --- /dev/null +++ b/crates/editor/src/input_sounds/tests.rs @@ -0,0 +1,274 @@ +use super::*; +use cap_project::{CursorClickEvent, KeyPressEvent, KeyboardData, KeyboardSettings}; + +fn rms_db(samples: &[f32]) -> f32 { + let energy = samples.iter().map(|sample| sample * sample).sum::() / samples.len() as f32; + 10.0 * energy.max(1e-20).log10() +} + +#[test] +fn every_pack_decodes_with_each_stroke_on_its_onset() { + for id in PackId::ALL { + let bank = decoded(id).expect("pack decodes"); + let pack = id.pack(); + assert!(bank.len() >= pack.frames as usize); + let mut spans = 0; + for (slot, strokes) in pack.strokes.iter().enumerate() { + for stroke in strokes.iter() { + for (offset, len) in std::iter::once(stroke.down).chain(stroke.up) { + let (offset, len) = (offset as usize, len as usize); + assert!(offset + len <= bank.len()); + let before = rms_db(&bank[offset - 480..offset - 48]); + let attack = rms_db(&bank[offset..offset + 480.min(len)]); + assert!( + attack - before > 20.0, + "pack {} slot {slot} at {offset}: attack {attack:.1} dB, before {before:.1} dB", + id.index() + ); + assert!(bank[offset..offset + len].iter().all(|s| s.abs() < 1.0)); + spans += 1; + } + } + } + assert!(spans >= 12, "pack {} has {spans} sounds", id.index()); + let keys = !pack.strokes[CLICK].is_empty(); + assert_eq!(keys, matches!(id, PackId::Mouse | PackId::Trackpad)); + assert!(pack.strokes[LETTER].len() >= 4 || keys); + } +} + +fn key_project(volume: f32) -> ProjectConfiguration { + let mut project: ProjectConfiguration = serde_json::from_value(serde_json::json!({ + "timeline": { + "segments": [ + { "start": 0.0, "end": 4.0, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 6.0, "end": 8.0, "timescale": 2.0, "recordingSegment": 0, + "speedAudioMode": "maintainPitch" } + ], + "zoomSegments": [], + "keyboardSegments": [ + { "id": "a", "start": 0.0, "end": 9.0, "displayText": "x" } + ] + } + })) + .unwrap(); + project.keyboard = Some(KeyboardData { + settings: KeyboardSettings { + enabled: true, + key_sounds: true, + key_sound_volume: volume, + ..Default::default() + }, + }); + project.cursor.click_sounds = true; + project.cursor.click_sound_volume = volume; + project +} + +fn typing() -> (cap_project::KeyboardEvents, Vec) { + let keys = [ + "A", + "S", + "Space", + "D", + "D", + "D", + "Enter", + "Backspace", + "LShift", + "K", + ]; + let presses = (0..60) + .flat_map(|index| { + let key = keys[index % keys.len()]; + let ms = 200.0 + index as f64 * 120.0; + [true, false].map(|down| KeyPressEvent { + key: key.into(), + key_code: key.into(), + time_ms: if down { ms } else { ms + 70.0 }, + down, + }) + }) + .collect(); + let clicks = (0..10) + .flat_map(|index| { + let ms = 300.0 + index as f64 * 700.0; + [true, false].map(|down| CursorClickEvent { + active_modifiers: Vec::new(), + cursor_num: 0, + cursor_id: "0".into(), + time_ms: if down { ms } else { ms + 90.0 }, + down, + }) + }) + .collect(); + (cap_project::KeyboardEvents { presses }, clicks) +} + +fn sounds(project: &ProjectConfiguration) -> Option> { + let (keyboard, clicks) = typing(); + InputSounds::new( + project, + &[InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }], + ) +} + +#[test] +fn nothing_is_built_when_both_toggles_are_off() { + let mut project = key_project(0.6); + project.keyboard.as_mut().unwrap().settings.key_sounds = false; + project.cursor.click_sounds = false; + assert!(sounds(&project).is_none()); + assert!(sounds(&ProjectConfiguration::default()).is_none()); + project.keyboard.as_mut().unwrap().settings.key_sounds = true; + project.keyboard.as_mut().unwrap().settings.key_sound_volume = 0.0; + project.cursor.click_sound_volume = 0.0; + assert!(sounds(&project).is_none()); +} + +#[test] +fn block_mixing_matches_one_pass_and_is_repeatable() { + let project = key_project(0.8); + let first = sounds(&project).unwrap(); + let second = sounds(&project).unwrap(); + assert_eq!(first.fingerprint(), second.fingerprint()); + let frames = 48_000 * 9; + let mut whole = vec![0.0; frames * 2]; + first.mix(&mut whole, 0); + let mut blocks = vec![0.0; frames * 2]; + let mut position = 0; + for size in [1usize, 7, 480, 1024, 4095, 333].iter().cycle() { + if position >= frames { + break; + } + let count = (*size).min(frames - position); + second.mix(&mut blocks[position * 2..(position + count) * 2], position); + position += count; + } + assert!( + whole + .iter() + .zip(&blocks) + .all(|(a, b)| a.to_bits() == b.to_bits()) + ); + assert!(whole.iter().any(|sample| *sample != 0.0)); + assert!(whole.iter().all(|sample| sample.abs() < 1.0)); +} + +#[test] +fn sounds_start_on_the_press_sample() { + let project = key_project(1.0); + let sounds = sounds(&project).unwrap(); + let mut output = vec![0.0; 48_000 * 9 * 2]; + sounds.mix(&mut output, 0); + let first = output.iter().position(|sample| *sample != 0.0).unwrap() / 2; + assert_eq!(first, (0.2 * 48_000.0) as usize); + let sped_press: f64 = 4.0 + (0.2 + 50.0 * 0.12 - 6.0) / 2.0; + let frame = (sped_press * 48_000.0).round() as usize; + assert!(sounds.voices.iter().any(|voice| voice.start == frame)); + assert!( + !sounds + .voices + .iter() + .any(|voice| voice.start > 4 * 48_000 - 100 && voice.start < 4 * 48_000 - 10) + ); +} + +#[test] +fn repeated_keys_never_repeat_a_recording_back_to_back() { + let project = key_project(0.6); + let sounds = sounds(&project).unwrap(); + let downs: Vec<&Voice> = sounds + .voices + .iter() + .filter(|voice| voice.pack == PackId::Mechanical) + .collect(); + assert!(downs.len() > 50); + let letters: std::collections::HashSet = packs::MECHANICAL.strokes[LETTER] + .iter() + .map(|stroke| stroke.down.0) + .collect(); + let letter_downs: Vec = downs + .iter() + .map(|voice| voice.offset) + .filter(|offset| letters.contains(offset)) + .collect(); + assert!(letter_downs.windows(2).all(|pair| pair[0] != pair[1])); + assert!( + letter_downs + .iter() + .collect::>() + .len() + > 5 + ); + let steps: std::collections::HashSet = + downs.iter().map(|voice| voice.step.to_bits()).collect(); + assert!(steps.len() > 20); +} + +#[test] +fn volume_scales_and_type_switches_packs() { + let quiet = sounds(&key_project(0.3)).unwrap(); + let loud = sounds(&key_project(0.9)).unwrap(); + let peak = |sounds: &InputSounds| { + let mut output = vec![0.0; 48_000 * 9 * 2]; + sounds.mix(&mut output, 0); + output + .iter() + .fold(0.0f32, |peak, sample| peak.max(sample.abs())) + }; + let ratio = peak(&loud) / peak(&quiet); + assert!((ratio - 9.0).abs() < 0.5, "{ratio}"); + + let mut project = key_project(0.6); + project.keyboard.as_mut().unwrap().settings.key_sound_type = KeySoundType::Typewriter; + project.cursor.click_sound_type = ClickSoundType::Trackpad; + let switched = sounds(&project).unwrap(); + assert!( + switched + .voices + .iter() + .all(|voice| matches!(voice.pack, PackId::Typewriter | PackId::Trackpad)) + ); + assert_ne!(switched.fingerprint(), loud.fingerprint()); +} + +#[test] +fn previews_render_every_pack() { + for preview in [ + InputSoundPreview::Keys(KeySoundType::Mechanical), + InputSoundPreview::Keys(KeySoundType::Deep), + InputSoundPreview::Keys(KeySoundType::Laptop), + InputSoundPreview::Keys(KeySoundType::Typewriter), + InputSoundPreview::Clicks(ClickSoundType::Mouse), + InputSoundPreview::Clicks(ClickSoundType::Trackpad), + ] { + let audio = render_input_sound_preview(preview, 1.0).unwrap(); + assert!(audio.len() > 48_000, "{preview:?}"); + assert!(audio.iter().all(|sample| sample.abs() < 1.0), "{preview:?}"); + } + assert!(render_input_sound_preview(InputSoundPreview::Keys(KeySoundType::Deep), 0.0).is_none()); +} + +#[test] +fn cubic_interpolation_hits_the_samples_it_passes_through() { + let samples = [0.0, 0.5, -0.25, 1.0, 0.0]; + for (index, sample) in samples.iter().enumerate() { + assert_eq!(cubic(&samples, index as f32), *sample); + } + assert!(cubic(&samples, 10.0).abs() <= 1.0); +} + +#[test] +fn hiding_the_cursor_on_a_clip_changes_the_click_sounds() { + let mut before = key_project(0.6); + before.cursor.click_sounds = true; + let mut after = before.clone(); + assert!(!input_sounds_changed(&before, &after)); + after.timeline.as_mut().unwrap().segments[0].hide_cursor = Some(true); + assert!(input_sounds_changed(&before, &after)); +} diff --git a/crates/editor/src/lib.rs b/crates/editor/src/lib.rs index 68f7e5c1518..9f0ebf8397e 100644 --- a/crates/editor/src/lib.rs +++ b/crates/editor/src/lib.rs @@ -6,6 +6,7 @@ mod editor; mod editor_instance; mod export_audio; pub mod hover_analysis; +mod input_sounds; mod join_fade; mod playback; mod preparing_audio; @@ -49,6 +50,7 @@ pub use editor_instance::{ pub use export_audio::{ ExportAudioError, ExportAudioPreparation, ExportAudioRenderer, ExportAudioValidation, }; +pub use input_sounds::{InputSoundPreview, InputSounds, render_input_sound_preview}; pub use playback::{Playback, PlaybackEvent, PlaybackHandle, PlaybackStartError}; pub use preparing_editor::{PreparingEditorPhase, PreparingEditorProgress, PreparingPlaybackState}; pub use preparing_handoff::{ diff --git a/crates/editor/src/playback.rs b/crates/editor/src/playback.rs index b90393618b8..b0bd79ee261 100644 --- a/crates/editor/src/playback.rs +++ b/crates/editor/src/playback.rs @@ -23,7 +23,7 @@ use tokio::sync::{Notify, watch}; use tracing::{info, warn}; use crate::{ - PreviewSpeed, PreviewSpeedHandle, + InputSounds, PreviewSpeed, PreviewSpeedHandle, audio_output::{AudioOutput, PlaySpec}, editor, editor_instance::{SegmentMedia, cursor_timeline_settings}, @@ -1014,14 +1014,28 @@ impl Playback { diagnostic.stage("waiting_for_audio_decode"); let audio_wait_start = Instant::now(); let audio_segments = get_audio_segments(&self.segment_medias).await; + // The project the sounds are built from, to catch an edit made while + // the video warms up. + let sounds_project = Arc::new(self.project.borrow().clone()); + let input_sounds = { + let project = sounds_project.clone(); + let segment_medias = self.segment_medias.clone(); + tokio::task::spawn_blocking(move || { + InputSounds::for_segments(&project, &segment_medias) + }) + .await + .ok() + .flatten() + }; if let Some(telemetry) = &self.telemetry { telemetry.emit(PlaybackTelemetryEvent::AudioSegmentsResolved { elapsed: audio_wait_start.elapsed(), }); } + let has_extra_audio = !self.music.is_empty() || input_sounds.is_some(); // Restarts an idle-paused output stream while the video warms up, so // the play request after warmup finds it already running. - if adopted.is_none() && has_playback_audio(&audio_segments, !self.music.is_empty()) { + if adopted.is_none() && has_playback_audio(&audio_segments, has_extra_audio) { self.audio_output.prewarm(); } @@ -1190,6 +1204,11 @@ impl Playback { let playback_start_frame = frame_number; let mut cached_project = Arc::new(self.project.borrow().clone()); + // An edit to the sounds made during warm-up is already in + // `cached_project`, so the change check below would miss it: + // refresh them once on the first pass. + let mut sounds_stale = + crate::input_sounds::input_sounds_changed(&sounds_project, &cached_project); let mut speed = if adopted.is_some() { PreviewSpeed::NORMAL } else { @@ -1437,13 +1456,14 @@ impl Playback { let _ = audio_playhead_tx.send(playback_start_frame as f64 / fps_f64); let mut audio_generation = if adopted.is_some() { None - } else if !has_playback_audio(&audio_segments, !self.music.is_empty()) { + } else if !has_playback_audio(&audio_segments, has_extra_audio) { info!("No audio segments found, skipping audio playback."); None } else { self.audio_output.play(PlaySpec { segments: audio_segments.clone(), music: self.music.clone(), + input_sounds: input_sounds.clone(), project: self.project.borrow().clone(), duration_secs: duration, start_playhead_secs: playback_start_frame as f64 / fps_f64, @@ -1508,6 +1528,7 @@ impl Playback { let ticket = self.audio_output.prepare_playback(PlaySpec { segments: audio_segments.clone(), music: self.music.clone(), + input_sounds: InputSounds::for_segments(&project, &self.segment_medias), project, duration_secs: duration, start_playhead_secs: frame_number as f64 / fps_f64, @@ -1572,10 +1593,15 @@ impl Playback { continue; } - if self.project.has_changed().unwrap_or(false) { + if self.project.has_changed().unwrap_or(false) || sounds_stale { let next_project = self.project.borrow_and_update().clone(); - let audio_changed = cached_project.audio != next_project.audio - || clip_audio_changed(&cached_project, &next_project); + let audio_changed = std::mem::take(&mut sounds_stale) + || cached_project.audio != next_project.audio + || clip_audio_changed(&cached_project, &next_project) + || crate::input_sounds::input_sounds_changed( + &cached_project, + &next_project, + ); let cursor_changed = cursor_timeline_settings(&cached_project.cursor) != cursor_timeline_settings(&next_project.cursor); let zoom_changed = !ZoomTransformTimeline::project_inputs_match( @@ -1584,22 +1610,35 @@ impl Playback { self.render_constants.options.screen_size, ); cached_project = Arc::new(next_project); - if adopted.is_none() - && audio_changed - && let Some(generation) = audio_generation - { - self.audio_output.refresh_playback( - PlaySpec { - segments: audio_segments.clone(), - music: self.music.clone(), - project: (*cached_project).clone(), - duration_secs: duration, - start_playhead_secs: frame_number as f64 / fps_f64, - playhead_rx: audio_playhead_rx.clone(), - preview_speed: self.preview_speed.clone(), - }, - generation, - ); + if adopted.is_none() && audio_changed { + let input_sounds = + InputSounds::for_segments(&cached_project, &self.segment_medias); + let spec = PlaySpec { + segments: audio_segments.clone(), + music: self.music.clone(), + input_sounds, + project: (*cached_project).clone(), + duration_secs: duration, + start_playhead_secs: frame_number as f64 / fps_f64, + playhead_rx: audio_playhead_rx.clone(), + preview_speed: self.preview_speed.clone(), + }; + if let Some(generation) = audio_generation { + self.audio_output.refresh_playback(spec, generation); + } else if has_playback_audio( + &audio_segments, + !self.music.is_empty() || spec.input_sounds.is_some(), + ) { + // Playback that started silent gains a source the + // moment an edit gives it something to play, such + // as turning key or click sounds on. The source is + // installed without waiting on the device, so the + // video never stalls; it joins at the playhead. + let ticket = self.audio_output.prepare_playback(spec); + audio_generation = Some(ticket.generation()); + transitioned_audio = Some(ticket); + has_audio = true; + } } if cursor_changed { cursor_timelines = build_cursor_timelines(&cached_project); diff --git a/crates/export/src/audio.rs b/crates/export/src/audio.rs index a5a898bb377..21916fa0f2e 100644 --- a/crates/export/src/audio.rs +++ b/crates/export/src/audio.rs @@ -10,7 +10,8 @@ use std::{ }; use cap_editor::{ - AudioRenderer, ExportAudioPreparation, ExportAudioRenderer, audio_segment_from_decoded, + AudioRenderer, ExportAudioPreparation, ExportAudioRenderer, InputSounds, + audio_segment_from_decoded, }; use cap_enc_ffmpeg::mp3::Mp3Encoder; use cap_project::ProjectConfiguration; @@ -85,6 +86,8 @@ impl AudioExportFormat { } } +/// Whether the edited project makes any sound: a recorded audio track, or +/// key and click sounds that are turned on. pub fn has_audio( recordings: &cap_rendering::ProjectRecordingsMeta, project: &ProjectConfiguration, @@ -92,6 +95,8 @@ pub fn has_audio( crate::tracks::available_tracks(recordings, project) .iter() .any(|kind| !kind.is_video()) + || cap_project::input_sounds::key_sounds_enabled(project) + || cap_project::input_sounds::click_sounds_enabled(project) } /// Writes the project's edited sound to `output_path` in `format`. The file @@ -167,7 +172,8 @@ pub async fn export_audio( AudioSource::Streaming(Box::new( preparation .finish(&segments) - .map_err(|error| error.to_string())?, + .map_err(|error| error.to_string())? + .with_input_sounds(InputSounds::for_segments(&project, &segments)), )) } else { let segments = @@ -193,7 +199,9 @@ pub async fn export_audio( )); } let music = cap_editor::load_music_tracks_uncached(&project, &recording_meta.project_path); - let mut renderer = AudioRenderer::new(audio_segments).with_music(music); + let mut renderer = AudioRenderer::new(audio_segments) + .with_music(music) + .with_input_sounds(InputSounds::for_segments(&project, &segments)); renderer.set_playhead(0.0, &project); AudioSource::Renderer(Box::new(renderer)) }; diff --git a/crates/export/src/audio_project.rs b/crates/export/src/audio_project.rs index 1dc1f87d8c3..8f5d90182b9 100644 --- a/crates/export/src/audio_project.rs +++ b/crates/export/src/audio_project.rs @@ -122,6 +122,7 @@ pub fn create_audio_only_project( status: Some(StudioRecordingStatus::Complete), cursor_shape_version: cap_project::CURSOR_SHAPE_VERSION, audio_only: true, + blank: false, }, })), upload: None, @@ -135,6 +136,98 @@ pub fn create_audio_only_project( Ok(meta) } +/// How long a blank project can run: its placeholder is two black frames, +/// so the length costs nothing on disk. +pub const BLANK_PROJECT_LENGTH: Duration = Duration::from_secs(10 * 60); +/// How long a new blank project's clip starts out; it can be dragged out to +/// [`BLANK_PROJECT_LENGTH`]. +pub const BLANK_PROJECT_CLIP: Duration = Duration::from_secs(15); + +/// Creates `project_path` as an empty Studio project: no recording, just an +/// animated background and a clip to lay music, keys, text and images over. +/// Nothing is left behind when a step fails. +pub fn create_blank_project( + project_path: &Path, + pretty_name: &str, +) -> Result { + let _ = ffmpeg::init(); + if let Some(parent) = project_path.parent() { + std::fs::create_dir_all(parent).map_err(|error| io(parent, error))?; + } + match std::fs::create_dir(project_path) { + Ok(()) => {} + Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(AudioProjectError::Exists(project_path.to_path_buf())); + } + Err(error) => return Err(io(project_path, error)), + } + let mut cleanup = RemoveOnDrop(Some(project_path.to_path_buf())); + let segment_dir = project_path.join(SEGMENT_DIR); + std::fs::create_dir_all(&segment_dir).map_err(|error| io(&segment_dir, error))?; + write_placeholder(&segment_dir.join("display.mp4"), BLANK_PROJECT_LENGTH)?; + + let meta = RecordingMeta { + platform: Some(Platform::default()), + project_path: project_path.to_path_buf(), + pretty_name: pretty_name.to_string(), + sharing: None, + inner: RecordingMetaInner::Studio(Box::new(StudioRecordingMeta::MultipleSegments { + inner: MultipleSegments { + segments: vec![MultipleSegment { + display: VideoMeta { + path: format!("{SEGMENT_DIR}/display.mp4").into(), + fps: AUDIO_ONLY_CANVAS_FPS, + start_time: Some(0.0), + device_id: None, + }, + camera: None, + mic: None, + system_audio: None, + cursor: None, + keyboard: None, + display_notch: None, + cursor_shape_version: None, + }], + cursors: Cursors::default(), + status: Some(StudioRecordingStatus::Complete), + cursor_shape_version: cap_project::CURSOR_SHAPE_VERSION, + audio_only: false, + blank: true, + }, + })), + upload: None, + }; + blank_project_config() + .write(project_path) + .map_err(|error| io(project_path, error))?; + meta.save_for_project() + .map_err(|error| AudioProjectError::Io(format!("Could not save the project: {error:?}")))?; + cleanup.0 = None; + Ok(meta) +} + +/// A blank project starts on a moving background with one short clip, and +/// with keys ready to show as soon as one is added. +pub fn blank_project_config() -> cap_project::ProjectConfiguration { + let mut config = cap_project::ProjectConfiguration::for_new_project(); + config.background.source = cap_project::BackgroundSource::AnimatedGradient { + config: cap_project::AnimatedGradientConfig::default(), + }; + config.timeline = serde_json::from_value(serde_json::json!({ + "segments": [{ + "recordingSegment": 0, + "start": 0.0, + "end": BLANK_PROJECT_CLIP.as_secs_f64(), + "timescale": 1.0 + }], + "zoomSegments": [] + })) + .ok(); + let keyboard = config.keyboard.get_or_insert_with(Default::default); + keyboard.settings.enabled = true; + config +} + struct RemoveOnDrop(Option); impl Drop for RemoveOnDrop { @@ -195,6 +288,33 @@ fn write_placeholder(path: &Path, duration: Duration) -> Result<(), AudioProject mod tests { use super::*; + #[test] + fn a_blank_project_opens_on_a_moving_background_with_a_short_clip() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("Blank.cap"); + let meta = create_blank_project(&path, "Blank").unwrap(); + let studio = meta.studio_meta().unwrap(); + assert!(studio.is_blank() && studio.hides_display() && !studio.is_audio_only()); + let reread = RecordingMeta::load_for_project(&path).unwrap(); + assert!(reread.studio_meta().unwrap().is_blank()); + assert!(path.join(SEGMENT_DIR).join("display.mp4").exists()); + let config = cap_project::ProjectConfiguration::load(&path).unwrap(); + assert!(matches!( + config.background.source, + cap_project::BackgroundSource::AnimatedGradient { .. } + )); + let timeline = config.timeline.unwrap(); + assert_eq!(timeline.segments.len(), 1); + assert_eq!(timeline.segments[0].end, BLANK_PROJECT_CLIP.as_secs_f64()); + assert!(config.keyboard.unwrap().settings.enabled); + // A second project of the same name is refused, leaving the first. + assert!(matches!( + create_blank_project(&path, "Blank"), + Err(AudioProjectError::Exists(_)) + )); + assert!(path.join("recording-meta.json").exists()); + } + #[test] fn a_project_needs_audio() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/export/src/estimates.rs b/crates/export/src/estimates.rs index d576269424c..de7c5f8727b 100644 --- a/crates/export/src/estimates.rs +++ b/crates/export/src/estimates.rs @@ -305,6 +305,7 @@ async fn estimate( cursor: segment.cursor.clone(), hover_changes: segment.hover_changes.clone(), keyboard: segment.keyboard.clone(), + keyboard_offset_secs: segment.keyboard_offset_secs, decoders: sample.decoders.clone(), }) .collect(), diff --git a/crates/export/src/lib.rs b/crates/export/src/lib.rs index 717dedebc3b..1dc7658ba63 100644 --- a/crates/export/src/lib.rs +++ b/crates/export/src/lib.rs @@ -950,6 +950,7 @@ mod clip_offset_tests { status: None, cursor_shape_version: 0, audio_only: false, + blank: false, }, } } diff --git a/crates/export/src/mp4.rs b/crates/export/src/mp4.rs index 2106f1ba156..c6d42e32c1b 100644 --- a/crates/export/src/mp4.rs +++ b/crates/export/src/mp4.rs @@ -1,6 +1,6 @@ use crate::{ExporterBase, Mp4ExporterBase}; use cap_editor::{ - AudioRenderer, ExportAudioError, ExportAudioRenderer, get_audio_segments, + AudioRenderer, ExportAudioError, ExportAudioRenderer, InputSounds, get_audio_segments, load_music_tracks_uncached, }; use cap_enc_ffmpeg::{AudioEncoder, PacketQueue, aac::AACEncoder, h264::H264Encoder, mp4::*}; @@ -455,7 +455,11 @@ impl Mp4ExportSettings { let pipeline_start = std::time::Instant::now(); let sample_timing = base.sample_timing.clone(); let output_path = base.output_path.clone(); - let mut streaming_audio = base.streaming_audio.take(); + let input_sounds = InputSounds::for_segments(&base.project_config, &base.segments); + let mut streaming_audio = base + .streaming_audio + .take() + .map(|audio| audio.with_input_sounds(input_sounds.clone())); let audio_control = base.audio_cancellation.take(); let audio_cancellation = audio_control.as_ref().map(|control| control.stop.clone()); let user_cancellation = audio_control.as_ref().map(|control| control.user.clone()); @@ -500,7 +504,7 @@ impl Mp4ExportSettings { .first() .filter(|_| !base.project_config.audio.mute) .is_some(); - let has_audio = has_recording_audio || !music.is_empty(); + let has_audio = has_recording_audio || !music.is_empty() || input_sounds.is_some(); let record_first_queued_ms = mode.record_first_queued_ms_since_pipeline; let software_encoder = mode.software_encoder; @@ -613,7 +617,11 @@ impl Mp4ExportSettings { Some(audio_encoder) => { let source = match streaming_audio.take() { Some(audio) => ExportAudioSource::Streaming(audio), - None => ExportAudioSource::Renderer(AudioRenderer::new(audio_segments).with_music(music)), + None => ExportAudioSource::Renderer( + AudioRenderer::new(audio_segments) + .with_music(music) + .with_input_sounds(input_sounds), + ), }; let worker_cancellation = encoder_cancellation.clone(); let worker_user_cancellation = encoder_user_cancellation.clone(); diff --git a/crates/export/src/mp4/turbo.rs b/crates/export/src/mp4/turbo.rs index b77293a3255..46eba352007 100644 --- a/crates/export/src/mp4/turbo.rs +++ b/crates/export/src/mp4/turbo.rs @@ -10,7 +10,7 @@ use super::*; use crate::estimates::SampleTiming; -use cap_editor::SegmentMedia; +use cap_editor::{InputSounds, SegmentMedia}; use cap_enc_ffmpeg::{PacketSink, h264::SoftwareEncoderTuning}; use cap_project::{ProjectConfiguration, RecordingMeta, StudioRecordingMeta}; use cap_rendering::{ProjectRecordingsMeta, RenderVideoConstants}; @@ -851,7 +851,11 @@ pub(crate) async fn export bool + Send + 'static>( } let output_path = base.output_path.clone(); - let mut streaming_audio = base.streaming_audio.take(); + let input_sounds = InputSounds::for_segments(&base.project_config, &base.segments); + let mut streaming_audio = base + .streaming_audio + .take() + .map(|audio| audio.with_input_sounds(input_sounds.clone())); let audio_control = base.audio_cancellation.take(); let temporary_output = match base.streaming_output.take() { Some(staged) => Some(staged), @@ -893,12 +897,14 @@ pub(crate) async fn export bool + Send + 'static>( .first() .filter(|_| !base.project_config.audio.mute) .is_some(); - let audio = (has_recording_audio || !music.is_empty()).then(|| { + let audio = (has_recording_audio || !music.is_empty() || input_sounds.is_some()).then(|| { let source = match streaming_audio.take() { Some(audio) => ExportAudioSource::Streaming(audio), - None => { - ExportAudioSource::Renderer(AudioRenderer::new(audio_segments).with_music(music)) - } + None => ExportAudioSource::Renderer( + AudioRenderer::new(audio_segments) + .with_music(music) + .with_input_sounds(input_sounds), + ), }; (source, base.project_config.clone()) }); diff --git a/crates/project/src/animated_gradient.rs b/crates/project/src/animated_gradient.rs index 070bd94f358..60196b92278 100644 --- a/crates/project/src/animated_gradient.rs +++ b/crates/project/src/animated_gradient.rs @@ -10,9 +10,30 @@ pub struct AnimatedGradientStop { pub position: f32, } +/// How the gradient is drawn: as an ordered dither of its own palette, crisp +/// and printed-looking, or as a smooth wash. +#[derive(Type, Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq)] +#[serde(rename_all = "camelCase")] +pub enum AnimatedGradientStyle { + #[default] + Dither, + Smooth, +} + +impl AnimatedGradientStyle { + /// Backgrounds saved before the choice existed were smooth. + fn saved_before() -> Self { + Self::Smooth + } +} + #[derive(Type, Serialize, Deserialize, Clone, Debug, PartialEq)] #[serde(default, rename_all = "camelCase")] pub struct AnimatedGradientConfig { + #[serde(default = "AnimatedGradientStyle::saved_before")] + pub style: AnimatedGradientStyle, + /// The dither's cell, in pixels of a 1080p frame. + pub dither_size: f32, pub color_stops: Vec, pub direction: f32, pub flow_scale: f32, @@ -38,6 +59,10 @@ impl Default for AnimatedGradientConfig { } } +/// A dither cell a few pixels across at 1080p: fine enough to read as a +/// gradient, coarse enough to read as dither. +const DEFAULT_DITHER_SIZE: f32 = 3.0; + /// Aqua to periwinkle to lilac; also the default palette for a fresh animated background. const COOL_CANDY: [u32; 5] = [0x7ddcf9, 0x6f9bf5, 0x6b6fe8, 0x9a7ff5, 0xc0a6f7]; @@ -56,6 +81,8 @@ impl Mood { let variation = index as f32; match self { Self::Field => AnimatedGradientConfig { + style: AnimatedGradientStyle::Dither, + dither_size: DEFAULT_DITHER_SIZE, color_stops: stops(palette), direction: (30.0 + variation * 33.0) % 360.0, flow_scale: 0.8 + (index % 3) as f32 * 0.1, @@ -75,6 +102,8 @@ impl Mood { seed: index as u32 * 137, }, Self::Deep => AnimatedGradientConfig { + style: AnimatedGradientStyle::Dither, + dither_size: DEFAULT_DITHER_SIZE, color_stops: stops(palette), direction: (20.0 + variation * 37.0) % 360.0, flow_scale: 0.9 + (index % 3) as f32 * 0.1, @@ -188,6 +217,15 @@ parameters![ 3.0, 0.1 ), + ( + DitherSize, + dither_size, + "Dither Size", + "Texture", + 1.0, + 8.0, + 1.0 + ), (Exposure, exposure, "Exposure", "Colour", -50.0, 50.0, 1.0), (Contrast, contrast, "Contrast", "Colour", 50.0, 200.0, 1.0), (Vibrance, vibrance, "Vibrance", "Colour", 0.0, 200.0, 1.0), @@ -311,6 +349,8 @@ impl AnimatedGradientConfig { }) .collect(); Self { + style: AnimatedGradientStyle::Dither, + dither_size: DEFAULT_DITHER_SIZE, color_stops, direction: random.range(0.0, 360.0), flow_scale: random.range(0.6, 1.8), @@ -527,8 +567,21 @@ mod tests { #[test] fn defaults_and_library_round_trip() { + // A gradient saved before the style choice stays smooth; a new one + // starts as a dither. let config: AnimatedGradientConfig = serde_json::from_str("{}").unwrap(); - assert_eq!(config, AnimatedGradientConfig::default()); + assert_eq!(config.style, AnimatedGradientStyle::Smooth); + assert_eq!( + AnimatedGradientConfig::default().style, + AnimatedGradientStyle::Dither + ); + assert_eq!( + config, + AnimatedGradientConfig { + style: AnimatedGradientStyle::Smooth, + ..AnimatedGradientConfig::default() + } + ); assert_eq!(config, config.normalized()); assert_eq!( serde_json::from_str::("{}").unwrap(), @@ -588,7 +641,13 @@ mod tests { let crate::BackgroundSource::AnimatedGradient { config } = &source else { panic!("Expected animated gradient"); }; - assert_eq!(config, &AnimatedGradientConfig::default()); + assert_eq!( + config, + &AnimatedGradientConfig { + style: AnimatedGradientStyle::Smooth, + ..AnimatedGradientConfig::default() + } + ); let json = serde_json::to_value(&source).unwrap(); assert_eq!(json["type"], "animatedGradient"); let round_trip: crate::BackgroundSource = serde_json::from_value(json).unwrap(); diff --git a/crates/project/src/configuration.rs b/crates/project/src/configuration.rs index 2fee1bef1c5..39ecaa4753d 100644 --- a/crates/project/src/configuration.rs +++ b/crates/project/src/configuration.rs @@ -1174,8 +1174,31 @@ pub struct CursorConfiguration { /// keep it off. #[serde(default)] pub hover_anchors: bool, + pub click_sounds: bool, + pub click_sound_type: ClickSoundType, + pub click_sound_volume: f32, } +#[derive(Type, Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[serde(rename_all = "camelCase")] +pub enum ClickSoundType { + #[default] + Mouse, + Trackpad, +} + +#[derive(Type, Serialize, Deserialize, Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +#[serde(rename_all = "camelCase")] +pub enum KeySoundType { + #[default] + Mechanical, + Deep, + Laptop, + Typewriter, +} + +pub const DEFAULT_INPUT_SOUND_VOLUME: f32 = 0.6; + impl Default for CursorConfiguration { fn default() -> Self { let animation_style = CursorAnimationStyle::default(); @@ -1201,6 +1224,9 @@ impl Default for CursorConfiguration { ripple: CursorRippleConfig::default(), interaction_anchors: true, hover_anchors: true, + click_sounds: false, + click_sound_type: ClickSoundType::default(), + click_sound_volume: DEFAULT_INPUT_SOUND_VOLUME, }; if let Some(preset) = animation_style.preset() { @@ -1319,6 +1345,20 @@ impl TimelineSegment { .map(|voice_eq| voice_eq.sanitized()) } + /// Whether this clip's own audio is silent: a clip at normal speed only + /// when muted outright, a sped-up or slowed one unless its audio was + /// kept, and any clip whose speed is outside what audio can follow. + pub fn speed_audio_muted(&self) -> bool { + if !self.timescale.is_finite() || !(0.25..=8.0).contains(&self.timescale) { + return true; + } + if (self.timescale - 1.0).abs() < f64::EPSILON { + self.speed_audio_mode == Some(ClipSpeedAudioMode::Mute) + } else { + self.speed_audio_mode.unwrap_or_default() == ClipSpeedAudioMode::Mute + } + } + pub fn volume(&self) -> f64 { self.volume .filter(|volume| volume.is_finite()) @@ -2725,7 +2765,7 @@ fn active_hold_window(windows: &[(f64, f64)], time: f64) -> Option<(f64, f64)> { .copied() } -fn held_time_before(windows: &[(f64, f64)], time: f64) -> f64 { +pub(crate) fn held_time_before(windows: &[(f64, f64)], time: f64) -> f64 { windows .iter() .map(|(start, end)| (time.min(*end) - start).max(0.0)) @@ -2796,16 +2836,26 @@ pub enum CaptionPosition { #[serde(rename_all = "camelCase", default)] pub struct CaptionSettings { pub enabled: bool, + /// New captions start in the Soft style; a project saved without one of + /// these fields keeps the Classic value it was drawn with. + #[serde(default = "CaptionSettings::legacy_font")] pub font: String, + #[serde(default = "CaptionSettings::legacy_size")] pub size: u32, pub color: String, - #[serde(alias = "backgroundColor")] + #[serde( + alias = "backgroundColor", + default = "CaptionSettings::legacy_background_color" + )] pub background_color: String, - #[serde(alias = "backgroundOpacity")] + #[serde( + alias = "backgroundOpacity", + default = "CaptionSettings::legacy_background_opacity" + )] pub background_opacity: u32, pub position: String, pub italic: bool, - #[serde(alias = "fontWeight")] + #[serde(alias = "fontWeight", default = "CaptionSettings::default_font_weight")] pub font_weight: u32, pub outline: bool, #[serde(alias = "outlineColor")] @@ -2820,11 +2870,18 @@ pub struct CaptionSettings { pub linger_duration: f32, #[serde(alias = "wordTransitionDuration")] pub word_transition_duration: f32, - #[serde(alias = "activeWordHighlight")] + /// New captions highlight the spoken word; a project saved before this + /// setting existed keeps its captions unhighlighted. + #[serde( + alias = "activeWordHighlight", + default = "CaptionSettings::legacy_active_word_highlight" + )] pub active_word_highlight: bool, #[serde(alias = "manualPosition")] pub manual_position: Option>, + #[serde(default = "CaptionSettings::default_preset")] pub preset: String, + #[serde(default = "CaptionSettings::default_animation")] pub animation: String, #[serde(alias = "highlightStyle")] pub highlight_style: String, @@ -2865,10 +2922,27 @@ impl CaptionSettings { 0.25 } - fn default_active_word_highlight() -> bool { + fn legacy_active_word_highlight() -> bool { false } + fn legacy_font() -> String { + "System Sans-Serif".to_string() + } + + /// Captions saved without a size had the old default. + fn legacy_size() -> u32 { + 24 + } + + fn legacy_background_color() -> String { + "#000000".to_string() + } + + fn legacy_background_opacity() -> u32 { + 90 + } + fn default_preset() -> String { "classic".to_string() } @@ -2884,16 +2958,17 @@ impl CaptionSettings { impl Default for CaptionSettings { fn default() -> Self { + // The Soft preset: the keycaps' charcoal, Inter and a gentle pop. Self { enabled: false, - font: "System Sans-Serif".to_string(), - size: 24, + font: "Inter".to_string(), + size: 46, color: "#FFFFFF".to_string(), - background_color: "#000000".to_string(), - background_opacity: 90, + background_color: "#1C1C1F".to_string(), + background_opacity: 86, position: "bottom-center".to_string(), italic: false, - font_weight: Self::default_font_weight(), + font_weight: 600, outline: false, outline_color: "#000000".to_string(), export_with_subtitles: false, @@ -2901,10 +2976,10 @@ impl Default for CaptionSettings { fade_duration: Self::default_fade_duration(), linger_duration: Self::default_linger_duration(), word_transition_duration: Self::default_word_transition_duration(), - active_word_highlight: Self::default_active_word_highlight(), + active_word_highlight: true, manual_position: None, - preset: Self::default_preset(), - animation: Self::default_animation(), + preset: "soft".to_string(), + animation: "pop".to_string(), highlight_style: Self::default_highlight_style(), uppercase: false, show_filler_words: false, @@ -2937,6 +3012,9 @@ pub struct KeyboardSettings { pub color: String, pub background_color: String, pub background_opacity: u32, + /// New overlays sit at the top; one saved without a position keeps + /// the bottom it was drawn at. + #[serde(default = "KeyboardSettings::legacy_position")] pub position: String, pub font_weight: u32, pub fade_duration: f32, @@ -2945,25 +3023,137 @@ pub struct KeyboardSettings { pub show_modifiers: bool, pub show_special_keys: bool, pub uppercase: bool, + /// How key presses are drawn: `classic` (one rounded label), keycaps + /// shaped like a `macos`, `windows` or `linux` keyboard, or `auto`, which + /// a project saved before keycaps existed reads as: see + /// [`KeyboardSettings::effective_style`]. + #[serde(default = "KeyboardSettings::legacy_style")] + pub style: String, + /// The keycaps' colours: `light`, `dark` or `retro`. + #[serde(default = "KeyboardSettings::default_theme")] + pub theme: String, + /// Whether typed text shows, or only shortcuts. New projects show + /// shortcuts only; a project saved before the choice existed keeps its + /// typing. + #[serde(default = "KeyboardSettings::legacy_show_typing")] + pub show_typing: bool, + /// How the Keyboard tab's composer shows a sentence: `words`, each word + /// typed out on its own key, `keys`, every keystroke on its own, or + /// `sentence`, the whole of it on one key. + pub typing_split: String, + pub key_sounds: bool, + pub key_sound_type: KeySoundType, + pub key_sound_volume: f32, +} + +impl KeyboardSettings { + fn legacy_position() -> String { + "bottom-center".to_string() + } + + fn legacy_show_typing() -> bool { + true + } + + fn legacy_style() -> String { + "auto".to_string() + } + + /// The style to draw, on the platform this process runs on. See + /// [`KeyboardSettings::effective_style_for`], which every renderer uses. + pub fn effective_style(&self) -> &str { + self.effective_style_for(None, false) + } + + /// The style to draw. `auto` gives an older project the keys of the + /// platform it was recorded on (this machine's when that is unknown) when + /// its label was never styled, and keeps the classic label for one whose + /// colours or font were chosen, globally or on any key (`styled_keys`). + /// Read every frame, so it allocates nothing. + pub fn effective_style_for( + &self, + platform: Option<&crate::Platform>, + styled_keys: bool, + ) -> &str { + if self.style != "auto" { + return &self.style; + } + let untouched = self.color.eq_ignore_ascii_case(DEFAULT_KEYBOARD_COLOR) + && self + .background_color + .eq_ignore_ascii_case(DEFAULT_KEYBOARD_BACKGROUND) + && self.background_opacity == DEFAULT_KEYBOARD_BACKGROUND_OPACITY + && self.font == DEFAULT_KEYBOARD_FONT + && self.font_weight == DEFAULT_KEYBOARD_FONT_WEIGHT + && !styled_keys; + if untouched { + Self::platform_style_name_for(platform) + } else { + "classic" + } + } + + fn default_theme() -> String { + "dark".to_string() + } + + /// The keycap style a new project starts with: the keyboard of the + /// platform it was made on. + pub fn platform_style() -> String { + Self::platform_style_name().to_string() + } + + fn platform_style_name_for(platform: Option<&crate::Platform>) -> &'static str { + match platform { + Some(crate::Platform::MacOS) => "macos", + Some(crate::Platform::Windows) => "windows", + Some(crate::Platform::Linux) => "linux", + None => Self::platform_style_name(), + } + } + + fn platform_style_name() -> &'static str { + if cfg!(target_os = "macos") { + "macos" + } else if cfg!(target_os = "windows") { + "windows" + } else { + "linux" + } + } } +const DEFAULT_KEYBOARD_FONT: &str = "System Sans-Serif"; +const DEFAULT_KEYBOARD_COLOR: &str = "#FFFFFF"; +const DEFAULT_KEYBOARD_BACKGROUND: &str = "#000000"; +const DEFAULT_KEYBOARD_BACKGROUND_OPACITY: u32 = 95; +const DEFAULT_KEYBOARD_FONT_WEIGHT: u32 = 400; + impl Default for KeyboardSettings { fn default() -> Self { Self { enabled: false, - font: "System Sans-Serif".to_string(), + font: DEFAULT_KEYBOARD_FONT.to_string(), + // Keys a tenth of the frame tall. size: 50, - color: "#FFFFFF".to_string(), - background_color: "#000000".to_string(), - background_opacity: 95, - position: "bottom-center".to_string(), - font_weight: 400, + color: DEFAULT_KEYBOARD_COLOR.to_string(), + background_color: DEFAULT_KEYBOARD_BACKGROUND.to_string(), + background_opacity: DEFAULT_KEYBOARD_BACKGROUND_OPACITY, + position: "top-center".to_string(), + font_weight: DEFAULT_KEYBOARD_FONT_WEIGHT, fade_duration: 0.15, - linger_duration: 0.8, + linger_duration: 1.5, grouping_threshold_ms: 500.0, show_modifiers: true, - show_special_keys: true, + show_special_keys: false, uppercase: false, + style: Self::platform_style(), + theme: Self::default_theme(), + show_typing: false, + typing_split: "words".to_string(), + key_sounds: false, + key_sound_type: KeySoundType::default(), + key_sound_volume: DEFAULT_INPUT_SOUND_VOLUME, } } } @@ -5674,6 +5864,74 @@ mod tests { assert_eq!(loaded.background.display_position, Some(XY::new(0.5, 0.4))); } + #[test] + fn older_captions_keep_their_classic_look_and_new_ones_start_soft() { + let older: CaptionSettings = serde_json::from_value(serde_json::json!({ + "enabled": true, + "color": "#FFFFFF", + "position": "bottom-center" + })) + .unwrap(); + assert_eq!(older.font, "System Sans-Serif"); + assert_eq!(older.size, 24); + assert_eq!(older.background_color, "#000000"); + assert_eq!(older.background_opacity, 90); + assert_eq!(older.font_weight, 700); + assert_eq!(older.preset, "classic"); + assert_eq!(older.animation, "bounce"); + assert!(!older.active_word_highlight); + + let new = CaptionSettings::default(); + assert_eq!(new.preset, "soft"); + assert_eq!(new.font, "Inter"); + let saved: CaptionSettings = + serde_json::from_value(serde_json::to_value(&new).unwrap()).unwrap(); + assert_eq!(saved.preset, "soft"); + assert_eq!(saved.font, "Inter"); + assert_eq!(saved.size, new.size); + } + + #[test] + fn an_older_keyboard_overlay_gets_keys_unless_its_label_was_styled() { + let old: KeyboardSettings = serde_json::from_value(serde_json::json!({ + "enabled": true, "font": "System Sans-Serif", "size": 50, + "color": "#ffffff", "backgroundColor": "#000000", "backgroundOpacity": 95, + "position": "bottom-center", "fontWeight": 400, "fadeDuration": 0.15, + "lingerDuration": 0.8, "groupingThresholdMs": 500.0, "showModifiers": true, + "showSpecialKeys": true, "uppercase": false + })) + .unwrap(); + assert_eq!(old.style, "auto"); + assert!(old.show_typing); + assert_eq!(old.effective_style(), KeyboardSettings::platform_style()); + let styled = KeyboardSettings { + background_color: "#2255FF".into(), + ..old.clone() + }; + assert_eq!(styled.effective_style(), "classic"); + let bold = KeyboardSettings { + font_weight: 700, + ..old.clone() + }; + assert_eq!(bold.effective_style(), "classic"); + let chosen = KeyboardSettings { + style: "windows".into(), + ..styled + }; + assert_eq!(chosen.effective_style(), "windows"); + // The renderer resolves `auto` for the platform the recording was + // made on, wherever it runs, and keeps the label when any key was + // coloured on its own. + for (platform, style) in [ + (crate::Platform::MacOS, "macos"), + (crate::Platform::Windows, "windows"), + (crate::Platform::Linux, "linux"), + ] { + assert_eq!(old.effective_style_for(Some(&platform), false), style); + assert_eq!(old.effective_style_for(Some(&platform), true), "classic"); + } + } + #[test] fn manual_caption_position_loads_from_old_projects_and_round_trips() { let dir = tempfile::tempdir().unwrap(); diff --git a/crates/project/src/input_sounds.rs b/crates/project/src/input_sounds.rs new file mode 100644 index 00000000000..d2bba2d680d --- /dev/null +++ b/crates/project/src/input_sounds.rs @@ -0,0 +1,431 @@ +use crate::{ + CursorClickEvent, KeyPressEvent, KeyboardEvents, ProjectConfiguration, + keyboard::{clip_windows, held_output_time, is_modifier_key}, +}; + +/// One recording clip's input, in its own time base: cursor clicks are in +/// segment time, key presses are offset from it by `keyboard_offset_secs`. +#[derive(Clone, Copy)] +pub struct InputSoundTake<'a> { + pub keyboard: &'a KeyboardEvents, + pub keyboard_offset_secs: f64, + pub clicks: &'a [CursorClickEvent], +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum KeySoundClass { + Letter, + Space, + Enter, + Backspace, + Modifier, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum InputSoundKind { + Key { class: KeySoundClass, down: bool }, + Click { down: bool }, +} + +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct InputSoundEvent { + /// Output (timeline) seconds. + pub time: f64, + pub kind: InputSoundKind, + /// Stable per press, so the same edit always picks the same variation. + pub seed: u64, + /// -1 is hard left, 1 hard right. + pub pan: f32, + /// The clip's own volume, so a quieter clip is quieter throughout. + pub gain: f32, +} + +impl KeySoundClass { + pub fn of(key: &str) -> Self { + match key { + "Space" => Self::Space, + "Enter" | "Return" => Self::Enter, + "Backspace" | "Delete" => Self::Backspace, + _ if is_modifier_key(key) || matches!(key, "CapsLock" | "Tab" | "Escape") => { + Self::Modifier + } + _ => Self::Letter, + } + } +} + +const KEY_ROWS: &[(f32, &[&str])] = &[ + ( + 0.0, + &[ + "Escape", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", + ], + ), + ( + 0.0, + &[ + "Grave", + "Key1", + "Key2", + "Key3", + "Key4", + "Key5", + "Key6", + "Key7", + "Key8", + "Key9", + "Key0", + "Minus", + "Equal", + "Backspace", + ], + ), + ( + 0.0, + &[ + "Tab", + "Q", + "W", + "E", + "R", + "T", + "Y", + "U", + "I", + "O", + "P", + "LeftBracket", + "RightBracket", + "BackSlash", + ], + ), + ( + 0.25, + &[ + "CapsLock", + "A", + "S", + "D", + "F", + "G", + "H", + "J", + "K", + "L", + "Semicolon", + "Apostrophe", + "Enter", + ], + ), + ( + 0.75, + &[ + "LShift", "Z", "X", "C", "V", "B", "N", "M", "Comma", "Dot", "Slash", "RShift", + ], + ), +]; + +/// Where a key sits across a full-size keyboard, so a mono recording can be +/// placed the way the press would reach a microphone in front of it. +fn key_pan(key_code: &str) -> f32 { + const WIDTH: f32 = 14.0; + let column = match key_code { + "Space" => Some(6.5), + "LControl" => Some(0.5), + "LAlt" | "LOption" => Some(1.75), + "Meta" | "LMeta" | "Command" => Some(3.0), + "RMeta" | "RCommand" => Some(10.0), + "RAlt" | "ROption" => Some(11.25), + "RControl" | "Left" => Some(12.5), + "Up" | "Down" => Some(13.25), + "Right" | "Delete" | "Home" | "End" | "PageUp" | "PageDown" | "Insert" => Some(14.0), + _ => KEY_ROWS.iter().find_map(|(offset, row)| { + row.iter() + .position(|candidate| *candidate == key_code) + .map(|column| offset + column as f32 + 0.5) + }), + }; + column.map_or(0.0, |column| { + ((column / WIDTH - 0.5) * 0.7).clamp(-0.35, 0.35) + }) +} + +fn mix_seed(mut value: u64) -> u64 { + value ^= value >> 33; + value = value.wrapping_mul(0xff51_afd7_ed55_8ccd); + value ^= value >> 33; + value = value.wrapping_mul(0xc4ce_b9fe_1a85_ec53); + value ^ (value >> 33) +} + +fn text_seed(text: &str) -> u64 { + text.bytes().fold(0xcbf2_9ce4_8422_2325, |hash, byte| { + (hash ^ u64::from(byte)).wrapping_mul(0x0100_0000_01b3) + }) +} + +fn event_seed(kind: u64, recording_clip: u32, source_time_ms: f64, extra: u64) -> u64 { + mix_seed( + kind ^ (u64::from(recording_clip) << 48) + ^ source_time_ms.to_bits().rotate_left(17) + ^ extra.rotate_left(29), + ) +} + +/// Merged, sorted output ranges of the keyboard track. Key sounds follow the +/// overlay: a press sounds only while its keys are on screen. +fn shown_key_ranges(project: &ProjectConfiguration) -> Vec<(f64, f64)> { + let Some(timeline) = &project.timeline else { + return Vec::new(); + }; + // Keys added by hand sound their own text; a recorded press under one + // is not on screen. + let mut ranges = timeline + .keyboard_segments + .iter() + .filter(|segment| !crate::keyboard::is_manual_keyboard_segment(segment)) + .filter(|segment| { + segment.start.is_finite() && segment.end.is_finite() && segment.end > segment.start + }) + .map(|segment| (segment.start, segment.end)) + .collect::>(); + ranges.sort_by(|a, b| a.0.total_cmp(&b.0)); + let mut merged: Vec<(f64, f64)> = Vec::with_capacity(ranges.len()); + for (start, end) in ranges { + match merged.last_mut() { + Some(last) if start <= last.1 => last.1 = last.1.max(end), + _ => merged.push((start, end)), + } + } + merged +} + +fn within(ranges: &[(f64, f64)], time: f64) -> bool { + let index = ranges.partition_point(|range| range.1 < time); + ranges + .get(index) + .is_some_and(|range| range.0 <= time && time <= range.1) +} + +pub fn key_sounds_enabled(project: &ProjectConfiguration) -> bool { + project + .keyboard + .as_ref() + .is_some_and(|keyboard| keyboard.settings.enabled && keyboard.settings.key_sounds) +} + +/// Click sounds are on, and the cursor shows somewhere: globally, or in a +/// style segment that shows it over a hidden global cursor. +pub fn click_sounds_enabled(project: &ProjectConfiguration) -> bool { + project.cursor.click_sounds + && (!project.cursor.hide + || project.timeline.as_ref().is_some_and(|timeline| { + timeline.style_segments.iter().any(|segment| { + segment.enabled + && segment + .overrides + .cursor + .as_ref() + .is_some_and(|cursor| !cursor.hide) + }) + })) +} + +/// How long a key added by hand is held down before it springs back. +const MANUAL_KEY_HOLD_SECS: f64 = 0.085; + +/// Every key and click sound the edited timeline plays, sorted by output +/// time. Recording time maps to output time exactly as the keyboard track +/// does, so sounds stay on the frames that show them through cuts, speed +/// changes, transitions and fullscreen-text holds. +pub fn input_sound_events( + project: &ProjectConfiguration, + takes: &[InputSoundTake<'_>], +) -> Vec { + let keys = key_sounds_enabled(project); + let clicks = click_sounds_enabled(project); + let Some(timeline) = project.timeline.as_ref().filter(|_| keys || clicks) else { + return Vec::new(); + }; + let shown = if keys { + shown_key_ranges(project) + } else { + Vec::new() + }; + let holds = timeline.hold_windows(); + let mut events = Vec::new(); + let mut ordered_keys: Vec>> = vec![None; takes.len()]; + let mut ordered_clicks: Vec>> = vec![None; takes.len()]; + let mut held: Vec<&str> = Vec::new(); + // Where the last clip's keys left off, so a key held across a split is + // still released on the far side of it. + let mut held_until: Option<(u32, f64, f64)> = None; + + for window in clip_windows(timeline) { + let clip = window.clip; + // A clip whose own audio is silent, muted or sped up without kept + // audio, is silent here too. + if clip.speed_audio_muted() { + continue; + } + let Some(take) = takes.get(clip.recording_clip as usize) else { + continue; + }; + let gain = clip.volume() as f32; + let to_output = |source_secs: f64| { + held_output_time( + &holds, + window.edited_start + (source_secs - clip.start) / clip.timescale, + false, + ) + }; + + if keys && !shown.is_empty() && take.keyboard_offset_secs.is_finite() { + let presses = ordered_keys[clip.recording_clip as usize].get_or_insert_with(|| { + let mut presses = take + .keyboard + .presses + .iter() + .filter(|press| press.time_ms.is_finite()) + .collect::>(); + presses.sort_by(|a, b| a.time_ms.total_cmp(&b.time_ms)); + presses + }); + let source_start = clip.start + take.keyboard_offset_secs; + let source_end = clip.end + take.keyboard_offset_secs; + let first = presses.partition_point(|press| press.time_ms / 1000.0 < source_start); + let last = presses.partition_point(|press| press.time_ms / 1000.0 < source_end); + // Only a clip that carries straight on from the last one, in the + // recording and on the timeline, keeps its held keys. After a cut + // a release whose press was cut away stays silent, and across a + // transition the clips overlap, so a release could land before + // its press. + let continues = held_until.is_some_and(|(recording_clip, end, edited_end)| { + recording_clip == clip.recording_clip + && (end - clip.start).abs() < 1e-6 + && (edited_end - window.edited_start).abs() < 1e-6 + }); + if !continues { + held.clear(); + } + held_until = Some((clip.recording_clip, clip.end, window.edited_end)); + for &press in &presses[first..last.max(first)] { + let time = to_output(press.time_ms / 1000.0 - take.keyboard_offset_secs); + if press.down { + if held.contains(&press.key_code.as_str()) || !within(&shown, time) { + continue; + } + held.push(press.key_code.as_str()); + } else if let Some(index) = held.iter().position(|key| *key == press.key_code) { + held.swap_remove(index); + } else { + continue; + } + events.push(InputSoundEvent { + time, + kind: InputSoundKind::Key { + class: KeySoundClass::of(&press.key), + down: press.down, + }, + seed: event_seed( + 1, + clip.recording_clip, + press.time_ms, + text_seed(&press.key_code), + ), + pan: key_pan(&press.key_code), + gain, + }); + } + } + + if clicks { + let presses = ordered_clicks[clip.recording_clip as usize].get_or_insert_with(|| { + let mut presses = take + .clicks + .iter() + .filter(|click| click.time_ms.is_finite()) + .collect::>(); + presses.sort_by(|a, b| a.time_ms.total_cmp(&b.time_ms)); + presses + }); + let first = presses.partition_point(|click| click.time_ms / 1000.0 < clip.start); + let last = presses.partition_point(|click| click.time_ms / 1000.0 < clip.end); + for &click in &presses[first..last.max(first)] { + // A clip that hides the cursor hides its clicks too, fading + // with the cursor at the ends of the hidden run. + let time = to_output(click.time_ms / 1000.0); + // So does a style segment that hides it, and one that shows + // it over a hidden global cursor brings them back. + let visible = timeline.cursor_visibility_at(time) as f32; + if visible <= 0.0 || project.cursor_at(time).hide { + continue; + } + let seed = event_seed( + 2, + clip.recording_clip, + click.time_ms, + u64::from(click.cursor_num), + ); + events.push(InputSoundEvent { + time, + kind: InputSoundKind::Click { down: click.down }, + seed, + pan: ((seed >> 40) as f32 / (1u64 << 24) as f32 - 0.5) * 0.1, + gain: gain * visible, + }); + } + } + } + + // Keys added by hand have no recording behind them: they sound as their + // text strikes them, each key down and back up, over whatever clip lies + // beneath (silent where that clip is). + if keys { + let windows = clip_windows(timeline); + for (index, segment) in timeline + .keyboard_segments + .iter() + .enumerate() + .filter(|(_, segment)| crate::keyboard::is_manual_keyboard_segment(segment)) + { + for key in &segment.keys { + let down = segment.start + key.time_offset / 1000.0; + if !down.is_finite() || down >= segment.end { + continue; + } + let edited = down - crate::configuration::held_time_before(&holds, down); + let Some(window) = windows + .iter() + .find(|window| window.edited_start <= edited && edited < window.edited_end) + else { + continue; + }; + if window.clip.speed_audio_muted() { + continue; + } + let up = (down + MANUAL_KEY_HOLD_SECS).min(segment.end); + let seed = event_seed(3, index as u32, key.time_offset, text_seed(&key.key)); + for (time, down) in [(down, true), (up, false)] { + events.push(InputSoundEvent { + time, + kind: InputSoundKind::Key { + class: KeySoundClass::of(&key.key), + down, + }, + seed: seed ^ u64::from(down), + pan: key_pan(&key.key), + gain: window.clip.volume() as f32, + }); + } + } + } + } + + events.retain(|event| event.time.is_finite() && event.time >= 0.0); + events.sort_by(|a, b| a.time.total_cmp(&b.time).then(a.seed.cmp(&b.seed))); + events +} + +#[cfg(test)] +mod tests; diff --git a/crates/project/src/input_sounds/tests.rs b/crates/project/src/input_sounds/tests.rs new file mode 100644 index 00000000000..8d53c81a532 --- /dev/null +++ b/crates/project/src/input_sounds/tests.rs @@ -0,0 +1,632 @@ +use super::*; +use crate::{KeyboardData, KeyboardSettings, KeyboardTrackSegment}; + +fn press(key: &str, time_ms: f64, down: bool) -> KeyPressEvent { + KeyPressEvent { + key: key.to_string(), + key_code: key.to_string(), + time_ms, + down, + } +} + +fn click(time_ms: f64, down: bool) -> CursorClickEvent { + CursorClickEvent { + active_modifiers: Vec::new(), + cursor_num: 0, + cursor_id: "0".into(), + time_ms, + down, + } +} + +fn shown(start: f64, end: f64) -> KeyboardTrackSegment { + serde_json::from_value(serde_json::json!({ + "id": format!("kb-{start}"), "start": start, "end": end, "displayText": "x" + })) + .unwrap() +} + +fn project(timeline: serde_json::Value, keys: bool, clicks: bool) -> ProjectConfiguration { + let mut project: ProjectConfiguration = + serde_json::from_value(serde_json::json!({ "timeline": timeline })).unwrap(); + project.keyboard = Some(KeyboardData { + settings: KeyboardSettings { + enabled: true, + key_sounds: keys, + ..Default::default() + }, + }); + project.cursor.click_sounds = clicks; + project +} + +fn cut_and_sped_timeline() -> serde_json::Value { + serde_json::json!({ + "segments": [ + { "start": 0.0, "end": 2.0, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 4.0, "end": 6.0, "timescale": 2.0, "recordingSegment": 0, + "speedAudioMode": "maintainPitch" }, + { "start": 1.0, "end": 2.0, "timescale": 0.5, "recordingSegment": 1, + "speedAudioMode": "maintainPitch" } + ], + "zoomSegments": [] + }) +} + +fn key_times(events: &[InputSoundEvent], want_down: bool) -> Vec { + events + .iter() + .filter_map(|event| match event.kind { + InputSoundKind::Key { down, .. } if down == want_down => Some(event.time), + _ => None, + }) + .collect() +} + +#[test] +fn off_by_default_and_for_old_projects() { + let project: ProjectConfiguration = serde_json::from_value(serde_json::json!({ + "timeline": cut_and_sped_timeline(), + "keyboard": { "settings": { "enabled": true, "showModifiers": false } }, + "cursor": { "size": 120 } + })) + .unwrap(); + let settings = &project.keyboard.as_ref().unwrap().settings; + assert!(!settings.key_sounds); + assert_eq!(settings.key_sound_type, crate::KeySoundType::Mechanical); + assert_eq!(settings.key_sound_volume, crate::DEFAULT_INPUT_SOUND_VOLUME); + assert!(!settings.show_modifiers); + assert!(!project.cursor.click_sounds); + assert_eq!( + project.cursor.click_sound_type, + crate::ClickSoundType::Mouse + ); + assert_eq!(project.cursor.size, 120); + + let keyboard = KeyboardEvents { + presses: vec![press("a", 500.0, true), press("a", 560.0, false)], + }; + let clicks = [click(500.0, true), click(580.0, false)]; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + assert!(input_sound_events(&project, &takes).is_empty()); + assert!(input_sound_events(&ProjectConfiguration::default(), &takes).is_empty()); +} + +#[test] +fn settings_round_trip_through_json() { + let mut project = ProjectConfiguration { + keyboard: Some(KeyboardData { + settings: KeyboardSettings { + key_sounds: true, + key_sound_type: crate::KeySoundType::Typewriter, + key_sound_volume: 0.25, + ..Default::default() + }, + }), + ..Default::default() + }; + project.cursor.click_sounds = true; + project.cursor.click_sound_type = crate::ClickSoundType::Trackpad; + project.cursor.click_sound_volume = 0.8; + let json = serde_json::to_value(&project).unwrap(); + assert_eq!(json["keyboard"]["settings"]["keySoundType"], "typewriter"); + assert_eq!(json["cursor"]["clickSoundType"], "trackpad"); + let back: ProjectConfiguration = serde_json::from_value(json).unwrap(); + let settings = back.keyboard.unwrap().settings; + assert!(settings.key_sounds); + assert_eq!(settings.key_sound_type, crate::KeySoundType::Typewriter); + assert_eq!(settings.key_sound_volume, 0.25); + assert!(back.cursor.click_sounds); + assert_eq!( + back.cursor.click_sound_type, + crate::ClickSoundType::Trackpad + ); + assert_eq!(back.cursor.click_sound_volume, 0.8); +} + +#[test] +fn presses_follow_cuts_speed_and_clip_order() { + let mut project = project(cut_and_sped_timeline(), true, true); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.0, 10.0)]; + let offset = 0.25; + let first = KeyboardEvents { + presses: [1.0, 3.0, 5.0] + .into_iter() + .flat_map(|secs| { + let ms = (secs + offset) * 1000.0; + [press("a", ms, true), press("a", ms + 40.0, false)] + }) + .collect(), + }; + let second = KeyboardEvents { + presses: vec![press("b", 1_500.0, true), press("b", 1_540.0, false)], + }; + let first_clicks = [ + click(1_000.0, true), + click(3_000.0, true), + click(5_000.0, true), + ]; + let takes = [ + InputSoundTake { + keyboard: &first, + keyboard_offset_secs: offset, + clicks: &first_clicks, + }, + InputSoundTake { + keyboard: &second, + keyboard_offset_secs: 0.0, + clicks: &[], + }, + ]; + let events = input_sound_events(&project, &takes); + let downs = key_times(&events, true); + let ups = key_times(&events, false); + let expected_downs = [1.0, 2.0 + 1.0 / 2.0, 3.0 + 0.5 / 0.5]; + assert_eq!(downs.len(), expected_downs.len()); + for (got, want) in downs.iter().zip(expected_downs) { + assert!((got - want).abs() < 1e-9, "{got} vs {want}"); + } + let expected_ups = [1.04, 2.5 + 0.02, 4.0 + 0.08]; + for (got, want) in ups.iter().zip(expected_ups) { + assert!((got - want).abs() < 1e-9, "{got} vs {want}"); + } + let clicks: Vec = events + .iter() + .filter(|event| matches!(event.kind, InputSoundKind::Click { .. })) + .map(|event| event.time) + .collect(); + assert_eq!(clicks, vec![1.0, 2.5]); + assert!(events.windows(2).all(|pair| pair[0].time <= pair[1].time)); + + for event in &events { + let (source, segment) = project.get_segment_time(event.time).unwrap(); + let recorded = match (event.kind, segment.recording_clip) { + (InputSoundKind::Key { .. }, 0) => source + offset, + _ => source, + }; + let matches_input = takes[segment.recording_clip as usize] + .keyboard + .presses + .iter() + .map(|press| press.time_ms / 1000.0) + .chain( + takes[segment.recording_clip as usize] + .clicks + .iter() + .map(|click| click.time_ms / 1000.0), + ) + .any(|time| (time - recorded).abs() < 1e-6); + assert!(matches_input, "{event:?} does not land on its source frame"); + } +} + +#[test] +fn key_sounds_play_only_where_the_keyboard_track_shows_keys() { + let mut project = project(cut_and_sped_timeline(), true, false); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.9, 1.2)]; + let keyboard = KeyboardEvents { + presses: vec![ + press("LShift", 800.0, true), + press("a", 1_000.0, true), + press("LShift", 1_050.0, false), + press("a", 1_080.0, false), + press("b", 1_500.0, true), + press("b", 1_560.0, false), + ], + }; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let events = input_sound_events(&project, &takes); + assert_eq!( + events.iter().map(|event| event.kind).collect::>(), + vec![ + InputSoundKind::Key { + class: KeySoundClass::Letter, + down: true + }, + InputSoundKind::Key { + class: KeySoundClass::Letter, + down: false + }, + ] + ); + + project.keyboard.as_mut().unwrap().settings.enabled = false; + assert!(input_sound_events(&project, &takes).is_empty()); +} + +#[test] +fn hidden_cursor_mutes_clicks() { + let mut project = project(cut_and_sped_timeline(), false, true); + let clicks = [click(500.0, true), click(560.0, false)]; + let keyboard = KeyboardEvents::default(); + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + assert_eq!(input_sound_events(&project, &takes).len(), 2); + project.cursor.hide = true; + assert!(input_sound_events(&project, &takes).is_empty()); +} + +#[test] +fn transitions_and_holds_match_the_keyboard_track() { + let mut project = project( + serde_json::json!({ + "segments": [ + { "start": 0.0, "end": 3.0, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 5.0, "end": 9.0, "timescale": 1.5, "recordingSegment": 0, + "speedAudioMode": "maintainPitch" } + ], + "transitions": [{ "segmentIndex": 1, "type": "cross-fade", "duration": 0.5 }], + "zoomSegments": [], + "textSegments": [{ "start": 1.0, "end": 2.25, "layout": "fullscreen" }] + }), + true, + false, + ); + let presses = (0..90) + .flat_map(|index| { + let ms = index as f64 * 100.0 + 37.0; + [press("k", ms, true), press("k", ms + 30.0, false)] + }) + .collect(); + let keyboard = KeyboardEvents { presses }; + // Plain letters are typing, which new projects hide; show it. + let settings = KeyboardSettings { + show_modifiers: true, + show_typing: true, + ..Default::default() + }; + let timeline = project.timeline.clone().unwrap(); + let track = + crate::keyboard::project_keyboard_events(&[(keyboard.clone(), 0.0)], &timeline, &settings); + assert!(!track.is_empty()); + let mut shown_times: Vec = track + .iter() + .flat_map(|segment| { + segment + .keys + .iter() + .map(move |key| segment.start + key.time_offset / 1000.0) + }) + .collect(); + shown_times.sort_by(f64::total_cmp); + project.timeline.as_mut().unwrap().keyboard_segments = track; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let downs = key_times(&input_sound_events(&project, &takes), true); + assert_eq!(downs.len(), shown_times.len()); + for (sound, shown) in downs.iter().zip(&shown_times) { + assert!((sound - shown).abs() < 1e-6, "{sound} vs {shown}"); + } +} + +#[test] +fn events_are_deterministic_and_vary_per_press() { + let mut project = project(cut_and_sped_timeline(), true, true); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.0, 10.0)]; + let keyboard = KeyboardEvents { + presses: (0..20) + .flat_map(|index| { + let ms = 100.0 + index as f64 * 80.0; + [press("a", ms, true), press("a", ms + 30.0, false)] + }) + .collect(), + }; + let clicks = [click(100.0, true), click(150.0, false)]; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + let first = input_sound_events(&project, &takes); + let second = input_sound_events(&project, &takes); + assert_eq!(first, second); + let mut seeds: Vec = first.iter().map(|event| event.seed).collect(); + seeds.sort_unstable(); + seeds.dedup(); + assert_eq!(seeds.len(), first.len()); +} + +#[test] +fn keys_are_classed_and_placed_across_the_keyboard() { + assert_eq!(KeySoundClass::of("Space"), KeySoundClass::Space); + assert_eq!(KeySoundClass::of("Enter"), KeySoundClass::Enter); + assert_eq!(KeySoundClass::of("Backspace"), KeySoundClass::Backspace); + assert_eq!(KeySoundClass::of("LShift"), KeySoundClass::Modifier); + assert_eq!(KeySoundClass::of("Meta"), KeySoundClass::Modifier); + assert_eq!(KeySoundClass::of("a"), KeySoundClass::Letter); + assert!(key_pan("Q") < key_pan("P")); + assert!(key_pan("LShift") < 0.0 && key_pan("Enter") > 0.0); + assert!(key_pan("Space").abs() < 0.05); + assert_eq!(key_pan("SomethingElse"), 0.0); + for code in ["Grave", "Q", "A", "Z", "Backspace", "RShift", "Right"] { + assert!(key_pan(code).abs() <= 0.35); + } +} + +#[test] +fn muted_clips_stay_silent_and_clip_volume_carries_over() { + let mut project = project(cut_and_sped_timeline(), true, true); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.0, 10.0)]; + let keyboard = KeyboardEvents { + presses: vec![press("a", 1_000.0, true), press("a", 5_000.0, true)], + }; + let clicks = [click(1_000.0, true), click(5_000.0, true)]; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + { + let timeline = project.timeline.as_mut().unwrap(); + timeline.segments[0].speed_audio_mode = Some(crate::ClipSpeedAudioMode::Mute); + timeline.segments[1].volume = Some(0.5); + } + let events = input_sound_events(&project, &takes); + assert_eq!(events.len(), 2); + assert!(events.iter().all(|event| (event.time - 2.5).abs() < 1e-9)); + assert!(events.iter().all(|event| event.gain == 0.5)); + + // A sped-up clip is silent unless its audio was kept, as its own sound + // is, and so is a clip faster than audio can follow. + project.timeline.as_mut().unwrap().segments[1].speed_audio_mode = None; + assert!(input_sound_events(&project, &takes).is_empty()); + { + let timeline = project.timeline.as_mut().unwrap(); + timeline.segments[1].speed_audio_mode = Some(crate::ClipSpeedAudioMode::MatchSpeed); + timeline.segments[1].timescale = 16.0; + } + assert!(input_sound_events(&project, &takes).is_empty()); +} + +#[test] +fn a_split_keeps_a_held_key_and_a_cut_drops_its_release() { + let keyboard = KeyboardEvents { + presses: vec![press("a", 1000.0, true), press("a", 1080.0, false)], + }; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let events_for = |segments: serde_json::Value| { + let mut project = project( + serde_json::json!({ "segments": segments, "zoomSegments": [] }), + true, + false, + ); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.0, 3.0)]; + input_sound_events(&project, &takes) + }; + let whole = events_for(serde_json::json!([ + { "start": 0.0, "end": 3.0, "timescale": 1.0, "recordingSegment": 0 } + ])); + let split = events_for(serde_json::json!([ + { "start": 0.0, "end": 1.04, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 1.04, "end": 3.0, "timescale": 1.0, "recordingSegment": 0 } + ])); + assert_eq!(key_times(&whole, true).len(), 1); + assert_eq!(key_times(&whole, false).len(), 1); + // Splitting without removing anything sounds exactly the same. + assert_eq!(split.len(), whole.len()); + for (a, b) in split.iter().zip(&whole) { + assert!((a.time - b.time).abs() < 1e-9); + assert_eq!(a.kind, b.kind); + } + // Cutting out the moment between press and release drops the release. + let cut = events_for(serde_json::json!([ + { "start": 0.0, "end": 1.04, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 1.06, "end": 3.0, "timescale": 1.0, "recordingSegment": 0 } + ])); + assert_eq!(key_times(&cut, true).len(), 1); + assert!(key_times(&cut, false).is_empty()); +} + +#[test] +fn a_clip_that_hides_the_cursor_hides_its_clicks() { + let clicks = [ + click(500.0, true), + click(560.0, false), + click(3500.0, true), + click(3560.0, false), + ]; + let keyboard = KeyboardEvents::default(); + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + let mut project = project( + serde_json::json!({ + "segments": [ + { "start": 0.0, "end": 2.0, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 2.0, "end": 5.0, "timescale": 1.0, "recordingSegment": 0 } + ], + "zoomSegments": [] + }), + false, + true, + ); + assert_eq!(input_sound_events(&project, &takes).len(), 4); + project.timeline.as_mut().unwrap().segments[1].hide_cursor = Some(true); + let events = input_sound_events(&project, &takes); + assert_eq!(events.len(), 2); + assert!(events.iter().all(|event| event.time < 1.0)); +} + +#[test] +fn a_transition_never_sounds_a_release_before_its_press() { + let keyboard = KeyboardEvents { + presses: vec![press("a", 1900.0, true), press("a", 2100.0, false)], + }; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let mut project = project( + serde_json::json!({ + "segments": [ + { "start": 0.0, "end": 2.0, "timescale": 1.0, "recordingSegment": 0 }, + { "start": 2.0, "end": 4.0, "timescale": 1.0, "recordingSegment": 0 } + ], + "transitions": [{ "segmentIndex": 1, "type": "cross-fade", "duration": 0.5 }], + "zoomSegments": [] + }), + true, + false, + ); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shown(0.0, 4.0)]; + let events = input_sound_events(&project, &takes); + let downs = key_times(&events, true); + let ups = key_times(&events, false); + assert_eq!(downs.len(), 1); + for up in ups { + assert!( + up >= downs[0], + "release at {up} before press at {}", + downs[0] + ); + } +} + +#[test] +fn a_style_segment_that_hides_or_shows_the_cursor_gates_its_clicks() { + let clicks = [click(1_000.0, true), click(3_000.0, true)]; + let keyboard = KeyboardEvents::default(); + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &clicks, + }]; + let mut project = project( + serde_json::json!({ + "segments": [{ "start": 0.0, "end": 5.0, "timescale": 1.0, "recordingSegment": 0 }], + "zoomSegments": [] + }), + false, + true, + ); + let mut hidden = project.cursor.clone(); + hidden.hide = true; + project.timeline.as_mut().unwrap().style_segments = vec![crate::StyleSegment { + start: 2.0, + end: 4.0, + overrides: crate::StyleOverrides { + cursor: Some(hidden), + ..Default::default() + }, + ..Default::default() + }]; + let times: Vec = input_sound_events(&project, &takes) + .iter() + .map(|event| event.time) + .collect(); + assert_eq!(times, vec![1.0]); + + // The other way round: hidden everywhere but shown in the segment. + project.cursor.hide = true; + let timeline = project.timeline.as_mut().unwrap(); + let cursor = timeline.style_segments[0] + .overrides + .cursor + .as_mut() + .unwrap(); + cursor.hide = false; + let times: Vec = input_sound_events(&project, &takes) + .iter() + .map(|event| event.time) + .collect(); + assert_eq!(times, vec![3.0]); +} + +#[test] +fn keys_added_by_hand_sound_as_their_text_strikes_them() { + let keyboard = KeyboardEvents::default(); + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let mut project = project( + serde_json::json!({ + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0, "recordingSegment": 0 }], + "zoomSegments": [] + }), + true, + false, + ); + let mut shortcut = shown(2.0, 3.0); + shortcut.id = format!("{}1", crate::keyboard::MANUAL_KEYBOARD_PREFIX); + shortcut.display_text = "⌘K".into(); + shortcut.keys = crate::keyboard::manual_key_presses("⌘K"); + let mut typed = shown(5.0, 6.0); + typed.id = format!("{}2", crate::keyboard::MANUAL_KEYBOARD_PREFIX); + typed.display_text = "hi".into(); + typed.keys = crate::keyboard::manual_key_presses("hi"); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shortcut, typed]; + + let events = input_sound_events(&project, &takes); + let downs: Vec<(f64, KeySoundClass)> = events + .iter() + .filter_map(|event| match event.kind { + InputSoundKind::Key { class, down: true } => Some((event.time, class)), + _ => None, + }) + .collect(); + // ⌘ goes down at the start with K a beat behind; "hi" one key at a time. + assert_eq!(downs.len(), 4); + assert!((downs[0].0 - 2.0).abs() < 1e-9 && downs[0].1 == KeySoundClass::Modifier); + assert!((downs[1].0 - 2.07).abs() < 1e-9 && downs[1].1 == KeySoundClass::Letter); + assert!((downs[2].0 - 5.0).abs() < 1e-9 && (downs[3].0 - 5.11).abs() < 1e-9); + assert_eq!(key_times(&events, false).len(), 4); + + // A recorded segment of the same text makes no sound of its own. + project.timeline.as_mut().unwrap().keyboard_segments[0].id = "kb-1".into(); + assert_eq!( + key_times(&input_sound_events(&project, &takes), true).len(), + 2 + ); +} + +#[test] +fn a_key_added_by_hand_sounds_only_its_own_keys_over_recorded_typing() { + let keyboard = KeyboardEvents { + presses: vec![press("a", 2_200.0, true), press("a", 2_260.0, false)], + }; + let takes = [InputSoundTake { + keyboard: &keyboard, + keyboard_offset_secs: 0.0, + clicks: &[], + }]; + let mut project = project( + serde_json::json!({ + "segments": [{ "start": 0.0, "end": 10.0, "timescale": 1.0, "recordingSegment": 0 }], + "zoomSegments": [] + }), + true, + false, + ); + let mut shortcut = shown(2.0, 3.0); + shortcut.id = format!("{}0", crate::keyboard::MANUAL_KEYBOARD_PREFIX); + shortcut.display_text = "⌘K".into(); + shortcut.keys = crate::keyboard::manual_key_presses("⌘K"); + project.timeline.as_mut().unwrap().keyboard_segments = vec![shortcut]; + let downs = key_times(&input_sound_events(&project, &takes), true); + assert_eq!(downs.len(), 2, "{downs:?}"); + assert!(downs.iter().all(|time| (*time - 2.2).abs() > 1e-6)); +} diff --git a/crates/project/src/keyboard.rs b/crates/project/src/keyboard.rs index b04caeb7c36..78a4730a332 100644 --- a/crates/project/src/keyboard.rs +++ b/crates/project/src/keyboard.rs @@ -95,7 +95,193 @@ const SPECIAL_KEY_SYMBOLS: &[(&str, &str)] = &[ ("PageDown", "⇟"), ]; -fn is_modifier_key(key: &str) -> bool { +/// Key presses made by hand in the editor, rather than recorded, carry ids +/// with this prefix so regenerating from a recording keeps them. +pub const MANUAL_KEYBOARD_PREFIX: &str = "kb-manual-"; + +/// Whether `segment` was made by hand rather than recorded. +pub fn is_manual_keyboard_segment(segment: &KeyboardTrackSegment) -> bool { + segment.id.starts_with(MANUAL_KEYBOARD_PREFIX) +} + +/// The text a hand-made key press shows, from what was typed for it. A +/// shortcut can be written in words, joined by `+` or spaces, as in +/// `cmd+shift+p` or `ctrl k`, and becomes its keys' symbols; a named key on +/// its own (`enter`, `tab`, `esc`, ...) becomes its symbol; anything else is +/// typed text and shows as written. +pub fn manual_key_text(input: &str) -> String { + let input = input.trim(); + let modifier = |word: &str| -> Option { + match word.to_ascii_lowercase().as_str() { + "cmd" | "command" | "meta" | "super" | "win" | "windows" | "⌘" => Some('⌘'), + "ctrl" | "control" | "⌃" => Some('⌃'), + "alt" | "opt" | "option" | "⌥" => Some('⌥'), + "shift" | "⇧" => Some('⇧'), + _ => None, + } + }; + let named = |word: &str| -> Option { + match word.to_ascii_lowercase().as_str() { + "enter" | "return" => Some('⏎'), + "tab" => Some('⇥'), + "esc" | "escape" => Some('⎋'), + "space" => Some('␣'), + "backspace" => Some('⌫'), + "delete" | "del" => Some('⌦'), + "up" => Some('↑'), + "down" => Some('↓'), + "left" => Some('←'), + "right" => Some('→'), + _ => None, + } + }; + let words: Vec<&str> = if input.contains('+') { + input.split('+').map(str::trim).collect() + } else { + input.split_whitespace().collect() + }; + let shortcut = words.len() > 1 + && words[..words.len() - 1] + .iter() + .all(|word| modifier(word).is_some()); + if shortcut { + let last = words[words.len() - 1]; + let mut text: String = words[..words.len() - 1] + .iter() + .filter_map(|word| modifier(word)) + .collect(); + match (modifier(last), named(last)) { + (Some(symbol), _) | (None, Some(symbol)) => text.push(symbol), + _ => text.extend(last.chars().flat_map(char::to_uppercase)), + } + return text; + } + if words.len() == 1 + && let Some(symbol) = named(words[0]) + { + return symbol.to_string(); + } + input.to_string() +} + +/// One key press of a run the composer lays out: its text, where it starts +/// after the first, and how long it stays up, in seconds. +#[derive(Debug, Clone, PartialEq)] +pub struct ManualKeyRun { + pub text: String, + pub offset: f64, + pub length: f64, +} + +/// Lays `text` out as the key presses that show it, back to back. A +/// shortcut is always one; typed text is one long key (`sentence`), a key +/// per word typed out letter by letter (`words`), or a key per keystroke +/// (`keys`), each held a beat before the next, the last held to be read. +pub fn manual_key_runs(text: &str, split: &str) -> Vec { + // Long enough to read before the next word types in over it. + const WORD_HOLD: f64 = 0.55; + const KEY_HOLD: f64 = 0.16; + // Any modifier makes a shortcut, Shift too: `shift+tab` is struck + // together, never split into keys of its own. + let shortcut = text.chars().any(|c| matches!(c, '⌘' | '⌥' | '⌃' | '⇧')); + let pieces: Vec = match split { + _ if shortcut => vec![text.to_string()], + "keys" => text + .chars() + .filter(|c| !c.is_control()) + .map(|c| { + if c == ' ' { + "␣".to_string() + } else { + c.to_string() + } + }) + .collect(), + "sentence" => vec![text.to_string()], + _ => text.split_whitespace().map(str::to_string).collect(), + }; + let count = pieces.len(); + let mut offset = 0.0; + pieces + .into_iter() + .enumerate() + .filter(|(_, piece)| !piece.is_empty()) + .map(|(index, piece)| { + let keys = manual_key_presses(&piece); + let length = if index + 1 == count { + manual_key_length(&keys) + } else { + let typing = keys.iter().map(|key| key.time_offset).fold(0.0, f64::max) / 1000.0; + typing + if split == "keys" { KEY_HOLD } else { WORD_HOLD } + }; + let run = ManualKeyRun { + text: piece, + offset, + length, + }; + offset += length; + run + }) + .collect() +} + +/// How long a hand-made key press stays up: long enough to strike its last +/// key and then be read, and never shorter than a glance. +pub fn manual_key_length(keys: &[KeyPressDisplay]) -> f64 { + const READ_SECS: f64 = 1.1; + const MIN_SECS: f64 = 1.2; + let last = keys + .iter() + .map(|key| key.time_offset) + .fold(0.0_f64, f64::max); + (last / 1000.0 + READ_SECS).max(MIN_SECS) +} + +/// The keys a hand-made key press strikes, from its text: a shortcut's +/// modifiers go down together with its key a beat behind, and typed text +/// one key after another at a typist's pace. Offsets are milliseconds from +/// the segment's start, as recorded keys carry them. +pub fn manual_key_presses(display_text: &str) -> Vec { + const TYPING_GAP_MS: f64 = 110.0; + const SHORTCUT_KEY_MS: f64 = 70.0; + let key_name = |c: char| -> String { + match c { + '⌘' => "Meta".into(), + '⌥' => "LAlt".into(), + '⇧' => "LShift".into(), + '⌃' => "LControl".into(), + ' ' | '␣' => "Space".into(), + other => SPECIAL_KEY_SYMBOLS + .iter() + .find(|(_, symbol)| symbol.chars().eq(std::iter::once(other))) + .map(|(key, _)| (*key).to_string()) + .unwrap_or_else(|| other.to_uppercase().collect()), + } + }; + let shortcut = display_text.chars().any(|c| matches!(c, '⌘' | '⌥' | '⌃')); + let mut offset = 0.0; + display_text + .chars() + .filter(|c| !c.is_control()) + .map(|c| { + let key = key_name(c); + let time_offset = if shortcut { + if is_modifier_key(&key) { + 0.0 + } else { + SHORTCUT_KEY_MS + } + } else { + let at = offset; + offset += TYPING_GAP_MS; + at + }; + KeyPressDisplay { key, time_offset } + }) + .collect() +} + +pub(crate) fn is_modifier_key(key: &str) -> bool { MODIFIER_KEYS.contains(&key) } @@ -349,7 +535,7 @@ fn project_legacy_keyboard_track( let mut result: Vec<_> = timeline .keyboard_segments .iter() - .filter(|segment| segment.id.starts_with("kb-edit-")) + .filter(|segment| segment.id.starts_with("kb-edit-") || is_manual_keyboard_segment(segment)) .cloned() .collect(); for (index, clip) in timeline.segments.iter().enumerate() { @@ -360,7 +546,7 @@ fn project_legacy_keyboard_track( let source_start = clip.start + offset; let source_end = clip.end + offset; for previous in &timeline.keyboard_segments { - if previous.id.starts_with("kb-edit-") { + if previous.id.starts_with("kb-edit-") || is_manual_keyboard_segment(previous) { continue; } let start = previous.start.max(source_start); @@ -661,27 +847,19 @@ impl<'a> PreparedKeyboardEvents<'a> { } } -fn project_keyboard_events( - takes: &[(KeyboardEvents, f64)], - timeline: &crate::TimelineConfiguration, - settings: &crate::KeyboardSettings, -) -> Vec { - let holds = timeline.hold_windows(); - let output_time = |time: f64, end: bool| { - let mut output = time; - for (start, finish) in &holds { - if output > *start || (!end && output == *start) { - output += finish - start; - } else { - break; - } - } - output - }; +pub(crate) struct ClipWindow<'a> { + pub clip: &'a crate::TimelineSegment, + pub index: usize, + pub edited_start: f64, + pub edited_end: f64, +} + +/// Where each playable clip starts and ends on the edited timeline before +/// fullscreen-text holds are inserted, with transitions overlapping the clips +/// on either side of them. +pub(crate) fn clip_windows(timeline: &crate::TimelineConfiguration) -> Vec> { let mut edited_start = 0.0; - let mut result = Vec::new(); - let mut prepared: Vec> = - (0..takes.len()).map(|_| None).collect(); + let mut windows = Vec::with_capacity(timeline.segments.len()); for (index, clip) in timeline.segments.iter().enumerate() { if !clip.start.is_finite() || !clip.end.is_finite() @@ -695,6 +873,46 @@ fn project_keyboard_events( .effective_transition(index) .map_or(0.0, |transition| transition.duration); let edited_end = edited_start + clip.duration(); + windows.push(ClipWindow { + clip, + index, + edited_start, + edited_end, + }); + edited_start = edited_end; + } + windows +} + +pub(crate) fn held_output_time(holds: &[(f64, f64)], time: f64, end: bool) -> f64 { + let mut output = time; + for (start, finish) in holds { + if output > *start || (!end && output == *start) { + output += finish - start; + } else { + break; + } + } + output +} + +pub(crate) fn project_keyboard_events( + takes: &[(KeyboardEvents, f64)], + timeline: &crate::TimelineConfiguration, + settings: &crate::KeyboardSettings, +) -> Vec { + let holds = timeline.hold_windows(); + let output_time = |time: f64, end: bool| held_output_time(&holds, time, end); + let mut result = Vec::new(); + let mut prepared: Vec> = + (0..takes.len()).map(|_| None).collect(); + for ClipWindow { + clip, + index, + edited_start, + edited_end, + } in clip_windows(timeline) + { if let Some((events, capture_offset)) = takes.get(clip.recording_clip as usize) && capture_offset.is_finite() { @@ -713,13 +931,19 @@ fn project_keyboard_events( projected.time_ms = output_time(time, false) * 1000.0; presses.push(projected); } - let mut grouped = group_key_events( - &KeyboardEvents { presses }, - settings.grouping_threshold_ms, - f64::from(settings.linger_duration) * 1000.0, - settings.show_modifiers, - settings.show_special_keys, - ); + let events = KeyboardEvents { presses }; + let linger_ms = f64::from(settings.linger_duration) * 1000.0; + let mut grouped = if settings.show_typing { + group_key_events( + &events, + settings.grouping_threshold_ms, + linger_ms, + settings.show_modifiers, + settings.show_special_keys, + ) + } else { + group_shortcuts(&events, linger_ms, settings.show_special_keys) + }; for segment in &mut grouped { segment.id = format!("kb-edit-{index}-{}", segment.id); segment.end = segment.end.min(output_time(edited_end, true)); @@ -730,7 +954,6 @@ fn project_keyboard_events( .filter(|segment| segment.end > segment.start), ); } - edited_start = edited_end; } result.sort_by(|a, b| a.start.total_cmp(&b.start)); result @@ -1002,6 +1225,149 @@ pub fn group_key_events( segments } +/// How close together two shortcuts must be to count as one, repeated. +const SHORTCUT_REPEAT_MS: f64 = 2000.0; +/// A label shows a moment before its first press, so it is there as the key +/// goes down, and stops a moment before the next one shows. +const SHORTCUT_LEAD_MS: f64 = 100.0; +const SHORTCUT_GAP_MS: f64 = 120.0; +/// However close the next shortcut, a label stays this long after its last +/// press, and the next one waits for it. +const SHORTCUT_MIN_VISIBLE_MS: f64 = 250.0; +/// Plain keys this close together are someone typing, not single-key +/// shortcuts. +const TYPING_GAP_MS: f64 = 1000.0; + +fn is_single_character(key: &str) -> bool { + key.chars().count() == 1 +} + +/// Key presses as shortcuts only, the way a demo shows them: every press +/// with a modifier (other than Shift on a character, which is typing in +/// capitals), and, with `show_single_keys`, named keys such as Return or +/// Escape and lone characters that are not part of typing. Pressing the same +/// shortcut again within two seconds extends its label rather than starting +/// a new one; each press is kept so the overlay can strike the key again. +pub fn group_shortcuts( + events: &KeyboardEvents, + linger_duration_ms: f64, + show_single_keys: bool, +) -> Vec { + struct Press { + time_ms: f64, + key: String, + display: String, + } + + let mut ordered: Vec<&KeyPressEvent> = events + .presses + .iter() + .filter(|event| event.time_ms.is_finite()) + .collect(); + ordered.sort_by(|a, b| a.time_ms.total_cmp(&b.time_ms)); + + let mut active: Vec = Vec::new(); + let mut presses: Vec = Vec::new(); + let mut plain: Vec<(usize, f64)> = Vec::new(); + for event in ordered { + if is_modifier_key(&event.key) { + if event.down { + if !active.contains(&event.key) { + active.push(event.key.clone()); + } + } else { + active.retain(|key| key != &event.key); + } + continue; + } + if !event.down { + continue; + } + let shift_only = !active.is_empty() + && active + .iter() + .all(|key| matches!(key.as_str(), "LShift" | "RShift")); + let character = is_single_character(&event.key); + let display_key = display_char_for_key(&event.key).unwrap_or_else(|| event.key.clone()); + // Shift on a character is typing a capital, not a shortcut. + let typing_capital = shift_only && character; + if !(active.is_empty() || typing_capital) { + presses.push(Press { + time_ms: event.time_ms, + key: event.key.clone(), + display: format!("{}{}", modifier_prefix(&active), display_key.to_uppercase()), + }); + } else if active.is_empty() && show_single_keys { + if character { + plain.push((presses.len(), event.time_ms)); + } + presses.push(Press { + time_ms: event.time_ms, + key: event.key.clone(), + display: if character { + display_key.to_uppercase() + } else { + display_key + }, + }); + } + } + + // Lone characters next to other characters within a second are typing. + let typing: std::collections::HashSet = plain + .windows(2) + .filter(|pair| pair[1].1 - pair[0].1 <= TYPING_GAP_MS) + .flat_map(|pair| [pair[0].0, pair[1].0]) + .collect(); + + let mut segments: Vec = Vec::new(); + let mut last_press_ms = f64::NEG_INFINITY; + for (index, press) in presses.into_iter().enumerate() { + if typing.contains(&index) { + continue; + } + if let Some(previous) = segments.last_mut() + && previous.display_text == press.display + && press.time_ms - last_press_ms <= SHORTCUT_REPEAT_MS + { + previous.keys.push(KeyPressDisplay { + key: press.key, + time_offset: press.time_ms - previous.start * 1000.0, + }); + previous.end = (press.time_ms + linger_duration_ms) / 1000.0; + last_press_ms = press.time_ms; + continue; + } + let mut start = ((press.time_ms - SHORTCUT_LEAD_MS) / 1000.0).max(0.0); + if let Some(previous) = segments.last_mut() { + previous.end = previous + .end + .min(start - SHORTCUT_GAP_MS / 1000.0) + .max((last_press_ms + SHORTCUT_MIN_VISIBLE_MS) / 1000.0); + start = start.max(previous.end); + } + segments.push(KeyboardTrackSegment { + id: format!("kb-{}", segments.len() + 1), + start, + end: (press.time_ms + linger_duration_ms) / 1000.0, + display_text: press.display, + keys: vec![KeyPressDisplay { + key: press.key, + time_offset: (press.time_ms - start * 1000.0).max(0.0), + }], + fade_duration_override: None, + position_override: None, + color_override: None, + background_color_override: None, + font_size_override: None, + uppercase_override: None, + }); + last_press_ms = press.time_ms; + } + segments.retain(|segment| segment.end > segment.start); + segments +} + fn take_group_keys(keys: &mut Vec) -> Vec { let mut completed = Vec::with_capacity(keys.len()); completed.append(keys); @@ -1030,6 +1396,90 @@ mod tests { } } + fn shortcut(modifiers: &[&str], key: &str, time_ms: f64) -> Vec { + let mut events: Vec = modifiers + .iter() + .map(|m| key_down(m, time_ms - 20.0)) + .collect(); + events.push(key_down(key, time_ms)); + events.push(key_up(key, time_ms + 80.0)); + events.extend(modifiers.iter().map(|m| key_up(m, time_ms + 100.0))); + events + } + + #[test] + fn shortcuts_only_shows_modified_presses_and_not_typing() { + let mut presses = Vec::new(); + presses.extend(shortcut(&["LMeta"], "c", 1000.0)); + // Typing, with Shift for a capital: not shortcuts. + presses.extend(shortcut(&["LShift"], "h", 2000.0)); + presses.extend([key_down("e", 2100.0), key_down("y", 2200.0)]); + presses.extend(shortcut(&["LShift", "LMeta"], "p", 4000.0)); + // Shift with a named key is a shortcut. + presses.extend(shortcut(&["LShift"], "Tab", 6000.0)); + let segments = group_shortcuts(&KeyboardEvents { presses }, 1500.0, false); + let texts: Vec<&str> = segments.iter().map(|s| s.display_text.as_str()).collect(); + assert_eq!(texts, ["⌘C", "⇧⌘P", "⇧⇥"]); + // Shown from just before the press until the linger ends. + assert!((segments[0].start - 0.9).abs() < 1e-9); + assert!((segments[0].end - 2.5).abs() < 1e-9); + } + + #[test] + fn a_repeated_shortcut_is_one_label_struck_each_time() { + let mut presses = Vec::new(); + for time in [1000.0, 1400.0, 1800.0] { + presses.extend(shortcut(&["LMeta"], "z", time)); + } + // Too long after: a new label. + presses.extend(shortcut(&["LMeta"], "z", 5000.0)); + let segments = group_shortcuts(&KeyboardEvents { presses }, 1500.0, false); + assert_eq!(segments.len(), 2); + assert_eq!(segments[0].keys.len(), 3); + assert!((segments[0].end - 3.3).abs() < 1e-9); + let offsets: Vec = segments[0].keys.iter().map(|k| k.time_offset).collect(); + assert_eq!(offsets, [100.0, 500.0, 900.0]); + } + + #[test] + fn a_label_stops_just_before_the_next_one() { + let mut presses = Vec::new(); + presses.extend(shortcut(&["LMeta"], "c", 1000.0)); + presses.extend(shortcut(&["LMeta"], "v", 1600.0)); + let segments = group_shortcuts(&KeyboardEvents { presses }, 1500.0, false); + assert_eq!(segments.len(), 2); + assert!((segments[0].end - (1.5 - 0.12)).abs() < 1e-9); + } + + #[test] + fn shortcuts_pressed_in_quick_succession_each_get_their_moment() { + let mut presses = Vec::new(); + presses.extend(shortcut(&["LMeta"], "c", 1000.0)); + presses.extend(shortcut(&["LMeta"], "v", 1200.0)); + let segments = group_shortcuts(&KeyboardEvents { presses }, 1500.0, false); + assert_eq!(segments.len(), 2); + assert!((segments[0].end - 1.25).abs() < 1e-9, "{}", segments[0].end); + assert!(segments[1].start >= segments[0].end); + assert!(segments[1].end > segments[1].start); + } + + #[test] + fn single_keys_show_only_when_asked_and_never_while_typing() { + let presses = vec![ + key_down("Escape", 1000.0), + key_down("a", 3000.0), + key_down("b", 3200.0), + key_down("x", 6000.0), + ]; + let events = KeyboardEvents { presses }; + assert!(group_shortcuts(&events, 1500.0, false).is_empty()); + let texts: Vec = group_shortcuts(&events, 1500.0, true) + .into_iter() + .map(|s| s.display_text) + .collect(); + assert_eq!(texts, ["⎋", "X"]); + } + #[test] fn completed_groups_preserve_text_keys_and_times_after_reusing_the_accumulator() { for (terminator, time, end) in [ @@ -1649,8 +2099,19 @@ mod tests { #[cfg(test)] mod timing_tests { + /// The typing display these regeneration tests exercise; new projects + /// default to shortcuts only. + fn typing_settings() -> crate::KeyboardSettings { + crate::KeyboardSettings { + show_typing: true, + show_special_keys: true, + linger_duration: 0.8, + ..Default::default() + } + } + use super::*; - use crate::{KeyboardSettings, TimelineConfiguration}; + use crate::TimelineConfiguration; fn timeline(segments: serde_json::Value) -> TimelineConfiguration { serde_json::from_value(serde_json::json!({"segments": segments, "zoomSegments": []})) @@ -1683,7 +2144,9 @@ mod timing_tests { })).unwrap(); meta.project_path = directory.path().to_path_buf(); let mut project = crate::ProjectConfiguration { - keyboard: Some(crate::KeyboardData::default()), + keyboard: Some(crate::KeyboardData { + settings: typing_settings(), + }), timeline: Some(timeline(serde_json::json!([ {"start":0.0,"end":5.0,"timescale":1.0}, {"start":6.0,"end":10.0,"timescale":1.0} @@ -1733,7 +2196,9 @@ mod timing_tests { })).unwrap(); meta.project_path = directory.path().to_path_buf(); let mut project = crate::ProjectConfiguration { - keyboard: Some(crate::KeyboardData::default()), + keyboard: Some(crate::KeyboardData { + settings: typing_settings(), + }), timeline: Some(timeline(serde_json::json!([ {"start":0.0,"end":5.0,"timescale":1.0}, {"start":6.0,"end":10.0,"timescale":1.0} @@ -1798,7 +2263,9 @@ mod timing_tests { })) .unwrap(); let mut project = crate::ProjectConfiguration { - keyboard: Some(crate::KeyboardData::default()), + keyboard: Some(crate::KeyboardData { + settings: typing_settings(), + }), timeline: Some(timeline), ..Default::default() }; @@ -1933,10 +2400,7 @@ mod timing_tests { let mut meta: crate::RecordingMeta = serde_json::from_value(serde_json::json!({"pretty_name":"corrupt", "segments":[{"display":{"path":"display.mp4","fps":30,"start_time":0.0},"keyboard":"keyboard.bin"}],"cursors":{}})).unwrap(); meta.project_path = directory.path().to_path_buf(); let timeline = timeline(serde_json::json!([{ "start":0.0,"end":10.0,"timescale":1.0 }])); - assert!( - generate_project_keyboard_segments(&meta, &timeline, &KeyboardSettings::default()) - .is_err() - ); + assert!(generate_project_keyboard_segments(&meta, &timeline, &typing_settings()).is_err()); } #[test] @@ -1946,7 +2410,7 @@ mod timing_tests { {"start":6.0,"end":10.0,"timescale":1.0} ])); let takes = [(events(&[("x", 5.7), ("a", 8.2)]), 0.2)]; - let result = project_keyboard_events(&takes, &timeline, &KeyboardSettings::default()); + let result = project_keyboard_events(&takes, &timeline, &typing_settings()); assert_eq!(result.len(), 1); assert_eq!(result[0].display_text, "a"); assert!((result[0].start - 7.0).abs() < 1e-9); @@ -1964,7 +2428,7 @@ mod timing_tests { let result = project_keyboard_events( &[(events(&[("a", 1.0)]), 0.0), (events(&[("b", 1.0)]), 0.0)], &timeline, - &KeyboardSettings::default(), + &typing_settings(), ); assert_eq!( result @@ -1992,7 +2456,7 @@ mod timing_tests { (events(&[("b", 1.0)]), 0.0), ], &timeline, - &KeyboardSettings::default(), + &typing_settings(), ); assert_eq!(result[0].start, 5.0); let (source, clip) = timeline.get_segment_time(result[0].start).unwrap(); @@ -2006,7 +2470,7 @@ mod timing_tests { let result = project_keyboard_events( &[(events(&[("a", 8.0), ("b", 8.4)]), 0.0)], &timeline, - &KeyboardSettings::default(), + &typing_settings(), ); assert_eq!(result.len(), 1); assert_eq!(result[0].start, 0.5); @@ -2026,7 +2490,7 @@ mod timing_tests { let result = project_keyboard_events( &[(events(&[("a", 2.0), ("b", 3.0)]), 0.0)], &timeline, - &KeyboardSettings::default(), + &typing_settings(), ); assert_eq!(result[0].start, 4.0); assert_eq!(result[1].start, 5.0); @@ -2038,7 +2502,7 @@ mod timing_tests { let result = project_keyboard_events( &[(events(&[("LMeta", 1.0), ("w", 2.2)]), 0.0)], &timeline, - &KeyboardSettings::default(), + &typing_settings(), ); assert_eq!(result.len(), 1); assert_eq!(result[0].display_text, "⌘W"); @@ -2103,3 +2567,68 @@ mod timing_tests { assert!(!segment.ripple_delete(10.5, 11.5, 1.0)); } } + +#[cfg(test)] +mod manual_key_tests { + use super::*; + + #[test] + fn a_sentence_splits_into_words_or_keys_back_to_back() { + let words = manual_key_runs("ship it now", "words"); + assert_eq!( + words + .iter() + .map(|run| run.text.as_str()) + .collect::>(), + vec!["ship", "it", "now"] + ); + for pair in words.windows(2) { + assert!((pair[0].offset + pair[0].length - pair[1].offset).abs() < 1e-9); + } + // The last word is held to be read. + assert!(words[2].length >= 1.2); + let keys = manual_key_runs("hi you", "keys"); + assert_eq!( + keys.iter().map(|run| run.text.as_str()).collect::>(), + vec!["h", "i", "␣", "y", "o", "u"] + ); + assert!(keys[0].length < 0.3); + assert_eq!(manual_key_runs("ship it", "sentence").len(), 1); + // A shortcut is always one key press. + assert_eq!(manual_key_runs("⌘⇧P", "keys").len(), 1); + assert_eq!(manual_key_runs("⇧⇥", "keys").len(), 1); + assert!(manual_key_runs(" ", "words").is_empty()); + } + + #[test] + fn words_become_a_shortcuts_symbols_and_text_stays_text() { + assert_eq!(manual_key_text("cmd+shift+p"), "⌘⇧P"); + assert_eq!(manual_key_text("Ctrl K"), "⌃K"); + assert_eq!(manual_key_text("alt + enter"), "⌥⏎"); + assert_eq!(manual_key_text("enter"), "⏎"); + assert_eq!(manual_key_text("⌘K"), "⌘K"); + assert_eq!(manual_key_text("hello world"), "hello world"); + assert_eq!(manual_key_text("shift"), "shift"); + assert_eq!(manual_key_text(""), ""); + } + + #[test] + fn a_shortcut_strikes_its_key_after_its_modifiers_and_text_one_key_at_a_time() { + let shortcut = manual_key_presses("⌘⇧P"); + assert_eq!( + shortcut + .iter() + .map(|key| (key.key.as_str(), key.time_offset)) + .collect::>(), + vec![("Meta", 0.0), ("LShift", 0.0), ("P", 70.0)] + ); + let typed = manual_key_presses("hi ⏎"); + assert_eq!( + typed + .iter() + .map(|key| (key.key.as_str(), key.time_offset)) + .collect::>(), + vec![("H", 0.0), ("I", 110.0), ("Space", 220.0), ("Enter", 330.0)] + ); + } +} diff --git a/crates/project/src/keyboard/projection_tests.rs b/crates/project/src/keyboard/projection_tests.rs index f97e797b641..959a78e2540 100644 --- a/crates/project/src/keyboard/projection_tests.rs +++ b/crates/project/src/keyboard/projection_tests.rs @@ -237,6 +237,9 @@ fn indexed_projection_matches_baseline_with_offsets_speed_transitions_and_holds( for show_modifiers in [false, true] { let settings = KeyboardSettings { show_modifiers, + show_typing: true, + show_special_keys: true, + linger_duration: 0.8, ..Default::default() }; assert_eq!( @@ -299,7 +302,12 @@ fn benchmark_keyboard_projection() { timeline.segments[0].end = start + 2.0; } let takes = [(events, 0.0)]; - let settings = KeyboardSettings::default(); + let settings = KeyboardSettings { + show_typing: true, + show_special_keys: true, + linger_duration: 0.8, + ..Default::default() + }; let expected = baseline_project_keyboard_events(&takes, &timeline, &settings); let actual = project_keyboard_events(&takes, &timeline, &settings); assert_eq!( diff --git a/crates/project/src/lib.rs b/crates/project/src/lib.rs index c9d78c17d37..8c5ced7e2c3 100644 --- a/crates/project/src/lib.rs +++ b/crates/project/src/lib.rs @@ -8,6 +8,7 @@ mod config_write_tests; mod configuration; pub mod cursor; pub mod disfluency; +pub mod input_sounds; pub mod keyboard; mod late_audio; mod meta; diff --git a/crates/project/src/meta.rs b/crates/project/src/meta.rs index fe2bc4b8634..4886cf0eb23 100644 --- a/crates/project/src/meta.rs +++ b/crates/project/src/meta.rs @@ -520,6 +520,19 @@ impl StudioRecordingMeta { } } + /// A project started empty rather than recorded. + pub fn is_blank(&self) -> bool { + match self { + StudioRecordingMeta::SingleSegment { .. } => false, + StudioRecordingMeta::MultipleSegments { inner } => inner.blank, + } + } + + /// Whether the display track is a placeholder that is never drawn. + pub fn hides_display(&self) -> bool { + self.is_audio_only() || self.is_blank() + } + /// Notch captured with this recording. Every segment shares one display, so /// the first segment speaks for all of them. `None` for recordings made /// before Cap started capturing this, and for every non-macOS recording. @@ -720,6 +733,11 @@ pub struct MultipleSegments { /// recovery and the editor on their usual paths; it is never shown. #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub audio_only: bool, + /// Made from nothing: a project started empty to build a video from the + /// background, music, keys, text and images alone. Its display track is + /// the same never-shown placeholder, as long as the project can run. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub blank: bool, } fn is_legacy_cursor_shape_version(version: &u32) -> bool { @@ -1783,6 +1801,7 @@ mod ordinary_media_access_tests { status, cursor_shape_version: 0, audio_only: false, + blank: false, }, } } diff --git a/crates/recording/src/recovery.rs b/crates/recording/src/recovery.rs index f0215d3aa39..ae6fef43c8d 100644 --- a/crates/recording/src/recovery.rs +++ b/crates/recording/src/recovery.rs @@ -2938,6 +2938,10 @@ impl RecoveryManager { .meta .studio_meta() .is_some_and(StudioRecordingMeta::is_audio_only), + blank: recording + .meta + .studio_meta() + .is_some_and(StudioRecordingMeta::is_blank), }, }) } @@ -5291,6 +5295,7 @@ mod clean_studio_snapshot_tests { status: Some(status), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), } @@ -7390,6 +7395,7 @@ mod empty_orphan_segment_tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; @@ -7805,6 +7811,7 @@ mod transactional_recovery_tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; @@ -8508,6 +8515,7 @@ mod transactional_recovery_tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; diff --git a/crates/recording/src/recovery/needs_remux_tests.rs b/crates/recording/src/recovery/needs_remux_tests.rs index ae11a64dec2..9fe2145b5c2 100644 --- a/crates/recording/src/recovery/needs_remux_tests.rs +++ b/crates/recording/src/recovery/needs_remux_tests.rs @@ -84,6 +84,7 @@ fn needs_remux_project(parent: &Path, name: &str, fragments: usize) -> PathBuf { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), } diff --git a/crates/recording/src/recovery/preparing_observer.rs b/crates/recording/src/recovery/preparing_observer.rs index 2c629c493bd..031569ca811 100644 --- a/crates/recording/src/recovery/preparing_observer.rs +++ b/crates/recording/src/recovery/preparing_observer.rs @@ -479,6 +479,7 @@ mod tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), upload: None, diff --git a/crates/recording/src/recovery/preparing_projection.rs b/crates/recording/src/recovery/preparing_projection.rs index 0f960532bf3..ddd1a26c6fe 100644 --- a/crates/recording/src/recovery/preparing_projection.rs +++ b/crates/recording/src/recovery/preparing_projection.rs @@ -888,6 +888,7 @@ mod tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, }, )), diff --git a/crates/recording/src/studio_recording.rs b/crates/recording/src/studio_recording.rs index 676a181178b..8757c9c0e95 100644 --- a/crates/recording/src/studio_recording.rs +++ b/crates/recording/src/studio_recording.rs @@ -3322,6 +3322,7 @@ async fn stop_recording( status, cursor_shape_version: cap_project::CURSOR_SHAPE_VERSION, audio_only, + blank: false, }, }; @@ -4294,6 +4295,7 @@ fn write_in_progress_meta_for( status: Some(StudioRecordingStatus::InProgress), cursor_shape_version: cap_project::CURSOR_SHAPE_VERSION, audio_only, + blank: false, }, })), upload: None, @@ -7805,6 +7807,7 @@ mod tests { status: Some(StudioRecordingStatus::Complete), cursor_shape_version: 0, audio_only: false, + blank: false, }, }; let persisted = @@ -7947,6 +7950,7 @@ mod clean_stop_receipt_tests { status: Some(StudioRecordingStatus::NeedsRemux), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), upload: None, diff --git a/crates/recording/src/track_heal.rs b/crates/recording/src/track_heal.rs index d2a1f4d9a9a..c168445706f 100644 --- a/crates/recording/src/track_heal.rs +++ b/crates/recording/src/track_heal.rs @@ -796,6 +796,7 @@ mod tests { status: None, cursor_shape_version: 0, audio_only: false, + blank: false, }, })), } diff --git a/crates/recording/tests/recovery.rs b/crates/recording/tests/recovery.rs index a5302a508d3..ee11d288fad 100644 --- a/crates/recording/tests/recovery.rs +++ b/crates/recording/tests/recovery.rs @@ -143,6 +143,7 @@ impl TestRecording { status: Some(status), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; @@ -2342,6 +2343,7 @@ fn recovery_aligns_tracks_with_the_start_times_persisted_while_recording() { status: Some(StudioRecordingStatus::InProgress), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; @@ -2398,6 +2400,7 @@ fn recovery_keeps_the_camera_frame_rate_of_an_interrupted_recording() { status: Some(StudioRecordingStatus::InProgress), cursor_shape_version: 0, audio_only: false, + blank: false, }, })), }; diff --git a/crates/render-farm/src/audio.rs b/crates/render-farm/src/audio.rs index 0eb34e89281..0ea3d3c9bee 100644 --- a/crates/render-farm/src/audio.rs +++ b/crates/render-farm/src/audio.rs @@ -83,7 +83,14 @@ pub async fn render(request: AudioRequest) -> Result { (end + 2 * PACKET_SAMPLES).min(total_samples) }; - let mut renderer = AudioRenderer::new(segments).with_music(music); + let input_sounds = cap_editor::InputSounds::load( + &project.config, + &project.recording_meta, + &project.studio_meta, + ); + let mut renderer = AudioRenderer::new(segments) + .with_music(music) + .with_input_sounds(input_sounds); renderer.set_playhead(input_start as f64 / SAMPLE_RATE as f64, &project.config); let codec = ffmpeg::encoder::find(ffmpeg::codec::Id::AAC).context("aac encoder")?; diff --git a/crates/rendering/assets/fonts/overlay/IBMPlexSans-Medium.ttf b/crates/rendering/assets/fonts/overlay/IBMPlexSans-Medium.ttf new file mode 100644 index 00000000000..d9c2edb7305 Binary files /dev/null and b/crates/rendering/assets/fonts/overlay/IBMPlexSans-Medium.ttf differ diff --git a/crates/rendering/assets/fonts/overlay/IBMPlexSans-OFL.txt b/crates/rendering/assets/fonts/overlay/IBMPlexSans-OFL.txt new file mode 100644 index 00000000000..c35c4c618fa --- /dev/null +++ b/crates/rendering/assets/fonts/overlay/IBMPlexSans-OFL.txt @@ -0,0 +1,93 @@ +Copyright © 2017 IBM Corp. with Reserved Font Name "Plex" + +This Font Software is licensed under the SIL Open Font License, Version 1.1. + +This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/crates/rendering/assets/fonts/overlay/Inter-Medium-Keycaps.ttf b/crates/rendering/assets/fonts/overlay/Inter-Medium-Keycaps.ttf new file mode 100644 index 00000000000..26b0b66c1c3 Binary files /dev/null and b/crates/rendering/assets/fonts/overlay/Inter-Medium-Keycaps.ttf differ diff --git a/crates/rendering/assets/fonts/overlay/Inter-OFL.txt b/crates/rendering/assets/fonts/overlay/Inter-OFL.txt new file mode 100644 index 00000000000..21f6aff9610 --- /dev/null +++ b/crates/rendering/assets/fonts/overlay/Inter-OFL.txt @@ -0,0 +1,93 @@ +Copyright 2020 The Inter Project Authors (https://github.com/rsms/inter) + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +https://scripts.sil.org/OFL + + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- + +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/crates/rendering/assets/fonts/overlay/Inter-SemiBold-Captions.ttf b/crates/rendering/assets/fonts/overlay/Inter-SemiBold-Captions.ttf new file mode 100644 index 00000000000..c2f9f161b57 Binary files /dev/null and b/crates/rendering/assets/fonts/overlay/Inter-SemiBold-Captions.ttf differ diff --git a/crates/rendering/assets/fonts/overlay/README.md b/crates/rendering/assets/fonts/overlay/README.md new file mode 100644 index 00000000000..9272f30ba2f --- /dev/null +++ b/crates/rendering/assets/fonts/overlay/README.md @@ -0,0 +1,13 @@ +# Overlay fonts + +The key-press overlay draws each keyboard's keys in a typeface that fits it, +and captions can be set in Inter. These fonts are embedded in the renderer so +exports look the same on every platform. + +| File | Font | Licence | Notes | +| --- | --- | --- | --- | +| `Inter-Medium-Keycaps.ttf` | Inter 4, weight 500, renamed "Inter Keycaps" | SIL OFL 1.1 (`Inter-OFL.txt`) | A static instance of the variable font, subset to Latin, punctuation, arrows and keyboard symbols, with the shift and caps lock arrows redrawn to stand at cap height in the stroke of the other modifier symbols. Every key symbol is printed in this face whatever the key's own face. Inter has no Reserved Font Name. | +| `Inter-SemiBold-Captions.ttf` | Inter 4, weight 600, optical size 28 | SIL OFL 1.1 (`Inter-OFL.txt`) | A static instance of the variable font, subset to Latin, Latin Extended, Vietnamese, Greek, Cyrillic, punctuation, currency and arrows. Other scripts fall back to system fonts. | +| `Selawik-Regular.ttf` | Selawik 1.01 | SIL OFL 1.1 (`Selawik-OFL.txt`) | Unmodified. | +| `Ubuntu-Medium.ttf` | Ubuntu Medium | Ubuntu Font Licence 1.0 (`Ubuntu-UFL.txt`) | Unmodified. | +| `IBMPlexSans-Medium.ttf` | IBM Plex Sans Medium | SIL OFL 1.1 (`IBMPlexSans-OFL.txt`) | Unmodified. | diff --git a/crates/rendering/assets/fonts/overlay/Selawik-OFL.txt b/crates/rendering/assets/fonts/overlay/Selawik-OFL.txt new file mode 100644 index 00000000000..ac8fdb95604 --- /dev/null +++ b/crates/rendering/assets/fonts/overlay/Selawik-OFL.txt @@ -0,0 +1,91 @@ +Copyright 2015, Microsoft Corporation (www.microsoft.com), with Reserved Font Name Selawik. All Rights Reserved. Selawik is a trademark of Microsoft Corporation in the United States and/or other countries. + +This Font Software is licensed under the SIL Open Font License, Version 1.1. +This license is copied below, and is also available with a FAQ at: +http://scripts.sil.org/OFL + +----------------------------------------------------------- +SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007 +----------------------------------------------------------- +PREAMBLE +The goals of the Open Font License (OFL) are to stimulate worldwide +development of collaborative font projects, to support the font creation +efforts of academic and linguistic communities, and to provide a free and +open framework in which fonts may be shared and improved in partnership +with others. + +The OFL allows the licensed fonts to be used, studied, modified and +redistributed freely as long as they are not sold by themselves. The +fonts, including any derivative works, can be bundled, embedded, +redistributed and/or sold with any software provided that any reserved +names are not used by derivative works. The fonts and derivatives, +however, cannot be released under any other type of license. The +requirement for fonts to remain under this license does not apply +to any document created using the fonts or their derivatives. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this license and clearly marked as such. This may +include source files, build scripts and documentation. + +"Reserved Font Name" refers to any names specified as such after the +copyright statement(s). + +"Original Version" refers to the collection of Font Software components as +distributed by the Copyright Holder(s). + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to a +new environment. + +"Author" refers to any designer, engineer, programmer, technical +writer or other person who contributed to the Font Software. + +PERMISSION & CONDITIONS +Permission is hereby granted, free of charge, to any person obtaining +a copy of the Font Software, to use, study, copy, merge, embed, modify, +redistribute, and sell modified and unmodified copies of the Font +Software, subject to the following conditions: + +1) Neither the Font Software nor any of its individual components, +in Original or Modified Versions, may be sold by itself. + +2) Original or Modified Versions of the Font Software may be bundled, +redistributed and/or sold with any software, provided that each copy +contains the above copyright notice and this license. These can be +included either as stand-alone text files, human-readable headers or +in the appropriate machine-readable metadata fields within text or +binary files as long as those fields can be easily viewed by the user. + +3) No Modified Version of the Font Software may use the Reserved Font +Name(s) unless explicit written permission is granted by the corresponding +Copyright Holder. This restriction only applies to the primary font name as +presented to the users. + +4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font +Software shall not be used to promote, endorse or advertise any +Modified Version, except to acknowledge the contribution(s) of the +Copyright Holder(s) and the Author(s) or with their explicit written +permission. + +5) The Font Software, modified or unmodified, in part or in whole, +must be distributed entirely under this license, and must not be +distributed under any other license. The requirement for fonts to +remain under this license does not apply to any document created +using the Font Software. + +TERMINATION +This license becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT +OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM +OTHER DEALINGS IN THE FONT SOFTWARE. diff --git a/crates/rendering/assets/fonts/overlay/Selawik-Regular.ttf b/crates/rendering/assets/fonts/overlay/Selawik-Regular.ttf new file mode 100644 index 00000000000..736bac3c210 Binary files /dev/null and b/crates/rendering/assets/fonts/overlay/Selawik-Regular.ttf differ diff --git a/crates/rendering/assets/fonts/overlay/Ubuntu-Medium.ttf b/crates/rendering/assets/fonts/overlay/Ubuntu-Medium.ttf new file mode 100644 index 00000000000..c6f048e44c6 Binary files /dev/null and b/crates/rendering/assets/fonts/overlay/Ubuntu-Medium.ttf differ diff --git a/crates/rendering/assets/fonts/overlay/Ubuntu-UFL.txt b/crates/rendering/assets/fonts/overlay/Ubuntu-UFL.txt new file mode 100644 index 00000000000..ae78a8f94ea --- /dev/null +++ b/crates/rendering/assets/fonts/overlay/Ubuntu-UFL.txt @@ -0,0 +1,96 @@ +------------------------------- +UBUNTU FONT LICENCE Version 1.0 +------------------------------- + +PREAMBLE +This licence allows the licensed fonts to be used, studied, modified and +redistributed freely. The fonts, including any derivative works, can be +bundled, embedded, and redistributed provided the terms of this licence +are met. The fonts and derivatives, however, cannot be released under +any other licence. The requirement for fonts to remain under this +licence does not require any document created using the fonts or their +derivatives to be published under this licence, as long as the primary +purpose of the document is not to be a vehicle for the distribution of +the fonts. + +DEFINITIONS +"Font Software" refers to the set of files released by the Copyright +Holder(s) under this licence and clearly marked as such. This may +include source files, build scripts and documentation. + +"Original Version" refers to the collection of Font Software components +as received under this licence. + +"Modified Version" refers to any derivative made by adding to, deleting, +or substituting -- in part or in whole -- any of the components of the +Original Version, by changing formats or by porting the Font Software to +a new environment. + +"Copyright Holder(s)" refers to all individuals and companies who have a +copyright ownership of the Font Software. + +"Substantially Changed" refers to Modified Versions which can be easily +identified as dissimilar to the Font Software by users of the Font +Software comparing the Original Version with the Modified Version. + +To "Propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification and with or without charging +a redistribution fee), making available to the public, and in some +countries other activities as well. + +PERMISSION & CONDITIONS +This licence does not grant any rights under trademark law and all such +rights are reserved. + +Permission is hereby granted, free of charge, to any person obtaining a +copy of the Font Software, to propagate the Font Software, subject to +the below conditions: + +1) Each copy of the Font Software must contain the above copyright +notice and this licence. These can be included either as stand-alone +text files, human-readable headers or in the appropriate machine- +readable metadata fields within text or binary files as long as those +fields can be easily viewed by the user. + +2) The font name complies with the following: +(a) The Original Version must retain its name, unmodified. +(b) Modified Versions which are Substantially Changed must be renamed to +avoid use of the name of the Original Version or similar names entirely. +(c) Modified Versions which are not Substantially Changed must be +renamed to both (i) retain the name of the Original Version and (ii) add +additional naming elements to distinguish the Modified Version from the +Original Version. The name of such Modified Versions must be the name of +the Original Version, with "derivative X" where X represents the name of +the new work, appended to that name. + +3) The name(s) of the Copyright Holder(s) and any contributor to the +Font Software shall not be used to promote, endorse or advertise any +Modified Version, except (i) as required by this licence, (ii) to +acknowledge the contribution(s) of the Copyright Holder(s) or (iii) with +their explicit written permission. + +4) The Font Software, modified or unmodified, in part or in whole, must +be distributed entirely under this licence, and must not be distributed +under any other licence. The requirement for fonts to remain under this +licence does not affect any document created using the Font Software, +except any version of the Font Software extracted from a document +created using the Font Software may only be distributed under this +licence. + +TERMINATION +This licence becomes null and void if any of the above conditions are +not met. + +DISCLAIMER +THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF +COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE +COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL +DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER +DEALINGS IN THE FONT SOFTWARE. diff --git a/crates/rendering/src/layers/animated_gradient.rs b/crates/rendering/src/layers/animated_gradient.rs index e44a0b2df43..043aa16681f 100644 --- a/crates/rendering/src/layers/animated_gradient.rs +++ b/crates/rendering/src/layers/animated_gradient.rs @@ -15,6 +15,17 @@ struct AnimatedGradientUniforms { texture: [f32; 4], motion: [f32; 4], output: [f32; 4], + /// 1 to draw an ordered dither of the palette, then the dither's cell in + /// output pixels. + dither: [f32; 4], +} + +/// The dither's cell in output pixels: `size` is set against a 1080p frame, +/// so the pattern keeps its look at every export size and in the preview. +fn dither_cell(size: f32, output: (u32, u32)) -> f32 { + (size * output.1.min(output.0) as f32 / 1080.0) + .round() + .max(1.0) } impl AnimatedGradientUniforms { @@ -60,6 +71,14 @@ impl AnimatedGradientUniforms { config.color_stops.len() as f32, config.detail, ], + dither: [ + f32::from(u8::from( + config.style == cap_project::AnimatedGradientStyle::Dither, + )), + dither_cell(config.dither_size, output), + 0.0, + 0.0, + ], } } } @@ -285,6 +304,8 @@ impl AnimatedGradientLayer { uniforms.motion[0] = (seconds * self.motion_speed) as f32; uniforms.output[0] = project.output_size.0 as f32; uniforms.output[1] = project.output_size.1 as f32; + uniforms.dither[1] = + dither_cell(self.config.dither_size.clamp(1.0, 8.0), project.output_size); if uniforms != self.uniforms { queue.write_buffer(&self.buffer, 0, bytemuck::bytes_of(&uniforms)); self.uniforms = uniforms; @@ -368,7 +389,7 @@ mod tests { assert_ne!(at(&config, 2.0), at(&config, 3.0)); config.motion_speed = 0.0; assert_eq!(at(&config, 0.0), at(&config, 7200.0)); - assert_eq!(std::mem::size_of::(), 160); + assert_eq!(std::mem::size_of::(), 176); } async fn test_constants() -> RenderVideoConstants { diff --git a/crates/rendering/src/layers/captions.rs b/crates/rendering/src/layers/captions.rs index c3afc18354b..5d4cbeb1f2b 100644 --- a/crates/rendering/src/layers/captions.rs +++ b/crates/rendering/src/layers/captions.rs @@ -49,7 +49,9 @@ struct CaptionBackgroundUniforms { rect: [f32; 4], color: [f32; 4], radius: f32, - _padding: [f32; 3], + shadow: f32, + edge: f32, + _padding: f32, _padding2: [f32; 4], } @@ -127,6 +129,18 @@ impl CaptionFrameBox { pub fn is_at(&self, position: &str) -> bool { self.position == CaptionPosition::from_str(position) } + + /// Whether the box is held to the left or right margin, so it widens + /// from that edge rather than about its centre. + pub fn is_side_pinned(&self) -> bool { + matches!( + self.position, + CaptionPosition::TopLeft + | CaptionPosition::BottomLeft + | CaptionPosition::TopRight + | CaptionPosition::BottomRight + ) + } } /// Top-left corner of a caption box of `box_size` at rest in a frame of @@ -196,6 +210,13 @@ fn record_caption_center(position: CaptionPosition, rect: [f32; 4], frame: (u32, } const BASE_TEXT_OPACITY: f32 = 0.8; + +/// The caption preset drawn as a card in the keycaps' material. +pub const SOFT_PRESET: &str = "soft"; +/// The Soft card's shadow, as dark as the dark keycaps'. +const SOFT_SHADOW: f32 = 0.42; +/// The Soft card's lit top edge. +const SOFT_EDGE: f32 = 0.16; const BOUNCE_OFFSET_PIXELS: f32 = 8.0; // Safety net for caption segments whose trailing word end was stretched across a // silence by transcription (e.g. a 16s "seconds."). Without this, such a segment @@ -458,6 +479,27 @@ fn caption_text_left(buffer: &Buffer, content_left: f32, render_scale: f32) -> f /// A size of 0 (from a hand-edited or tool-written config; the editor's /// slider stops at 12) would give cosmic-text a zero line height, which it /// asserts against. +/// The family and weight a caption or key label setting draws with. Inter +/// ships with the renderer at one weight, which every weight setting uses. +pub(crate) fn overlay_face(font: &str, font_weight: u32) -> (Family<'static>, Weight) { + let family = match font { + "System Serif" => Family::Serif, + "System Monospace" => Family::Monospace, + super::keycaps::OVERLAY_TEXT_FONT => Family::Name(super::keycaps::OVERLAY_TEXT_FONT), + _ => Family::SansSerif, + }; + let weight = if font == super::keycaps::OVERLAY_TEXT_FONT { + Weight::SEMIBOLD + } else if font_weight >= 700 { + Weight::BOLD + } else if font_weight >= 500 { + Weight::MEDIUM + } else { + Weight::NORMAL + }; + (family, weight) +} + pub(crate) fn scaled_font_size(size: u32, output_height: u32) -> Option { let font_size = size as f32 * (output_height as f32 / 1080.0); (font_size > 0.0).then_some(font_size) @@ -564,7 +606,9 @@ impl CaptionsLayer { rect: [0.0; 4], color: [0.0; 4], radius: 0.0, - _padding: [0.0; 3], + shadow: 0.0, + edge: 0.0, + _padding: 0.0, _padding2: [0.0; 4], }; @@ -862,19 +906,10 @@ impl CaptionsLayer { return; }; let wrap_width = (width as f32 - margin * 2.0).max(font_size); - let font_family = match caption_data.settings.font.as_str() { - "System Serif" => Family::Serif, - "System Monospace" => Family::Monospace, - _ => Family::SansSerif, - }; - - let weight = if caption_data.settings.font_weight >= 700 { - Weight::BOLD - } else if caption_data.settings.font_weight >= 500 { - Weight::MEDIUM - } else { - Weight::NORMAL - }; + let (font_family, weight) = overlay_face( + &caption_data.settings.font, + caption_data.settings.font_weight, + ); let base_alpha = (fade_opacity * BASE_TEXT_OPACITY).clamp(0.0, 1.0); let highlight_alpha = fade_opacity.clamp(0.0, 1.0); @@ -949,20 +984,29 @@ impl CaptionsLayer { layout_width = layout_width.max(font_size); } + // The Soft card is made like the keycaps above it: a wider, calmer + // inset, their corner, their shadow and their lit top edge. + let soft = caption_data.settings.preset == SOFT_PRESET; + let (pad_x_ratio, pad_y_ratio, radius_ratio) = if soft { + (0.62, 0.42, 0.36) + } else { + (0.5, 0.5, 0.55) + }; let available_width = (width as f32 - margin * 2.0).max(1.0); - let initial_padding = font_size * 0.5; + let initial_padding = font_size * pad_x_ratio; let fit_scale = if layout_width + initial_padding * 2.0 > available_width { (available_width / (layout_width + initial_padding * 2.0)).clamp(0.35, 1.0) } else { 1.0 }; let effective_font_size = font_size * fit_scale; - let padding = effective_font_size * 0.5; - let corner_radius = effective_font_size * 0.55; + let pad_x = effective_font_size * pad_x_ratio; + let pad_y = effective_font_size * pad_y_ratio; + let corner_radius = effective_font_size * radius_ratio; let text_width = (layout_width * fit_scale).min(available_width); let text_height = layout_height * fit_scale; - let box_width = (text_width + padding * 2.0).min(available_width).max(1.0); - let box_height = (text_height + padding * 2.0).min(height as f32).max(1.0); + let box_width = (text_width + pad_x * 2.0).min(available_width).max(1.0); + let box_height = (text_height + pad_y * 2.0).min(height as f32).max(1.0); let (background_left, base_background_top) = caption_box_origin( position, @@ -984,9 +1028,9 @@ impl CaptionsLayer { let draw_text_width = text_width * anim_scale; let draw_text_height = text_height * anim_scale; - let content_left = draw_box_left + padding * anim_scale; + let content_left = draw_box_left + pad_x * anim_scale; let text_left = caption_text_left(&self.text.buffer, content_left, render_scale); - let text_top = draw_box_top + padding * anim_scale; + let text_top = draw_box_top + pad_y * anim_scale; let bounds = TextBounds { left: (content_left - 2.0).floor() as i32, @@ -1097,7 +1141,19 @@ impl CaptionsLayer { radius: (corner_radius * anim_scale) .min(draw_box_width / 2.0) .min(draw_box_height / 2.0), - _padding: [0.0; 3], + shadow: if soft { + SOFT_SHADOW + * fade_opacity + * (caption_data.settings.background_opacity as f32 / 60.0).min(1.0) + } else { + 0.0 + }, + edge: if soft { + SOFT_EDGE * background_alpha + } else { + 0.0 + }, + _padding: 0.0, _padding2: [0.0; 4], }; @@ -1131,7 +1187,9 @@ impl CaptionsLayer { fade_opacity, ], radius: pill_radius, - _padding: [0.0; 3], + shadow: 0.0, + edge: 0.0, + _padding: 0.0, _padding2: [0.0; 4], }; queue.write_buffer( @@ -1166,7 +1224,12 @@ impl CaptionsLayer { } } - let scissor_padding = 4.0; + // Room for the Soft card's shadow, which falls below and around it. + let scissor_padding = if soft { + 4.0 + draw_box_height * 0.42 + } else { + 4.0 + }; let scissor_x = (draw_box_left - scissor_padding).max(0.0).floor() as u32; let scissor_y = (draw_box_top - scissor_padding).max(0.0).floor() as u32; let max_width = width.saturating_sub(scissor_x); @@ -1379,7 +1442,14 @@ mod tests { "سلام Cap 123 دنیا", "Hello world", ] { - for wrap_width in [80.0_f32, 1800.0] { + for (wrap_width, (family, weight)) in [80.0_f32, 1800.0].into_iter().flat_map(|width| { + [ + (Family::SansSerif, Weight::BOLD), + // Inter ships without Arabic, which falls back. + super::overlay_face("Inter", 700), + ] + .map(|face| (width, face)) + }) { for colors in [ CaptionTextColors::Uniform(Color::rgb(255, 255, 255)), CaptionTextColors::Spans(vec![(0..text.len(), Color::rgb(255, 200, 0))]), @@ -1390,8 +1460,8 @@ mod tests { text: text.to_string(), font_size: 48.0_f32.to_bits(), wrap_width: wrap_width.to_bits(), - family: Family::SansSerif, - weight: Weight::BOLD, + family, + weight, colors, }, ); @@ -1555,6 +1625,18 @@ mod tests { #[test] fn the_frame_box_knows_its_placement() { + for (position, pinned) in [ + ("bottom-left", true), + ("top-right", true), + ("bottom-center", false), + ("manual", false), + ] { + assert_eq!( + CaptionFrameBox::new([0.0, 0.0, 10.0, 10.0], position).is_side_pinned(), + pinned, + "{position}" + ); + } let manual = CaptionFrameBox::new([0.0, 0.0, 10.0, 10.0], "manual"); assert!(manual.is_manual()); assert!(manual.is_at("manual")); @@ -2401,9 +2483,11 @@ mod gpu_tests { assert!(layer.has_caption); render_pixels(&constants, layer, size) }; + // A system font, which every weight setting changes. let plain = CaptionSettings { enabled: true, size: 60, + font: "System Sans-Serif".to_string(), ..Default::default() }; let color_highlight = CaptionSettings { diff --git a/crates/rendering/src/layers/keyboard.rs b/crates/rendering/src/layers/keyboard.rs index 15d72a7bcdf..5db567e3fd6 100644 --- a/crates/rendering/src/layers/keyboard.rs +++ b/crates/rendering/src/layers/keyboard.rs @@ -8,6 +8,7 @@ use log::warn; use wgpu::{Device, Queue, include_wgsl, util::DeviceExt}; use super::captions::{CaptionOverlayLayout, CaptionPosition}; +use super::keycaps::{self, Keycap, KeycapKind, KeycapStyle, KeycapTheme}; use crate::{DecodedSegmentFrames, ProjectUniforms, RenderVideoConstants, parse_color_component}; #[repr(C)] @@ -20,6 +21,48 @@ struct KeyboardBackgroundUniforms { _padding2: [f32; 4], } +/// One key for `shaders/keycap.wgsl`. +#[repr(C)] +#[derive(Copy, Clone, Pod, Zeroable, Debug, Default)] +struct KeycapUniform { + rect: [f32; 4], + top_a: [f32; 4], + top_b: [f32; 4], + side_a: [f32; 4], + side_b: [f32; 4], + border: [f32; 4], + highlight: [f32; 4], + shape: [f32; 4], + finish: [f32; 4], + motion: [f32; 4], +} + +const MAX_KEYCAPS: usize = 16; +/// The label slots for typed text's settled letters and its newest letter +/// while that one rises in, past every row's own slots. +const RISING_SLOT: usize = MAX_KEYCAPS * 6; +/// How long a typed letter takes to rise into its key. +const LETTER_RISE: f64 = 0.16; + +#[repr(C)] +#[derive(Copy, Clone, Pod, Zeroable, Debug)] +struct KeycapUniforms { + caps: [KeycapUniform; MAX_KEYCAPS], + info: [f32; 4], +} + +/// A key label placed for glyphon: which shaped buffer, where, in what colour. +struct PlacedLabel { + slot: usize, + left: f32, + top: f32, + width: f32, + height: f32, + color: Color, + /// How far the label travels while the shutter is open. + travel: [f32; 2], +} + #[derive(Debug, Clone, Copy, PartialEq)] pub enum KeyboardPosition { AboveCaptions, @@ -29,6 +72,10 @@ pub enum KeyboardPosition { BottomLeft, BottomCenter, BottomRight, + /// The middle of the frame, for a video that is all about its keys. + MiddleLeft, + Center, + MiddleRight, } impl KeyboardPosition { @@ -40,6 +87,9 @@ impl KeyboardPosition { "bottom-left" => Self::BottomLeft, "bottom-right" => Self::BottomRight, "bottom-center" | "bottom" => Self::BottomCenter, + "middle-left" => Self::MiddleLeft, + "center" | "middle-center" => Self::Center, + "middle-right" => Self::MiddleRight, _ => Self::AboveCaptions, } } @@ -48,6 +98,7 @@ impl KeyboardPosition { match self { Self::TopLeft | Self::TopCenter | Self::TopRight => 0.08, Self::AboveCaptions => 0.75, + Self::MiddleLeft | Self::Center | Self::MiddleRight => 0.5, Self::BottomLeft | Self::BottomCenter | Self::BottomRight => 0.85, } } @@ -56,6 +107,16 @@ impl KeyboardPosition { const BOUNCE_OFFSET_PIXELS: f32 = 6.0; const MIN_OVERLAY_GAP: f32 = 15.0; +/// A keycap is this many times the size setting tall, at 1080p: the default +/// size of 50 makes a key a tenth of the frame's height. +const KEYCAP_HEIGHT_PER_SIZE: f32 = 2.15; + +/// The keycaps stand a little apart from the captions, more on a taller frame, +/// so their shadows never touch the caption card. +fn keycap_caption_gap(frame_height: f32) -> f32 { + (frame_height * 0.035).max(MIN_OVERLAY_GAP) +} + /// Manual captions can sit anywhere, so they share the keyboard's lane /// wherever the two boxes would meet side by side. fn shares_caption_lane( @@ -109,12 +170,14 @@ fn resolve_keyboard_position( } } -/// `keyboard` is the box at its own position, `[left, top, width, height]`. +/// `keyboard` is the box at its own position, `[left, top, width, height]`, +/// kept `gap` pixels clear of the captions. fn resolve_background_top( keyboard: [f32; 4], frame_height: f32, keyboard_position: KeyboardPosition, caption_layout: Option, + gap: f32, ) -> f32 { let [left, current_top, width, box_height] = keyboard; let Some(caption_layout) = caption_layout else { @@ -130,14 +193,12 @@ fn resolve_background_top( let caption_bottom = caption_layout.rect[1] + caption_layout.rect[3]; let current_bottom = current_top + box_height; - if current_bottom + MIN_OVERLAY_GAP <= caption_top - || current_top >= caption_bottom + MIN_OVERLAY_GAP - { + if current_bottom + gap <= caption_top || current_top >= caption_bottom + gap { return current_top.clamp(0.0, max_top); } - let above_candidate = caption_top - MIN_OVERLAY_GAP - box_height; - let below_candidate = caption_bottom + MIN_OVERLAY_GAP; + let above_candidate = caption_top - gap - box_height; + let below_candidate = caption_bottom + gap; let above_valid = above_candidate >= 0.0; let below_valid = below_candidate <= max_top; @@ -183,6 +244,13 @@ pub struct KeyboardLayer { background_scissor: Option<[u32; 4]>, output_size: (u32, u32), has_content: bool, + keycap_pipeline: wgpu::RenderPipeline, + keycap_bind_group: wgpu::BindGroup, + keycap_uniform_buffer: wgpu::Buffer, + /// Shaped key labels, kept between frames while their text holds. + keycap_buffers: Vec>, + keycap_labels: Vec, + keycap_mode: bool, } impl KeyboardLayer { @@ -282,6 +350,9 @@ impl KeyboardLayer { cache: None, }); + let (keycap_pipeline, keycap_bind_group, keycap_uniform_buffer) = + create_keycap_pipeline(device); + Self { font_system, swash_cache, @@ -296,6 +367,12 @@ impl KeyboardLayer { background_scissor: None, output_size: (0, 0), has_content: false, + keycap_pipeline, + keycap_bind_group, + keycap_uniform_buffer, + keycap_buffers: Vec::new(), + keycap_labels: Vec::new(), + keycap_mode: false, } } @@ -308,6 +385,7 @@ impl KeyboardLayer { caption_layout: Option, ) { self.has_content = false; + self.keycap_mode = false; self.background_scissor = None; self.output_size = (output_size.x, output_size.y); @@ -426,19 +504,120 @@ impl KeyboardLayer { }; let wrap_width = (width as f32 - margin * 2.0).max(font_size); - let font_family = match settings.font.as_str() { - "System Serif" => Family::Serif, - "System Monospace" => Family::Monospace, - _ => Family::SansSerif, - }; + let (font_family, weight) = + super::captions::overlay_face(&settings.font, settings.font_weight); - let weight = if settings.font_weight >= 700 { - Weight::BOLD - } else if settings.font_weight >= 500 { - Weight::MEDIUM - } else { - Weight::NORMAL - }; + // Keys are drawn for the platform the recording was made on, so a + // project looks the same wherever it is exported. A key coloured on + // its own keeps an older project on its styled label. + let platform = constants.recording_meta.platform.as_ref(); + let styled_keys = settings.style == "auto" + && timeline.keyboard_segments.iter().any(|segment| { + segment.color_override.is_some() || segment.background_color_override.is_some() + }); + let style = KeycapStyle::from_setting_for( + settings.effective_style_for(platform, styled_keys), + platform, + ); + if style != KeycapStyle::Classic { + let case = |text: String| { + if should_uppercase { + text.to_uppercase() + } else { + text + } + }; + let pace = keycap_pace(segment_fade); + let struck = last_struck_at(active.segment, current_time); + // The keys animate from the first key press, not from the + // segment's lead-in. + let first_key = active + .segment + .keys + .iter() + .map(|key| key.time_offset) + .reduce(f64::min) + .map_or(active.segment.start, |offset| { + active.segment.start + offset / 1000.0 + }); + let since_press = current_time - first_key; + // With typing shown, what is not a shortcut is typed text, from + // its first letter on. + let typed = |segment: &cap_project::KeyboardTrackSegment| { + // Text typed into the composer is typing, shown or not; a + // space struck on its own there is the space bar. + let manual = cap_project::is_manual_keyboard_segment(segment); + (settings.show_typing || manual) + && !is_shortcut(segment) + && !(manual && segment.display_text == "␣") + }; + // What the keys showed just before the latest key went down, when + // that key changed them (a typed letter, a key added). + let before_strike = (struck > first_key + 1e-6) + .then(|| case(build_visible_text(active.segment, struck - 1e-4))) + .filter(|before| *before != visible_text); + // The same shortcut struck again changes nothing on screen: the + // keys dip once for it instead. + let repeat = + before_strike.is_none() && is_shortcut(active.segment) && struck > first_key + 1e-6; + // Typed into the composer, each letter rises into the key as it + // is struck, and the key dips for it. + let typing = cap_project::is_manual_keyboard_segment(active.segment) + && typed(active.segment) + && before_strike.is_some(); + let change = match before_strike { + Some(before) if since_press >= (KEYCAP_MOVE + KEYCAP_ENTER) * pace => KeyChange { + from: Some((before, typed(active.segment))), + by_identity: false, + since: current_time - struck, + }, + // Keys appearing, or taking over from the keys before them as + // those last stood. + _ => KeyChange { + from: handed_from(active.segment, &timeline.keyboard_segments, settings) + .map(|previous| { + ( + case(build_visible_text(previous, active.segment.start - 1e-4)), + typed(previous), + ) + }) + .filter(|(text, _)| !text.is_empty()), + by_identity: true, + since: since_press, + }, + }; + // Keys that hand straight over to the next keys stay up through + // the gap; the rest leave once their segment ends. + let leaving = (current_time >= active.segment.end + && !handed_over(active.segment, &timeline.keyboard_segments, segment_fade)) + .then_some(current_time - active.segment.end); + self.prepare_keycaps( + KeycapFrame { + text: &visible_text, + typed: typed(active.segment), + style, + theme: KeycapTheme::from_setting(&settings.theme), + font_size, + opacity: uniforms.takeover_overlay_fade(), + change, + leaving, + pace, + fade: segment_fade, + since_struck: if repeat || typing { + current_time - struck + } else { + f64::INFINITY + }, + fresh: typing.then_some(current_time - struck), + position, + margin, + }, + (width, height), + caption_layout, + constants, + ); + return; + } let text_alpha = fade_opacity.clamp(0.0, 1.0); let color = Color::rgba( @@ -492,10 +671,12 @@ impl KeyboardLayer { let resolved_position = resolve_keyboard_position(position, caption_layout); let background_left = match resolved_position { - KeyboardPosition::TopLeft | KeyboardPosition::BottomLeft => margin, - KeyboardPosition::TopRight | KeyboardPosition::BottomRight => { - (width as f32 - margin - box_width).max(0.0) - } + KeyboardPosition::TopLeft + | KeyboardPosition::MiddleLeft + | KeyboardPosition::BottomLeft => margin, + KeyboardPosition::TopRight + | KeyboardPosition::MiddleRight + | KeyboardPosition::BottomRight => (width as f32 - margin - box_width).max(0.0), _ => ((width as f32 - box_width) / 2.0).max(0.0), }; @@ -512,6 +693,7 @@ impl KeyboardLayer { height as f32, position, caption_layout, + MIN_OVERLAY_GAP, ); let text_left = background_left + padding; @@ -611,6 +793,25 @@ impl KeyboardLayer { return; } + if self.keycap_mode { + if let Some([x, y, width, height]) = self.background_scissor { + pass.set_scissor_rect(x, y, width, height); + pass.set_pipeline(&self.keycap_pipeline); + pass.set_bind_group(0, &self.keycap_bind_group, &[]); + pass.draw(0..6, 0..1); + } + if let Err(e) = self + .text_renderer + .render(&self.text_atlas, &self.viewport, pass) + { + warn!("Error rendering keycap labels: {e:?}"); + } + if self.output_size.0 > 0 && self.output_size.1 > 0 { + pass.set_scissor_rect(0, 0, self.output_size.0, self.output_size.1); + } + return; + } + if let Some([x, y, width, height]) = self.background_scissor { pass.set_scissor_rect(x, y, width, height); pass.set_pipeline(&self.background_pipeline); @@ -635,10 +836,872 @@ impl KeyboardLayer { } } +/// A shaped label's set width and height, and where its baseline sits below +/// the top of its line box. +#[derive(Debug, Clone, Copy, Default)] +struct LabelMetrics { + width: f32, + height: f32, + baseline: f32, + size: f32, +} + +/// What one frame of keycaps needs from the segment and settings. +struct KeycapFrame<'a> { + text: &'a str, + /// Whether `text` was typed rather than a shortcut struck. + typed: bool, + style: KeycapStyle, + theme: KeycapTheme, + font_size: f32, + opacity: f32, + /// How the keys came to be what they are, which the frame animates out + /// of. + change: KeyChange, + /// Seconds since the keys started leaving, while they leave. + leaving: Option, + /// How long the animations run, 1 at the default fade. + pace: f64, + fade: f64, + /// Seconds since a repeat of the same shortcut went down, or the newest + /// letter of a word being typed. + since_struck: f64, + /// Seconds since the last letter of typed text was struck, while it is + /// typing: that letter rises into the key. + fresh: Option, + position: KeyboardPosition, + margin: f32, +} + +/// A key drawn this frame: the key, its shaped label, corner word and width, +/// its first label slot, where it is, and how far it travels while the +/// shutter is open. +type DrawnKey<'a> = ( + &'a Keycap, + &'a (LabelMetrics, LabelMetrics, f32), + usize, + KeyMotion, + [f32; 2], +); + +/// The keys before the latest change and how long ago it was. +struct KeyChange { + /// The keys' text before, or none when they appeared from nothing, and + /// whether it was typed. + from: Option<(String, bool)>, + /// Keys carry over when they are the same key (a shortcut handing over + /// to the next); otherwise by their place in the row (typed text + /// growing). + by_identity: bool, + since: f64, +} + +/// Where one key is drawn this frame: its place, width, how far below its +/// rest it sits, and how opaque it is. +#[derive(Clone, Copy)] +struct KeyMotion { + x: f32, + width: f32, + drop: f32, + opacity: f32, +} + +// The keys' motion at the default fade, in seconds, timed from the key +// press. Keys that appear fade in where they rest, a beat after the key +// before them; keys that carry over glide to their new places first, and +// the new keys follow as they settle; keys that go fade out where they are. +const KEYCAP_ENTER: f64 = 0.18; +const KEYCAP_STAGGER: f64 = 0.065; +const KEYCAP_RISE: f64 = 0.2; +const KEYCAP_MOVE: f64 = 0.09; +const KEYCAP_AFTER_MOVE: f64 = 0.07; +const KEYCAP_GROW: f64 = 0.08; +const KEYCAP_EXIT: f64 = 0.12; +/// How long the virtual shutter is open: a moving key smears over the +/// distance it covers in this time, as a camera would catch it. +const KEYCAP_SHUTTER: f64 = 0.022; + +fn ease_out_cubic(t: f64) -> f64 { + let t = t.clamp(0.0, 1.0); + 1.0 - (1.0 - t).powi(3) +} + +fn ease_out_quint(t: f64) -> f64 { + let t = t.clamp(0.0, 1.0); + 1.0 - (1.0 - t).powi(5) +} + +fn ease_in_out_cubic(t: f64) -> f64 { + let t = t.clamp(0.0, 1.0); + if t < 0.5 { + 4.0 * t * t * t + } else { + 1.0 - (-2.0 * t + 2.0).powi(3) / 2.0 + } +} + +/// How far along a motion `duration` long is after `elapsed`, with no +/// motion at all when there is no time for one. +fn progress(elapsed: f64, duration: f64) -> f64 { + if duration <= 0.0 { + 1.0 + } else { + (elapsed / duration).clamp(0.0, 1.0) + } +} + +/// The animations scale with the fade setting, so a shorter fade makes for +/// snappier keys and none for none. +fn keycap_pace(fade: f64) -> f64 { + (fade / 0.15).clamp(0.0, 4.0) +} + +/// Pairs each key in `to` with the key it was in `from`, if any. +fn match_keys(from: &[Keycap], to: &[Keycap], by_identity: bool) -> Vec> { + let mut used = vec![false; from.len()]; + to.iter() + .enumerate() + .map(|(index, cap)| { + let found = if by_identity { + // Typed text is one key whatever it says, so a new word + // reuses the key the last one was on. + from.iter().enumerate().position(|(at, old)| { + !used[at] + && (old == cap + || (old.kind == KeycapKind::Wide && cap.kind == KeycapKind::Wide)) + }) + } else { + from.get(index) + .filter(|old| old.kind == cap.kind && !used[index]) + .map(|_| index) + }; + if let Some(at) = found { + used[at] = true; + } + found + }) + .collect() +} + +/// Where every key is drawn `change.since` after a change: those in `to` +/// (at rest at `to_x`), then the ones from `from` that have gone (at rest at +/// `from_x`). +fn key_motions( + to: (&[Keycap], &[(f32, f32)]), + from: (&[Keycap], &[(f32, f32)]), + change: &KeyChange, + pace: f64, + cap_height: f32, +) -> (Vec, Vec<(usize, KeyMotion)>) { + let matches = match_keys(from.0, to.0, change.by_identity); + let carry = if change.by_identity { + ease_in_out_cubic(progress(change.since, KEYCAP_MOVE * pace)) + } else { + ease_out_cubic(progress(change.since, KEYCAP_GROW * pace)) + } as f32; + // New keys wait for the keys carrying over to make room, or for the + // keys they replace to have mostly gone, so no two keys share a place. + let delay = if matches.iter().any(Option::is_some) { + KEYCAP_AFTER_MOVE * pace + } else if !from.0.is_empty() { + KEYCAP_EXIT * 0.7 * pace + } else { + 0.0 + }; + let mut entering = 0; + let current = + to.1.iter() + .zip(&matches) + .map(|(&(x, width), found)| match found { + Some(at) => { + let (old_x, old_width) = from.1[*at]; + KeyMotion { + x: old_x + (x - old_x) * carry, + width: old_width + (width - old_width) * carry, + drop: 0.0, + opacity: 1.0, + } + } + None => { + let local = change.since - delay - entering as f64 * KEYCAP_STAGGER * pace; + entering += 1; + KeyMotion { + x, + width, + drop: (1.0 - ease_out_quint(progress(local, KEYCAP_RISE * pace))) as f32 + * cap_height + * 0.1, + opacity: ease_out_cubic(progress(local, KEYCAP_ENTER * pace)) as f32, + } + } + }) + .collect(); + let gone = ease_out_cubic(progress(change.since, KEYCAP_EXIT * pace)) as f32; + let leaving = from + .1 + .iter() + .enumerate() + .filter(|(at, _)| !matches.contains(&Some(*at))) + .filter(|_| gone < 1.0) + .map(|(at, &(x, width))| { + ( + at, + KeyMotion { + x, + width, + drop: gone * cap_height * 0.06, + opacity: 1.0 - gone, + }, + ) + }) + .collect(); + (current, leaving) +} + +impl KeyboardLayer { + /// Shapes `text` into the buffer at `slot`, reusing it while the text + /// holds, and returns how wide and tall the set text is. + fn shape_keycap_label( + &mut self, + slot: usize, + text: &str, + size: f32, + family: Family<'static>, + weight: Weight, + ) -> LabelMetrics { + if self.keycap_buffers.len() <= slot { + self.keycap_buffers.resize_with(slot + 1, || None); + } + let shaped = ShapedKeyboardText { + text: text.to_string(), + font_size: size.to_bits(), + wrap_width: 0, + family, + weight, + }; + let fresh = !matches!(&self.keycap_buffers[slot], Some((held, _)) if *held == shaped); + if fresh { + let metrics = Metrics::new(size, size * 1.2); + let mut buffer = Buffer::new(&mut self.font_system, metrics); + buffer.set_size(&mut self.font_system, None, None); + // Key symbols are printed in the one face that draws them all + // alike; letters and words in the key's own face. + let attrs = Attrs::new().family(family).weight(weight); + let symbol_attrs = Attrs::new() + .family(Family::Name(keycaps::KEY_SYMBOL_FONT)) + .weight(Weight(500)); + buffer.set_rich_text( + &mut self.font_system, + keycaps::symbol_runs(text).into_iter().map(|(run, symbol)| { + ( + run, + if symbol { + symbol_attrs.clone() + } else { + attrs.clone() + }, + ) + }), + &attrs, + Shaping::Advanced, + None, + ); + buffer.shape_until_scroll(&mut self.font_system, false); + self.keycap_buffers[slot] = Some((shaped, buffer)); + } + let Some((_, buffer)) = &self.keycap_buffers[slot] else { + return LabelMetrics::default(); + }; + let mut metrics = LabelMetrics { + size, + ..LabelMetrics::default() + }; + for (index, run) in glyphon::cosmic_text::LayoutRunIter::new(buffer).enumerate() { + metrics.width = metrics.width.max(run.line_w); + metrics.height = metrics.height.max(run.line_top + run.line_height); + if index == 0 { + metrics.baseline = run.line_y; + } + } + metrics.height = metrics.height.max(size * 1.2); + if metrics.baseline <= 0. { + metrics.baseline = size; + } + metrics + } + + /// Shapes a row of keys at `cap_height` into the label slots from + /// `slot_base`, and returns each key's label, corner word and width. + fn layout_keycap_row( + &mut self, + caps: &[Keycap], + cap_height: f32, + style: KeycapStyle, + (family, weight): (Family<'static>, Weight), + slot_base: usize, + ) -> Vec<(LabelMetrics, LabelMetrics, f32)> { + caps.iter() + .enumerate() + .map(|(index, cap)| { + let label = self.shape_keycap_label( + slot_base + index * 2, + &cap.label, + keycaps::label_size(cap, cap_height, style), + family, + weight, + ); + let legend = match &cap.legend { + Some(legend) => self.shape_keycap_label( + slot_base + index * 2 + 1, + legend, + keycaps::legend_size(cap_height), + family, + weight, + ), + None => LabelMetrics::default(), + }; + let cap_width = keycaps::cap_width(cap, cap_height, label.width, legend.width); + (label, legend, cap_width) + }) + .collect() + } + + fn prepare_keycaps( + &mut self, + frame: KeycapFrame<'_>, + (width, height): (u32, u32), + caption_layout: Option, + constants: &RenderVideoConstants, + ) { + // The newest keys are the ones being struck: keep those. + let newest = |mut caps: Vec| { + if caps.len() > MAX_KEYCAPS { + caps.drain(..caps.len() - MAX_KEYCAPS); + } + caps + }; + let keys_for = |text: &str, typed: bool| { + newest(if typed { + keycaps::typed_keycaps(text, frame.style) + } else { + keycaps::keycaps_for(text, frame.style) + }) + }; + let caps = keys_for(frame.text, frame.typed); + if caps.is_empty() { + return; + } + let from_caps = frame + .change + .from + .as_ref() + .map(|(text, typed)| keys_for(text, *typed)) + .unwrap_or_default(); + let available = (width as f32 - frame.margin * 2.0).max(1.0); + let geometry = frame.style.geometry(); + let (face, face_weight) = keycaps::keycap_font(frame.style, frame.theme); + let face = (Family::Name(face), Weight(face_weight)); + + // Lay the keys out at the set size; a row too wide for the frame is + // set again, smaller, so every key stays on screen. + let row_width = |sizes: &[(LabelMetrics, LabelMetrics, f32)], cap_height: f32| { + sizes.iter().map(|size| size.2).sum::() + + keycaps::cap_gap(cap_height) * sizes.len().saturating_sub(1) as f32 + }; + // The row is measured at full size in slots of its own, so a row + // that has to shrink keeps both its measured and its drawn labels + // shaped from one frame to the next. + let mut cap_height = frame.font_size * KEYCAP_HEIGHT_PER_SIZE; + let measured = + self.layout_keycap_row(&caps, cap_height, frame.style, face, MAX_KEYCAPS * 4); + let total = row_width(&measured, cap_height); + if total > available { + cap_height *= available / total; + } + let sizes = self.layout_keycap_row(&caps, cap_height, frame.style, face, 0); + let from_sizes = if from_caps.is_empty() { + Vec::new() + } else { + self.layout_keycap_row(&from_caps, cap_height, frame.style, face, MAX_KEYCAPS * 2) + }; + + let radius = geometry.radius * cap_height; + let depth = (geometry.depth * cap_height).max(2.0); + let inset = geometry.inset * cap_height; + let gap = keycaps::cap_gap(cap_height); + let resolved_position = resolve_keyboard_position(frame.position, caption_layout); + // Each row at rest: every key's left edge and width, squeezed to fit. + let place_row = |sizes: &[(LabelMetrics, LabelMetrics, f32)]| { + let total = row_width(sizes, cap_height); + let squeeze = (available / total.max(1.0)).min(1.0); + let total = total * squeeze; + let left = match resolved_position { + KeyboardPosition::TopLeft + | KeyboardPosition::MiddleLeft + | KeyboardPosition::BottomLeft => frame.margin, + KeyboardPosition::TopRight + | KeyboardPosition::MiddleRight + | KeyboardPosition::BottomRight => (width as f32 - frame.margin - total).max(0.0), + _ => ((width as f32 - total) / 2.0).max(0.0), + }; + let mut x = left; + let placed: Vec<(f32, f32)> = sizes + .iter() + .map(|size| { + let at = (x, size.2 * squeeze); + x += (size.2 + gap) * squeeze; + at + }) + .collect(); + (left, total, placed) + }; + let (left, total, rest) = place_row(&sizes); + let (_, _, from_rest) = place_row(&from_sizes); + let center_y = height as f32 * resolved_position.y_factor(); + let max_top = (height as f32 - cap_height).max(0.0); + let base_top = (center_y - cap_height / 2.0).clamp(0.0, max_top); + let top = resolve_background_top( + [left, base_top, total, cap_height], + height as f32, + frame.position, + caption_layout, + keycap_caption_gap(height as f32), + ); + + // Where every key is `before` seconds ago, so how far it travels + // while the shutter is open. + let motions_at = |before: f64| { + let (mut current, leaving_from) = key_motions( + (&caps, &rest), + (&from_caps, &from_rest), + &KeyChange { + from: None, + by_identity: frame.change.by_identity, + since: frame.change.since - before, + }, + frame.pace, + cap_height, + ); + // Leaving for good: every key fades as it settles a little + // lower. + if let Some(elapsed) = frame.leaving { + let out = ease_in_out_cubic(progress(elapsed - before, frame.fade)) as f32; + for motion in &mut current { + motion.opacity *= 1.0 - out; + motion.drop += out * cap_height * 0.08; + } + } + (current, leaving_from) + }; + let (current, leaving_from) = motions_at(0.0); + let (earlier, earlier_leaving) = motions_at(KEYCAP_SHUTTER * frame.pace.min(1.0)); + let travel = |now: &KeyMotion, then: Option<&KeyMotion>| { + then.map_or([0.0, 0.0], |then| [now.x - then.x, now.drop - then.drop]) + }; + let current: Vec<(KeyMotion, [f32; 2])> = current + .iter() + .zip(&earlier) + .map(|(now, then)| (*now, travel(now, Some(then)))) + .collect(); + let leaving_from: Vec<(usize, KeyMotion, [f32; 2])> = leaving_from + .iter() + .map(|(at, now)| { + let then = earlier_leaving + .iter() + .find(|(earlier_at, _)| earlier_at == at) + .map(|(_, motion)| motion); + (*at, *now, travel(now, then)) + }) + .collect(); + // Keys going are drawn first, under the keys arriving. + let mut drawn: Vec = leaving_from + .iter() + .map(|&(at, motion, travel)| { + ( + &from_caps[at], + &from_sizes[at], + MAX_KEYCAPS * 2 + at * 2, + motion, + travel, + ) + }) + .collect(); + drawn.truncate(MAX_KEYCAPS - caps.len()); + drawn.extend(caps.iter().zip(&sizes).zip(¤t).enumerate().map( + |(index, ((cap, size), (motion, travel)))| (cap, size, index * 2, *motion, *travel), + )); + + let press = keycaps::press_depth(frame.since_struck); + let mut uniforms = KeycapUniforms { + caps: [KeycapUniform::default(); MAX_KEYCAPS], + info: [drawn.len() as f32, 1.0, 0.0, 0.0], + }; + self.keycap_labels.clear(); + let pad = cap_height * 0.13; + let (mut reach_left, mut reach_right) = (f32::INFINITY, f32::NEG_INFINITY); + let mut reach_bottom = top + cap_height; + for (index, (cap, (label, legend, _), slot, motion, travel)) in drawn.iter().enumerate() { + // While text types out, only the key it is typing into dips. + let press = if frame.fresh.is_some() && index + 1 != drawn.len() { + 0.0 + } else { + press + }; + let opacity = (motion.opacity * frame.opacity).clamp(0.0, 1.0); + let x = motion.x; + let cap_top = top + motion.drop; + let cap_width = motion.width; + reach_left = reach_left.min(x); + reach_right = reach_right.max(x + cap_width); + reach_bottom = reach_bottom.max(cap_top + cap_height); + let palette = frame.theme.palette_for(frame.style, cap); + uniforms.info[2] = uniforms.info[2].max(palette.shadow); + uniforms.caps[index] = KeycapUniform { + rect: [x, cap_top, cap_width, cap_height], + top_a: palette.top[0], + top_b: palette.top[1], + side_a: palette.side[0], + side_b: palette.side[1], + border: palette.border, + highlight: palette.highlight, + shape: [radius, depth, inset, press], + finish: [geometry.dish, palette.grain, palette.gloss, opacity], + motion: [travel[0], travel[1], geometry.sculpt, 0.0], + }; + let sink = depth * press; + let face_x = x + inset; + let face_y = cap_top + sink + inset * 0.4; + let face_w = cap_width - inset * 2.0; + let face_h = cap_height - depth - inset * 0.85; + let color = |c: [f32; 4]| { + Color::rgba( + (c[0] * 255.0) as u8, + (c[1] * 255.0) as u8, + (c[2] * 255.0) as u8, + (c[3] * opacity * 255.0) as u8, + ) + }; + // Labels are placed by their ink, not their line box: the + // baseline sits so the capitals (or running text) centre on the + // face, or, for Apple's modifiers, hug the corners as printed. + let place = |metrics: &LabelMetrics, left: f32, ink_top: f32, ink: f32| { + (left, ink_top + metrics.size * ink - metrics.baseline) + }; + if cap.legend.is_some() { + // Apple's keys print towards the space bar: the symbol in + // the top corner, the word in the bottom one, same side. + let right = cap.align == keycaps::LegendAlign::Right; + let along = |width: f32| { + if right { + face_x + face_w - pad - width + } else { + face_x + pad + } + }; + if !cap.label.is_empty() { + let (left, top) = place(label, along(label.width), face_y + pad * 0.75, 0.71); + self.keycap_labels.push(PlacedLabel { + slot: *slot, + left, + top, + width: label.width, + height: label.height, + color: color(palette.label), + travel: *travel, + }); + } + let legend_ink = legend.size * 0.71; + let (left, top) = place( + legend, + along(legend.width), + face_y + face_h - pad * 0.7 - legend_ink, + 0.71, + ); + self.keycap_labels.push(PlacedLabel { + slot: *slot + 1, + left, + top, + width: legend.width, + height: legend.height, + color: color(palette.legend), + travel: *travel, + }); + } else if !cap.label.is_empty() { + let ink = keycaps::ink_height(cap); + let (left, top) = place( + label, + face_x + (face_w - label.width) / 2.0, + face_y + (face_h - label.size * ink) / 2.0, + ink, + ); + // While text types out, its newest letter rises into place + // beside the ones already set, fading in as it comes. + let rising = frame.fresh.filter(|age| { + *age < LETTER_RISE && index + 1 == drawn.len() && cap.kind == KeycapKind::Wide + }); + if let Some(age) = rising { + let split = cap.label.char_indices().last().map_or(0, |(at, _)| at); + let (settled, newest) = cap.label.split_at(split); + let settled_width = if settled.is_empty() { + 0.0 + } else { + let metrics = self.shape_keycap_label( + RISING_SLOT, + settled, + label.size, + face.0, + face.1, + ); + self.keycap_labels.push(PlacedLabel { + slot: RISING_SLOT, + left, + top, + width: metrics.width, + height: metrics.height, + color: color(palette.label), + travel: *travel, + }); + metrics.width + }; + let newest_metrics = self.shape_keycap_label( + RISING_SLOT + 1, + newest, + label.size, + face.0, + face.1, + ); + let rise = |age: f64| { + (1.0 - ease_out_cubic(progress(age, LETTER_RISE))) as f32 + * label.size + * 0.45 + }; + let shown = ease_out_cubic(progress(age, LETTER_RISE * 0.7)) as f32; + let mut ink_color = palette.label; + ink_color[3] *= shown; + let shutter = KEYCAP_SHUTTER * frame.pace.min(1.0); + self.keycap_labels.push(PlacedLabel { + slot: RISING_SLOT + 1, + left: left + settled_width, + top: top + rise(age), + width: newest_metrics.width, + height: newest_metrics.height, + color: color(ink_color), + travel: [travel[0], travel[1] + rise(age) - rise(age - shutter)], + }); + } else { + self.keycap_labels.push(PlacedLabel { + slot: *slot, + left, + top, + width: label.width, + height: label.height, + color: color(palette.label), + travel: *travel, + }); + } + } + } + + let device = &constants.device; + let queue = &constants.queue; + queue.write_buffer( + &self.keycap_uniform_buffer, + 0, + bytemuck::bytes_of(&uniforms), + ); + self.viewport.update(queue, Resolution { width, height }); + + // A moving label is printed along its path at a share of its + // opacity each time, smearing it like the key under it. + let text_areas: Vec