@@ -7,7 +7,7 @@ use std::{
7
7
#[ cfg( not( target_arch = "wasm32" ) ) ]
8
8
use anyhow:: { bail, Context } ;
9
9
use data:: filter:: { Comparator , FieldSpecifier , FilterConfig , FilterOn } ;
10
- use egui:: Align ;
10
+ use egui:: { Align , KeyboardShortcut } ;
11
11
use egui_extras:: { Column , TableBuilder } ;
12
12
use log:: info;
13
13
use shortcut:: Shortcuts ;
@@ -29,6 +29,8 @@ pub struct LogViewerApp {
29
29
track_item_align : Option < Align > ,
30
30
shortcuts : Shortcuts ,
31
31
32
+ #[ serde( skip) ]
33
+ should_focus_search : bool ,
32
34
#[ serde( skip) ]
33
35
should_scroll : bool ,
34
36
#[ serde( skip) ]
@@ -45,6 +47,7 @@ impl Default for LogViewerApp {
45
47
last_filename : Default :: default ( ) ,
46
48
track_item_align : Default :: default ( ) ,
47
49
shortcuts : Default :: default ( ) ,
50
+ should_focus_search : Default :: default ( ) ,
48
51
should_scroll : Default :: default ( ) ,
49
52
show_last_filename : true ,
50
53
}
@@ -456,6 +459,31 @@ impl LogViewerApp {
456
459
data. unfilter ( ) ;
457
460
}
458
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
+
484
+ if ui. input_mut ( |i| i. consume_shortcut ( & self . shortcuts . search ) ) {
485
+ self . focus_search_text_edit ( ) ;
486
+ }
459
487
}
460
488
461
489
fn navigation_and_filtering_ui ( & mut self , ui : & mut egui:: Ui ) {
@@ -481,16 +509,11 @@ impl LogViewerApp {
481
509
}
482
510
let mut should_apply_filter = false ;
483
511
if is_filter_enabled {
484
- if ui . button ( "Apply" ) . clicked ( ) {
512
+ if shortcut_button ( ui , "Apply" , "" , & self . shortcuts . apply_filter ) . clicked ( ) {
485
513
should_apply_filter = true ;
486
514
}
487
515
if data. is_filtered ( )
488
- && ui
489
- . button ( "Unfilter" )
490
- . on_hover_text ( format ! (
491
- "Clears Filter ({})" ,
492
- ui. ctx( ) . format_shortcut( & self . shortcuts. unfilter)
493
- ) )
516
+ && shortcut_button ( ui, "Unfilter" , "Clears Filter " , & self . shortcuts . unfilter )
494
517
. clicked ( )
495
518
{
496
519
data. unfilter ( ) ;
@@ -505,7 +528,12 @@ impl LogViewerApp {
505
528
comparator,
506
529
} = filter;
507
530
ui. label ( "Search Key: " ) ;
508
- if ui. text_edit_singleline ( search_key) . lost_focus ( )
531
+ let search_key_text_edit = ui. text_edit_singleline ( search_key) ;
532
+ if self . should_focus_search {
533
+ self . should_focus_search = false ;
534
+ search_key_text_edit. request_focus ( ) ;
535
+ }
536
+ if search_key_text_edit. lost_focus ( )
509
537
&& ui. input ( |i| i. key_pressed ( egui:: Key :: Enter ) )
510
538
{
511
539
should_apply_filter = true ;
@@ -570,59 +598,32 @@ impl LogViewerApp {
570
598
571
599
fn navigation_ui ( & mut self , ui : & mut egui:: Ui ) {
572
600
ui. label ( "Nav:" ) ;
573
- if ui
574
- . button ( "⏪" )
575
- . on_hover_text ( format ! (
576
- "First ({})" ,
577
- ui. ctx( ) . format_shortcut( & self . shortcuts. first)
578
- ) )
579
- . clicked ( )
580
- {
601
+ if shortcut_button ( ui, "⏪" , "First " , & self . shortcuts . first ) . clicked ( ) {
581
602
self . move_selected_first ( ) ;
582
603
}
583
- if ui
584
- . button ( "⬆" )
585
- . on_hover_text ( format ! (
586
- "Previous ({})" ,
587
- ui. ctx( ) . format_shortcut( & self . shortcuts. prev)
588
- ) )
589
- . clicked ( )
590
- {
604
+ if shortcut_button ( ui, "⬆" , "Previous " , & self . shortcuts . prev ) . clicked ( ) {
591
605
self . move_selected_prev ( ) ;
592
606
}
593
- if ui
594
- . button ( "⬇" )
595
- . on_hover_text ( format ! (
596
- "Next ({})" ,
597
- ui. ctx( ) . format_shortcut( & self . shortcuts. next)
598
- ) )
599
- . clicked ( )
600
- {
607
+ if shortcut_button ( ui, "⬇" , "Next " , & self . shortcuts . next ) . clicked ( ) {
601
608
self . move_selected_next ( ) ;
602
609
}
603
- if ui
604
- . button ( "⏩" )
605
- . on_hover_text ( format ! (
606
- "Last ({})" ,
607
- ui. ctx( ) . format_shortcut( & self . shortcuts. last)
608
- ) )
609
- . clicked ( )
610
- {
610
+ if shortcut_button ( ui, "⏩" , "Last " , & self . shortcuts . last ) . clicked ( ) {
611
611
self . move_selected_last ( ) ;
612
612
}
613
613
}
614
614
fn data_load_ui ( & mut self , ui : & mut egui:: Ui ) {
615
615
ui. horizontal ( |ui| {
616
- if ui. button ( "📂 Open log file..." ) . clicked ( ) {
617
- let ctx = ui. ctx ( ) . clone ( ) ;
618
- self . loading_status = self . initiate_loading ( ctx) ;
616
+ if shortcut_button ( ui, "📂 Open log file..." , "" , & self . shortcuts . open ) . clicked ( ) {
617
+ self . loading_status = self . initiate_loading ( ui. ctx ( ) . clone ( ) ) ;
619
618
}
620
619
#[ cfg( not( target_arch = "wasm32" ) ) ]
621
620
{
622
- if ui . button ( "Reload" ) . clicked ( ) {
621
+ if shortcut_button ( ui , "Reload" , "" , & self . shortcuts . reload ) . clicked ( ) {
623
622
self . loading_status = self . reload_file ( ) ;
624
623
}
625
- if ui. button ( "Load Most Recent File" ) . clicked ( ) {
624
+ if shortcut_button ( ui, "Load Most Recent File" , "" , & self . shortcuts . load_latest )
625
+ . clicked ( )
626
+ {
626
627
self . loading_status = self . load_most_recent_file ( ) ;
627
628
}
628
629
}
@@ -637,6 +638,13 @@ impl LogViewerApp {
637
638
}
638
639
} ) ;
639
640
}
641
+
642
+ fn focus_search_text_edit ( & mut self ) {
643
+ if let Some ( data) = self . data . as_mut ( ) {
644
+ data. filter . get_or_insert ( Default :: default ( ) ) ; // Create filter if it doesn't exist
645
+ self . should_focus_search = true ;
646
+ }
647
+ }
640
648
}
641
649
642
650
#[ cfg( not( target_arch = "wasm32" ) ) ]
@@ -760,3 +768,15 @@ pub fn calculate_hash<T: Hash + ?Sized>(t: &T) -> u64 {
760
768
t. hash ( & mut s) ;
761
769
s. finish ( )
762
770
}
771
+
772
+ fn shortcut_button (
773
+ ui : & mut egui:: Ui ,
774
+ caption : impl Into < egui:: WidgetText > ,
775
+ hint_msg : & str ,
776
+ shortcut : & KeyboardShortcut ,
777
+ ) -> egui:: Response {
778
+ ui. button ( caption) . on_hover_text ( format ! (
779
+ "{hint_msg}({})" ,
780
+ ui. ctx( ) . format_shortcut( shortcut)
781
+ ) )
782
+ }
0 commit comments