| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | use crate::ui::menubar::Title; |
| 9 | use engine::project::LaneKind; |
| 10 | use gpui::{Context, Timer}; |
| 11 | use std::time::Duration; |
| 12 | |
| 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 14 | pub(crate) enum Card { |
| 15 | Color, |
| 16 | Transform, |
| 17 | Speed, |
| 18 | Eq, |
| 19 | Mix, |
| 20 | Silence, |
| 21 | } |
| 22 | |
| 23 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 24 | pub(crate) enum Open { |
| 25 | Export, |
| 26 | Settings, |
| 27 | Palette, |
| 28 | Keys, |
| 29 | Markers, |
| 30 | Menu(Title), |
| 31 | Clip(Card), |
| 32 | } |
| 33 | |
| 34 | |
| 35 | pub(crate) fn parse(spec: &str) -> Vec<Open> { |
| 36 | spec.split(',') |
| 37 | .map(str::trim) |
| 38 | .filter(|v| !v.is_empty()) |
| 39 | .filter_map(|v| { |
| 40 | let found = match v.split_once(':') { |
| 41 | None => match v { |
| 42 | "export" => Some(Open::Export), |
| 43 | "settings" => Some(Open::Settings), |
| 44 | "palette" => Some(Open::Palette), |
| 45 | "keys" => Some(Open::Keys), |
| 46 | "markers" => Some(Open::Markers), |
| 47 | _ => None, |
| 48 | }, |
| 49 | Some(("menu", name)) => match name { |
| 50 | "file" => Some(Title::File), |
| 51 | "edit" => Some(Title::Edit), |
| 52 | "clip" => Some(Title::Clip), |
| 53 | "timeline" => Some(Title::Timeline), |
| 54 | "playback" => Some(Title::Playback), |
| 55 | "view" => Some(Title::View), |
| 56 | "help" => Some(Title::Help), |
| 57 | _ => None, |
| 58 | } |
| 59 | .map(Open::Menu), |
| 60 | Some(("clip", card)) => match card { |
| 61 | "color" => Some(Card::Color), |
| 62 | "transform" => Some(Card::Transform), |
| 63 | "speed" => Some(Card::Speed), |
| 64 | "eq" => Some(Card::Eq), |
| 65 | "mix" => Some(Card::Mix), |
| 66 | "silence" => Some(Card::Silence), |
| 67 | _ => None, |
| 68 | } |
| 69 | .map(Open::Clip), |
| 70 | _ => None, |
| 71 | }; |
| 72 | if found.is_none() { |
| 73 | eprintln!("EDITH_OPEN: unknown value {v:?}, skipped"); |
| 74 | } |
| 75 | found |
| 76 | }) |
| 77 | .collect() |
| 78 | } |
| 79 | |
| 80 | impl crate::Player { |
| 81 | |
| 82 | |
| 83 | pub(crate) fn start_open_hook(&mut self, wanted: Vec<Open>, cx: &mut Context<Self>) { |
| 84 | if wanted.is_empty() { |
| 85 | return; |
| 86 | } |
| 87 | cx.spawn(async move |this, cx| { |
| 88 | for _ in 0..300 { |
| 89 | Timer::after(Duration::from_millis(200)).await; |
| 90 | let ready = this |
| 91 | .update(cx, |p, _| { |
| 92 | p.importing.is_none() && p.opening.is_none() && p.imports.is_empty() |
| 93 | }) |
| 94 | .unwrap_or(true); |
| 95 | if ready { |
| 96 | break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | Timer::after(Duration::from_millis(400)).await; |
| 101 | this.update(cx, |p, cx| p.apply_open(&wanted, cx)).ok(); |
| 102 | }) |
| 103 | .detach(); |
| 104 | } |
| 105 | |
| 106 | fn apply_open(&mut self, wanted: &[Open], cx: &mut Context<Self>) { |
| 107 | for open in wanted { |
| 108 | match *open { |
| 109 | Open::Export => self.open_export(cx), |
| 110 | Open::Settings => self.open_settings(cx), |
| 111 | Open::Palette => self.show_actions(cx), |
| 112 | Open::Keys => self.show_keys(cx), |
| 113 | Open::Markers => self.open_markers(cx), |
| 114 | Open::Menu(title) => { |
| 115 | self.menu_open = Some(title); |
| 116 | cx.notify(); |
| 117 | } |
| 118 | Open::Clip(card) => { |
| 119 | let wants_sound = matches!(card, Card::Eq | Card::Mix | Card::Silence); |
| 120 | let pick = self.session.as_ref().and_then(|s| { |
| 121 | let lanes = s.lanes(); |
| 122 | let by_kind = |kind| { |
| 123 | lanes |
| 124 | .iter() |
| 125 | .copied() |
| 126 | .find(|l| l.kind == kind && !s.lane_clips(*l).is_empty()) |
| 127 | }; |
| 128 | let (first, second) = match wants_sound { |
| 129 | true => (LaneKind::Audio, LaneKind::Video), |
| 130 | false => (LaneKind::Video, LaneKind::Audio), |
| 131 | }; |
| 132 | by_kind(first).or_else(|| by_kind(second)) |
| 133 | }); |
| 134 | if let Some(lane) = pick { |
| 135 | self.select((lane, 0), cx); |
| 136 | } |
| 137 | |
| 138 | |
| 139 | self.dock_src_active = false; |
| 140 | match card { |
| 141 | Card::Color => self.open_color(cx), |
| 142 | Card::Transform => self.open_transform(cx), |
| 143 | Card::Speed => self.open_speed(cx), |
| 144 | Card::Eq => self.open_eq(cx), |
| 145 | Card::Mix => self.open_mix(pick, cx), |
| 146 | Card::Silence => self.open_silence(cx), |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | #[cfg(test)] |
| 155 | mod tests { |
| 156 | use super::*; |
| 157 | |
| 158 | #[test] |
| 159 | fn parses_plain_menu_and_clip_values() { |
| 160 | assert_eq!( |
| 161 | parse("export, settings,palette,keys,markers"), |
| 162 | [Open::Export, Open::Settings, Open::Palette, Open::Keys, Open::Markers] |
| 163 | ); |
| 164 | assert_eq!(parse("menu:clip"), [Open::Menu(Title::Clip)]); |
| 165 | assert_eq!( |
| 166 | parse("clip:color,clip:eq"), |
| 167 | [Open::Clip(Card::Color), Open::Clip(Card::Eq)] |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | #[test] |
| 172 | fn unknown_and_empty_values_are_skipped() { |
| 173 | assert!(parse("").is_empty()); |
| 174 | assert!(parse(",,").is_empty()); |
| 175 | assert_eq!(parse("bogus,menu:nope,clip:,palette"), [Open::Palette]); |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | #[test] |
| 181 | fn clip_cards_switch_the_dock_to_the_clip_tab() { |
| 182 | let src = include_str!("open_hook.rs"); |
| 183 | let branch = src |
| 184 | .split("Open::Clip(card) => {") |
| 185 | .nth(1) |
| 186 | .expect("the Clip branch"); |
| 187 | let before_card = branch.split("match card {").next().unwrap(); |
| 188 | assert!(before_card.contains("self.dock_src_active = false;")); |
| 189 | } |
| 190 | } |