mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
feat: new label module
Takes a text label, with the ability to include embedded scripts. Resolves #80.
This commit is contained in:
parent
55c06c4766
commit
6c622864b3
11 changed files with 153 additions and 1 deletions
|
@ -21,6 +21,7 @@
|
||||||
- [Clock](clock)
|
- [Clock](clock)
|
||||||
- [Custom](custom)
|
- [Custom](custom)
|
||||||
- [Focused](focused)
|
- [Focused](focused)
|
||||||
|
- [Label](label)
|
||||||
- [Launcher](launcher)
|
- [Launcher](launcher)
|
||||||
- [Music](music)
|
- [Music](music)
|
||||||
- [Script](script)
|
- [Script](script)
|
||||||
|
|
70
docs/modules/Label.md
Normal file
70
docs/modules/Label.md
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
Displays custom text, with the ability to embed [scripts](https://github.com/JakeStanger/ironbar/wiki/scripts#embedding).
|
||||||
|
|
||||||
|
## Configuration
|
||||||
|
|
||||||
|
> Type: `label`
|
||||||
|
|
||||||
|
| Name | Type | Default | Description |
|
||||||
|
|---------|----------|---------|-----------------------------------------|
|
||||||
|
| `label` | `string` | `null` | Text, optionally with embedded scripts. |
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>JSON</summary>
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"end": [
|
||||||
|
{
|
||||||
|
"type": "label",
|
||||||
|
"label": "random num: {{500:echo $RANDOM}}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>TOML</summary>
|
||||||
|
|
||||||
|
```toml
|
||||||
|
[[end]]
|
||||||
|
type = "label"
|
||||||
|
label = "random num: {{500:echo $RANDOM}}"
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>YAML</summary>
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
end:
|
||||||
|
- type: "label"
|
||||||
|
label: "random num: {{500:echo $RANDOM}}"
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
<details>
|
||||||
|
<summary>Corn</summary>
|
||||||
|
|
||||||
|
```corn
|
||||||
|
{
|
||||||
|
end = [
|
||||||
|
{
|
||||||
|
type = "label"
|
||||||
|
label = "random num: {{500:echo $RANDOM}}"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
## Styling
|
||||||
|
|
||||||
|
| Selector | Description |
|
||||||
|
|--------------------------------|------------------------------------------------------------------------------------|
|
||||||
|
| `#label` | Label widget |
|
|
@ -67,6 +67,8 @@ let {
|
||||||
|
|
||||||
$clipboard = { type = "clipboard" max_items = 3 truncate.mode = "end" truncate.length = 50 }
|
$clipboard = { type = "clipboard" max_items = 3 truncate.mode = "end" truncate.length = 50 }
|
||||||
|
|
||||||
|
$label = { type = "label" label = "random num: {{500:echo $RANDOM}}" }
|
||||||
|
|
||||||
// -- begin custom --
|
// -- begin custom --
|
||||||
$button = { type = "button" name="power-btn" label = "" on_click = "popup:toggle" }
|
$button = { type = "button" name="power-btn" label = "" on_click = "popup:toggle" }
|
||||||
|
|
||||||
|
@ -97,7 +99,7 @@ let {
|
||||||
}
|
}
|
||||||
// -- end custom --
|
// -- end custom --
|
||||||
|
|
||||||
$left = [ $workspaces $launcher ]
|
$left = [ $workspaces $launcher $label ]
|
||||||
$right = [ $mpd_local $mpd_server $phone_battery $sys_info $clipboard $power_menu $clock ]
|
$right = [ $mpd_local $mpd_server $phone_battery $sys_info $clipboard $power_menu $clock ]
|
||||||
}
|
}
|
||||||
in {
|
in {
|
||||||
|
|
|
@ -126,6 +126,10 @@
|
||||||
"show_icons": true,
|
"show_icons": true,
|
||||||
"show_names": false,
|
"show_names": false,
|
||||||
"type": "launcher"
|
"type": "launcher"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"label": "random num: {{500:echo $RANDOM}}",
|
||||||
|
"type": "label"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -116,3 +116,7 @@ favorites = [
|
||||||
'Steam',
|
'Steam',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[start]]
|
||||||
|
label = 'random num: {{500:echo $RANDOM}}'
|
||||||
|
type = 'label'
|
||||||
|
|
||||||
|
|
|
@ -82,4 +82,6 @@ start:
|
||||||
show_icons: true
|
show_icons: true
|
||||||
show_names: false
|
show_names: false
|
||||||
type: launcher
|
type: launcher
|
||||||
|
- label: 'random num: {{500:echo $RANDOM}}'
|
||||||
|
type: label
|
||||||
|
|
||||||
|
|
|
@ -209,6 +209,7 @@ fn add_modules(content: >k::Box, modules: Vec<ModuleConfig>, info: &ModuleInfo
|
||||||
ModuleConfig::Clock(mut module) => add_module!(module, id),
|
ModuleConfig::Clock(mut module) => add_module!(module, id),
|
||||||
ModuleConfig::Custom(mut module) => add_module!(module, id),
|
ModuleConfig::Custom(mut module) => add_module!(module, id),
|
||||||
ModuleConfig::Focused(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),
|
ModuleConfig::Launcher(mut module) => add_module!(module, id),
|
||||||
#[cfg(feature = "music")]
|
#[cfg(feature = "music")]
|
||||||
ModuleConfig::Music(mut module) => add_module!(module, id),
|
ModuleConfig::Music(mut module) => add_module!(module, id),
|
||||||
|
|
|
@ -7,6 +7,7 @@ use crate::modules::clipboard::ClipboardModule;
|
||||||
use crate::modules::clock::ClockModule;
|
use crate::modules::clock::ClockModule;
|
||||||
use crate::modules::custom::CustomModule;
|
use crate::modules::custom::CustomModule;
|
||||||
use crate::modules::focused::FocusedModule;
|
use crate::modules::focused::FocusedModule;
|
||||||
|
use crate::modules::label::LabelModule;
|
||||||
use crate::modules::launcher::LauncherModule;
|
use crate::modules::launcher::LauncherModule;
|
||||||
#[cfg(feature = "music")]
|
#[cfg(feature = "music")]
|
||||||
use crate::modules::music::MusicModule;
|
use crate::modules::music::MusicModule;
|
||||||
|
@ -47,6 +48,7 @@ pub enum ModuleConfig {
|
||||||
Clock(Box<ClockModule>),
|
Clock(Box<ClockModule>),
|
||||||
Custom(Box<CustomModule>),
|
Custom(Box<CustomModule>),
|
||||||
Focused(Box<FocusedModule>),
|
Focused(Box<FocusedModule>),
|
||||||
|
Label(Box<LabelModule>),
|
||||||
Launcher(Box<LauncherModule>),
|
Launcher(Box<LauncherModule>),
|
||||||
#[cfg(feature = "music")]
|
#[cfg(feature = "music")]
|
||||||
Music(Box<MusicModule>),
|
Music(Box<MusicModule>),
|
||||||
|
|
|
@ -10,9 +10,12 @@ enum DynamicStringSegment {
|
||||||
Dynamic(Script),
|
Dynamic(Script),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A string with embedded scripts for dynamic content.
|
||||||
pub struct DynamicString;
|
pub struct DynamicString;
|
||||||
|
|
||||||
impl 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
|
pub fn new<F>(input: &str, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: FnMut(String) -> Continue + 'static,
|
F: FnMut(String) -> Continue + 'static,
|
||||||
|
|
62
src/modules/label.rs
Normal file
62
src/modules/label.rs
Normal 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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -10,6 +10,7 @@ pub mod clipboard;
|
||||||
pub mod clock;
|
pub mod clock;
|
||||||
pub mod custom;
|
pub mod custom;
|
||||||
pub mod focused;
|
pub mod focused;
|
||||||
|
pub mod label;
|
||||||
pub mod launcher;
|
pub mod launcher;
|
||||||
#[cfg(feature = "music")]
|
#[cfg(feature = "music")]
|
||||||
pub mod music;
|
pub mod music;
|
||||||
|
|
Loading…
Add table
Reference in a new issue