wifui 0.5.0

A lightweight, keyboard-driven Terminal User Interface (TUI) for managing Wi-Fi connections on Windows and Linux.
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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
use crate::app::AppState;
use crate::config;
use crate::error::WifiError;
use crate::ui::LayoutAreas;
use crate::wifi::{disconnect, get_connected_ssid, get_wifi_networks};
use color_eyre::eyre::eyre;
use crossterm::event::{self, KeyEvent, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use secrecy::SecretString;
use std::time::Instant;
use tokio::sync::mpsc;

async fn disconnect_if_connected() {
    let is_connected = matches!(
        tokio::task::spawn_blocking(get_connected_ssid).await,
        Ok(Ok(Some(_)))
    );
    if is_connected {
        let _ = tokio::task::spawn_blocking(crate::wifi::disconnect_and_wait).await;
    }
}

const MANUAL_SECURITY_OPTIONS: [&str; 5] = [
    "WPA2-Personal",
    "WPA3-Personal",
    "Open",
    "WPA-Personal",
    "WEP",
];

/// Handle keyboard events for the QR code popup
pub fn handle_qr_popup(key: KeyEvent, state: &mut AppState) -> bool {
    match key.code {
        event::KeyCode::Esc | event::KeyCode::Char('q') | event::KeyCode::Enter => {
            state.ui.show_qr_popup = false;
            state.ui.qr_code_lines.clear();
        }
        event::KeyCode::Char('[') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            state.ui.show_qr_popup = false;
            state.ui.qr_code_lines.clear();
        }
        _ => {}
    }
    false
}

