Skip to main content

glory_render/
scroll.rs

1//! Finite viewport scrolling commands and CSSOM-like geometry observations.
2use crate::{Error, KeyboardInput, Orientation};
3
4#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
5pub enum ScrollAxes {
6    #[default]
7    Both,
8    Horizontal,
9    Vertical,
10}
11impl ScrollAxes {
12    pub fn as_str(self) -> &'static str {
13        match self {
14            Self::Both => "both",
15            Self::Horizontal => "horizontal",
16            Self::Vertical => "vertical",
17        }
18    }
19    pub fn allows(self, axis: Orientation) -> bool {
20        matches!(self, Self::Both)
21            || matches!(
22                (self, axis),
23                (Self::Horizontal, Orientation::Horizontal)
24                    | (Self::Vertical, Orientation::Vertical)
25            )
26    }
27    /// Hosts call this only when the viewport itself owns actual focus. Native
28    /// descendants keep their editing, activation and collection navigation.
29    pub fn key(self, input: KeyboardInput<'_>) -> Option<ScrollCommand> {
30        if input.editable
31            || input.composing
32            || input.control
33            || input.meta
34            || input.alt
35            || input.shift
36        {
37            return None;
38        }
39        let primary = if self == Self::Horizontal {
40            Orientation::Horizontal
41        } else {
42            Orientation::Vertical
43        };
44        let (axis, movement) = match input.key {
45            "ArrowLeft" => (Orientation::Horizontal, ScrollMovement::StepNegative),
46            "ArrowRight" => (Orientation::Horizontal, ScrollMovement::StepPositive),
47            "ArrowUp" => (Orientation::Vertical, ScrollMovement::StepNegative),
48            "ArrowDown" => (Orientation::Vertical, ScrollMovement::StepPositive),
49            "PageUp" => (primary, ScrollMovement::PageNegative),
50            "PageDown" => (primary, ScrollMovement::PagePositive),
51            "Home" => (primary, ScrollMovement::Start),
52            "End" => (primary, ScrollMovement::End),
53            _ => return None,
54        };
55        self.allows(axis)
56            .then_some(ScrollCommand { axis, movement })
57    }
58}
59
60#[derive(Clone, Copy, Debug, PartialEq, Eq)]
61pub enum ScrollMovement {
62    StepNegative,
63    StepPositive,
64    PageNegative,
65    PagePositive,
66    Start,
67    End,
68}
69#[derive(Clone, Copy, Debug, PartialEq, Eq)]
70pub struct ScrollCommand {
71    pub axis: Orientation,
72    pub movement: ScrollMovement,
73}
74
75/// Logical pixels at one accepted query watermark. Native/browser offsets can
76/// be negative (RTL/elastic scrolling); dimensions are finite and nonnegative.
77/// Content dimensions are independent and need not exceed viewport dimensions.
78#[derive(Clone, Copy, Debug, Default, PartialEq)]
79pub struct ScrollMetrics {
80    pub x: f64,
81    pub y: f64,
82    pub viewport_width: f64,
83    pub viewport_height: f64,
84    pub content_width: f64,
85    pub content_height: f64,
86}
87impl ScrollMetrics {
88    pub fn validate(self) -> Result<(), Error> {
89        if ![
90            self.x,
91            self.y,
92            self.viewport_width,
93            self.viewport_height,
94            self.content_width,
95            self.content_height,
96        ]
97        .into_iter()
98        .all(f64::is_finite)
99            || self.viewport_width < 0.0
100            || self.viewport_height < 0.0
101            || self.content_width < 0.0
102            || self.content_height < 0.0
103        {
104            return Err(Error::InvalidResponse("invalid scroll metrics"));
105        }
106        Ok(())
107    }
108}