#![cfg_attr(test, allow(dead_code))]
use iced::{
Element, Length,
widget::{Space, column, row, text},
};
use knotra_ui::widget::{
list,
overlay::{OverlayWidth, surface},
};
use crate::{
message::{KeyboardMessage, Message},
state::{
AppState,
focus::{FocusTarget, LayerFocus},
},
};
mod focus_target {
pub const CLOSE: &str = "shortcuts.close";
}
pub fn layer_focus() -> LayerFocus<Message> {
let close_target = FocusTarget::control(focus_target::CLOSE);
LayerFocus {
order: vec![(
close_target.clone(),
Some(Message::KeyEvent(KeyboardMessage::CheatSheetToggled)),
)],
entry: Some(close_target),
}
}
pub(crate) struct Binding {
pub(crate) keys: &'static str,
where_key: &'static str,
action_key: &'static str,
}
pub(crate) const BINDINGS: &[Binding] = &[
Binding {
keys: "Tab / Shift+Tab",
where_key: "shortcut.where_everywhere",
action_key: "shortcut.action_focus_move",
},
Binding {
keys: "Enter / Space",
where_key: "shortcut.where_everywhere",
action_key: "shortcut.action_press",
},
Binding {
keys: "Esc",
where_key: "shortcut.where_everywhere",
action_key: "shortcut.action_close_or_clear",
},
Binding {
keys: "↑ / ↓",
where_key: "shortcut.where_window",
action_key: "shortcut.action_focus_move",
},
Binding {
keys: "↑ / ↓ / j / k",
where_key: "shortcut.ctx_dashboard",
action_key: "shortcut.action_move_between_projects",
},
Binding {
keys: "/ or Ctrl+/",
where_key: "shortcut.ctx_dashboard",
action_key: "shortcut.action_go_to_search",
},
Binding {
keys: "Ctrl+R",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_check_now",
},
Binding {
keys: "Ctrl+K",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_change_work_area",
},
Binding {
keys: "Ctrl+T",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_save_release_point",
},
Binding {
keys: "Ctrl+Plus / Ctrl+=",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_zoom_in",
},
Binding {
keys: "Ctrl+Minus",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_zoom_out",
},
Binding {
keys: "Ctrl+0",
where_key: "shortcut.where_main_screens",
action_key: "shortcut.action_zoom_reset",
},
];
const KEYS_COL: f32 = 180.0;
const WHERE_COL: f32 = 150.0;
const ACTION_COL: f32 = 320.0;
pub fn view(state: &AppState) -> Element<'_, Message> {
let tokens = &state.theme.tokens;
let col_header = row![
text(state.t("shortcut.column_keys"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(KEYS_COL)),
text(state.t("shortcut.column_where"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(WHERE_COL)),
text(state.t("shortcut.column_action"))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(ACTION_COL)),
]
.spacing(16);
let rows: Vec<Element<'_, Message>> = BINDINGS
.iter()
.map(|b| {
row![
text(b.keys)
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(KEYS_COL)),
text(state.t(b.where_key))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(WHERE_COL)),
text(state.t(b.action_key))
.size(snora::design::style::text::body_size(tokens))
.line_height(snora::design::style::text::body_line_height(tokens))
.width(Length::Fixed(ACTION_COL)),
]
.spacing(16)
.into()
})
.collect();
let body = column![col_header, list(tokens, rows)].spacing(8);
surface(
tokens,
OverlayWidth::Large.resolve(state.window_width),
state.t("shortcut.overlay_title"),
Some(Message::KeyEvent(KeyboardMessage::CheatSheetToggled)),
crate::app::ring(state, &FocusTarget::control(focus_target::CLOSE)),
body,
Space::new(),
)
}
#[cfg(test)]
mod tests {
use super::*;
use knotra_ui::i18n::{Catalog, Locale};
#[test]
fn every_binding_key_resolves_in_both_catalogs() {
let en = Catalog::for_locale(Locale::En);
let ja = Catalog::for_locale(Locale::Ja);
let mut missing = Vec::new();
for b in BINDINGS {
for key in [b.where_key, b.action_key] {
if !en.contains_key(key) {
missing.push(format!("{key} (missing from English)"));
}
if !ja.contains_key(key) {
missing.push(format!("{key} (missing from Japanese)"));
}
}
}
assert!(
missing.is_empty(),
"these shortcut.* keys, referenced by BINDINGS, do not resolve: {missing:?}"
);
}
}