forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

toolbar.rs425 lines, 17.8 KiBHistoryRawDownload
1//! The edit toolbar (REDESIGN §3.4, DESIGN.md §6.4): the row of editing commands
2//! that sits at the top of the bench, directly above the lanes. `stance.rs::bench()`
3//! mounts it and spends its height out of the bench's own box, so nothing is
4//! reserved twice: the seam drag, the floor and the lanes all read the one
5//! [`TOOLBAR_H`].
6//!
7//! Groups, left to right, separated by 1px rules: split, delete and lift; undo
8//! and redo; snap and the two marks. Then, pushed right, zoom out, a zoom
9//! slider, zoom in and Fit. Export lives on the menu bar now.
10//!
11//! Every button is an icon and a word (Split, Delete, Lift, Undo, Redo, Snap,
12//! In, Out, Fit) -- never a key: the key is in the tooltip, read from the
13//! keymap. Below [`COMPACT_BELOW`] the second and third groups drop their words
14//! and keep their icons, so nothing is ever clipped. A button the oracle
15//! refuses is drawn dim with the reason as its tooltip; a click still goes
16//! through [`Player::act`], which says the same reason as a notice.
17
18use crate::layout::{SEQ_BAR_H, SEQ_TAB_H, TOOLBAR_BTN_H, TOOLBAR_H, TOOLBAR_ROW_H};
19use crate::ui::hitmap;
20use crate::ui::icons::Icon;
21use crate::ui::kit::{Button, ButtonSize, ButtonStyle};
22use crate::ui::timeband_stance;
23use crate::ui::type_scale::{self, Typeset};
24use crate::ui::widgets::{action_hover, tip_hover};
25use crate::*;
26
27/// The zoom slider's track, end to end, in px.
28const ZOOM_TRACK_W: f32 = 120.;
29/// The slider's thumb, square.
30const ZOOM_THUMB: f32 = 12.;
31
32/// Below this column width the second and third groups (undo, redo, snap, the
33/// marks) keep only their icons (the word stays in the tooltip), so the zoom
34/// group always fits. Measured at 1280x720 (column 958 px): all words need
35/// about 1010 px, the compact row about 730.
36const COMPACT_BELOW: f32 = 900.;
37
38/// Below this the zoom slider's track is [`ZOOM_TRACK_TIGHT_W`] wide.
39const TIGHT_BELOW: f32 = 1100.;
40const ZOOM_TRACK_TIGHT_W: f32 = 60.;
41
42/// Whether the column is narrow enough that buttons shed their key.
43pub(crate) fn compact() -> bool {
44 let w = timeband_stance::column_w();
45 w > 0. && w < COMPACT_BELOW
46}
47
48/// The zoom track's width at the column's current width.
49/// One recorded box per tab (the anchor registry is keyed by static names), so a tab's menu can
50/// hang from the tab that was pressed.
51const SEQ_TAB_ANCHORS: [&str; 8] = ["seq-tab-0", "seq-tab-1", "seq-tab-2", "seq-tab-3", "seq-tab-4", "seq-tab-5", "seq-tab-6", "seq-tab-7"];
52
53fn zoom_track_w() -> f32 {
54 let w = timeband_stance::column_w();
55 if w > 0. && w < TIGHT_BELOW { ZOOM_TRACK_TIGHT_W } else { ZOOM_TRACK_W }
56}
57
58/// The drag payload that carries a zoom-slider gesture. Only its type matters:
59/// `on_drag_move::<ZoomDrag>` is how the slider hears the pointer after the
60/// press, with no state kept on [`Player`] for it.
61#[derive(Clone)]
62struct ZoomDrag;
63
64/// The toolbar region: the button row over the sequence strip, full width of the centre column.
65pub(crate) fn render(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
66 div()
67 .id("stance-toolbar-region")
68 .flex_none()
69 .w_full()
70 .h(px(TOOLBAR_H))
71 .flex()
72 .flex_col()
73 .child(row(player, cx))
74 .child(sequence_strip(player, cx))
75}
76
77/// The sequence strip (REDESIGN §3.5): one tab per sequence, the open one marked
78/// with a 2px accent underline, a plus for a new one, and the way back out of a
79/// compound clip with the path in. A fixed height whatever it holds, drawn with
80/// one sequence too, so nothing moves when a second appears.
81fn sequence_strip(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
82 let t = crate::ui::theme::tokens();
83 let tabs = player.sequence_tabs();
84 let name_of = |id: u32| tabs.iter().find(|q| q.id == id).map(|q| q.name.clone());
85 let back = player.enable(ActionId::CloseCompound, None).yes();
86 let current = tabs.iter().find(|q| q.active).map(|q| q.name.clone()).unwrap_or_default();
87 let path: Vec<String> = player
88 .seq_trail
89 .iter()
90 .filter_map(|&id| name_of(id))
91 .chain(std::iter::once(current))
92 .collect();
93 let typing = player.seq_edit.clone();
94 div()
95 .id("stance-sequences")
96 .flex_none()
97 .w_full()
98 .h(px(SEQ_BAR_H))
99 .flex()
100 .items_center()
101 .gap(px(2.))
102 .px(px(8.))
103 .bg(rgb(t.e1))
104 .border_t_1()
105 .border_color(rgba(t.line))
106 // The way back out of a compound: a labelled "Back" control, drawn only inside one, with
107 // the sequence it returns to named in its tooltip. Its slot is always there, so the strip
108 // never shifts when one opens.
109 .child(
110 div()
111 .flex_none()
112 .w(px(78.))
113 .h(px(24.))
114 .mr(px(4.))
115 .when(back, |d| {
116 d.child(
117 Button::new("seq-back")
118 .style(ButtonStyle::Ghost)
119 .size(ButtonSize::Small)
120 .icon(Icon::ChevronLeft)
121 .label("Back")
122 .build()
123 .tooltip(action_hover(player, ActionId::CloseCompound))
124 .on_click(cx.listener(|this, _: &ClickEvent, window, cx| {
125 this.act(ActionId::CloseCompound, window, cx);
126 }))
127 .children(hitmap::action(ActionId::CloseCompound, true)),
128 )
129 }),
130 )
131 .child(
132 div()
133 .id("seq-tabs")
134 .flex_shrink()
135 .min_w(px(0.))
136 .h_full()
137 .flex()
138 .items_center()
139 .gap(px(2.))
140 .overflow_hidden()
141 .children(tabs.iter().enumerate().map(|(ti, q)| {
142 let id = q.id;
143 let active = q.active;
144 // The tab being renamed is a text field in the tab's own place: a plate with
145 // the accent edge, the name selected (all of it) or with the caret after it.
146 let editing = typing.clone().filter(|_| active);
147 let name = q.name.clone();
148 // The 2px underline is always there (transparent when inactive), so
149 // a tab switch moves nothing.
150 div()
151 .id(("seq-tab", id as usize))
152 .relative()
153 .flex_none()
154 .flex()
155 .items_center()
156 .max_w(px(180.))
157 .h(px(SEQ_TAB_H.max(28.)))
158 .px(px(10.))
159 .border_b_2()
160 .border_color(if active { rgb(t.accent) } else { rgba(0) })
161 .cursor_pointer()
162 .when(!active, |d| d.hover(|s| s.bg(rgba(crate::ui::theme::WASH_HOVER))))
163 .type_style(type_scale::body_strong())
164 .text_color(rgb(if active { t.ink1 } else { t.ink2 }))
165 .tooltip(tip_hover(
166 "Sequence",
167 "",
168 Some(if active { "Double-click to rename" } else { "Click to open" }),
169 ))
170 .on_mouse_down(
171 MouseButton::Right,
172 cx.listener(move |this, event: &MouseDownEvent, _, cx| {
173 this.open_seq_tab_menu(id, event.position, cx);
174 cx.stop_propagation();
175 }),
176 )
177 .on_click(cx.listener(move |this, event: &ClickEvent, _, cx| {
178 if event.click_count() >= 2 && this.seq_edit.is_none() {
179 this.switch_sequence_tab(id, cx);
180 this.begin_seq_edit(cx);
181 } else {
182 this.switch_sequence_tab(id, cx);
183 }
184 }))
185 .children(hitmap::dynamic(
186 move || (format!("seq-tab.{id}"), format!("Sequence {name}")),
187 true,
188 ))
189 .child(crate::ui::anchor::probe(SEQ_TAB_ANCHORS[ti.min(SEQ_TAB_ANCHORS.len() - 1)]))
190 .child(match editing {
191 Some(field) => seq_name_field(&field).into_any_element(),
192 None => div().min_w(px(0.)).truncate().child(q.name.clone()).into_any_element(),
193 })
194 })),
195 )
196 .child(
197 Button::new("seq-new")
198 .style(ButtonStyle::Ghost)
199 .size(ButtonSize::Small)
200 .icon(Icon::Plus)
201 .build()
202 .tooltip(action_hover(player, ActionId::NewSequence))
203 .on_click(cx.listener(|this, _: &ClickEvent, window, cx| {
204 this.act(ActionId::NewSequence, window, cx);
205 }))
206 .children(hitmap::action(ActionId::NewSequence, true)),
207 )
208 .child(div().flex_1().min_w(px(0.)))
209 .when(path.len() > 1, |d| {
210 d.child(
211 div()
212 .flex_none()
213 .pl(px(10.))
214 .type_style(type_scale::caption())
215 .text_color(rgb(t.ink3))
216 .child(path.join(" › ")),
217 )
218 })
219}
220
221/// A name while it is being typed: a text field in the name's own place, with the accent edge the
222/// other fields wear, the name selected when it is all selected (a rename that has not been typed
223/// into yet) and the caret after it otherwise. Shared by the sequence tabs and the markers card.
224pub(crate) fn seq_name_field(field: &crate::player::text_field::TextField) -> impl IntoElement {
225 let t = crate::ui::theme::tokens();
226 let text = match field.all {
227 true => div()
228 .rounded(px(2.))
229 .bg(rgba(crate::ui::theme::with_alpha(t.accent, 0x66)))
230 .child(field.text.clone())
231 .into_any_element(),
232 false => format!("{}\u{258f}", field.text).into_any_element(),
233 };
234 div()
235 .flex_none()
236 .min_w(px(96.))
237 .max_w(px(168.))
238 .h(px(24.))
239 .px(px(6.))
240 .flex()
241 .items_center()
242 .rounded(px(4.))
243 .border_1()
244 .border_color(rgb(t.accent))
245 .bg(rgb(t.e2))
246 .type_style(type_scale::body())
247 .text_color(rgb(t.ink1))
248 .child(div().min_w(px(0.)).truncate().child(text))
249}
250
251/// The toolbar's button row.
252fn row(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
253 let t = crate::ui::theme::tokens();
254 let words = !compact();
255 div()
256 .id("stance-toolbar")
257 .flex_none()
258 .w_full()
259 .h(px(TOOLBAR_ROW_H))
260 .flex()
261 .items_center()
262 .gap(px(4.))
263 .px(px(8.))
264 .bg(rgb(t.e1))
265 // Group one: split, delete, lift. Their words stay at every width.
266 .child(button("tb-split", player, ActionId::Cut, Icon::Scissors, Some("Split"), false, cx))
267 .child(button("tb-delete", player, ActionId::Delete, Icon::Trash2, Some("Delete"), false, cx))
268 .child(button("tb-lift", player, ActionId::Lift, Icon::Eraser, Some("Lift"), false, cx))
269 .child(rule())
270 // Group two: undo, redo.
271 .child(button("tb-undo", player, ActionId::Undo, Icon::Undo2, words.then_some("Undo"), false, cx))
272 .child(button("tb-redo", player, ActionId::Redo, Icon::Redo2, words.then_some("Redo"), false, cx))
273 .child(rule())
274 // Group three: snap, whose state is the tint and fill (the word never changes), and the marks.
275 .child(button("tb-snap", player, ActionId::ToggleSnap, Icon::Magnet, words.then_some("Snap"), player.snap, cx))
276 .child(button("tb-mark-in", player, ActionId::SetIn, Icon::SquareArrowDownRight, words.then_some("In"), false, cx))
277 .child(button("tb-mark-out", player, ActionId::SetOut, Icon::SquareArrowDownLeft, words.then_some("Out"), false, cx))
278 // Pushed right: the zoom group and Fit.
279 .child(div().flex_1().min_w(px(0.)))
280 .child(button("tb-zoom-out", player, ActionId::ZoomOut, Icon::ZoomOut, None, false, cx))
281 .child(zoom_slider(player, cx))
282 .child(button("tb-zoom-in", player, ActionId::ZoomIn, Icon::ZoomIn, None, false, cx))
283 .child(button("tb-fit", player, ActionId::ZoomFit, Icon::Scan, Some("Fit"), false, cx))
284}
285
286/// A 1px rule between two groups, 16px tall with 8px of margin.
287fn rule() -> impl IntoElement {
288 div()
289 .flex_none()
290 .w(px(1.))
291 .h(px(16.))
292 .mx(px(4.))
293 .bg(rgba(crate::ui::theme::tokens().line))
294}
295
296/// One command: a ghost button with its icon and (at wide widths) its word, never
297/// its key. `on` is the toggle state (Snap): accent icon on an `e3` fill. Always
298/// dispatches through [`Player::act`] so a refused press still says why; draws
299/// dim when the oracle refuses it.
300fn button(
301 id: &'static str,
302 player: &Player,
303 action: ActionId,
304 icon: Icon,
305 word: Option<&'static str>,
306 on: bool,
307 cx: &mut Context<Player>,
308) -> impl IntoElement {
309 let yes = player.enable(action, None).yes();
310 let icon_only = word.is_none();
311 let mut b = Button::new(id)
312 .style(ButtonStyle::Ghost)
313 .size(if icon_only && matches!(action, ActionId::ZoomIn | ActionId::ZoomOut) {
314 ButtonSize::Small
315 } else {
316 ButtonSize::Large
317 })
318 .icon(icon)
319 .on(on)
320 .enabled(yes);
321 if let Some(word) = word {
322 b = b.label(word);
323 }
324 b.build()
325 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
326 this.act(action, window, cx);
327 }))
328 .tooltip(toolbar_tip(player, action))
329 .children(hitmap::action(action, yes))
330}
331
332/// The hover line a toolbar button wears. Delete and Lift say what they leave behind in the
333/// toolbar's own words; every other button has its action's name, as ever.
334fn toolbar_tip(player: &Player, action: ActionId) -> impl Fn(&mut Window, &mut App) -> gpui::AnyView + use<> {
335 let name = match action {
336 ActionId::Delete => "Remove and close the gap".to_string(),
337 ActionId::Lift => "Remove and leave a gap".to_string(),
338 _ => player.action_name(action),
339 };
340 tip_hover(&name, &player.keymap.display(action), player.enable(action, None).why())
341}
342
343/// Where a share of the slider, 0 at the far-out stop and 1 at the far-in one,
344/// sits in pixels per second. The scale is logarithmic because a zoom is a
345/// multiplier: each equal step of the thumb is the same factor of magnification.
346fn zoom_at(min: f64, max: f64, share: f64) -> f64 {
347 min * (max / min).powf(share.clamp(0., 1.))
348}
349
350/// The inverse of [`zoom_at`]: which share of the slider a given zoom sits at.
351fn share_of(min: f64, max: f64, pps: f64) -> f64 {
352 if max <= min {
353 return 0.;
354 }
355 ((pps / min).ln() / (max / min).ln()).clamp(0., 1.)
356}
357
358/// Zooms the timeline so that the slider reads `share`. Goes through
359/// [`Player::zoom`], the one zoom door, so the stops clamp it exactly as they do
360/// a key press, and the playhead stays where it was.
361fn zoom_to_share(player: &mut Player, share: f32, cx: &mut Context<Player>) {
362 let view = player.view();
363 let Some((min, max)) = view.stops() else {
364 return;
365 };
366 let target = zoom_at(min, max, f64::from(share));
367 let factor = target / view.scale.pps;
368 player.zoom(factor as f32, None, cx);
369}
370
371/// The zoom slider: a thumb on a hairline track, showing the timeline's current
372/// zoom on every render, and dragged to set it. A bed that has never been
373/// painted has no stops yet, so the slider is drawn dim and takes no drag.
374fn zoom_slider(player: &Player, cx: &mut Context<Player>) -> impl IntoElement {
375 let view = player.view();
376 let stops = view.stops();
377 let share = match stops {
378 Some((min, max)) => share_of(min, max, view.settled().pps) as f32,
379 None => 0.,
380 };
381 let thumb_x = share * (zoom_track_w() - ZOOM_THUMB);
382 let mut track = div()
383 .id("tb-zoom-slider")
384 .relative()
385 .flex_none()
386 .w(px(zoom_track_w()))
387 .h(px(TOOLBAR_BTN_H))
388 .child(
389 div()
390 .absolute()
391 .left(px(0.))
392 .right(px(0.))
393 .top(px((TOOLBAR_BTN_H - 3.) / 2.))
394 .h(px(3.))
395 .rounded(px(2.))
396 .bg(rgb(crate::ui::theme::tokens().e3)),
397 )
398 .child(
399 div()
400 .absolute()
401 .left(px(thumb_x))
402 .top(px((TOOLBAR_BTN_H - ZOOM_THUMB) / 2.))
403 .size(px(ZOOM_THUMB))
404 .rounded(px(ZOOM_THUMB / 2.))
405 .bg(rgb(if stops.is_some() { crate::ui::theme::tokens().ink1 } else { crate::ui::theme::tokens().ink4 })),
406 );
407 if stops.is_some() {
408 track = track
409 .cursor_pointer()
410 .tooltip(tip_hover("Zoom", "", Some("Drag to zoom the timeline")))
411 .on_drag(ZoomDrag, |_, _, _, cx| cx.new(|_| gpui::Empty))
412 .on_drag_move(
413 cx.listener(move |this, event: &DragMoveEvent<ZoomDrag>, _, cx| {
414 // The thumb's centre follows the pointer, so the half
415 // thumb at each end is taken off before the share is
416 // worked out.
417 let at = f32::from(event.event.position.x)
418 - f32::from(event.bounds.left())
419 - ZOOM_THUMB / 2.;
420 zoom_to_share(this, at / (zoom_track_w() - ZOOM_THUMB), cx);
421 }),
422 );
423 }
424 track
425}