Browse Source

First checkin of code

main v0.0.1
hackerncoder 3 months ago
parent
commit
b00d73f738
  1. 9
      Cargo.toml
  2. BIN
      proxy
  3. 77
      src/main.rs

9
Cargo.toml

@ -0,0 +1,9 @@
[package]
name = "snowflake-frontend"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
gtk = { version = "0.2", package = "gtk4" }

BIN
proxy

Binary file not shown.

77
src/main.rs

@ -0,0 +1,77 @@
use std::process::{Command, Stdio};
use std::io::{BufRead, BufReader};
use std::thread;
use std::sync::mpsc;
use std::rc::Rc;
use std::cell::Cell;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
fn main() {
let app = Application::builder()
.application_id("org.hackerncoder.snowflake-frontend")
.build();
app.connect_activate(build_ui);
app.run();
}
fn build_ui (app: &Application) {
let window = ApplicationWindow::builder()
.application(app)
.title("Snowflake Frontend")
.build();
let button = Button::builder()
.label("Start Snowflake")
.margin_top(12)
.margin_bottom(12)
.margin_start(12)
.margin_end(12)
.build();
let is_snowflake_running = Rc::new(Cell::new(false));
let snowflake_thread_id = Rc::new(Cell::new(0));
button.connect_clicked(gtk::glib::clone!(@strong is_snowflake_running =>
move |button| {
if is_snowflake_running.get() {
button.set_label("Start Snowflake");
is_snowflake_running.set(false);
Command::new("kill").arg(snowflake_thread_id.get().to_string()).spawn().unwrap();
} else {
button.set_label("Stop Snowflake");
let (tx, rx) = mpsc::channel();
thread::spawn(|| {
run_snowflake(tx);
});
snowflake_thread_id.set(rx.recv().unwrap());
//println!("{}", snowflake_thread_id.get());
is_snowflake_running.set(true);
}
}));
window.set_child(Some(&button));
window.present();
}
fn run_snowflake(tx: mpsc::Sender<u32>) {
let mut proxy = Command::new("./proxy")
//.args(&["./proxy"])
.stdout(Stdio::piped())
.spawn()
.unwrap();
tx.send(proxy.id()).unwrap();
let mut child_out = BufReader::new(proxy.stdout.as_mut().unwrap());
let mut line = String::new();
loop {
child_out.read_line(&mut line).unwrap();
//println!("{}", line);
}
}
Loading…
Cancel
Save