llmfit 0.9.23

Right-size LLM models to your system hardware. Interactive TUI and CLI to match models against available RAM, CPU, and GPU.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use std::time::Duration;

use crate::tui_app::{App, InputMode};

/// Poll for and handle events. Returns true if an event was processed.
pub fn handle_events(app: &mut App) -> std::io::Result<bool> {
    // Always tick the pull progress and live-bench worker messages (non-blocking)
    app.tick_pull();
    app.tick_bench();

    if event::poll(Duration::from_millis(50))?
        && let Event::Key(key) = event::read()?
    {
        // Only handle Press events (ignore Release on some platforms)
        if key.kind != KeyEventKind::Press {
            return Ok(false);
        }
        match app.input_mode {
            InputMode::Normal => handle_normal_mode(app, key),
            InputMode::Visual => handle_visual_mode(app, key),
            InputMode::Select => handle_select_mode(app, key),
            InputMode::Search => handle_search_mode(app, key),
            InputMode::Plan => handle_plan_mode(app, key),
            InputMode::ProviderPopup => handle_provider_popup_mode(app, key),
            InputMode::UseCasePopup => handle_use_case_popup_mode(app, key),
            InputMode::CapabilityPopup => handle_capability_popup_mode(app, key),
            InputMode::DownloadProviderPopup => handle_download_provider_popup_mode(app, key),
            InputMode::QuantPopup => handle_quant_popup_mode(app, key),
            InputMode::RunModePopup => handle_run_mode_popup_mode(app, key),
            InputMode::ParamsBucketPopup => handle_params_bucket_popup_mode(app, key),
            InputMode::LicensePopup => handle_license_popup_mode(app, key),
            InputMode::RuntimePopup => handle_runtime_popup_mode(app, key),
            InputMode::HelpPopup => handle_help_popup_mode(app, key),
            InputMode::Simulation => handle_simulation_mode(app, key),
            InputMode::AdvancedConfig => handle_advanced_config_mode(app, key),
            InputMode::DownloadManager => handle_download_manager_mode(app, key),
            InputMode::FilterPopup => handle_filter_popup_mode(app, key),
            InputMode::Benchmarks => handle_benchmarks_mode(app, key),
        }
        return Ok(true);
    }
    Ok(false)
}

