more ui stuff

This commit is contained in:
Joe Ardent 2025-07-07 20:18:19 -07:00
parent 91beef2657
commit d3d8a5f3fa
3 changed files with 40 additions and 2 deletions

View file

@ -1,4 +1,4 @@
use std::io; use std::{collections::BTreeMap, io, net::SocketAddr};
use joecalsend::{Config, JoecalState, RunningState, error, models::Device}; use joecalsend::{Config, JoecalState, RunningState, error, models::Device};
use local_ip_address::local_ip; use local_ip_address::local_ip;
@ -14,6 +14,8 @@ use ratatui::{
widgets::{Block, Paragraph, Widget}, widgets::{Block, Paragraph, Widget},
}; };
mod ui;
#[tokio::main] #[tokio::main]
async fn main() -> error::Result<()> { async fn main() -> error::Result<()> {
let device = Device::default(); let device = Device::default();
@ -60,6 +62,8 @@ struct App {
state: JoecalState, state: JoecalState,
rstate: RunningState, rstate: RunningState,
screen: CurrentScreen, screen: CurrentScreen,
// addr to (alias, fingerprint)
peers: BTreeMap<SocketAddr, (String, String)>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
@ -75,6 +79,7 @@ impl App {
state: state.clone(), state: state.clone(),
rstate: *state.running_state.lock().await, rstate: *state.running_state.lock().await,
screen: CurrentScreen::Main, screen: CurrentScreen::Main,
peers: Default::default(),
} }
} }
@ -87,6 +92,14 @@ impl App {
if *rstate == RunningState::Stopping { if *rstate == RunningState::Stopping {
break; break;
} }
let peers = self.state.peers.lock().await;
peers.iter().for_each(|(k, v)| {
// k is fingerprint, v is addr, device
let addr = v.0;
let alias = v.1.alias.clone();
let fingerprint = k.clone();
self.peers.insert(addr, (alias, fingerprint));
});
} }
Ok(()) Ok(())
} }

View file

@ -103,7 +103,7 @@ pub enum Protocol {
impl Default for Device { impl Default for Device {
fn default() -> Self { fn default() -> Self {
Self { Self {
alias: "RustSend".to_string(), alias: "Joecalsend".to_string(),
version: "2.1".to_string(), version: "2.1".to_string(),
device_model: None, device_model: None,
device_type: Some(DeviceType::Headless), device_type: Some(DeviceType::Headless),

25
src/ui/mod.rs Normal file
View file

@ -0,0 +1,25 @@
use ratatui::layout::{Constraint, Direction, Layout, Rect};
// helper function to create a centered rect using up certain percentage of the
// available rect `r`
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
// Cut the given rectangle into three vertical pieces
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);
// Then cut the middle vertical piece into three width-wise pieces
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] // Return the middle chunk
}