Skip to content

Commit 7d67700

Browse files
committed
Add hotkeys to select the next symbol above/below the current one in the listing
1 parent 76f1952 commit 7d67700

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

objdiff-gui/src/hotkeys.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use egui::{style::ScrollAnimation, vec2, Context, Key, PointerButton};
1+
use egui::{style::ScrollAnimation, vec2, Context, Key, Modifiers, PointerButton};
22

33
pub fn enter_pressed(ctx: &Context) -> bool {
44
ctx.input_mut(|i| i.key_pressed(Key::Enter) || i.pointer.button_pressed(PointerButton::Extra2))
@@ -26,11 +26,11 @@ pub fn home_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key
2626

2727
pub fn end_pressed(ctx: &Context) -> bool { ctx.input_mut(|i| i.key_pressed(Key::End)) }
2828

29-
pub fn check_scroll_hotkeys(ui: &mut egui::Ui) {
29+
pub fn check_scroll_hotkeys(ui: &mut egui::Ui, include_small_increments: bool) {
3030
let ui_height = ui.available_rect_before_wrap().height();
31-
if up_pressed(ui.ctx()) {
31+
if up_pressed(ui.ctx()) && include_small_increments {
3232
ui.scroll_with_delta_animation(vec2(0.0, ui_height / 10.0), ScrollAnimation::none());
33-
} else if down_pressed(ui.ctx()) {
33+
} else if down_pressed(ui.ctx()) && include_small_increments {
3434
ui.scroll_with_delta_animation(vec2(0.0, -ui_height / 10.0), ScrollAnimation::none());
3535
} else if page_up_pressed(ui.ctx()) {
3636
ui.scroll_with_delta_animation(vec2(0.0, ui_height), ScrollAnimation::none());
@@ -42,3 +42,15 @@ pub fn check_scroll_hotkeys(ui: &mut egui::Ui) {
4242
ui.scroll_with_delta_animation(vec2(0.0, -f32::INFINITY), ScrollAnimation::none());
4343
}
4444
}
45+
46+
pub fn consume_up_key(ctx: &Context) -> bool {
47+
ctx.input_mut(|i| {
48+
i.consume_key(Modifiers::NONE, Key::ArrowUp) || i.consume_key(Modifiers::NONE, Key::W)
49+
})
50+
}
51+
52+
pub fn consume_down_key(ctx: &Context) -> bool {
53+
ctx.input_mut(|i| {
54+
i.consume_key(Modifiers::NONE, Key::ArrowDown) || i.consume_key(Modifiers::NONE, Key::S)
55+
})
56+
}

objdiff-gui/src/views/data_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ fn data_table_ui(
179179
let left_diffs = left_section.map(|(_, section)| split_diffs(&section.data_diff));
180180
let right_diffs = right_section.map(|(_, section)| split_diffs(&section.data_diff));
181181

182-
hotkeys::check_scroll_hotkeys(ui);
182+
hotkeys::check_scroll_hotkeys(ui, true);
183183

184184
render_table(ui, available_width, 2, config.code_font.size, total_rows, |row, column| {
185185
let i = row.index();

objdiff-gui/src/views/extab_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ pub fn extab_diff_ui(
235235
}
236236
});
237237

238-
hotkeys::check_scroll_hotkeys(ui);
238+
hotkeys::check_scroll_hotkeys(ui, true);
239239

240240
// Table
241241
render_strips(ui, available_width, 2, |ui, column| {

objdiff-gui/src/views/function_diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ fn asm_table_ui(
435435
};
436436
if left_len.is_some() && right_len.is_some() {
437437
// Joint view
438-
hotkeys::check_scroll_hotkeys(ui);
438+
hotkeys::check_scroll_hotkeys(ui, true);
439439
render_table(
440440
ui,
441441
available_width,

objdiff-gui/src/views/symbol_diff.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{collections::BTreeMap, mem::take};
1+
use std::{collections::BTreeMap, mem::take, ops::Bound};
22

33
use egui::{
44
text::LayoutJob, CollapsingHeader, Color32, Id, OpenUrl, ScrollArea, SelectableLabel, TextEdit,
@@ -649,7 +649,55 @@ pub fn symbol_list_ui(
649649
}
650650
}
651651

652-
hotkeys::check_scroll_hotkeys(ui);
652+
hotkeys::check_scroll_hotkeys(ui, false);
653+
654+
let mut new_key_value_to_highlight = None;
655+
if let Some(sym_ref) =
656+
if column == 0 { state.highlighted_symbol.0 } else { state.highlighted_symbol.1 }
657+
{
658+
let up = if hotkeys::consume_up_key(ui.ctx()) {
659+
Some(true)
660+
} else if hotkeys::consume_down_key(ui.ctx()) {
661+
Some(false)
662+
} else {
663+
None
664+
};
665+
if let Some(mut up) = up {
666+
if state.reverse_fn_order {
667+
up = !up;
668+
}
669+
new_key_value_to_highlight = if up {
670+
mapping.range(..sym_ref).next_back()
671+
} else {
672+
mapping.range((Bound::Excluded(sym_ref), Bound::Unbounded)).next()
673+
};
674+
};
675+
} else {
676+
// No symbol is highlighted in this column. Select the topmost symbol instead.
677+
// Note that we intentionally do not consume the up/down key presses in this case, but
678+
// we do when a symbol is highlighted. This is so that if only one column has a symbol
679+
// highlighted, that one takes precedence over the one with nothing highlighted.
680+
if hotkeys::up_pressed(ui.ctx()) || hotkeys::down_pressed(ui.ctx()) {
681+
new_key_value_to_highlight = if state.reverse_fn_order {
682+
mapping.last_key_value()
683+
} else {
684+
mapping.first_key_value()
685+
};
686+
}
687+
}
688+
if let Some((new_sym_ref, new_symbol_diff)) = new_key_value_to_highlight {
689+
ret = Some(if column == 0 {
690+
DiffViewAction::SetSymbolHighlight(
691+
Some(*new_sym_ref),
692+
new_symbol_diff.target_symbol,
693+
)
694+
} else {
695+
DiffViewAction::SetSymbolHighlight(
696+
new_symbol_diff.target_symbol,
697+
Some(*new_sym_ref),
698+
)
699+
});
700+
}
653701

654702
ui.scope(|ui| {
655703
ui.style_mut().override_text_style = Some(egui::TextStyle::Monospace);

0 commit comments

Comments
 (0)