/// Handle keyboard events for the manual add network popup
pub fn handle_manual_add_popup(key: KeyEvent, state: &mut AppState) -> bool {
    match key.code {
        event::KeyCode::Esc => {
            state.ui.show_manual_add_popup = false;
            state.inputs.clear_manual();
        }
        event::KeyCode::Char('[') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            // Close popup like Esc
            state.ui.show_manual_add_popup = false;
            state.inputs.clear_manual();
        }
        event::KeyCode::Tab | event::KeyCode::Down => {
            state.inputs.manual_input_field = (state.inputs.manual_input_field + 1) % 6;
        }
        event::KeyCode::BackTab | event::KeyCode::Up => {
            if state.inputs.manual_input_field == 0 {
                state.inputs.manual_input_field = 5;
            } else {
                state.inputs.manual_input_field -= 1;
            }
        }
        event::KeyCode::Enter => {
            match state.inputs.manual_input_field {
                3 => state.inputs.manual_hidden = !state.inputs.manual_hidden,
                4 => {
                    // Connect
                    if !state.inputs.manual_ssid_input.value.is_empty() {
                        let ssid = state.inputs.manual_ssid_input.value.clone();
                        let operation_id = state.connection.begin_connection_attempt(ssid.clone());
                        let password =
                            SecretString::from(state.inputs.manual_password_input.value.clone());
                        let security = state.inputs.manual_security.clone();
                        let hidden = state.inputs.manual_hidden;

                        let (tx, rx) = mpsc::channel(1);
                        state.connection.connection_result_rx = Some(rx);

                        tokio::spawn(async move {
                            disconnect_if_connected().await;
                            let result = tokio::task::spawn_blocking(move || {
                                if security == "Open" {
                                    crate::wifi::connect_open(&ssid, hidden)
                                } else {
                                    // Map security string to auth/cipher
                                    let (auth, cipher) = match security.as_str() {
                                        "WPA3-Personal" => ("WPA3-SAE", "AES"),
                                        "WPA2-Personal" => ("WPA2-PSK", "AES"),
                                        "WPA-Personal" => ("WPA-PSK", "AES"),
                                        "WEP" => ("Shared", "WEP"),
                                        _ => ("WPA2-PSK", "AES"),
                                    };
                                    crate::wifi::connect_with_password(
                                        &ssid, &password, auth, cipher, hidden,
                                    )
                                }
                            })
                            .await
                            .unwrap_or_else(|e| Err(WifiError::Internal(e.to_string())));
                            let _ = tx
                                .send((operation_id, result.map_err(|e: WifiError| e.into())))
                                .await;
                        });

                        state.ui.show_manual_add_popup = false;
                        state.inputs.clear_manual();
                    }
                }
                5 => {
                    // Cancel
                    state.ui.show_manual_add_popup = false;
                    state.inputs.clear_manual();
                }
                _ => {}
            }
        }
        event::KeyCode::Char(' ') if state.inputs.manual_input_field == 3 => {
            state.inputs.manual_hidden = !state.inputs.manual_hidden;
        }
        event::KeyCode::Char(c) => {
            match state.inputs.manual_input_field {
                0 => state.inputs.manual_ssid_input.insert(c),
                1 => state.inputs.manual_password_input.insert(c),
                2 => {
                    // Handle h/j/k/l for Security field
                    let current_idx = MANUAL_SECURITY_OPTIONS
                        .iter()
                        .position(|&s| s == state.inputs.manual_security)
                        .unwrap_or(0);
                    match c {
                        'h' | 'k' => {
                            let next_idx = if current_idx == 0 {
                                MANUAL_SECURITY_OPTIONS.len() - 1
                            } else {
                                current_idx - 1
                            };
                            state.inputs.manual_security =
                                MANUAL_SECURITY_OPTIONS[next_idx].to_string();
                        }
                        'l' | 'j' => {
                            let next_idx = (current_idx + 1) % MANUAL_SECURITY_OPTIONS.len();
                            state.inputs.manual_security =
                                MANUAL_SECURITY_OPTIONS[next_idx].to_string();
                        }
                        _ => {}
                    }
                }
                _ => {}
            }
        }
        event::KeyCode::Backspace
            if key
                .modifiers
                .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
        {
            match state.inputs.manual_input_field {
                0 => state.inputs.manual_ssid_input.backspace_word(),
                1 => state.inputs.manual_password_input.backspace_word(),
                _ => {}
            }
        }
        event::KeyCode::Backspace => match state.inputs.manual_input_field {
            0 => state.inputs.manual_ssid_input.backspace(),
            1 => state.inputs.manual_password_input.backspace(),
            _ => {}
        },
        event::KeyCode::Left
            if key
                .modifiers
                .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
        {
            match state.inputs.manual_input_field {
                0 => state.inputs.manual_ssid_input.move_word_left(),
                1 => state.inputs.manual_password_input.move_word_left(),
                _ => {}
            }
        }
        event::KeyCode::Left => match state.inputs.manual_input_field {
            0 => state.inputs.manual_ssid_input.move_left(),
            1 => state.inputs.manual_password_input.move_left(),
            2 => {
                let current_idx = MANUAL_SECURITY_OPTIONS
                    .iter()
                    .position(|&s| s == state.inputs.manual_security)
                    .unwrap_or(0);
                let next_idx = if current_idx == 0 {
                    MANUAL_SECURITY_OPTIONS.len() - 1
                } else {
                    current_idx - 1
                };
                state.inputs.manual_security = MANUAL_SECURITY_OPTIONS[next_idx].to_string();
            }
            _ => {}
        },
        event::KeyCode::Right
            if key
                .modifiers
                .intersects(KeyModifiers::CONTROL | KeyModifiers::ALT) =>
        {
            match state.inputs.manual_input_field {
                0 => state.inputs.manual_ssid_input.move_word_right(),
                1 => state.inputs.manual_password_input.move_word_right(),
                _ => {}
            }
        }
        event::KeyCode::Right => match state.inputs.manual_input_field {
            0 => state.inputs.manual_ssid_input.move_right(),
            1 => state.inputs.manual_password_input.move_right(),
            2 => {
                let current_idx = MANUAL_SECURITY_OPTIONS
                    .iter()
                    .position(|&s| s == state.inputs.manual_security)
                    .unwrap_or(0);
                let next_idx = (current_idx + 1) % MANUAL_SECURITY_OPTIONS.len();
                state.inputs.manual_security = MANUAL_SECURITY_OPTIONS[next_idx].to_string();
            }
            _ => {}
        },
        event::KeyCode::Home => match state.inputs.manual_input_field {
            0 => state.inputs.manual_ssid_input.move_home(),
            1 => state.inputs.manual_password_input.move_home(),
            _ => {}
        },
        event::KeyCode::End => match state.inputs.manual_input_field {
            0 => state.inputs.manual_ssid_input.move_end(),
            1 => state.inputs.manual_password_input.move_end(),
            _ => {}
        },
        _ => {}
    }
    false
}

