GitCorner

Code

ossartcraftfilmcraft🌐
f67d38b35cc5
//! Panel bodies. `show` dispatches on [`PanelKind`]; drag-and-drop between panels (project items,
//! effects) is carried in egui temp data so the timeline/monitors can accept drops.

pub mod audio_fx_editor;
pub mod caption_export;
pub mod clip_dialogs;
pub mod clip_mismatch;
pub mod color_dialogs;
pub mod dialogs;
pub mod effect_controls;
pub mod effects;
pub mod essential_sound;
pub mod events;
pub mod export_mode;
pub mod file_dialogs;
pub mod frame_export;
pub mod graphics;
pub mod graphics_templates;
pub mod import_mode;
pub mod interchange_export;
pub mod keyboard;
pub mod lumetri;
pub mod masks;
pub mod media_browser;
pub mod media_dialogs;
pub mod menu_dialogs;
pub mod metadata;
pub mod meters;
pub mod misc;
pub mod mixer;
pub mod monitor;
pub mod monitor_view;
pub mod motion_path;
pub mod multicam;
pub mod notes;
pub mod panel_state;
pub mod presets;
pub mod project;
pub mod project_dialogs;
pub mod project_views;
pub mod reference;
pub mod remix;
pub mod scopes;
pub mod sequence_settings;
pub mod settings;
pub mod shortcuts_dialog;
pub mod text;
pub mod timecode;
pub mod timeline;
pub mod timeline_automation;
pub mod timeline_captions;
pub mod timeline_hit;
pub mod timeline_volume;
pub mod tools;
pub mod transform_handles;
pub mod transition_controls;
pub mod trim_monitor;
pub mod tts;
pub mod voiceover;
pub mod workspaces;

use egui::{Align2, Color32, Rect};
use filmcraft_project::ItemId;

use crate::FilmcraftApp;
use crate::dock::PanelKind;
use crate::theme::Tokens;

/// The status bar's job text as last shown. It changes at most every [`JobText::EVERY`] seconds,
/// and the room it takes only grows while the job runs, so where it starts stays put.
pub struct JobText {
    pub job: u64,
    /// [`FilmcraftApp::now`] when `text` was made.
    pub at: f64,
    /// `Exporting… 46% · 56:11 left`.
    pub text: String,
    /// The same without the verb, for agents (`status.job.progress`).
    pub label: String,
    /// The widest the text has been for this job.
    pub width: f32,
}

impl JobText {
    /// More often than about once a second, a changing number is flicker rather than news.
    pub const EVERY: f64 = 1.0;
}

/// The texts of running jobs elsewhere (an export queue item, a row of the Progress panel) as last
/// shown, by key: each changes at most every [`JobText::EVERY`] seconds.
#[derive(Default)]
pub struct SteadyTexts(std::collections::HashMap<String, (f64, String)>);

impl SteadyTexts {
    /// The text last shown under `key`, or `make`'s when that is [`JobText::EVERY`] old at `now`.
    /// Texts no longer asked for (their job ended) are let go.
    pub fn get(&mut self, key: &str, now: f64, make: impl FnOnce() -> String) -> String {
        self.0.retain(|_, (at, _)| (*at..*at + 10.0 * JobText::EVERY).contains(&now));
        if let Some((at, text)) = self.0.get(key)
            && (*at..*at + JobText::EVERY).contains(&now)
        {
            return text.clone();
        }
        let text = make();
        self.0.insert(key.to_owned(), (now, text.clone()));
        text
    }
}

#[cfg(test)]
mod steady_text_tests {
    use super::SteadyTexts;

    #[test]
    fn a_text_is_made_again_once_a_second_and_forgotten_when_no_longer_asked_for() {
        let mut texts = SteadyTexts::default();
        assert_eq!(texts.get("a", 5.0, || "1".into()), "1");
        assert_eq!(texts.get("a", 5.9, || "2".into()), "1");
        assert_eq!(texts.get("b", 5.9, || "x".into()), "x", "each key has its own");
        assert_eq!(texts.get("a", 6.0, || "3".into()), "3");
        // a clock that went back (a test, a new session) makes the text again
        assert_eq!(texts.get("a", 1.0, || "4".into()), "4");
        // "b", last made at 5.9, ended long ago
        texts.get("a", 100.0, || "5".into());
        assert_eq!(texts.0.len(), 1);
    }
}

