1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

feat: new label module

Takes a text label, with the ability to include embedded scripts.

Resolves #80.
This commit is contained in:
Jake Stanger 2023-04-07 14:27:16 +01:00
parent 55c06c4766
commit 6c622864b3
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
11 changed files with 153 additions and 1 deletions

View file

@ -209,6 +209,7 @@ fn add_modules(content: &gtk::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo
ModuleConfig::Clock(mut module) => add_module!(module, id),
ModuleConfig::Custom(mut module) => add_module!(module, id),
ModuleConfig::Focused(mut module) => add_module!(module, id),
ModuleConfig::Label(mut module) => add_module!(module, id),
ModuleConfig::Launcher(mut module) => add_module!(module, id),
#[cfg(feature = "music")]
ModuleConfig::Music(mut module) => add_module!(module, id),

View file

@ -7,6 +7,7 @@ use crate::modules::clipboard::ClipboardModule;
use crate::modules::clock::ClockModule;
use crate::modules::custom::CustomModule;
use crate::modules::focused::FocusedModule;
use crate::modules::label::LabelModule;
use crate::modules::launcher::LauncherModule;
#[cfg(feature = "music")]
use crate::modules::music::MusicModule;
@ -47,6 +48,7 @@ pub enum ModuleConfig {
Clock(Box<ClockModule>),
Custom(Box<CustomModule>),
Focused(Box<FocusedModule>),
Label(Box<LabelModule>),
Launcher(Box<LauncherModule>),
#[cfg(feature = "music")]
Music(Box<MusicModule>),

View file

@ -10,9 +10,12 @@ enum DynamicStringSegment {
Dynamic(Script),
}
/// A string with embedded scripts for dynamic content.
pub struct DynamicString;
impl DynamicString {
/// Creates a new dynamic string, based off the input template.
/// Runs `f` with the compiled string each time one of the scripts updates.
pub fn new<F>(input: &str, f: F) -> Self
where
F: FnMut(String) -> Continue + 'static,

62
src/modules/label.rs Normal file
View file

@ -0,0 +1,62 @@
use crate::config::CommonConfig;
use crate::dynamic_string::DynamicString;
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
use crate::try_send;
use color_eyre::Result;
use glib::Continue;
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>,
}
impl Module<Label> for LabelModule {
type SendMessage = String;
type ReceiveMessage = ();
fn name() -> &'static str {
"label"
}
fn spawn_controller(
&self,
_info: &ModuleInfo,
tx: mpsc::Sender<ModuleUpdateEvent<Self::SendMessage>>,
_rx: mpsc::Receiver<Self::ReceiveMessage>,
) -> Result<()> {
DynamicString::new(&self.label, move |string| {
try_send!(tx, ModuleUpdateEvent::Update(string));
Continue(true)
});
Ok(())
}
fn into_widget(
self,
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
_info: &ModuleInfo,
) -> Result<ModuleWidget<Label>> {
let label = Label::new(None);
{
let label = label.clone();
context.widget_rx.attach(None, move |string| {
label.set_label(&string);
Continue(true)
});
}
Ok(ModuleWidget {
widget: label,
popup: None,
})
}
}

View file

@ -10,6 +10,7 @@ pub mod clipboard;
pub mod clock;
pub mod custom;
pub mod focused;
pub mod label;
pub mod launcher;
#[cfg(feature = "music")]
pub mod music;