Skip to content

Commit 5bc5076

Browse files
committed
feat: fix shortcuts working when buttons disabled
Also ensure they never go out of sync
1 parent 1f628e5 commit 5bc5076

File tree

1 file changed

+22
-61
lines changed

1 file changed

+22
-61
lines changed

src/app.rs

Lines changed: 22 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -437,50 +437,8 @@ impl LogViewerApp {
437437
}
438438
}
439439

440-
fn check_shortcuts(&mut self, ui: &mut egui::Ui) {
441-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.prev)) {
442-
self.move_selected_prev();
443-
}
444-
445-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.next)) {
446-
self.move_selected_next();
447-
}
448-
449-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.first)) {
450-
self.move_selected_first();
451-
}
452-
453-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.last)) {
454-
self.move_selected_last();
455-
}
456-
457-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.unfilter)) {
458-
if let Some(data) = self.data.as_mut() {
459-
data.unfilter();
460-
}
461-
}
462-
463-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.open)) {
464-
self.loading_status = self.initiate_loading(ui.ctx().clone());
465-
}
466-
467-
#[cfg(not(target_arch = "wasm32"))]
468-
{
469-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.reload)) {
470-
self.loading_status = self.reload_file();
471-
}
472-
473-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.load_latest)) {
474-
self.loading_status = self.load_most_recent_file();
475-
}
476-
}
477-
478-
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.apply_filter)) {
479-
if let Some(data) = self.data.as_mut() {
480-
data.apply_filter(self.data_display_options.common_fields());
481-
}
482-
}
483-
440+
/// These shortcuts are always enabled
441+
fn check_global_shortcuts(&mut self, ui: &mut egui::Ui) {
484442
if ui.input_mut(|i| i.consume_shortcut(&self.shortcuts.search)) {
485443
self.focus_search_text_edit();
486444
}
@@ -509,12 +467,11 @@ impl LogViewerApp {
509467
}
510468
let mut should_apply_filter = false;
511469
if is_filter_enabled {
512-
if shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter).clicked() {
470+
if shortcut_button(ui, "Apply", "", &self.shortcuts.apply_filter) {
513471
should_apply_filter = true;
514472
}
515473
if data.is_filtered()
516474
&& shortcut_button(ui, "Unfilter", "Clears Filter ", &self.shortcuts.unfilter)
517-
.clicked()
518475
{
519476
data.unfilter();
520477
}
@@ -598,32 +555,30 @@ impl LogViewerApp {
598555

599556
fn navigation_ui(&mut self, ui: &mut egui::Ui) {
600557
ui.label("Nav:");
601-
if shortcut_button(ui, "⏪", "First ", &self.shortcuts.first).clicked() {
558+
if shortcut_button(ui, "⏪", "First ", &self.shortcuts.first) {
602559
self.move_selected_first();
603560
}
604-
if shortcut_button(ui, "⬆", "Previous ", &self.shortcuts.prev).clicked() {
561+
if shortcut_button(ui, "⬆", "Previous ", &self.shortcuts.prev) {
605562
self.move_selected_prev();
606563
}
607-
if shortcut_button(ui, "⬇", "Next ", &self.shortcuts.next).clicked() {
564+
if shortcut_button(ui, "⬇", "Next ", &self.shortcuts.next) {
608565
self.move_selected_next();
609566
}
610-
if shortcut_button(ui, "⏩", "Last ", &self.shortcuts.last).clicked() {
567+
if shortcut_button(ui, "⏩", "Last ", &self.shortcuts.last) {
611568
self.move_selected_last();
612569
}
613570
}
614571
fn data_load_ui(&mut self, ui: &mut egui::Ui) {
615572
ui.horizontal(|ui| {
616-
if shortcut_button(ui, "📂 Open log file...", "", &self.shortcuts.open).clicked() {
573+
if shortcut_button(ui, "📂 Open log file...", "", &self.shortcuts.open) {
617574
self.loading_status = self.initiate_loading(ui.ctx().clone());
618575
}
619576
#[cfg(not(target_arch = "wasm32"))]
620577
{
621-
if shortcut_button(ui, "Reload", "", &self.shortcuts.reload).clicked() {
578+
if shortcut_button(ui, "Reload", "", &self.shortcuts.reload) {
622579
self.loading_status = self.reload_file();
623580
}
624-
if shortcut_button(ui, "Load Most Recent File", "", &self.shortcuts.load_latest)
625-
.clicked()
626-
{
581+
if shortcut_button(ui, "Load Most Recent File", "", &self.shortcuts.load_latest) {
627582
self.loading_status = self.load_most_recent_file();
628583
}
629584
}
@@ -701,7 +656,7 @@ impl eframe::App for LogViewerApp {
701656
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
702657
// The top panel is often a good place for a menu bar:
703658

704-
self.check_shortcuts(ui);
659+
self.check_global_shortcuts(ui);
705660

706661
egui::menu::bar(ui, |ui| {
707662
// NOTE: no File->Quit on web pages!
@@ -769,14 +724,20 @@ pub fn calculate_hash<T: Hash + ?Sized>(t: &T) -> u64 {
769724
s.finish()
770725
}
771726

727+
/// Returns true if the button is clicked or the shortcut is pressed
728+
///
729+
/// Note: This makes it the case that the code for both the button and the shortcut press will do the same thing and you cannot use the shortcut to bypass the button not showing
772730
fn shortcut_button(
773731
ui: &mut egui::Ui,
774732
caption: impl Into<egui::WidgetText>,
775733
hint_msg: &str,
776734
shortcut: &KeyboardShortcut,
777-
) -> egui::Response {
778-
ui.button(caption).on_hover_text(format!(
779-
"{hint_msg}({})",
780-
ui.ctx().format_shortcut(shortcut)
781-
))
735+
) -> bool {
736+
ui.button(caption)
737+
.on_hover_text(format!(
738+
"{hint_msg}({})",
739+
ui.ctx().format_shortcut(shortcut)
740+
))
741+
.clicked()
742+
|| ui.input_mut(|i| i.consume_shortcut(shortcut))
782743
}

0 commit comments

Comments
 (0)