/// ` · 2:05 left`: the time a job has left, as it follows a percentage.
pub fn left_text(d: std::time::Duration) -> String {
    tlf!(" · {time} left", time = filmcraft_engine::export::format_eta(d))
}

/// [`left_text`] for a job or queue item whose JSON has `etaSeconds`, nothing while it is null.
pub fn eta_suffix(job: &serde_json::Value) -> String {
    job["etaSeconds"].as_f64().and_then(|s| std::time::Duration::try_from_secs_f64(s).ok()).map(left_text).unwrap_or_default()
}

pub fn show(app: &mut FilmcraftApp, ui: &mut egui::Ui, p: PanelKind, rect: Rect) {
    match p {
        PanelKind::Program if !app.session.state.edit_points.is_empty() => trim_monitor::show(app, ui, rect),
        PanelKind::Program => monitor::show(app, ui, rect, monitor::Which::Program),
        PanelKind::Source => monitor::show(app, ui, rect, monitor::Which::Source),
        PanelKind::Timeline => timeline::show(app, ui, rect),
        PanelKind::Project => project::show(app, ui, rect),
        PanelKind::Tools => tools::show(app, ui, rect),
        PanelKind::Effects => effects::show(app, ui, rect),
        PanelKind::EffectControls => effect_controls::show(app, ui, rect),
        PanelKind::AudioMeters => meters::show(app, ui, rect),
        PanelKind::LumetriColor => lumetri::show(app, ui, rect),
        PanelKind::Properties if graphics::graphic_selected(app) => graphics::properties(app, ui, rect),
        PanelKind::Properties if text::caption_selected(app) => text::caption_properties(app, ui, rect),
        PanelKind::Properties => effect_controls::properties_panel(app, ui, rect),
        PanelKind::EssentialGraphics => graphics_templates::essential_graphics(app, ui, rect),
        PanelKind::History => misc::history(app, ui, rect),
        PanelKind::Markers => misc::markers(app, ui, rect),
        PanelKind::Info => misc::info(app, ui, rect),
        PanelKind::MediaBrowser => media_browser::show(app, ui, rect),
        PanelKind::AudioTrackMixer => mixer::track_mixer(app, ui, rect),
        PanelKind::AudioClipMixer => mixer::clip_mixer(app, ui, rect),
        PanelKind::LumetriScopes => scopes::show(app, ui, rect),
        PanelKind::Metadata => metadata::show(app, ui, rect),
        PanelKind::Timecode => timecode::show(app, ui, rect),
        PanelKind::Events => events::events(app, ui, rect),
        PanelKind::Progress => events::progress(app, ui, rect),
        PanelKind::ReferenceMonitor => reference::show(app, ui, rect),
        PanelKind::Text => text::show(app, ui, rect),
        PanelKind::EssentialSound => essential_sound::show(app, ui, rect),
        PanelKind::TextToSpeech => tts::show(app, ui, rect),
        PanelKind::ProjectNotes => notes::show(app, ui, rect),
        other => crate::dock::placeholder(ui, rect, &app.tokens, &tlf!("{panel} — coming in a later milestone", panel = crate::i18n::t(other.title()))),
    }
}

pub mod source_drag;
pub mod source_range;

#[derive(Clone, Debug)]
enum DragPayload {
    Item(ItemId),
    /// A bin (by id) dragged in the Project panel.
    Bin(u64),
    Source(source_drag::SourceDrag),
    Effect(String),
    /// A graphics template (id, name) from Essential Graphics ▸ Browse.
    Template(String, String),
}

/// Single egui temp slot holding the active drag payload: a project item or bin, a source, an effect or a graphics template.
fn payload_id() -> egui::Id {
    egui::Id::unique("filmcraft-drag-payload")
}