/// Handle keyboard events for the password popup
pub fn handle_password_popup(key: KeyEvent, state: &mut AppState) -> bool {
    match key.code {
        event::KeyCode::Enter => {
            if let Some(ssid) = state.connection.pending_password_ssid.take() {
                let operation_id = state.connection.begin_connection_attempt(ssid.clone());
                let password = SecretString::from(state.inputs.password_input.value.clone());
                let (tx, rx) = mpsc::channel(1);
                state.connection.connection_result_rx = Some(rx);

                let wifi_info = state
                    .network
                    .wifi_list
                    .iter()
                    .find(|w| w.ssid == ssid)
                    .cloned();

                tokio::spawn(async move {
                    disconnect_if_connected().await;
                    let result = tokio::task::spawn_blocking(move || {
                        if let Some(info) = wifi_info {
                            crate::wifi::connect_with_password(
                                &ssid,
                                &password,
                                &info.authentication,
                                &info.encryption,
                                false,
                            )
                        } else {
                            crate::wifi::connect_with_password(
                                &ssid, &password, "WPA2-PSK", "AES", false,
                            )
                        }
                    })
                    .await
                    .unwrap_or_else(|e| Err(WifiError::Internal(e.to_string())));
                    let _ = tx
                        .send((operation_id, result.map_err(|e: WifiError| e.into())))
                        .await;
                });
            }
            state.ui.show_password_popup = false;
            state.inputs.password_input.clear();
        }
        event::KeyCode::Char('[') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            state.ui.show_password_popup = false;
            state.inputs.password_input.clear();
        }
        event::KeyCode::Esc => {
            state.ui.show_password_popup = false;
            state.inputs.password_input.clear();
        }
        _ => {
            // Use the input helper for common key handling
            state.inputs.password_input.handle_key(&key);
        }
    }
    false
}

/// Handle keyboard events for the search mode
pub fn handle_search_mode(key: KeyEvent, state: &mut AppState) -> bool {
    match key.code {
        event::KeyCode::Esc => {
            state.ui.is_searching = false;
        }
        event::KeyCode::Char('[') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            state.ui.is_searching = false;
        }
        event::KeyCode::Enter => {
            state.ui.is_searching = false;
            if !state.network.filtered_wifi_list.is_empty() {
                state.ui.l_state.select(Some(0));
            }
        }
        event::KeyCode::Char(c) => {
            state.inputs.search_input.insert(c);
            state.update_filtered_list();
        }
        _ => {
            if state.inputs.search_input.handle_key(&key) {
                state.update_filtered_list();
            }
        }
    }
    false
}

