diff --git a/src/bin/edit/draw_editor.rs b/src/bin/edit/draw_editor.rs index 69713a7d6309..ae1d2fc39817 100644 --- a/src/bin/edit/draw_editor.rs +++ b/src/bin/edit/draw_editor.rs @@ -239,11 +239,11 @@ pub fn draw_handle_wants_close(ctx: &mut Context, state: &mut State) { ctx.table_next_row(); ctx.inherit_focus(); - if ctx.button("yes", loc(LocId::UnsavedChangesDialogYes)) { + if ctx.shortcut_button("yes", loc(LocId::UnsavedChangesDialogYes), vk::S) { action = Action::Save; } ctx.inherit_focus(); - if ctx.button("no", loc(LocId::UnsavedChangesDialogNo)) { + if ctx.shortcut_button("no", loc(LocId::UnsavedChangesDialogNo), vk::N) { action = Action::Discard; } if ctx.button("cancel", loc(LocId::Cancel)) { diff --git a/src/tui.rs b/src/tui.rs index 1ea52d5f94c2..7b452c7ea806 100644 --- a/src/tui.rs +++ b/src/tui.rs @@ -1932,6 +1932,50 @@ impl<'a> Context<'a, '_> { self.button_activated() } + pub fn shortcut_button( + &mut self, + classname: &'static str, + text: &str, + shortcut: InputKey, + ) -> bool { + self.styled_label_begin(classname); + self.attr_focusable(); + if self.is_focused() { + self.attr_reverse(); + } + let shortcut_letter = shortcut.value() as u8 as char; + if shortcut_letter.is_ascii_uppercase() { + let mut shortcut_text = String::new(); + if shortcut.modifiers_contains(kbmod::CTRL) { + shortcut_text.push_str(self.tui.modifier_translations.ctrl); + shortcut_text.push('+'); + } + if shortcut.modifiers_contains(kbmod::ALT) { + shortcut_text.push_str(self.tui.modifier_translations.alt); + shortcut_text.push('+'); + } + if shortcut.modifiers_contains(kbmod::SHIFT) { + shortcut_text.push_str(self.tui.modifier_translations.shift); + shortcut_text.push('+'); + } + shortcut_text.push(shortcut_letter); + self.styled_label_add_text("["); + let button_text = format!("{} ({})", text, shortcut_text); + self.styled_label_add_text(&button_text); + self.styled_label_add_text("]"); + } else { + { + self.styled_label_add_text("["); + self.styled_label_add_text(text); + self.styled_label_add_text("]"); + } + } + + self.styled_label_end(); + + self.button_activated() + } + /// Creates a checkbox with the given text. /// Returns true if the checkbox was activated. pub fn checkbox(&mut self, classname: &'static str, text: &str, checked: &mut bool) -> bool {