Skip to content

Commit aaf6c1c

Browse files
authored
refactor(show): split the key handler into grid, navigation and tool keys (S3776) (storytold#1402)
1 parent 16fe4eb commit aaf6c1c

1 file changed

Lines changed: 114 additions & 74 deletions

File tree

‎crates/ui-egui/src/show.rs‎

Lines changed: 114 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -739,84 +739,19 @@ fn keyboard(ctx: &egui::Context, show: &mut Show, doc: &Presentation, now: f64,
739739
let mut keep = true;
740740
let mut actions = vec![];
741741
for (k, m) in keys {
742-
use egui::Key::*;
743-
let presenter_grid = presenter && show.presenter_grid;
744-
let grid_open = if presenter { show.presenter_grid } else { show.grid };
745-
if grid_open && !matches!(k, Escape | Minus) {
742+
let handled = if grid_open(show, presenter) && !matches!(k, egui::Key::Escape | egui::Key::Minus) {
746743
// See All Slides takes no other keys until a slide is chosen.
747-
if k == G && plain(m) {
748-
if presenter_grid {
749-
show.presenter_grid = false;
750-
} else {
751-
show.grid = false;
752-
}
744+
if k == egui::Key::G && plain(m) {
745+
close_grid(show, presenter);
746+
true
753747
} else {
754-
continue;
748+
false
755749
}
756750
} else {
757-
match k {
758-
Escape | Minus => {
759-
let cancelled = k == Escape && show.clear_goto();
760-
if !cancelled && !show.back_out(presenter) {
761-
keep = false;
762-
}
763-
}
764-
ArrowRight | ArrowDown | Space | PageDown | N => {
765-
if show.ended {
766-
keep = false;
767-
} else {
768-
actions.push(show.state.next(doc));
769-
}
770-
}
771-
Enter => {
772-
if !show.goto.is_empty() {
773-
if let Some(i) = show.take_goto()
774-
&& i < doc.slides.len()
775-
{
776-
actions.push(show.state.goto(doc, i));
777-
}
778-
} else if show.ended {
779-
keep = false;
780-
} else {
781-
actions.push(show.state.next(doc));
782-
}
783-
}
784-
Backspace if !show.goto.is_empty() => {
785-
show.goto.pop();
786-
}
787-
ArrowLeft | ArrowUp | Backspace | PageUp | P if !(k == P && m.command) => {
788-
show.ended = false;
789-
actions.push(show.state.prev(doc));
790-
}
791-
P if m.command => show.set_pen(!show.pen),
792-
L if m.command => show.set_laser(!show.laser),
793-
A if m.command => show.arrow(),
794-
Home => {
795-
if let Some(i) = show.state.first_index(doc) {
796-
actions.push(show.state.goto(doc, i));
797-
}
798-
}
799-
End => {
800-
if let Some(i) = show.state.last_index(doc) {
801-
actions.push(show.state.goto(doc, i));
802-
}
803-
}
804-
B | Period => toggle_blank(show, Color32::BLACK),
805-
W | Comma => toggle_blank(show, Color32::WHITE),
806-
E => show.strokes.clear(),
807-
G if plain(m) => show.open_grid(presenter),
808-
Z if plain(m) => show.zoom_key(),
809-
T if plain(m) => show.timer.toggle(now),
810-
R if plain(m) => show.timer.reset(now),
811-
OpenBracket if plain(m) => show.nudge_notes(-NOTES_STEP),
812-
CloseBracket if plain(m) => show.nudge_notes(NOTES_STEP),
813-
Num0 | Num1 | Num2 | Num3 | Num4 | Num5 | Num6 | Num7 | Num8 | Num9 => {
814-
if let Some(d) = k.name().chars().next() {
815-
show.push_slide_number(d, now);
816-
}
817-
}
818-
_ => continue,
819-
}
751+
navigation_key(show, doc, k, m, presenter, &mut keep, &mut actions) || tool_key(show, k, m, now, presenter)
752+
};
753+
if !handled {
754+
continue;
820755
}
821756
// The same key must not also activate a focused presenter button or be handled again
822757
// by an embedded presenter viewport.
@@ -831,6 +766,111 @@ fn keyboard(ctx: &egui::Context, show: &mut Show, doc: &Presentation, now: f64,
831766
keep
832767
}
833768

769+
/// Whether the grid that the presenter key G opens is showing in this viewport.
770+
fn grid_open(show: &Show, presenter: bool) -> bool {
771+
if presenter { show.presenter_grid } else { show.grid }
772+
}
773+
774+
/// Closes the grid that G opens in this viewport.
775+
fn close_grid(show: &mut Show, presenter: bool) {
776+
if presenter && show.presenter_grid {
777+
show.presenter_grid = false;
778+
} else {
779+
show.grid = false;
780+
}
781+
}
782+
783+
/// Keys that move through the show or end it. False when `k` isn't one of them.
784+
fn navigation_key(
785+
show: &mut Show,
786+
doc: &Presentation,
787+
k: egui::Key,
788+
m: egui::Modifiers,
789+
presenter: bool,
790+
keep: &mut bool,
791+
actions: &mut Vec<ShowAction>,
792+
) -> bool {
793+
use egui::Key::*;
794+
match k {
795+
Escape | Minus => {
796+
let cancelled = k == Escape && show.clear_goto();
797+
if !cancelled && !show.back_out(presenter) {
798+
*keep = false;
799+
}
800+
}
801+
ArrowRight | ArrowDown | Space | PageDown | N => {
802+
if show.ended {
803+
*keep = false;
804+
} else {
805+
actions.push(show.state.next(doc));
806+
}
807+
}
808+
Enter => {
809+
if !show.goto.is_empty() {
810+
if let Some(i) = show.take_goto()
811+
&& i < doc.slides.len()
812+
{
813+
actions.push(show.state.goto(doc, i));
814+
}
815+
} else if show.ended {
816+
*keep = false;
817+
} else {
818+
actions.push(show.state.next(doc));
819+
}
820+
}
821+
Backspace if !show.goto.is_empty() => {
822+
show.goto.pop();
823+
}
824+
ArrowLeft | ArrowUp | Backspace | PageUp => {
825+
show.ended = false;
826+
actions.push(show.state.prev(doc));
827+
}
828+
P if m.command => show.set_pen(!show.pen),
829+
P => {
830+
show.ended = false;
831+
actions.push(show.state.prev(doc));
832+
}
833+
Home => {
834+
if let Some(i) = show.state.first_index(doc) {
835+
actions.push(show.state.goto(doc, i));
836+
}
837+
}
838+
End => {
839+
if let Some(i) = show.state.last_index(doc) {
840+
actions.push(show.state.goto(doc, i));
841+
}
842+
}
843+
_ => return false,
844+
}
845+
true
846+
}
847+
848+
/// Pen, laser, blanking, zoom, the timer, note size and typed slide numbers. False when `k`
849+
/// isn't one of them.
850+
fn tool_key(show: &mut Show, k: egui::Key, m: egui::Modifiers, now: f64, presenter: bool) -> bool {
851+
use egui::Key::*;
852+
match k {
853+
L if m.command => show.set_laser(!show.laser),
854+
A if m.command => show.arrow(),
855+
B | Period => toggle_blank(show, Color32::BLACK),
856+
W | Comma => toggle_blank(show, Color32::WHITE),
857+
E => show.strokes.clear(),
858+
G if plain(m) => show.open_grid(presenter),
859+
Z if plain(m) => show.zoom_key(),
860+
T if plain(m) => show.timer.toggle(now),
861+
R if plain(m) => show.timer.reset(now),
862+
OpenBracket if plain(m) => show.nudge_notes(-NOTES_STEP),
863+
CloseBracket if plain(m) => show.nudge_notes(NOTES_STEP),
864+
Num0 | Num1 | Num2 | Num3 | Num4 | Num5 | Num6 | Num7 | Num8 | Num9 => {
865+
if let Some(d) = k.name().chars().next() {
866+
show.push_slide_number(d, now);
867+
}
868+
}
869+
_ => return false,
870+
}
871+
true
872+
}
873+
834874
/// Draw and advance the active show, handling hidden frames, timing, input, transitions, and
835875
/// presenter/reading presentation.
836876
pub fn ui(app: &mut SlideApp, ui: &mut Ui) {

0 commit comments

Comments
 (0)