/// Handle keyboard events for the main view (network list)
pub fn handle_main_view(key: KeyEvent, state: &mut AppState) -> bool {
    use std::time::Duration;

    match key.code {
        event::KeyCode::Char('/') => {
            state.ui.is_searching = true;
        }
        event::KeyCode::Char('n') => {
            state.ui.show_manual_add_popup = true;
            state.inputs.manual_input_field = 0;
        }
        event::KeyCode::Esc => {
            if state.connection.is_connecting {
                state.connection.cancel_operation();
                state.connection.connection_result_rx = None;
            } else if !state.inputs.search_input.value.is_empty() {
                state.inputs.search_input.clear();
                state.update_filtered_list();
            }
        }
        event::KeyCode::Char('q') => return true,
        event::KeyCode::Char('[') if key.modifiers.contains(KeyModifiers::CONTROL) => {
            if !state.inputs.search_input.value.is_empty() {
                state.inputs.search_input.clear();
                state.update_filtered_list();
            }
        }
        event::KeyCode::Char('j') | event::KeyCode::Down => state.next(),
        event::KeyCode::Char('k') | event::KeyCode::Up => state.previous(),
        event::KeyCode::Char('g') | event::KeyCode::Home => state.go_to_top(),
        event::KeyCode::Char('G') | event::KeyCode::End => state.go_to_bottom(),
        event::KeyCode::Enter => {
            if let Some(selected) = state.ui.l_state.selected() {
                connect_selected(state, selected);
            }
        }
        event::KeyCode::Char('r') => {
            // Debounce rapid 'r' key presses
            if state.refresh.last_manual_refresh.elapsed()
                < Duration::from_millis(config::MANUAL_REFRESH_DEBOUNCE_MS)
            {
                return false;
            }
            state.refresh.last_manual_refresh = Instant::now();
            state.refresh.is_refreshing_networks = true;
            let (tx, rx) = mpsc::channel(1);
            state.refresh.network_update_rx = Some(rx);

            tokio::spawn(async move {
                let result = tokio::task::spawn_blocking(|| {
                    crate::wifi::scan_networks()?;
                    std::thread::sleep(Duration::from_millis(config::SCAN_DELAY_MS));
                    let networks = get_wifi_networks()?;
                    let connected = get_connected_ssid()?;
                    Ok((networks, connected))
                })
                .await;
                let result = match result {
                    Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                    Err(e) => Err(eyre!(e.to_string())),
                };
                let _ = tx.send(result).await;
            });
        }
        event::KeyCode::Char('a') => {
            if let Some(selected) = state.ui.l_state.selected() {
                if let Some(wifi) = state.network.filtered_wifi_list.get(selected).cloned() {
                    if wifi.is_saved {
                        let ssid = wifi.ssid.clone();
                        let auto_connect = !wifi.auto_connect;
                        let operation_id = state.connection.begin_operation();
                        let (tx, rx) = mpsc::channel(1);
                        state.connection.connection_result_rx = Some(rx);

                        tokio::spawn(async move {
                            let result = tokio::task::spawn_blocking(move || {
                                crate::wifi::set_auto_connect(&ssid, auto_connect)
                            })
                            .await;
                            let result = match result {
                                Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                                Err(e) => Err(eyre!(e.to_string())),
                            };
                            let _ = tx.send((operation_id, result)).await;
                        });
                    }
                }
            }
        }
        event::KeyCode::Char('f') => {
            if let Some(selected) = state.ui.l_state.selected() {
                if let Some(wifi) = state.network.filtered_wifi_list.get(selected).cloned() {
                    if wifi.is_saved {
                        let ssid = wifi.ssid.clone();
                        let operation_id = state.connection.begin_operation();
                        let (tx, rx) = mpsc::channel(1);
                        state.connection.connection_result_rx = Some(rx);

                        tokio::spawn(async move {
                            let result = tokio::task::spawn_blocking(move || {
                                crate::wifi::forget_network(&ssid)
                            })
                            .await;
                            let result = match result {
                                Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                                Err(e) => Err(eyre!(e.to_string())),
                            };
                            let _ = tx.send((operation_id, result)).await;
                        });
                    }
                }
            }
        }
        event::KeyCode::Char('s') => {
            if let Some(selected) = state.ui.l_state.selected() {
                if let Some(wifi) = state.network.filtered_wifi_list.get(selected).cloned() {
                    if wifi.is_saved {
                        let ssid = wifi.ssid.clone();
                        let auth = wifi.authentication.clone();
                        if auth == "Open" || auth == "open" {
                            let qr_lines = generate_wifi_qr(&ssid, &auth, None);
                            state.ui.qr_code_lines = qr_lines;
                            state.ui.show_qr_popup = true;
                        } else if qr_auth_type(&auth).is_none() {
                            state.ui.error_message = Some(
                                "Secured-network QR sharing is unavailable for this security type"
                                    .to_string(),
                            );
                        } else if state.ui.qr_result_rx.is_none() {
                            let password_ssid = ssid.clone();
                            let (tx, rx) = mpsc::channel(1);
                            state.ui.qr_result_rx = Some(rx);

                            tokio::spawn(async move {
                                let result = tokio::task::spawn_blocking(move || {
                                    crate::wifi::get_wifi_password(&password_ssid)
                                })
                                .await;
                                let result = match result {
                                    Ok(Ok(Some(password))) => {
                                        Ok(generate_wifi_qr(&ssid, &auth, Some(&password)))
                                    }
                                    Ok(Ok(None)) => {
                                        Err(eyre!("the saved profile has no readable password"))
                                    }
                                    Ok(Err(error)) => Err(eyre!(error.to_string())),
                                    Err(error) => Err(eyre!(error.to_string())),
                                };
                                let _ = tx.send(result).await;
                            });
                        }
                    }
                }
            }
        }
        _ => {}
    }
    false
}

