2023-01-29 22:48:42 +00:00
|
|
|
use super::ImageProvider;
|
2024-11-17 23:46:02 +00:00
|
|
|
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
|
2023-01-29 22:48:42 +00:00
|
|
|
use gtk::prelude::*;
|
|
|
|
use gtk::{Button, IconTheme, Image, Label, Orientation};
|
2024-11-17 23:46:02 +00:00
|
|
|
use std::ops::Deref;
|
2023-01-29 22:48:42 +00:00
|
|
|
|
2023-02-25 14:30:45 +00:00
|
|
|
#[cfg(any(feature = "music", feature = "workspaces", feature = "clipboard"))]
|
2023-01-29 22:48:42 +00:00
|
|
|
pub fn new_icon_button(input: &str, icon_theme: &IconTheme, size: i32) -> Button {
|
|
|
|
let button = Button::new();
|
|
|
|
|
|
|
|
if ImageProvider::is_definitely_image_input(input) {
|
|
|
|
let image = Image::new();
|
2023-07-16 18:57:00 +01:00
|
|
|
image.add_class("image");
|
|
|
|
image.add_class("icon");
|
2023-02-02 20:37:02 +00:00
|
|
|
|
2023-07-26 21:49:45 +01:00
|
|
|
match ImageProvider::parse(input, icon_theme, false, size)
|
2024-06-28 23:26:36 +01:00
|
|
|
.map(|provider| provider.load_into_image(&image))
|
2023-01-29 22:48:42 +00:00
|
|
|
{
|
2023-05-20 14:36:04 +01:00
|
|
|
Some(_) => {
|
2023-01-29 22:48:42 +00:00
|
|
|
button.set_image(Some(&image));
|
|
|
|
button.set_always_show_image(true);
|
|
|
|
}
|
2023-05-20 14:36:04 +01:00
|
|
|
None => {
|
2023-01-29 22:48:42 +00:00
|
|
|
button.set_label(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
button.set_label(input);
|
|
|
|
}
|
|
|
|
|
|
|
|
button
|
|
|
|
}
|
|
|
|
|
2025-02-04 00:19:30 +03:00
|
|
|
#[cfg(any(feature = "music", feature = "keyboard"))]
|
2024-11-17 23:46:02 +00:00
|
|
|
pub struct IconLabel {
|
|
|
|
container: gtk::Box,
|
|
|
|
label: Label,
|
|
|
|
image: Image,
|
|
|
|
|
|
|
|
icon_theme: IconTheme,
|
|
|
|
size: i32,
|
|
|
|
}
|
|
|
|
|
2025-02-04 00:19:30 +03:00
|
|
|
#[cfg(any(feature = "music", feature = "keyboard"))]
|
2024-11-17 23:46:02 +00:00
|
|
|
impl IconLabel {
|
|
|
|
pub fn new(input: &str, icon_theme: &IconTheme, size: i32) -> Self {
|
|
|
|
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
|
|
|
|
2024-11-17 23:46:02 +00:00
|
|
|
if ImageProvider::is_definitely_image_input(input) {
|
|
|
|
ImageProvider::parse(input, icon_theme, false, size)
|
|
|
|
.map(|provider| provider.load_into_image(&image));
|
2023-02-02 20:37:02 +00:00
|
|
|
|
2024-11-17 23:46:02 +00:00
|
|
|
image.show();
|
|
|
|
} else {
|
|
|
|
label.set_text(input);
|
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
container,
|
|
|
|
label,
|
|
|
|
image,
|
|
|
|
icon_theme: icon_theme.clone(),
|
|
|
|
size,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_label(&self, input: Option<&str>) {
|
|
|
|
let label = &self.label;
|
|
|
|
let image = &self.image;
|
|
|
|
|
|
|
|
if let Some(input) = input {
|
|
|
|
if ImageProvider::is_definitely_image_input(input) {
|
|
|
|
ImageProvider::parse(input, &self.icon_theme, false, self.size)
|
|
|
|
.map(|provider| provider.load_into_image(image));
|
|
|
|
|
|
|
|
label.hide();
|
|
|
|
image.show();
|
|
|
|
} else {
|
|
|
|
label.set_label_escaped(input);
|
|
|
|
|
|
|
|
image.hide();
|
|
|
|
label.show();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
label.hide();
|
|
|
|
image.hide();
|
|
|
|
}
|
2023-01-29 22:48:42 +00:00
|
|
|
}
|
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
|
|
|
}
|