forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBef5832fTahinli3 months ago597 commits

interact.rs315 lines, 15.4 KiBHistoryRawDownload
1//! What a pointer is doing: the drags, the ghosts, the trims and the key repeat.
2
3use crate::*;
4
5/// A library row being dragged: the file and which of its audio streams that
6/// row is, which is the whole of what a row names. Where it lands does not
7/// change what is inserted.
8pub(crate) struct AssetDrag(pub(crate) PathBuf, pub(crate) usize);
9
10/// A clip already on the timeline being dragged: the lane it is on and its index
11/// there, which is how every other edit names a clip. Unlike an [`AssetDrag`]
12/// nothing is inserted -- the same clip changes lane and keeps the frames it
13/// plays -- but where along the bed it is let go is exactly where it lands, less
14/// the offset the hand grabbed it at ([`Player::grab`]).
15#[derive(Clone, Copy)]
16pub(crate) struct ClipDrag {
17 pub(crate) lane: Lane,
18 pub(crate) idx: usize,
19 /// The clip that was picked up, so the drop can find it again: gpui freezes
20 /// the payload for the whole gesture, and an edit made *during* one -- a
21 /// stroke deletes, undoes or pastes, none of which a drag blocks -- ripples
22 /// the indices under it. The index alone would then name a different take at
23 /// the release, and the drag would move a clip nobody touched (see
24 /// [`live_idx`]).
25 pub(crate) clip: Clip,
26}
27
28/// A *palette* subtitle row being dragged onto a subtitle lane: which track of
29/// [`PlaybackSession::subtitles`] it is, which is the whole of what a row names.
30/// The window it lands with is the track's own, from its first microsecond to
31/// its last cue -- a placement is trimmed after it is placed, like every clip
32/// here.
33pub(crate) struct SubPick(pub(crate) usize);
34
35/// A subtitle already placed on a lane being dragged: [`ClipDrag`]'s twin, and
36/// carrying the placement itself for that struct's reason -- an edit made during
37/// the gesture moves the indices gpui froze into the payload, so the drop finds
38/// its placement by value ([`Player::dragged_sub`]) rather than by an index that
39/// may since have become another caption's.
40#[derive(Clone, Copy)]
41pub(crate) struct SubDrag {
42 pub(crate) lane: Lane,
43 pub(crate) idx: usize,
44 pub(crate) sub: SubClip,
45}
46
47/// A track *header* being dragged: which track the hand took hold of, to be
48/// let go over the header of the one whose place it is to take
49/// ([`Player::reorder_lane`]). The lane alone -- a track carries its own clips
50/// wherever it goes, so unlike a [`ClipDrag`] there is nothing else to name.
51#[derive(Clone, Copy)]
52pub(crate) struct LaneDrag(pub(crate) Lane);
53
54/// Where a header drag in flight would leave the track in the hand: the lane
55/// whose slot it is about to take, and whether the line is drawn at that lane's
56/// top edge (it is coming up from below) or its bottom one. The drop indicator
57/// every editor draws between two tracks, and the header answer to the ghost a
58/// clip drag lays on a lane ([`Ghost`]) -- stale between gestures, which costs
59/// nothing because it is drawn only while one is live.
60#[derive(Clone, Copy, PartialEq)]
61pub(crate) struct LaneDrop {
62 pub(crate) lane: Lane,
63 pub(crate) above: bool,
64}
65
66/// Where the drag in flight would leave what it is carrying: the lane the
67/// pointer is over, the snapped head the release will commit ([`landing`]), and
68/// how long the thing is. Drawn on that lane as a translucent box the size of
69/// the take, so a landing is *seen* before the release rather than discovered
70/// after it -- the line ([`Player::snap_cue`]) marks the frame, this shows the
71/// body. `refused` is a drop the lane cannot take -- a picture over an audio
72/// track, a sound over a video one -- tinted rather than silent, because the
73/// refusal is coming at the release either way ([`lane_refuses`],
74/// [`Project::move_clip`]).
75#[derive(Clone, Copy, PartialEq)]
76pub(crate) struct Ghost {
77 pub(crate) lane: Lane,
78 pub(crate) start: u32,
79 /// Timeline frames, which a speed has already been counted into: the box is
80 /// as wide as the clip is *long where it lands*. Zero for a library row of
81 /// unknown length, drawn as a head marker.
82 pub(crate) frames: u32,
83 /// The swatch of the file being carried ([`file_tint`]), so the
84 /// ghost reads as the thing in the hand.
85 pub(crate) tint: u32,
86 pub(crate) refused: bool,
87}
88
89/// A clip edge being dragged: which end of which clip, and the timeline frame
90/// the pointer has pulled it to. The box on screen is drawn from `to` while this
91/// is set and the engine hears about it once, at the release
92/// ([`Player::commit_trim`]) -- one edit, one undo step for the whole gesture,
93/// exactly as an equalizer drag works.
94#[derive(Clone, Copy)]
95pub(crate) struct Trim {
96 pub(crate) lane: Lane,
97 pub(crate) idx: usize,
98 pub(crate) edge: Edge,
99 /// Where the edge stood at the press: with `to`, the delta the whole group
100 /// follows by -- each member's own edge moves this far, clamped to its own
101 /// room, which is exactly what the engine commits at the release.
102 pub(crate) from: u32,
103 /// Already clamped by `PlaybackSession::trim_room`, so the width drawn from
104 /// it is the width the release commits -- an edge stops under the pointer
105 /// rather than snapping back after the fact.
106 pub(crate) to: u32,
107 /// The dragged placement's group, so the boxes of everything it names --
108 /// clips and captions alike -- follow the edge on screen exactly as the
109 /// engine will move them.
110 pub(crate) link: Option<u32>,
111}
112
113/// How wide a clip's edge is as a *target*: the strip at each end where a press
114/// means "make this longer or shorter" instead of "move this to another lane".
115/// Wide enough to hit, narrow enough that the middle of even a small box is
116/// still the body.
117pub(crate) const EDGE_W: f32 = 6.;
118
119/// A fade handle's own hit zone, at each *top* corner of an audio clip's box --
120/// never the trim strip's height, which is [`EDGE_W`] wide down the whole box
121/// ([`trims`]) and would swallow a fade press if the two overlapped. Sitting
122/// just inside the trim strip rather than on top of it, so one gesture is
123/// still exactly one thing: the outer [`EDGE_W`] column lengthens or shortens
124/// the clip, this small square at its top corner shapes the fade instead.
125pub(crate) const FADE_HANDLE_W: f32 = 10.;
126/// Only the label row's own height tall -- a fade handle reaching down into
127/// the waveform would sit on top of the body drag and the waveform's own
128/// tooltip both.
129pub(crate) const FADE_HANDLE_H: f32 = 10.;
130
131/// A fade handle being dragged: which clip, which end (`is_in` for the head's
132/// ramp-up, the tail's ramp-down otherwise), and the gesture's own state --
133/// `press_x` to measure the pixel delta from, `start` the fade length the
134/// press found, `to` what the drag is showing right now, `cap` the clip's own
135/// length in frames ([`Project::set_fade_in`]'s own clamp, so the drag never
136/// draws past what a release would refuse). One clip, no group: unlike a trim
137/// a fade never drags a neighbour, and unlike a caption it never wants a
138/// track's walls.
139#[derive(Clone, Copy)]
140pub(crate) struct FadeDrag {
141 pub(crate) lane: Lane,
142 pub(crate) idx: usize,
143 pub(crate) is_in: bool,
144 pub(crate) press_x: Pixels,
145 pub(crate) start: u32,
146 pub(crate) to: u32,
147 pub(crate) cap: u32,
148}
149
150/// Whether a clip box this wide gets its two trim strips at all. Below three
151/// handles wide the pair would occlude the whole box: every press on it would
152/// trim, and the clip could not be selected, dragged to another lane or picked
153/// up by its middle -- which is exactly what a jumpcut leaves behind
154/// ([`Player::cut_silences`] manufactures a great many short clips). Above it,
155/// what is left between the two strips is a handle's width of body in its own
156/// right. A clip too short for its handles is trimmed by zooming in first: the
157/// bed is a magnifier, and the strip grows with the box.
158pub(crate) fn trims(width: f32) -> bool {
159 width >= 3. * EDGE_W
160}
161
162/// How wide a clip's box is *drawn*, given the width its own length is worth
163/// (`span`). Never under [`HIT_MIN`], even where that is wider than the clip is
164/// long: zoomed far out a short take is worth a fraction of a pixel, and a box
165/// nobody can put a pointer on is a clip that cannot be selected, dragged, given
166/// a menu or reached at all -- which is strictly worse than one drawn a few
167/// pixels too wide. The same call [`cue_box`] makes for a mark, and what every
168/// editor draws.
169///
170/// A drawing only: [`Scale::time_at`] still reads the bed, so a press inside the
171/// padding names the frame it points at, and the box's head is the clip's own.
172pub(crate) fn clip_width(span: f32) -> f32 {
173 span.max(HIT_MIN)
174}
175
176/// The sheet a card or a menu is painted on: the whole window, and the mouse
177/// stops at it. Occluding is what tells gpui that nothing under this sheet is
178/// hovered any more (`Hitbox::is_hovered`) -- without it the window carries on
179/// hovering behind an open menu and pops *its* tooltip over the menu's items,
180/// which is a card being painted over by the thing it covers.
181///
182/// Every card and every menu takes its sheet from here, so no surface can be
183/// drawn over the top of one by having been given a plain scrim.
184pub(crate) fn scrim() -> Div {
185 div().absolute().inset_0().occlude()
186}
187
188/// The sheet a card with a *slider* in it takes instead: the same scrim, with
189/// the window's own drag listeners on it as well.
190///
191/// A scrim occludes, and occluding is where gpui's hit test stops
192/// (`Hitbox::is_hovered`, window.rs:788) -- so while a card is up the root is
193/// not hovered anywhere behind it and its `on_mouse_move`/`on_mouse_up` hear
194/// nothing at all. Every drag in this window is tracked from the root, because
195/// each of them starts on a strip a few pixels wide that the pointer leaves at
196/// once ([`Player::drag_move`]), so a card's handles were set by the press and
197/// then frozen: the value never followed the hand and the release never wrote.
198/// The scrim is the one surface above the occluder that covers the whole card,
199/// so the same two listeners go here, and a drag that leaves the card is picked
200/// up by the root's copy of them without a seam.
201pub(crate) fn drag_scrim(cx: &mut Context<Player>) -> Div {
202 scrim()
203 .on_mouse_move(cx.listener(Player::drag_move))
204 .on_mouse_up(MouseButton::Left, cx.listener(Player::drag_release))
205}
206
207/// The seam between two panels, and the handle that resizes them: a strip
208/// [`SPLIT_W`] wide with the resize cursor of its axis on it, which is the whole
209/// of how a person finds out the layout moves at all.
210///
211/// The press is all this element does. The gesture itself is the root's, like
212/// every other drag in this window ([`Player::drag_move`]): a 6 px strip is not
213/// where the pointer stays, and a divider whose own listeners tracked it would
214/// stop following the hand on the first move. `stop_propagation` so the press
215/// never reaches the region under it -- a seam over the timeline must not scrub.
216pub(crate) fn divider(split: Split, cx: &mut Context<Player>) -> Div {
217 let across = matches!(split, Split::Timeline);
218 div()
219 .flex_none()
220 .when(across, |d| d.h(px(SPLIT_W)).w_full().cursor_row_resize())
221 .when(!across, |d| d.w(px(SPLIT_W)).h_full().cursor_col_resize())
222 .bg(rgb(STROKE_DIVIDER()))
223 // Lit under the pointer: the second half of "this can be dragged", said
224 // before the button goes down rather than after.
225 .hover(|s| s.bg(rgb(ACCENT_PRIMARY())))
226 .on_mouse_down(
227 MouseButton::Left,
228 cx.listener(move |this, _: &MouseDownEvent, _, cx| {
229 this.split_drag = Some(split);
230 cx.stop_propagation();
231 }),
232 )
233}
234
235/// A press that stops here. What every card's body hands its scrim: the scrim
236/// closes the card on a press, and the card is painted after it, so this listener
237/// runs first (gpui dispatches topmost-first, window.rs:3705) and a press meant
238/// for a button never closes the card out from under its own click -- the rule
239/// the menus already follow ([`Player::library_card`]).
240pub(crate) fn swallow(_: &MouseDownEvent, _: &mut Window, cx: &mut App) {
241 cx.stop_propagation();
242}
243
244/// How close to an edge a dragged clip has to be let go for it to land *on* it:
245/// the snap every timeline has, in pixels rather than frames so that it feels
246/// the same at every zoom. Narrower than [`EDGE_W`] -- a hand aiming between two
247/// takes must still be able to leave a gap of a few frames there.
248pub(crate) const SNAP_PX: f64 = 5.;
249
250/// The scrollbar's thumb never narrows past this, however long the timeline:
251/// a thumb a pixel wide is a thumb no hand can hold. A quarter of the ruler's
252/// own seek strip, which is the idiom it sits under.
253pub(crate) const SCROLL_THUMB_MIN: f32 = 24.;
254
255/// The thickness of a scrollbar strip's hit area, in the one dimension the
256/// strip spans: the height of the horizontal one under the lanes, the width
257/// of the vertical one on the beds' right edge. The bar inside it looks 6 px,
258/// and the strip that takes the press is thicker for the ruler strip's
259/// reason -- a hand must be able to find it without aiming (WCAG 2.5.8).
260pub(crate) const SCROLL_HIT: f32 = 14.;
261
262/// Where a held key would land, which is the whole of what auto-repeat has to
263/// know. [`Player::repeat_scope`] answers it from the same state the handler
264/// walks below itself.
265#[derive(Clone, Copy, PartialEq, Eq, Debug)]
266pub(crate) enum Repeat {
267 /// A card with values in it owns the keyboard: equalizer, colour, speed or
268 /// silence. Its arrows are sliders.
269 Card,
270 /// Nobody does, so the keymap answers -- and only one pair of its actions
271 /// is a value being moved rather than a thing being done once.
272 Keymap,
273 /// A stroke is being captured, an export is running, or an overlay with no
274 /// value in it is up. Nothing there is worth a repeat.
275 Nothing,
276}
277
278/// Whether a *held* stroke means it again. One press is always one action; a
279/// hold is only ever a value running, so this is what tells the two apart.
280///
281/// The cards' arrows, because that is what every one of them moves a slider
282/// with -- and only the arrows, so the equalizer's `r` cannot flatten five
283/// bands forty times a second and the silence card's `enter` cannot cut forty
284/// places again on the next tick. Outside a card the volume pair and nothing
285/// else: play, cut, delete, save, export and every other binding is a one-shot,
286/// exactly as it was when the handler filtered every held key alike.
287pub(crate) fn repeats(scope: Repeat, key: &str, action: Option<ActionId>) -> bool {
288 match scope {
289 Repeat::Card => matches!(key, "up" | "down" | "left" | "right"),
290 Repeat::Keymap => matches!(
291 action,
292 Some(
293 ActionId::VolumeUp
294 | ActionId::VolumeDown
295 // A zoom is a value being moved as much as a level is:
296 // held, it runs from the whole timeline down to a handful
297 // of frames and stops there. The fit is one press.
298 | ActionId::ZoomIn
299 | ActionId::ZoomOut
300 )
301 ),
302 Repeat::Nothing => false,
303 }
304}
305
306/// The keys that are only ever half a chord. gpui delivers a lone modifier
307/// press as a keystroke of its own, and taking one as a binding would leave an
308/// action that fires the moment the user reaches for any chord that uses it --
309/// so a capture waits through them instead.
310pub(crate) fn is_bare_modifier(key: &str) -> bool {
311 matches!(
312 key,
313 "control" | "shift" | "alt" | "super" | "platform" | "function" | "fn" | "meta" | "command"
314 )
315}