2023-04-07 20:22:31 +01:00
|
|
|
mod r#box;
|
|
|
|
mod button;
|
|
|
|
mod image;
|
|
|
|
mod label;
|
2023-04-10 00:17:52 +01:00
|
|
|
mod progress;
|
2023-04-09 22:42:35 +01:00
|
|
|
mod slider;
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2025-02-21 16:35:54 +00:00
|
|
|
use self::r#box::BoxWidget;
|
2023-04-07 20:22:31 +01:00
|
|
|
use self::image::ImageWidget;
|
|
|
|
use self::label::LabelWidget;
|
2023-04-09 22:42:35 +01:00
|
|
|
use self::slider::SliderWidget;
|
2025-05-18 15:17:09 +01:00
|
|
|
use crate::channels::AsyncSenderExt;
|
2024-03-14 22:35:33 +00:00
|
|
|
use crate::config::{CommonConfig, ModuleConfig};
|
2023-04-07 20:22:31 +01:00
|
|
|
use crate::modules::custom::button::ButtonWidget;
|
2023-04-10 00:17:52 +01:00
|
|
|
use crate::modules::custom::progress::ProgressWidget;
|
2023-04-22 13:34:39 +01:00
|
|
|
use crate::modules::{
|
2025-02-21 16:35:54 +00:00
|
|
|
AnyModuleFactory, BarModuleFactory, Module, ModuleInfo, ModuleParts, ModulePopup,
|
|
|
|
ModuleUpdateEvent, PopupButton, PopupModuleFactory, WidgetContext, wrap_widget,
|
2023-04-22 13:34:39 +01:00
|
|
|
};
|
2023-04-07 20:22:31 +01:00
|
|
|
use crate::script::Script;
|
2025-05-18 15:17:09 +01:00
|
|
|
use crate::{module_impl, spawn};
|
2024-04-05 13:28:47 -04:00
|
|
|
use color_eyre::Result;
|
2023-04-07 20:22:31 +01:00
|
|
|
use gtk::prelude::*;
|
2023-07-16 18:57:00 +01:00
|
|
|
use gtk::{Button, IconTheme, Orientation};
|
2023-04-07 20:22:31 +01:00
|
|
|
use serde::Deserialize;
|
2023-07-16 18:57:00 +01:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2023-12-17 23:51:43 +00:00
|
|
|
use tokio::sync::{broadcast, mpsc};
|
2023-04-07 20:22:31 +01:00
|
|
|
use tracing::{debug, error};
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-04-07 20:22:31 +01:00
|
|
|
pub struct CustomModule {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// Modules and widgets to add to the bar container.
|
|
|
|
///
|
|
|
|
/// **Default**: `[]`
|
2023-04-22 13:34:39 +01:00
|
|
|
bar: Vec<WidgetConfig>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Modules and widgets to add to the popup container.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-22 13:34:39 +01:00
|
|
|
popup: Option<Vec<WidgetConfig>>,
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2024-05-19 15:16:01 +01:00
|
|
|
/// See [common options](module-level-options#common-options).
|
2023-04-07 20:22:31 +01:00
|
|
|
#[serde(flatten)]
|
|
|
|
pub common: Option<CommonConfig>,
|
|
|
|
}
|
|
|
|
|
2023-04-22 13:34:39 +01:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-04-22 13:34:39 +01:00
|
|
|
pub struct WidgetConfig {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// One of a custom module native Ironbar module.
|
2023-04-22 13:34:39 +01:00
|
|
|
#[serde(flatten)]
|
2024-03-14 22:35:33 +00:00
|
|
|
widget: WidgetOrModule,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// See [common options](module-level-options#common-options).
|
2023-04-22 13:34:39 +01:00
|
|
|
#[serde(flatten)]
|
|
|
|
common: CommonConfig,
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:35:33 +00:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
#[serde(untagged)]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2024-03-14 22:35:33 +00:00
|
|
|
pub enum WidgetOrModule {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A custom-module specific basic widget
|
2024-03-14 22:35:33 +00:00
|
|
|
Widget(Widget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A native Ironbar module, such as `clock` or `focused`.
|
|
|
|
/// All widgets are supported, including their popups.
|
2024-03-14 22:35:33 +00:00
|
|
|
Module(ModuleConfig),
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
#[serde(tag = "type", rename_all = "snake_case")]
|
2024-05-10 22:40:00 +01:00
|
|
|
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
|
2023-04-07 20:22:31 +01:00
|
|
|
pub enum Widget {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A container to place nested widgets inside.
|
2023-04-07 20:22:31 +01:00
|
|
|
Box(BoxWidget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A text label. Pango markup is supported.
|
2023-04-07 20:22:31 +01:00
|
|
|
Label(LabelWidget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A clickable button, which can run a command when clicked.
|
2023-04-07 20:22:31 +01:00
|
|
|
Button(ButtonWidget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// An image or icon from disk or http.
|
2023-04-09 22:42:35 +01:00
|
|
|
Image(ImageWidget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A draggable slider.
|
2023-04-09 22:42:35 +01:00
|
|
|
Slider(SliderWidget),
|
2024-05-19 15:16:01 +01:00
|
|
|
/// A progress bar.
|
2023-04-10 00:17:52 +01:00
|
|
|
Progress(ProgressWidget),
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
#[derive(Clone)]
|
2023-04-07 20:22:31 +01:00
|
|
|
struct CustomWidgetContext<'a> {
|
2024-03-14 22:35:33 +00:00
|
|
|
info: &'a ModuleInfo<'a>,
|
2023-12-17 23:51:43 +00:00
|
|
|
tx: &'a mpsc::Sender<ExecEvent>,
|
2023-04-07 20:22:31 +01:00
|
|
|
bar_orientation: Orientation,
|
2025-03-22 19:19:47 +00:00
|
|
|
is_popup: bool,
|
2023-04-07 20:22:31 +01:00
|
|
|
icon_theme: &'a IconTheme,
|
2023-07-16 18:57:00 +01:00
|
|
|
popup_buttons: Rc<RefCell<Vec<Button>>>,
|
2024-03-14 22:35:33 +00:00
|
|
|
module_factory: AnyModuleFactory,
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
trait CustomWidget {
|
|
|
|
type Widget;
|
|
|
|
|
|
|
|
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget;
|
|
|
|
}
|
|
|
|
|
2023-04-10 13:51:07 +01:00
|
|
|
/// Creates a new widget of type `ty`,
|
|
|
|
/// setting its name and class based on
|
|
|
|
/// the values available on `self`.
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! build {
|
|
|
|
($self:ident, $ty:ty) => {{
|
|
|
|
let mut builder = <$ty>::builder();
|
|
|
|
|
|
|
|
if let Some(name) = &$self.name {
|
|
|
|
builder = builder.name(name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let widget = builder.build();
|
|
|
|
|
|
|
|
if let Some(class) = &$self.class {
|
|
|
|
widget.style_context().add_class(class);
|
|
|
|
}
|
|
|
|
|
|
|
|
widget
|
|
|
|
}};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the widget length,
|
|
|
|
/// using either a width or height request
|
|
|
|
/// based on the bar's orientation.
|
|
|
|
pub fn set_length<W: WidgetExt>(widget: &W, length: i32, bar_orientation: Orientation) {
|
|
|
|
match bar_orientation {
|
|
|
|
Orientation::Horizontal => widget.set_width_request(length),
|
|
|
|
Orientation::Vertical => widget.set_height_request(length),
|
|
|
|
_ => {}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-14 22:35:33 +00:00
|
|
|
impl WidgetOrModule {
|
|
|
|
fn add_to(self, parent: >k::Box, context: &CustomWidgetContext, common: CommonConfig) {
|
|
|
|
match self {
|
|
|
|
WidgetOrModule::Widget(widget) => widget.add_to(parent, context, common),
|
|
|
|
WidgetOrModule::Module(config) => {
|
|
|
|
if let Err(err) = config.create(&context.module_factory, parent, context.info) {
|
|
|
|
error!("{err:?}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
impl Widget {
|
|
|
|
/// Creates this widget and adds it to the parent container
|
2023-07-16 18:57:00 +01:00
|
|
|
fn add_to(self, parent: >k::Box, context: &CustomWidgetContext, common: CommonConfig) {
|
2023-04-22 13:34:39 +01:00
|
|
|
macro_rules! create {
|
|
|
|
($widget:expr) => {
|
2023-04-22 14:49:15 +01:00
|
|
|
wrap_widget(
|
2023-07-16 18:57:00 +01:00
|
|
|
&$widget.into_widget(context.clone()),
|
2023-04-22 14:49:15 +01:00
|
|
|
common,
|
|
|
|
context.bar_orientation,
|
|
|
|
)
|
2023-04-22 13:34:39 +01:00
|
|
|
};
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
2023-04-22 13:34:39 +01:00
|
|
|
|
|
|
|
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);
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ExecEvent {
|
|
|
|
cmd: String,
|
2023-04-09 22:42:35 +01:00
|
|
|
args: Option<Vec<String>>,
|
2023-07-16 18:57:00 +01:00
|
|
|
id: usize,
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<gtk::Box> for CustomModule {
|
|
|
|
type SendMessage = ();
|
|
|
|
type ReceiveMessage = ExecEvent;
|
|
|
|
|
2024-03-14 22:35:33 +00:00
|
|
|
module_impl!("custom");
|
2023-04-07 20:22:31 +01:00
|
|
|
|
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
2024-01-07 23:42:34 +00:00
|
|
|
context: &WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2023-12-17 23:51:43 +00:00
|
|
|
mut rx: mpsc::Receiver<Self::ReceiveMessage>,
|
2023-04-07 20:22:31 +01:00
|
|
|
) -> Result<()> {
|
2024-01-07 23:42:34 +00:00
|
|
|
let tx = context.tx.clone();
|
2023-04-07 20:22:31 +01:00
|
|
|
spawn(async move {
|
|
|
|
while let Some(event) = rx.recv().await {
|
|
|
|
if event.cmd.starts_with('!') {
|
|
|
|
let script = Script::from(&event.cmd[1..]);
|
|
|
|
|
|
|
|
debug!("executing command: '{}'", script.cmd);
|
2023-04-09 22:42:35 +01:00
|
|
|
|
2023-04-10 20:04:59 +01:00
|
|
|
let args = event.args.unwrap_or_default();
|
2025-03-24 12:01:59 +00:00
|
|
|
script.run_as_oneshot(Some(&args));
|
2023-04-07 20:22:31 +01:00
|
|
|
} else if event.cmd == "popup:toggle" {
|
2025-05-18 15:17:09 +01:00
|
|
|
tx.send_expect(ModuleUpdateEvent::TogglePopup(event.id))
|
|
|
|
.await;
|
2023-04-07 20:22:31 +01:00
|
|
|
} else if event.cmd == "popup:open" {
|
2025-05-18 15:17:09 +01:00
|
|
|
tx.send_expect(ModuleUpdateEvent::OpenPopup(event.id)).await;
|
2023-04-07 20:22:31 +01:00
|
|
|
} else if event.cmd == "popup:close" {
|
2025-05-18 15:17:09 +01:00
|
|
|
tx.send_expect(ModuleUpdateEvent::ClosePopup).await;
|
2023-04-07 20:22:31 +01:00
|
|
|
} else {
|
|
|
|
error!("Received invalid command: '{}'", event.cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
2024-03-14 22:35:33 +00:00
|
|
|
mut context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2023-04-07 20:22:31 +01:00
|
|
|
info: &ModuleInfo,
|
2023-07-16 18:57:00 +01:00
|
|
|
) -> Result<ModuleParts<gtk::Box>> {
|
2024-02-18 14:54:17 +00:00
|
|
|
let orientation = info.bar_position.orientation();
|
2023-04-07 20:22:31 +01:00
|
|
|
let container = gtk::Box::builder().orientation(orientation).build();
|
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
let popup_buttons = Rc::new(RefCell::new(Vec::new()));
|
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
let custom_context = CustomWidgetContext {
|
2024-03-14 22:35:33 +00:00
|
|
|
info,
|
2023-04-07 20:22:31 +01:00
|
|
|
tx: &context.controller_tx,
|
|
|
|
bar_orientation: orientation,
|
2025-03-22 19:19:47 +00:00
|
|
|
is_popup: false,
|
2023-04-07 20:22:31 +01:00
|
|
|
icon_theme: info.icon_theme,
|
2023-07-16 18:57:00 +01:00
|
|
|
popup_buttons: popup_buttons.clone(),
|
2024-03-14 22:35:33 +00:00
|
|
|
module_factory: BarModuleFactory::new(context.ironbar.clone(), context.popup.clone())
|
|
|
|
.into(),
|
2023-04-07 20:22:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.bar.clone().into_iter().for_each(|widget| {
|
2023-04-22 13:34:39 +01:00
|
|
|
widget
|
|
|
|
.widget
|
2023-07-16 18:57:00 +01:00
|
|
|
.add_to(&container, &custom_context, widget.common);
|
2023-04-07 20:22:31 +01:00
|
|
|
});
|
|
|
|
|
2024-03-14 22:35:33 +00:00
|
|
|
for button in popup_buttons.borrow().iter() {
|
|
|
|
button.ensure_popup_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
context.button_id = popup_buttons
|
|
|
|
.borrow()
|
|
|
|
.first()
|
2024-04-01 16:33:38 +01:00
|
|
|
.map_or(usize::MAX, PopupButton::popup_id);
|
2024-03-14 22:35:33 +00:00
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
let popup = self
|
2024-03-14 22:35:33 +00:00
|
|
|
.into_popup(
|
|
|
|
context.controller_tx.clone(),
|
|
|
|
context.subscribe(),
|
|
|
|
context,
|
|
|
|
info,
|
|
|
|
)
|
2023-07-16 18:57:00 +01:00
|
|
|
.into_popup_parts_owned(popup_buttons.take());
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2023-07-16 18:57:00 +01:00
|
|
|
Ok(ModuleParts {
|
2023-04-07 20:22:31 +01:00
|
|
|
widget: container,
|
|
|
|
popup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_popup(
|
|
|
|
self,
|
2023-12-17 23:51:43 +00:00
|
|
|
tx: mpsc::Sender<Self::ReceiveMessage>,
|
|
|
|
_rx: broadcast::Receiver<Self::SendMessage>,
|
2024-03-14 22:35:33 +00:00
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
2023-04-07 20:22:31 +01:00
|
|
|
info: &ModuleInfo,
|
|
|
|
) -> Option<gtk::Box>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
2023-05-06 00:40:06 +01:00
|
|
|
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
2023-04-07 20:22:31 +01:00
|
|
|
|
|
|
|
if let Some(popup) = self.popup {
|
|
|
|
let custom_context = CustomWidgetContext {
|
2024-03-14 22:35:33 +00:00
|
|
|
info,
|
2023-04-07 20:22:31 +01:00
|
|
|
tx: &tx,
|
2025-03-22 19:19:47 +00:00
|
|
|
bar_orientation: Orientation::Horizontal,
|
|
|
|
is_popup: true,
|
2023-04-07 20:22:31 +01:00
|
|
|
icon_theme: info.icon_theme,
|
2023-07-16 18:57:00 +01:00
|
|
|
popup_buttons: Rc::new(RefCell::new(vec![])),
|
2024-03-14 22:35:33 +00:00
|
|
|
module_factory: PopupModuleFactory::new(
|
|
|
|
context.ironbar,
|
|
|
|
context.popup,
|
|
|
|
context.button_id,
|
|
|
|
)
|
|
|
|
.into(),
|
2023-04-07 20:22:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
for widget in popup {
|
2023-04-22 13:34:39 +01:00
|
|
|
widget
|
|
|
|
.widget
|
2023-07-16 18:57:00 +01:00
|
|
|
.add_to(&container, &custom_context, widget.common);
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
container.show_all();
|
|
|
|
|
|
|
|
Some(container)
|
|
|
|
}
|
|
|
|
}
|