diff --git a/crates/app/src/interact.rs b/crates/app/src/interact.rs index 6ac8f06..779458d 100644 --- a/crates/app/src/interact.rs +++ b/crates/app/src/interact.rs @@ -168,6 +168,34 @@ pub(crate) fn drag_scrim(cx: &mut Context) -> Div { .on_mouse_up(MouseButton::Left, cx.listener(Player::drag_release)) } +/// The seam between two panels, and the handle that resizes them: a strip +/// [`SPLIT_W`] wide with the resize cursor of its axis on it, which is the whole +/// of how a person finds out the layout moves at all. +/// +/// The press is all this element does. The gesture itself is the root's, like +/// every other drag in this window ([`Player::drag_move`]): a 6 px strip is not +/// where the pointer stays, and a divider whose own listeners tracked it would +/// stop following the hand on the first move. `stop_propagation` so the press +/// never reaches the region under it -- a seam over the timeline must not scrub. +pub(crate) fn divider(split: Split, cx: &mut Context) -> Div { + let across = matches!(split, Split::Timeline); + div() + .flex_none() + .when(across, |d| d.h(px(SPLIT_W)).w_full().cursor_row_resize()) + .when(!across, |d| d.w(px(SPLIT_W)).h_full().cursor_col_resize()) + .bg(rgb(STROKE_DIVIDER())) + // Lit under the pointer: the second half of "this can be dragged", said + // before the button goes down rather than after. + .hover(|s| s.bg(rgb(ACCENT_PRIMARY()))) + .on_mouse_down( + MouseButton::Left, + cx.listener(move |this, _: &MouseDownEvent, _, cx| { + this.split_drag = Some(split); + cx.stop_propagation(); + }), + ) +} + /// A press that stops here. What every card's body hands its scrim: the scrim /// closes the card on a press, and the card is painted after it, so this listener /// runs first (gpui dispatches topmost-first, window.rs:3705) and a press meant diff --git a/crates/app/src/layout.rs b/crates/app/src/layout.rs index dce15ce..6c51aeb 100644 --- a/crates/app/src/layout.rs +++ b/crates/app/src/layout.rs @@ -1,5 +1,7 @@ //! The window's fixed geometry: what every region is measured in. +use crate::*; + /// ...and the narrowest it is drawn: a library row whose length the engine has /// not measured yet has a landing place but no width, and a head marker says /// where it goes where a zero-width box would say nothing. @@ -294,3 +296,132 @@ pub(crate) const ZOOM_OUT_MARGIN: f64 = 1.05; /// short -- the thing a bed scaled to the content's own length cannot say. /// [`View::fit`] is the one way back to "the whole timeline across the bed". pub(crate) const PPS_DEFAULT: f64 = 40.; + +// -- the seams between the regions, and where a hand has dragged them --------- + +/// Which divider a hand has hold of. The three seams the main layout has -- +/// library|picture, picture|inspector and the timeline's top edge -- and so the +/// only three sizes in this window a person sets rather than is given. +#[derive(Clone, Copy, PartialEq, Eq, Debug)] +pub(crate) enum Split { + Library, + Inspector, + Timeline, +} + +/// How wide a divider is drawn *and* hit. Wider than the 1 px stroke it stands +/// in for: a seam nobody can put a pointer on is a seam nobody can drag, which +/// is why every editor that ships this draws a strip rather than the hairline +/// it looks like. +pub(crate) const SPLIT_W: f32 = 6.; + +/// The most of the window one side column may be dragged to. A third each, so +/// the picture keeps the middle at every size -- the rule [`library_w`] and +/// [`inspector_w`] already lay an untouched window out by, kept once the hand +/// takes over. +pub(crate) const SIDE_MAX_FRAC: f32 = 1. / 3.; + +/// The most of the window the timeline may be dragged to. Past +/// [`TIMELINE_SHARE`], which is what it is *given*: a hand asking for a tall +/// timeline is asking on purpose, and the picture still keeps most of a third. +pub(crate) const TIMELINE_MAX_SHARE: f32 = 0.7; + +/// What a hand has done to the three seams: the size it dragged each panel to, +/// or `None` where nobody has touched one and the window's own share still +/// answers. Held in the model and not recomputed, so a size outlives the +/// release -- and read through [`split_size`], so it cannot outlive the window +/// it was set in. +#[derive(Clone, Copy, Default)] +pub(crate) struct Splits { + library: Option, + inspector: Option, + timeline: Option, +} + +impl Splits { + pub(crate) fn get(&self, split: Split) -> Option { + match split { + Split::Library => self.library, + Split::Inspector => self.inspector, + Split::Timeline => self.timeline, + } + } + + pub(crate) fn set(&mut self, split: Split, size: f32) { + *match split { + Split::Library => &mut self.library, + Split::Inspector => &mut self.inspector, + Split::Timeline => &mut self.timeline, + } = Some(size); + } +} + +/// How small a panel may be dragged and how large: the floor is the size it +/// still shows something at, the ceiling is what its neighbour needs to stay a +/// region rather than a sliver. Neither end is passable -- a panel dragged to +/// nothing is a panel nobody can get back, and a picture squeezed out by two +/// side columns is the same loss on the other side of the window. +pub(crate) fn split_bounds(split: Split, window: Size) -> (f32, f32) { + let (w, h) = (f32::from(window.width), f32::from(window.height)); + match split { + Split::Library => (LIBRARY_MIN_W, w * SIDE_MAX_FRAC), + Split::Inspector => (INSPECTOR_MIN_W, w * SIDE_MAX_FRAC), + // One whole lane under the chrome: a timeline shorter than that is a + // ruler with nothing beneath it. + Split::Timeline => (TIMELINE_FIXED_H + LANE_H, h * TIMELINE_MAX_SHARE), + } +} + +/// How big the panel at this seam is drawn: what the hand dragged it to, held +/// inside the window *being drawn now* -- the window is resizable, and a width +/// set at 1920 px is not a width at 640 -- or the share the untouched layout +/// gives it. +pub(crate) fn split_size( + split: Split, + set: Option, + lanes: usize, + window: Size, +) -> f32 { + let (w, h) = (f32::from(window.width), f32::from(window.height)); + let (min, max) = split_bounds(split, window); + let default = match split { + Split::Library => library_w(w), + Split::Inspector => inspector_w(w), + Split::Timeline => timeline_h(lanes).min(h * TIMELINE_SHARE), + }; + // The floor wins a window too small to honour both ends: a panel at its + // floor is still a panel, and `clamp` panics outright on a ceiling under + // its floor. + set.unwrap_or(default).clamp(min, max.max(min)) +} + +/// Where a divider drag puts the panel it belongs to: the pointer's position +/// turned into that panel's size. Half a strip is taken off each answer because +/// the strip is grabbed by its middle -- without it the panel jumps a strip's +/// width on the press and then trails the pointer for the rest of the gesture. +pub(crate) fn split_drag_size(split: Split, at: Point, window: Size) -> f32 { + let (x, y) = (f32::from(at.x), f32::from(at.y)); + let (w, h) = (f32::from(window.width), f32::from(window.height)); + match split { + Split::Library => x - SPLIT_W / 2., + Split::Inspector => w - x - SPLIT_W / 2., + // This seam sits above the edit toolbar, which is a fixed height: what + // is under the pointer is the toolbar and the timeline together. + Split::Timeline => h - y - TOOLBAR_H - SPLIT_W / 2., + } +} + +impl Player { + /// How big the panel on this seam is this frame -- the one door every + /// region measures itself through, so a dragged size and a drawn one cannot + /// disagree. + pub(crate) fn split_px(&self, split: Split, window: Size) -> f32 { + // The pair a fresh project starts with where there is no session yet, + // which is what the timeline itself draws (`Player::timeline`). + let lanes = self + .session + .as_ref() + .map_or(2, |session| session.lanes().len()); + split_size(split, self.splits.get(split), lanes, window) + } +} diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index f8c20c7..f670805 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -210,6 +210,14 @@ struct Player { /// A drag that started on the ruler. Moves anywhere in the window scrub /// while it is set; the release commits the exact position. scrubbing: bool, + /// What the hand has made of the three panel seams, and which one it is + /// holding now. Kept here rather than worked out per frame from the + /// window's own shares: a size a person dragged is a size that has to + /// survive the release ([`Splits`]). + splits: Splits, + /// A drag that started on a divider, tracked on the root for `scrubbing`'s + /// reason -- the strip is 6 px and the pointer leaves it at once. + split_drag: Option, /// A drag that started on a clip's edge, tracked on the root for /// `scrubbing`'s reason: a 6 px strip is not where the pointer stays. See /// [`Trim`]. @@ -665,6 +673,8 @@ fn main() { hw_caps: None, clipboard: None, scrubbing: false, + splits: Splits::default(), + split_drag: None, trim: None, grab: 0, snap: true, diff --git a/crates/app/src/player/timeline_edit.rs b/crates/app/src/player/timeline_edit.rs index 02dbf5f..e16d729 100644 --- a/crates/app/src/player/timeline_edit.rs +++ b/crates/app/src/player/timeline_edit.rs @@ -1012,7 +1012,26 @@ impl Player { /// where it sits (`Hitbox::is_hovered`, window.rs:788), so while a card is /// up the root is not hovered anywhere under it and hears none of this: the /// press set a value and the drag then froze on it. - pub(crate) fn drag_move(&mut self, event: &MouseMoveEvent, _: &mut Window, cx: &mut Context) { + pub(crate) fn drag_move( + &mut self, + event: &MouseMoveEvent, + window: &mut Window, + cx: &mut Context, + ) { + // A divider is answered before every gesture below it: it is pressed on + // a strip of its own that nothing else is under, so neither can swallow + // the other -- a seam over the timeline never scrubs, and a scrub is + // never mistaken for a resize. + if let Some(split) = self.split_drag { + match event.pressed_button { + Some(MouseButton::Left) => self.drag_split(split, event.position, window, cx), + // Released outside the window: the up below never came, so this + // is where the gesture ends. Nothing is owed -- every move has + // already been written. + _ => self.split_drag = None, + } + return; + } // A handle is 10 px across and the pointer leaves it at once, so // the equalizer drag is tracked here for the ruler's reason. if self.eq_dragging { @@ -1092,7 +1111,18 @@ impl Player { /// live-writing bars -- is paid here. On the root and on a card's scrim /// both, for [`Player::drag_move`]'s reason: a release over an open card /// never reaches the root. - pub(crate) fn drag_release(&mut self, event: &MouseUpEvent, _: &mut Window, cx: &mut Context) { + pub(crate) fn drag_release( + &mut self, + event: &MouseUpEvent, + window: &mut Window, + cx: &mut Context, + ) { + // The panel is left exactly where the hand let go, for the reason every + // other release here lands one last sample. + if let Some(split) = self.split_drag.take() { + self.drag_split(split, event.position, window, cx); + return; + } if std::mem::take(&mut self.eq_dragging) { // The release lands exactly, then the gesture is written // once -- the append-only table's whole reason. @@ -1130,4 +1160,22 @@ impl Player { self.scrub_to(event.position.x, true, cx); } } + + /// Where the hand has taken a divider, live: the panel it belongs to is set + /// to what the pointer says and the next frame draws it there. Nothing else + /// is written -- a layout is not an edit, so there is no undo step to take + /// and no worker to flush. The clamps are the *reader's* + /// ([`split_size`]), so a size set in one window is still a size in the + /// next one it is drawn in. + fn drag_split( + &mut self, + split: Split, + at: Point, + window: &mut Window, + cx: &mut Context, + ) { + self.splits + .set(split, split_drag_size(split, at, window.viewport_size())); + cx.notify(); + } } diff --git a/crates/app/src/render.rs b/crates/app/src/render.rs index 29ccae2..1ac0ee0 100644 --- a/crates/app/src/render.rs +++ b/crates/app/src/render.rs @@ -555,10 +555,14 @@ impl Render for Player { .min_h(px(0.)) .flex() .child(self.library( - library_w(f32::from(window.viewport_size().width)), + self.split_px(Split::Library, window.viewport_size()), f32::from(window.viewport_size().height), cx, )) + // The seams, one per pair of regions: what a hand drags to + // give a panel more room and its neighbour less + // ([`divider`]). + .child(divider(Split::Library, cx)) .child( div() .flex_1() @@ -636,21 +640,21 @@ impl Render for Player { .child(self.transport_bar( position, state, - f32::from(window.viewport_size().width), + window.viewport_size(), cx, )), ) + .child(divider(Split::Inspector, cx)) // The settings cards live in here rather than over the // timeline: adjusting a clip must not hide the clip. .child(self.inspector(window.viewport_size(), cx)), ) + // Above the toolbar rather than under it: the toolbar is a fixed + // strip belonging to the timeline, so the pair moves as one and the + // edge the hand is pulling is the edge under the pointer. + .child(divider(Split::Timeline, cx)) .child(self.toolbar(cx)) - .child(self.timeline( - position, - state, - f32::from(window.viewport_size().height), - cx, - )) + .child(self.timeline(position, state, window.viewport_size(), cx)) // Over the region they were opened on, and under the modal cards: // they are only ever up while none of those is (`modal`). .children(self.context_card(window.viewport_size(), cx)) diff --git a/crates/app/src/tests/layout.rs b/crates/app/src/tests/layout.rs index 692a993..946e55e 100644 --- a/crates/app/src/tests/layout.rs +++ b/crates/app/src/tests/layout.rs @@ -1202,3 +1202,111 @@ fn the_volume_slider_lands_where_it_paints() { ); } } + +/// The three seams a hand may move, and the two ends neither of them may be +/// dragged past: a panel dragged to nothing is a panel nobody can get back, and +/// a picture squeezed out by its two neighbours is that same loss from the +/// other side of the window. +#[test] +fn a_dragged_divider_stops_before_either_panel_disappears() { + use crate::ui::theme::INSPECTOR_MIN_W; + use crate::{ + SIDE_MAX_FRAC, SPLIT_W, Split, TIMELINE_MAX_SHARE, TOOLBAR_H, inspector_w, library_w, + split_drag_size, split_size, timeline_h, + }; + use gpui::{point, px, size}; + + let window = size(px(1280.), px(720.)); + // Untouched, every region is still the share the window gives it. + assert_eq!(split_size(Split::Library, None, 2, window), library_w(1280.)); + assert_eq!( + split_size(Split::Inspector, None, 2, window), + inspector_w(1280.) + ); + assert_eq!( + split_size(Split::Timeline, None, 2, window), + timeline_h(2).min(720. * TIMELINE_SHARE) + ); + // Dragged, it is what the hand asked for... + assert_eq!(split_size(Split::Library, Some(300.), 2, window), 300.); + assert_eq!(split_size(Split::Timeline, Some(300.), 2, window), 300.); + // ...and never past either end of it. + assert_eq!( + split_size(Split::Library, Some(0.), 2, window), + LIBRARY_MIN_W + ); + assert_eq!( + split_size(Split::Library, Some(9000.), 2, window), + 1280. * SIDE_MAX_FRAC + ); + assert_eq!( + split_size(Split::Inspector, Some(-40.), 2, window), + INSPECTOR_MIN_W + ); + assert_eq!( + split_size(Split::Timeline, Some(0.), 2, window), + TIMELINE_FIXED_H + LANE_H + ); + assert_eq!( + split_size(Split::Timeline, Some(9000.), 2, window), + 720. * TIMELINE_MAX_SHARE + ); + // A window too narrow to honour both ends keeps the floor rather than + // panicking inside `clamp`, which is what a ceiling under its own floor + // does. + let narrow = size(px(600.), px(360.)); + assert_eq!( + split_size(Split::Inspector, Some(9000.), 2, narrow), + INSPECTOR_MIN_W + ); + + // The pointer turned into a size: the panel follows the hand, and the two + // that do not start at the window's own edge are measured from the far one. + // Half a strip off each, because the strip is grabbed by its middle. + assert_eq!( + split_drag_size(Split::Library, point(px(300.), px(400.)), window), + 300. - SPLIT_W / 2. + ); + assert_eq!( + split_drag_size(Split::Inspector, point(px(1000.), px(400.)), window), + 280. - SPLIT_W / 2. + ); + // The seam sits above the fixed toolbar, so what the pointer leaves under + // it is that strip and the timeline together. + assert_eq!( + split_drag_size(Split::Timeline, point(px(300.), px(500.)), window), + 220. - TOOLBAR_H - SPLIT_W / 2. + ); +} + +/// Every seam in the main layout has a handle on it, and every region draws +/// itself at the size that handle sets: a region still measuring itself off the +/// window's own share is a panel whose divider moves nothing. +#[test] +fn every_seam_in_the_layout_has_a_divider_on_it() { + let render = src_text("render.rs"); + for split in ["Split::Library", "Split::Inspector", "Split::Timeline"] { + assert!( + render.contains(&format!("divider({split}")), + "{split} has no divider to drag" + ); + } + // The strip is drawn wide enough to hit and says which way it moves -- + // an invisible hairline is a feature nobody finds. + let interact = src_text("interact.rs"); + assert!( + interact.contains("cursor_col_resize") && interact.contains("cursor_row_resize"), + "a divider with no resize cursor on it" + ); + // ...and nothing lays a region out off the untouched share any more. + for (file, stale) in [ + ("ui/inspector.rs", "inspector_w("), + ("ui/toolbar.rs", "library_w("), + ("render.rs", "library_w("), + ] { + assert!( + !src_text(file).contains(stale), + "{file} still measures a panel with {stale}" + ); + } +} diff --git a/crates/app/src/ui/inspector.rs b/crates/app/src/ui/inspector.rs index 6ae1613..d67b546 100644 --- a/crates/app/src/ui/inspector.rs +++ b/crates/app/src/ui/inspector.rs @@ -16,7 +16,7 @@ impl Player { /// is open, which is a section of this panel rather than a sheet over the /// window. pub(crate) fn inspector(&self, viewport: Size, cx: &mut Context) -> impl IntoElement { - let width = inspector_w(f32::from(viewport.width)); + let width = self.split_px(Split::Inspector, viewport); // The cards measure themselves against the room they are given, and the // room they are given is this column -- not the window. let room = size(px(width), viewport.height); diff --git a/crates/app/src/ui/timeline.rs b/crates/app/src/ui/timeline.rs index 2fad105..c4edf6c 100644 --- a/crates/app/src/ui/timeline.rs +++ b/crates/app/src/ui/timeline.rs @@ -13,7 +13,7 @@ impl Player { &self, position: f64, state: Transport, - viewport_h: f32, + viewport: Size, cx: &mut Context, ) -> impl IntoElement { // Where the playhead is *on the bed*, in pixels from its left edge. @@ -79,7 +79,10 @@ impl Player { // number a scroll is measured against, and the one the affordance below // counts with. The line it costs is taken off the box before the count, // or the row that says "1 more" would be the row hiding it. - let region_h = timeline_h(lanes.len()).min(viewport_h * TIMELINE_SHARE); + // What the lane count asks for against its share of the window -- or + // what a hand dragged the seam above to, which wins both + // ([`Player::split_px`]). + let region_h = self.split_px(Split::Timeline, viewport); let lanes_box = (region_h - TIMELINE_FIXED_H).max(LANE_H); let overflows = lanes.len() > lanes_shown(lanes_box); let lanes_box = match overflows { diff --git a/crates/app/src/ui/toolbar.rs b/crates/app/src/ui/toolbar.rs index 93a5a34..47a7be4 100644 --- a/crates/app/src/ui/toolbar.rs +++ b/crates/app/src/ui/toolbar.rs @@ -131,7 +131,7 @@ impl Player { &self, position: f64, state: Transport, - viewport_w: f32, + viewport: Size, cx: &mut Context, ) -> impl IntoElement { let muted = self.volume.muted; @@ -140,7 +140,13 @@ impl Player { // level together do not fit -- so the level's *bar* stands down (its // button, its keys and its row on the actions card all remain), and // the clock keeps a narrower rect rather than being pushed off. - let room = viewport_w - library_w(viewport_w) - inspector_w(viewport_w); + // The two panels as they are *now*, dividers and all: a room worked out + // from the window's own shares would go stale the moment a seam moved, + // and the clock would be sized for a column that is no longer there. + let room = f32::from(viewport.width) + - self.split_px(Split::Library, viewport) + - self.split_px(Split::Inspector, viewport) + - 2. * SPLIT_W; let clock_w = TIME_W.min(room - 140.).max(96.); let slider = room >= TRANSPORT_ROOMY; div()