2022-11-28 22:28:13 +00:00
|
|
|
use crate::config::CommonConfig;
|
2022-11-30 22:49:49 +00:00
|
|
|
use crate::dynamic_string::DynamicString;
|
2023-01-29 17:47:01 +00:00
|
|
|
use crate::image::ImageProvider;
|
2022-10-16 22:16:48 +01:00
|
|
|
use crate::modules::{Module, ModuleInfo, ModuleUpdateEvent, ModuleWidget, WidgetContext};
|
|
|
|
use crate::popup::{ButtonGeometry, Popup};
|
2022-11-28 21:55:08 +00:00
|
|
|
use crate::script::Script;
|
2022-12-11 22:45:52 +00:00
|
|
|
use crate::{send_async, try_send};
|
2022-11-28 22:28:13 +00:00
|
|
|
use color_eyre::{Report, Result};
|
|
|
|
use gtk::prelude::*;
|
2023-01-29 17:47:01 +00:00
|
|
|
use gtk::{Button, IconTheme, Label, Orientation};
|
2022-10-16 22:16:48 +01:00
|
|
|
use serde::Deserialize;
|
|
|
|
use tokio::spawn;
|
|
|
|
use tokio::sync::mpsc::{Receiver, Sender};
|
|
|
|
use tracing::{debug, error};
|
|
|
|
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct CustomModule {
|
|
|
|
/// Container class name
|
|
|
|
class: Option<String>,
|
|
|
|
/// Widgets to add to the bar container
|
|
|
|
bar: Vec<Widget>,
|
|
|
|
/// Widgets to add to the popup container
|
|
|
|
popup: Option<Vec<Widget>>,
|
2022-11-28 21:55:08 +00:00
|
|
|
|
|
|
|
#[serde(flatten)]
|
2022-12-04 23:23:22 +00:00
|
|
|
pub common: Option<CommonConfig>,
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Attempts to parse an `Orientation` from `String`
|
2022-11-05 17:32:42 +00:00
|
|
|
fn try_get_orientation(orientation: &str) -> Result<Orientation> {
|
2022-10-16 22:16:48 +01:00
|
|
|
match orientation.to_lowercase().as_str() {
|
|
|
|
"horizontal" | "h" => Ok(Orientation::Horizontal),
|
|
|
|
"vertical" | "v" => Ok(Orientation::Vertical),
|
|
|
|
_ => Err(Report::msg("Invalid orientation string in config")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Widget attributes
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct Widget {
|
|
|
|
/// Type of GTK widget to add
|
|
|
|
#[serde(rename = "type")]
|
|
|
|
widget_type: WidgetType,
|
|
|
|
widgets: Option<Vec<Widget>>,
|
|
|
|
label: Option<String>,
|
|
|
|
name: Option<String>,
|
|
|
|
class: Option<String>,
|
2022-11-28 22:26:14 +00:00
|
|
|
on_click: Option<String>,
|
2022-10-16 22:16:48 +01:00
|
|
|
orientation: Option<String>,
|
2023-01-29 17:47:01 +00:00
|
|
|
src: Option<String>,
|
|
|
|
size: Option<i32>,
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Supported GTK widget types
|
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
2022-11-28 22:27:31 +00:00
|
|
|
#[serde(rename_all = "snake_case")]
|
2022-10-16 22:16:48 +01:00
|
|
|
pub enum WidgetType {
|
|
|
|
Box,
|
|
|
|
Label,
|
|
|
|
Button,
|
2023-01-29 17:47:01 +00:00
|
|
|
Image,
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Widget {
|
|
|
|
/// Creates this widget and adds it to the parent container
|
2023-01-29 18:38:57 +00:00
|
|
|
fn add_to(
|
|
|
|
self,
|
|
|
|
parent: >k::Box,
|
|
|
|
tx: Sender<ExecEvent>,
|
|
|
|
bar_orientation: Orientation,
|
|
|
|
icon_theme: &IconTheme,
|
|
|
|
) {
|
2022-10-16 22:16:48 +01:00
|
|
|
match self.widget_type {
|
2023-01-29 18:38:57 +00:00
|
|
|
WidgetType::Box => parent.add(&self.into_box(&tx, bar_orientation, icon_theme)),
|
2022-11-30 22:27:56 +00:00
|
|
|
WidgetType::Label => parent.add(&self.into_label()),
|
2022-10-16 22:16:48 +01:00
|
|
|
WidgetType::Button => parent.add(&self.into_button(tx, bar_orientation)),
|
2023-01-29 18:38:57 +00:00
|
|
|
WidgetType::Image => parent.add(&self.into_image(icon_theme)),
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a `gtk::Box` from this widget
|
2023-01-29 18:38:57 +00:00
|
|
|
fn into_box(
|
|
|
|
self,
|
|
|
|
tx: &Sender<ExecEvent>,
|
|
|
|
bar_orientation: Orientation,
|
|
|
|
icon_theme: &IconTheme,
|
|
|
|
) -> gtk::Box {
|
2022-10-16 22:16:48 +01:00
|
|
|
let mut builder = gtk::Box::builder();
|
|
|
|
|
|
|
|
if let Some(name) = self.name {
|
|
|
|
builder = builder.name(&name);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(orientation) = self.orientation {
|
|
|
|
builder = builder
|
2022-11-05 17:32:42 +00:00
|
|
|
.orientation(try_get_orientation(&orientation).unwrap_or(Orientation::Horizontal));
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let container = builder.build();
|
|
|
|
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
container.style_context().add_class(&class);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(widgets) = self.widgets {
|
2023-01-29 18:38:57 +00:00
|
|
|
widgets.into_iter().for_each(|widget| {
|
|
|
|
widget.add_to(&container, tx.clone(), bar_orientation, icon_theme)
|
|
|
|
});
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
container
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a `gtk::Label` from this widget
|
2022-11-30 22:27:56 +00:00
|
|
|
fn into_label(self) -> Label {
|
2022-10-16 22:16:48 +01:00
|
|
|
let mut builder = Label::builder().use_markup(true);
|
|
|
|
|
|
|
|
if let Some(name) = self.name {
|
|
|
|
builder = builder.name(&name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let label = builder.build();
|
|
|
|
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
label.style_context().add_class(&class);
|
|
|
|
}
|
|
|
|
|
2022-11-28 22:27:31 +00:00
|
|
|
let text = self.label.map_or_else(String::new, |text| text);
|
|
|
|
|
2022-11-30 22:27:56 +00:00
|
|
|
{
|
|
|
|
let label = label.clone();
|
|
|
|
DynamicString::new(&text, move |string| {
|
|
|
|
label.set_label(&string);
|
|
|
|
Continue(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
label
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a `gtk::Button` from this widget
|
|
|
|
fn into_button(self, tx: Sender<ExecEvent>, bar_orientation: Orientation) -> Button {
|
|
|
|
let mut builder = Button::builder();
|
|
|
|
|
|
|
|
if let Some(name) = self.name {
|
|
|
|
builder = builder.name(&name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let button = builder.build();
|
|
|
|
|
|
|
|
if let Some(text) = self.label {
|
|
|
|
let label = Label::new(None);
|
|
|
|
label.set_use_markup(true);
|
|
|
|
label.set_markup(&text);
|
|
|
|
button.add(&label);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
button.style_context().add_class(&class);
|
|
|
|
}
|
|
|
|
|
2022-11-28 22:26:14 +00:00
|
|
|
if let Some(exec) = self.on_click {
|
2022-10-16 22:16:48 +01:00
|
|
|
button.connect_clicked(move |button| {
|
2022-12-11 22:45:52 +00:00
|
|
|
try_send!(
|
|
|
|
tx,
|
|
|
|
ExecEvent {
|
|
|
|
cmd: exec.clone(),
|
|
|
|
geometry: Popup::button_pos(button, bar_orientation),
|
|
|
|
}
|
|
|
|
);
|
2022-10-16 22:16:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
button
|
|
|
|
}
|
2023-01-29 17:47:01 +00:00
|
|
|
|
2023-01-29 18:38:57 +00:00
|
|
|
fn into_image(self, icon_theme: &IconTheme) -> gtk::Image {
|
2023-01-29 17:47:01 +00:00
|
|
|
let mut builder = gtk::Image::builder();
|
|
|
|
|
|
|
|
if let Some(name) = self.name {
|
|
|
|
builder = builder.name(&name);
|
|
|
|
}
|
|
|
|
|
|
|
|
let gtk_image = builder.build();
|
|
|
|
|
|
|
|
if let Some(src) = self.src {
|
|
|
|
let size = self.size.unwrap_or(32);
|
2023-01-29 18:38:57 +00:00
|
|
|
if let Err(err) = ImageProvider::parse(src, icon_theme, size)
|
2023-01-29 17:47:01 +00:00
|
|
|
.and_then(|image| image.load_into_image(gtk_image.clone()))
|
|
|
|
{
|
|
|
|
error!("{err:?}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
gtk_image.style_context().add_class(&class);
|
|
|
|
}
|
|
|
|
|
|
|
|
gtk_image
|
|
|
|
}
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct ExecEvent {
|
|
|
|
cmd: String,
|
|
|
|
geometry: ButtonGeometry,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Module<gtk::Box> for CustomModule {
|
|
|
|
type SendMessage = ();
|
|
|
|
type ReceiveMessage = ExecEvent;
|
|
|
|
|
2022-12-04 23:23:22 +00:00
|
|
|
fn name() -> &'static str {
|
|
|
|
"custom"
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:16:48 +01:00
|
|
|
fn spawn_controller(
|
|
|
|
&self,
|
|
|
|
_info: &ModuleInfo,
|
|
|
|
tx: Sender<ModuleUpdateEvent<Self::SendMessage>>,
|
|
|
|
mut rx: Receiver<Self::ReceiveMessage>,
|
|
|
|
) -> Result<()> {
|
|
|
|
spawn(async move {
|
|
|
|
while let Some(event) = rx.recv().await {
|
|
|
|
if event.cmd.starts_with('!') {
|
2022-11-28 21:55:08 +00:00
|
|
|
let script = Script::from(&event.cmd[1..]);
|
|
|
|
|
|
|
|
debug!("executing command: '{}'", script.cmd);
|
|
|
|
// TODO: Migrate to use script.run
|
|
|
|
if let Err(err) = script.get_output().await {
|
2022-10-16 22:16:48 +01:00
|
|
|
error!("{err:?}");
|
|
|
|
}
|
|
|
|
} else if event.cmd == "popup:toggle" {
|
2022-12-11 22:45:52 +00:00
|
|
|
send_async!(tx, ModuleUpdateEvent::TogglePopup(event.geometry));
|
2022-10-16 22:16:48 +01:00
|
|
|
} else if event.cmd == "popup:open" {
|
2022-12-11 22:45:52 +00:00
|
|
|
send_async!(tx, ModuleUpdateEvent::OpenPopup(event.geometry));
|
2022-10-16 22:16:48 +01:00
|
|
|
} else if event.cmd == "popup:close" {
|
2022-12-11 22:45:52 +00:00
|
|
|
send_async!(tx, ModuleUpdateEvent::ClosePopup);
|
2022-10-16 22:16:48 +01:00
|
|
|
} else {
|
|
|
|
error!("Received invalid command: '{}'", event.cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_widget(
|
|
|
|
self,
|
|
|
|
context: WidgetContext<Self::SendMessage, Self::ReceiveMessage>,
|
|
|
|
info: &ModuleInfo,
|
|
|
|
) -> Result<ModuleWidget<gtk::Box>> {
|
|
|
|
let orientation = info.bar_position.get_orientation();
|
|
|
|
let container = gtk::Box::builder().orientation(orientation).build();
|
|
|
|
|
|
|
|
if let Some(ref class) = self.class {
|
2022-11-05 17:32:42 +00:00
|
|
|
container.style_context().add_class(class);
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
self.bar.clone().into_iter().for_each(|widget| {
|
2023-01-29 18:38:57 +00:00
|
|
|
widget.add_to(
|
|
|
|
&container,
|
|
|
|
context.controller_tx.clone(),
|
|
|
|
orientation,
|
|
|
|
info.icon_theme,
|
|
|
|
);
|
2022-10-16 22:16:48 +01:00
|
|
|
});
|
|
|
|
|
2023-01-29 18:38:57 +00:00
|
|
|
let popup = self.into_popup(context.controller_tx, context.popup_rx, info);
|
2022-10-16 22:16:48 +01:00
|
|
|
|
|
|
|
Ok(ModuleWidget {
|
|
|
|
widget: container,
|
|
|
|
popup,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn into_popup(
|
|
|
|
self,
|
|
|
|
tx: Sender<Self::ReceiveMessage>,
|
|
|
|
_rx: glib::Receiver<Self::SendMessage>,
|
2023-01-29 18:38:57 +00:00
|
|
|
info: &ModuleInfo,
|
2022-10-16 22:16:48 +01:00
|
|
|
) -> Option<gtk::Box>
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
{
|
|
|
|
let container = gtk::Box::builder().name("popup-custom").build();
|
|
|
|
|
|
|
|
if let Some(class) = self.class {
|
|
|
|
container
|
|
|
|
.style_context()
|
2022-11-05 17:32:42 +00:00
|
|
|
.add_class(format!("popup-{class}").as_str());
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(popup) = self.popup {
|
2023-01-29 18:38:57 +00:00
|
|
|
popup.into_iter().for_each(|widget| {
|
|
|
|
widget.add_to(
|
|
|
|
&container,
|
|
|
|
tx.clone(),
|
|
|
|
Orientation::Horizontal,
|
|
|
|
info.icon_theme,
|
|
|
|
)
|
|
|
|
});
|
2022-10-16 22:16:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Some(container)
|
|
|
|
}
|
|
|
|
}
|