use std::sync::{Arc, Mutex};
use crate::{
animation::Easing,
core::signal::Signal,
layout::{
Alignment, Offset, Size, StackAlignment,
scrollbar::{
ScrollBarGeometry, ScrollBarPart, ScrollBarReveal, ScrollBarRevealFrame, ScrollBarStyle, ScrollBarVisibility,
},
},
node::{dimension::Dimension, spacing_value::SpacingValue},
time::{Duration, Instant},
};
const DEFAULT_FLEX_GROW: f32 = 1.0;
const DEFAULT_FLEX_SHRINK: f32 = 0.0;
const SCROLL_END_EPSILON: f32 = 2.0;
const TEXT_OVERFLOW_EPSILON: f32 = 0.5;
const WHEEL_SCROLL_DURATION: Duration = Duration::from_millis(150);
const WHEEL_SCROLL_EASING: Easing = Easing::CubicBezier {
x1: 1.0 / 3.0,
y1: 1.0,
x2: 2.0 / 3.0,
y2: 1.0,
};
#[derive(Clone)]
pub enum LayoutKind {
Leaf,
Row {
spacing: SpacingValue,
align: Alignment,
justify: Justify,
wrap: FlexWrap,
},
Column {
spacing: SpacingValue,
align: Alignment,
justify: Justify,
wrap: FlexWrap,
},
Stack {
align: StackAlignment,
},
Grid(Box<crate::layout::grid::GridSpec>),
LogicalModifier,
ScrollModifier {
state: ScrollState,
direction: ScrollDirection,
culling: bool,
middle_button_autoscroll: bool,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum KeyboardDismissal {
#[default]
None,
OnDrag,
Interactive,
}
#[derive(Clone)]
pub struct ScrollState {
inner: Arc<Mutex<ScrollStateInner>>,
}
fn squeezed_thumb(start: f32, length: f32, overscroll: f32, bar_width: f32) -> (f32, f32) {
if overscroll == 0.0 {
return (start, length);
}
let squeezed = (length - overscroll.abs()).max(bar_width.min(length));
if overscroll < 0.0 {
(start, squeezed)
} else {
(start + length - squeezed, squeezed)
}
}
#[derive(Clone, Copy)]
enum PendingScroll {
Offset(f32),
Start,
End,
PreservePrependAnchor {
previous_content: f32,
previous_scroll: f32,
},
Reveal {
start: f32,
end: f32,
},
Anchor(f32),
}
impl PendingScroll {
fn is_request(self) -> bool {
!matches!(self, Self::PreservePrependAnchor { .. } | Self::Anchor(_))
}
fn moved_by(self, by: f32) -> Self {
match self {
Self::Anchor(offset) => Self::Anchor(offset + by),
Self::PreservePrependAnchor {
previous_content,
previous_scroll,
} => Self::PreservePrependAnchor {
previous_content,
previous_scroll: previous_scroll + by,
},
request => request,
}
}
}
#[derive(Clone, Copy)]
struct WheelScroll {
target: f32,
distance: f32,
turned: Instant,
stepped: f32,
to_end: bool,
}
impl WheelScroll {
fn to(target: f32, offset: f32, max: f32, now: Instant) -> Self {
Self {
target,
distance: target - offset,
turned: now,
stepped: offset,
to_end: max > 0.0 && max - target <= SCROLL_END_EPSILON,
}
}
fn turned(wheel: Option<Self>, offset: f32, max: f32, delta: f32, now: Instant) -> (Option<Self>, f32) {
if delta == 0.0 {
return (wheel, 0.0);
}
let from = wheel.map_or(offset, |wheel| wheel.target);
let target = (from + delta).clamp(0.0, max);
let wheel = (target != offset).then(|| Self::to(target, offset, max, now));
(wheel, target - from)
}
fn step(&mut self, offset: f32, max: f32, now: Instant) -> (f32, bool) {
self.target += offset - self.stepped;
let target = if self.to_end { max } else { self.target.clamp(0.0, max) };
if target != self.target {
*self = Self::to(target, offset, max, now);
}
let progress = now.saturating_duration_since(self.turned).as_secs_f64() / WHEEL_SCROLL_DURATION.as_secs_f64();
let next = if progress >= 1.0 {
self.target
} else {
let left = self.distance * (1.0 - WHEEL_SCROLL_EASING.evaluate(progress) as f32);
self.target - left
};
self.stepped = next;
(next, next == self.target)
}
}
#[derive(Clone, Copy)]
struct ScrollPassStart {
scroll_x: f32,
scroll_y: f32,
max_scroll_x: f32,
max_scroll_y: f32,
}
struct ScrollStateInner {
scroll_x: f32,
scroll_y: f32,
overscroll_x: f32,
overscroll_y: f32,
scrolls_to_top: bool,
keyboard_dismissal: KeyboardDismissal,
paging: bool,
held: bool,
page_target: Option<(f32, f32)>,
rested_page: (usize, usize),
page_signal: Option<Signal<(usize, usize)>>,
pending_scroll_x: Option<PendingScroll>,
pending_scroll_y: Option<PendingScroll>,
wheel_x: Option<WheelScroll>,
wheel_y: Option<WheelScroll>,
max_scroll_x: f32,
max_scroll_y: f32,
content_width: f32,
content_height: f32,
container_width: f32,
container_height: f32,
viewport_width: f32,
viewport_height: f32,
viewport_abs_x: f32,
viewport_abs_y: f32,
bar_clear_x: f32,
bar_clear_y: f32,
bar_clear_end_x: f32,
bar_clear_end_y: f32,
reserved_across: bool,
scrolls: (bool, bool),
layout_viewport_y: f32,
pass_start: Option<ScrollPassStart>,
thumb_hovered: bool,
hovered_bar: Option<ScrollAxis>,
pointer_over: bool,
reveal: ScrollBarReveal,
painted_scroll: Option<(f32, f32)>,
dragging: bool,
drag_axis: Option<ScrollAxis>,
drag_start_x: f32,
drag_start_y: f32,
drag_start_scroll_x: f32,
drag_start_scroll_y: f32,
scrollbar_style: ScrollBarStyle,
scroll_dirty: bool,
layout_subscribers: crate::core::layout_dirty::LayoutSubscribers,
equal_overflow_scroll_probe_enabled: bool,
sticks_to_end: bool,
held_at_end_x: bool,
held_at_end_y: bool,
scroll_y_requests: u64,
}
impl ScrollStateInner {
fn page_of(&self, offset: (f32, f32)) -> (usize, usize) {
let page = |offset: f32, size: f32| {
if size > 0.0 {
(offset / size).round().max(0.0) as usize
} else {
0
}
};
(
page(offset.0, self.viewport_width),
page(offset.1, self.viewport_height),
)
}
fn moves(&self) -> bool {
self.held || self.dragging || self.wheel_x.is_some() || self.wheel_y.is_some()
}
fn step_wheel(&mut self, now: Instant) -> bool {
let mut moved = false;
if let Some(mut wheel) = self.wheel_x.take() {
let (next, there) = wheel.step(self.scroll_x, self.max_scroll_x, now);
if next != self.scroll_x {
let by = next - self.scroll_x;
self.pending_scroll_x = self.pending_scroll_x.map(|pending| pending.moved_by(by));
self.scroll_x = next;
moved = true;
}
self.wheel_x = (!there).then_some(wheel);
}
if let Some(mut wheel) = self.wheel_y.take() {
let (next, there) = wheel.step(self.scroll_y, self.max_scroll_y, now);
if next != self.scroll_y {
let by = next - self.scroll_y;
self.pending_scroll_y = self.pending_scroll_y.map(|pending| pending.moved_by(by));
self.scroll_y = next;
moved = true;
}
self.wheel_y = (!there).then_some(wheel);
}
if moved {
self.scroll_dirty = true;
self.layout_subscribers.mark();
}
moved
}
}
impl ScrollState {
pub(crate) fn same(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
}
fn bar_track(inner: &ScrollStateInner, axis: ScrollAxis, style: &ScrollBarStyle) -> Option<(f32, f32, f32)> {
let (viewport, content, clear, clear_end) = match axis {
ScrollAxis::Horizontal => (
inner.viewport_width,
inner.content_width,
inner.bar_clear_x,
inner.bar_clear_end_x,
),
ScrollAxis::Vertical => (
inner.viewport_height,
inner.content_height,
inner.bar_clear_y,
inner.bar_clear_end_y,
),
};
let (clear, clear_end) = if clear + clear_end < viewport {
(clear, clear_end)
} else {
(0.0, 0.0)
};
let (other_scrolls, other_content, other_viewport) = match axis {
ScrollAxis::Horizontal => (inner.scrolls.1, inner.content_height, inner.viewport_height),
ScrollAxis::Vertical => (inner.scrolls.0, inner.content_width, inner.viewport_width),
};
let other_shows = match style.visible {
ScrollBarVisibility::Never => false,
ScrollBarVisibility::Always => true,
_ => other_content > other_viewport,
};
let other_reserved = axis == ScrollAxis::Vertical && inner.reserved_across;
let corner = if other_scrolls && other_shows && !other_reserved {
style.width + style.resolved_edge_inset()
} else {
0.0
};
let start = clear + style.resolved_end_inset();
let track = viewport - clear - clear_end - style.resolved_end_inset() * 2.0 - corner;
if track <= 0.0 {
return None;
}
let ratio = (viewport - clear - clear_end) / (content - clear - clear_end).max(1.0);
let travel = thumb_travel(track, style);
let thumb = (travel * ratio).max(style.min_thumb_length).min(travel);
Some((start, track, thumb))
}
fn thumb_travel(track: f32, style: &ScrollBarStyle) -> f32 {
(track - style.thumb_inset * 2.0).max(0.0)
}
impl Default for ScrollState {
fn default() -> Self {
Self::new()
}
}
impl ScrollState {
pub(crate) fn track_layout(&self, watch: &Arc<crate::core::layout_dirty::LayoutWatch>) {
self.inner.lock().unwrap().layout_subscribers.bind(watch);
}
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(ScrollStateInner {
scroll_x: 0.0,
scroll_y: 0.0,
overscroll_x: 0.0,
overscroll_y: 0.0,
scrolls_to_top: true,
keyboard_dismissal: KeyboardDismissal::None,
paging: false,
held: false,
page_target: None,
rested_page: (0, 0),
page_signal: None,
pending_scroll_x: None,
pending_scroll_y: None,
wheel_x: None,
wheel_y: None,
max_scroll_x: 0.0,
max_scroll_y: 0.0,
content_width: 0.0,
content_height: 0.0,
container_width: 0.0,
container_height: 0.0,
viewport_width: 0.0,
viewport_height: 0.0,
viewport_abs_x: 0.0,
viewport_abs_y: 0.0,
bar_clear_x: 0.0,
scrolls: (false, false),
bar_clear_y: 0.0,
bar_clear_end_x: 0.0,
bar_clear_end_y: 0.0,
reserved_across: false,
layout_viewport_y: 0.0,
pass_start: None,
thumb_hovered: false,
hovered_bar: None,
pointer_over: false,
reveal: ScrollBarReveal::default(),
painted_scroll: None,
dragging: false,
drag_axis: None,
drag_start_x: 0.0,
drag_start_y: 0.0,
drag_start_scroll_x: 0.0,
drag_start_scroll_y: 0.0,
scrollbar_style: ScrollBarStyle::default(),
scroll_dirty: false,
layout_subscribers: Default::default(),
equal_overflow_scroll_probe_enabled: false,
sticks_to_end: false,
held_at_end_x: false,
held_at_end_y: false,
scroll_y_requests: 0,
})),
}
}
pub fn scroll_x(&self) -> f32 {
self.inner.lock().unwrap().scroll_x
}
pub fn set_scrolls_to_top(&self, scrolls_to_top: bool) {
self.inner.lock().unwrap().scrolls_to_top = scrolls_to_top;
}
pub fn scrolls_to_top(&self) -> bool {
self.inner.lock().unwrap().scrolls_to_top
}
pub fn set_sticks_to_end(&self, sticks: bool) {
self.inner.lock().unwrap().sticks_to_end = sticks;
}
pub fn sticks_to_end(&self) -> bool {
self.inner.lock().unwrap().sticks_to_end
}
pub fn set_dismisses_keyboard(&self, dismisses_keyboard: bool) {
self.set_keyboard_dismissal(if dismisses_keyboard {
KeyboardDismissal::OnDrag
} else {
KeyboardDismissal::None
});
}
pub fn dismisses_keyboard(&self) -> bool {
self.keyboard_dismissal() == KeyboardDismissal::OnDrag
}
pub fn set_keyboard_dismissal(&self, dismissal: KeyboardDismissal) {
self.inner.lock().unwrap().keyboard_dismissal = dismissal;
}
pub fn keyboard_dismissal(&self) -> KeyboardDismissal {
self.inner.lock().unwrap().keyboard_dismissal
}
pub fn set_paging(&self, paging: bool) {
self.inner.lock().unwrap().paging = paging;
}
pub fn paging(&self) -> bool {
self.inner.lock().unwrap().paging
}
pub fn page(&self) -> (usize, usize) {
let inner = self.inner.lock().unwrap();
inner.page_of((inner.scroll_x, inner.scroll_y))
}
pub fn target_page(&self) -> (usize, usize) {
let inner = self.inner.lock().unwrap();
let going = inner.page_target.unwrap_or((
inner.wheel_x.map_or(inner.scroll_x, |wheel| wheel.target),
inner.wheel_y.map_or(inner.scroll_y, |wheel| wheel.target),
));
inner.page_of(going)
}
pub fn rested_page(&self) -> (usize, usize) {
self.inner.lock().unwrap().rested_page
}
pub fn page_signal(&self) -> Signal<(usize, usize)> {
let mut inner = self.inner.lock().unwrap();
let page = inner.rested_page;
inner.page_signal.get_or_insert_with(|| Signal::new(page)).clone()
}
pub fn moving(&self) -> bool {
let inner = self.inner.lock().unwrap();
inner.moves()
}
pub(crate) fn set_held(&self, held: bool) {
let mut inner = self.inner.lock().unwrap();
inner.held = held;
if !held {
inner.page_target = None;
}
}
pub(crate) fn held(&self) -> bool {
self.inner.lock().unwrap().held
}
pub(crate) fn set_page_target(&self, target: Option<(f32, f32)>) {
self.inner.lock().unwrap().page_target = target;
}
pub(crate) fn page_watched(&self) -> bool {
self.inner.lock().unwrap().page_signal.is_some()
}
pub(crate) fn rest_page(&self) -> Option<((usize, usize), (usize, usize))> {
self.take_page(false)
}
pub(crate) fn arrive_at_page(&self) -> Option<((usize, usize), (usize, usize))> {
self.take_page(true)
}
fn take_page(&self, moving: bool) -> Option<((usize, usize), (usize, usize))> {
let (before, page, signal) = {
let mut inner = self.inner.lock().unwrap();
if inner.moves() && !moving {
return None;
}
let page = inner.page_of((inner.scroll_x, inner.scroll_y));
if page == inner.rested_page {
return None;
}
let before = std::mem::replace(&mut inner.rested_page, page);
(before, page, inner.page_signal.clone())
};
if let Some(signal) = signal {
signal.set(page);
}
Some((before, page))
}
pub fn overscroll(&self) -> (f32, f32) {
let inner = self.inner.lock().unwrap();
(inner.overscroll_x, inner.overscroll_y)
}
pub fn set_overscroll(&self, x: f32, y: f32) {
let mut inner = self.inner.lock().unwrap();
if (inner.overscroll_x, inner.overscroll_y) == (x, y) {
return;
}
(inner.overscroll_x, inner.overscroll_y) = (x, y);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub(crate) fn content_offset(&self) -> crate::layout::offset::Offset {
let inner = self.inner.lock().unwrap();
crate::layout::offset::Offset::new(
-(inner.scroll_x + inner.overscroll_x),
-(inner.scroll_y + inner.overscroll_y),
)
}
pub fn scroll_y(&self) -> f32 {
self.inner.lock().unwrap().scroll_y
}
pub fn set_scroll(&self, x: f32, y: f32) {
self.stop_wheel();
self.keep_scroll(x, y);
}
pub(crate) fn keep_scroll(&self, x: f32, y: f32) {
let mut inner = self.inner.lock().unwrap();
let offset = (x.clamp(0.0, inner.max_scroll_x), y.clamp(0.0, inner.max_scroll_y));
inner.scroll_y_requests = inner.scroll_y_requests.wrapping_add(1);
if offset == (inner.scroll_x, inner.scroll_y) {
return;
}
(inner.scroll_x, inner.scroll_y) = offset;
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn set_scroll_pending(&self, x: f32, y: f32) {
let mut inner = self.inner.lock().unwrap();
(inner.wheel_x, inner.wheel_y) = (None, None);
let offset = (x.max(0.0), y.max(0.0));
let already_there = inner.pending_scroll_x.is_none()
&& inner.pending_scroll_y.is_none()
&& offset == (inner.scroll_x, inner.scroll_y);
inner.pending_scroll_x = Some(PendingScroll::Offset(offset.0));
inner.pending_scroll_y = Some(PendingScroll::Offset(offset.1));
if already_there {
return;
}
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub(crate) fn reveal_y_pending(&self, start: f32, end: f32) {
let mut inner = self.inner.lock().unwrap();
inner.wheel_y = None;
inner.pending_scroll_y = Some(PendingScroll::Reveal { start, end });
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub(crate) fn keep_scroll_y_pending(&self, y: f32) {
let mut inner = self.inner.lock().unwrap();
if inner.pending_scroll_y.is_some_and(PendingScroll::is_request) {
return;
}
inner.pending_scroll_y = Some(PendingScroll::Anchor(y.max(0.0)));
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn scroll_to_top_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.wheel_y = None;
inner.pending_scroll_y = Some(PendingScroll::Start);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn scroll_to_bottom_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.wheel_y = None;
inner.pending_scroll_y = Some(PendingScroll::End);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub(crate) fn opens_at_end(&self) -> bool {
matches!(self.inner.lock().unwrap().pending_scroll_y, Some(PendingScroll::End))
}
pub(crate) fn pending_scroll_y_offset(&self) -> Option<f32> {
match self.inner.lock().unwrap().pending_scroll_y {
Some(PendingScroll::Offset(offset)) => Some(offset),
_ => None,
}
}
pub(crate) fn drop_pending_scroll_y_offset(&self, offset: f32) {
let mut inner = self.inner.lock().unwrap();
if matches!(inner.pending_scroll_y, Some(PendingScroll::Offset(pending)) if pending == offset) {
inner.pending_scroll_y = None;
}
}
pub(crate) fn has_pending_scroll_y_request(&self) -> bool {
self
.inner
.lock()
.unwrap()
.pending_scroll_y
.is_some_and(PendingScroll::is_request)
}
pub(crate) fn next_layout_scroll_y(&self, content_height: f32) -> f32 {
let inner = self.inner.lock().unwrap();
let max_scroll = (content_height - inner.viewport_height).max(0.0);
let was_at_bottom = (inner.sticks_to_end || inner.held_at_end_y)
&& inner.max_scroll_y > 0.0
&& inner.max_scroll_y - inner.scroll_y <= SCROLL_END_EPSILON
&& inner.wheel_y.is_none();
let target = match inner.pending_scroll_y {
Some(pending) => resolve_pending_scroll(
pending,
content_height,
max_scroll,
inner.scroll_y,
inner.viewport_height,
),
None if was_at_bottom => max_scroll,
None => inner.scroll_y,
};
target.clamp(0.0, max_scroll)
}
pub(crate) fn scroll_y_requests(&self) -> u64 {
self.inner.lock().unwrap().scroll_y_requests
}
pub(crate) fn equal_overflow_scroll_probe_enabled(&self) -> bool {
self.inner.lock().unwrap().equal_overflow_scroll_probe_enabled
}
pub fn scroll_to_left_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.wheel_x = None;
inner.pending_scroll_x = Some(PendingScroll::Start);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn scroll_to_right_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.wheel_x = None;
inner.pending_scroll_x = Some(PendingScroll::End);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn stick_to_bottom_if_near_end(&self, threshold: f32) -> bool {
let mut inner = self.inner.lock().unwrap();
if inner.max_scroll_y - inner.scroll_y <= threshold.max(0.0) {
inner.wheel_y = None;
inner.pending_scroll_y = Some(PendingScroll::End);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
true
} else {
false
}
}
pub fn stick_to_right_if_near_end(&self, threshold: f32) -> bool {
let mut inner = self.inner.lock().unwrap();
if inner.max_scroll_x - inner.scroll_x <= threshold.max(0.0) {
inner.wheel_x = None;
inner.pending_scroll_x = Some(PendingScroll::End);
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
true
} else {
false
}
}
pub fn preserve_prepend_anchor_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.pending_scroll_y = Some(PendingScroll::PreservePrependAnchor {
previous_content: inner.content_height,
previous_scroll: inner.scroll_y,
});
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn preserve_horizontal_prepend_anchor_pending(&self) {
let mut inner = self.inner.lock().unwrap();
inner.pending_scroll_x = Some(PendingScroll::PreservePrependAnchor {
previous_content: inner.content_width,
previous_scroll: inner.scroll_x,
});
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn scroll_by(&self, dx: f32, dy: f32) {
self.scroll_by_with_overflow(dx, dy);
}
pub fn scroll_by_with_overflow(&self, dx: f32, dy: f32) -> (f32, f32) {
let mut inner = self.inner.lock().unwrap();
if dx != 0.0 {
inner.pending_scroll_x = None;
inner.wheel_x = None;
}
if dy != 0.0 {
inner.pending_scroll_y = None;
inner.wheel_y = None;
}
let old_x = inner.scroll_x;
let old_y = inner.scroll_y;
inner.scroll_x = (inner.scroll_x + dx).clamp(0.0, inner.max_scroll_x);
inner.scroll_y = (inner.scroll_y + dy).clamp(0.0, inner.max_scroll_y);
let consumed_x = inner.scroll_x - old_x;
let consumed_y = inner.scroll_y - old_y;
if consumed_x != 0.0 || consumed_y != 0.0 {
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
(dx - consumed_x, dy - consumed_y)
}
pub fn scroll_target(&self) -> (f32, f32) {
let inner = self.inner.lock().unwrap();
(
inner.wheel_x.map_or(inner.scroll_x, |wheel| wheel.target),
inner.wheel_y.map_or(inner.scroll_y, |wheel| wheel.target),
)
}
pub(crate) fn wheel_by_with_overflow(&self, dx: f32, dy: f32, now: Instant) -> (f32, f32) {
let mut inner = self.inner.lock().unwrap();
inner.step_wheel(now);
if dx != 0.0 && inner.pending_scroll_x.is_some_and(PendingScroll::is_request) {
inner.pending_scroll_x = None;
}
if dy != 0.0 && inner.pending_scroll_y.is_some_and(PendingScroll::is_request) {
inner.pending_scroll_y = None;
}
let (wheel_x, taken_x) = WheelScroll::turned(inner.wheel_x, inner.scroll_x, inner.max_scroll_x, dx, now);
let (wheel_y, taken_y) = WheelScroll::turned(inner.wheel_y, inner.scroll_y, inner.max_scroll_y, dy, now);
(inner.wheel_x, inner.wheel_y) = (wheel_x, wheel_y);
(dx - taken_x, dy - taken_y)
}
pub(crate) fn wheel_scrolling(&self) -> bool {
let inner = self.inner.lock().unwrap();
inner.wheel_x.is_some() || inner.wheel_y.is_some()
}
pub(crate) fn step_wheel(&self, now: Instant) -> bool {
self.inner.lock().unwrap().step_wheel(now)
}
pub(crate) fn end_wheel(&self) -> bool {
let mut inner = self.inner.lock().unwrap();
let Some(turned) = [inner.wheel_x, inner.wheel_y]
.into_iter()
.flatten()
.map(|wheel| wheel.turned)
.max()
else {
return false;
};
inner.step_wheel(turned + WHEEL_SCROLL_DURATION)
}
pub(crate) fn stop_wheel(&self) {
let mut inner = self.inner.lock().unwrap();
(inner.wheel_x, inner.wheel_y) = (None, None);
}
pub fn content_width(&self) -> f32 {
self.inner.lock().unwrap().content_width
}
pub fn content_height(&self) -> f32 {
self.inner.lock().unwrap().content_height
}
pub fn viewport_width(&self) -> f32 {
self.inner.lock().unwrap().viewport_width
}
pub fn viewport_height(&self) -> f32 {
self.inner.lock().unwrap().viewport_height
}
pub(crate) fn ptr_eq(&self, other: &ScrollState) -> bool {
Arc::ptr_eq(&self.inner, &other.inner)
}
pub fn style(&self) -> ScrollBarStyle {
self.inner.lock().unwrap().scrollbar_style.clone()
}
pub fn set_style(&self, style: ScrollBarStyle) {
self.inner.lock().unwrap().scrollbar_style = style;
}
pub fn is_thumb_hovered(&self) -> bool {
self.inner.lock().unwrap().thumb_hovered
}
pub fn is_dragging(&self) -> bool {
self.inner.lock().unwrap().dragging
}
pub fn hovered_bar(&self) -> Option<ScrollAxis> {
self.inner.lock().unwrap().hovered_bar
}
pub fn dragged_bar(&self) -> Option<ScrollAxis> {
let inner = self.inner.lock().unwrap();
inner.drag_axis.filter(|_| inner.dragging)
}
pub(crate) fn set_hovered_bar(&self, bar: Option<ScrollAxis>) {
let mut inner = self.inner.lock().unwrap();
inner.hovered_bar = bar;
inner.thumb_hovered = bar.is_some();
}
pub fn set_thumb_hovered(&self, hovered: bool) {
let mut inner = self.inner.lock().unwrap();
inner.thumb_hovered = hovered;
if !hovered {
inner.hovered_bar = None;
}
}
pub(crate) fn set_pointer_over(&self, over: bool) {
self.inner.lock().unwrap().pointer_over = over;
}
pub(crate) fn reveal_scrollbar(&self, style: &ScrollBarStyle, now: Instant) -> ScrollBarRevealFrame {
let mut inner = self.inner.lock().unwrap();
let offset = (inner.scroll_x + inner.overscroll_x, inner.scroll_y + inner.overscroll_y);
let scrolled = inner
.painted_scroll
.replace(offset)
.is_some_and(|painted| painted != offset);
if !style.visible.fades() {
inner.reveal = ScrollBarReveal::default();
return ScrollBarRevealFrame {
opacity: 1.0,
next_change: None,
};
}
let held = (style.visible == ScrollBarVisibility::OnHover && inner.pointer_over) || inner.dragging;
let overflows = inner.content_width > inner.viewport_width || inner.content_height > inner.viewport_height;
inner.reveal.advance(style, now, held, scrolled, overflows)
}
pub fn begin_drag(&self, mouse_y: f32) {
self.begin_drag_axis(ScrollAxis::Vertical, 0.0, mouse_y);
}
pub fn begin_drag_axis(&self, axis: ScrollAxis, mouse_x: f32, mouse_y: f32) {
let mut inner = self.inner.lock().unwrap();
match axis {
ScrollAxis::Horizontal => (inner.pending_scroll_x, inner.wheel_x) = (None, None),
ScrollAxis::Vertical => (inner.pending_scroll_y, inner.wheel_y) = (None, None),
}
inner.dragging = true;
inner.drag_axis = Some(axis);
inner.drag_start_x = mouse_x;
inner.drag_start_y = mouse_y;
inner.drag_start_scroll_x = inner.scroll_x;
inner.drag_start_scroll_y = inner.scroll_y;
}
pub fn end_drag(&self) {
let mut inner = self.inner.lock().unwrap();
inner.dragging = false;
inner.drag_axis = None;
}
pub fn drag_to(&self, mouse_y: f32, style: &crate::layout::scrollbar::ScrollBarStyle) {
self.drag_to_axis(ScrollAxis::Vertical, 0.0, mouse_y, style);
}
pub fn drag_to_axis(
&self,
axis: ScrollAxis,
mouse_x: f32,
mouse_y: f32,
style: &crate::layout::scrollbar::ScrollBarStyle,
) {
let mut inner = self.inner.lock().unwrap();
if !inner.dragging {
return;
}
match axis {
ScrollAxis::Horizontal => {
let Some((_, track_width, thumb_width)) = bar_track(&inner, ScrollAxis::Horizontal, style) else {
return;
};
let scrollable_track = thumb_travel(track_width, style) - thumb_width;
if scrollable_track <= 0.0 {
return;
}
let delta_px = mouse_x - inner.drag_start_x;
let scroll_delta = delta_px / scrollable_track * inner.max_scroll_x;
inner.wheel_x = None;
inner.scroll_x = (inner.drag_start_scroll_x + scroll_delta).clamp(0.0, inner.max_scroll_x);
}
ScrollAxis::Vertical => {
let Some((_, track_height, thumb_height)) = bar_track(&inner, ScrollAxis::Vertical, style) else {
return;
};
let scrollable_track = thumb_travel(track_height, style) - thumb_height;
if scrollable_track <= 0.0 {
return;
}
let delta_px = mouse_y - inner.drag_start_y;
let scroll_delta = delta_px / scrollable_track * inner.max_scroll_y;
inner.wheel_y = None;
inner.scroll_y = (inner.drag_start_scroll_y + scroll_delta).clamp(0.0, inner.max_scroll_y);
}
}
inner.scroll_dirty = true;
inner.layout_subscribers.mark();
}
pub fn thumb_rect(&self, style: &crate::layout::scrollbar::ScrollBarStyle) -> (f32, f32, f32, f32) {
let inner = self.inner.lock().unwrap();
let track_x = inner.viewport_abs_x + inner.viewport_width - style.width - style.resolved_edge_inset();
let (start, track_height, thumb_height) =
bar_track(&inner, ScrollAxis::Vertical, style).unwrap_or((style.resolved_end_inset(), 0.0, 0.0));
let track_y = inner.viewport_abs_y + start;
let scroll_ratio = if inner.max_scroll_y > 0.0 {
inner.scroll_y / inner.max_scroll_y
} else {
0.0
};
let inset = style.thumb_inset;
let thumb_y = track_y + inset + (thumb_travel(track_height, style) - thumb_height) * scroll_ratio;
(track_x + inset, thumb_y, style.width - inset * 2.0, thumb_height)
}
pub fn thumb_rect_for_axis(
&self,
axis: ScrollAxis,
style: &crate::layout::scrollbar::ScrollBarStyle,
) -> Option<(f32, f32, f32, f32)> {
let geo = self.scrollbar_geometry_for_axis(axis, style)?;
Some((geo.thumb_x, geo.thumb_y, geo.thumb_width, geo.thumb_height))
}
pub fn scrollbar_geometry_for_axis(
&self,
axis: ScrollAxis,
style: &crate::layout::scrollbar::ScrollBarStyle,
) -> Option<ScrollBarGeometry> {
let inner = self.inner.lock().unwrap();
match (style.visible, axis) {
(ScrollBarVisibility::Never, _) => return None,
(
ScrollBarVisibility::Auto | ScrollBarVisibility::OnHover | ScrollBarVisibility::WhileScrolling,
ScrollAxis::Horizontal,
) if inner.content_width <= inner.viewport_width => {
return None;
}
(
ScrollBarVisibility::Auto | ScrollBarVisibility::OnHover | ScrollBarVisibility::WhileScrolling,
ScrollAxis::Vertical,
) if inner.content_height <= inner.viewport_height => {
return None;
}
_ => {}
}
match axis {
ScrollAxis::Horizontal => {
let (start, track_width, thumb_width) = bar_track(&inner, ScrollAxis::Horizontal, style)?;
let track_x = inner.viewport_abs_x + start;
let track_y = inner.viewport_abs_y + inner.container_height - style.width - style.resolved_edge_inset();
let track_height = style.width;
if track_height <= 0.0 {
return None;
}
let max_scroll = (inner.content_width - inner.viewport_width).max(0.0);
let scroll_ratio = if max_scroll > 0.0 {
inner.scroll_x / max_scroll
} else {
0.0
};
let inset = style.thumb_inset;
let thickness = track_height - inset * 2.0;
let thumb_x = track_x + inset + (thumb_travel(track_width, style) - thumb_width) * scroll_ratio;
let (thumb_x, thumb_width) = squeezed_thumb(thumb_x, thumb_width, inner.overscroll_x, thickness);
Some(ScrollBarGeometry {
track_x,
track_y,
track_width,
track_height,
thumb_x,
thumb_y: track_y + inset,
thumb_width,
thumb_height: thickness,
})
}
ScrollAxis::Vertical => {
let (start, track_height, thumb_height) = bar_track(&inner, ScrollAxis::Vertical, style)?;
let track_x = inner.viewport_abs_x + inner.container_width - style.width - style.resolved_edge_inset();
let track_y = inner.viewport_abs_y + start;
let track_width = style.width;
if track_width <= 0.0 {
return None;
}
let max_scroll = (inner.content_height - inner.viewport_height).max(0.0);
let scroll_ratio = if max_scroll > 0.0 {
inner.scroll_y / max_scroll
} else {
0.0
};
let inset = style.thumb_inset;
let thickness = track_width - inset * 2.0;
let thumb_y = track_y + inset + (thumb_travel(track_height, style) - thumb_height) * scroll_ratio;
let (thumb_y, thumb_height) = squeezed_thumb(thumb_y, thumb_height, inner.overscroll_y, thickness);
Some(ScrollBarGeometry {
track_x,
track_y,
track_width,
track_height,
thumb_x: track_x + inset,
thumb_y,
thumb_width: thickness,
thumb_height,
})
}
}
}
pub(crate) fn scrollbar_part_at(
&self,
axis: ScrollAxis,
style: &ScrollBarStyle,
x: f32,
y: f32,
) -> Option<ScrollBarPart> {
let overflows = match axis {
ScrollAxis::Horizontal => self.content_width() > self.viewport_width(),
ScrollAxis::Vertical => self.content_height() > self.viewport_height(),
};
if !overflows {
return None;
}
if style.visible.fades() && !self.inner.lock().unwrap().reveal.is_shown() {
return None;
}
self
.scrollbar_geometry_for_axis(axis, style)?
.hit_part_at(axis == ScrollAxis::Horizontal, style, x, y)
}
pub(crate) fn page_axis(&self, axis: ScrollAxis, forward: bool) {
let page = match axis {
ScrollAxis::Horizontal => self.viewport_width(),
ScrollAxis::Vertical => self.viewport_height(),
};
let delta = if forward { page } else { -page };
match axis {
ScrollAxis::Horizontal => self.scroll_by(delta, 0.0),
ScrollAxis::Vertical => self.scroll_by(0.0, delta),
}
}
pub(crate) fn take_scroll_dirty(&self) -> bool {
let mut inner = self.inner.lock().unwrap();
let dirty = inner.scroll_dirty;
inner.scroll_dirty = false;
dirty
}
pub(crate) fn has_scroll_dirty(&self) -> bool {
self.inner.lock().unwrap().scroll_dirty
}
pub(crate) fn set_reserved_across(&self, reserved: bool) {
self.inner.lock().unwrap().reserved_across = reserved;
}
pub(crate) fn set_scrolls(&self, across: bool, down: bool) {
self.inner.lock().unwrap().scrolls = (across, down);
}
pub(crate) fn set_bar_clearance(&self, x: f32, y: f32, end_x: f32, end_y: f32) {
let mut inner = self.inner.lock().unwrap();
inner.bar_clear_x = x.max(0.0);
inner.bar_clear_y = y.max(0.0);
inner.bar_clear_end_x = end_x.max(0.0);
inner.bar_clear_end_y = end_y.max(0.0);
}
pub(crate) fn update_layout_with_container(
&self,
content_w: f32,
content_h: f32,
viewport_w: f32,
viewport_h: f32,
container_w: f32,
container_h: f32,
) {
let mut inner = self.inner.lock().unwrap();
let start = inner.pass_start.unwrap_or(ScrollPassStart {
scroll_x: inner.scroll_x,
scroll_y: inner.scroll_y,
max_scroll_x: inner.max_scroll_x,
max_scroll_y: inner.max_scroll_y,
});
inner.pass_start = Some(start);
let at_right = start.max_scroll_x > 0.0 && start.max_scroll_x - start.scroll_x <= SCROLL_END_EPSILON;
let at_bottom = start.max_scroll_y > 0.0 && start.max_scroll_y - start.scroll_y <= SCROLL_END_EPSILON;
if start.max_scroll_x > 0.0 && !at_right && inner.wheel_x.is_none() {
inner.held_at_end_x = false;
}
if start.max_scroll_y > 0.0 && !at_bottom && inner.wheel_y.is_none() {
inner.held_at_end_y = false;
}
let was_at_right = at_right && (inner.sticks_to_end || inner.held_at_end_x) && inner.wheel_x.is_none();
let was_at_bottom = at_bottom && (inner.sticks_to_end || inner.held_at_end_y) && inner.wheel_y.is_none();
match inner.pending_scroll_x {
Some(PendingScroll::End) => inner.held_at_end_x = true,
Some(pending) if pending.is_request() => inner.held_at_end_x = false,
_ => {}
}
match inner.pending_scroll_y {
Some(PendingScroll::End) => inner.held_at_end_y = true,
Some(pending) if pending.is_request() => inner.held_at_end_y = false,
_ => {}
}
inner.content_width = content_w;
inner.content_height = content_h;
inner.container_width = container_w;
inner.container_height = container_h;
inner.viewport_width = viewport_w.max(0.0);
inner.viewport_height = viewport_h.max(0.0);
inner.max_scroll_x = (content_w - inner.viewport_width).max(0.0);
inner.max_scroll_y = (content_h - inner.viewport_height).max(0.0);
inner.scroll_x = match inner.pending_scroll_x {
Some(pending) => resolve_pending_scroll(
pending,
inner.content_width,
inner.max_scroll_x,
start.scroll_x,
inner.viewport_width,
),
None if was_at_right => inner.max_scroll_x,
None => start.scroll_x,
};
inner.scroll_y = match inner.pending_scroll_y {
Some(pending) => resolve_pending_scroll(
pending,
inner.content_height,
inner.max_scroll_y,
start.scroll_y,
inner.viewport_height,
),
None if was_at_bottom => inner.max_scroll_y,
None => start.scroll_y,
};
inner.scroll_x = inner.scroll_x.clamp(0.0, inner.max_scroll_x);
inner.scroll_y = inner.scroll_y.clamp(0.0, inner.max_scroll_y);
}
pub(crate) fn finish_layout_pass(&self) {
let mut inner = self.inner.lock().unwrap();
if inner.pass_start.take().is_some() {
inner.pending_scroll_x = None;
if inner.pending_scroll_y.take().is_some_and(PendingScroll::is_request) {
inner.scroll_y_requests = inner.scroll_y_requests.wrapping_add(1);
}
}
}
pub(crate) fn set_text_layout(&self, viewport_width: f32, viewport_height: f32, content_height: f32, scroll_y: f32) {
let mut inner = self.inner.lock().unwrap();
inner.viewport_width = viewport_width.max(0.0);
inner.viewport_height = viewport_height.max(0.0);
inner.container_width = inner.viewport_width;
inner.container_height = inner.viewport_height;
inner.content_width = inner.viewport_width;
inner.content_height = if content_height - inner.viewport_height < TEXT_OVERFLOW_EPSILON {
content_height.min(inner.viewport_height)
} else {
content_height
};
inner.max_scroll_x = 0.0;
inner.max_scroll_y = (inner.content_height - inner.viewport_height).max(0.0);
inner.scroll_x = 0.0;
inner.scroll_y = scroll_y.clamp(0.0, inner.max_scroll_y);
}
pub(crate) fn set_layout_viewport_y(&self, y: f32) {
self.inner.lock().unwrap().layout_viewport_y = y;
}
pub(crate) fn layout_viewport_y(&self) -> f32 {
self.inner.lock().unwrap().layout_viewport_y
}
pub(crate) fn set_viewport_position(&self, x: f32, y: f32) {
let mut inner = self.inner.lock().unwrap();
inner.viewport_abs_x = x;
inner.viewport_abs_y = y;
}
}
fn resolve_pending_scroll(
pending: PendingScroll,
content_size: f32,
max_scroll: f32,
current: f32,
viewport: f32,
) -> f32 {
match pending {
PendingScroll::Offset(offset) | PendingScroll::Anchor(offset) => offset,
PendingScroll::Start => 0.0,
PendingScroll::End => max_scroll,
PendingScroll::PreservePrependAnchor {
previous_content,
previous_scroll,
} => previous_scroll + (content_size - previous_content),
PendingScroll::Reveal { start, end } => {
if start < current || end - start > viewport {
start
} else if end > current + viewport {
end - viewport
} else {
current
}
}
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct FlexParams {
pub grow: f32,
pub shrink: f32,
pub basis: Option<f32>,
}
impl FlexParams {
pub fn grow(factor: f32) -> Self {
Self {
grow: factor,
shrink: 0.0,
basis: None,
}
}
}
impl Default for FlexParams {
fn default() -> Self {
Self {
grow: DEFAULT_FLEX_GROW,
shrink: DEFAULT_FLEX_SHRINK,
basis: None,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ShrinkLimit {
#[default]
MinSize,
Content,
ContentFirst,
Drop,
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct ShrinkRule {
pub(crate) order: i32,
pub(crate) limit: ShrinkLimit,
pub(crate) drop_below: Option<f32>,
pub(crate) marker: bool,
}
type Bound = Option<(f32, bool)>;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct SizeBounds {
width: (Bound, Bound),
height: (Bound, Bound),
}
impl SizeBounds {
pub const fn new() -> Self {
Self {
width: (None, None),
height: (None, None),
}
}
pub const fn min_width(mut self, width: f32) -> Self {
self.width.0 = Some((width, true));
self
}
pub const fn above_width(mut self, width: f32) -> Self {
self.width.0 = Some((width, false));
self
}
pub const fn max_width(mut self, width: f32) -> Self {
self.width.1 = Some((width, true));
self
}
pub const fn below_width(mut self, width: f32) -> Self {
self.width.1 = Some((width, false));
self
}
pub const fn min_height(mut self, height: f32) -> Self {
self.height.0 = Some((height, true));
self
}
pub const fn above_height(mut self, height: f32) -> Self {
self.height.0 = Some((height, false));
self
}
pub const fn max_height(mut self, height: f32) -> Self {
self.height.1 = Some((height, true));
self
}
pub const fn below_height(mut self, height: f32) -> Self {
self.height.1 = Some((height, false));
self
}
pub fn holds(&self, size: Size) -> bool {
let within = |value: f32, (min, max): (Bound, Bound)| {
min.is_none_or(|(bound, inclusive)| if inclusive { value >= bound } else { value > bound })
&& max.is_none_or(|(bound, inclusive)| if inclusive { value <= bound } else { value < bound })
};
within(size.width, self.width) && within(size.height, self.height)
}
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum FlexWrap {
#[default]
NoWrap,
Wrap,
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum Justify {
#[default]
Start,
End,
Center,
SpaceBetween,
SpaceAround,
SpaceEvenly,
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum Overflow {
Visible,
#[default]
Hidden,
}
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum ScrollDirection {
Horizontal,
#[default]
Vertical,
Both,
}
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum ScrollAxis {
Horizontal,
Vertical,
}
#[derive(Clone, Copy, Default, PartialEq)]
pub struct FrameConstraints {
pub width: Option<Dimension>,
pub height: Option<Dimension>,
pub min_width: Option<Dimension>,
pub max_width: Option<Dimension>,
pub min_height: Option<Dimension>,
pub max_height: Option<Dimension>,
pub aspect_ratio: Option<f32>,
}
#[derive(Clone, Copy, Default, PartialEq)]
pub enum Position {
#[default]
Static,
Absolute {
x: f32,
y: f32,
width: Option<Dimension>,
height: Option<Dimension>,
},
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Sticky {
pub top: Option<f32>,
pub right: Option<f32>,
pub bottom: Option<f32>,
pub left: Option<f32>,
}
impl Sticky {
pub fn top(inset: f32) -> Self {
Self::default().with_top(inset)
}
pub fn bottom(inset: f32) -> Self {
Self::default().with_bottom(inset)
}
pub fn left(inset: f32) -> Self {
Self::default().with_left(inset)
}
pub fn right(inset: f32) -> Self {
Self::default().with_right(inset)
}
pub fn with_top(mut self, inset: f32) -> Self {
self.top = Some(inset);
self
}
pub fn with_right(mut self, inset: f32) -> Self {
self.right = Some(inset);
self
}
pub fn with_bottom(mut self, inset: f32) -> Self {
self.bottom = Some(inset);
self
}
pub fn with_left(mut self, inset: f32) -> Self {
self.left = Some(inset);
self
}
pub(crate) fn place(&self, natural: Offset, size: Size, viewport: [f32; 4], block: [f32; 4]) -> Offset {
Offset {
x: stick(
natural.x,
size.width,
self.left,
self.right,
(viewport[0], viewport[2]),
(block[0], block[2]),
),
y: stick(
natural.y,
size.height,
self.top,
self.bottom,
(viewport[1], viewport[3]),
(block[1], block[3]),
),
}
}
}
fn stick(
natural: f32,
length: f32,
start: Option<f32>,
end: Option<f32>,
viewport: (f32, f32),
block: (f32, f32),
) -> f32 {
let mut at = natural;
if let Some(end) = end.filter(|inset| inset.is_finite()) {
at = at.min(viewport.0 + viewport.1 - end - length);
}
if let Some(start) = start.filter(|inset| inset.is_finite()) {
at = at.max(viewport.0 + start);
}
let low = block.0.min(natural);
let high = (block.0 + block.1 - length).max(natural);
at.clamp(low, high)
}
impl FrameConstraints {
pub fn with_width(mut self, value: Option<Dimension>) -> Self {
self.width = value;
self
}
pub fn with_height(mut self, value: Option<Dimension>) -> Self {
self.height = value;
self
}
pub fn with_min_width(mut self, value: Option<Dimension>) -> Self {
self.min_width = value;
self
}
pub fn with_max_width(mut self, value: Option<Dimension>) -> Self {
self.max_width = value;
self
}
pub fn with_min_height(mut self, value: Option<Dimension>) -> Self {
self.min_height = value;
self
}
pub fn with_max_height(mut self, value: Option<Dimension>) -> Self {
self.max_height = value;
self
}
pub fn with_aspect_ratio(mut self, value: Option<f32>) -> Self {
self.aspect_ratio = value;
self
}
}