mirror of
https://github.com/Zedfrigg/ironbar.git
synced 2025-04-19 19:34:24 +02:00
Merge pull request #114 from JakeStanger/feat/common-in-custom
feat(custom): support common options in widgets
This commit is contained in:
commit
1855416db4
3 changed files with 41 additions and 17 deletions
|
@ -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 |
|
||||
|---------|-------------------------------------------------------------------|---------|-------------------------------|
|
||||
|
|
|
@ -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<String>,
|
||||
class: Option<String>,
|
||||
orientation: Option<String>,
|
||||
widgets: Option<Vec<Widget>>,
|
||||
widgets: Option<Vec<WidgetConfig>>,
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<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 {
|
||||
|
@ -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: >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<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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue