improve gui

This commit is contained in:
LordMZTE 2022-03-31 16:29:17 +02:00
parent 16ad213ae4
commit bd8f1409bd
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6

View file

@ -1,3 +1,5 @@
use std::{cell::Cell, rc::Rc, thread::JoinHandle};
use gtk4::prelude::*;
use crate::start_streamlink;
@ -9,12 +11,22 @@ pub struct GuiInitData {
}
pub fn run_gui(init: GuiInitData) {
let streamlink_handle = Rc::new(Cell::new(None));
let app = gtk4::Application::new(Some("de.mzte.playtwitch"), Default::default());
app.connect_activate(move |app| build_ui(app, &init));
let streamlink_handle_ = streamlink_handle.clone();
app.connect_activate(move |app| build_ui(app, &init, streamlink_handle_.clone()));
app.run();
if let Some(handle) = streamlink_handle.take() {
handle.join().unwrap();
}
}
fn build_ui(app: &gtk4::Application, init: &GuiInitData) {
fn build_ui(
app: &gtk4::Application,
init: &GuiInitData,
streamlink_handle_out: Rc<Cell<Option<JoinHandle<()>>>>,
) {
let win = gtk4::ApplicationWindow::builder()
.application(app)
.title("Pick a stream!")
@ -32,8 +44,45 @@ fn build_ui(app: &gtk4::Application, init: &GuiInitData) {
quality_entry.set_hexpand(true);
quality_entry.set_text(&init.quality);
let other_channel = gtk4::Entry::builder()
.placeholder_text("Other Channel...")
.hexpand(true)
.build();
vbox.append(&other_channel);
// focus other channel initially
vbox.set_focus_child(Some(&other_channel));
let app_ = app.clone();
let quality_entry_ = quality_entry.clone();
let streamlink_handle_out_ = streamlink_handle_out.clone();
other_channel.connect_activate(move |this| {
let channel = this.text().to_string();
let quality = quality_entry_.text().to_string();
streamlink_handle_out_.set(Some(std::thread::spawn(move || {
if let Err(e) = start_streamlink(&channel, &quality) {
eprintln!("Streamlink Error: {:?}", e);
}
})));
app_.quit();
});
let list = gtk4::ListBox::new();
vbox.append(&list);
vbox.append(
&gtk4::Frame::builder()
.child(
&gtk4::ScrolledWindow::builder()
.child(&list)
.vexpand(true)
.vscrollbar_policy(gtk4::PolicyType::Always)
.hscrollbar_policy(gtk4::PolicyType::Automatic)
.build(),
)
.label("Quick Channels")
.build(),
);
for channel in init.channels.iter() {
let entry = gtk4::ListBoxRow::new();
@ -53,11 +102,11 @@ fn build_ui(app: &gtk4::Application, init: &GuiInitData) {
let quality = quality_entry.text().to_string();
let channel = label.text().to_string();
std::thread::spawn(move || {
streamlink_handle_out.set(Some(std::thread::spawn(move || {
if let Err(e) = start_streamlink(&channel, &quality) {
eprintln!("Streamlink Error: {:?}", e);
}
});
})));
app_.quit();
});