GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
use super::{ClipId, FilmcraftApp, MarkerId, Rect, Sense, Sequence, TrackId, TrackItem, Value, json};

/// The clip context menu: groups (separated by rules) of (label, command id). Entries marked `…`
/// open their dialog through `menus::invoke`, like the same item in the Clip menu.
/// The menu of a right-clicked Timeline marker (automation ids `timeline.markerMenu.<command>`);
/// the marker is under the playhead, where Clear Selected Marker looks for it (#695).
pub(super) fn marker_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui, marker: MarkerId) {
    let Some(marker) = app.session.active_sequence().and_then(|q| q.markers.iter().find(|m| m.id == marker)).cloned() else {
        ui.close();
        return;
    };
    let label = crate::i18n::t("Edit Marker…");
    let r = ui.button(label);
    app.auto.add("timeline.markerMenu.markers.edit", r.rect, label);
    if r.clicked() {
        super::super::marker_properties::open(app, &marker);
        ui.close();
    }
    crate::panels::misc::marker_label_menu(app, ui, &marker, "timeline.markerMenu");
    ui.separator();
    for (label, cmd) in [("Clear Selected Marker", "markers.clearCurrent"), ("Clear Markers", "markers.clearAll")] {
        let label = crate::i18n::t(label);
        let r = ui.button(label);
        app.auto.add(&format!("timeline.markerMenu.{cmd}"), r.rect, label);
        if r.clicked() {
            if let Err(e) = crate::menus::invoke(app, ctx, cmd, json!({"target":"program"})) {
                app.ui.status = e;
            }
            ui.close();
        }
    }
}

pub(crate) const CLIP_MENU: &[&[(&str, &str)]] = &[
    &[
        ("Cut", "edit.cut"),
        ("Copy", "edit.copy"),
        ("Paste Attributes…", "edit.pasteAttributes"),
        ("Remove Attributes…", "edit.removeAttributes"),
        ("Clear", "edit.clear"),
        ("Ripple Delete", "edit.rippleDelete"),
    ],
    &[("Edit Original", "edit.editOriginal"), ("Replace With Clip From Source Monitor", "clip.replaceFromSource")],
    &[
        ("Enable", "clip.enable"),
        ("Link", "clip.link"),
        ("Group", "clip.group"),
        ("Ungroup", "clip.ungroup"),
        ("Synchronize…", "clip.synchronize"),
        ("Merge Clips…", "clip.mergeClips"),
        ("Nest…", "clip.nest"),
        ("Make Subsequence", "sequence.makeSubsequence"),
        ("Reveal Nested Sequence", "sequence.revealNested"),
        ("Multi-Camera", "clip.multicam"),
    ],
    &[("Label", "edit.label")],
    &[("Add Marker", "markers.add")],
    &[("Speed/Duration…", "clip.speedDuration"), ("Scene Edit Detection…", "clip.sceneEditDetection")],
    &[
        ("Frame Hold Options…", "clip.frameHoldOptions"),
        ("Add Frame Hold", "clip.frameHold"),
        ("Insert Frame Hold Segment", "clip.insertFrameHoldSegment"),
        ("Field Options…", "clip.fieldOptions"),
        ("Scale to Frame Size", "clip.scaleToFrameSize"),
        ("Fit to frame", "clip.fitToFrame"),
        ("Fill frame", "clip.fillFrame"),
    ],
    &[("Reveal in Project", "clip.revealInProject"), ("Join Through Edits", "sequence.joinThroughEdits")],
];

/// The cut right-clicked on a track (#219): the clip ending there and the clip starting there,
/// either missing at the ends of a gap. Its menu offers the edit point's trim types, Apply Default
/// Transitions and Join Through Edits, like Premiere's edit point menu.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct EditPointTarget {
    pub(super) left: Option<ClipId>,
    pub(super) right: Option<ClipId>,
}

