1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +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`
| Name | Type | Default | Description |
|--------|-----------|---------|-------------------------------------------------------------|
| `src` | `image` | `null` | Image source. See [here](images) for information on images. |
| `size` | `integer` | `null` | Width/height of the image. Aspect ratio is preserved. |
| Name | Type | Default | Description |
|--------|-----------|---------|---------------------------------------------------------------------------------------------|
| `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. |
#### Slider

View file

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