forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

oracle.rs933 lines, 45.2 KiBHistoryRawDownload
1//! Whether an action can be asked for -- the one answer every surface reads.
2
3use crate::*;
4
5/// Whether an action can be asked for, and what to say when it cannot. Two
6/// kinds of no: `Hidden` is about the *kind* of thing the action was aimed at
7/// -- an audio clip has no picture, so a grade is not a thing that exists for
8/// it, whatever the editor does next -- and `No` is about the state of this
9/// moment, which the next click of the playhead can change. The clip menu
10/// leaves the class refusals *out* and dims the state ones ([`Enable::listed`]);
11/// the actions card, which is the whole registry laid out, dims both with their
12/// reason -- an action missing from the one surface that lists everything would
13/// read as an action that does not exist.
14#[derive(Clone, Copy, PartialEq, Eq, Debug)]
15pub(crate) enum Enable {
16 Yes,
17 No(&'static str),
18 Hidden(&'static str),
19}
20
21impl Enable {
22 /// Whether the row takes a click.
23 pub(crate) fn yes(self) -> bool {
24 self == Enable::Yes
25 }
26
27 /// Whether a menu *about this clip* draws the row at all: a class refusal
28 /// is a thing that does not exist for what was clicked, and a row nothing
29 /// the user does could ever light is noise between the ones they came for.
30 pub(crate) fn listed(self) -> bool {
31 !matches!(self, Enable::Hidden(_))
32 }
33
34 /// What the row says instead of its stroke, if it says anything.
35 pub(crate) fn why(self) -> Option<&'static str> {
36 match self {
37 Enable::Yes => None,
38 Enable::No(why) | Enable::Hidden(why) => Some(why),
39 }
40 }
41}
42
43/// What an enablement question is asked *about*: the clip in question, if there
44/// is one, and the little of the editor's state the answers need. Handed in
45/// rather than read off the player, so [`enable`] is a pure function a test can
46/// ask about a clip without building a window.
47#[derive(Clone, Default)]
48pub(crate) struct Ctx {
49 /// The clip the question is about -- the one a menu was opened on, or the
50 /// marked one. `None` means the question is about the editor as a whole,
51 /// and the clip-relative answers stand aside: those actions find their own
52 /// clip under the playhead and word their own refusal.
53 pub(crate) clip: Option<(Clip, Lane)>,
54 /// A gap is picked ([`Player::gap_pick`]): Delete closes it, whether or
55 /// not any clip is marked.
56 pub(crate) gap: bool,
57 /// A gap is picked or the playhead stands in one ([`Player::gap_here`]).
58 pub(crate) gap_here: bool,
59 /// Some clip is under the playhead.
60 pub(crate) clip_here: bool,
61 /// Transcript words are picked and the transcript is up to date, so the words can be cut.
62 pub(crate) words: bool,
63 /// A render preview is being made ([`Player::render_preview`]), so there is one to stop.
64 pub(crate) rendering: bool,
65 /// A library item is selected (or was last previewed) to edit from.
66 pub(crate) asset: bool,
67 /// What was clicked is a placed caption and not a clip: a subtitle lane
68 /// holds no `Clip` at all, so [`Ctx::clip`] is `None` there and the
69 /// clip-relative answers would read it as "nothing marked". Its own flag,
70 /// so the one action a caption has -- coming off the lane again -- is a
71 /// live row while the rest are not rows at all.
72 pub(crate) caption: bool,
73 /// The clip plays a still ([`engine::is_image`]), which has no sound to
74 /// reach at all -- not the lane's business, because a still sits on a video
75 /// lane exactly like a take whose sound is one lane down.
76 pub(crate) image: bool,
77 /// The clip's source is known, from the probed stream table
78 /// ([`Player::streams`]), to carry no audio track at all -- a video shot
79 /// mute, not merely a video whose sound is quiet. `false` (the default)
80 /// covers both "has sound" and "not probed yet": a row nobody has asked
81 /// about is not one this hides a working action from.
82 pub(crate) no_sound: bool,
83 pub(crate) playhead: u32,
84 /// A timeline is open.
85 pub(crate) timeline: bool,
86 /// Something has been copied.
87 pub(crate) clipboard: bool,
88 /// This timeline has at least one subtitle track: a toggle with nothing to
89 /// show is a switch that does nothing, and it says so rather than flipping.
90 pub(crate) subtitles: bool,
91 /// There is something on the lanes to play. A transport with nothing under
92 /// it is the one refusal that is about the timeline's contents rather than
93 /// about a clip.
94 pub(crate) playable: bool,
95 pub(crate) exporting: bool,
96 /// How many placements the selection holds, and on how many distinct
97 /// lanes -- the manual group's two questions in one reading. Picks that
98 /// name nothing (a stroke moved the indices under them) count as none.
99 pub(crate) picks: usize,
100 pub(crate) pick_lanes: usize,
101 /// The group id of the marked caption, when what is marked is one: the
102 /// detach question, asked over a box the clip index space cannot name.
103 pub(crate) caption_link: Option<u32>,
104 /// What the question is about sits on a locked lane: the clip the menu was
105 /// opened on or the marked one, or the caption. The edits that would change
106 /// it are refused with [`LOCKED_REASON`] (DESIGN §7); the answers that only
107 /// read it are not.
108 pub(crate) locked: bool,
109 /// The edit history is empty on that side: nothing for undo to take back,
110 /// nothing for redo to walk forward. Negative on purpose, so a default
111 /// `Ctx` keeps both live and only a real reading dims them.
112 pub(crate) nothing_to_undo: bool,
113 pub(crate) nothing_to_redo: bool,
114 /// The clip's group holds no other placement (its link names only itself),
115 /// so there is nothing for Ungroup to take it out of. Negative like the
116 /// two above.
117 pub(crate) alone: bool,
118 /// No lane has two halves of one take meeting at the playhead, so Regroup
119 /// would find no cut to close. Negative like the above.
120 pub(crate) no_join: bool,
121 /// No clip on another track covers exactly this clip's frames, so a lone
122 /// Group has no partner to take it to. Negative like the above.
123 pub(crate) no_partner: bool,
124 /// The clip the question is about plays another sequence (a compound clip).
125 pub(crate) compound: bool,
126 /// The clip plays a multicam source ([`engine::project::Project::multicam_of`]).
127 pub(crate) multicam: bool,
128 /// How many library rows are picked, and whether the angle monitor is on.
129 pub(crate) media_picks: usize,
130 pub(crate) monitor: bool,
131 /// The angle count of the multicam clip under the playhead; 0 with none there.
132 pub(crate) angles_here: usize,
133 /// How many sequences the project holds, and whether the open one is played
134 /// by a compound clip somewhere (so cannot be deleted).
135 pub(crate) sequences: usize,
136 pub(crate) sequence_used: bool,
137 /// The open sequence was entered from a compound clip: there is a parent to go back to.
138 pub(crate) parent: bool,
139}
140
141/// The refusal every edit to a locked lane's item carries, in the words the
142/// toast and the dimmed row both say.
143pub(crate) const LOCKED_REASON: &str = "The track is locked";
144
145/// Whether `action` changes the clip or caption it is asked about: the ones a
146/// lock refuses. Selection, playback, the playhead and the editor's own
147/// switches are not in it -- a locked lane still plays and still reads.
148fn edits_placement(action: ActionId) -> bool {
149 matches!(
150 action,
151 ActionId::Cut
152 | ActionId::Regroup
153 | ActionId::Detach
154 | ActionId::Group
155 | ActionId::Delete
156 | ActionId::Lift
157 | ActionId::Paste
158 | ActionId::PasteAttributes
159 | ActionId::PasteColor
160 | ActionId::PasteEffects
161 | ActionId::PasteTransform
162 | ActionId::PasteSpeed
163 | ActionId::PasteAudio
164 | ActionId::NudgeBack
165 | ActionId::NudgeForward
166 | ActionId::NudgeBack10
167 | ActionId::NudgeForward10
168 | ActionId::ExtendAudio
169 | ActionId::ExtendVideo
170 | ActionId::Crossfade
171 | ActionId::Dissolve
172 | ActionId::TransitionDissolve
173 | ActionId::TransitionDipBlack
174 | ActionId::TransitionDipWhite
175 | ActionId::TransitionWipeLeft
176 | ActionId::TransitionWipeRight
177 | ActionId::TransitionWipeUp
178 | ActionId::TransitionWipeDown
179 | ActionId::TransitionSlideLeft
180 | ActionId::TransitionSlideRight
181 | ActionId::TransitionSlideUp
182 | ActionId::TransitionSlideDown
183 | ActionId::TransitionPushLeft
184 | ActionId::TransitionPushRight
185 | ActionId::TransitionPushUp
186 | ActionId::TransitionPushDown
187 | ActionId::TransitionZoom
188 | ActionId::Color
189 | ActionId::Fit
190 | ActionId::Reframe
191 | ActionId::TrackMask
192 | ActionId::NestClips
193 | ActionId::BreakApart
194 | ActionId::Transform
195 | ActionId::Effects
196 | ActionId::KeyPlacement
197 | ActionId::KeyOpacity
198 | ActionId::KeyColor
199 | ActionId::KeyEasing
200 | ActionId::Equalizer
201 | ActionId::Silence
202 | ActionId::Reverse
203 | ActionId::SpeedRampUp
204 | ActionId::SpeedRampDown
205 | ActionId::SpeedRampSlowFastSlow
206 | ActionId::SpeedRampFastSlowFast
207 | ActionId::SpeedRampKey
208 | ActionId::SpeedRampClear
209 | ActionId::Mix
210 | ActionId::CleanVoice
211 | ActionId::VoiceDynamics
212 | ActionId::TrimIn
213 | ActionId::TrimOut
214 | ActionId::TrimInToPlayhead
215 | ActionId::TrimOutToPlayhead
216 | ActionId::RippleTrimInToPlayhead
217 | ActionId::RippleTrimOutToPlayhead
218 | ActionId::RollBack
219 | ActionId::RollForward
220 | ActionId::SlipBack
221 | ActionId::SlipForward
222 | ActionId::SlideBack
223 | ActionId::SlideForward
224 | ActionId::LoopTrim
225 )
226}
227
228/// Whether `action` can be asked for, on `ctx`. One arm per action and nothing
229/// else in the editor asks the question: the clip menu dims a row with this,
230/// the actions card dims a row with this, and the two can never come to
231/// disagree about what an action needs -- exactly the reason [`Player::act`] is
232/// one table too.
233pub(crate) fn enable(action: ActionId, ctx: Ctx) -> Enable {
234 if action == ActionId::CancelRender && !ctx.rendering {
235 return Enable::No("nothing is rendering");
236 }
237 if action == ActionId::CutSelectedWords && !ctx.words {
238 return Enable::No("no transcript words picked");
239 }
240 // The one action that is about the editor rather than about the timeline:
241 // the list of what everything does, and where a key is changed. An empty
242 // window has no clips and still has keys -- so this answers ahead of the
243 // timeline question, and only an export shuts it (a waiting row would
244 // swallow the escape the progress line promises cancels the export).
245 // The window's own colours: there is always a window, and repainting it
246 // touches no timeline -- so this one is live even while an export is
247 // reading one, which is the only state that dims the list above.
248 // ...and whether that window fills the screen: also always there, also
249 // touching no timeline, live through an export the same way.
250 // ...and the cue plate's own font and size: a reading preference like the
251 // theme, never burned into an export, live the same way.
252 //
253 // Settings is not in this list, on purpose: it opens the same modal slot
254 // the exporting card owns (`Player::open_settings`, one card at a time),
255 // so a permission this oracle granted here would be a permission nobody
256 // could ever observe. It is refused below, with every other card.
257 if action == ActionId::Theme
258 || action == ActionId::AccentColour
259 || action == ActionId::Fullscreen
260 || action == ActionId::SubtitleStyle
261 // Moving the keyboard ring is the same kind of always-there,
262 // touches-no-timeline action as the three above: there is always a
263 // dock/bench/inspector to land the ring on, export or no.
264 || action == ActionId::FocusPanels
265 // The shortcuts sheet is the same kind of door: it opens and closes
266 // a tab and touches no timeline, so it is live through an export too.
267 || action == ActionId::ShowKeys
268 || action == ActionId::About
269 // The viewer's own look (zoom, playback quality, guides, mask): view state, no timeline.
270 || action == ActionId::ViewZoom
271 || action == ActionId::PlaybackQuality
272 || action == ActionId::GuideStyle
273 || action == ActionId::GuideMask
274 // The video output window and its display: a second window onto the picture, no timeline.
275 || action == ActionId::ToggleOutputWindow
276 || action == ActionId::OutputDisplay
277 {
278 return Enable::Yes;
279 }
280 if action == ActionId::Settings {
281 return match ctx.exporting {
282 true => Enable::No("an export is running"),
283 false => Enable::Yes,
284 };
285 }
286 if action == ActionId::ShowActions {
287 return match ctx.exporting {
288 true => Enable::No("an export is running"),
289 false => Enable::Yes,
290 };
291 }
292 // The six that are about the *editor* and its monitoring rather than
293 // about the edit list: they work with nothing open, the keyboard has
294 // always fired them there, and so their buttons are live there too.
295 //
296 // The two stand-in switches are here because they are *import* options:
297 // what an arriving film gets made for it, and what the picture is decoded
298 // from once it has. A person who wants no encode started decides that
299 // before the first file, and a switch that could only be set once a film
300 // was already in was a switch that came too late to mean anything.
301 if matches!(
302 action,
303 ActionId::ToggleSnap
304 | ActionId::ToggleMute
305 | ActionId::VolumeUp
306 | ActionId::VolumeDown
307 | ActionId::ToggleProxies
308 | ActionId::ToggleAutoProxies
309 // The project settings decide what the *next* file scaffolds as
310 // (`Player::pending_settings`), so refusing them before the first
311 // file was refusing them exactly when they mean the most.
312 | ActionId::Resolution
313 // The dock's import pair: how a first file gets in at all, so
314 // both are live with no timeline open, exactly like the two
315 // stand-in switches above.
316 | ActionId::AddFiles
317 | ActionId::PasteFilePath
318 // The way into a project from nothing: live exactly when there is none.
319 | ActionId::NewProject
320 // Opening a project file replaces whatever is open, so it is live with or without one.
321 | ActionId::OpenProject
322 ) {
323 return match ctx.exporting {
324 true => Enable::No("an export is running"),
325 false => Enable::Yes,
326 };
327 }
328 // Export writes what is on the timeline, so an empty one has nothing to write. The
329 // export that is already running answers for itself below (its progress button stays live).
330 if action == ActionId::Export && !ctx.exporting && !ctx.playable {
331 return Enable::No("the timeline is empty");
332 }
333 if !ctx.timeline {
334 return Enable::No("no timeline open");
335 }
336 // An export is reading the edit list every other action would change, which
337 // is the rule the key handler already follows.
338 if ctx.exporting {
339 return match action {
340 ActionId::CancelExport => Enable::Yes,
341 _ => Enable::No("an export is running"),
342 };
343 }
344 // A lock is the one refusal about the item itself rather than the editor:
345 // the item is there and can be looked at, only the edits that would change
346 // it are turned away, and say why in the toast's own words.
347 if ctx.locked && edits_placement(action) {
348 return Enable::No(LOCKED_REASON);
349 }
350 // A caption is not a clip: the actions that act on *the marked clip and
351 // nothing else* have nothing to act on over a placed subtitle, so they are
352 // refusals of kind -- not rows at all, rather than rows saying "click a clip
353 // first" over a clicked caption. Delete is the one that means something: it
354 // routes to [`Player::lift_sub`] when what is marked is a caption, and so
355 // are Group and Detach now that a caption may be grouped by hand -- the one
356 // over the selection the menu was opened on, the other over the group the
357 // marked caption is in.
358 //
359 // And it is *only* those. The playhead actions are about the timeline rather
360 // than about what is marked -- the split and the regroup act at the line
361 // ([`Player::cut`], [`Player::regroup`]), and the grade, the rate, the fit
362 // and the scan fall back to the clip under it -- so a caption in hand is no
363 // reason for them to stop. Hiding them here made them dead *and* silent
364 // ([`Player::act`] returns without a word on `Hidden`) from the moment a
365 // caption was clicked until something else was, which no click on the bed
366 // undoes: there is no handler there to clear the mark.
367 if ctx.caption
368 && matches!(
369 action,
370 ActionId::Copy
371 | ActionId::Delete
372 | ActionId::Lift
373 | ActionId::Equalizer
374 | ActionId::NestClips
375 | ActionId::BreakApart
376 | ActionId::OpenCompound
377 )
378 {
379 return match action {
380 ActionId::Delete => Enable::Yes,
381 _ => Enable::Hidden("this is a caption"),
382 };
383 }
384 // A compound clip plays another sequence: its own rate, grade, placement and the scans of a
385 // file are not applied to it (grade the clips inside), so those rows are not offered.
386 if ctx.compound
387 && ctx.clip.is_some()
388 && matches!(
389 action,
390 ActionId::Speed
391 | ActionId::SpeedRampUp
392 | ActionId::SpeedRampDown
393 | ActionId::SpeedRampSlowFastSlow
394 | ActionId::SpeedRampFastSlowFast
395 | ActionId::SpeedRampKey
396 | ActionId::SpeedRampClear
397 | ActionId::Reverse
398 | ActionId::Color
399 | ActionId::AutoColour
400 | ActionId::SaveLook
401 | ActionId::Transform
402 | ActionId::Effects
403 | ActionId::KeyPlacement
404 | ActionId::KeyOpacity
405 | ActionId::KeyColor
406 | ActionId::KeyEasing
407 | ActionId::Fit
408 | ActionId::Equalizer
409 | ActionId::Silence
410 | ActionId::Visualizer
411 | ActionId::Reframe
412 | ActionId::TrackMask
413 | ActionId::Mix
414 | ActionId::CleanVoice
415 | ActionId::VoiceDynamics
416 )
417 {
418 return Enable::Hidden("a compound clip plays a sequence");
419 }
420 match action {
421 ActionId::Undo if ctx.nothing_to_undo => return Enable::No("nothing to undo"),
422 ActionId::Redo if ctx.nothing_to_redo => return Enable::No("nothing to redo"),
423 _ => {}
424 }
425 match action {
426 // -- class: what kind of thing the action is about. The equalizer
427 // filters samples, and a video clip has none of its own here: the sound
428 // is the audio lane's, clip for clip.
429 // The crossfade is the sound join, for the same reason: it fades one
430 // clip's samples into the next one's, and a picture has none.
431 ActionId::Equalizer | ActionId::Crossfade => match ctx.clip {
432 Some((_, lane)) if lane.kind != LaneKind::Audio => {
433 Enable::Hidden("this clip is picture")
434 }
435 _ => Enable::Yes,
436 },
437 // A grade is a picture setting and an audio clip has no picture. A fit
438 // policy is a picture setting for the same reason.
439 // The dissolve is the picture join and the crossfade's mirror: two
440 // pictures cross, and there is no picture on a waveform to cross.
441 a if matches!(
442 a,
443 ActionId::Color
444 | ActionId::Fit
445 | ActionId::Transform
446 | ActionId::Effects
447 | ActionId::KeyPlacement
448 | ActionId::KeyOpacity
449 | ActionId::KeyColor
450 | ActionId::KeyEasing
451 | ActionId::Dissolve
452 ) || a.transition_kind().is_some() =>
453 {
454 match ctx.clip {
455 Some((_, lane)) if lane.kind != LaneKind::Video => {
456 Enable::Hidden("this clip is sound")
457 }
458 _ => Enable::Yes,
459 }
460 }
461 // The scan reads samples, and a still has none -- ever, unlike a video
462 // clip whose sound may be one lane down or simply silent. Exactly what
463 // `unscannable` says after the fact, said before the row is drawn so
464 // there is no row left to click. The mixer's track volumes are the
465 // same class refusal over the same clip: nothing to mix for a picture
466 // with no sound at all.
467 // Beats need a sound to listen to: a picture-only clip is refused by name, dimmed.
468 ActionId::MarkBeats if ctx.image || ctx.no_sound => Enable::No("Pick a clip with sound"),
469 ActionId::Silence | ActionId::Mix | ActionId::CleanVoice | ActionId::VoiceDynamics
470 if ctx.image => Enable::Hidden("this clip is a still"),
471 // A still has no footage to run the other way.
472 ActionId::Reverse if ctx.image => Enable::Hidden("this clip is a still"),
473 // Sequences: always there with a timeline, bar the cap and the rules
474 // the engine would refuse them by.
475 ActionId::NewSequence | ActionId::DuplicateSequence if ctx.sequences >= engine::project::MAX_SEQUENCES => {
476 Enable::No("the project holds as many sequences as it can")
477 }
478 ActionId::NewSequence | ActionId::DuplicateSequence | ActionId::RenameSequence => Enable::Yes,
479 ActionId::DeleteSequence if ctx.sequences <= 1 => Enable::No("this is the only sequence"),
480 ActionId::DeleteSequence if ctx.sequence_used => Enable::No("a compound clip plays this sequence"),
481 ActionId::DeleteSequence => Enable::Yes,
482 ActionId::CloseCompound if !ctx.parent => Enable::No("this sequence was not opened from a compound clip"),
483 ActionId::CloseCompound => Enable::Yes,
484 ActionId::ZoomToSelection if ctx.picks == 0 => Enable::No("Select a clip first"),
485 ActionId::NestClips if ctx.picks == 0 && ctx.clip.is_none() => Enable::No("select the clips to nest"),
486 ActionId::NestClips => Enable::Yes,
487 ActionId::CreateMulticam if ctx.media_picks < 2 => Enable::No("Needs two or more picked clips"),
488 ActionId::CreateMulticam => Enable::Yes,
489 ActionId::FlattenMulticam if ctx.multicam => Enable::Yes,
490 ActionId::FlattenMulticam => match ctx.clip {
491 None => Enable::No("select a multicam clip"),
492 Some(_) => Enable::Hidden("not a multicam clip"),
493 },
494 ActionId::ToggleAngleMonitor | ActionId::ToggleWaveforms | ActionId::ToggleScrubAudio | ActionId::CompareOff | ActionId::CompareSplit | ActionId::CompareSide => Enable::Yes,
495 // The number keys are the monitor's: silent anywhere else, so a digit is never eaten.
496 ActionId::Angle1
497 | ActionId::Angle2
498 | ActionId::Angle3
499 | ActionId::Angle4
500 | ActionId::Angle5
501 | ActionId::Angle6
502 | ActionId::Angle7
503 | ActionId::Angle8
504 | ActionId::Angle9
505 if !ctx.monitor || ctx.angles_here == 0 =>
506 {
507 Enable::Hidden("the angle monitor is off or no multicam clip is under the playhead")
508 }
509 ActionId::Angle1
510 | ActionId::Angle2
511 | ActionId::Angle3
512 | ActionId::Angle4
513 | ActionId::Angle5
514 | ActionId::Angle6
515 | ActionId::Angle7
516 | ActionId::Angle8
517 | ActionId::Angle9 => {
518 let n = action.angle_number().unwrap_or(0);
519 if n > ctx.angles_here { Enable::No("this multicam clip has fewer angles") } else { Enable::Yes }
520 }
521 ActionId::BreakApart | ActionId::OpenCompound => match ctx.clip {
522 None => Enable::No("select a compound clip"),
523 Some(_) if !ctx.compound => Enable::Hidden("not a compound clip"),
524 Some(_) => Enable::Yes,
525 },
526 // Tracking follows a mask's box through footage, so it needs a picture clip with a mask on it.
527 ActionId::TrackMask => match &ctx.clip {
528 Some((_, lane)) if lane.kind != LaneKind::Video => Enable::Hidden("this clip is sound"),
529 Some((c, _)) if !c.effects.iter().any(|e| matches!(e.kind, engine::effects::EffectKind::RectMask | engine::effects::EffectKind::EllipseMask)) => {
530 Enable::No("no rect or ellipse mask")
531 }
532 Some(_) => Enable::Yes,
533 None => Enable::No("select a video clip"),
534 },
535 // Reframing follows a subject through footage: a still has none to follow.
536 ActionId::Reframe => match ctx.clip {
537 Some((_, lane)) if lane.kind != LaneKind::Video => Enable::Hidden("this clip is sound"),
538 Some(_) if ctx.image => Enable::Hidden("this clip is a still"),
539 Some(_) => Enable::Yes,
540 None => Enable::No("select a video clip"),
541 },
542 ActionId::Visualizer if ctx.image => Enable::Hidden("this clip is a still"),
543 ActionId::Visualizer if ctx.no_sound => Enable::Hidden("this clip has no sound"),
544 ActionId::Visualizer => match ctx.clip {
545 Some(_) => Enable::Yes,
546 None => Enable::No("select a clip with sound"),
547 },
548 ActionId::Silence | ActionId::Mix | ActionId::CleanVoice | ActionId::VoiceDynamics
549 if ctx.no_sound => {
550 Enable::Hidden("this clip has no sound")
551 }
552 // -- state: true of this clip now, and the next playhead click or the
553 // next selection changes the answer. Splits this clip only from inside
554 // it: at either edge there is nothing to split off -- and, on a speeded
555 // clip, only at a frame its own rate can address, which is the same
556 // question `splittable` asks.
557 ActionId::Cut => match ctx.clip {
558 Some((clip, _)) if !(clip.start < ctx.playhead && ctx.playhead < clip.end()) => {
559 Enable::No("only from inside a clip")
560 }
561 // Inside it, and still not a cut: a slowed clip shows one frame of
562 // the file for several frames of the timeline, and only the first of
563 // those is a frame the file has ([`Speed::split_at`]). Cutting
564 // between two showings of one frame would leave halves whose lengths
565 // no longer add up, so it is refused -- and it says *that* rather
566 // than repeating "inside a clip" at a playhead that plainly is.
567 Some((clip, _)) if !clip.splits_at(ctx.playhead) => Enable::No(match clip.speed_keys.is_empty() {
568 true => "this speed holds one frame here",
569 false => "this speed ramp cannot be cut at this frame",
570 }),
571 _ => Enable::Yes,
572 },
573 // Rejoins whatever meets at the playhead, so it can mean something only
574 // at an edge of this clip. Whether those two halves were ever one take
575 // is the engine's question, and it words that refusal itself.
576 ActionId::Regroup => match ctx.clip {
577 Some((clip, _)) if ctx.playhead != clip.start && ctx.playhead != clip.end() => {
578 Enable::No("only where two clips meet")
579 }
580 Some(_) if ctx.no_join => Enable::No("no cut under the playhead to close"),
581 _ => Enable::Yes,
582 },
583 // Nothing to take apart in a placement that names no group at all --
584 // a clip by its own field, a caption by the one the mark carries.
585 // Whether the group it names still has another half is the engine's
586 // question, like the regroup above.
587 ActionId::Detach => match (ctx.clip, ctx.caption) {
588 (Some((clip, _)), _) if clip.link.is_none() || ctx.alone => {
589 Enable::No("this clip is not grouped")
590 }
591 (None, true) if ctx.caption_link.is_none() => Enable::No("this caption is not grouped"),
592 _ => Enable::Yes,
593 },
594 // The manual group: a *selection* of placements, at most one per lane.
595 // Two picks or more, all on their own lanes, is a group waiting to be
596 // made; a lane picked twice is the one thing a group may never hold;
597 // and a single pick is the partner hunt it always was, which the engine
598 // words itself.
599 ActionId::Group => match ctx.picks {
600 2.. if ctx.pick_lanes == ctx.picks => Enable::Yes,
601 2.. => Enable::No("a lane is picked twice"),
602 _ if ctx.clip.is_some() && ctx.no_partner => {
603 Enable::No("needs a clip on another track at the same time")
604 }
605 _ => Enable::Yes,
606 },
607 // The three that act on the marked clip and on nothing else: with none
608 // marked they would silently do nothing, which is what the Delete
609 // button's own dimming has always said.
610 // Delete over a picked gap closes the gap, so it is live with nothing
611 // marked (DESIGN §7); the clip-only rows beside it stay refused.
612 ActionId::Delete if ctx.gap => Enable::Yes,
613 ActionId::Copy | ActionId::Delete | ActionId::Lift if ctx.clip.is_none() => {
614 Enable::No("nothing selected")
615 }
616 // The cut machinery (DESIGN.md §6): the odometer, the trim pair and
617 // the loop-trim all act on the subject cut, and with none marked
618 // there is nothing to walk, trim or loop -- named the same way the
619 // three above it refuse an empty mark.
620 ActionId::WalkCutNext
621 | ActionId::WalkCutPrev
622 | ActionId::WalkCutNext10
623 | ActionId::WalkCutPrev10
624 | ActionId::TrimIn
625 | ActionId::TrimOut
626 | ActionId::LoopTrim
627 if ctx.clip.is_none() =>
628 {
629 Enable::No("no clip is selected")
630 }
631 // Slip shifts which part of the file a clip shows, and a still has no
632 // more of a file to show -- a class refusal like the scan's.
633 ActionId::SlipBack | ActionId::SlipForward if ctx.image => {
634 Enable::Hidden("this clip is a still")
635 }
636 // The pro trims (roll, slip, slide) act on the subject clip and its
637 // neighbours; whether a neighbour is there is the engine's question,
638 // and it words that refusal itself.
639 ActionId::RollBack
640 | ActionId::RollForward
641 | ActionId::SlipBack
642 | ActionId::SlipForward
643 | ActionId::SlideBack
644 | ActionId::SlideForward
645 if ctx.clip.is_none() =>
646 {
647 Enable::No("no clip is selected")
648 }
649 // Trim-to-playhead (debt #42): the keyboard's own version of the
650 // pointer's drag-the-edge-to-a-spot trim, so it needs the two things
651 // that gesture needs -- a subject cut, and the playhead actually
652 // resting somewhere on it, or the "spot" the edge would snap to is
653 // not on the clip at all.
654 ActionId::TrimInToPlayhead
655 | ActionId::TrimOutToPlayhead
656 | ActionId::RippleTrimInToPlayhead
657 | ActionId::RippleTrimOutToPlayhead => match ctx.clip {
658 None => Enable::No("no clip is selected"),
659 Some((clip, _)) if ctx.playhead < clip.start || ctx.playhead > clip.end() => {
660 Enable::No("the playhead is off this clip")
661 }
662 _ => Enable::Yes,
663 },
664 ActionId::Paste if !ctx.clipboard => Enable::No("nothing copied yet"),
665 ActionId::CloseGap if !ctx.gap_here => Enable::No("no gap under the playhead"),
666 ActionId::Freeze | ActionId::MatchFrame if !ctx.clip_here => Enable::No("nothing under the playhead"),
667 ActionId::ReplaceEdit if !ctx.clip_here => Enable::No("nothing under the playhead"),
668 ActionId::ReplaceEdit if !ctx.asset => Enable::No("no library item selected"),
669 ActionId::PasteAttributes
670 | ActionId::PasteColor
671 | ActionId::PasteEffects
672 | ActionId::PasteTransform
673 | ActionId::PasteSpeed
674 | ActionId::PasteAudio
675 if !ctx.clipboard =>
676 {
677 Enable::No("nothing copied yet")
678 }
679 ActionId::PasteAttributes
680 | ActionId::PasteColor
681 | ActionId::PasteEffects
682 | ActionId::PasteTransform
683 | ActionId::PasteSpeed
684 | ActionId::PasteAudio
685 | ActionId::NudgeBack
686 | ActionId::NudgeForward
687 | ActionId::NudgeBack10
688 | ActionId::NudgeForward10
689 if ctx.picks == 0 && ctx.clip.is_none() =>
690 {
691 Enable::No("no clips selected")
692 }
693 ActionId::ExtendAudio | ActionId::ExtendVideo => {
694 let want = if action == ActionId::ExtendAudio { LaneKind::Audio } else { LaneKind::Video };
695 match ctx.clip {
696 None => Enable::No("no clip selected"),
697 Some((_, lane)) if lane.kind == want => Enable::Yes,
698 Some((clip, _)) if clip.link.is_none() || ctx.alone => Enable::No(match want {
699 LaneKind::Audio => "select the sound clip, or group it with the picture",
700 _ => "select the picture clip, or group it with the sound",
701 }),
702 Some(_) => Enable::Yes,
703 }
704 }
705 // Nothing to draw over the picture, so nothing to switch off: the
706 // library says how subtitles arrive, and this row would flip a state
707 // with no visible half either way.
708 //
709 // The × on a palette row is refused by this very fact and asks this very
710 // arm ([`Player::remove_subtitle_track`]): an empty list is a list with
711 // no row to take off, said in the same words the toggle says it in.
712 ActionId::ToggleSubtitles if !ctx.subtitles => Enable::No("no subtitles yet"),
713 // Not `No`: with nothing running there is no export for this to be
714 // about at all, which is what keeps `esc` -- the same key -- a quiet
715 // way out of a card rather than a line about exports.
716 ActionId::CancelExport => Enable::Hidden("nothing is exporting"),
717 // A clock started against an empty timeline is a clock counting
718 // nothing: the transport says so by being dim, which is what its own
719 // ad-hoc boolean used to say before the oracle knew the question.
720 // Play is refused while an export runs (`toggle_or_restart`), and so
721 // are the shuttle's forward and pause: the same clock, the same reason.
722 ActionId::ShuttleStop | ActionId::ShuttleBack | ActionId::ShuttleForward if ctx.exporting => {
723 Enable::No("an export is running")
724 }
725 ActionId::Play | ActionId::Loop | ActionId::PlayAround | ActionId::ShuttleBack | ActionId::ShuttleForward
726 if !ctx.playable => {
727 Enable::No("the timeline is empty")
728 }
729 // A rate applies to a clip of either kind and to its whole group, so
730 // there is no lane it means nothing on, and the engine words the one
731 // refusal there is (no room). Everything else is the editor's own and
732 // needs nothing but a timeline.
733 _ => Enable::Yes,
734 }
735}
736
737/// Whether a lane-header verb applies to this particular lane as well as to
738/// the editor state. The shared action oracle still owns timeline/export
739/// refusal; this narrow gate prevents a header from borrowing a type-level
740/// chord for the wrong kind of lane.
741pub(crate) fn enable_lane(action: ActionId, lane: Lane, ctx: Ctx) -> Enable {
742 match (action, lane.kind) {
743 (ActionId::Mix, LaneKind::Audio)
744 | (ActionId::RemoveVideoLane, LaneKind::Video)
745 | (ActionId::RemoveAudioLane, LaneKind::Audio) => enable(action, ctx),
746 (ActionId::RenameLane, _) => Enable::Yes,
747 (ActionId::TrackSmall | ActionId::TrackMedium | ActionId::TrackLarge, _) => Enable::Yes,
748 _ => Enable::Hidden("this verb does not apply to this lane"),
749 }
750}
751
752/// The rows a lane head's menu draws: the three tracks a project can gain,
753/// and -- under the rule line the render draws before it ([`destructive`]) --
754/// the one remove that means anything on the lane that was right-clicked. A
755/// video head never offers to take an audio track off: DESIGN §9's "verbs of
756/// the thing under the cursor", read literally.
757///
758/// The removes take the *last* track of the kind (the engine's own rule,
759/// which their labels say), so this lists at most one of them and never a
760/// second row arguing about the same act.
761pub(crate) fn lane_items(lane: Lane) -> Vec<ActionId> {
762 let mut items = HEAD_ADDS.to_vec();
763 // The verbs that used to hang off the head itself (cleanse round 2): the
764 // head is a dot and a name now, and this menu is where the pointer reaches
765 // its mix -- an audio lane's alone, the way `enable_lane` reads it.
766 if lane.kind == LaneKind::Audio {
767 items.push(ActionId::Mix);
768 }
769 items.push(ActionId::RenameLane);
770 // This track's own height, three radio rows: the same three sizes the
771 // Timeline menu sets for every track at once (`overlays` words them for one).
772 items.extend([ActionId::TrackSmall, ActionId::TrackMedium, ActionId::TrackLarge]);
773 items.push(match lane.kind {
774 LaneKind::Video => ActionId::RemoveVideoLane,
775 LaneKind::Audio => ActionId::RemoveAudioLane,
776 LaneKind::Subtitle => ActionId::RemoveSubtitleLane,
777 });
778 items
779}
780
781/// The rows a clip menu draws, for the clip it was opened on: the registry
782/// filtered by the one availability oracle, and the *only* way that menu is
783/// built. An action that means nothing for what was right-clicked -- a grade on
784/// a waveform, an equalizer on a picture -- is not a row at all, so a future
785/// action cannot appear where it does not apply by being added to
786/// [`MENU_ITEMS`] alone.
787///
788/// Being *live* and *belonging in this menu* are two different questions, and
789/// only this one is the menu's. The playhead actions stay live over a marked
790/// caption -- [`enable`] says so, and the strokes and the toolbar read that --
791/// but a split, a rate, a grade or a fit listed under a right-clicked caption
792/// reads as something the caption is about to get, when what they reach is the
793/// video clip the playhead is standing in. So a caption's menu carries the rows
794/// that are the caption's -- its removal ([`Player::lift_sub`], reached through
795/// Delete) and, since a caption may be grouped by hand, the group pair over the
796/// selection and its own group -- and the rows the card already prints
797/// "(global)" beside, which say for themselves that they are not about what was
798/// clicked.
799pub(crate) fn menu_items(ctx: Ctx) -> Vec<ActionId> {
800 MENU_ITEMS
801 .into_iter()
802 .filter(|&action| enable(action, ctx.clone()).listed())
803 .filter(|&action| {
804 !ctx.caption
805 || matches!(
806 action,
807 ActionId::Delete
808 | ActionId::Paste
809 | ActionId::ToggleMute
810 | ActionId::Group
811 | ActionId::Detach
812 )
813 })
814 .collect()
815}
816
817/// What a library row's items are asked *about*: the file that was
818/// right-clicked and the little of the editor's state the answers need. The
819/// library's [`Ctx`], handed in for the same reason.
820#[derive(Clone, Copy, Default)]
821pub(crate) struct RowCtx {
822 /// A timeline to put it on.
823 pub(crate) timeline: bool,
824 pub(crate) exporting: bool,
825 /// This row can join *this* timeline: what greys it in the list, and what
826 /// the engine would otherwise refuse the Add with after the click.
827 pub(crate) usable: bool,
828 /// How many clips play this exact row -- a source with any is one the
829 /// engine will not take out of the list.
830 pub(crate) placed: usize,
831 /// A stand-in is being made or wound down, so the proxy row reads "Stop
832 /// proxy" and its click stops it ([`RowItem::label_in`]).
833 pub(crate) proxy_stops: bool,
834 /// The source is starred, so the favourite row reads "Remove from favourites".
835 pub(crate) starred: bool,
836 /// The file is gone from disk: it can only be relinked, not played or added.
837 pub(crate) offline: bool,
838 /// The library rows picked for a multicam clip ([`Player::picked_sources`]).
839 pub(crate) media_picks: usize,
840}
841
842/// Whether a library row's item can be asked for. The library's half of
843/// [`enable`], and the same rule: one table, no second policy in the render.
844pub(crate) fn row_enable(item: RowItem, ctx: RowCtx) -> Enable {
845 match item {
846 RowItem::Relink if !ctx.offline => Enable::Hidden("the file is not missing"),
847 RowItem::Relink if ctx.exporting => Enable::No("an export is running"),
848 RowItem::Relink => Enable::Yes,
849 RowItem::CreateMulticam if ctx.media_picks < 2 => Enable::No("Needs two or more picked clips"),
850 RowItem::CreateMulticam => Enable::Yes,
851 // A file that is not on disk has nothing to play, add or copy.
852 RowItem::Add | RowItem::Insert | RowItem::Preview | RowItem::Proxy if ctx.offline => {
853 Enable::No("the file is missing")
854 }
855 // The two that change the timeline, so an export reading it stops them
856 // both -- the key handler's rule, applied to a menu.
857 RowItem::Favourite | RowItem::MoveToBin | RowItem::NewBin if ctx.exporting => {
858 Enable::No("an export is running")
859 }
860 RowItem::Favourite | RowItem::MoveToBin | RowItem::NewBin if !ctx.timeline => {
861 Enable::No("no timeline open")
862 }
863 RowItem::Add | RowItem::Insert | RowItem::Remove | RowItem::RemoveWithClips if ctx.exporting => {
864 Enable::No("an export is running")
865 }
866 RowItem::Add | RowItem::Insert | RowItem::Remove | RowItem::RemoveWithClips if !ctx.timeline => {
867 Enable::No("no timeline open")
868 }
869 // Dimmed and saying why rather than clicked and refused afterwards: the
870 // row's own grey already says the file cannot join this timeline.
871 RowItem::Add | RowItem::Insert if !ctx.usable => Enable::No("it cannot join this one"),
872 // A file the engine cannot read has nothing to play and nothing to
873 // make a stand-in from: hidden rather than dimmed, the way the two
874 // removes are, because neither verb has a state that would light it.
875 RowItem::Preview | RowItem::Proxy if !ctx.usable => Enable::Hidden("it cannot be read"),
876 // The two removes are listed together, always (DESIGN §6.7): the plain
877 // one is refused while a clip plays the file, and the clips-too one is
878 // refused while none does, each saying why in the column where its
879 // chord would be. Dimmed rather than hidden, so the pointer can see
880 // both ways out and which one applies right now.
881 RowItem::Remove if ctx.placed > 0 => Enable::No("It's used on the timeline"),
882 RowItem::RemoveWithClips if ctx.placed == 0 => Enable::No("no clip uses it"),
883 // Neither of these touches the timeline: a file can be found on disk and
884 // described whatever the editor is doing, with no timeline at all.
885 _ => Enable::Yes,
886 }
887}
888
889/// The rows a library menu draws, for the row it was opened on -- the clip
890/// menu's [`menu_items`] on the other panel, and the only way that menu is
891/// built.
892pub(crate) fn row_items(ctx: RowCtx) -> Vec<RowItem> {
893 ROW_ITEMS
894 .into_iter()
895 .filter(|&item| row_enable(item, ctx).listed())
896 .collect()
897}
898
899/// How tall a menu's list may draw: the whole of it where the window has room,
900/// and what the window has where it has not -- only then does the list scroll.
901/// A cap fixed at twelve rows put the last items behind a scroll on a window
902/// with room to spare, which reads as a menu cut off by the bottom edge. Both
903/// [`MENU_EDGE`] margins come off the room first, so the plate `menu_at` places
904/// from this always fits between them whatever the anchor.
905pub(crate) fn menu_rows_h(rows: usize, viewport: Size<Pixels>) -> f32 {
906 let room = f32::from(viewport.height) - MENU_PAD * 2. - MENU_EDGE * 2.;
907 (rows as f32 * MENU_ROW_H).min(room.max(MENU_ROW_H))
908}
909
910/// Where the menu actually hangs: at the pointer, pulled back inside the window
911/// when it would not fit -- an item off the bottom edge is an item nobody can
912/// click. All four edges, [`MENU_EDGE`] clear of each: a plate flush on the
913/// frame reads as a list that was cut, not one that ended. Never past the top
914/// or left edge, so a window smaller than the menu loses the bottom of it
915/// rather than the top, where the items are.
916#[cfg(test)]
917pub(crate) fn menu_at(at: Point<Pixels>, viewport: Size<Pixels>, height: f32) -> (f32, f32) {
918 menu_at_w(at, viewport, MENU_W, height)
919}
920
921/// [`menu_at`] for a plate that fits its content and so is `width` wide.
922pub(crate) fn menu_at_w(
923 at: Point<Pixels>,
924 viewport: Size<Pixels>,
925 width: f32,
926 height: f32,
927) -> (f32, f32) {
928 let fit = |v: f32, size: f32, room: f32| v.min(room - MENU_EDGE - size).max(MENU_EDGE);
929 (
930 fit(f32::from(at.x), width, f32::from(viewport.width)),
931 fit(f32::from(at.y), height, f32::from(viewport.height)),
932 )
933}