impl EditPointTarget {
    pub(super) fn at(seq: &Sequence, track: TrackId, clip: ClipId, edge: filmcraft_edit::Edge) -> Option<Self> {
        let tr = seq.track(track)?;
        let it = tr.item(clip)?;
        Some(match edge {
            filmcraft_edit::Edge::Out => Self { left: Some(clip), right: tr.items.iter().find(|x| x.start == it.end()).map(|x| x.id) },
            filmcraft_edit::Edge::In => Self { left: tr.items.iter().find(|x| x.end() == it.start).map(|x| x.id), right: Some(clip) },
        })
    }
}

/// The edit point menu's trim types: label, id (`timeline.editPointMenu.<id>`), trim kind, and
/// which side of the cut it selects (the incoming clip's In or the outgoing clip's Out).
pub(crate) const EDIT_POINT_TYPES: &[(&str, &str, &str, filmcraft_edit::Edge)] = &[
    ("Ripple Trim In", "rippleIn", "ripple", filmcraft_edit::Edge::In),
    ("Ripple Trim Out", "rippleOut", "ripple", filmcraft_edit::Edge::Out),
    ("Roll Edit", "roll", "roll", filmcraft_edit::Edge::Out),
    ("Trim In", "trimIn", "trim", filmcraft_edit::Edge::In),
    ("Trim Out", "trimOut", "trim", filmcraft_edit::Edge::Out),
];

/// The edit point context menu (#219).
pub(super) fn edit_point_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui, seq: &Sequence, target: EditPointTarget) {
    let selected = app.session.state.edit_points.first().copied();
    for &(label, id, kind, edge) in EDIT_POINT_TYPES {
        let clip = match edge {
            filmcraft_edit::Edge::In => target.right,
            filmcraft_edit::Edge::Out => target.left,
        };
        // a roll needs a clip on both sides of the cut
        let clip = clip.filter(|_| kind != "roll" || (target.left.is_some() && target.right.is_some()));
        let out = edge == filmcraft_edit::Edge::Out;
        let current = selected.is_some_and(|ep| Some(ep.clip) == clip && ep.out == out && serde_json::to_value(ep.kind).ok() == Some(json!(kind)));
        let label = crate::i18n::t(label);
        let text = if current { format!("✓ {label}") } else { label.to_string() };
        let r = ui.add_enabled(clip.is_some(), egui::Button::new(text));
        app.auto.add(&format!("timeline.editPointMenu.{id}"), r.rect, label);
        if r.clicked()
            && let Some(c) = clip
        {
            let e = if out { "out" } else { "in" };
            if let Err(e) = app.session.execute("trim.selectEditPoint", json!({"clip": c.0, "edge": e, "kind": kind})) {
                app.ui.status = e.to_string();
            }
            ui.close();
        }
    }
    ui.separator();
    let label = tl!("Apply Default Transitions");
    let r = ui.add_enabled(app.session.is_enabled("trim.applyDefaultTransition"), egui::Button::new(label));
    app.auto.add("timeline.editPointMenu.applyDefaultTransitions", r.rect, label);
    if r.clicked() {
        if let Err(e) = crate::menus::invoke(app, ctx, "trim.applyDefaultTransition", json!({})) {
            app.ui.status = e;
        }
        ui.close();
    }
    // only this cut (and its linked partners), and only when it is a through edit
    let through = filmcraft_edit::through::through_edits(seq).iter().any(|e| Some(e.left) == target.left && Some(e.right) == target.right);
    let label = tl!("Join Through Edits");
    let r = ui.add_enabled(through, egui::Button::new(label));
    app.auto.add("timeline.editPointMenu.joinThroughEdits", r.rect, label);
    if r.clicked()
        && let (Some(left), Some(right)) = (target.left, target.right)
    {
        if let Err(e) = app.session.execute("sequence.joinThroughEdits", json!({"cut": [left.0, right.0]})) {
            app.ui.status = e.to_string();
        }
        ui.close();
    }
}

