crisol-core 1.0.0

Crisol core: an egui-free data model, themes, metrics, localization, commands, state, and storage.
Documentation
//! The set of pressed modifier keys.

/// A set of pressed modifier keys.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
/// # Example
///
/// ```
/// use crisol_core::commands::Modifiers;
/// ```
pub struct Modifiers {
    /// Alt / Option.
    pub alt: bool,
    /// Control.
    pub ctrl: bool,
    /// Shift.
    pub shift: bool,
    /// The Command key on macOS.
    pub mac_cmd: bool,
    /// The platform command key: Ctrl elsewhere, Command on macOS.
    pub command: bool,
}

impl Modifiers {
    /// No modifiers.
    pub const NONE: Self = Self {
        alt: false,
        ctrl: false,
        shift: false,
        mac_cmd: false,
        command: false,
    };
    /// Alt / Option.
    pub const ALT: Self = Self {
        alt: true,
        ..Self::NONE
    };
    /// Control.
    pub const CTRL: Self = Self {
        ctrl: true,
        ..Self::NONE
    };
    /// Shift.
    pub const SHIFT: Self = Self {
        shift: true,
        ..Self::NONE
    };
    /// The platform command key.
    pub const COMMAND: Self = Self {
        command: true,
        ..Self::NONE
    };
    /// The macOS Command key.
    pub const MAC_CMD: Self = Self {
        mac_cmd: true,
        ..Self::NONE
    };

    /// Whether no modifier is set.
    pub const fn is_none(self) -> bool {
        !self.alt && !self.ctrl && !self.shift && !self.mac_cmd && !self.command
    }

    /// Whether the pressed set satisfies the pattern, treating `command` as the
    /// platform key, exactly as `egui::Modifiers::matches_logically` does.
    pub fn matches_logically(self, pattern: Self) -> bool {
        if pattern.alt && !self.alt {
            return false;
        }
        if pattern.shift && !self.shift {
            return false;
        }
        if pattern.mac_cmd {
            return self.mac_cmd && pattern.ctrl == self.ctrl;
        }
        if !pattern.ctrl && !pattern.command {
            return !self.ctrl && !self.command;
        }
        (!pattern.ctrl || self.ctrl) && (!pattern.command || self.command)
    }
}

impl std::ops::BitOr for Modifiers {
    type Output = Self;

    fn bitor(self, other: Self) -> Self {
        Self {
            alt: self.alt || other.alt,
            ctrl: self.ctrl || other.ctrl,
            shift: self.shift || other.shift,
            mac_cmd: self.mac_cmd || other.mac_cmd,
            command: self.command || other.command,
        }
    }
}

impl std::ops::BitOrAssign for Modifiers {
    fn bitor_assign(&mut self, other: Self) {
        *self = *self | other;
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn logical_matching_follows_the_platform_key() {
        assert!(Modifiers::COMMAND.matches_logically(Modifiers::COMMAND));
        assert!(Modifiers::CTRL.matches_logically(Modifiers::CTRL));
        assert!((Modifiers::CTRL | Modifiers::COMMAND).matches_logically(Modifiers::COMMAND));
        assert!(!Modifiers::NONE.matches_logically(Modifiers::COMMAND));
        assert!(Modifiers::MAC_CMD.matches_logically(Modifiers::MAC_CMD));
        assert!(!Modifiers::CTRL.matches_logically(Modifiers::MAC_CMD));
    }
}