use super::{DEFAULT_QUAD_OPACITY, LayoutEngine, scroll_direction_has_axis};
use crate::{
layout::{
layout_kind::{ScrollAxis, ScrollDirection, ScrollState},
quad::{ClipRect, Quad, QuadContent},
scrollbar::ScrollBarStyle,
},
node::{border::BorderRadius, color::Color, transform::Transform2D},
time::Instant,
};
impl LayoutEngine {
pub(crate) fn begin_scrollbar_frame(&self, now: Instant) {
self.scrollbar_frame_at.set(now);
self.scrollbar_change_at.set(None);
}
pub(crate) fn take_scrollbar_change(&self) -> Option<Instant> {
self.scrollbar_change_at.take()
}
pub(super) fn push_scrollbar_quads(
&self,
state: &ScrollState,
direction: ScrollDirection,
style: &ScrollBarStyle,
clip: ClipRect,
quads: &mut Vec<Quad>,
) {
let reveal = state.reveal_scrollbar(style, self.scrollbar_frame_at.get());
if let Some(change) = reveal.next_change {
let first = self.scrollbar_change_at.get().map_or(change, |at| at.min(change));
self.scrollbar_change_at.set(Some(first));
}
if reveal.opacity <= 0.0 {
return;
}
let opacity = DEFAULT_QUAD_OPACITY * reveal.opacity;
let palette = self.palette.borrow();
let (hovered, dragged) = (state.hovered_bar(), state.dragged_bar());
for axis in [ScrollAxis::Vertical, ScrollAxis::Horizontal] {
if !scroll_direction_has_axis(direction, axis) {
continue;
}
let Some(bar) = state.scrollbar_geometry_for_axis(axis, style) else {
continue;
};
if style.track_color.a() > 0 {
let track = (bar.track_x, bar.track_y, bar.track_width, bar.track_height);
quads.push(bar_quad(track, style.track_color, style.track_radius, opacity, clip));
}
let thumb_color = style.thumb_when(&palette, hovered == Some(axis), dragged == Some(axis));
let thumb = (bar.thumb_x, bar.thumb_y, bar.thumb_width, bar.thumb_height);
quads.push(bar_quad(thumb, thumb_color, style.thumb_radius, opacity, clip));
}
}
}
fn bar_quad(
(x, y, width, height): (f32, f32, f32, f32),
color: Color,
radius: f32,
opacity: f32,
clip: ClipRect,
) -> Quad {
Quad {
x,
y,
width,
height,
opacity,
transform: Transform2D::IDENTITY,
transform_origin: None,
content: QuadContent::Rect { color, gradient: None },
border_radius: Some(BorderRadius::all(radius)),
border: None,
clip,
}
}