pub fn start_drag_source(ui: &egui::Ui, source: source_drag::SourceDrag) {
    ui.ctx().data_mut(|d| d.insert_temp(payload_id(), Some(DragPayloadBox(DragPayload::Source(source)))));
}
pub fn dragged_source(ui: &egui::Ui) -> Option<source_drag::SourceDrag> {
    match payload(ui) {
        Some(DragPayload::Source(s)) => Some(s),
        _ => None,
    }
}
pub fn start_drag_item(ui: &egui::Ui, item: ItemId) {
    ui.ctx().data_mut(|d| d.insert_temp(payload_id(), Some(DragPayloadBox(DragPayload::Item(item)))));
}
pub fn start_drag_bin(ui: &egui::Ui, bin: u64) {
    ui.ctx().data_mut(|d| d.insert_temp(payload_id(), Some(DragPayloadBox(DragPayload::Bin(bin)))));
}
pub fn dragged_bin(ui: &egui::Ui) -> Option<u64> {
    match payload(ui) {
        Some(DragPayload::Bin(b)) => Some(b),
        _ => None,
    }
}
pub fn start_drag_template(ui: &egui::Ui, id: &str, name: &str) {
    ui.ctx().data_mut(|d| d.insert_temp(payload_id(), Some(DragPayloadBox(DragPayload::Template(id.to_string(), name.to_string())))));
}
pub fn dragged_template(ui: &egui::Ui) -> Option<String> {
    match payload(ui) {
        Some(DragPayload::Template(id, _)) => Some(id),
        _ => None,
    }
}
pub fn start_drag_effect(ui: &egui::Ui, id: &str) {
    ui.ctx().data_mut(|d| d.insert_temp(payload_id(), Some(DragPayloadBox(DragPayload::Effect(id.to_string())))));
}
/// Prefix of a dragged effect payload that names a Color Grading preset rather than an effect id.
pub const LUMETRI_PRESET_PREFIX: &str = "lumetri:";
/// A Color Grading preset (Effects ▸ Color Grading Presets) dragged onto a clip: an effect payload
/// `lumetri:<name>` the Timeline applies with `lumetri.applyPreset`.
pub fn start_drag_lumetri_preset(ui: &egui::Ui, name: &str) {
    start_drag_effect(ui, &format!("{LUMETRI_PRESET_PREFIX}{name}"));
}
#[derive(Clone, Debug)]
struct DragPayloadBox(DragPayload);

/// Whether one of FilmCraft's own drags (an item, bin, source range, effect or template) is under way.
pub(crate) fn dragging(ctx: &egui::Context) -> bool {
    ctx.data(|d| d.get_temp::<Option<DragPayloadBox>>(payload_id())).flatten().is_some()
}

fn payload(ui: &egui::Ui) -> Option<DragPayload> {
    ui.ctx().data(|d| d.get_temp::<Option<DragPayloadBox>>(payload_id())).flatten().map(|b| b.0)
}
pub fn dragged_project_item(ui: &egui::Ui) -> Option<ItemId> {
    match payload(ui) {
        Some(DragPayload::Item(i)) => Some(i),
        Some(DragPayload::Source(s)) => Some(s.item),
        _ => None,
    }
}
/// An item dragged from the Project panel (or the Media Browser), but not a Source Monitor drag,
/// whose In/Out range and video/audio choice only a timeline drop keeps.
pub fn dragged_panel_item(ui: &egui::Ui) -> Option<ItemId> {
    match payload(ui) {
        Some(DragPayload::Item(i)) => Some(i),
        _ => None,
    }
}
/// The items a drag from the Project panel carries: the whole selection when the dragged item is
/// one of several selected, otherwise just the dragged item.
pub fn dragged_selection(app: &FilmcraftApp, item: ItemId) -> Vec<ItemId> {
    let sel = &app.session.state.project_selection;
    // the selection can hold bins beside items (one id space): only items can be placed
    if sel.len() > 1 && sel.contains(&item) { sel.iter().copied().filter(|i| app.session.project.item(*i).is_some()).collect() } else { vec![item] }
}
pub fn dragged_effect(ui: &egui::Ui) -> Option<String> {
    match payload(ui) {
        Some(DragPayload::Effect(e)) => Some(e),
        _ => None,
    }
}
pub fn clear_drag(ui: &egui::Ui) {
    ui.ctx().data_mut(|d| d.insert_temp::<Option<DragPayloadBox>>(payload_id(), None));
}