fn handle_normal_mode(app: &mut App, key: KeyEvent) {
    // Handle bench quit-confirmation first (overrides all other handlers)
    if app.bench_confirm_quit {
        match key.code {
            KeyCode::Char('q') | KeyCode::Char('y') | KeyCode::Char('Y') => {
                app.bench_confirm_quit = false;
                app.close_bench();
            }
            _ => {
                app.bench_confirm_quit = false;
                app.bench_progress = format!(
                    "{}/{} tests — Benchmarking...",
                    app.bench_tests_done, app.bench_tests_total
                );
            }
        }
        return;
    }

    match key.code {
        // Quit
        KeyCode::Char('q') | KeyCode::Esc => {
            if app.show_bench {
                if app.bench_show_detail {
                    app.bench_show_detail = false;
                } else if app.bench_running {
                    app.bench_confirm_quit = true;
                    app.bench_progress =
                        "Inference bench running! Press q again to exit, any key to cancel"
                            .to_string();
                } else {
                    app.close_bench();
                }
            } else if app.show_downloads {
                app.close_downloads();
            } else if app.show_multi_compare {
                app.close_multi_compare();
            } else if app.show_detail {
                app.show_detail = false;
            } else if app.show_compare {
                app.show_compare = false;
            } else {
                app.save_filters();
                app.should_quit = true;
            }
        }

        // Live bench view navigation (only active when bench view is open)
        KeyCode::Char('j') | KeyCode::Down if app.show_bench => {
            if app.bench_show_detail {
                app.live_bench_scroll += 1;
            } else {
                let max = app.bench_model_status.len().saturating_sub(1);
                if app.bench_selected_row < max {
                    app.bench_selected_row += 1;
                }
            }
        }
        KeyCode::Char('k') | KeyCode::Up if app.show_bench => {
            if app.bench_show_detail {
                app.live_bench_scroll = app.live_bench_scroll.saturating_sub(1);
            } else if app.bench_selected_row > 0 {
                app.bench_selected_row -= 1;
            }
        }
        KeyCode::Char('r') if app.show_bench => {
            app.toggle_bench_view();
        }
        KeyCode::Enter if app.show_bench => {
            if app.bench_show_detail {
                app.bench_show_detail = false;
            } else {
                app.bench_show_detail = true;
                app.live_bench_scroll = 0;
            }
        }

        // Navigation — in multi-compare, h/l scroll columns
        KeyCode::Char('h') if app.show_multi_compare => app.multi_compare_scroll_left(),
        KeyCode::Char('l') if app.show_multi_compare => app.multi_compare_scroll_right(),
        KeyCode::Left if app.show_multi_compare => app.multi_compare_scroll_left(),
        KeyCode::Right if app.show_multi_compare => app.multi_compare_scroll_right(),

        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => app.half_page_up(),
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => app.half_page_down(),
        KeyCode::Up | KeyCode::Char('k') => app.move_up(),
        KeyCode::Down | KeyCode::Char('j') => app.move_down(),
        KeyCode::PageUp => app.page_up(),
        KeyCode::PageDown => app.page_down(),
        KeyCode::Home | KeyCode::Char('g') => app.cycle_top_bottom(),
        // Visual mode
        KeyCode::Char('v') => app.enter_visual_mode(),

        // Select mode
        KeyCode::Char('V') => app.enter_select_mode(),

        // Search
        KeyCode::Char('/') => app.enter_search(),

        // Fit filter
        KeyCode::Char('f') => app.cycle_fit_filter(),

        // Filter popup (range filters, sort direction, fit)
        KeyCode::Char('F') => app.open_filter_popup(),

        // Availability filter
        KeyCode::Char('a') => app.cycle_availability_filter(),

        // TP compatibility filter
        KeyCode::Char('T') => app.cycle_tp_filter(),

        // Sort column
        KeyCode::Char('s') => app.cycle_sort_column(),

        // Theme
        KeyCode::Char('t') => app.cycle_theme(),

        // Plan view
        KeyCode::Char('p') => app.open_plan_mode(),

        // Provider popup
        KeyCode::Char('P') => app.open_provider_popup(),
        KeyCode::Char('U') => app.open_use_case_popup(),
        KeyCode::Char('C') => app.open_capability_popup(),
        KeyCode::Char('L') => app.open_license_popup(),
        KeyCode::Char('R') => app.open_runtime_popup(),
        KeyCode::Char('S') => app.open_simulation_popup(),
        KeyCode::Char('h') => app.open_help_popup(),

        // Installed-first sort toggle (any provider)
        KeyCode::Char('i')
            if app.ollama_available
                || app.mlx_available
                || app.llamacpp_available
                || app.lmstudio_available
                || app.vllm_available =>
        {
            app.toggle_installed_first()
        }

        // Download model via best provider (requires confirmation)
        KeyCode::Char('d')
            if app.ollama_available
                || app.mlx_available
                || app.llamacpp_available
                || app.lmstudio_available
                || app.vllm_available =>
        {
            if app.pull_active.is_none() {
                app.start_download();
            }
        }

        // Refresh installed models
        KeyCode::Char('r')
            if app.ollama_available
                || app.mlx_available
                || app.llamacpp_available
                || app.lmstudio_available
                || app.vllm_available =>
        {
            app.refresh_installed()
        }

        // Download manager view
        KeyCode::Char('D') => app.toggle_downloads(),

        // Benchmarks view (localmaxxing.com community leaderboard)
        KeyCode::Char('b') => app.open_benchmarks(),

        // Live inference-bench view (llmfit bench — I=open, I again=rerun)
        KeyCode::Char('I') if app.show_bench => app.rerun_bench(),
        KeyCode::Char('I') => app.open_bench(),

        // Advanced Config popup
        KeyCode::Char('A') => app.open_advanced_config_popup(),

        // Detail view
        KeyCode::Enter => app.toggle_detail(),

        // Compare view
        KeyCode::Char('m') => app.mark_selected_for_compare(),
        KeyCode::Char('c') => app.toggle_compare_view(),
        KeyCode::Char('x') => app.clear_compare_mark(),
        KeyCode::Char('y') => app.copy_selected_model_name(),

        _ => {}
    }
}

fn handle_visual_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        // Exit visual mode
        KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('v') => app.exit_visual_mode(),

        // Navigation (extends selection)
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => app.half_page_up(),
        KeyCode::Char('d') if key.modifiers.contains(KeyModifiers::CONTROL) => app.half_page_down(),
        KeyCode::Up | KeyCode::Char('k') => app.move_up(),
        KeyCode::Down | KeyCode::Char('j') => app.move_down(),
        KeyCode::PageUp => app.page_up(),
        KeyCode::PageDown => app.page_down(),
        KeyCode::Home | KeyCode::Char('g') => app.cycle_top_bottom(),

        // Mark all selected for compare
        KeyCode::Char('m') => app.mark_selected_for_compare(),

        // Compare first and last in visual selection
        KeyCode::Char('c') => app.visual_compare(),

        _ => {}
    }
}

fn handle_select_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        // Exit select mode
        KeyCode::Esc | KeyCode::Char('q') => app.exit_select_mode(),

        // Column navigation
        KeyCode::Left | KeyCode::Char('h') => app.select_column_left(),
        KeyCode::Right | KeyCode::Char('l') => app.select_column_right(),

        // Activate filter for current column
        KeyCode::Enter | KeyCode::Char(' ') => app.activate_select_column_filter(),

        // Row navigation (still works in select mode)
        KeyCode::Up | KeyCode::Char('k') => app.move_up(),
        KeyCode::Down | KeyCode::Char('j') => app.move_down(),

        _ => {}
    }
}

fn handle_search_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Enter => app.exit_search(),

        KeyCode::Backspace => app.search_backspace(),
        KeyCode::Delete => app.search_delete(),

        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.clear_search();
        }

        KeyCode::Char(c) => app.search_input(c),

        // Allow navigation while searching
        KeyCode::Up => app.move_up(),
        KeyCode::Down => app.move_down(),

        _ => {}
    }
}

fn handle_provider_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('P') | KeyCode::Char('q') => app.close_provider_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.provider_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.provider_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.provider_popup_toggle(),

        KeyCode::Char('a') => app.provider_popup_select_all(),
        KeyCode::Char('c') => app.provider_popup_clear_all(),

        _ => {}
    }
}

fn handle_plan_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_plan_mode(),
        KeyCode::Tab | KeyCode::Down | KeyCode::Char('j') => app.plan_next_field(),
        KeyCode::BackTab | KeyCode::Up | KeyCode::Char('k') => app.plan_prev_field(),
        KeyCode::Left => app.plan_cursor_left(),
        KeyCode::Right => app.plan_cursor_right(),
        KeyCode::Backspace => app.plan_backspace(),
        KeyCode::Delete => app.plan_delete(),
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.plan_clear_field()
        }
        KeyCode::Char(c) => app.plan_input(c),
        _ => {}
    }
}

fn handle_use_case_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('U') | KeyCode::Char('q') => app.close_use_case_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.use_case_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.use_case_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.use_case_popup_toggle(),

        KeyCode::Char('a') => app.use_case_popup_select_all(),

        _ => {}
    }
}

fn handle_capability_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('C') | KeyCode::Char('q') => app.close_capability_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.capability_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.capability_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.capability_popup_toggle(),

        KeyCode::Char('a') => app.capability_popup_select_all(),

        _ => {}
    }
}

fn handle_download_provider_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_download_provider_popup(),
        KeyCode::Up | KeyCode::Char('k') => app.download_provider_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.download_provider_popup_down(),
        KeyCode::Enter | KeyCode::Char(' ') => app.confirm_download_provider_selection(),
        _ => {}
    }
}

fn handle_quant_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_quant_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.quant_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.quant_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.quant_popup_toggle(),

        KeyCode::Char('a') => app.quant_popup_select_all(),

        _ => {}
    }
}

fn handle_run_mode_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_run_mode_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.run_mode_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.run_mode_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.run_mode_popup_toggle(),

        KeyCode::Char('a') => app.run_mode_popup_select_all(),

        _ => {}
    }
}

