index : nmrs-tui
A fast, Vim-friendly NetworkManager TUI built in Rust.
about summary refs log tree commit diff

Diffstat (limited to 'src/ui/mod.rs')

-rw-r--r-- src/ui/mod.rs 230
 1 file changed, 65 insertions(+), 165 deletions(-)

diff --git a/src/ui/mod.rs b/src/ui/mod.rs
index e1551c1..24ac5d9 100644
--- a/src/ui/mod.rs
+++ b/src/ui/mod.rs
@@ -1,176 +1,76 @@
use std::rc::Rc;
use ratatui::layout::Rect;

use nmrs::Network;
use ratatui::{
    Frame,
    layout::{Constraint, Direction, Layout, Position, Rect},
    style::{Color, Style, Stylize},
    widgets::{Block, Cell, Paragraph, Row},
};

use crate::{
    app::App,
    tui::Tabs,
    ui::{input::Input, table::TableData},
};

pub mod input;
pub mod list;
pub mod popup;
pub mod table;

// Helper to calculate centered rect
pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
#[allow(dead_code)]
#[derive(Default)]
pub enum Position {
    #[default]
    LeftTop,
    Top,
    RightTop,

    LeftCenter,
    Center,
    RightCenter,

    LeftBottom,
    Bottom,
    RightBottom,
}

pub fn render_password_popup(f: &mut Frame, input: &Input, selected_network: &Option<Network>) {
    // Dim the background.
    let dimming_block = ratatui::widgets::Block::default().bg(Color::Black);
    f.render_widget(dimming_block, f.area());

    let input_area = centered_rect(30, 6, f.area()); // Height 3 for one line + borders
    f.render_widget(ratatui::widgets::Clear, input_area); // Clear the table behind it

    // Hide password with asterisks for security
    let display_pass: String = input.value.chars().map(|_| '*').collect();

    let block = Block::bordered()
        .title(format!(
            " Password for {} ",
            selected_network
                .as_ref()
                .map(|n| n.ssid.as_str())
                .unwrap_or("Network")
        ))
        .border_style(Style::default().fg(Color::Yellow));

    let text = Paragraph::new(display_pass).block(block);
    f.render_widget(text, input_area);
    f.set_cursor_position(Position::new(
        // Draw the cursor at the current position in the input field.
        // This position can be controlled via the left and right arrow key
        input_area.x + input.cx as u16 + 1,
        // Move one line down, from the border to the input line
        input_area.y + 1,
    ))
pub enum Urgency {
    Success,
    Warning,
    Critical,
}

pub fn render_all_tables(
    f: &mut Frame<'_>,
    body_chunks: Rc<[Rect]>,
    app: &mut App,
    active_tab: &Tabs,
) {
    let rows: Vec<Row> = app
        .known_networks
        .items
        .iter()
        .map(|net| -> Row<'_> {
            Row::new(vec![
                Cell::from(net.ssid.clone()),
                Cell::from(if net.secured { "Secured" } else { "Open" }),
                Cell::from(if app.network_manager.is_connected_cached(&net.ssid) {
                    "Connected"
                } else {
                    "-"
                }),
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
            ])
        })
        .collect();
    table::draw(
        f,
        body_chunks[0],
        &mut TableData {
            title: " Known Networks ",
            header_cols: vec!["Name", "Security", "State", "Signal"],
            constraint: vec![
                Constraint::Percentage(40),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
            ],
            cells: rows,
            list: &mut app.known_networks,
        },
        *active_tab == Tabs::KnownNetworks,
    );
    let rows: Vec<Row> = app
        .available_networks
        .items
        .iter()
        .map(|net| -> Row<'_> {
            Row::new(vec![
                Cell::from(net.ssid.clone()),
                Cell::from(if net.secured { "Secured" } else { "Open" }),
                Cell::from(format!("{}%", net.strength.unwrap_or(0))),
            ])
        })
        .collect();
    table::draw(
        f,
        body_chunks[1],
        &mut TableData {
            title: " Available Networks ",
            header_cols: vec!["Name", "Security", "Signal"],
            constraint: vec![
                Constraint::Percentage(40),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
            ],
            cells: rows,
            list: &mut app.available_networks,
        },
        *active_tab == Tabs::AvailableNetworks,
    );
#[derive(Default)]
pub struct Gaps {
    top: u16,
    bottom: u16,
    left: u16,
    right: u16,
}

    let rows: Vec<Row> = app
        .devices
        .items
        .iter()
        .map(|net| -> Row<'_> {
            Row::new(vec![
                Cell::from(net.interface.clone()),
                Cell::from(if app.network_manager.enabled {
                    "On"
                } else {
                    "Off"
                }),
            ])
        })
        .collect();
    table::draw(
        f,
        body_chunks[2],
        &mut TableData {
            title: " Device ",
            header_cols: vec!["Name", "Powered", "Scanning", "Frequency", "Security"],
            constraint: vec![
                Constraint::Percentage(20),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
                Constraint::Percentage(20),
            ],
            cells: rows,
            list: &mut app.devices,
        },
        *active_tab == Tabs::Devices,
    );
// calculate area position
pub fn position_rect(
    area: Rect,
    width: u16,
    height: u16,
    position: Position,
    gaps: Option<Gaps>,
) -> Rect {
    let gaps = gaps.unwrap_or_default();
    let x = match position {
        Position::LeftTop | Position::LeftCenter | Position::LeftBottom => 0,
        Position::Top | Position::Center | Position::Bottom => (area.width.saturating_sub(width)
            / 2)
        .saturating_add(gaps.left)
        .saturating_sub(gaps.right),
        Position::RightTop | Position::RightCenter | Position::RightBottom => area
            .width
            .saturating_sub(width)
            .saturating_add(gaps.left)
            .saturating_sub(gaps.right),
    };

    let y = match position {
        Position::LeftTop | Position::Top | Position::RightTop => 0,
        Position::LeftCenter | Position::Center | Position::RightCenter => {
            (area.height.saturating_sub(height) / 2)
                .saturating_add(gaps.top)
                .saturating_sub(gaps.bottom)
        }
        Position::LeftBottom | Position::Bottom | Position::RightBottom => area
            .height
            .saturating_sub(height)
            .saturating_add(gaps.top)
            .saturating_sub(gaps.bottom),
    };

    Rect::new(x, y, width, height)
}