1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add Ui.input_mut & InputState.ignore_key (#1212)

This commit is contained in:
cat-state
2022-02-15 16:14:24 +00:00
committed by GitHub
parent c8c871fcd1
commit c1569ed0d7
5 changed files with 86 additions and 48 deletions

View File

@@ -117,59 +117,27 @@ impl EasyMarkEditor {
fn shortcuts(ui: &Ui, code: &mut dyn TextBuffer, ccursor_range: &mut CCursorRange) -> bool {
let mut any_change = false;
for event in &ui.input().events {
if let Event::Key {
key,
pressed: true,
modifiers,
} = event
for (key, surrounding) in [
(Key::B, "*"), // *bold*
(Key::C, "`"), // `code`
(Key::I, "/"), // /italics/
(Key::L, "$"), // $subscript$
(Key::R, "^"), // ^superscript^
(Key::S, "~"), // ~strikethrough~
(Key::U, "_"), // _underline_
] {
if ui
.input_mut()
.consume_key(egui::Modifiers::new().command(true), key)
{
if modifiers.command_only() {
match &key {
// toggle *bold*
Key::B => {
toggle_surrounding(code, ccursor_range, "*");
any_change = true;
}
// toggle `code`
Key::C => {
toggle_surrounding(code, ccursor_range, "`");
any_change = true;
}
// toggle /italics/
Key::I => {
toggle_surrounding(code, ccursor_range, "/");
any_change = true;
}
// toggle $lowered$
Key::L => {
toggle_surrounding(code, ccursor_range, "$");
any_change = true;
}
// toggle ^raised^
Key::R => {
toggle_surrounding(code, ccursor_range, "^");
any_change = true;
}
// toggle ~strikethrough~
Key::S => {
toggle_surrounding(code, ccursor_range, "~");
any_change = true;
}
// toggle _underline_
Key::U => {
toggle_surrounding(code, ccursor_range, "_");
any_change = true;
}
_ => {}
}
}
}
toggle_surrounding(code, ccursor_range, surrounding);
any_change = true;
};
}
any_change
}
/// E.g. toggle *strong* with `toggle(&mut text, &mut cursor, "*")`
/// E.g. toggle *strong* with `toggle_surrounding(&mut text, &mut cursor, "*")`
fn toggle_surrounding(
code: &mut dyn TextBuffer,
ccursor_range: &mut CCursorRange,