playit-gg 0.1.0

Unofficial Rust wrapper for https://playit.gg
Documentation
#![windows_subsystem = "windows"]

use iced::{widget, Align, Button, Column, Element, Result, Sandbox, Settings, Text, TextInput};
use playit_gg::{PlayIt, Prototype, Tunnel};

#[derive(Debug, Clone)]
pub enum Message {
  Action(String),
  SetPort(u16),
  CreateTunnel,
  Nothing,
}

#[derive(Default)]
pub struct PlayItGUI {
  action: String,
  port: u16,
  port_state: widget::text_input::State,
  start_button_state: widget::button::State,
  playit: PlayIt,
  tunnels: Vec<Tunnel>,
}

impl Sandbox for PlayItGUI {
  type Message = Message;

  fn new() -> Self {
    Self::default()
  }

  fn title(&self) -> String {
    println!("Action: {}", self.action);
    format!(
      "PlayIt.GG - {}",
      if self.action == String::new() {
        "Idle".to_owned()
      } else {
        self.action.clone()
      }
    )
  }

  fn update(&mut self, message: Message) {
    match message {
      Message::CreateTunnel => {
        self.action = "Creating A Tunnel".to_owned();

        self.tunnels.push(
          self
            .playit
            .create_tunnel(self.port, Prototype::Tcp, None)
            .unwrap(),
        );

        println!("{:#?}", self.tunnels)
      }
      Message::Action(action) => self.action = action,
      Message::SetPort(port) => {
        self.action = format!("Using Port: {}", port);
        self.port = port;
      }
      Message::Nothing => (),
    }
  }

  fn view(&mut self) -> Element<Message> {
    Column::new()
      .padding(60)
      .align_items(Align::Center)
      .push(TextInput::new(
        &mut self.port_state,
        "Port",
        &self.port.to_string(),
        |port| {
          let (length, port) = (port.len(), port.parse::<u16>());

          if length == 1 || port.is_ok() {
            Message::SetPort(port.unwrap_or(0))
          } else {
            Message::Nothing
          }
        },
      ))
      .spacing(30)
      .push(
        Button::new(&mut self.start_button_state, Text::new("Create Tunnel"))
          .on_press(Message::CreateTunnel),
      )
      .into()
  }
}

pub fn main() -> Result {
  PlayItGUI::run(Settings::default())
}