Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shortcuts): improve key binding validity checks #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions config/src/shortcuts/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ impl Binding {
/// Check if the binding has been set
pub fn is_set(&self) -> bool {
(self.has_modifier() && self.key.is_some())
|| self.is_super()
|| self.key.map_or(false, |key| {
// Allow Home/End, Print, PageDown/Up, etc.
key.is_misc_function_key()
Expand Down Expand Up @@ -135,28 +136,27 @@ impl FromStr for Binding {
"alt" => binding.modifiers.alt = true,
"shift" => binding.modifiers.shift = true,
lowercased => {
let name = if token.chars().count() == 1 {
binding.key = Some(Keysym::from_char(lowercased.chars().next().unwrap()));
return Ok(binding);
let key = if token.chars().count() == 1 {
Keysym::from_char(lowercased.chars().next().unwrap())
} else {
token
};

return match xkb::keysym_from_name(&name, xkb::KEYSYM_NO_FLAGS) {
x if x.raw() == super::sym::NO_SYMBOL => {
Err(format!("'{name}' is not a valid key symbol"))
}

x => {
binding.key = Some(x);
Ok(binding)
let key = xkb::keysym_from_name(&token, xkb::KEYSYM_NO_FLAGS);
if key.raw() == super::sym::NO_SYMBOL {
return Err(format!("'{token}' is not a valid key symbol"));
}
key
};
if let Some(prev_key) = binding.key {
let prev_key = xkb::keysym_get_name(prev_key);
let new_key = xkb::keysym_get_name(key);
return Err(format!(
"Multiple keys were defined for this binding ('{prev_key}' and '{new_key}')"
));
}
binding.key = Some(key);
}
}
}

Err(format!("no key was defined for this binding"))
Ok(binding)
}
}

Expand Down Expand Up @@ -215,5 +215,10 @@ mod tests {
Some(xkbcommon::xkb::Keysym::space)
))
);

assert_eq!(
Binding::from_str("Super"),
Ok(Binding::new(Modifiers::new().logo(), None))
);
}
}