/// Temp-store key marking a cancelled drag until the button is released, so it cannot restart immediately.
fn cancelled_id() -> egui::Id {
    egui::Id::unique("filmcraft-drag-cancelled")
}

/// Abandon the drag in progress (Escape, #580): nothing is dropped, and nothing restarts it until
/// the button comes up.
pub fn cancel_drag(ui: &egui::Ui) {
    clear_drag(ui);
    ui.ctx().data_mut(|d| d.insert_temp(cancelled_id(), true));
}

/// Whether the press in progress had its drag cancelled with Escape.
pub fn drag_cancelled(ui: &egui::Ui) -> bool {
    ui.ctx().data(|d| d.get_temp::<bool>(cancelled_id())).unwrap_or(false)
}

/// Draw the drag ghost near the pointer and clear the payload after release. Escape cancels it.
pub fn drag_ghost(app: &FilmcraftApp, ui: &egui::Ui) {
    if !ui.ctx().input(|i| i.pointer.any_down()) {
        ui.ctx().data_mut(|d| d.remove::<bool>(cancelled_id()));
    }
    let Some(pl) = payload(ui) else { return };
    let ctx = ui.ctx();
    if ctx.input(|i| i.key_pressed(egui::Key::Escape)) {
        cancel_drag(ui);
        return;
    }
    if let Some(p) = ctx.pointer_hover_pos() {
        let label = match &pl {
            DragPayload::Item(i) => match dragged_selection(app, *i).len() {
                1 => app.session.project.item(*i).map(|x| x.name.clone()).unwrap_or_default(),
                n => format!("{n} items"),
            },
            DragPayload::Bin(b) => app.session.project.root.find_bin(filmcraft_project::BinId(*b)).map(|x| x.name.clone()).unwrap_or_default(),
            DragPayload::Source(s) => {
                let name = app.session.project.item(s.item).map(|i| i.name.as_str()).unwrap_or("Source");
                let mode = match (s.video, s.audio) {
                    (true, true) => "video + audio",
                    (true, false) => "video",
                    _ => "audio",
                };
                format!("{name} · {mode} · {:.2} s", s.range.duration.seconds())
            }
            DragPayload::Template(_, name) => name.clone(),
            DragPayload::Effect(e) => match (e.strip_prefix("preset:"), e.strip_prefix(LUMETRI_PRESET_PREFIX)) {
                (Some(name), _) => name.to_string(),
                (_, Some(name)) => crate::i18n::t(name).to_string(),
                _ => filmcraft_project::find_effect(e).map(|d| d.name.to_string()).unwrap_or_default(),
            },
        };
        let painter = ctx.layer_painter(egui::LayerId::new(egui::Order::Tooltip, egui::Id::unique("drag-ghost")));
        let r = Rect::from_min_size(p + egui::vec2(12.0, 8.0), egui::vec2(label.len() as f32 * 7.0 + 16.0, 20.0));
        painter.rect_filled(r, 4.0, Color32::from_black_alpha(200));
        painter.text(r.center(), Align2::CENTER_CENTER, label, Tokens::ui(11.5), Color32::WHITE);
    }
    if ctx.input(|i| i.pointer.any_released()) {
        // cleared next frame so drop targets see the release this frame
        let f = ctx.cumulative_frame_nr();
        let k = egui::Id::unique("drag-release-frame");
        match ctx.data(|d| d.get_temp::<u64>(k)) {
            Some(prev) if prev < f => {
                clear_drag(ui);
                ctx.data_mut(|d| d.remove::<u64>(k));
            }
            None => {
                ctx.data_mut(|d| d.insert_temp(k, f));
            }
            Some(_) => {}
        }
    } else if !ctx.input(|i| i.pointer.any_down()) {
        clear_drag(ui);
    }
}

