2023-04-10 00:17:52 +01:00
|
|
|
use gtk::prelude::*;
|
2023-04-10 13:51:07 +01:00
|
|
|
use gtk::ProgressBar;
|
2023-04-10 00:17:52 +01:00
|
|
|
use serde::Deserialize;
|
2023-12-17 23:51:43 +00:00
|
|
|
use tokio::sync::mpsc;
|
2023-04-10 00:17:52 +01:00
|
|
|
use tracing::error;
|
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
use crate::dynamic_value::dynamic_string;
|
|
|
|
use crate::modules::custom::set_length;
|
|
|
|
use crate::script::{OutputStream, Script, ScriptInput};
|
2023-12-17 23:51:43 +00:00
|
|
|
use crate::{build, glib_recv_mpsc, spawn, try_send};
|
2023-07-16 18:57:00 +01:00
|
|
|
|
|
|
|
use super::{try_get_orientation, CustomWidget, CustomWidgetContext};
|
|
|
|
|
2023-04-10 00:17:52 +01:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct ProgressWidget {
|
|
|
|
name: Option<String>,
|
|
|
|
class: Option<String>,
|
|
|
|
orientation: Option<String>,
|
|
|
|
label: Option<String>,
|
|
|
|
value: Option<ScriptInput>,
|
|
|
|
#[serde(default = "default_max")]
|
|
|
|
max: f64,
|
|
|
|
length: Option<i32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
const fn default_max() -> f64 {
|
|
|
|
100.0
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomWidget for ProgressWidget {
|
|
|
|
type Widget = ProgressBar;
|
|
|
|
|
|
|
|
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget {
|
2023-04-10 13:51:07 +01:00
|
|
|
let progress = build!(self, Self::Widget);
|
2023-04-10 00:17:52 +01:00
|
|
|
|
|
|
|
if let Some(orientation) = self.orientation {
|
2023-04-10 13:51:07 +01:00
|
|
|
progress.set_orientation(
|
|
|
|
try_get_orientation(&orientation).unwrap_or(context.bar_orientation),
|
|
|
|
);
|
2023-04-10 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(length) = self.length {
|
2023-04-10 13:51:07 +01:00
|
|
|
set_length(&progress, length, context.bar_orientation);
|
2023-04-10 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(value) = self.value {
|
|
|
|
let script = Script::from(value);
|
|
|
|
let progress = progress.clone();
|
|
|
|
|
2023-12-17 23:51:43 +00:00
|
|
|
let (tx, mut rx) = mpsc::channel(128);
|
2023-04-10 00:17:52 +01:00
|
|
|
|
|
|
|
spawn(async move {
|
|
|
|
script
|
|
|
|
.run(None, move |stream, _success| match stream {
|
|
|
|
OutputStream::Stdout(out) => match out.parse::<f64>() {
|
2023-12-17 23:51:43 +00:00
|
|
|
Ok(value) => try_send!(tx, value),
|
2023-04-10 00:17:52 +01:00
|
|
|
Err(err) => error!("{err:?}"),
|
|
|
|
},
|
|
|
|
OutputStream::Stderr(err) => error!("{err:?}"),
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
});
|
|
|
|
|
2023-12-17 23:51:43 +00:00
|
|
|
glib_recv_mpsc!(rx, value => progress.set_fraction(value / self.max));
|
2023-04-10 00:17:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(text) = self.label {
|
|
|
|
let progress = progress.clone();
|
|
|
|
progress.set_show_text(true);
|
|
|
|
|
2023-06-22 23:07:40 +01:00
|
|
|
dynamic_string(&text, move |string| {
|
2023-04-10 00:17:52 +01:00
|
|
|
progress.set_text(Some(&string));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
progress
|
|
|
|
}
|
|
|
|
}
|