1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-07-01 10:41:03 +02:00

feat(custom): support dynamic string in image source

Resolves #94.
This commit is contained in:
Jake Stanger 2023-04-10 20:04:36 +01:00
parent b770ae716c
commit 3d308ab572
No known key found for this signature in database
GPG key ID: C51FC8F9CB0BEA61
2 changed files with 26 additions and 13 deletions

View file

@ -61,10 +61,10 @@ An image or icon from disk or http.
> Type `image` > Type `image`
| Name | Type | Default | Description | | Name | Type | Default | Description |
|--------|-----------|---------|-------------------------------------------------------------| |--------|-----------|---------|---------------------------------------------------------------------------------------------|
| `src` | `image` | `null` | Image source. See [here](images) for information on images. | | `src` | `image` | `null` | Image source. See [here](images) for information on images. Embedded scripts are supported. |
| `size` | `integer` | `null` | Width/height of the image. Aspect ratio is preserved. | | `size` | `integer` | `null` | Width/height of the image. Aspect ratio is preserved. |
#### Slider #### Slider

View file

@ -1,5 +1,6 @@
use super::{CustomWidget, CustomWidgetContext}; use super::{CustomWidget, CustomWidgetContext};
use crate::build; use crate::build;
use crate::dynamic_string::DynamicString;
use crate::image::ImageProvider; use crate::image::ImageProvider;
use gtk::prelude::*; use gtk::prelude::*;
use gtk::Image; use gtk::Image;
@ -10,8 +11,13 @@ use tracing::error;
pub struct ImageWidget { pub struct ImageWidget {
name: Option<String>, name: Option<String>,
class: Option<String>, class: Option<String>,
src: Option<String>, src: String,
size: Option<i32>, #[serde(default = "default_size")]
size: i32,
}
const fn default_size() -> i32 {
32
} }
impl CustomWidget for ImageWidget { impl CustomWidget for ImageWidget {
@ -20,13 +26,20 @@ impl CustomWidget for ImageWidget {
fn into_widget(self, context: CustomWidgetContext) -> Self::Widget { fn into_widget(self, context: CustomWidgetContext) -> Self::Widget {
let gtk_image = build!(self, Self::Widget); let gtk_image = build!(self, Self::Widget);
if let Some(src) = self.src { {
let size = self.size.unwrap_or(32); let gtk_image = gtk_image.clone();
if let Err(err) = ImageProvider::parse(&src, context.icon_theme, size) let icon_theme = context.icon_theme.clone();
.and_then(|image| image.load_into_image(gtk_image.clone()))
{ DynamicString::new(&self.src, move |src| {
error!("{err:?}"); let res = ImageProvider::parse(&src, &icon_theme, self.size)
} .and_then(|image| image.load_into_image(gtk_image.clone()));
if let Err(err) = res {
error!("{err:?}");
}
Continue(true)
});
} }
gtk_image gtk_image