gpui: Apply cfg at compile time (#31428)

- Use compile time `cfg` macro instead of a runtime check
- Use `Modifiers` instead of a bunch of `bool` when parsing a
`Keystroke`.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-05-26 16:34:04 +03:00 committed by GitHub
parent f2601ce52c
commit 2e62f16149
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -94,37 +94,33 @@ impl Keystroke {
/// secondary means "cmd" on macOS and "ctrl" on other platforms /// secondary means "cmd" on macOS and "ctrl" on other platforms
/// when matching a key with an key_char set will be matched without it. /// when matching a key with an key_char set will be matched without it.
pub fn parse(source: &str) -> std::result::Result<Self, InvalidKeystrokeError> { pub fn parse(source: &str) -> std::result::Result<Self, InvalidKeystrokeError> {
let mut control = false; let mut modifiers = Modifiers::none();
let mut alt = false;
let mut shift = false;
let mut platform = false;
let mut function = false;
let mut key = None; let mut key = None;
let mut key_char = None; let mut key_char = None;
let mut components = source.split('-').peekable(); let mut components = source.split('-').peekable();
while let Some(component) = components.next() { while let Some(component) = components.next() {
if component.eq_ignore_ascii_case("ctrl") { if component.eq_ignore_ascii_case("ctrl") {
control = true; modifiers.control = true;
continue; continue;
} }
if component.eq_ignore_ascii_case("alt") { if component.eq_ignore_ascii_case("alt") {
alt = true; modifiers.alt = true;
continue; continue;
} }
if component.eq_ignore_ascii_case("shift") { if component.eq_ignore_ascii_case("shift") {
shift = true; modifiers.shift = true;
continue; continue;
} }
if component.eq_ignore_ascii_case("fn") { if component.eq_ignore_ascii_case("fn") {
function = true; modifiers.function = true;
continue; continue;
} }
if component.eq_ignore_ascii_case("secondary") { if component.eq_ignore_ascii_case("secondary") {
if cfg!(target_os = "macos") { if cfg!(target_os = "macos") {
platform = true; modifiers.platform = true;
} else { } else {
control = true; modifiers.control = true;
}; };
continue; continue;
} }
@ -134,7 +130,7 @@ impl Keystroke {
|| component.eq_ignore_ascii_case("win"); || component.eq_ignore_ascii_case("win");
if is_platform { if is_platform {
platform = true; modifiers.platform = true;
continue; continue;
} }
@ -158,7 +154,7 @@ impl Keystroke {
if component.len() == 1 && component.as_bytes()[0].is_ascii_uppercase() { if component.len() == 1 && component.as_bytes()[0].is_ascii_uppercase() {
// Convert to shift + lowercase char // Convert to shift + lowercase char
shift = true; modifiers.shift = true;
key_str.make_ascii_lowercase(); key_str.make_ascii_lowercase();
} else { } else {
// convert ascii chars to lowercase so that named keys like "tab" and "enter" // convert ascii chars to lowercase so that named keys like "tab" and "enter"
@ -170,37 +166,30 @@ impl Keystroke {
// Allow for the user to specify a keystroke modifier as the key itself // Allow for the user to specify a keystroke modifier as the key itself
// This sets the `key` to the modifier, and disables the modifier // This sets the `key` to the modifier, and disables the modifier
if key.is_none() { key = key.or_else(|| {
if shift { use std::mem;
key = Some("shift".to_string()); // std::mem::take clears bool incase its true
shift = false; if mem::take(&mut modifiers.shift) {
} else if control { Some("shift".to_string())
key = Some("control".to_string()); } else if mem::take(&mut modifiers.control) {
control = false; Some("control".to_string())
} else if alt { } else if mem::take(&mut modifiers.alt) {
key = Some("alt".to_string()); Some("alt".to_string())
alt = false; } else if mem::take(&mut modifiers.platform) {
} else if platform { Some("platform".to_string())
key = Some("platform".to_string()); } else if mem::take(&mut modifiers.function) {
platform = false; Some("function".to_string())
} else if function { } else {
key = Some("function".to_string()); None
function = false;
} }
} });
let key = key.ok_or_else(|| InvalidKeystrokeError { let key = key.ok_or_else(|| InvalidKeystrokeError {
keystroke: source.to_owned(), keystroke: source.to_owned(),
})?; })?;
Ok(Keystroke { Ok(Keystroke {
modifiers: Modifiers { modifiers,
control,
alt,
shift,
platform,
function,
},
key, key,
key_char, key_char,
}) })
@ -331,18 +320,18 @@ fn is_printable_key(key: &str) -> bool {
impl std::fmt::Display for Keystroke { impl std::fmt::Display for Keystroke {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.modifiers.control { if self.modifiers.control {
if cfg!(target_os = "macos") { #[cfg(target_os = "macos")]
f.write_char('^')?; f.write_char('^')?;
} else {
write!(f, "ctrl-")?; #[cfg(not(target_os = "macos"))]
} write!(f, "ctrl-")?;
} }
if self.modifiers.alt { if self.modifiers.alt {
if cfg!(target_os = "macos") { #[cfg(target_os = "macos")]
f.write_char('⌥')?; f.write_char('⌥')?;
} else {
write!(f, "alt-")?; #[cfg(not(target_os = "macos"))]
} write!(f, "alt-")?;
} }
if self.modifiers.platform { if self.modifiers.platform {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
@ -355,31 +344,38 @@ impl std::fmt::Display for Keystroke {
f.write_char('⊞')?; f.write_char('⊞')?;
} }
if self.modifiers.shift { if self.modifiers.shift {
if cfg!(target_os = "macos") { #[cfg(target_os = "macos")]
f.write_char('⇧')?; f.write_char('⇧')?;
} else {
write!(f, "shift-")?; #[cfg(not(target_os = "macos"))]
} write!(f, "shift-")?;
} }
let key = match self.key.as_str() { let key = match self.key.as_str() {
"backspace" if cfg!(target_os = "macos") => '⌫', #[cfg(target_os = "macos")]
"up" if cfg!(target_os = "macos") => '↑', "backspace" => '⌫',
"down" if cfg!(target_os = "macos") => '↓', #[cfg(target_os = "macos")]
"left" if cfg!(target_os = "macos") => '←', "up" => '↑',
"right" if cfg!(target_os = "macos") => '→', #[cfg(target_os = "macos")]
"tab" if cfg!(target_os = "macos") => '⇥', "down" => '↓',
"escape" if cfg!(target_os = "macos") => '⎋', #[cfg(target_os = "macos")]
"shift" if cfg!(target_os = "macos") => '⇧', "left" => '←',
"control" if cfg!(target_os = "macos") => '⌃', #[cfg(target_os = "macos")]
"alt" if cfg!(target_os = "macos") => '⌥', "right" => '→',
"platform" if cfg!(target_os = "macos") => '⌘', #[cfg(target_os = "macos")]
key => { "tab" => '⇥',
if key.len() == 1 { #[cfg(target_os = "macos")]
key.chars().next().unwrap().to_ascii_uppercase() "escape" => '⎋',
} else { #[cfg(target_os = "macos")]
return f.write_str(key); "shift" => '⇧',
} #[cfg(target_os = "macos")]
} "control" => '⌃',
#[cfg(target_os = "macos")]
"alt" => '⌥',
#[cfg(target_os = "macos")]
"platform" => '⌘',
key if key.len() == 1 => key.chars().next().unwrap().to_ascii_uppercase(),
key => return f.write_str(key),
}; };
f.write_char(key) f.write_char(key)
} }