ui: Add functions for generating textual representations of key bindings (#15287)

This PR adds some helper functions in the `ui` crate that can be used to
get textural representations of keystrokes or key bindings.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers 2024-07-26 12:52:59 -04:00 committed by GitHub
parent a5279cc48a
commit c937a2fcdd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 206 additions and 23 deletions

View file

@ -237,14 +237,15 @@ pub struct Modifiers {
}
impl Modifiers {
/// Returns true if any modifier key is pressed
/// Returns whether any modifier key is pressed.
pub fn modified(&self) -> bool {
self.control || self.alt || self.shift || self.platform || self.function
}
/// Whether the semantically 'secondary' modifier key is pressed
/// On macos, this is the command key
/// On windows and linux, this is the control key
/// Whether the semantically 'secondary' modifier key is pressed.
///
/// On macOS, this is the command key.
/// On Linux and Windows, this is the control key.
pub fn secondary(&self) -> bool {
#[cfg(target_os = "macos")]
{
@ -257,7 +258,7 @@ impl Modifiers {
}
}
/// How many modifier keys are pressed
/// Returns how many modifier keys are pressed.
pub fn number_of_modifiers(&self) -> u8 {
self.control as u8
+ self.alt as u8
@ -266,12 +267,12 @@ impl Modifiers {
+ self.function as u8
}
/// helper method for Modifiers with no modifiers
/// Returns [`Modifiers`] with no modifiers.
pub fn none() -> Modifiers {
Default::default()
}
/// helper method for Modifiers with just the command key
/// Returns [`Modifiers`] with just the command key.
pub fn command() -> Modifiers {
Modifiers {
platform: true,
@ -279,7 +280,7 @@ impl Modifiers {
}
}
/// A helper method for Modifiers with just the secondary key pressed
/// A Returns [`Modifiers`] with just the secondary key pressed.
pub fn secondary_key() -> Modifiers {
#[cfg(target_os = "macos")]
{
@ -298,7 +299,7 @@ impl Modifiers {
}
}
/// helper method for Modifiers with just the windows key
/// Returns [`Modifiers`] with just the windows key.
pub fn windows() -> Modifiers {
Modifiers {
platform: true,
@ -306,7 +307,7 @@ impl Modifiers {
}
}
/// helper method for Modifiers with just the super key
/// Returns [`Modifiers`] with just the super key.
pub fn super_key() -> Modifiers {
Modifiers {
platform: true,
@ -314,7 +315,7 @@ impl Modifiers {
}
}
/// helper method for Modifiers with just control
/// Returns [`Modifiers`] with just control.
pub fn control() -> Modifiers {
Modifiers {
control: true,
@ -322,7 +323,15 @@ impl Modifiers {
}
}
/// helper method for Modifiers with just shift
/// Returns [`Modifiers`] with just control.
pub fn alt() -> Modifiers {
Modifiers {
alt: true,
..Default::default()
}
}
/// Returns [`Modifiers`] with just shift.
pub fn shift() -> Modifiers {
Modifiers {
shift: true,
@ -330,7 +339,7 @@ impl Modifiers {
}
}
/// helper method for Modifiers with command + shift
/// Returns [`Modifiers`] with command + shift.
pub fn command_shift() -> Modifiers {
Modifiers {
shift: true,
@ -339,7 +348,7 @@ impl Modifiers {
}
}
/// helper method for Modifiers with command + shift
/// Returns [`Modifiers`] with command + shift.
pub fn control_shift() -> Modifiers {
Modifiers {
shift: true,
@ -348,7 +357,7 @@ impl Modifiers {
}
}
/// Checks if this Modifiers is a subset of another Modifiers
/// Checks if this [`Modifiers`] is a subset of another [`Modifiers`].
pub fn is_subset_of(&self, other: &Modifiers) -> bool {
(other.control || !self.control)
&& (other.alt || !self.alt)