2024-11-17 23:46:02 +00:00
|
|
|
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
|
2025-05-25 15:50:21 +01:00
|
|
|
use crate::image;
|
2023-01-29 22:48:42 +00:00
|
|
|
use gtk::prelude::*;
|
2025-05-25 15:50:21 +01:00
|
|
|
use gtk::{Button, Image, Label, Orientation};
|
2024-11-17 23:46:02 +00:00
|
|
|
use std::ops::Deref;
|
2023-01-29 22:48:42 +00:00
|
|
|
|
2025-03-22 19:19:47 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2025-03-24 12:22:02 +00:00
|
|
|
#[cfg(any(
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "cairo",
|
|
|
|
feature = "clipboard",
|
2025-03-24 12:22:02 +00:00
|
|
|
feature = "clipboard",
|
|
|
|
feature = "keyboard",
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "launcher",
|
2025-03-24 12:22:02 +00:00
|
|
|
feature = "music",
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "workspaces",
|
2025-03-24 12:22:02 +00:00
|
|
|
))]
|
2025-03-22 19:19:47 +00:00
|
|
|
pub struct IconButton {
|
|
|
|
button: Button,
|
|
|
|
label: Label,
|
|
|
|
}
|
2023-01-29 22:48:42 +00:00
|
|
|
|
2025-03-24 12:22:02 +00:00
|
|
|
#[cfg(any(
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "cairo",
|
|
|
|
feature = "clipboard",
|
2025-03-24 12:22:02 +00:00
|
|
|
feature = "clipboard",
|
|
|
|
feature = "keyboard",
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "launcher",
|
2025-03-24 12:22:02 +00:00
|
|
|
feature = "music",
|
2025-04-16 18:43:14 -03:00
|
|
|
feature = "workspaces",
|
2025-03-24 12:22:02 +00:00
|
|
|
))]
|
2025-03-22 19:19:47 +00:00
|
|
|
impl IconButton {
|
2025-05-25 15:50:21 +01:00
|
|
|
pub fn new(input: &str, size: i32, image_provider: image::Provider) -> Self {
|
2025-03-22 19:19:47 +00:00
|
|
|
let button = Button::new();
|
2023-01-29 22:48:42 +00:00
|
|
|
let image = Image::new();
|
2025-03-22 19:19:47 +00:00
|
|
|
let label = Label::new(Some(input));
|
2023-02-02 20:37:02 +00:00
|
|
|
|
2025-05-25 15:50:21 +01:00
|
|
|
if image::Provider::is_explicit_input(input) {
|
2025-03-22 19:19:47 +00:00
|
|
|
image.add_class("image");
|
|
|
|
image.add_class("icon");
|
|
|
|
|
2025-05-25 15:50:21 +01:00
|
|
|
let image = image.clone();
|
|
|
|
let label = label.clone();
|
|
|
|
let button = button.clone();
|
|
|
|
|
|
|
|
let input = input.to_string(); // ew
|
|
|
|
|
|
|
|
glib::spawn_future_local(async move {
|
|
|
|
if let Ok(true) = image_provider
|
|
|
|
.load_into_image(&input, size, false, &image)
|
|
|
|
.await
|
|
|
|
{
|
2025-03-22 19:19:47 +00:00
|
|
|
button.set_image(Some(&image));
|
|
|
|
button.set_always_show_image(true);
|
2025-05-25 15:50:21 +01:00
|
|
|
} else {
|
2025-03-22 19:19:47 +00:00
|
|
|
button.set_child(Some(&label));
|
|
|
|
label.show();
|
|
|
|
}
|
2025-05-25 15:50:21 +01:00
|
|
|
});
|
2025-03-22 19:19:47 +00:00
|
|
|
} else {
|
|
|
|
button.set_child(Some(&label));
|
|
|
|
label.show();
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|
2025-03-22 19:19:47 +00:00
|
|
|
|
|
|
|
Self { button, label }
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|
|
|
|
|
2025-03-22 19:19:47 +00:00
|
|
|
pub fn label(&self) -> &Label {
|
|
|
|
&self.label
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-16 18:43:14 -03:00
|
|
|
#[cfg(any(
|
|
|
|
feature = "clipboard",
|
|
|
|
feature = "keyboard",
|
|
|
|
feature = "music",
|
|
|
|
feature = "workspaces",
|
|
|
|
feature = "cairo",
|
|
|
|
feature = "clipboard",
|
|
|
|
feature = "launcher",
|
|
|
|
))]
|
2025-03-22 19:19:47 +00:00
|
|
|
impl Deref for IconButton {
|
|
|
|
type Target = Button;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.button
|
|
|
|
}
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|
|
|
|
|
2025-03-24 12:22:02 +00:00
|
|
|
#[cfg(any(feature = "keyboard", feature = "music", feature = "workspaces"))]
|
2024-11-17 23:46:02 +00:00
|
|
|
pub struct IconLabel {
|
2025-05-25 15:50:21 +01:00
|
|
|
provider: image::Provider,
|
2024-11-17 23:46:02 +00:00
|
|
|
container: gtk::Box,
|
|
|
|
label: Label,
|
|
|
|
image: Image,
|
|
|
|
|
|
|
|
size: i32,
|
|
|
|
}
|
|
|
|
|
2025-03-24 12:22:02 +00:00
|
|
|
#[cfg(any(feature = "keyboard", feature = "music", feature = "workspaces"))]
|
2024-11-17 23:46:02 +00:00
|
|
|
impl IconLabel {
|
2025-05-25 15:50:21 +01:00
|
|
|
pub fn new(input: &str, size: i32, image_provider: &image::Provider) -> Self {
|
2024-11-17 23:46:02 +00:00
|
|
|
let container = gtk::Box::new(Orientation::Horizontal, 0);
|
|
|
|
|
|
|
|
let label = Label::builder().use_markup(true).build();
|
|
|
|
label.add_class("icon");
|
|
|
|
label.add_class("text-icon");
|
2023-01-29 22:48:42 +00:00
|
|
|
|
|
|
|
let image = Image::new();
|
2023-07-16 18:57:00 +01:00
|
|
|
image.add_class("icon");
|
|
|
|
image.add_class("image");
|
2023-02-02 20:37:02 +00:00
|
|
|
|
2023-01-29 22:48:42 +00:00
|
|
|
container.add(&image);
|
2024-11-17 23:46:02 +00:00
|
|
|
container.add(&label);
|
2023-01-29 22:48:42 +00:00
|
|
|
|
2025-05-25 15:50:21 +01:00
|
|
|
if image::Provider::is_explicit_input(input) {
|
|
|
|
let image = image.clone();
|
|
|
|
let label = label.clone();
|
|
|
|
let image_provider = image_provider.clone();
|
|
|
|
|
|
|
|
let input = input.to_string();
|
|
|
|
|
|
|
|
glib::spawn_future_local(async move {
|
|
|
|
let res = image_provider
|
|
|
|
.load_into_image(&input, size, false, &image)
|
|
|
|
.await;
|
|
|
|
if matches!(res, Ok(true)) {
|
|
|
|
image.show();
|
|
|
|
} else {
|
2025-05-25 23:27:03 +01:00
|
|
|
label.set_label_escaped(&input);
|
2025-05-25 15:50:21 +01:00
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
});
|
2024-11-17 23:46:02 +00:00
|
|
|
} else {
|
2025-05-25 23:27:03 +01:00
|
|
|
label.set_label_escaped(input);
|
2024-11-17 23:46:02 +00:00
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
2025-05-25 15:50:21 +01:00
|
|
|
provider: image_provider.clone(),
|
2024-11-17 23:46:02 +00:00
|
|
|
container,
|
|
|
|
label,
|
|
|
|
image,
|
|
|
|
size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_label(&self, input: Option<&str>) {
|
|
|
|
let label = &self.label;
|
|
|
|
let image = &self.image;
|
|
|
|
|
|
|
|
if let Some(input) = input {
|
2025-05-25 15:50:21 +01:00
|
|
|
if image::Provider::is_explicit_input(input) {
|
|
|
|
let provider = self.provider.clone();
|
|
|
|
let size = self.size;
|
|
|
|
|
|
|
|
let label = label.clone();
|
|
|
|
let image = image.clone();
|
|
|
|
let input = input.to_string();
|
|
|
|
|
|
|
|
glib::spawn_future_local(async move {
|
|
|
|
let res = provider.load_into_image(&input, size, false, &image).await;
|
|
|
|
if matches!(res, Ok(true)) {
|
|
|
|
label.hide();
|
|
|
|
image.show();
|
|
|
|
} else {
|
|
|
|
label.set_label_escaped(&input);
|
|
|
|
|
|
|
|
image.hide();
|
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
});
|
2024-11-17 23:46:02 +00:00
|
|
|
} else {
|
|
|
|
label.set_label_escaped(input);
|
|
|
|
|
|
|
|
image.hide();
|
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.hide();
|
|
|
|
image.hide();
|
|
|
|
}
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|
2025-03-22 19:19:47 +00:00
|
|
|
|
|
|
|
pub fn label(&self) -> &Label {
|
|
|
|
&self.label
|
|
|
|
}
|
2024-11-17 23:46:02 +00:00
|
|
|
}
|
|
|
|
|
2025-04-16 18:43:14 -03:00
|
|
|
#[cfg(any(feature = "keyboard", feature = "music", feature = "workspaces"))]
|
2024-11-17 23:46:02 +00:00
|
|
|
impl Deref for IconLabel {
|
|
|
|
type Target = gtk::Box;
|
2023-01-29 22:48:42 +00:00
|
|
|
|
2024-11-17 23:46:02 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.container
|
|
|
|
}
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|