forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB978e685Tahinli3 months ago627 commits

widgets.rs389 lines, 15.2 KiBHistoryRawDownload
1//! The small elements every region is built out of.
2
3use crate::*;
4
5/// One clip's audio, drawn as a filled min/max envelope inside whatever box it
6/// is given. Peaks are the source's whole envelope; `from`/`to` are the source
7/// seconds this clip plays, so a cut clip shows its own stretch of the file.
8pub(crate) fn waveform(peaks: Arc<Vec<(f32, f32)>>, from: f64, to: f64) -> impl IntoElement {
9 waveform_ink(peaks, from, to, FG_SECONDARY())
10}
11
12/// As [`waveform`], but in a caller-picked ink rather than the fixed
13/// secondary foreground -- the darkroom bench's own audio clips draw their
14/// envelope in the source's ink (DESIGN §5), which the legacy timeline never
15/// needed a colour for.
16pub(crate) fn waveform_ink(peaks: Arc<Vec<(f32, f32)>>, from: f64, to: f64, ink: u32) -> impl IntoElement {
17 canvas(
18 |_, _, _| (),
19 move |bounds, _, window, _| {
20 let (o, s) = (bounds.origin, bounds.size);
21 let cols = envelope(&peaks, from, to, f32::from(s.width), f32::from(s.height));
22 if cols.len() < 2 {
23 return;
24 }
25 // Down the tops and back along the bottoms: one closed outline of
26 // the whole envelope, which is one path rather than a path a column.
27 let mut points: Vec<Point<Pixels>> = cols
28 .iter()
29 .map(|&(x, top, _)| point(o.x + px(x), o.y + px(top)))
30 .collect();
31 points.extend(
32 cols.iter()
33 .rev()
34 .map(|&(x, _, bottom)| point(o.x + px(x), o.y + px(bottom))),
35 );
36 let mut path = PathBuilder::fill();
37 path.add_polygon(&points, true);
38 if let Ok(path) = path.build() {
39 window.paint_path(path, rgb(ink));
40 }
41 },
42 )
43 .size_full()
44}
45
46/// An audio clip's fade, drawn as a translucent wedge over its box: silent
47/// (full shade) at the clip's own edge, clear by the fade's far side --
48/// [`is_in`] mirrors it for the tail's ramp-down. The same `canvas` +
49/// `PathBuilder` idiom [`waveform`] draws its envelope with, filled with the
50/// same wash the in/out range already paints the bed with
51/// (`ACCENT_WASH`, timeline.rs), so a fade reads as one more overlay on this
52/// bed rather than a shape borrowed from somewhere else.
53pub(crate) fn fade_wedge(is_in: bool) -> impl IntoElement {
54 canvas(
55 |_, _, _| (),
56 move |bounds, _, window, _| {
57 let (o, s) = (bounds.origin, bounds.size);
58 let (w, h) = (f32::from(s.width), f32::from(s.height));
59 if w <= 0. || h <= 0. {
60 return;
61 }
62 // A right triangle: the clip's silent corner (top of the clip's
63 // own edge) down to the bed and across to the fade's far side --
64 // the wedge shape every editor draws for a ramp.
65 let points = match is_in {
66 true => vec![
67 point(o.x, o.y),
68 point(o.x, o.y + px(h)),
69 point(o.x + px(w), o.y + px(h)),
70 ],
71 false => vec![
72 point(o.x + px(w), o.y),
73 point(o.x + px(w), o.y + px(h)),
74 point(o.x, o.y + px(h)),
75 ],
76 };
77 let mut path = PathBuilder::fill();
78 path.add_polygon(&points, true);
79 if let Ok(path) = path.build() {
80 window.paint_path(path, rgba(ACCENT_WASH()));
81 }
82 },
83 )
84 .size_full()
85}
86
87/// A video clip's dissolve into its neighbour, drawn as a small X astride the
88/// join -- the box it is given is the last [`Clip::transition_out`] frames of
89/// the clip's own width, so the mark sits right at the edge it dissolves
90/// through. Two strokes rather than [`fade_wedge`]'s fill: a dissolve is not
91/// a ramp to or from silence, it is two clips overlapping, and an X reads as
92/// a splice the way a wedge reads as a fade.
93pub(crate) fn dissolve_glyph() -> impl IntoElement {
94 canvas(
95 |_, _, _| (),
96 move |bounds, _, window, _| {
97 let (o, s) = (bounds.origin, bounds.size);
98 let (w, h) = (f32::from(s.width), f32::from(s.height));
99 if w <= 0. || h <= 0. {
100 return;
101 }
102 let mut path = PathBuilder::stroke(px(1.5));
103 path.move_to(point(o.x, o.y));
104 path.line_to(point(o.x + px(w), o.y + px(h)));
105 path.move_to(point(o.x + px(w), o.y));
106 path.line_to(point(o.x, o.y + px(h)));
107 if let Ok(path) = path.build() {
108 window.paint_path(path, rgba(ACCENT_WASH()));
109 }
110 },
111 )
112 .size_full()
113}
114
115/// A toolbar button: its glyph, its name, and its key on hover. `id` only buys
116/// `on_click` and the tooltip -- it is still not focusable, so the root's own
117/// key listener keeps working after a press, and the click lands on mouse-up
118/// inside the button (a press that slides off does nothing).
119///
120/// A button that would do nothing says so: dimmed, no pointer, no listener.
121pub(crate) fn control(
122 id: &'static str,
123 // The rect the label is allowed to change inside: 0 hugs the text, anything
124 // else is reserved once and never moves again, which is what keeps a button
125 // that relabels itself ("Export"/"Cancel") from shoving its neighbours
126 // along the row every time its state changes.
127 w: f32,
128 // The plane the button stands on. One button in the window is the accent
129 // (Export, the primary action); everything else is `BG_RAISED`.
130 bg: u32,
131 glyph: Option<AnyElement>,
132 // Not `&'static str`: the volume button's label is its state.
133 label: impl Into<SharedString>,
134 shortcut: String,
135 enabled: bool,
136 on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
137) -> impl IntoElement {
138 let label = label.into();
139 let tip: SharedString = format!("{label} — {shortcut}").into();
140 div()
141 .id(id)
142 .flex_none()
143 .h(px(CONTROL_H))
144 .flex()
145 .items_center()
146 .justify_center()
147 .gap(px(6.))
148 .px(px(8.))
149 .when(w > 0., |d| d.w(px(w)).overflow_hidden())
150 .rounded(px(4.))
151 .bg(rgb(bg))
152 .when(bg == ACCENT_PRIMARY(), |d| d.text_color(rgb(BG_CANVAS())))
153 // A glyph sits in a box of its own width, never in the width it happens
154 // to draw: the pause bars are 12 px wide and the play triangle is 11, so
155 // pressing Play used to slide every button in the row one pixel left. A
156 // slot is the only fix that holds for the next glyph as well as this one.
157 .children(glyph.map(|glyph| {
158 div()
159 .flex_none()
160 .w(px(GLYPH_SLOT))
161 .flex()
162 .items_center()
163 .justify_center()
164 .child(glyph)
165 }))
166 .child(label)
167 .tooltip(move |_, cx| cx.new(|_| Tip(tip.clone())).into())
168 .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed())
169 .when(enabled, |d| {
170 // An accented button keeps its accent under the pointer: hovering it
171 // to the chrome's own hover colour reads as the primary action
172 // turning itself off.
173 d.cursor_pointer()
174 .hover(move |s| {
175 s.bg(rgb(match bg == ACCENT_PRIMARY() {
176 true => ACCENT_HOVER(),
177 false => BG_HOVER(),
178 }))
179 })
180 .on_click(on_click)
181 })
182}
183
184/// The monitoring level as something to drag: 4 px of bar to look at and the
185/// whole control's height to hit (WCAG 2.5.8), the split the speed bar and the
186/// colour sliders both make. Only the level -- mute is the button beside it, so
187/// a muted slider still shows what unmuting comes back to, drawn dim.
188///
189/// Dimmed and inert without a timeline, like every other control that would
190/// have nothing to act on.
191pub(crate) fn volume_slider(
192 volume: Volume,
193 bar: Rc<Cell<Bounds<Pixels>>>,
194 enabled: bool,
195 cx: &mut Context<Player>,
196) -> impl IntoElement {
197 div()
198 .id("volume-bar")
199 .relative()
200 .flex_none()
201 .w(px(VOLUME_W))
202 .h(px(CONTROL_H))
203 .flex()
204 .items_center()
205 .tooltip(|_, cx| {
206 cx.new(|_| Tip("Volume — drag to set the level; the button mutes".into()))
207 .into()
208 })
209 .when(!enabled, |d| d.opacity(0.4).cursor_not_allowed())
210 .when(enabled, |d| {
211 d.cursor_pointer()
212 .on_mouse_down(
213 MouseButton::Left,
214 cx.listener(|this, event: &MouseDownEvent, _, cx| {
215 this.volume_dragging = true;
216 this.drag_volume(event.position.x, cx);
217 }),
218 )
219 .child(bounds_probe(bar))
220 })
221 .child(
222 div()
223 .w_full()
224 .h(px(4.))
225 .rounded(px(2.))
226 .bg(rgb(BG_RAISED()))
227 .child(
228 div()
229 .h_full()
230 .w(relative(volume.along()))
231 .rounded(px(2.))
232 .bg(rgb(if volume.muted { FG_SECONDARY() } else { ACCENT_PRIMARY() })),
233 ),
234 )
235}
236
237/// The line between two groups of buttons.
238/// What a group of buttons is *for*, printed in front of it. A toolbar of
239/// twelve unlabelled boxes is a toolbar nobody reads twice; three named groups
240/// are three things to skip.
241pub(crate) fn group_label(name: &'static str) -> impl IntoElement {
242 div()
243 .flex_none()
244 .text_size(px(10.))
245 .text_color(rgb(FG_SECONDARY()))
246 .child(name)
247}
248
249pub(crate) fn separator() -> impl IntoElement {
250 div()
251 .flex_none()
252 .mx(px(4.))
253 .w(px(1.))
254 .h(px(18.))
255 .bg(rgb(BG_HOVER()))
256}
257
258/// Whether a card or a menu is drawn over the window, as the hover labels see
259/// it: written once a frame by [`Player::render`], read by every [`Tip`] before
260/// it paints.
261///
262/// A tooltip already on screen when an overlay opens *stays* on screen in gpui:
263/// occluding the surface under it does not take it back, because the check that
264/// keeps it visible works off the element's absolute bounds and knows nothing
265/// about what was painted over it (`div.rs::handle_tooltip_mouse_move`, its own
266/// TODO). So the tip is what has to stand aside -- here, once, for every hover
267/// label in this window, rather than at fifteen call sites of which the
268/// sixteenth would be forgotten.
269pub(crate) static OVERLAID: AtomicBool = AtomicBool::new(false);
270
271/// A tooltip is a view in gpui and nothing smaller, so this is the smallest one
272/// that carries a line of text. It paints outside the window's element tree and
273/// therefore owns its colours.
274pub(crate) struct Tip(pub(crate) SharedString);
275
276impl Render for Tip {
277 fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
278 // A card or a menu is up: nothing. A line of text over the items of the
279 // menu that just opened under the pointer is the card being painted
280 // over by the window it covers.
281 if OVERLAID.load(Ordering::Relaxed) {
282 return div();
283 }
284 // DESIGN §4: "Notices, menus, chips, cues, and tooltips are all the
285 // same plate" -- canvas-on-panel, 2px radius, the room's own type
286 // (`ui::type_scale`), not a bordered box in an ad hoc size. This one
287 // struct is every tooltip in the app (grepped: `timeline.rs`,
288 // `library.rs`, `overlays.rs`, `spine_stance.rs`, `dock_stance.rs`,
289 // `timeband_stance.rs`, `preview.rs`, here), so the fix belongs here
290 // once, not per call site.
291 let style = crate::ui::type_scale::label(crate::ui::type_scale::LABEL_ROW_PX, gpui::FontWeight::MEDIUM);
292 div()
293 .px(px(8.))
294 .py(px(4.))
295 .rounded(px(2.))
296 .bg(rgb(DARK_CANVAS()))
297 .font(style.font)
298 .text_size(style.size)
299 .text_color(rgb(INK1()))
300 .child(self.0.clone())
301 }
302}
303
304/// Scissors: two blades crossed. Drawn this way and not as a split clip because
305/// two bars is what the transport wears when it is playing -- the one glyph a
306/// cut must never be mistaken for.
307pub(crate) fn cut_glyph() -> impl IntoElement {
308 canvas(
309 |_, _, _| (),
310 |bounds, _, window, _| {
311 let (o, s) = (bounds.origin, bounds.size);
312 let mut path = PathBuilder::stroke(px(1.5));
313 path.move_to(point(o.x + s.width * 0.15, o.y + s.height));
314 path.line_to(point(o.x + s.width * 0.9, o.y + s.height * 0.1));
315 path.move_to(point(o.x + s.width * 0.85, o.y + s.height));
316 path.line_to(point(o.x + s.width * 0.1, o.y + s.height * 0.1));
317 if let Ok(path) = path.build() {
318 window.paint_path(path, rgb(FG_PRIMARY()));
319 }
320 },
321 )
322 .w(px(13.))
323 .h(px(13.))
324}
325
326/// A lid over a bin.
327pub(crate) fn delete_glyph() -> impl IntoElement {
328 div()
329 .flex()
330 .flex_col()
331 .items_center()
332 .gap(px(2.))
333 .child(div().w(px(13.)).h(px(2.)).bg(rgb(FG_PRIMARY())))
334 .child(div().w(px(9.)).h(px(9.)).bg(rgb(FG_PRIMARY())))
335}
336
337/// What a window with no file open is waiting for. Both ways in are already
338/// built -- the whole window is the drop target and the Import chooser takes a
339/// project as readily as media -- so this only has to say so.
340pub(crate) fn empty_hint() -> impl IntoElement {
341 div()
342 .flex()
343 .flex_col()
344 .items_center()
345 .gap(px(6.))
346 .text_color(rgb(FG_SECONDARY()))
347 .child("Drop a video or an .edith project here")
348 .child(
349 div()
350 .text_size(px(11.))
351 .child("or click Import in the media list"),
352 )
353}
354
355/// Two bars while playing, a triangle in every other state -- paused, nothing
356/// open, and played out, where the button's next act is to start over rather
357/// than to stop something. Drawn, so there is no icon font and no glyph
358/// coverage to depend on.
359pub(crate) fn transport_glyph(state: Transport) -> impl IntoElement {
360 let playing = state.is_playing();
361 div()
362 .flex()
363 .items_center()
364 .gap(px(4.))
365 .when(playing, |d| {
366 d.child(div().w(px(3.)).h(px(12.)).bg(rgb(FG_PRIMARY())))
367 .child(div().w(px(3.)).h(px(12.)).bg(rgb(FG_PRIMARY())))
368 })
369 .when(!playing, |d| {
370 d.child(
371 canvas(
372 |_, _, _| (),
373 |bounds, _, window, _| {
374 let (o, s) = (bounds.origin, bounds.size);
375 let mut path = PathBuilder::fill();
376 path.move_to(o);
377 path.line_to(point(o.x + s.width, o.y + s.height / 2.));
378 path.line_to(point(o.x, o.y + s.height));
379 path.close();
380 if let Ok(path) = path.build() {
381 window.paint_path(path, rgb(FG_PRIMARY()));
382 }
383 },
384 )
385 .w(px(11.))
386 .h(px(13.)),
387 )
388 })
389}