/// The main window's share of the panel "≡" menu pass: the drag ghost, the bins in their own
/// windows, and the menu itself when it was opened in the main window.
pub fn panel_menu_popup(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
    drag_ghost(app, ui);
    // bins opened in new windows (Project panel)
    project::floating(app, ui.ctx());
    panel_menu_area(app, ui);
}

/// The panel "≡" menu, drawn in the viewport that owns the click that opened it: a panel in its
/// own OS window opens its menu in that window, at that window's coordinates (#3633).
pub fn panel_menu_area(app: &mut FilmcraftApp, ui: &mut egui::Ui) {
    let id = egui::Id::unique("panel-menu");
    let viewport = ui.ctx().viewport_id();
    let Some((p, pos, owner)) = ui.ctx().data(|d| d.get_temp::<(PanelKind, egui::Pos2, egui::ViewportId)>(id)) else { return };
    if owner != viewport {
        // a click in another window dismisses the menu, as a click elsewhere in its own window does
        if ui.ctx().input(|i| i.pointer.any_pressed()) {
            ui.ctx().data_mut(|d| d.remove::<(PanelKind, egui::Pos2, egui::ViewportId)>(id));
        }
        return;
    }
    // its elements are registered in the window that draws them
    let outer = std::mem::replace(&mut app.auto.viewport, viewport);
    let mut close = false;
    let area = egui::Area::new(id.with("area")).order(egui::Order::Foreground).fixed_pos(pos).show(ui.ctx(), |ui| {
        egui::Frame::popup(ui.style()).show(ui, |ui| {
            let scroll =
                egui::ScrollArea::vertical().id_salt("panel-menu-scroll").max_height((ui.ctx().content_rect().height() - 16.0).max(0.0)).show(ui, |ui| {
                    // a small window (a floated panel can be narrower than the menu) takes the menu at its width
                    let width = ui.ctx().content_rect().width() - 16.0;
                    ui.set_min_width(190.0_f32.min(width.max(0.0)));
                    ui.set_max_width(width.max(0.0));
                    // the Timeline's tabs are its open sequences: Close Panel closes the active one and
                    // keeps the panel (Premiere's wording and behaviour)
                    if p == PanelKind::Timeline && app.session.state.active_sequence.is_some() {
                        let r = ui.button(tl!("Close Panel"));
                        app.auto.add("panel.menu.Timeline.close", r.rect, "Close Panel");
                        if r.clicked() {
                            let _ = app.session.execute("sequence.close", serde_json::json!({}));
                            close = true;
                        }
                        let r = ui.add_enabled(app.session.state.open_sequences.len() > 1, egui::Button::new(tl!("Close Other Timeline Panels")));
                        app.auto.add("panel.menu.Timeline.closeOthers", r.rect, "Close Other Timeline Panels");
                        if r.clicked() {
                            let _ = app.session.execute("sequence.closeOthers", serde_json::json!({}));
                            close = true;
                        }
                    } else if ui.button(tl!("Close Panel")).clicked() {
                        let _ = crate::menus::invoke(app, ui.ctx(), "window.closePanel", serde_json::json!({"panel": p.id()}));
                        close = true;
                    }
                    let undock = ui.button(tl!("Undock Panel"));
                    app.auto.add_response(&format!("panel.menu.{}.undock", p.id()), &undock, "Undock Panel");
                    if undock.clicked() {
                        let _ = crate::menus::invoke(app, ui.ctx(), "window.floatPanel", serde_json::json!({"panel": p.id()}));
                        close = true;
                    }
                    let maximized = app.ui.keys.maximized.is_some_and(|q| crate::dock::LayoutExt::is_docked(&app.ui.dock, q));
                    let r = ui.button(if maximized { tl!("Restore Frame") } else { tl!("Maximize Frame") });
                    app.auto.add(&format!("panel.menu.{}.maximize", p.id()), r.rect, if maximized { "Restore Frame" } else { "Maximize Frame" });
                    if r.clicked() {
                        keyboard::toggle_maximize(app, p);
                        close = true;
                    }
                    if ui.button(tl!("Restore Workspace")).clicked() {
                        let w = app.ui.workspace.clone();
                        app.set_workspace(&w);
                        close = true;
                    }
                    close |= panel_items(app, ui, p);
                });
            app.auto.add("panel.menu.scroll", scroll.inner_rect, "Panel menu");
        });
    });
    // A click elsewhere or Escape closes the menu. The click that opened it is over the tab, not
    // the menu, and must not close it again in the same frame.
    let fresh = ui.ctx().data(|d| d.get_temp::<u64>(egui::Id::unique("panel-menu-opened"))) == Some(ui.ctx().cumulative_frame_nr());
    if close || (!fresh && area.response.clicked_elsewhere()) || crate::widgets::escape_closes(ui.ctx()) {
        ui.ctx().data_mut(|d| d.remove::<(PanelKind, egui::Pos2, egui::ViewportId)>(id));
    }
    app.auto.viewport = outer;
}

