index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff
nmrs-tui / src / events / tabs.rs
-rw-r--r-- blob 8918c03f81e44e4fc79625b4da7d60bd64168c64 2839 bytes (plain)
1
use crossterm::event::{KeyCode, KeyEvent};
2
use nmrs::WifiSecurity;
3
4
use crate::{
5
    action::{Action, ConnectRequest},
6
    app::{App, Focus, Selected},
7
    ui::{input::Input, popup::Popups, tabs::Tabs},
8
};
9
10
pub fn handle(app: &mut App, key: KeyEvent, tab: Tabs) {
11
    match key.code {
12
        KeyCode::Char('q') => app.action.send(Action::Quit),
13
14
        // Navigation
15
        KeyCode::Tab | KeyCode::Char('l') | KeyCode::Right => {
16
            app.action.send(Action::SetFocus(Focus::Tab(tab.next())));
17
        }
18
        KeyCode::BackTab | KeyCode::Char('h') | KeyCode::Left => {
19
            app.action.send(Action::SetFocus(Focus::Tab(tab.prev())));
20
        }
21
        KeyCode::Char('j') | KeyCode::Down => app.action.send(Action::NextItem(tab)),
22
        KeyCode::Char('k') | KeyCode::Up => app.action.send(Action::PreviousItem(tab)),
23
24
        // Global / Tab Actions
25
        KeyCode::Char('r') => app.action.send(Action::Refresh),
26
        KeyCode::Char('a') if tab == Tabs::Devices => {
27
            app.action.send(Action::ToggleAirplaneMode);
28
        }
29
        KeyCode::Char('o') if tab == Tabs::Devices => {
30
            app.action.send(Action::TogglePower);
31
        }
32
        KeyCode::Char('d') if tab == Tabs::KnownNetworks => {
33
            if let Some(Selected::Network(net)) = app.selected() {
34
                app.action.send(Action::Forget { ssid: net.ssid });
35
            }
36
        }
37
        KeyCode::Char('t') if tab == Tabs::KnownNetworks => {
38
            if let Some(Selected::Network(net)) = app.selected() {
39
                app.action
40
                    .send(Action::ToggleAutoconnect { ssid: net.ssid });
41
            }
42
        }
43
44
        KeyCode::Enter | KeyCode::Char(' ') => match tab {
45
            Tabs::KnownNetworks => {
46
                if let Some(Selected::Network(net)) = app.selected() {
47
                    let is_connected = app
48
                        .network_manager
49
                        .current_network
50
                        .as_ref()
51
                        .is_some_and(|current| current.ssid == net.ssid);
52
53
                    if is_connected {
54
                        app.action.send(Action::Disconnect);
55
                    } else {
56
                        app.action.send(Action::Connect(Box::new(ConnectRequest {
57
                            ssid: net.ssid,
58
                            interface: None,
59
                            credentials: WifiSecurity::Open,
60
                        })));
61
                    }
62
                }
63
            }
64
            Tabs::AvailableNetworks => {
65
                if let Some(Selected::Network(net)) = app.selected()
66
                    && net.is_psk
67
                {
68
                    Popups::Password(Input::new(), Box::new(net)).open(&app.action);
69
                }
70
            }
71
            Tabs::Devices => {}
72
        },
73
74
        _ => {}
75
    }
76
}