fn handle_params_bucket_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_params_bucket_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.params_bucket_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.params_bucket_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.params_bucket_popup_toggle(),

        KeyCode::Char('a') => app.params_bucket_popup_select_all(),

        _ => {}
    }
}

fn handle_license_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('L') | KeyCode::Char('q') => app.close_license_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.license_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.license_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.license_popup_toggle(),

        KeyCode::Char('a') => app.license_popup_select_all(),

        _ => {}
    }
}

fn handle_runtime_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('R') | KeyCode::Char('q') => app.close_runtime_popup(),

        KeyCode::Up | KeyCode::Char('k') => app.runtime_popup_up(),
        KeyCode::Down | KeyCode::Char('j') => app.runtime_popup_down(),

        KeyCode::Char(' ') | KeyCode::Enter => app.runtime_popup_toggle(),

        KeyCode::Char('a') => app.runtime_popup_select_all(),

        _ => {}
    }
}

fn handle_help_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('h') | KeyCode::Char('q') => app.close_help_popup(),
        KeyCode::Up | KeyCode::Char('k') => {
            if app.help_scroll > 0 {
                app.help_scroll -= 1;
            }
        }
        KeyCode::Down | KeyCode::Char('j') => {
            app.help_scroll += 1;
        }
        _ => {}
    }
}

fn handle_simulation_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_simulation_popup(),

        // Apply simulation
        KeyCode::Enter => app.apply_simulation(),

        // Reset to real hardware
        KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.reset_simulation();
            app.close_simulation_popup();
        }

        // Field navigation
        KeyCode::Tab | KeyCode::Down | KeyCode::Char('j') => app.sim_next_field(),
        KeyCode::BackTab | KeyCode::Up | KeyCode::Char('k') => app.sim_prev_field(),

        // Cursor movement within field
        KeyCode::Left => app.sim_cursor_left(),
        KeyCode::Right => app.sim_cursor_right(),

        // Editing
        KeyCode::Backspace => app.sim_backspace(),
        KeyCode::Delete => app.sim_delete(),
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.sim_clear_field()
        }

        // Character input (digits and decimal point)
        KeyCode::Char(c) if c.is_ascii_digit() || c == '.' => app.sim_input(c),

        _ => {}
    }
}

fn handle_advanced_config_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_advanced_config_popup(),

        // Apply config changes
        KeyCode::Enter => app.apply_advanced_config(),

        // Field navigation
        KeyCode::Tab | KeyCode::Down | KeyCode::Char('j') => app.adv_config_next_field(),
        KeyCode::BackTab | KeyCode::Up | KeyCode::Char('k') => app.adv_config_prev_field(),

        // Cursor movement within field
        KeyCode::Left => app.adv_config_cursor_left(),
        KeyCode::Right => app.adv_config_cursor_right(),

        // Editing
        KeyCode::Backspace => app.adv_config_backspace(),
        KeyCode::Delete => app.adv_config_delete(),
        KeyCode::Char('r') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.reset_advanced_config()
        }
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            app.adv_config_clear_field()
        }

        // Character input (digits and decimal point)
        KeyCode::Char(c) if c.is_ascii_digit() || c == '.' => app.adv_config_input(c),

        _ => {}
    }
}

