From 98b869c15a126abc7c6c2ca4ff58f32120d3ab5c Mon Sep 17 00:00:00 2001 From: Victoria Brekenfeld Date: Thu, 7 Dec 2023 19:27:00 +0000 Subject: [PATCH] config: Allow receiving a string repr of a shortcut --- src/config/mod.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/config/mod.rs b/src/config/mod.rs index bffbc380..f3068e0f 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -54,6 +54,36 @@ pub struct StaticConfig { pub data_control_enabled: bool, } +impl StaticConfig { + pub fn get_shortcut_for_action(&self, action: &Action) -> Option { + let possible_variants = self + .key_bindings + .iter() + .filter(|(_, a)| *a == action) + .map(|(b, _)| b) + .collect::>(); + + possible_variants + .iter() + .find(|b| b.key.is_none()) // prefer short bindings + .or_else(|| { + possible_variants + .iter() // prefer bindings containing arrow keys + .find(|b| { + matches!( + b.key, + Some(Keysym::Down) + | Some(Keysym::Up) + | Some(Keysym::Left) + | Some(Keysym::Right) + ) + }) + }) + .or_else(|| possible_variants.first()) // take the first one + .map(|binding| binding.to_string()) + } +} + #[derive(Debug)] pub struct DynamicConfig { outputs: (Option, OutputsConfig),