2023-04-07 14:27:16 +01:00
|
|
|
use crate::config::CommonConfig;
|
2023-06-22 23:07:40 +01:00
|
|
|
use crate::dynamic_value::dynamic_string;
|
2023-07-16 18:57:00 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleParts, ModuleUpdateEvent, WidgetContext};
|
2023-12-17 23:51:43 +00:00
|
|
|
use crate::{glib_recv, try_send};
|
2023-04-07 14:27:16 +01:00
|
|
|
use color_eyre::Result;
|
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::Label;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct LabelModule {
|
|
|
|
label: String,
|
|
|
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: Option<CommonConfig>,
|
|
|
|
}
|
|
|
|
|
2023-07-03 22:47:36 +01:00
|
|
|
impl LabelModule {
|
|
|
|
pub(crate) fn new(label: String) -> Self {
|
|
|
|
Self {
|
|
|
|
label,
|
|
|
|
common: Some(CommonConfig::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:27:16 +01:00
|
|
|
impl Module<Label> for LabelModule {
|
|
|
|
type SendMessage = String;
|
|
|
|
type ReceiveMessage = ();
|
|
|
|
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"label"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
2024-01-07 23:42:34 +00:00
|
|
|
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2023-04-07 14:27:16 +01:00
|
|
|
_rx: mpsc::Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
2024-01-07 23:42:34 +00:00
|
|
|
let tx = context.tx.clone();
|
2023-06-22 23:07:40 +01:00
|
|
|
dynamic_string(&self.label, move |string| {
|
2023-04-07 14:27:16 +01:00
|
|
|
try_send!(tx, ModuleUpdateEvent::Update(string));
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
|
|
|
_info: &ModuleInfo,
|
2023-07-16 18:57:00 +01:00
|
|
|
) -> Result<ModuleParts<Label>> {
|
2023-04-07 14:27:16 +01:00
|
|
|
let label = Label::new(None);
|
2023-07-03 22:46:04 +01:00
|
|
|
label.set_use_markup(true);
|
2023-04-07 14:27:16 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
let label = label.clone();
|
2023-12-17 23:51:43 +00:00
|
|
|
glib_recv!(context.subscribe(), string => label.set_markup(&string));
|
2023-04-07 14:27:16 +01:00
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
Ok(ModuleParts {
|
2023-04-07 14:27:16 +01:00
|
|
|
widget: label,
|
|
|
|
popup: None,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|