forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

sections.rs193 lines, 7.5 KiBHistoryRawDownload
1//! The Clip tab's sections (speed, colour, transform, effects, EQ, mix): several may be open at
2//! once, and which ones are open is remembered per clip kind (video, audio, caption), across
3//! selection changes and in the project's view file ([`crate::view_state`]). A dot on a section's
4//! head says its values differ from the defaults.
5
6use crate::*;
7
8/// One docked section of the Clip tab.
9#[derive(Clone, Copy, PartialEq, Eq, Debug)]
10pub(crate) enum Section {
11 Speed,
12 Color,
13 Transform,
14 Effects,
15 Eq,
16 Mix,
17}
18
19impl Section {
20 pub(crate) const ALL: [Section; 6] =
21 [Section::Speed, Section::Color, Section::Transform, Section::Effects, Section::Eq, Section::Mix];
22
23 /// The name the view file stores it by.
24 pub(crate) fn name(self) -> &'static str {
25 match self {
26 Section::Speed => "speed",
27 Section::Color => "color",
28 Section::Transform => "transform",
29 Section::Effects => "effects",
30 Section::Eq => "eq",
31 Section::Mix => "mix",
32 }
33 }
34
35 pub(crate) fn action(self) -> ActionId {
36 match self {
37 Section::Speed => ActionId::Speed,
38 Section::Color => ActionId::Color,
39 Section::Transform => ActionId::Transform,
40 Section::Effects => ActionId::Effects,
41 Section::Eq => ActionId::Equalizer,
42 Section::Mix => ActionId::Mix,
43 }
44 }
45
46 pub(crate) fn is_open(self, p: &Player) -> bool {
47 match self {
48 Section::Speed => p.speed_open.is_some(),
49 Section::Color => p.color_open.is_some(),
50 Section::Transform => p.transform_open.is_some(),
51 Section::Effects => p.effects_open.is_some(),
52 Section::Eq => p.eq_open.is_some(),
53 Section::Mix => p.mix_open,
54 }
55 }
56
57 /// Folds just this section; the others stay as they are.
58 pub(crate) fn close(self, p: &mut Player) {
59 match self {
60 Section::Speed => {
61 p.speed_open = None;
62 p.speed_dragging = false;
63 p.speed_ramp_mode = false;
64 p.ramp_key_speed = None;
65 }
66 Section::Color => {
67 p.color_open = None;
68 p.color_dragging = false;
69 p.color_wheel_drag = None;
70 }
71 Section::Transform => {
72 p.transform_open = None;
73 p.transform_dragging = false;
74 }
75 Section::Effects => {
76 p.effects_open = None;
77 p.effects_drag = None;
78 p.effects_draft = None;
79 p.effects_adding = false;
80 }
81 Section::Eq => {
82 p.eq_open = None;
83 p.eq_dragging = false;
84 }
85 Section::Mix => p.mix_open = false,
86 }
87 }
88
89 pub(crate) fn open(self, p: &mut Player, cx: &mut Context<Player>) {
90 match self {
91 Section::Speed => p.open_speed(cx),
92 Section::Color => p.open_color(cx),
93 Section::Transform => p.open_transform(cx),
94 Section::Effects => p.open_effects(cx),
95 Section::Eq => p.open_eq(cx),
96 Section::Mix => p.open_mix(None, cx),
97 }
98 }
99}
100
101impl Player {
102 /// The kind the selected clip is, for the sections' memory.
103 fn section_kind(&self) -> Option<&'static str> {
104 Some(match self.selected.anchor()?.0.kind {
105 LaneKind::Video => "video",
106 LaneKind::Audio => "audio",
107 LaneKind::Subtitle => "caption",
108 })
109 }
110
111 fn open_sections_now(&self) -> Vec<String> {
112 Section::ALL.iter().filter(|s| s.is_open(self)).map(|s| s.name().to_string()).collect()
113 }
114
115 /// Once a frame: while the same clip stays selected the open sections are what the kind
116 /// remembers; when another clip is selected, the sections fold and the ones that kind
117 /// remembers open on it.
118 pub(crate) fn sync_sections(&mut self, cx: &mut Context<Self>) {
119 let anchor = self.selected.anchor();
120 let (Some(kind), Some(anchor)) = (self.section_kind(), anchor) else {
121 self.sections_anchor = None;
122 return;
123 };
124 if self.exporting().is_some() || self.session.is_none() || self.settings_open || self.export_open {
125 return;
126 }
127 if self.sections_anchor == Some(anchor) {
128 let now = self.open_sections_now();
129 let remembered = self.sections_mem.iter().position(|(k, _)| k == kind);
130 let same = remembered.map_or(now.is_empty(), |i| self.sections_mem[i].1 == now);
131 if !same {
132 match remembered {
133 Some(i) => self.sections_mem[i].1 = now,
134 None => self.sections_mem.push((kind.to_string(), now)),
135 }
136 self.save_view_state();
137 }
138 return;
139 }
140 let first = self.sections_anchor.is_none();
141 self.sections_anchor = Some(anchor);
142 let want: Vec<String> =
143 self.sections_mem.iter().find(|(k, _)| k == kind).map(|(_, v)| v.clone()).unwrap_or_default();
144 // The first clip picked after a pause keeps what the hand left open on it.
145 if first && want.is_empty() && Section::ALL.iter().any(|s| s.is_open(self)) {
146 return;
147 }
148 if self.modal() && !self.docked_card_only() {
149 return;
150 }
151 for s in Section::ALL {
152 if s.is_open(self) {
153 s.close(self);
154 }
155 }
156 for s in Section::ALL {
157 if want.iter().any(|n| n == s.name()) && self.enable(s.action(), None).yes() {
158 s.open(self, cx);
159 }
160 }
161 cx.notify();
162 }
163
164 /// Whether the selected clip's values for `section` differ from the defaults (the dot on the head).
165 pub(crate) fn section_changed(&self, section: Section) -> bool {
166 let Some((lane, idx)) = self.selected.anchor() else { return false };
167 self.section_changed_at(lane, idx, section)
168 }
169
170 /// [`section_changed`](Self::section_changed) for any clip. A stored look that is the
171 /// do-nothing one, and the size a new shape is placed at, are not changes.
172 pub(crate) fn section_changed_at(&self, lane: Lane, idx: usize, section: Section) -> bool {
173 let Some(session) = self.session.as_ref() else { return false };
174 let Some(clip) = session.lane_clips(lane).get(idx) else { return false };
175 match section {
176 Section::Speed => clip.speed != Speed::NORMAL || !clip.speed_keys.is_empty() || clip.reversed,
177 Section::Color => {
178 session.color_of(lane, idx).is_some_and(|c| !c.is_identity()) || !clip.color_keys.is_empty()
179 }
180 Section::Transform => {
181 // A shape is placed at a smaller size on purpose; that is where it starts.
182 let start = session.generator_of(lane, idx).and_then(|g| g.default_scale());
183 let moved = session.transform_of(lane, idx).is_some_and(|t| {
184 !t.is_identity() && start.is_none_or(|scale| *t != TransformParams { scale, ..Default::default() })
185 });
186 moved || !clip.transform_keys.is_empty() || !clip.opacity_keys.is_empty()
187 }
188 Section::Effects => !clip.effects.is_empty(),
189 Section::Eq => session.eq_of(lane, idx).is_some_and(|e| !e.is_identity()),
190 Section::Mix => clip.gain_db != 0. || clip.pan != 0 || clip.pitch != 0.0 || !clip.gain_keys.is_empty() || clip.fade_in != 0 || clip.fade_out != 0,
191 }
192 }
193}