Skip to main content

glory_render/
splitter.rs

1use crate::{Error, KeyboardInput, Orientation, RangeValue};
2
3/// Finite range proposals. Application state remains application-owned.
4#[derive(Clone, Copy, Debug, PartialEq)]
5pub enum RangeCommand {
6    Decrease,
7    PageDecrease,
8    PageIncrease,
9    Increase,
10    Minimum,
11    Maximum,
12    Set(f64),
13}
14impl RangeCommand {
15    /// The argument is the separator line's orientation: movement is perpendicular.
16    pub fn splitter_key(line: Orientation, input: KeyboardInput<'_>) -> Option<Self> {
17        if input.editable
18            || input.composing
19            || input.control
20            || input.meta
21            || input.alt
22            || input.shift
23        {
24            return None;
25        }
26        match (line, input.key) {
27            (Orientation::Vertical, "ArrowLeft") | (Orientation::Horizontal, "ArrowUp") => {
28                Some(Self::Decrease)
29            }
30            (Orientation::Vertical, "ArrowRight") | (Orientation::Horizontal, "ArrowDown") => {
31                Some(Self::Increase)
32            }
33            (_, "Home") => Some(Self::Minimum),
34            (_, "End") => Some(Self::Maximum),
35            _ => None,
36        }
37    }
38    pub fn validate(self, range: RangeValue) -> Result<(), Error> {
39        if let Self::Set(value) = self
40            && (!value.is_finite() || value < range.min || value > range.max)
41        {
42            return Err(Error::InvalidEvent(
43                "range proposal must be finite and within accepted bounds",
44            ));
45        }
46        Ok(())
47    }
48}