Skip to content

Commit

Permalink
feat(display): Set up the other keybindings that sent supports for …
Browse files Browse the repository at this point in the history
…navigating through the presentation.
  • Loading branch information
zedseven committed Jan 11, 2024
1 parent e3004e4 commit 0789604
Showing 1 changed file with 50 additions and 13 deletions.
63 changes: 50 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ use glutin::surface::GlSurface;
use glutin_winit::GlWindow;
use old_school_gfx_glutin_ext::{window_builder as old_school_gfx_glutin_ext_window_builder, Init};
use winit::{
event::{ElementState, Event, WindowEvent},
event::{ElementState, Event, MouseButton, WindowEvent},
event_loop::{ControlFlow, EventLoop},
keyboard::{Key, NamedKey},
platform::modifier_supplement::KeyEventExtModifierSupplement,
window::WindowBuilder,
window::{Window, WindowBuilder},
};

use crate::sent::{Presentation, Slide};
Expand Down Expand Up @@ -223,23 +223,41 @@ fn run(presentation: &Presentation) -> AnyhowResult<()> {
gl_surface.swap_buffers(&gl_context).unwrap();
device.cleanup();
}
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Right | MouseButton::Back,
..
} => change_slide(&window, presentation, &mut current_slide, false),
WindowEvent::MouseInput {
state: ElementState::Pressed,
button: MouseButton::Left | MouseButton::Forward,
..
} => change_slide(&window, presentation, &mut current_slide, true),
WindowEvent::KeyboardInput { event, .. } => {
if event.state == ElementState::Pressed && !event.repeat {
// TODO: Functionality to reload the presentation
match event.key_without_modifiers().as_ref() {
Key::Named(NamedKey::Escape) | Key::Character("q") => {
window_target.exit()
window_target.exit();
}
Key::Named(NamedKey::ArrowLeft) => {
if current_slide > 0 {
current_slide -= 1;
window.request_redraw();
}
Key::Named(
NamedKey::ArrowLeft
| NamedKey::ArrowUp
| NamedKey::Backspace
| NamedKey::NavigatePrevious,
)
| Key::Character("h" | "k" | "p") => {
change_slide(&window, presentation, &mut current_slide, false);
}
Key::Named(NamedKey::ArrowRight) => {
if current_slide < presentation.0.len() - 1 {
current_slide += 1;
window.request_redraw();
}
Key::Named(
NamedKey::ArrowRight
| NamedKey::ArrowDown
| NamedKey::Enter
| NamedKey::Space
| NamedKey::NavigateNext,
)
| Key::Character("l" | "j" | "n") => {
change_slide(&window, presentation, &mut current_slide, true);
}
_ => {}
}
Expand All @@ -251,3 +269,22 @@ fn run(presentation: &Presentation) -> AnyhowResult<()> {
})
.with_context(|| "encountered an error during the event loop")
}

fn change_slide(
window: &Window,
presentation: &Presentation,
current_slide: &mut usize,
advance: bool,
) {
if advance {
if *current_slide < presentation.0.len() - 1 {
*current_slide += 1;
window.request_redraw();
}
} else {
if *current_slide > 0 {
*current_slide -= 1;
window.request_redraw();
}
}
}

0 comments on commit 0789604

Please sign in to comment.