2025-05-18 15:17:09 +01:00
|
|
|
use crate::channels::{AsyncSenderExt, BroadcastReceiverExt};
|
2025-03-22 19:19:47 +00:00
|
|
|
use crate::config::{CommonConfig, LayoutConfig, TruncateMode};
|
2023-06-22 23:07:40 +01:00
|
|
|
use crate::dynamic_value::dynamic_string;
|
2024-10-15 22:10:37 +01:00
|
|
|
use crate::gtk_helpers::IronbarLabelExt;
|
2025-05-18 15:17:09 +01:00
|
|
|
use crate::module_impl;
|
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleParts, WidgetContext};
|
2023-04-07 14:27:16 +01:00
|
|
|
use color_eyre::Result;
|
|
|
|
use gtk::Label;
|
|
|
|
use serde::Deserialize;
|
|
|
|
use tokio::sync::mpsc;
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-04-07 14:27:16 +01:00
|
|
|
pub struct LabelModule {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// The text to show on the label.
|
|
|
|
/// This is a [Dynamic String](dynamic-values#dynamic-string).
|
|
|
|
///
|
|
|
|
/// **Required**
|
2023-04-07 14:27:16 +01:00
|
|
|
label: String,
|
|
|
|
|
2025-02-21 21:11:41 +00:00
|
|
|
// -- Common --
|
|
|
|
/// See [truncate options](module-level-options#truncate-mode).
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
|
|
|
truncate: Option<TruncateMode>,
|
|
|
|
|
2025-03-22 19:19:47 +00:00
|
|
|
/// See [layout options](module-level-options#layout)
|
|
|
|
#[serde(default, flatten)]
|
|
|
|
layout: LayoutConfig,
|
|
|
|
|
2024-05-19 15:16:01 +01:00
|
|
|
/// See [common options](module-level-options#common-options).
|
2023-04-07 14:27:16 +01:00
|
|
|
#[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,
|
2025-02-21 21:11:41 +00:00
|
|
|
truncate: None,
|
2025-03-22 19:19:47 +00:00
|
|
|
layout: LayoutConfig::default(),
|
2023-07-03 22:47:36 +01:00
|
|
|
common: Some(CommonConfig::default()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 14:27:16 +01:00
|
|
|
impl Module<Label> for LabelModule {
|
|
|
|
type SendMessage = String;
|
|
|
|
type ReceiveMessage = ();
|
|
|
|
|
2024-03-14 22:35:33 +00:00
|
|
|
module_impl!("label");
|
2023-04-07 14:27:16 +01:00
|
|
|
|
|
|
|
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<()> {
|
2025-05-26 22:17:26 +01:00
|
|
|
dynamic_string(&self.label, &context.tx, move |tx, string| {
|
2025-05-18 15:17:09 +01:00
|
|
|
tx.send_update_spawn(string);
|
2023-04-07 14:27:16 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2025-03-22 19:19:47 +00:00
|
|
|
info: &ModuleInfo,
|
2023-07-16 18:57:00 +01:00
|
|
|
) -> Result<ModuleParts<Label>> {
|
2025-03-22 19:19:47 +00:00
|
|
|
let label = Label::builder()
|
|
|
|
.use_markup(true)
|
|
|
|
.angle(self.layout.angle(info))
|
|
|
|
.justify(self.layout.justify.into())
|
|
|
|
.build();
|
2023-04-07 14:27:16 +01:00
|
|
|
|
2025-02-21 21:11:41 +00:00
|
|
|
if let Some(truncate) = self.truncate {
|
|
|
|
label.truncate(truncate);
|
|
|
|
}
|
|
|
|
|
2025-05-26 22:17:26 +01:00
|
|
|
context.subscribe().recv_glib(&label, move |label, string| {
|
|
|
|
label.set_label_escaped(&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,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|