/// The menu of a right-click on the header column away from any track: the Add Tracks and Delete
/// Tracks dialogs. Automation ids `timeline.tracks.menu.<item>`.
pub(super) fn track_column_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, picked: &mut Option<(String, Value)>) {
    for (key, text, command) in [("addTracks", tl!("Add Tracks…"), "sequence.addTracks"), ("deleteTracks", tl!("Delete Tracks…"), "sequence.deleteTracks")]
    {
        let button = ui.add_enabled(app.session.is_enabled(command), egui::Button::new(text));
        app.auto.add(&format!("timeline.tracks.menu.{key}"), button.rect, text);
        if button.clicked() {
            *picked = Some((command.to_string(), json!({})));
            ui.close();
        }
    }
}

/// The menu of a right-clicked ruler (not on a marker): the In and Out points and the marker, at
/// the playhead. Groups of (label, command id); automation ids `timeline.rulerMenu.<command>`.
const RULER_MENU: &[&[(&str, &str)]] = &[
    &[("Mark In", "markers.markIn"), ("Mark Out", "markers.markOut"), ("Mark Clip", "markers.markClip"), ("Mark Selection", "markers.markSelection")],
    &[("Go to In", "markers.goToIn"), ("Go to Out", "markers.goToOut")],
    &[("Clear In", "markers.clearIn"), ("Clear Out", "markers.clearOut"), ("Clear In and Out", "markers.clearInOut")],
    &[("Add Marker", "markers.add")],
];

pub(super) fn ruler_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui) {
    for (i, group) in RULER_MENU.iter().enumerate() {
        if i > 0 {
            ui.separator();
        }
        for &(label, cmd) in *group {
            let label = crate::i18n::t(label);
            let r = ui.add_enabled(app.session.is_enabled(cmd), egui::Button::new(label));
            app.auto.add(&format!("timeline.rulerMenu.{cmd}"), r.rect, label);
            if r.clicked() {
                if let Err(e) = crate::menus::invoke(app, ctx, cmd, json!({})) {
                    app.ui.status = e;
                }
                ui.close();
            }
        }
    }
}

/// The menu of a right-clicked track header: add or delete tracks here, or through the dialogs of
/// Sequence ▸ Add Tracks… / Delete Tracks… (#1117). The command picked goes to `picked`; automation
/// ids `timeline.track.<V1|A1…>.menu.<item>`.
fn track_header_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, label: &str, track: TrackId, video: bool, picked: &mut Option<(String, Value)>) {
    let mut item = |ui: &mut egui::Ui, key: &str, text: &str, command: &str, params: Value| {
        let button = ui.add_enabled(app.session.is_enabled(command), egui::Button::new(text));
        app.auto.add(&format!("timeline.track.{label}.menu.{key}"), button.rect, text);
        if button.clicked() {
            *picked = Some((command.to_string(), params));
            ui.close();
        }
    };
    let after = if video { json!({"video": 1, "audio": 0, "videoAfter": label}) } else { json!({"video": 0, "audio": 1, "audioAfter": label}) };
    item(ui, "addTrack", tl!("Add Track"), "sequence.addTracks", after);
    item(ui, "addTracks", tl!("Add Tracks…"), "sequence.addTracks", json!({}));
    ui.separator();
    item(ui, "deleteTrack", tl!("Delete Track"), "sequence.deleteTrack", json!({"track": track.0}));
    item(ui, "deleteTracks", tl!("Delete Tracks…"), "sequence.deleteTracks", json!({}));
}

/// Opens the track header menu on a right-click anywhere on `visible`, a header, even over its
/// buttons, which take only the click.
pub(super) fn track_header_popup(
    app: &mut FilmcraftApp,
    ui: &mut egui::Ui,
    visible: Rect,
    label: &str,
    track: TrackId,
    video: bool,
    picked: &mut Option<(String, Value)>,
) {
    let menu_resp = ui.interact(visible, egui::Id::unique(("hdr-menu", track.0)), Sense::click());
    let mut menu = egui::Popup::context_menu(&menu_resp);
    if ui.input(|i| i.pointer.secondary_clicked()) && ui.rect_contains_pointer(visible) {
        menu = menu.open_memory(egui::SetOpenCommand::Bool(true));
    }
    menu.show(|ui| track_header_menu(app, ui, label, track, video, picked));
}

