1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-08-17 23:01:04 +02:00
ironbar/src/image/gtk.rs

138 lines
3.5 KiB
Rust
Raw Normal View History

use super::ImageProvider;
use crate::gtk_helpers::{IronbarGtkExt, IronbarLabelExt};
use gtk::prelude::*;
use gtk::{Button, IconTheme, Image, Label, Orientation};
use std::ops::Deref;
2023-02-25 14:30:45 +00:00
#[cfg(any(feature = "music", feature = "workspaces", feature = "clipboard"))]
#[derive(Debug, Clone)]
pub struct IconButton {
button: Button,
label: Label,
}
#[cfg(any(feature = "music", feature = "workspaces", feature = "clipboard"))]
impl IconButton {
pub fn new(input: &str, icon_theme: &IconTheme, size: i32) -> Self {
let button = Button::new();
let image = Image::new();
let label = Label::new(Some(input));
2023-02-02 20:37:02 +00:00
if ImageProvider::is_definitely_image_input(input) {
image.add_class("image");
image.add_class("icon");
match ImageProvider::parse(input, icon_theme, false, size)
.map(|provider| provider.load_into_image(&image))
{
Some(_) => {
button.set_image(Some(&image));
button.set_always_show_image(true);
}
None => {
button.set_child(Some(&label));
label.show();
}
}
} else {
button.set_child(Some(&label));
label.show();
}
Self { button, label }
}
pub fn label(&self) -> &Label {
&self.label
}
}
impl Deref for IconButton {
type Target = Button;
fn deref(&self) -> &Self::Target {
&self.button
}
}
#[cfg(any(feature = "music", feature = "keyboard"))]
pub struct IconLabel {
container: gtk::Box,
label: Label,
image: Image,
icon_theme: IconTheme,
size: i32,
}
#[cfg(any(feature = "music", feature = "keyboard"))]
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");
let image = Image::new();
image.add_class("icon");
image.add_class("image");
2023-02-02 20:37:02 +00:00
container.add(&image);
container.add(&label);
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
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();
}
}
pub fn label(&self) -> &Label {
&self.label
}
}
impl Deref for IconLabel {
type Target = gtk::Box;
fn deref(&self) -> &Self::Target {
&self.container
}
}