mirror of
https://github.com/emilk/egui.git
synced 2026-08-30 05:10:03 -04:00
Keyboard shortcut helpers (#2202)
* eframe web: Add WebInfo::user_agent * Deprecate `Modifier::ALT_SHIFT` * Add code for formatting Modifiers and Key * Add type KeyboardShortcut * Code cleanup * Add Context::os/set_os to query/set what OS egui believes it is on * Add Fonts::has_glyph(s) * Add helper function for formatting keyboard shortcuts * Faster code * Add way to set a shortcut text on menu buttons * Cleanup * format_keyboard_shortcut -> format_shortcut * Add TODO about supporting more keyboard sumbols * Modifiers::plus * Use the new keyboard shortcuts in emark editor demo * Explain why ALT+SHIFT is a bad modifier combo * Fix doctest
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use egui::{Context, ScrollArea, Ui};
|
||||
use egui::{Context, Modifiers, ScrollArea, Ui};
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
use super::About;
|
||||
@@ -239,7 +239,7 @@ impl DemoWindows {
|
||||
fn desktop_ui(&mut self, ctx: &Context) {
|
||||
egui::SidePanel::right("egui_demo_panel")
|
||||
.resizable(false)
|
||||
.default_width(145.0)
|
||||
.default_width(150.0)
|
||||
.show(ctx, |ui| {
|
||||
egui::trace!(ui);
|
||||
ui.vertical_centered(|ui| {
|
||||
@@ -301,13 +301,42 @@ impl DemoWindows {
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
fn file_menu_button(ui: &mut Ui) {
|
||||
let organize_shortcut =
|
||||
egui::KeyboardShortcut::new(Modifiers::ALT | Modifiers::SHIFT, egui::Key::O);
|
||||
let reset_shortcut =
|
||||
egui::KeyboardShortcut::new(Modifiers::ALT | Modifiers::SHIFT, egui::Key::R);
|
||||
|
||||
// NOTE: we must check the shortcuts OUTSIDE of the actual "File" menu,
|
||||
// or else they would only be checked if the "File" menu was actually open!
|
||||
|
||||
if ui.input_mut().consume_shortcut(&organize_shortcut) {
|
||||
ui.ctx().memory().reset_areas();
|
||||
}
|
||||
|
||||
if ui.input_mut().consume_shortcut(&reset_shortcut) {
|
||||
*ui.ctx().memory() = Default::default();
|
||||
}
|
||||
|
||||
ui.menu_button("File", |ui| {
|
||||
if ui.button("Organize windows").clicked() {
|
||||
ui.set_min_width(220.0);
|
||||
ui.style_mut().wrap = Some(false);
|
||||
|
||||
if ui
|
||||
.add(
|
||||
egui::Button::new("Organize Windows")
|
||||
.shortcut_text(ui.ctx().format_shortcut(&organize_shortcut)),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
ui.ctx().memory().reset_areas();
|
||||
ui.close_menu();
|
||||
}
|
||||
|
||||
if ui
|
||||
.button("Reset egui memory")
|
||||
.add(
|
||||
egui::Button::new("Reset egui memory")
|
||||
.shortcut_text(ui.ctx().format_shortcut(&reset_shortcut)),
|
||||
)
|
||||
.on_hover_text("Forget scroll, positions, sizes etc")
|
||||
.clicked()
|
||||
{
|
||||
|
||||
@@ -106,23 +106,42 @@ impl EasyMarkEditor {
|
||||
}
|
||||
}
|
||||
|
||||
pub const SHORTCUT_BOLD: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::B);
|
||||
pub const SHORTCUT_CODE: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::N);
|
||||
pub const SHORTCUT_ITALICS: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::I);
|
||||
pub const SHORTCUT_SUBSCRIPT: KeyboardShortcut = KeyboardShortcut::new(Modifiers::COMMAND, Key::L);
|
||||
pub const SHORTCUT_SUPERSCRIPT: KeyboardShortcut =
|
||||
KeyboardShortcut::new(Modifiers::COMMAND, Key::Y);
|
||||
pub const SHORTCUT_STRIKETHROUGH: KeyboardShortcut =
|
||||
KeyboardShortcut::new(Modifiers::CTRL.plus(Modifiers::SHIFT), Key::Q);
|
||||
pub const SHORTCUT_UNDERLINE: KeyboardShortcut =
|
||||
KeyboardShortcut::new(Modifiers::CTRL.plus(Modifiers::SHIFT), Key::W);
|
||||
pub const SHORTCUT_INDENT: KeyboardShortcut =
|
||||
KeyboardShortcut::new(Modifiers::CTRL.plus(Modifiers::SHIFT), Key::E);
|
||||
|
||||
fn nested_hotkeys_ui(ui: &mut egui::Ui) {
|
||||
let _ = ui.label("CTRL+B *bold*");
|
||||
let _ = ui.label("CTRL+N `code`");
|
||||
let _ = ui.label("CTRL+I /italics/");
|
||||
let _ = ui.label("CTRL+L $subscript$");
|
||||
let _ = ui.label("CTRL+Y ^superscript^");
|
||||
let _ = ui.label("ALT+SHIFT+Q ~strikethrough~");
|
||||
let _ = ui.label("ALT+SHIFT+W _underline_");
|
||||
let _ = ui.label("ALT+SHIFT+E two spaces"); // Placeholder for tab indent
|
||||
egui::Grid::new("shortcuts").striped(true).show(ui, |ui| {
|
||||
let mut label = |shortcut, what| {
|
||||
ui.label(what);
|
||||
ui.weak(ui.ctx().format_shortcut(&shortcut));
|
||||
ui.end_row();
|
||||
};
|
||||
|
||||
label(SHORTCUT_BOLD, "*bold*");
|
||||
label(SHORTCUT_CODE, "`code`");
|
||||
label(SHORTCUT_ITALICS, "/italics/");
|
||||
label(SHORTCUT_SUBSCRIPT, "$subscript$");
|
||||
label(SHORTCUT_SUPERSCRIPT, "^superscript^");
|
||||
label(SHORTCUT_STRIKETHROUGH, "~strikethrough~");
|
||||
label(SHORTCUT_UNDERLINE, "_underline_");
|
||||
label(SHORTCUT_INDENT, "two spaces"); // Placeholder for tab indent
|
||||
});
|
||||
}
|
||||
|
||||
fn shortcuts(ui: &Ui, code: &mut dyn TextBuffer, ccursor_range: &mut CCursorRange) -> bool {
|
||||
let mut any_change = false;
|
||||
if ui
|
||||
.input_mut()
|
||||
.consume_key(egui::Modifiers::ALT_SHIFT, Key::E)
|
||||
{
|
||||
|
||||
if ui.input_mut().consume_shortcut(&SHORTCUT_INDENT) {
|
||||
// This is a placeholder till we can indent the active line
|
||||
any_change = true;
|
||||
let [primary, _secondary] = ccursor_range.sorted();
|
||||
@@ -131,20 +150,22 @@ fn shortcuts(ui: &Ui, code: &mut dyn TextBuffer, ccursor_range: &mut CCursorRang
|
||||
ccursor_range.primary.index += advance;
|
||||
ccursor_range.secondary.index += advance;
|
||||
}
|
||||
for (modifier, key, surrounding) in [
|
||||
(egui::Modifiers::COMMAND, Key::B, "*"), // *bold*
|
||||
(egui::Modifiers::COMMAND, Key::N, "`"), // `code`
|
||||
(egui::Modifiers::COMMAND, Key::I, "/"), // /italics/
|
||||
(egui::Modifiers::COMMAND, Key::L, "$"), // $subscript$
|
||||
(egui::Modifiers::COMMAND, Key::Y, "^"), // ^superscript^
|
||||
(egui::Modifiers::ALT_SHIFT, Key::Q, "~"), // ~strikethrough~
|
||||
(egui::Modifiers::ALT_SHIFT, Key::W, "_"), // _underline_
|
||||
|
||||
for (shortcut, surrounding) in [
|
||||
(SHORTCUT_BOLD, "*"),
|
||||
(SHORTCUT_CODE, "`"),
|
||||
(SHORTCUT_ITALICS, "/"),
|
||||
(SHORTCUT_SUBSCRIPT, "$"),
|
||||
(SHORTCUT_SUPERSCRIPT, "^"),
|
||||
(SHORTCUT_STRIKETHROUGH, "~"),
|
||||
(SHORTCUT_UNDERLINE, "_"),
|
||||
] {
|
||||
if ui.input_mut().consume_key(modifier, key) {
|
||||
if ui.input_mut().consume_shortcut(&shortcut) {
|
||||
any_change = true;
|
||||
toggle_surrounding(code, ccursor_range, surrounding);
|
||||
};
|
||||
}
|
||||
|
||||
any_change
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user