/// Generate WiFi QR code in standard format: WIFI:S:ssid;T:auth;P:password;;
fn generate_wifi_qr(ssid: &str, auth: &str, password: Option<&SecretString>) -> Vec<String> {
    use qrcode::QrCode;
    use qrcode::render::unicode;
    use secrecy::ExposeSecret;

    let auth_type = qr_auth_type(auth).unwrap_or("WPA");

    let qr_string = if auth_type == "nopass" {
        format!("WIFI:S:{};T:nopass;;", escape_special_chars(ssid))
    } else if let Some(pwd) = password {
        format!(
            "WIFI:S:{};T:{};P:{};;",
            escape_special_chars(ssid),
            auth_type,
            escape_special_chars(pwd.expose_secret())
        )
    } else {
        format!("WIFI:S:{};T:{};;", escape_special_chars(ssid), auth_type)
    };

    match QrCode::new(&qr_string) {
        Ok(code) => {
            let string = code.render::<unicode::Dense1x2>().build();
            string.lines().map(|s| s.to_string()).collect()
        }
        Err(_) => vec!["Error generating QR code".to_string()],
    }
}

fn qr_auth_type(auth: &str) -> Option<&'static str> {
    match auth {
        "Open" | "open" => Some("nopass"),
        "WPA3-SAE" | "WPA3" | "WPA2-PSK" | "WPA2-Personal" | "WPA-PSK" | "WPA-Personal" | "WPA" => {
            Some("WPA")
        }
        "Shared" | "WEP" => Some("WEP"),
        _ => None,
    }
}

/// Escape special characters for WiFi QR code format
fn escape_special_chars(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace(';', "\\;")
        .replace(',', "\\,")
        .replace(':', "\\:")
}

// ─── Mouse helpers ────────────────────────────────────────────────────────────

/// Returns true if terminal cell (col, row) falls inside the given Rect.
#[inline]
fn contains(rect: ratatui::layout::Rect, col: u16, row: u16) -> bool {
    col >= rect.x && col < rect.x + rect.width && row >= rect.y && row < rect.y + rect.height
}

/// Returns the index into `filtered_wifi_list` that the cursor is currently over,
/// accounting for the current scroll offset and list border.
fn row_under_cursor(col: u16, row: u16, state: &AppState, areas: &LayoutAreas) -> Option<usize> {
    let list = areas.list_area;
    // list_area includes the rounded border; inner content starts one cell inside
    if col < list.x + 1 || col >= list.x + list.width.saturating_sub(1) {
        return None;
    }
    if row < list.y + 1 || row >= list.y + list.height.saturating_sub(1) {
        return None;
    }
    let relative_row = (row - list.y - 1) as usize;
    let actual_index = state.mouse.scroll_offset + relative_row;
    if actual_index < state.network.filtered_wifi_list.len() {
        Some(actual_index)
    } else {
        None
    }
}

