Skip to content

Commit

Permalink
feat(custom): support common options in widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Apr 22, 2023
1 parent 9d09855 commit 4a09b70
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
3 changes: 2 additions & 1 deletion docs/modules/Custom.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
|---------|-------------------------------------------------------------------|---------|-------------------------------|
Expand Down
7 changes: 4 additions & 3 deletions src/modules/custom/box.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -9,7 +10,7 @@ pub struct BoxWidget {
name: Option<String>,
class: Option<String>,
orientation: Option<String>,
widgets: Option<Vec<Widget>>,
widgets: Option<Vec<WidgetConfig>>,
}

impl CustomWidget for BoxWidget {
Expand All @@ -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);
}
}

Expand Down
48 changes: 35 additions & 13 deletions src/modules/custom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,14 +31,22 @@ pub struct CustomModule {
/// Container class name
class: Option<String>,
/// Widgets to add to the bar container
bar: Vec<Widget>,
bar: Vec<WidgetConfig>,
/// Widgets to add to the popup container
popup: Option<Vec<Widget>>,
popup: Option<Vec<WidgetConfig>>,

#[serde(flatten)]
pub common: Option<CommonConfig>,
}

#[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 {
Expand Down Expand Up @@ -107,15 +117,23 @@ fn try_get_orientation(orientation: &str) -> Result<Orientation> {

impl Widget {
/// Creates this widget and adds it to the parent container
fn add_to(self, parent: &gtk::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: &gtk::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);
}
}

Expand Down Expand Up @@ -186,7 +204,9 @@ impl Module<gtk::Box> 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);
Expand Down Expand Up @@ -222,7 +242,9 @@ impl Module<gtk::Box> for CustomModule {
};

for widget in popup {
widget.add_to(&container, custom_context);
widget
.widget
.add_to(&container, custom_context, widget.common);
}
}

Expand Down

0 comments on commit 4a09b70

Please sign in to comment.