forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

sequences.rs287 lines, 11.4 KiBHistoryRawDownload
1//! Sequences and compound clips on the window's side: the tab strip's verbs (new, duplicate,
2//! rename, delete, switch), nesting the selection into a compound clip, breaking one apart,
3//! opening one (double-click) and going back to its parent. The model is the engine's
4//! ([`engine::project`]'s sequences); every change here is one undo step through the session,
5//! except switching, which edits nothing.
6
7use crate::*;
8use engine::project::{MAX_SEQUENCE_NAME_CHARS, sequence_id_of};
9
10impl Player {
11 /// The sequence ids the strip draws, in tab order, with the open one marked.
12 pub(crate) fn sequence_tabs(&self) -> Vec<engine::project::SequenceInfo> {
13 let mut tabs = self.session.as_ref().map(|s| s.sequences()).unwrap_or_default();
14 // A sequence with no name still has a tab worth reading: it is called by its place.
15 for (i, tab) in tabs.iter_mut().enumerate() {
16 if tab.name.trim().is_empty() {
17 tab.name = format!("Sequence {}", i + 1);
18 }
19 }
20 tabs
21 }
22
23 /// Opens sequence `id` for editing. What the old one had selected names nothing in the new
24 /// one, and an edit being retyped is given up. Not an undo step.
25 fn show_sequence(&mut self, id: u32, cx: &mut Context<Self>) -> bool {
26 let Some(session) = self.session.as_mut() else {
27 return false;
28 };
29 if let Err(e) = session.switch_sequence(id) {
30 self.notify_user(format!("Not opened — {e}").into());
31 cx.notify();
32 return false;
33 }
34 self.selected.clear();
35 self.seq_edit = None;
36 self.gap = None;
37 self.reset_after_reseek();
38 cx.notify();
39 true
40 }
41
42 /// A tab clicked: that sequence opens, and the trail back through compound clips starts over.
43 pub(crate) fn switch_sequence_tab(&mut self, id: u32, cx: &mut Context<Self>) {
44 if self.exporting().is_some() {
45 return;
46 }
47 if self.session.as_ref().is_some_and(|s| s.active_sequence() != id) && self.show_sequence(id, cx) {
48 self.mark_structural();
49 self.seq_trail.clear();
50 }
51 }
52
53 /// Makes a new empty sequence and opens it.
54 pub(crate) fn new_sequence(&mut self, cx: &mut Context<Self>) {
55 let Some(session) = self.session.as_mut() else {
56 return;
57 };
58 match session.new_sequence(None) {
59 Ok(id) => {
60 self.mark_structural();
61 self.seq_trail.clear();
62 if self.show_sequence(id, cx) {
63 let name = self.session.as_ref().and_then(|s| s.sequence_name(id)).unwrap_or_default();
64 self.notify_user(format!("New sequence “{name}”").into());
65 }
66 }
67 Err(e) => self.notify_user(format!("No sequence made — {e}").into()),
68 }
69 cx.notify();
70 }
71
72 /// Copies the open sequence and opens the copy.
73 pub(crate) fn duplicate_sequence(&mut self, cx: &mut Context<Self>) {
74 let Some(session) = self.session.as_mut() else {
75 return;
76 };
77 match session.duplicate_sequence(session.active_sequence()) {
78 Ok(id) => {
79 self.mark_structural();
80 self.seq_trail.clear();
81 if self.show_sequence(id, cx) {
82 let name = self.session.as_ref().and_then(|s| s.sequence_name(id)).unwrap_or_default();
83 self.notify_user(format!("Duplicated as “{name}”").into());
84 }
85 }
86 Err(e) => self.notify_user(format!("Not duplicated — {e}").into()),
87 }
88 cx.notify();
89 }
90
91 /// Deletes the open sequence; the engine picks the one that opens in its place.
92 pub(crate) fn delete_sequence(&mut self, cx: &mut Context<Self>) {
93 let Some(session) = self.session.as_mut() else {
94 return;
95 };
96 let (id, name) = (session.active_sequence(), session.sequence_name(session.active_sequence()));
97 match session.delete_sequence(id) {
98 Ok(_) => {
99 self.selected.clear();
100 self.seq_edit = None;
101 self.seq_trail.retain(|&t| t != id);
102 self.reset_after_reseek();
103 self.notify_user_action(
104 format!("Deleted “{}”", name.unwrap_or_default()).into(),
105 crate::notices::ToastAction::Undo,
106 );
107 }
108 Err(e) => self.notify_user(format!("Not deleted — {e}").into()),
109 }
110 cx.notify();
111 }
112
113 /// Starts retyping the open sequence's name on its tab, from the name it has.
114 pub(crate) fn begin_seq_edit(&mut self, cx: &mut Context<Self>) {
115 let Some(session) = self.session.as_ref() else {
116 return;
117 };
118 self.seq_edit = session.sequence_name(session.active_sequence()).map(crate::player::text_field::TextField::selected);
119 cx.notify();
120 }
121
122 /// Writes what was typed as the sequence's name (one undo step). An empty name is refused
123 /// out loud and the old one stays; an unchanged one is no edit.
124 pub(crate) fn commit_seq_edit(&mut self, cx: &mut Context<Self>) {
125 let Some(field) = self.seq_edit.take() else {
126 return;
127 };
128 let text = field.text;
129 let Some(session) = self.session.as_mut() else {
130 return;
131 };
132 let id = session.active_sequence();
133 let text = text.trim().to_string();
134 if text.is_empty() {
135 self.notify_user(format!("A sequence needs a name (up to {MAX_SEQUENCE_NAME_CHARS} characters)").into());
136 } else if session.sequence_name(id).as_deref() != Some(text.as_str()) {
137 match session.rename_sequence(id, &text) {
138 Ok(()) => self.mark_dirty(),
139 Err(e) => self.notify_user(format!("Not renamed — {e}").into()),
140 }
141 }
142 cx.notify();
143 }
144
145 /// The keyboard while a name is being retyped (ahead of the palette and every chord): enter
146 /// saves, escape gives up, the rest is text.
147 pub(crate) fn seq_edit_key(
148 &mut self,
149 key: &str,
150 ctrl: bool,
151 alt: bool,
152 key_char: Option<&str>,
153 cx: &mut Context<Self>,
154 ) {
155 use crate::player::text_field::FieldKey;
156 let Some(field) = self.seq_edit.as_mut() else {
157 return;
158 };
159 match field.key(key, ctrl, alt, key_char, MAX_SEQUENCE_NAME_CHARS) {
160 FieldKey::Cancel => self.seq_edit = None,
161 FieldKey::Commit => self.commit_seq_edit(cx),
162 FieldKey::Edited => {}
163 }
164 cx.notify();
165 }
166
167 /// Folds the selected clips (and their linked partners) into a new sequence and leaves one
168 /// compound clip in their place. One undo step.
169 pub(crate) fn nest_selected(&mut self, cx: &mut Context<Self>) {
170 if self.exporting().is_some() {
171 return;
172 }
173 let mut picks: Vec<(Lane, usize)> =
174 self.marks().0.into_iter().filter(|(lane, _)| lane.kind != LaneKind::Subtitle).collect();
175 if picks.is_empty() {
176 picks.extend(self.selected.anchor().filter(|(lane, _)| lane.kind != LaneKind::Subtitle));
177 }
178 if let Some(lane) = picks.iter().map(|p| p.0).find(|&l| self.session.as_ref().is_some_and(|s| s.lane_locked(l))) {
179 self.lane_locked_refusal(lane);
180 return;
181 }
182 let Some(session) = self.session.as_mut() else {
183 return;
184 };
185 match session.nest(&picks, None) {
186 Ok(made) => {
187 self.mark_structural();
188 self.selected.clear();
189 if let Some(&first) = made.clips.first() {
190 self.selected.set_one(first);
191 }
192 for &pick in made.clips.iter().skip(1) {
193 self.selected.add(pick);
194 }
195 let name = self.session.as_ref().and_then(|s| s.sequence_name(made.sequence)).unwrap_or_default();
196 self.notify_user(format!("Nested into “{name}”").into());
197 }
198 Err(e) => self.notify_user(format!("Not nested — {e}").into()),
199 }
200 self.reset_after_reseek();
201 cx.notify();
202 }
203
204 /// Puts the selected compound clips' contents back on the timeline. One undo step.
205 pub(crate) fn break_apart_selected(&mut self, cx: &mut Context<Self>) {
206 if self.exporting().is_some() {
207 return;
208 }
209 let picks: Vec<(Lane, usize)> = {
210 let mut p = self.marks().0;
211 if p.is_empty() {
212 p.extend(self.selected.anchor());
213 }
214 p
215 };
216 let Some(session) = self.session.as_mut() else {
217 return;
218 };
219 let compounds: Vec<(Lane, usize)> = picks
220 .into_iter()
221 .filter(|&(l, i)| l.kind != LaneKind::Subtitle && session.compound_at(l, i).is_some())
222 .collect();
223 if compounds.is_empty() {
224 self.notify_user("Not broken apart — select a compound clip".into());
225 cx.notify();
226 return;
227 }
228 if let Some(lane) = compounds.iter().map(|p| p.0).find(|&l| session.lane_locked(l)) {
229 self.lane_locked_refusal(lane);
230 return;
231 }
232 match session.break_apart(&compounds) {
233 Ok(clips) => {
234 self.selected.clear();
235 if let Some(&first) = clips.first() {
236 self.selected.set_one(first);
237 }
238 for &pick in clips.iter().skip(1) {
239 self.selected.add(pick);
240 }
241 self.notify_user(format!("Broke apart into {}", crate::notices::count_noun(clips.len(), "clip", "clips")).into());
242 }
243 Err(e) => self.notify_user(format!("Not broken apart — {e}").into()),
244 }
245 self.reset_after_reseek();
246 cx.notify();
247 }
248
249 /// Opens the selected compound clip's sequence, remembering where it came from.
250 pub(crate) fn open_compound_selected(&mut self, cx: &mut Context<Self>) {
251 if let Some((lane, idx)) = self.selected.anchor() {
252 self.open_compound(lane, idx, cx);
253 }
254 }
255
256 /// Opens the sequence the compound clip at `(lane, idx)` plays. A clip that is not a
257 /// compound is left alone (a double-click on any clip lands here).
258 pub(crate) fn open_compound(&mut self, lane: Lane, idx: usize, cx: &mut Context<Self>) {
259 if self.exporting().is_some() {
260 return;
261 }
262 let Some(session) = self.session.as_ref() else {
263 return;
264 };
265 let Some(id) = session.lane_clips(lane).get(idx).and_then(|c| sequence_id_of(c.source)) else {
266 return;
267 };
268 let here = session.active_sequence();
269 if self.show_sequence(id, cx) {
270 self.seq_trail.push(here);
271 // The playhead was the parent's: the compound opens at its own start, not at the
272 // parent's time (which can lie past the end of the sequence just entered).
273 self.seek(0., cx);
274 }
275 }
276
277 /// Back to the sequence the open one was entered from. Parents that have since gone are skipped.
278 pub(crate) fn close_compound(&mut self, cx: &mut Context<Self>) {
279 let alive: Vec<u32> = self.sequence_tabs().iter().map(|q| q.id).collect();
280 while let Some(parent) = self.seq_trail.pop() {
281 if alive.contains(&parent) {
282 self.show_sequence(parent, cx);
283 return;
284 }
285 }
286 }
287}