/// Shared logic for "activate the currently selected network" — called by both
/// the keyboard Enter handler and mouse double-click.
fn connect_selected(state: &mut AppState, selected: usize) {
    if let Some(wifi) = state.network.filtered_wifi_list.get(selected).cloned() {
        let is_connected = wifi.is_connected;

        if is_connected {
            let ssid = wifi.ssid.clone();
            let operation_id = state.connection.begin_disconnect_attempt(ssid);
            let (tx, rx) = mpsc::channel(1);
            state.connection.connection_result_rx = Some(rx);
            tokio::spawn(async move {
                let result = tokio::task::spawn_blocking(disconnect).await;
                let result = match result {
                    Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                    Err(e) => Err(eyre!(e.to_string())),
                };
                let _ = tx.send((operation_id, result)).await;
            });
        } else if wifi.authentication != "Open" {
            if wifi.is_saved {
                let ssid = wifi.ssid.clone();
                let operation_id = state.connection.begin_connection_attempt(ssid.clone());
                let (tx, rx) = mpsc::channel(1);
                state.connection.connection_result_rx = Some(rx);
                tokio::spawn(async move {
                    disconnect_if_connected().await;
                    let result =
                        tokio::task::spawn_blocking(move || crate::wifi::connect_profile(&ssid))
                            .await;
                    let result = match result {
                        Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                        Err(e) => Err(eyre!(e.to_string())),
                    };
                    let _ = tx.send((operation_id, result)).await;
                });
            } else {
                state.ui.show_password_popup = true;
                state.inputs.password_input.clear();
                state.connection.pending_password_ssid = Some(wifi.ssid.clone());
            }
        } else {
            let ssid = wifi.ssid.clone();
            let operation_id = state.connection.begin_connection_attempt(ssid.clone());
            let (tx, rx) = mpsc::channel(1);
            state.connection.connection_result_rx = Some(rx);
            tokio::spawn(async move {
                disconnect_if_connected().await;
                let result =
                    tokio::task::spawn_blocking(move || crate::wifi::connect_open(&ssid, false))
                        .await;
                let result = match result {
                    Ok(inner) => inner.map_err(|e: WifiError| e.into()),
                    Err(e) => Err(eyre!(e.to_string())),
                };
                let _ = tx.send((operation_id, result)).await;
            });
        }
    }
}

/// Attempt to connect the network in the manual-add popup's Connect button slot.
/// Mirrors the `Enter` branch for `manual_input_field == 4` in `handle_manual_add_popup`.
fn trigger_manual_connect(state: &mut AppState) {
    if !state.inputs.manual_ssid_input.value.is_empty() {
        let ssid = state.inputs.manual_ssid_input.value.clone();
        let operation_id = state.connection.begin_connection_attempt(ssid.clone());
        let password = SecretString::from(state.inputs.manual_password_input.value.clone());
        let security = state.inputs.manual_security.clone();
        let hidden = state.inputs.manual_hidden;

        let (tx, rx) = mpsc::channel(1);
        state.connection.connection_result_rx = Some(rx);

        tokio::spawn(async move {
            disconnect_if_connected().await;
            let result = tokio::task::spawn_blocking(move || {
                if security == "Open" {
                    crate::wifi::connect_open(&ssid, hidden)
                } else {
                    let (auth, cipher) = match security.as_str() {
                        "WPA3-Personal" => ("WPA3-SAE", "AES"),
                        "WPA2-Personal" => ("WPA2-PSK", "AES"),
                        "WPA-Personal" => ("WPA-PSK", "AES"),
                        "WEP" => ("Shared", "WEP"),
                        _ => ("WPA2-PSK", "AES"),
                    };
                    crate::wifi::connect_with_password(&ssid, &password, auth, cipher, hidden)
                }
            })
            .await
            .unwrap_or_else(|e| Err(WifiError::Internal(e.to_string())));
            let _ = tx
                .send((operation_id, result.map_err(|e: WifiError| e.into())))
                .await;
        });

        state.ui.show_manual_add_popup = false;
        state.inputs.clear_manual();
    }
}