fn handle_download_manager_mode(app: &mut App, key: KeyEvent) {
    use crate::tui_app::DownloadManagerFocus;

    // Handle delete confirmation first
    if app.dm_confirm_delete {
        match key.code {
            KeyCode::Char('y') => {
                app.delete_selected_download();
                app.dm_confirm_delete = false;
            }
            _ => app.dm_confirm_delete = false,
        }
        return;
    }

    // Handle directory editing mode
    if app.dm_editing_dir {
        match key.code {
            KeyCode::Esc => {
                app.dm_editing_dir = false;
            }
            KeyCode::Enter => {
                app.apply_download_dir();
                app.dm_editing_dir = false;
            }
            KeyCode::Backspace => {
                if app.dm_dir_cursor > 0 {
                    app.dm_dir_cursor -= 1;
                    app.dm_dir_input.remove(app.dm_dir_cursor);
                }
            }
            KeyCode::Left => {
                if app.dm_dir_cursor > 0 {
                    app.dm_dir_cursor -= 1;
                }
            }
            KeyCode::Right => {
                if app.dm_dir_cursor < app.dm_dir_input.len() {
                    app.dm_dir_cursor += 1;
                }
            }
            KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
                app.dm_dir_input.clear();
                app.dm_dir_cursor = 0;
            }
            KeyCode::Char(c) => {
                app.dm_dir_input.insert(app.dm_dir_cursor, c);
                app.dm_dir_cursor += 1;
            }
            _ => {}
        }
        return;
    }

    match key.code {
        // Close
        KeyCode::Esc | KeyCode::Char('D') | KeyCode::Char('q') => app.close_downloads(),

        // Focus cycling
        KeyCode::Tab => app.dm_focus = app.dm_focus.next(),
        KeyCode::BackTab => app.dm_focus = app.dm_focus.prev(),

        // Navigation within history
        KeyCode::Up | KeyCode::Char('k') if app.dm_focus == DownloadManagerFocus::History => {
            if app.dm_history_cursor > 0 {
                app.dm_history_cursor -= 1;
            }
        }
        KeyCode::Down | KeyCode::Char('j') if app.dm_focus == DownloadManagerFocus::History => {
            let len = app.download_history.records.len();
            if len > 0 && app.dm_history_cursor < len - 1 {
                app.dm_history_cursor += 1;
            }
        }

        // Delete model
        KeyCode::Char('x') if app.dm_focus == DownloadManagerFocus::History => {
            if !app.download_history.records.is_empty() {
                app.dm_confirm_delete = true;
            }
        }

        // Edit download directory
        KeyCode::Char('e') if app.dm_focus == DownloadManagerFocus::Config => {
            app.start_editing_download_dir();
        }

        _ => {}
    }
}

fn handle_filter_popup_mode(app: &mut App, key: KeyEvent) {
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => app.close_filter_popup(),

        KeyCode::Enter => app.apply_filter_popup(),

        // Field navigation
        KeyCode::Tab | KeyCode::Down => app.filter_next_field(),
        KeyCode::BackTab | KeyCode::Up => app.filter_prev_field(),

        // Cursor movement within field
        KeyCode::Left => app.filter_cursor_left(),
        KeyCode::Right => app.filter_cursor_right(),

        // Editing
        KeyCode::Backspace => app.filter_backspace(),
        KeyCode::Delete => app.filter_delete(),
        KeyCode::Char('u') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            if matches!(
                app.filter_field,
                crate::tui_app::FilterPopupField::SortDirection
                    | crate::tui_app::FilterPopupField::FitFilter
            ) {
                return;
            }
            app.filter_clear_active_input();
        }

        // Sort direction toggle
        KeyCode::Char(' ')
            if app.filter_field == crate::tui_app::FilterPopupField::SortDirection =>
        {
            app.filter_toggle_sort_direction()
        }

        // Fit filter cycling
        KeyCode::Char(' ') if app.filter_field == crate::tui_app::FilterPopupField::FitFilter => {
            app.cycle_filter_fit()
        }

        // Numeric input
        KeyCode::Char(c) if c.is_ascii_digit() || c == '.' => app.filter_input(c),

        _ => {}
    }
}

fn handle_benchmarks_mode(app: &mut App, key: KeyEvent) {
    // Hardware picker sub-modal takes priority when open
    if app.bench_hw_picker_open {
        match key.code {
            KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('H') => app.close_bench_hw_picker(),
            KeyCode::Up | KeyCode::Char('k') => app.bench_hw_picker_up(),
            KeyCode::Down | KeyCode::Char('j') => app.bench_hw_picker_down(),
            KeyCode::Enter | KeyCode::Char(' ') => app.bench_hw_picker_select(),
            _ => {}
        }
        return;
    }

    match key.code {
        KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('b') => app.close_benchmarks(),
        KeyCode::Up | KeyCode::Char('k') => app.bench_move_up(),
        KeyCode::Down | KeyCode::Char('j') => app.bench_move_down(),
        KeyCode::Char('r') => app.bench_refresh(),
        KeyCode::Char('H') => app.open_bench_hw_picker(),
        _ => {}
    }
}