bigtext 0.18.0

Free editor for huge text and log files (1-50+ GB): opens them without loading them into memory, 4 GB/s search, atomic save
Documentation
//! Dragging the thumb of the views' own scroll bars (text, table, hex).

use egui::{Response, Ui};

/// A press on a scroll bar, kept while the button is down.
#[derive(Clone, Copy, Debug, PartialEq)]
struct Grab {
    /// Where the pointer was pressed, along the track.
    from: f32,
    /// Where the thumb was then, as a fraction of the way it can travel.
    frac: f64,
    /// Whether the pointer has left `from` since.
    moved: bool,
}

/// Where the thumb of the scroll bar `r` goes this frame, as a fraction of the way it can travel
/// (0 at the start, 1 at the end), or `None` to leave the view where it is. `at` is the pointer
/// along the track, `track` and `thumb` are (start, length) along it, and `frac` is where the
/// thumb is now.
///
/// A press on the thumb holds it where it was grabbed: a click changes nothing, and a drag moves
/// it as far as the pointer moved (counted from where it was, so a huge file does not lose rows
/// to rounding). A press on the track elsewhere puts the thumb's middle there.
pub(crate) fn thumb_drag(
    ui: &Ui,
    r: &Response,
    at: Option<f32>,
    track: (f32, f32),
    thumb: (f32, f32),
    frac: f64,
) -> Option<f64> {
    let id = r.id.with("thumb grab");
    let at = at.filter(|_| r.dragged() || r.is_pointer_button_down_on());
    let Some(at) = at else {
        ui.data_mut(|d| d.remove::<Grab>(id));
        return None;
    };
    // A new press starts over, also after a release this bar did not see.
    let prev = if ui.input(|i| i.pointer.any_pressed()) {
        None
    } else {
        ui.data(|d| d.get_temp::<Grab>(id))
    };
    let (grab, to) = step(prev, at, track, thumb, frac);
    ui.data_mut(|d| d.insert_temp(id, grab));
    to
}

fn step(
    prev: Option<Grab>,
    at: f32,
    track: (f32, f32),
    thumb: (f32, f32),
    frac: f64,
) -> (Grab, Option<f64>) {
    let room = (track.1 - thumb.1).max(1.0);
    match prev {
        None if at >= thumb.0 && at <= thumb.0 + thumb.1 => (
            Grab {
                from: at,
                frac,
                moved: false,
            },
            None,
        ),
        None => {
            let f = ((at - track.0 - thumb.1 / 2.0) / room).clamp(0.0, 1.0) as f64;
            let grab = Grab {
                from: at,
                frac: f,
                moved: false,
            };
            (grab, Some(f))
        }
        Some(g) if !g.moved && at == g.from => (g, None),
        Some(g) => {
            let f = (g.frac + ((at - g.from) / room) as f64).clamp(0.0, 1.0);
            (Grab { moved: true, ..g }, Some(f))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::step;

    // A track from 0 to 500 with a 100 long thumb at 200..300: 400 to travel, now at 0.5.
    const TRACK: (f32, f32) = (0.0, 500.0);
    const THUMB: (f32, f32) = (200.0, 100.0);

    #[test]
    fn a_click_on_the_thumb_leaves_the_view_where_it_is() {
        // Near the thumb's end, far from its middle: before, the view jumped by 40 pixels' worth.
        let (g, to) = step(None, 290.0, TRACK, THUMB, 0.5);
        assert_eq!(to, None);
        let (g, to) = step(Some(g), 290.0, TRACK, THUMB, 0.5);
        assert_eq!(to, None);
        assert!(!g.moved);
    }

    #[test]
    fn a_drag_moves_the_thumb_as_far_as_the_pointer() {
        let (g, _) = step(None, 290.0, TRACK, THUMB, 0.5);
        let (g, to) = step(Some(g), 330.0, TRACK, THUMB, 0.5);
        assert!(to.is_some_and(|f| (f - 0.6).abs() < 1e-6), "{to:?}");
        // Back where it was grabbed: back where the thumb was.
        let (g, to) = step(Some(g), 290.0, TRACK, THUMB, 0.6);
        assert_eq!(to, Some(0.5));
        // Not past either end.
        let (g, to) = step(Some(g), 1000.0, TRACK, THUMB, 0.5);
        assert_eq!(to, Some(1.0));
        let (_, to) = step(Some(g), -1000.0, TRACK, THUMB, 1.0);
        assert_eq!(to, Some(0.0));
    }

    #[test]
    fn a_press_on_the_track_puts_the_thumb_middle_there() {
        let (g, to) = step(None, 450.0, TRACK, THUMB, 0.5);
        assert_eq!(to, Some(1.0));
        let (_, to) = step(Some(g), 450.0, TRACK, THUMB, 1.0);
        assert_eq!(to, None);
        let (_, to) = step(None, 150.0, TRACK, THUMB, 0.5);
        assert_eq!(to, Some(0.25));
    }
}