Merge pull request #102 from JakeStanger/feat/labels

feat: new label module
This commit is contained in:
Jake Stanger 2023-04-07 14:53:56 +01:00 committed by GitHub
commit 0691db3b87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 153 additions and 1 deletions

View File

@ -21,6 +21,7 @@
- [Clock](clock)
- [Custom](custom)
- [Focused](focused)
- [Label](label)
- [Launcher](launcher)
- [Music](music)
- [Script](script)

70
docs/modules/Label.md Normal file
View 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 |

View File

@ -67,6 +67,8 @@ let {
$clipboard = { type = "clipboard" max_items = 3 truncate.mode = "end" truncate.length = 50 }
$label = { type = "label" label = "random num: {{500:echo $RANDOM}}" }
// -- begin custom --
$button = { type = "button" name="power-btn" label = "" on_click = "popup:toggle" }
@ -97,7 +99,7 @@ let {
}
// -- end custom --
$left = [ $workspaces $launcher ]
$left = [ $workspaces $launcher $label ]
$right = [ $mpd_local $mpd_server $phone_battery $sys_info $clipboard $power_menu $clock ]
}
in {

View File

@ -126,6 +126,10 @@
"show_icons": true,
"show_names": false,
"type": "launcher"
},
{
"label": "random num: {{500:echo $RANDOM}}",
"type": "label"
}
]
}

View File

@ -116,3 +116,7 @@ favorites = [
'Steam',
]
[[start]]
label = 'random num: {{500:echo $RANDOM}}'
type = 'label'

View File

@ -82,4 +82,6 @@ start:
show_icons: true
show_names: false
type: launcher
- label: 'random num: {{500:echo $RANDOM}}'
type: label

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;