/// The panel's own items, each section after a separator: the Timeline's sequence and track
/// display items, and the Project, Media Browser and Source menus. Shared by the ≡ menu and the
/// tab's context menu. True when an item asks the menu to close.
pub(crate) fn panel_items(app: &mut FilmcraftApp, ui: &mut egui::Ui, p: PanelKind) -> bool {
    let mut close = false;
    if p == PanelKind::Timeline {
        ui.separator();
        let r = ui.add_enabled(app.session.state.active_sequence.is_some(), egui::Button::new(tl!("Reveal Sequence in Project")));
        app.auto.add("panel.menu.Timeline.revealSequence", r.rect, "Reveal Sequence in Project");
        if r.clicked() {
            let _ = app.session.execute("sequence.revealInProject", serde_json::json!({}));
            close = true;
        }
        let r = ui.checkbox(&mut app.ui.timeline.show_thumbnails, tl!("Video Thumbnails"));
        app.auto.add("panel.menu.Timeline.videoThumbnails", r.rect, "Video Thumbnails");
        let r = ui.checkbox(&mut app.ui.timeline.show_waveforms, tl!("Audio Waveforms"));
        app.auto.add("panel.menu.Timeline.audioWaveforms", r.rect, "Audio Waveforms");
        let r = ui.checkbox(&mut app.ui.timeline.show_video_names, tl!("Video Names"));
        app.auto.add("panel.menu.Timeline.videoNames", r.rect, "Video Names");
        let r = ui.checkbox(&mut app.ui.timeline.show_audio_names, tl!("Audio Names"));
        app.auto.add("panel.menu.Timeline.audioNames", r.rect, "Audio Names");
        let mut work_area = app.session.active_sequence().is_some_and(|q| q.work_area.is_some());
        let r = ui.add_enabled(app.session.state.active_sequence.is_some(), egui::Checkbox::new(&mut work_area, tl!("Work Area Bar")));
        app.auto.add("panel.menu.Timeline.workAreaBar", r.rect, "Work Area Bar");
        if r.changed()
            && let Err(e) = app.session.execute("sequence.workAreaBar", serde_json::json!({"show": work_area}))
        {
            app.ui.status = e.to_string();
        }
    }
    if p == PanelKind::Project {
        ui.separator();
        close |= project::panel_menu(app, ui);
    }
    if p == PanelKind::MediaBrowser {
        ui.separator();
        close |= media_browser::panel_menu(app, ui);
    }
    if p == PanelKind::Source {
        ui.separator();
        close |= monitor::source_panel_menu(app, ui);
    }
    if p == PanelKind::LumetriColor {
        ui.separator();
        close |= lumetri::panel_menu(app, ui);
    }
    if p == PanelKind::History {
        ui.separator();
        close |= misc::history_panel_menu(app, ui);
    }
    close
}

pub(crate) mod transcript_cache;

pub mod custom_colors;
pub mod marker_properties;

pub mod markers_panel;
pub mod palette_manager;