From 20d072d61561215cdb1ab676bc8397e9275db8b9 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Tue, 10 Sep 2024 02:29:50 -0700 Subject: [PATCH] Combine key-down and key-up handlers --- app-proto/inversive-display/src/main.rs | 45 ++++++++++--------------- 1 file changed, 17 insertions(+), 28 deletions(-) diff --git a/app-proto/inversive-display/src/main.rs b/app-proto/inversive-display/src/main.rs index ef2628a..f516ed8 100644 --- a/app-proto/inversive-display/src/main.rs +++ b/app-proto/inversive-display/src/main.rs @@ -495,6 +495,21 @@ fn main() { start_animation_loop(); }); + let set_nav_signal = move |event: KeyboardEvent, value: f64| { + let mut navigating = true; + match event.key().as_str() { + "ArrowUp" => pitch_up.set(value), + "ArrowDown" => pitch_down.set(value), + "ArrowRight" => yaw_right.set(value), + "ArrowLeft" => yaw_left.set(value), + _ => navigating = false + }; + if navigating { + scene_changed.set(true); + event.prevent_default(); + } + }; + view! { div(id="app") { div(class="tab-pane") { @@ -523,34 +538,8 @@ fn main() { width=600, height=600, tabindex=0, - on:keydown=move |event: KeyboardEvent| { - let mut navigating = true; - match event.key().as_str() { - "ArrowUp" => pitch_up.set(1.0), - "ArrowDown" => pitch_down.set(1.0), - "ArrowRight" => yaw_right.set(1.0), - "ArrowLeft" => yaw_left.set(1.0), - _ => navigating = false - }; - if navigating { - scene_changed.set(true); - event.prevent_default(); - } - }, - on:keyup=move |event: KeyboardEvent| { - let mut navigating = true; - match event.key().as_str() { - "ArrowUp" => pitch_up.set(0.0), - "ArrowDown" => pitch_down.set(0.0), - "ArrowRight" => yaw_right.set(0.0), - "ArrowLeft" => yaw_left.set(0.0), - _ => navigating = false - }; - if navigating { - scene_changed.set(true); - event.prevent_default(); - } - }, + on:keydown=move |event: KeyboardEvent| { set_nav_signal(event, 1.0); }, + on:keyup=move |event: KeyboardEvent| { set_nav_signal(event, 0.0); }, on:blur=move |_| { pitch_up.set(0.0); pitch_down.set(0.0);