/// Handle all mouse events for the TUI.
pub fn handle_mouse(mouse: MouseEvent, state: &mut AppState, areas: &LayoutAreas) {
    let col = mouse.column;
    let row = mouse.row;

    match mouse.kind {
        // ── Scroll wheel ──────────────────────────────────────────────────────
        MouseEventKind::ScrollUp => {
            state.mouse.scroll_offset = state.mouse.scroll_offset.saturating_sub(1);
            *state.ui.l_state.offset_mut() = state.mouse.scroll_offset;
        }
        MouseEventKind::ScrollDown => {
            let list_height = areas.list_area.height.saturating_sub(2) as usize;
            let max_offset = state
                .network
                .filtered_wifi_list
                .len()
                .saturating_sub(list_height);
            if state.mouse.scroll_offset < max_offset {
                state.mouse.scroll_offset += 1;
            }
            *state.ui.l_state.offset_mut() = state.mouse.scroll_offset;
        }

        // ── Mouse move — hot-track ────────────────────────────────────────────
        MouseEventKind::Moved => {
            if !state.is_popup_open() {
                let new_hover = row_under_cursor(col, row, state, areas);
                state.mouse.hovered_row = new_hover;
            } else {
                state.mouse.hovered_row = None;
            }
        }

        // ── Left button press ─────────────────────────────────────────────────
        MouseEventKind::Down(MouseButton::Left) => {
            // Always dismiss the error message on any click
            if state.ui.error_message.is_some() {
                state.ui.error_message = None;
            }

            // --- QR popup: click-away to dismiss ---
            if state.ui.show_qr_popup {
                if let Some(qr_area) = areas.qr_popup_area {
                    if !contains(qr_area, col, row) {
                        state.ui.show_qr_popup = false;
                        state.ui.qr_code_lines.clear();
                    }
                }
                return;
            }

            // --- Manual-add popup ---
            if state.ui.show_manual_add_popup {
                if let Some(popup_area) = areas.manual_popup_area {
                    if !contains(popup_area, col, row) {
                        // Click outside — dismiss
                        state.ui.show_manual_add_popup = false;
                        state.inputs.clear_manual();
                        return;
                    }
                }
                // Click inside the popup: hit-test connect button first
                if let Some(btn) = areas.manual_connect_area {
                    if contains(btn, col, row) {
                        trigger_manual_connect(state);
                        return;
                    }
                }
                // Hit-test individual fields (SSID=0, Password=1, Security=2, Hidden=3)
                for (i, field_area) in areas.manual_field_areas.iter().enumerate() {
                    if let Some(area) = field_area {
                        if contains(*area, col, row) {
                            state.inputs.manual_input_field = i;
                            return;
                        }
                    }
                }
                return;
            }

            // --- Password popup: click-away to dismiss ---
            if state.ui.show_password_popup {
                if let Some(pw_area) = areas.password_popup_area {
                    if !contains(pw_area, col, row) {
                        state.ui.show_password_popup = false;
                        state.inputs.password_input.clear();
                    }
                }
                return;
            }

            // --- Main view: click on a list item ---
            if let Some(clicked_idx) = row_under_cursor(col, row, state, areas) {
                let is_double = state
                    .mouse
                    .last_list_click
                    .as_ref()
                    .map(|(prev_row, t)| {
                        *prev_row == clicked_idx
                            && t.elapsed()
                                < std::time::Duration::from_millis(config::DOUBLE_CLICK_MS)
                    })
                    .unwrap_or(false);

                if is_double {
                    // Double-click: select + activate (same as Enter)
                    state.ui.l_state.select(Some(clicked_idx));
                    state.mouse.last_list_click = None; // reset so triple-click doesn't re-fire
                    connect_selected(state, clicked_idx);
                } else {
                    // Single click: select only
                    state.ui.l_state.select(Some(clicked_idx));
                    state.mouse.last_list_click = Some((clicked_idx, Instant::now()));
                }
            }
        }

        // Right-click and any other events are intentionally ignored
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn qr_supports_personal_security_types_only() {
        assert_eq!(qr_auth_type("Open"), Some("nopass"));
        assert_eq!(qr_auth_type("WPA3-SAE"), Some("WPA"));
        assert_eq!(qr_auth_type("Shared"), Some("WEP"));
        assert_eq!(qr_auth_type("WPA2"), None);
        assert_eq!(qr_auth_type("Unknown"), None);
    }

    #[test]
    fn qr_escapes_wifi_special_characters() {
        assert_eq!(
            escape_special_chars(r#"a;b,c:d"e\f"#),
            r#"a\;b\,c\:d\"e\\f"#
        );
    }
}

#[cfg(test)]
mod mouse_tests {
    use super::*;
    use ratatui::layout::Rect;

    #[test]
    fn contains_identifies_interior_cells() {
        let rect = Rect::new(5, 5, 10, 5); // x=5..14, y=5..9
        assert!(contains(rect, 5, 5));
        assert!(contains(rect, 14, 9));
        assert!(!contains(rect, 4, 5));
        assert!(!contains(rect, 15, 5));
        assert!(!contains(rect, 5, 10));
    }

    #[test]
    fn contains_zero_size_rect_never_matches() {
        let rect = Rect::new(0, 0, 0, 0);
        assert!(!contains(rect, 0, 0));
    }
}