Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

feat(desktop-gpui): copy and paste text segment styles from the timeline menu

chrvasile481cfa002c09
diff --git a/apps/desktop-gpui/assets/icons/clipboard-paste.svg b/apps/desktop-gpui/assets/icons/clipboard-paste.svg
new file mode 100644
index 000000000..312524c27
--- /dev/null
+++ b/apps/desktop-gpui/assets/icons/clipboard-paste.svg
@@ -0,0 +1 @@
+<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-clipboard-paste"><path d="M11 14h10"/><path d="M16 4h2a2 2 0 0 1 2 2v1.344"/><path d="m17 18 4-4-4-4"/><path d="M8 4H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h8"/><rect x="8" y="2" width="8" height="4" rx="1"/></svg>
diff --git a/apps/desktop-gpui/src/assets.rs b/apps/desktop-gpui/src/assets.rs
index be8bf1b32..638020d00 100644
--- a/apps/desktop-gpui/src/assets.rs
+++ b/apps/desktop-gpui/src/assets.rs
@@ -182,6 +182,7 @@ const ICONS: &[(&str, &[u8])] = assets!("icons":
"import.svg",
"edit.svg",
"copy.svg",
+ "clipboard-paste.svg",
"record-fill.svg",
"warning-bold.svg",
"traffic-close.svg",
diff --git a/apps/desktop-gpui/src/editor_panels.rs b/apps/desktop-gpui/src/editor_panels.rs
index 4ad482e30..fbe65f6c9 100644
--- a/apps/desktop-gpui/src/editor_panels.rs
+++ b/apps/desktop-gpui/src/editor_panels.rs
@@ -668,14 +668,102 @@ pub fn pick_font_family(stack: &[&str], installed: &[String]) -> String {
.map_or_else(|| "sans-serif".to_string(), |family| (*family).to_string())
}
-/// `applyTextPreset` (`text-presets.ts:205-238`). Timing stays the user's, and
-/// so does the content unless it is still the placeholder a new segment is
-/// born with; the box is scaled with the font change about its **top** edge,
-/// exactly as the Size slider does.
-pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, installed: &[String]) {
- let style = &preset.style;
+/// The look of one text segment, lifted off it by "Copy Style" on the
+/// timeline. Content, timing, lane, position and layout stay with the target
+/// when it is pasted; the box is rescaled with the font size the same way a
+/// preset does it.
+#[derive(Debug, Clone, PartialEq)]
+pub struct TextStyle {
+ pub font_family: String,
+ pub font_size: f32,
+ pub font_weight: f32,
+ pub italic: bool,
+ pub uppercase: bool,
+ pub align: TextAlign,
+ pub letter_spacing: f32,
+ pub line_height: f32,
+ pub opacity: f32,
+ pub shadow: f32,
+ pub glow: f32,
+ pub stroke_width: f32,
+ pub stroke_color: String,
+ pub background_style: TextBackgroundStyle,
+ pub background_color: Option<String>,
+ pub color: String,
+ pub gradient_color: Option<String>,
+ pub animation_in: TextAnimation,
+ pub animation_out: TextAnimation,
+ pub animation_in_duration: f64,
+ pub animation_out_duration: f64,
+ pub fade_duration: f64,
+}
+
+impl TextStyle {
+ pub fn of(segment: &TextSegment) -> Self {
+ Self {
+ font_family: segment.font_family.clone(),
+ font_size: segment.font_size,
+ font_weight: segment.font_weight,
+ italic: segment.italic,
+ uppercase: segment.uppercase,
+ align: segment.align,
+ letter_spacing: segment.letter_spacing,
+ line_height: segment.line_height,
+ opacity: segment.opacity,
+ shadow: segment.shadow,
+ glow: segment.glow,
+ stroke_width: segment.stroke_width,
+ stroke_color: segment.stroke_color.clone(),
+ background_style: segment.background_style,
+ background_color: segment.background_color.clone(),
+ color: segment.color.clone(),
+ gradient_color: segment.gradient_color.clone(),
+ animation_in: segment.animation_in,
+ animation_out: segment.animation_out,
+ animation_in_duration: segment.animation_in_duration,
+ animation_out_duration: segment.animation_out_duration,
+ fade_duration: segment.fade_duration,
+ }
+ }
+
+ pub fn apply_to(&self, segment: &mut TextSegment) -> bool {
+ let previous_style = Self::of(segment);
+ let previous_center = segment.center;
+ let previous_size = segment.size;
+ scale_text_box(segment, self.font_size);
+ segment.font_family = self.font_family.clone();
+ segment.font_size = self.font_size;
+ segment.font_weight = self.font_weight;
+ segment.italic = self.italic;
+ segment.uppercase = self.uppercase;
+ segment.align = self.align;
+ segment.letter_spacing = self.letter_spacing;
+ segment.line_height = self.line_height;
+ segment.opacity = self.opacity;
+ segment.shadow = self.shadow;
+ segment.glow = self.glow;
+ segment.stroke_width = self.stroke_width;
+ segment.stroke_color = self.stroke_color.clone();
+ segment.background_style = self.background_style;
+ segment.background_color = self.background_color.clone();
+ segment.color = self.color.clone();
+ segment.gradient_color = self.gradient_color.clone();
+ segment.animation_in = self.animation_in;
+ segment.animation_out = self.animation_out;
+ segment.animation_in_duration = self.animation_in_duration;
+ segment.animation_out_duration = self.animation_out_duration;
+ segment.fade_duration = self.fade_duration;
+ previous_style != *self
+ || previous_center != segment.center
+ || previous_size != segment.size
+ }
+}
+
+/// Grows or shrinks the text box with a font-size change, top edge fixed,
+/// like the Size slider; the canvas overlay re-hugs the glyph bounds later.
+fn scale_text_box(segment: &mut TextSegment, font_size: f32) {
let box_scale = f64::from(
- style.font_size
+ font_size
/ if segment.font_size == 0. {
48.
} else {
@@ -686,6 +774,15 @@ pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, install
segment.size.x = (segment.size.x * box_scale).min(1.);
segment.size.y *= box_scale;
segment.center.y = top_edge + segment.size.y / 2.;
+}
+
+/// `applyTextPreset` (`text-presets.ts:205-238`). Timing stays the user's, and
+/// so does the content unless it is still the placeholder a new segment is
+/// born with; the box is scaled with the font change about its **top** edge,
+/// exactly as the Size slider does.
+pub fn apply_text_preset(segment: &mut TextSegment, preset: &TextPreset, installed: &[String]) {
+ let style = &preset.style;
+ scale_text_box(segment, style.font_size);
segment.font_family = pick_font_family(style.font_stack, installed);
segment.font_size = style.font_size;
@@ -7936,6 +8033,86 @@ impl EditorWindow {
mod tests {
use super::*;
+ #[test]
+ fn pasting_a_text_style_keeps_content_timing_position_and_layout() {
+ let mut source = crate::editor_edits::default_text_segment(0., 2., 0);
+ source.content = "Hello".into();
+ source.font_family = "Georgia".into();
+ source.font_size = 96.;
+ source.font_weight = 400.;
+ source.italic = true;
+ source.uppercase = true;
+ source.letter_spacing = 3.;
+ source.color = "#ff0000".into();
+ source.background_color = Some("#00000080".into());
+ source.gradient_color = Some("#00ff00".into());
+ source.animation_in = TextAnimation::Pop;
+ source.animation_out = TextAnimation::Wipe;
+ source.animation_in_duration = 0.4;
+ source.animation_out_duration = 0.6;
+ source.center = XY::new(0.2, 0.2);
+ source.layout = TextLayout::Fullscreen;
+
+ let mut target = crate::editor_edits::default_text_segment(5., 8., 1);
+ target.content = "World".into();
+ target.center = XY::new(0.7, 0.8);
+ target.size = XY::new(0.2, 0.1);
+
+ assert!(TextStyle::of(&source).apply_to(&mut target));
+
+ assert_eq!(target.content, "World");
+ assert_eq!(target.start, 5.);
+ assert_eq!(target.end, 8.);
+ assert_eq!(target.track, 1);
+ assert_eq!(target.layout, TextLayout::Overlay);
+ assert_eq!(target.center.x, 0.7);
+ assert_eq!(target.font_family, "Georgia");
+ assert_eq!(target.font_size, 96.);
+ assert!(target.italic && target.uppercase);
+ assert_eq!(target.letter_spacing, 3.);
+ assert_eq!(target.color, "#ff0000");
+ assert_eq!(target.background_color.as_deref(), Some("#00000080"));
+ assert_eq!(target.gradient_color.as_deref(), Some("#00ff00"));
+ assert_eq!(target.animation_in, TextAnimation::Pop);
+ assert_eq!(target.animation_out, TextAnimation::Wipe);
+ assert_eq!(target.animation_in_duration, 0.4);
+ assert_eq!(target.animation_out_duration, 0.6);
+ assert_eq!(TextStyle::of(&target), TextStyle::of(&source));
+ }
+
+ #[test]
+ fn pasting_a_text_style_scales_the_box_from_its_top_edge() {
+ let mut source = crate::editor_edits::default_text_segment(0., 2., 0);
+ source.font_size = 96.;
+ let mut target = crate::editor_edits::default_text_segment(0., 2., 0);
+ target.font_size = 48.;
+ target.center = XY::new(0.5, 0.5);
+ target.size = XY::new(0.2, 0.1);
+
+ assert!(TextStyle::of(&source).apply_to(&mut target));
+
+ assert!((target.size.x - 0.4).abs() < 1e-9);
+ assert!((target.size.y - 0.2).abs() < 1e-9);
+ assert!((target.center.y - 0.55).abs() < 1e-9);
+ assert_eq!(target.center.x, 0.5);
+ }
+
+ #[test]
+ fn pasting_a_text_style_onto_its_own_source_changes_nothing() {
+ let mut segment = crate::editor_edits::default_text_segment(0., 2., 0);
+ segment.color = "#ff0000".into();
+ segment.center = XY::new(0.3, 0.6);
+ segment.size = XY::new(0.25, 0.08);
+ let style = TextStyle::of(&segment);
+ let before = segment.clone();
+
+ assert!(!style.apply_to(&mut segment));
+
+ assert_eq!(TextStyle::of(&segment), style);
+ assert_eq!(segment.center, before.center);
+ assert_eq!(segment.size, before.size);
+ }
+
fn camera3d(start: f64, end: f64) -> Camera3DSegment {
Camera3DSegment {
start,
diff --git a/apps/desktop-gpui/src/editor_window.rs b/apps/desktop-gpui/src/editor_window.rs
index d19e1cd05..e65d3f1fc 100644
--- a/apps/desktop-gpui/src/editor_window.rs
+++ b/apps/desktop-gpui/src/editor_window.rs
@@ -1545,6 +1545,8 @@ pub struct EditorWindow {
preview_quality: crate::store::EditorPreviewQuality,
pub(crate) tracks: TrackLanes,
toolbar_menu: Option<OpenToolbarMenu>,
+ text_menu: Option<OpenTextMenu>,
+ text_style_clipboard: Option<crate::editor_panels::TextStyle>,
frame_controls: frame::FrameControls,
add_track: Option<AddTrackMenu>,
add_track_opens: usize,
@@ -1652,6 +1654,7 @@ fn add_track_tile_progress(delta: f32, index: usize) -> f32 {
}
const CLIP_MENU_WIDTH: f32 = 300.;
+const TEXT_MENU_WIDTH: f32 = 250.;
/// Roughly the menu's laid-out height; only decides which side of its anchor
/// the card opens on.
const CLIP_MENU_HEIGHT_ESTIMATE: f32 = 440.;
@@ -1679,10 +1682,11 @@ fn clip_menu_placement(
index: usize,
origin: Point<Pixels>,
viewport: gpui::Size<Pixels>,
+ menu_width: f32,
) -> ClipSpeedMenu {
let (width, height) = (f32::from(viewport.width), f32::from(viewport.height));
let left = px((f32::from(origin.x) - 8.)
- .min(width - CLIP_MENU_WIDTH - 12.)
+ .min(width - menu_width - 12.)
.max(12.));
let y = f32::from(origin.y);
let room_above = y - 20.;
@@ -1866,6 +1870,16 @@ struct OpenToolbarMenu {
state: ui::MenuState,
}
+/// The right-click menu on a text segment: the clip popover's card and rows,
+/// placed by `clip_menu_placement`. `index` is the segment under the pointer;
+/// `indices` is everything the menu acts on, which is the whole selection
+/// when the clicked segment was already part of it.
+struct OpenTextMenu {
+ index: usize,
+ indices: Vec<usize>,
+ placement: ClipSpeedMenu,
+}
+
impl EditorWindow {
pub fn new(project_path: PathBuf, window: &mut Window, cx: &mut Context<Self>) -> Self {
crate::theme::bind_window(window, cx);
@@ -2113,6 +2127,8 @@ impl EditorWindow {
preview_quality: crate::store::GeneralSettings::load().editor_preview_quality,
tracks: TrackLanes::from_project(&ProjectConfiguration::default(), false),
toolbar_menu: None,
+ text_menu: None,
+ text_style_clipboard: None,
frame_controls: frame::FrameControls::default(),
add_track: None,
add_track_opens: 0,
@@ -2253,6 +2269,7 @@ impl EditorWindow {
/// carried across.
fn rebuild_timeline(&mut self) {
dismiss_indexed_sidebar_menu(&mut self.sidebar.menu);
+ self.text_menu = None;
let mic = std::mem::take(&mut self.timeline.mic_waveforms);
let system = std::mem::take(&mut self.timeline.system_waveforms);
let waveform_paths = self.timeline.waveform_paths.clone();
@@ -2542,6 +2559,7 @@ impl EditorWindow {
) {
self.sidebar.style_target = None;
self.sidebar.menu = None;
+ self.text_menu = None;
self.selection = None;
self.sidebar.image_asset_status = None;
self.canvas_selection = None;
@@ -3448,6 +3466,12 @@ impl EditorWindow {
cx.stop_propagation();
return;
}
+ if self.text_menu.is_some() && event.keystroke.key.as_str() == "escape" {
+ self.text_menu = None;
+ cx.stop_propagation();
+ cx.notify();
+ return;
+ }
if self.add_track.is_some() && event.keystroke.key.as_str() == "escape" {
self.add_track = None;
cx.stop_propagation();
@@ -3983,6 +4007,7 @@ impl EditorWindow {
/// `setEditorState("timeline", "selection", ...)`.
pub(crate) fn set_selection(&mut self, selection: Option<Selection>, cx: &mut Context<Self>) {
if self.selection != selection {
+ self.text_menu = None;
self.dismiss_frame_controls(cx);
self.end_field_edit(cx);
self.close_color_picker(cx);
@@ -7809,7 +7834,8 @@ impl EditorWindow {
) {
let focus = self.focus_handle_for_menu();
window.focus(&focus, cx);
- let menu = clip_menu_placement(index, origin, window.viewport_size());
+ self.text_menu = None;
+ let menu = clip_menu_placement(index, origin, window.viewport_size(), CLIP_MENU_WIDTH);
tracing::info!(
index,
left = f32::from(menu.left),
@@ -7887,9 +7913,10 @@ impl EditorWindow {
}
/// `onContextMenu` on a clip's `SegmentRoot` (`TL/ClipTrack.tsx`): select
- /// the clip unless it is already part of the selection, then open its
- /// menu at the pointer. Other tracks have no context menu, and split mode
- /// keeps every press for the cut.
+ /// the segment unless it is already part of the selection, then open its
+ /// menu at the pointer. Clips get the speed popover, text segments the
+ /// Copy / Paste Style menu; other tracks have no context menu, and split
+ /// mode keeps every press for the cut.
fn track_context_menu(
&mut self,
kind: TrackKind,
@@ -7898,7 +7925,10 @@ impl EditorWindow {
window: &mut Window,
cx: &mut Context<Self>,
) {
- if kind != TrackKind::Clip || self.split_mode || self.transport.is_none() {
+ if !matches!(kind, TrackKind::Clip | TrackKind::Text)
+ || self.split_mode
+ || self.transport.is_none()
+ {
return;
}
let viewport_width: f32 = window.viewport_size().width.into();
@@ -7915,13 +7945,247 @@ impl EditorWindow {
return;
};
cx.stop_propagation();
- let already_selected = self.selection.as_ref().is_some_and(|selection| {
- selection.track == TrackKind::Clip && selection.indices.contains(&index)
- });
+ let already_selected = self
+ .selection
+ .as_ref()
+ .is_some_and(|selection| selection.contains(kind, index));
if !already_selected {
- self.set_selection(Some(Selection::single(TrackKind::Clip, index)), cx);
+ self.set_selection(Some(Selection::single(kind, index)), cx);
+ }
+ match kind {
+ TrackKind::Clip => self.open_clip_menu_at(index, event.position, window, cx),
+ _ => self.open_text_menu_at(index, event.position, window, cx),
+ }
+ }
+
+ fn open_text_menu_at(
+ &mut self,
+ index: usize,
+ origin: Point<Pixels>,
+ window: &mut Window,
+ cx: &mut Context<Self>,
+ ) {
+ let focus = self.focus_handle_for_menu();
+ window.focus(&focus, cx);
+ self.clip_speed = None;
+ self.add_track = None;
+ let indices = self
+ .selection
+ .as_ref()
+ .filter(|selection| selection.contains(TrackKind::Text, index))
+ .map_or_else(|| vec![index], |selection| selection.indices.clone());
+ self.text_menu = Some(OpenTextMenu {
+ index,
+ indices,
+ placement: clip_menu_placement(index, origin, window.viewport_size(), TEXT_MENU_WIDTH),
+ });
+ cx.notify();
+ }
+
+ fn copy_text_style(&mut self, index: usize) {
+ let segment = self
+ .project
+ .timeline
+ .as_ref()
+ .and_then(|timeline| timeline.text_segments.get(index));
+ let Some(segment) = segment else {
+ return;
+ };
+ self.text_style_clipboard = Some(crate::editor_panels::TextStyle::of(segment));
+ tracing::info!(index, "text style copied");
+ }
+
+ fn paste_text_style(&mut self, indices: &[usize], window: &mut Window, cx: &mut Context<Self>) {
+ let Some(style) = self.text_style_clipboard.clone() else {
+ return;
+ };
+ let mut changed = false;
+ self.edit_project("text-paste-style", window, cx, |project| {
+ let Some(timeline) = project.timeline.as_mut() else {
+ return false;
+ };
+ for index in indices {
+ if let Some(segment) = timeline.text_segments.get_mut(*index)
+ && style.apply_to(segment)
+ {
+ changed = true;
+ }
+ }
+ changed
+ });
+ if changed {
+ self.note_edit("text-paste-style", Some(TrackKind::Text));
}
- self.open_clip_menu_at(index, event.position, window, cx);
+ }
+
+ fn render_text_menu(&self, cx: &mut Context<Self>) -> Option<gpui::AnyElement> {
+ let menu = self.text_menu.as_ref()?;
+ let theme = self.theme;
+ let timeline = self.project.timeline.as_ref()?;
+ let segment = timeline.text_segments.get(menu.index)?;
+ let index = menu.index;
+ let indices = menu.indices.clone();
+ let count = indices.len();
+ let can_paste = self.text_style_clipboard.is_some();
+ let title: SharedString = if count > 1 {
+ format!("{count} text segments").into()
+ } else {
+ let content = segment.content.trim();
+ if content.is_empty() {
+ "Text".into()
+ } else {
+ content.to_string().into()
+ }
+ };
+ let summary = crate::editor_clips::format_clip_duration(segment.end - segment.start);
+ let paste_label: &'static str = if count > 1 {
+ "Paste style to all selected"
+ } else {
+ "Paste style"
+ };
+ let delete_label: &'static str = if count > 1 {
+ "Delete text segments"
+ } else {
+ "Delete text segment"
+ };
+ let divider = || div().h(px(1.)).bg(Hsla::from(theme.editor.line));
+
+ let mut card = div()
+ .id("text-menu-popover")
+ .absolute()
+ .left(menu.placement.left)
+ .w(px(TEXT_MENU_WIDTH))
+ .flex()
+ .flex_col()
+ .gap(px(8.))
+ .rounded(px(12.))
+ .border_1()
+ .border_color(Hsla::from(theme.editor.line))
+ .bg(Hsla::from(theme.editor.card))
+ .p(px(6.))
+ .shadow(theme.editor.pop_shadow())
+ .on_mouse_down(
+ MouseButton::Left,
+ cx.listener(|_, _, _, cx| cx.stop_propagation()),
+ );
+ card = match menu.placement.anchor {
+ ClipMenuAnchor::Above { bottom } => card.bottom(bottom),
+ ClipMenuAnchor::Below { top } => card.top(top),
+ };
+
+ let paste_indices = indices.clone();
+ let card = card
+ .child(
+ div()
+ .flex()
+ .items_center()
+ .gap(px(8.))
+ .px(px(8.))
+ .pt(px(4.))
+ .child(
+ div()
+ .flex_1()
+ .min_w_0()
+ .overflow_hidden()
+ .whitespace_nowrap()
+ .text_ellipsis()
+ .text_size(px(12.))
+ .font_weight(FontWeight::MEDIUM)
+ .text_color(Hsla::from(theme.editor.text_1))
+ .child(title),
+ )
+ .child(
+ div()
+ .flex_none()
+ .text_size(px(10.5))
+ .text_color(Hsla::from(theme.editor.text_3))
+ .child(summary),
+ ),
+ )
+ .child(
+ div()
+ .flex()
+ .flex_col()
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-copy-style",
+ "icons/copy.svg",
+ "Copy style",
+ ClipMenuTrailing::None,
+ true,
+ false,
+ )
+ .on_click(cx.listener(
+ move |this, _, _window, cx| {
+ this.text_menu = None;
+ this.copy_text_style(index);
+ cx.notify();
+ },
+ )),
+ )
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-paste-style",
+ "icons/clipboard-paste.svg",
+ paste_label,
+ if can_paste {
+ ClipMenuTrailing::None
+ } else {
+ ClipMenuTrailing::Hint("Copy a style first")
+ },
+ can_paste,
+ false,
+ )
+ .when(can_paste, |this| {
+ this.on_click(cx.listener(move |this, _, window, cx| {
+ this.text_menu = None;
+ this.paste_text_style(&paste_indices, window, cx);
+ cx.notify();
+ }))
+ }),
+ ),
+ )
+ .child(divider())
+ .child(
+ clip_menu_row(
+ &theme,
+ "text-menu-delete",
+ "icons/trash.svg",
+ delete_label,
+ ClipMenuTrailing::Kbd("backspace"),
+ true,
+ true,
+ )
+ .on_click(cx.listener(move |this, _, window, cx| {
+ this.text_menu = None;
+ this.delete_selection(window, cx);
+ cx.notify();
+ })),
+ );
+
+ Some(
+ div()
+ .absolute()
+ .top_0()
+ .left_0()
+ .size_full()
+ .child(
+ div()
+ .id("text-menu-backdrop")
+ .absolute()
+ .top_0()
+ .left_0()
+ .size_full()
+ .on_click(cx.listener(|this, _, _, cx| {
+ this.text_menu = None;
+ cx.notify();
+ })),
+ )
+ .child(card)
+ .into_any_element(),
+ )
}
fn set_clip_timescale(
@@ -11676,6 +11940,7 @@ impl Render for EditorWindow {
)
.children(self.with_style_controls(|this| this.render_sidebar_menu(cx)))
.children(self.render_toolbar_menu(cx))
+ .children(self.render_text_menu(cx))
.children(self.render_frame_controls(window, cx))
.children(self.render_add_track_popover(cx))
.children(self.render_clip_speed_popover(cx))