2023-04-07 20:22:31 +01:00
|
|
|
use gtk::prelude::*;
|
2024-04-01 15:42:26 +01:00
|
|
|
use gtk::{Button, Label, Orientation};
|
2023-04-07 20:22:31 +01:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
2024-04-04 16:12:45 -04:00
|
|
|
use crate::config::ModuleOrientation;
|
2023-07-16 18:57:00 +01:00
|
|
|
use crate::dynamic_value::dynamic_string;
|
|
|
|
use crate::modules::PopupButton;
|
|
|
|
use crate::{build, try_send};
|
|
|
|
|
2024-04-01 15:42:26 +01:00
|
|
|
use super::{CustomWidget, CustomWidgetContext, ExecEvent, WidgetConfig};
|
2023-07-16 18:57:00 +01:00
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
#[derive(Debug, Deserialize, Clone)]
|
|
|
|
pub struct ButtonWidget {
|
2024-05-19 15:16:01 +01:00
|
|
|
/// Widget name.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
name: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Widget class name.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
class: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Widget text label. Pango markup and embedded scripts are supported.
|
|
|
|
///
|
|
|
|
/// This is a shorthand for adding a label widget to the button.
|
|
|
|
/// Ignored if `widgets` is set.
|
|
|
|
///
|
|
|
|
/// This is a [Dynamic String](dynamic-values#dynamic-string).
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
label: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Command to execute. More on this [below](#commands).
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
2023-04-07 20:22:31 +01:00
|
|
|
on_click: Option<String>,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Orientation of the button.
|
|
|
|
///
|
|
|
|
/// **Valid options**: `horizontal`, `vertical`, `h`, `v`
|
|
|
|
/// <br />
|
|
|
|
/// **Default**: `horizontal`
|
2024-04-04 16:12:45 -04:00
|
|
|
#[serde(default)]
|
|
|
|
orientation: ModuleOrientation,
|
2024-05-19 15:16:01 +01:00
|
|
|
|
|
|
|
/// Modules and widgets to add to this box.
|
|
|
|
///
|
|
|
|
/// **Default**: `null`
|
|
|
|
widgets: Option<Vec<WidgetConfig>>,
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CustomWidget for ButtonWidget {
|
|
|
|
type Widget = Button;
|
|
|
|
|
|
|
|
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget {
|
2023-04-10 13:51:07 +01:00
|
|
|
let button = build!(self, Self::Widget);
|
2023-07-16 18:57:00 +01:00
|
|
|
context.popup_buttons.borrow_mut().push(button.clone());
|
2023-04-07 20:22:31 +01:00
|
|
|
|
2024-04-01 15:42:26 +01:00
|
|
|
if let Some(widgets) = self.widgets {
|
|
|
|
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
|
|
|
|
|
|
|
for widget in widgets {
|
|
|
|
widget.widget.add_to(&container, &context, widget.common);
|
|
|
|
}
|
|
|
|
|
|
|
|
button.add(&container);
|
|
|
|
} else if let Some(text) = self.label {
|
2023-04-07 20:22:31 +01:00
|
|
|
let label = Label::new(None);
|
|
|
|
label.set_use_markup(true);
|
2024-04-04 16:12:45 -04:00
|
|
|
|
2024-04-05 13:28:47 -04:00
|
|
|
label.set_angle(self.orientation.to_angle());
|
2024-04-04 16:12:45 -04:00
|
|
|
|
2023-04-07 20:22:31 +01:00
|
|
|
button.add(&label);
|
|
|
|
|
2023-06-22 23:07:40 +01:00
|
|
|
dynamic_string(&text, move |string| {
|
2023-04-10 13:49:09 +01:00
|
|
|
label.set_markup(&string);
|
|
|
|
});
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(exec) = self.on_click {
|
|
|
|
let tx = context.tx.clone();
|
|
|
|
|
|
|
|
button.connect_clicked(move |button| {
|
|
|
|
try_send!(
|
|
|
|
tx,
|
|
|
|
ExecEvent {
|
|
|
|
cmd: exec.clone(),
|
2023-04-09 22:42:35 +01:00
|
|
|
args: None,
|
2023-08-01 21:29:00 +01:00
|
|
|
id: button.try_popup_id().unwrap_or(usize::MAX), // may not be a popup button
|
2023-04-07 20:22:31 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
button
|
|
|
|
}
|
|
|
|
}
|