diff --git a/docs/modules/Custom.md b/docs/modules/Custom.md index 9a04381..05dc5d3 100644 --- a/docs/modules/Custom.md +++ b/docs/modules/Custom.md @@ -15,7 +15,8 @@ It is well worth looking at the examples. There are many widget types, each with their own config options. You can think of these like HTML elements and their attributes. -Every widget has the following options available; `type` is mandatory. +Every widget has the following options available; `type` is mandatory. +You can also add common [module-level options](https://github.com/JakeStanger/ironbar/wiki/configuration-guide#32-module-level-options) on a widget. | Name | Type | Default | Description | |---------|-------------------------------------------------------------------|---------|-------------------------------| diff --git a/src/modules/custom/box.rs b/src/modules/custom/box.rs index 13a6781..5f3340d 100644 --- a/src/modules/custom/box.rs +++ b/src/modules/custom/box.rs @@ -1,5 +1,6 @@ -use super::{try_get_orientation, CustomWidget, CustomWidgetContext, Widget}; +use super::{try_get_orientation, CustomWidget, CustomWidgetContext}; use crate::build; +use crate::modules::custom::WidgetConfig; use gtk::prelude::*; use gtk::Orientation; use serde::Deserialize; @@ -9,7 +10,7 @@ pub struct BoxWidget { name: Option, class: Option, orientation: Option, - widgets: Option>, + widgets: Option>, } impl CustomWidget for BoxWidget { @@ -26,7 +27,7 @@ impl CustomWidget for BoxWidget { if let Some(widgets) = self.widgets { for widget in widgets { - widget.add_to(&container, context); + widget.widget.add_to(&container, context, widget.common); } } diff --git a/src/modules/custom/mod.rs b/src/modules/custom/mod.rs index 5c9bfb5..182c37a 100644 --- a/src/modules/custom/mod.rs +++ b/src/modules/custom/mod.rs @@ -12,7 +12,9 @@ use self::slider::SliderWidget; use crate::config::CommonConfig; use crate::modules::custom::button::ButtonWidget; use crate::modules::custom::progress::ProgressWidget; -use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext}; +use crate::modules::{ + wrap_widget, Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext, +}; use crate::popup::WidgetGeometry; use crate::script::Script; use crate::send_async; @@ -29,14 +31,22 @@ pub struct CustomModule { /// Container class name class: Option, /// Widgets to add to the bar container - bar: Vec, + bar: Vec, /// Widgets to add to the popup container - popup: Option>, + popup: Option>, #[serde(flatten)] pub common: Option, } +#[derive(Debug, Deserialize, Clone)] +pub struct WidgetConfig { + #[serde(flatten)] + widget: Widget, + #[serde(flatten)] + common: CommonConfig, +} + #[derive(Debug, Deserialize, Clone)] #[serde(tag = "type", rename_all = "snake_case")] pub enum Widget { @@ -107,15 +117,23 @@ fn try_get_orientation(orientation: &str) -> Result { impl Widget { /// Creates this widget and adds it to the parent container - fn add_to(self, parent: >k::Box, context: CustomWidgetContext) { - match self { - Self::Box(widget) => parent.add(&widget.into_widget(context)), - Self::Label(widget) => parent.add(&widget.into_widget(context)), - Self::Button(widget) => parent.add(&widget.into_widget(context)), - Self::Image(widget) => parent.add(&widget.into_widget(context)), - Self::Slider(widget) => parent.add(&widget.into_widget(context)), - Self::Progress(widget) => parent.add(&widget.into_widget(context)), + fn add_to(self, parent: >k::Box, context: CustomWidgetContext, common: CommonConfig) { + macro_rules! create { + ($widget:expr) => { + wrap_widget(&$widget.into_widget(context), common) + }; } + + let event_box = match self { + Self::Box(widget) => create!(widget), + Self::Label(widget) => create!(widget), + Self::Button(widget) => create!(widget), + Self::Image(widget) => create!(widget), + Self::Slider(widget) => create!(widget), + Self::Progress(widget) => create!(widget), + }; + + parent.add(&event_box); } } @@ -186,7 +204,9 @@ impl Module for CustomModule { }; self.bar.clone().into_iter().for_each(|widget| { - widget.add_to(&container, custom_context); + widget + .widget + .add_to(&container, custom_context, widget.common); }); let popup = self.into_popup(context.controller_tx, context.popup_rx, info); @@ -222,7 +242,9 @@ impl Module for CustomModule { }; for widget in popup { - widget.add_to(&container, custom_context); + widget + .widget + .add_to(&container, custom_context, widget.common); } }