Skip to content

Commit

Permalink
Add support for panning with arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
bryevdv committed Oct 5, 2023
1 parent f0d1d37 commit bbb6ea9
Show file tree
Hide file tree
Showing 4 changed files with 258 additions and 115 deletions.
70 changes: 69 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ client = ["dep:reqwest", "dep:url"]
server = ["dep:actix-cors", "dep:actix-web"]

[dependencies]
percentage = "0.1.0"
egui = "0.22.0"
egui_extras = "0.22.0"
eframe = { version = "0.22.0", default-features = false, features = [
Expand Down
35 changes: 35 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use egui::{
Align2, Color32, NumExt, Pos2, Rect, RichText, ScrollArea, Slider, Stroke, TextStyle, Vec2,
};
use egui_extras::{Column, TableBuilder};
use percentage::{Percentage, PercentageInteger};
use serde::{Deserialize, Serialize};

use crate::data::{
Expand Down Expand Up @@ -1669,6 +1670,11 @@ impl Window {
}
}

enum PanDirection {
Left,
Right,
}

impl ProfApp {
/// Called once before the first frame.
pub fn new(
Expand Down Expand Up @@ -1711,6 +1717,19 @@ impl ProfApp {
result
}

fn pan(cx: &mut Context, percent: PercentageInteger, dir: PanDirection) {
if percent.value() == 0 {
return;
}

let duration: i64 = percent.apply_to(cx.view_interval.duration_ns());
let sign: i64 = match dir {
PanDirection::Left => -1,
PanDirection::Right => 1,
};
cx.view_interval = cx.view_interval.translate(duration * sign)
}

fn zoom(cx: &mut Context, interval: Interval) {
if cx.view_interval == interval {
return;
Expand Down Expand Up @@ -1783,6 +1802,7 @@ impl ProfApp {
UndoZoom,
RedoZoom,
ResetZoom,
Pan(PercentageInteger, PanDirection),
ExpandVertical,
ShrinkVertical,
ResetVertical,
Expand Down Expand Up @@ -1814,8 +1834,20 @@ impl ProfApp {
} else {
Actions::NoAction
}
} else if i.modifiers.shift {
if i.key_pressed(egui::Key::ArrowLeft) {
Actions::Pan(Percentage::from(1), PanDirection::Left)
} else if i.key_pressed(egui::Key::ArrowRight) {
Actions::Pan(Percentage::from(1), PanDirection::Right)
} else {
Actions::NoAction
}
} else if i.key_pressed(egui::Key::H) {
Actions::ToggleControls
} else if i.key_pressed(egui::Key::ArrowLeft) {
Actions::Pan(Percentage::from(5), PanDirection::Left)
} else if i.key_pressed(egui::Key::ArrowRight) {
Actions::Pan(Percentage::from(5), PanDirection::Right)
} else {
Actions::NoAction
}
Expand All @@ -1826,6 +1858,7 @@ impl ProfApp {
Actions::UndoZoom => ProfApp::undo_zoom(cx),
Actions::RedoZoom => ProfApp::redo_zoom(cx),
Actions::ResetZoom => ProfApp::zoom(cx, cx.total_interval),
Actions::Pan(percent, dir) => ProfApp::pan(cx, percent, dir),
Actions::ExpandVertical => ProfApp::multiply_scale_factor(cx, 2.0),
Actions::ShrinkVertical => ProfApp::multiply_scale_factor(cx, 0.5),
Actions::ResetVertical => ProfApp::reset_scale_factor(cx),
Expand Down Expand Up @@ -1966,6 +1999,8 @@ impl ProfApp {
});
};
show_row("Zoom to Interval", "Click and Drag");
show_row("Pan 5%", "Left/Right Arrow");
show_row("Pan 1%", "Shift + Left/Right Arrow");
show_row("Zoom In", "Ctrl + Plus/Equals");
show_row("Zoom Out", "Ctrl + Minus");
show_row("Undo Zoom", "Ctrl + Left Arrow");
Expand Down
Loading

0 comments on commit bbb6ea9

Please sign in to comment.