/// Runs the command picked in a track header's menu, if any.
pub(super) fn run_track_menu(app: &mut FilmcraftApp, ui: &egui::Ui, picked: Option<(String, Value)>) {
    if let Some((cmd, params)) = picked
        && let Err(e) = crate::menus::invoke(app, &ui.ctx().clone(), &cmd, params)
    {
        app.ui.status = e;
    }
}

/// The menu of a right-clicked gap: Ripple Delete closes it.
pub(super) fn gap_menu(app: &mut FilmcraftApp, ctx: &egui::Context, ui: &mut egui::Ui) {
    let label = tl!("Ripple Delete");
    // greyed out, with the reason on hover, when the gap can't close (a sync-locked track has a
    // clip in the way, #2960), as in Premiere Pro, instead of doing nothing when clicked
    let why = filmcraft_engine::find_command("edit.rippleDelete").map_or(Ok(()), |c| (c.enabled)(&app.session)).err();
    let r = ui.add_enabled(app.session.state.gap_selection.is_some() && why.is_none(), egui::Button::new(label));
    let r = match why {
        Some(why) => r.on_disabled_hover_text(why),
        None => r,
    };
    app.auto.add("timeline.gapMenu.edit.rippleDelete", r.rect, label);
    if r.clicked() {
        if let Err(e) = crate::menus::invoke(app, ctx, "edit.rippleDelete", json!({})) {
            app.ui.status = e;
        }
        ui.close();
    }
}

/// The clip menu's Multi-Camera submenu: Enable, Flatten, and the cameras of the selected nested
/// sequence clips (greyed out when none of the selected clips is a nested sequence).
pub(super) fn multicam_menu(app: &mut FilmcraftApp, ui: &mut egui::Ui, picked: &[&TrackItem]) {
    let nests: Vec<&TrackItem> = picked.iter().copied().filter(|it| app.session.project.sequence(it.item).is_some()).collect();
    let enabled = !nests.is_empty() && nests.iter().all(|it| it.multicam.is_some_and(|m| m.enabled));
    // camera names of the first selected nest, and the angle it shows
    let cameras: Vec<(usize, String)> = nests
        .first()
        .and_then(|it| app.session.project.sequence(it.item))
        .map(|q| q.cameras().video_angles().map(|(i, c)| (i, c.name.clone())).collect())
        .unwrap_or_default();
    let shown = nests.first().and_then(|it| it.multicam).filter(|m| m.enabled).map(|m| m.angle as usize);
    let mut run: Option<(&str, Value)> = None;
    ui.add_enabled_ui(!nests.is_empty(), |ui| {
        let r = ui.menu_button(tl!("Multi-Camera"), |ui| {
            for (label, cmd) in [(if enabled { tl!("✓ Enable") } else { tl!("Enable") }, "clip.multicamEnable"), (tl!("Flatten"), "clip.multicamFlatten")] {
                let r = ui.add_enabled(app.session.is_enabled(cmd), egui::Button::new(label));
                app.auto.add(&format!("timeline.clipMenu.{cmd}"), r.rect, label);
                if r.clicked() {
                    run = Some((cmd, json!({})));
                }
            }
            if enabled && !cameras.is_empty() {
                ui.separator();
                for (angle, name) in &cameras {
                    let label = if shown == Some(*angle) { format!("✓ {name}") } else { name.clone() };
                    let r = ui.button(&label);
                    app.auto.add(&format!("timeline.clipMenu.multicam.camera.{angle}"), r.rect, &label);
                    if r.clicked() {
                        run = Some(("multicam.switchAngle", json!({"angle": angle, "clips": nests.iter().map(|it| it.id.0).collect::<Vec<_>>()})));
                    }
                }
            }
        });
        app.auto.add("timeline.clipMenu.clip.multicam", r.response.rect, "Multi-Camera");
    });
    if let Some((cmd, params)) = run {
        if let Err(e) = app.session.execute(cmd, params) {
            app.ui.status = e.to_string();
        }
        ui.close();
    }
}