GUI is working well now

main
_ 2021-07-12 01:01:47 +00:00
parent ef27c0443d
commit e21ba78fa5
3 changed files with 120 additions and 29 deletions

1
Cargo.lock generated
View File

@ -1296,6 +1296,7 @@ dependencies = [
"fltk",
"ptth_core",
"ptth_server",
"serde",
"tokio",
"tracing",
"tracing-subscriber",

View File

@ -6,7 +6,8 @@ edition = "2018"
[dependencies]
anyhow = "1.0.38"
fltk = "1.0.1"
fltk = "1.1.0"
serde = {version = "1.0.117", features = ["derive"]}
tokio = "1.4.0"
tracing = "0.1.25"
tracing-subscriber = "0.2.16"

View File

@ -1,8 +1,13 @@
use std::{
path::PathBuf,
};
use fltk::{
app,
button::Button,
enums::CallbackTrigger,
frame::Frame,
input::*,
prelude::*,
window::Window
};
@ -28,9 +33,6 @@ fn main ()
tracing_subscriber::fmt::init ();
let stopped_msg = "PTTH server: Stopped";
let started_msg = "PTTH server: Running";
let mut server_instance = None;
// let shutdown_rx = ptth_core::graceful_shutdown::init ();
@ -38,17 +40,10 @@ fn main ()
let (fltk_tx, fltk_rx) = app::channel::<Message> ();
let app = app::App::default();
let mut wind = Window::new (100, 100, 400, 300, "PTTH server");
let mut frame = Frame::new (0, 0, 400, 200, stopped_msg);
let mut wind = Window::new (100, 100, 500, 180, "PTTH server");
let mut but_run = Button::new (120, 210, 80, 40, "Run");
but_run.set_trigger (CallbackTrigger::Changed);
but_run.emit (fltk_tx, Message::RunServer);
let mut but_stop = Button::new (200, 210, 80, 40, "Stop");
but_stop.set_trigger (CallbackTrigger::Changed);
but_stop.emit (fltk_tx, Message::StopServer);
but_stop.deactivate ();
let mut gui = Gui::new (fltk_tx);
gui.set_server_running (false);
wind.end ();
wind.show ();
@ -58,15 +53,15 @@ fn main ()
Some (Message::RunServer) => {
let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel ();
let task = rt.spawn (async {
let config_file = ptth_server::ConfigFile {
name: "ptth_server".into (),
api_key: ptth_core::gen_key (),
relay_url: "http://127.0.0.1:4000/7ZSFUKGV".into (),
file_server_root: Some (".".into ()),
throttle_upload: false,
};
let config_file = ptth_server::ConfigFile {
name: gui.input_name.value ().to_string (),
api_key: gui.input_api_key.value ().to_string (),
relay_url: gui.input_relay_url.value ().to_string (),
file_server_root: Some (std::path::PathBuf::from (gui.input_file_server_root.value ())),
throttle_upload: false,
};
let task = rt.spawn (async {
if let Err (e) = ptth_server::run_server (
config_file,
shutdown_rx,
@ -83,18 +78,112 @@ fn main ()
shutdown_tx,
}));
frame.set_label (started_msg);
but_run.deactivate ();
but_stop.activate ();
gui.set_server_running (true);
},
Some (Message::StopServer) => {
server_instance.replace (None);
frame.set_label (stopped_msg);
but_run.activate ();
but_stop.deactivate ();
gui.set_server_running (false);
},
None => (),
}
}
}
struct Gui {
frame: Frame,
but_run: Button,
but_stop: Button,
input_name: Input,
input_relay_url: Input,
input_file_server_root: Input,
input_api_key: SecretInput,
}
#[derive (Default, serde::Deserialize)]
pub struct ConfigFile {
pub name: Option <String>,
pub api_key: String,
pub relay_url: Option <String>,
pub file_server_root: Option <PathBuf>,
}
impl Gui {
fn new (fltk_tx: app::Sender <Message>) -> Self {
let mut input_name = Input::new (200, 10, 290, 20, "name");
input_name.set_value ("my_ptth_server");
let mut input_relay_url = Input::new (200, 40, 290, 20, "relay_url");
input_relay_url.set_value ("http://127.0.0.1:4000/7ZSFUKGV");
let mut input_file_server_root = Input::new (200, 70, 290, 20, "file_server_root");
input_file_server_root.set_value ("/home/user");
let mut input_api_key = SecretInput::new (200, 100, 290, 20, "api_key");
input_api_key.set_value ("");
let mut but_run = Button::new (10, 130, 90, 40, "Run");
but_run.set_trigger (CallbackTrigger::Changed);
but_run.emit (fltk_tx, Message::RunServer);
let frame = Frame::new (110, 130, 280, 40, "");
let mut but_stop = Button::new (400, 130, 90, 40, "Stop");
but_stop.set_trigger (CallbackTrigger::Changed);
but_stop.emit (fltk_tx, Message::StopServer);
if let Ok (config_file) = ptth_server::load_toml::load::<ConfigFile, _> ("./config/ptth_server.toml")
{
if let Some (v) = config_file.name.as_ref () {
input_name.set_value (v);
}
input_api_key.set_value (&config_file.api_key);
if let Some (v) = config_file.relay_url.as_ref () {
input_relay_url.set_value (v);
}
if let Some (v) = config_file.file_server_root.as_ref () {
if let Some (v) = v.to_str () {
input_file_server_root.set_value (v);
}
}
}
Self {
frame,
but_run,
but_stop,
input_name,
input_relay_url,
input_file_server_root,
input_api_key,
}
}
fn set_server_running (&mut self, b: bool) {
self.frame.set_label (if b {
"Running"
}
else {
"Stopped"
});
set_active (&mut self.but_run, ! b);
set_active (&mut self.but_stop, b);
self.but_run.set (b);
self.but_stop.set (! b);
set_active (&mut self.input_name, ! b);
set_active (&mut self.input_relay_url, ! b);
set_active (&mut self.input_file_server_root, ! b);
set_active (&mut self.input_api_key, ! b);
}
}
fn set_active <W: WidgetExt> (w: &mut W, b: bool) {
if b {
w.activate ();
}
else {
w.deactivate ();
}
}