forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

markers.rs339 lines, 13.1 KiBHistoryRawDownload
1//! The comment markers on the window's side: dropping one at the playhead, walking the
2//! playhead from marker to marker, and the markers card's rows (pick, retype, recolour,
3//! remove). The list itself is the project's ([`engine::project::Marker`]); every change
4//! is one undo step through the session, and a move of the playhead edits nothing.
5
6use crate::ui::widgets::MarkerField;
7use crate::*;
8use engine::project::{MAX_MARKER_NAME_CHARS, Marker};
9
10/// The name a marker dropped with the key gets: the lowest `Marker N` nobody in the list
11/// holds, so a run of them reads apart on the card, a renamed or removed one frees its
12/// number again, and a name never doubles up.
13pub(crate) fn default_marker_name<'a>(taken: impl IntoIterator<Item = &'a str>) -> String {
14 let taken: std::collections::HashSet<&str> = taken.into_iter().collect();
15 (1..)
16 .map(|n| format!("Marker {n}"))
17 .find(|name| !taken.contains(name.as_str()))
18 .unwrap_or_default()
19}
20
21/// The frame the next (`forward`) or previous marker sits on, strictly after or before
22/// `now`; `None` when there is none that way.
23pub(crate) fn marker_after(markers: &[Marker], now: u32, forward: bool) -> Option<u32> {
24 if forward {
25 markers.iter().map(|m| m.frame).find(|&f| f > now)
26 } else {
27 markers.iter().map(|m| m.frame).rev().find(|&f| f < now)
28 }
29}
30
31/// Why the engine refused a marker, in a plain sentence: the engine words its refusals for a log,
32/// and a toast is read by the person who pressed the key.
33pub(crate) fn marker_refusal(reason: &str) -> &'static str {
34 if reason.contains("needs a name") {
35 "The name is empty."
36 } else if reason.contains("name holds at most") {
37 "The name is too long."
38 } else if reason.contains("note holds at most") {
39 "The note is too long."
40 } else if reason.contains("runs past") {
41 "It would run past the end of the timeline."
42 } else {
43 "The timeline would not take it."
44 }
45}
46
47impl Player {
48 /// Drops a marker on the frame the playhead shows, named by its place in the list,
49 /// blue, and picked on the card. One undo step.
50 pub(crate) fn add_marker_here(&mut self, cx: &mut Context<Self>) {
51 let Some(session) = self.session.as_ref() else {
52 return;
53 };
54 let frame = crate::subs::frame_at(session.now(), self.fps);
55 self.add_marker_at(frame, cx);
56 }
57
58 /// "Add marker here" from a ruler or empty-track menu: at the frame that menu was opened on,
59 /// the playhead's frame when it was not.
60 pub(crate) fn add_marker_at_menu(&mut self, cx: &mut Context<Self>) {
61 match self.menu_frame {
62 Some(frame) => self.add_marker_at(frame, cx),
63 None => self.add_marker_here(cx),
64 }
65 }
66
67 /// Drops a marker at `frame` (see [`Self::add_marker_here`]).
68 pub(crate) fn add_marker_at(&mut self, frame: u32, cx: &mut Context<Self>) {
69 let fps = self.fps;
70 let Some(session) = self.session.as_mut() else {
71 return;
72 };
73 let marker = Marker {
74 frame,
75 length: 0,
76 name: default_marker_name(session.markers().iter().map(|m| m.name.as_str())),
77 note: String::new(),
78 color: Default::default(),
79 };
80 match session.add_marker(marker) {
81 Ok(idx) => {
82 self.marker_sel = idx;
83 self.mark_structural();
84 self.notify_user(format!("Marker added at {}", frames_timecode(frame, fps)).into());
85 }
86 Err(e) => self.notify_user(format!("No marker added. {}", marker_refusal(&e.to_string())).into()),
87 }
88 cx.notify();
89 }
90
91 /// Walks the playhead to the next (or previous) marker.
92 pub(crate) fn jump_marker(&mut self, forward: bool, cx: &mut Context<Self>) {
93 let Some(session) = self.session.as_ref() else {
94 return;
95 };
96 match marker_after(session.markers(), crate::subs::frame_at(session.now(), self.fps), forward) {
97 Some(frame) => {
98 let at = f64::from(frame) / self.fps;
99 self.seek(at, cx);
100 }
101 None => {
102 let way = if forward { "after" } else { "before" };
103 self.notify_user(format!("No marker {way} the playhead").into());
104 cx.notify();
105 }
106 }
107 }
108
109 /// Opens the markers card. Nothing here is a clip's, so nothing is refused for want
110 /// of a selection.
111 pub(crate) fn open_markers(&mut self, cx: &mut Context<Self>) {
112 if self.exporting().is_some() {
113 return;
114 }
115 // One card at a time -- `close_card`'s own rule.
116 self.close_other_cards();
117 self.markers_open = true;
118 let count = self.session.as_ref().map_or(0, |s| s.markers().len());
119 self.marker_sel = self.marker_sel.min(count.saturating_sub(1));
120 self.context_menu = None;
121 // Cards live in the Clip tab; every opening door routes the dock there.
122 self.dock_src_active = false;
123 crate::ui::dock_stance::save(false);
124 cx.notify();
125 }
126
127 /// Moves the card's pick by `by` rows, wrapping.
128 pub(crate) fn step_marker_pick(&mut self, by: isize, cx: &mut Context<Self>) {
129 let count = self.session.as_ref().map_or(0, |s| s.markers().len());
130 if count > 0 {
131 self.marker_sel = (self.marker_sel as isize + by).rem_euclid(count as isize) as usize;
132 }
133 cx.notify();
134 }
135
136 /// Puts the playhead on marker `idx`.
137 pub(crate) fn seek_marker(&mut self, idx: usize, cx: &mut Context<Self>) {
138 let Some(frame) = self.session.as_ref().and_then(|s| s.markers().get(idx).map(|m| m.frame)) else {
139 return;
140 };
141 self.marker_sel = idx;
142 let at = f64::from(frame) / self.fps;
143 self.seek(at, cx);
144 }
145
146 /// Sets marker `idx` to `color`, the one the colour list named. One undo step.
147 pub(crate) fn set_marker_color(&mut self, idx: usize, color: engine::project::MarkerColor, cx: &mut Context<Self>) {
148 let Some(session) = self.session.as_mut() else {
149 return;
150 };
151 let Some(mut marker) = session.markers().get(idx).cloned() else {
152 return;
153 };
154 marker.color = color;
155 if session.set_marker(idx, marker).is_ok() {
156 self.mark_dirty();
157 }
158 cx.notify();
159 }
160
161 /// Takes marker `idx` off. One undo step.
162 pub(crate) fn remove_marker_at(&mut self, idx: usize, cx: &mut Context<Self>) {
163 let Some(session) = self.session.as_mut() else {
164 return;
165 };
166 if session.remove_marker(idx) {
167 let count = session.markers().len();
168 self.marker_sel = self.marker_sel.min(count.saturating_sub(1));
169 self.marker_edit = None;
170 self.mark_dirty();
171 }
172 cx.notify();
173 }
174
175 /// The ruler's menu on marker `idx`: the marker is selected first, so its verbs act on it.
176 pub(crate) fn open_marker_menu(&mut self, idx: usize, at: Point<Pixels>, cx: &mut Context<Self>) {
177 if !self.menu_allowed() || self.session.is_none() {
178 return;
179 }
180 self.marker_sel = idx;
181 self.context_menu = Some(ContextMenu {
182 lane: Lane::V1,
183 on: MenuOn::Marker,
184 at,
185 details: false,
186 sub: Sub::Closed,
187 });
188 cx.notify();
189 }
190
191 /// Renames marker `idx` from the ruler (its double-click, or the menu's "Rename marker"):
192 /// the markers card opens with the name being typed on that row, where the name is edited.
193 pub(crate) fn rename_marker_at(&mut self, idx: usize, cx: &mut Context<Self>) {
194 self.context_menu = None;
195 self.begin_marker_edit(idx, MarkerField::Name, cx);
196 }
197
198 /// The menu's "Rename marker": the marker the menu was opened on.
199 pub(crate) fn rename_marker_at_pick(&mut self, cx: &mut Context<Self>) {
200 self.rename_marker_at(self.marker_sel, cx);
201 }
202
203 /// The menu's "Delete marker": the marker the menu was opened on. One undo step.
204 pub(crate) fn delete_marker_at_pick(&mut self, cx: &mut Context<Self>) {
205 self.remove_marker_at(self.marker_sel, cx);
206 }
207
208 /// Starts retyping marker `idx`'s name or note, from the words it has.
209 pub(crate) fn begin_marker_edit(&mut self, idx: usize, field: MarkerField, cx: &mut Context<Self>) {
210 let Some(marker) = self.session.as_ref().and_then(|s| s.markers().get(idx)) else {
211 return;
212 };
213 let seed = match field {
214 MarkerField::Name => marker.name.clone(),
215 MarkerField::Note => marker.note.clone(),
216 };
217 self.marker_sel = idx;
218 self.marker_edit = Some(MarkerEdit::new(idx, field, &seed));
219 cx.notify();
220 }
221
222 /// Writes what was typed into the marker, as one undo step; a name left empty is
223 /// refused out loud and the old one stays. Nothing typed that differs is no edit.
224 pub(crate) fn commit_marker_edit(&mut self, cx: &mut Context<Self>) {
225 let Some(edit) = self.marker_edit.take() else {
226 return;
227 };
228 let Some(session) = self.session.as_mut() else {
229 return;
230 };
231 let Some(mut marker) = session.markers().get(edit.idx).cloned() else {
232 return;
233 };
234 let text = edit.text.trim().to_string();
235 match edit.field {
236 MarkerField::Name if text.is_empty() => {
237 self.notify_user(format!("A marker needs a name (up to {MAX_MARKER_NAME_CHARS} characters)").into());
238 cx.notify();
239 return;
240 }
241 MarkerField::Name => marker.name = text,
242 MarkerField::Note => marker.note = text,
243 }
244 if session.set_marker(edit.idx, marker).is_ok() {
245 self.mark_dirty();
246 }
247 cx.notify();
248 }
249
250 /// The keyboard while a marker's words are being retyped (ahead of the palette and
251 /// every chord, as the cue editor is): enter saves, escape gives up, and the rest is
252 /// text. `key_char` is the character the layout made.
253 pub(crate) fn marker_edit_key(
254 &mut self,
255 key: &str,
256 ctrl: bool,
257 alt: bool,
258 key_char: Option<&str>,
259 cx: &mut Context<Self>,
260 ) {
261 match key {
262 "escape" => self.marker_edit = None,
263 "enter" => self.commit_marker_edit(cx),
264 "backspace" => {
265 if let Some(edit) = self.marker_edit.as_mut() {
266 edit.backspace();
267 }
268 }
269 "a" if ctrl && !alt => {
270 if let Some(edit) = self.marker_edit.as_mut() {
271 edit.all = true;
272 }
273 }
274 _ => {
275 if !ctrl
276 && !alt
277 && let Some(edit) = self.marker_edit.as_mut()
278 && let Some(chars) = key_char
279 {
280 for c in chars.chars() {
281 edit.typed(c);
282 }
283 }
284 }
285 }
286 cx.notify();
287 }
288
289 /// The markers card's own keys while it is up and nothing is being typed: up/down pick
290 /// a row, enter retypes the name and shift+enter the note. `true` once the card is up:
291 /// it owns the strokes it does not answer, like every card.
292 pub(crate) fn markers_card_key(&mut self, key: &str, shift: bool, cx: &mut Context<Self>) -> bool {
293 if !self.markers_open {
294 return false;
295 }
296 match key {
297 "escape" => self.markers_open = false,
298 "down" => self.step_marker_pick(1, cx),
299 "up" => self.step_marker_pick(-1, cx),
300 "enter" => {
301 let field = if shift { MarkerField::Note } else { MarkerField::Name };
302 self.begin_marker_edit(self.marker_sel, field, cx);
303 }
304 _ => {}
305 }
306 true
307 }
308}
309
310#[cfg(test)]
311mod tests {
312 use super::*;
313 use engine::project::MarkerColor;
314
315 fn at(frame: u32) -> Marker {
316 Marker { frame, length: 0, name: "m".into(), note: String::new(), color: MarkerColor::Blue }
317 }
318
319 #[test]
320 fn the_jump_goes_strictly_past_the_playhead_either_way() {
321 let list = [at(10), at(40), at(40), at(90)];
322 assert_eq!(marker_after(&list, 0, true), Some(10));
323 assert_eq!(marker_after(&list, 10, true), Some(40), "a marker on the playhead is passed");
324 assert_eq!(marker_after(&list, 40, true), Some(90));
325 assert_eq!(marker_after(&list, 90, true), None);
326 assert_eq!(marker_after(&list, 90, false), Some(40));
327 assert_eq!(marker_after(&list, 40, false), Some(10));
328 assert_eq!(marker_after(&list, 10, false), None);
329 assert_eq!(marker_after(&[], 5, true), None);
330 }
331
332 #[test]
333 fn a_dropped_marker_takes_the_lowest_free_number() {
334 assert_eq!(default_marker_name([]), "Marker 1", "the first one is Marker 1");
335 assert_eq!(default_marker_name(["Marker 1", "check colour"]), "Marker 2");
336 assert_eq!(default_marker_name(["check colour", "Marker 2"]), "Marker 1", "a free number is reused");
337 assert_eq!(default_marker_name(["Marker 1", "Marker 2", "Marker 3"]), "Marker 4");
338 }
339}