ui/hotkey_overlay: Reorganize bind formatting logic a bit
Browse files at this commite1afa712385bc4f2124a3bb7438743d3fdc1854aParent: 9b622b11 changed file +38 -26
- src/ui/hotkey_overlay.rs +38 -26
src/ui/hotkey_overlay.rsmodified+38 -26
| @@ -125,7 +125,7 @@ impl HotkeyOverlay { | |||
| 125 | } | 125 | } |
| 126 | } | 126 | } |
| 127 | 127 | ||
| 128 | -fn format_bind(binds: &[Bind], mod_key: ModKey, action: &Action) -> Option<(String, String)> { | 128 | +fn format_bind(binds: &[Bind], action: &Action) -> Option<(Option<Key>, String)> { |
| 129 | let mut bind_with_non_null = None; | 129 | let mut bind_with_non_null = None; |
| 130 | let mut bind_with_custom_title = None; | 130 | let mut bind_with_custom_title = None; |
| 131 | let mut found_null_title = false; | 131 | let mut found_null_title = false; |
| @@ -158,33 +158,16 @@ fn format_bind(binds: &[Bind], mod_key: ModKey, action: &Action) -> Option<(Stri | |||
| 158 | title = Some(custom.clone()); | 158 | title = Some(custom.clone()); |
| 159 | } | 159 | } |
| 160 | 160 | ||
| 161 | - key_name(mod_key, &bind.key) | 161 | + Some(bind.key) |
| 162 | } else { | 162 | } else { |
| 163 | - String::from("(not bound)") | 163 | + None |
| 164 | }; | 164 | }; |
| 165 | let title = title.unwrap_or_else(|| action_name(action)); | 165 | let title = title.unwrap_or_else(|| action_name(action)); |
| 166 | 166 | ||
| 167 | - Some((format!(" {key} "), title)) | 167 | + Some((key, title)) |
| 168 | } | 168 | } |
| 169 | 169 | ||
| 170 | -fn render( | 170 | +fn collect_actions(config: &Config) -> Vec<&Action> { |
| 171 | - renderer: &mut GlesRenderer, | ||
| 172 | - config: &Config, | ||
| 173 | - mod_key: ModKey, | ||
| 174 | - scale: f64, | ||
| 175 | -) -> anyhow::Result<RenderedOverlay> { | ||
| 176 | - let _span = tracy_client::span!("hotkey_overlay::render"); | ||
| 177 | - | ||
| 178 | - // let margin = MARGIN * scale; | ||
| 179 | - let padding: i32 = to_physical_precise_round(scale, PADDING); | ||
| 180 | - let line_interval: i32 = to_physical_precise_round(scale, LINE_INTERVAL); | ||
| 181 | - | ||
| 182 | - // FIXME: if it doesn't fit, try splitting in two columns or something. | ||
| 183 | - // let mut target_size = output_size; | ||
| 184 | - // target_size.w -= margin * 2; | ||
| 185 | - // target_size.h -= margin * 2; | ||
| 186 | - // anyhow::ensure!(target_size.w > 0 && target_size.h > 0); | ||
| 187 | - | ||
| 188 | let binds = &config.binds.0; | 171 | let binds = &config.binds.0; |
| 189 | 172 | ||
| 190 | // Collect actions that we want to show. | 173 | // Collect actions that we want to show. |
| @@ -290,9 +273,36 @@ fn render( | |||
| 290 | actions.retain(|&action| binds.iter().any(|bind| bind.action == *action)) | 273 | actions.retain(|&action| binds.iter().any(|bind| bind.action == *action)) |
| 291 | } | 274 | } |
| 292 | 275 | ||
| 293 | - let strings = actions | 276 | + actions |
| 277 | +} | ||
| 278 | + | ||
| 279 | +fn render( | ||
| 280 | + renderer: &mut GlesRenderer, | ||
| 281 | + config: &Config, | ||
| 282 | + mod_key: ModKey, | ||
| 283 | + scale: f64, | ||
| 284 | +) -> anyhow::Result<RenderedOverlay> { | ||
| 285 | + let _span = tracy_client::span!("hotkey_overlay::render"); | ||
| 286 | + | ||
| 287 | + // let margin = MARGIN * scale; | ||
| 288 | + let padding: i32 = to_physical_precise_round(scale, PADDING); | ||
| 289 | + let line_interval: i32 = to_physical_precise_round(scale, LINE_INTERVAL); | ||
| 290 | + | ||
| 291 | + // FIXME: if it doesn't fit, try splitting in two columns or something. | ||
| 292 | + // let mut target_size = output_size; | ||
| 293 | + // target_size.w -= margin * 2; | ||
| 294 | + // target_size.h -= margin * 2; | ||
| 295 | + // anyhow::ensure!(target_size.w > 0 && target_size.h > 0); | ||
| 296 | + | ||
| 297 | + let strings = collect_actions(config) | ||
| 294 | .into_iter() | 298 | .into_iter() |
| 295 | - .filter_map(|action| format_bind(binds, mod_key, action)) | 299 | + .filter_map(|action| format_bind(&config.binds.0, action)) |
| 300 | + .map(|(key, action)| { | ||
| 301 | + let key = key.map(|key| key_name(mod_key, &key)); | ||
| 302 | + let key = key.as_deref().unwrap_or("(not bound)"); | ||
| 303 | + let key = format!(" {key} "); | ||
| 304 | + (key, action) | ||
| 305 | + }) | ||
| 296 | .collect::<Vec<_>>(); | 306 | .collect::<Vec<_>>(); |
| 297 | 307 | ||
| 298 | let mut font = FontDescription::from_string(FONT); | 308 | let mut font = FontDescription::from_string(FONT); |
| @@ -563,8 +573,10 @@ mod tests { | |||
| 563 | #[track_caller] | 573 | #[track_caller] |
| 564 | fn check(config: &str, action: Action) -> String { | 574 | fn check(config: &str, action: Action) -> String { |
| 565 | let config = Config::parse("test.kdl", config).unwrap(); | 575 | let config = Config::parse("test.kdl", config).unwrap(); |
| 566 | - if let Some((key, title)) = format_bind(&config.binds.0, ModKey::Super, &action) { | 576 | + if let Some((key, title)) = format_bind(&config.binds.0, &action) { |
| 567 | - format!("{key}: {title}") | 577 | + let key = key.map(|key| key_name(ModKey::Super, &key)); |
| 578 | + let key = key.as_deref().unwrap_or("(not bound)"); | ||
| 579 | + format!(" {key} : {title}") | ||
| 568 | } else { | 580 | } else { |
| 569 | String::from("None") | 581 | String::from("None") |
| 570 | } | 582 | } |