`PlaceDescAxis::axis(axis)` shared its name with `PlaceDesc`'s extraction, which is now `Index<Axis>` and reads `place[axis]`. The two go opposite directions, so they get different words: `on_axis` pairs with the `from_axis` it is the shorthand for. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
104 lines
4.2 KiB
Rust
104 lines
4.2 KiB
Rust
use crate::prelude::*;
|
|
|
|
pub struct Scroll {
|
|
inner: StrongWidget,
|
|
axis: Axis,
|
|
amt: Px,
|
|
snap_end: bool,
|
|
container_len: Px,
|
|
content_len: Px,
|
|
}
|
|
|
|
impl Widget for Scroll {
|
|
fn draw(&mut self, painter: &mut Painter) -> Size {
|
|
let container_len = painter.px_len(self.axis);
|
|
// Asked in the whole viewport, then put at the scrolled offset.
|
|
let answer_len = painter
|
|
.widget_at(&self.inner, PlaceDesc::WHOLE.fills())
|
|
.len(self.axis);
|
|
let fixed = painter.to_px(answer_len.without_leftover(), self.axis);
|
|
self.container_len = container_len;
|
|
self.content_len = fixed.max(container_len);
|
|
|
|
if self.snap_end {
|
|
self.amt = self.content_len - self.container_len;
|
|
}
|
|
self.update_amt();
|
|
let align = painter.alignment()[self.axis];
|
|
// Content of a fixed length that fits sits at the start of any box it
|
|
// fits in -- but only anchored there. Anywhere else it is a part of
|
|
// the room left over, so it moves with every length the box takes and
|
|
// the drawing holds for that length alone. One scrolled part way sits
|
|
// where it is until the box shrinks past what is left of it. Kept to
|
|
// the end, it moves with every length.
|
|
let fixed_len = answer_len.is_px();
|
|
if fixed_len && self.content_len <= self.container_len && align == AxisAlign::NEG {
|
|
painter.holds(self.axis, fixed..=Px::MAX);
|
|
} else if fixed_len && !self.snap_end {
|
|
let left = self.content_len - self.amt;
|
|
painter.holds(self.axis, Px::MIN..=left);
|
|
}
|
|
|
|
// Content shorter than the viewport has room to sit in, and where it
|
|
// sits is this widget's own alignment -- the same property that would
|
|
// have placed the whole scroll in a box longer than it.
|
|
let slack = (self.container_len - self.content_len).max(Px::ZERO);
|
|
let anchor = slack.mul(align.rel());
|
|
// Content that fills the viewport and has not been scrolled is the
|
|
// viewport, and is handed back as it came. Writing the same box as
|
|
// its own length in pixels is the same box in another form, and the
|
|
// two do not round alike: a part centred in `rel 1` lands a step from
|
|
// one centred in `px 900`, since halving a difference is not halving
|
|
// each part of it.
|
|
let moved = anchor != Px::ZERO || self.amt != Px::ZERO;
|
|
let content = match moved || self.content_len != self.container_len {
|
|
true => {
|
|
let start = Len::from_parts(Rel::ZERO, anchor - self.amt);
|
|
UiSpan::new(start, start.offset(self.content_len)).shifted_desc()
|
|
}
|
|
false => PlaceDescAxis::WHOLE,
|
|
};
|
|
// The viewport is the inner's rel base, so a fraction it declares or
|
|
// reports is a fraction of what is on screen rather than of the
|
|
// content box its own answer decided. Where it goes is the content
|
|
// box, scrolled: its drawing moved there, not made again there.
|
|
painter.place_at(&self.inner, content.on_axis(self.axis).fills());
|
|
// What it occupies is its box, on both axes: it clips its content to
|
|
// that box, so it can neither take less of one nor honestly ask for
|
|
// more. The content's length is what it scrolls through, not what it
|
|
// is.
|
|
Size::LEFTOVER
|
|
}
|
|
|
|
fn size_hint(&self, _: Axis) -> Option<LayoutLen> {
|
|
Some(LayoutLen::LEFTOVER)
|
|
}
|
|
}
|
|
|
|
impl Scroll {
|
|
pub fn new(inner: StrongWidget, axis: Axis) -> Self {
|
|
Self {
|
|
inner,
|
|
axis,
|
|
amt: Px::ZERO,
|
|
snap_end: true,
|
|
container_len: Px::ZERO,
|
|
content_len: Px::ZERO,
|
|
}
|
|
}
|
|
|
|
pub fn update_amt(&mut self) {
|
|
self.amt = self.amt.max(Px::ZERO);
|
|
let len = (self.content_len - self.container_len).max(Px::ZERO);
|
|
self.amt = self.amt.min(len);
|
|
self.snap_end = self.amt == len;
|
|
}
|
|
|
|
/// Scrolled by a distance the platform measures, which is the last place
|
|
/// a wheel notch or a finger is a float.
|
|
pub fn scroll(&mut self, amt: f32) {
|
|
self.amt -= Px::from_f32(amt);
|
|
self.update_amt();
|
|
}
|
|
}
|