//! `TrackStack` and `TrackRow`: the tracks of one pane of a timeline on the collection contract (20.6). //! //! A stack is one collection keyed by [`TrackId`]. Its columns are the colour strip, the track name (the header width the //! app keeps, frozen and resizable), one sweepable toggle column per switch of the [`TrackInfo`] and the lane. A row paints //! its header in the cells and registers the lane's clips as parts of the lane cell through [`ClipLane`], so a track of //! any number of clips costs one pipeline run. Switches reach the app as the collection's `Toggle` events, the width of //! the header as its `Column` events; everything else a row reports is its [`TrackEvent`]. use super::clips::{ClipLane, ClipLaneEvents, ClipLook}; use super::view::{TimelineWords, time_axis}; use super::{DragEvent, LabelPalette, label_palette}; use crate::clips::ClipEdit; use crate::model::{ClipId, TrackId, TrackInfo, TrackKind}; use crate::time::Tick; use craft_behavior::{ ColumnKey, ColumnWidth, CursorIcon, DisplayOrder, EditEvent, EditEvents, HitLevel, HitPriority, IconId, ItemSource, MarqueeSelect, Metrics, Mixed, PartKey, PressPhase, Selection, SelectionChange, Sense, SizeHint, SpanLayout, }; use craft_ui::contract::{ Access, Bind, BindMut, Collection, CollectionProps, CollectionResponse, ColumnKind, ColumnSpec, Common, Component, ContainerProps, Cx, Edit as _, ItemKeys, ItemOutput, MeasureCx, Name, Outcome, PaintUi, PartsCx, Pointer as _, Props, Remember as _, Role, Scroll, ScrollAnchor, SelectMode, Sizing, Styled as _, ToggleColumn, WordSetCx as _, }; use craft_ui::primitives::{IconSpec, PaintShape, RuleKind}; use craft_ui::runtime::commands::FireSource; use craft_ui::runtime::env::Label; use craft_ui::style::{ComponentStyle, DataColor, IconLook, Stateful, StyleColor, StyleCx, SurfaceLook, TextLook, TypeRole, UiTokens}; use egui::{CornerRadius, Pos2, Rect, Response, Vec2}; use std::sync::Arc; /// The colour strip: a click on it opens the label menu. pub const STRIP: ColumnKey = ColumnKey::new("strip"); /// The track name. pub const NAME: ColumnKey = ColumnKey::new("name"); /// The lock switch. pub const LOCK: ColumnKey = ColumnKey::new("lock"); /// The targeting switch. pub const TARGET: ColumnKey = ColumnKey::new("target"); /// The sync lock switch. pub const SYNC_LOCK: ColumnKey = ColumnKey::new("syncLock"); /// The output switch (the eye or the speaker). pub const OUTPUT: ColumnKey = ColumnKey::new("output"); /// The mute switch. pub const MUTE: ColumnKey = ColumnKey::new("mute"); /// The solo switch. pub const SOLO: ColumnKey = ColumnKey::new("solo"); /// The lane with the clips. pub const LANE: ColumnKey = ColumnKey::new("lane"); const LABEL: PartKey = PartKey::new("label"); const HEADER: PartKey = PartKey::new("header"); const EDGE_BOTTOM: PartKey = PartKey::new("heightBottom"); const EDGE_TOP: PartKey = PartKey::new("heightTop"); /// The switch columns in header order, each with its icon while on and off. const SWITCHES: [(ColumnKey, &str, &str); 6] = [ (LOCK, "lock", "lock_open"), (TARGET, "target", "target"), (SYNC_LOCK, "sync_lock", "sync_lock"), (OUTPUT, "eye", "eye_off"), (MUTE, "track_mute", "track_mute"), (SOLO, "solo", "solo"), ]; /// How strongly a track's colour label tints its header: a share of the label colour (FilmCraft `TRACK_HEADER_TINT`). const HEADER_LABEL_TINT: f32 = 0.18; /// The sizes of a timeline's tracks and headers, all from the tokens. #[derive(Clone, Copy, Debug, PartialEq)] #[non_exhaustive] pub struct TrackSizes { /// A video track's default height. pub video: f32, /// An audio or other track's default height. pub audio: f32, /// A caption track's default height. pub caption: f32, /// The least a track's height can be dragged to. pub least: f32, /// The most a track's height can be dragged to. pub most: f32, /// One control height: the width of a switch column. pub unit: f32, } /// A track grows to at most this many heights of a large thumbnail. const MOST_IN_THUMBS: f32 = 4.0; /// The name column's width until the user drags it. pub(crate) const NAME_DEFAULT: ColumnWidth = ColumnWidth::Controls(6.0); /// A switch column is one control wide. const SWITCH_WIDTH: ColumnWidth = ColumnWidth::Controls(1.0); /// The colour strip is a quarter of a control wide. const STRIP_WIDTH: ColumnWidth = ColumnWidth::Controls(0.25); impl TrackSizes { /// The sizes the tokens give. pub fn of(tokens: &UiTokens) -> TrackSizes { let unit = tokens.size.control.md; let tall = tokens.size.thumb.lg; TrackSizes { video: tall, audio: tokens.size.thumb.md, caption: unit, least: unit, most: tall * MOST_IN_THUMBS, unit } } /// The height of a track of `kind` whose own height is `own`: that, or the default of its kind, within the least /// and the most. pub fn height(&self, kind: TrackKind, own: Option) -> f32 { let default = match kind { TrackKind::Video => self.video, TrackKind::Caption => self.caption, TrackKind::Audio | TrackKind::Other => self.audio, }; own.filter(|height| height.is_finite() && *height > 0.0).unwrap_or(default).clamp(self.least, self.most) } /// The height of `track`. pub fn height_of(&self, track: &TrackInfo<'_>) -> f32 { self.height(track.kind, track.height) } /// The height a double click on a header gives a track of height `now`: the tall one below the middle, else the /// short one. pub fn toggled(&self, now: f32) -> f32 { let (short, tall) = (self.caption, self.video); if now < (short + tall) / 2.0 { tall } else { short } } /// The width of the headers (colour strip, name and the switches) for a name column `name` wide: where the lane /// begins. The view reads it before its body has laid the stacks out, so it sums the same column widths the stacks /// declare. pub fn header_extent(&self, name: Option) -> f32 { let fixed = |width: ColumnWidth| match width.in_unit(self.unit) { ColumnWidth::Fixed(width) => Some(width), _ => None, }; let name = name.and_then(fixed).or_else(|| fixed(NAME_DEFAULT)).unwrap_or(0.0); let switches = SWITCHES.len() as f32 * fixed(SWITCH_WIDTH).unwrap_or(0.0); fixed(STRIP_WIDTH).unwrap_or(0.0) + name + switches } } /// The tracks of a pane as a collection source: keyed by track. #[non_exhaustive] pub struct Tracks<'a> { /// The tracks, top to bottom. pub tracks: &'a [TrackInfo<'a>], } impl<'a> Tracks<'a> { /// The source over `tracks`. pub fn new(tracks: &'a [TrackInfo<'a>]) -> Self { Tracks { tracks } } } impl DisplayOrder for Tracks<'_> { fn len(&self) -> usize { self.tracks.len() } fn key(&self, at: usize) -> TrackId { self.tracks[at].id } fn text(&self, at: usize) -> Option> { Some(std::borrow::Cow::Borrowed(self.tracks[at].name)) } } impl ItemSource for Tracks<'_> { fn toggled(&self, at: usize, column: &ColumnKey) -> bool { let track = &self.tracks[at]; let on = if *column == LOCK { track.locked } else if *column == TARGET { track.targeted } else if *column == SYNC_LOCK { track.sync_lock } else if *column == OUTPUT { track.enabled } else if *column == MUTE { track.muted } else { track.solo }; on.unwrap_or(false) } fn toggle_enabled(&self, at: usize, column: &ColumnKey) -> bool { let track = &self.tracks[at]; let switch = if *column == LOCK { track.locked } else if *column == TARGET { track.targeted } else if *column == SYNC_LOCK { track.sync_lock } else if *column == OUTPUT { track.enabled } else if *column == MUTE { track.muted } else { track.solo }; switch.is_some() } } impl TrackEvent { /// Whether the row reported nothing in the pass: such a row emits no item event. A field added later counts /// without a change here. fn is_idle(&self) -> bool { *self == Self::default() } } /// What a track row reports beyond the collection's own events, in one pass. #[derive(Clone, Debug, Default, PartialEq)] #[non_exhaustive] pub struct TrackEvent { /// A clip gesture of the lane. pub clip: EditEvents, /// A change of the clip selection. pub select: Option>, /// Cmd or Ctrl was held at the pick: the app leaves linked partners out. pub unlinked: bool, /// A double click on a clip. pub open: Option, /// A context menu on a clip, with the clip's rect and the pointer's position when a secondary click asked for it. pub context: Option<(ClipId, Rect, Option)>, /// The colour strip was clicked: the app shows the label menu. pub label_menu: bool, /// A drag of the row's edge, or a double click on the header. pub height: Option>, /// Where a clip move drag's pointer is over this row. pub pointer: Option, /// Where a lane drag snapped. pub snapped_at: Option, /// The marquee a drag on the lane's empty space sweeps. pub marquee: Option>, /// The marquee was released in this pass. pub marquee_ended: bool, } /// The pointer of a clip move drag over the row that holds the dragged clips: the panel works the track change out /// from it and the geometry of every row. #[derive(Clone, Copy, Debug, PartialEq)] #[non_exhaustive] pub struct MovePointer { /// The pointer's y. pub y: f32, /// The row's rect. pub row: Rect, } /// The stack has no look of its own: its rows and the view's overlay draw everything. #[derive(Clone, Debug, PartialEq, ComponentStyle)] #[non_exhaustive] pub struct TrackStackStyle { #[style(metrics)] metrics: Metrics, } /// The tracks of one pane. The video pane keeps its last track nearest the divider by anchoring its scroll at the end. #[derive(Component, Props)] #[component(collection)] pub struct TrackStack { #[common] common: Common, #[container] props: ContainerProps, items: CollectionProps, /// The kind and own height of each track, in order. heights: Vec<(TrackKind, Option)>, } impl TrackStack { /// A stack named `label` whose name column is `name` wide (the default until the user drags it). pub fn new(label: impl Into