feat: remove icon widget
This commit is contained in:
@@ -17,9 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
|
||||
- `text` and `show_text` property to progressbar widget.
|
||||
- Use built-in GTK image rendering feature on image widget.
|
||||
|
||||
### Changed
|
||||
|
||||
- `image_width` and `image_height` property of icon to `icon_width` and `icon_height`.
|
||||
|
||||
### Fixed
|
||||
|
||||
@@ -27,7 +24,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/).
|
||||
|
||||
### Removed
|
||||
|
||||
- `image_height` and `image_width` from image widget.
|
||||
- icon widget.
|
||||
|
||||
|
||||
## [0.3.1] - 2025-11-01
|
||||
|
||||
|
||||
@@ -1500,6 +1500,7 @@ pub(super) fn build_image(
|
||||
gtk4::ContentFit::Fill
|
||||
});
|
||||
widget.set_can_shrink(true);
|
||||
widget.set_can_grow(true);
|
||||
|
||||
if path.ends_with(".gif") {
|
||||
let pixbuf_animation =
|
||||
@@ -1556,105 +1557,6 @@ pub(super) fn build_image(
|
||||
Ok(gtk_widget)
|
||||
}
|
||||
|
||||
pub(super) fn build_icon(props: &Map, widget_registry: &mut WidgetRegistry) -> Result<gtk4::Image> {
|
||||
let gtk_widget = gtk4::Image::new();
|
||||
|
||||
let apply_props = |props: &Map, widget: >k4::Image| -> Result<()> {
|
||||
let path = get_string_prop(&props, "path", None)?;
|
||||
let icon_width = get_i32_prop(&props, "icon_width", Some(-1))?;
|
||||
let icon_height = get_i32_prop(&props, "icon_height", Some(-1))?;
|
||||
let preserve_aspect_ratio = get_bool_prop(&props, "preserve_aspect_ratio", Some(true))?;
|
||||
let fill_svg = get_string_prop(&props, "fill_svg", Some(""))?;
|
||||
|
||||
if !path.ends_with(".svg") && !fill_svg.is_empty() {
|
||||
log::warn!("Fill attribute ignored, file is not an svg image");
|
||||
}
|
||||
|
||||
if path.ends_with(".gif") {
|
||||
let pixbuf_animation =
|
||||
gtk4::gdk_pixbuf::PixbufAnimation::from_file(std::path::PathBuf::from(path))?;
|
||||
let iter = pixbuf_animation.iter(None);
|
||||
|
||||
let frame_pixbuf = iter.pixbuf();
|
||||
widget.set_from_pixbuf(Some(&frame_pixbuf));
|
||||
|
||||
let widget_clone = widget.clone();
|
||||
|
||||
if let Some(delay) = iter.delay_time() {
|
||||
glib::timeout_add_local(delay, move || {
|
||||
let frame_pixbuf = iter.pixbuf();
|
||||
widget_clone.set_from_pixbuf(Some(&frame_pixbuf));
|
||||
|
||||
glib::ControlFlow::Continue
|
||||
});
|
||||
}
|
||||
} else {
|
||||
let pixbuf;
|
||||
// populate the pixel buffer
|
||||
if path.ends_with(".svg") && !fill_svg.is_empty() {
|
||||
let svg_data = std::fs::read_to_string(std::path::PathBuf::from(path.clone()))?;
|
||||
// The fastest way to add/change fill color
|
||||
let svg_data = if svg_data.contains("fill=") {
|
||||
let reg = regex::Regex::new(r#"fill="[^"]*""#)?;
|
||||
reg.replace(&svg_data, &format!("fill=\"{}\"", fill_svg))
|
||||
} else {
|
||||
let reg = regex::Regex::new(r"<svg")?;
|
||||
reg.replace(&svg_data, &format!("<svg fill=\"{}\"", fill_svg))
|
||||
};
|
||||
let stream = gtk4::gio::MemoryInputStream::from_bytes(>k4::glib::Bytes::from(
|
||||
svg_data.as_bytes(),
|
||||
));
|
||||
pixbuf = gtk4::gdk_pixbuf::Pixbuf::from_stream_at_scale(
|
||||
&stream,
|
||||
icon_width,
|
||||
icon_height,
|
||||
preserve_aspect_ratio,
|
||||
None::<>k4::gio::Cancellable>,
|
||||
)?;
|
||||
stream.close(None::<>k4::gio::Cancellable>)?;
|
||||
} else {
|
||||
pixbuf = gtk4::gdk_pixbuf::Pixbuf::from_file_at_scale(
|
||||
std::path::PathBuf::from(path),
|
||||
icon_width,
|
||||
icon_height,
|
||||
preserve_aspect_ratio,
|
||||
)?;
|
||||
}
|
||||
widget.set_from_pixbuf(Some(&pixbuf));
|
||||
}
|
||||
|
||||
if let Ok(icon_name) = get_string_prop(&props, "icon", None) {
|
||||
widget.set_icon_name(Some(&icon_name));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
};
|
||||
|
||||
apply_props(&props, >k_widget)?;
|
||||
|
||||
let gtk_widget_clone = gtk_widget.clone();
|
||||
let update_fn: UpdateFn = Box::new(move |props: &Map| {
|
||||
let _ = apply_props(props, >k_widget_clone);
|
||||
|
||||
// now re-apply generic widget attrs
|
||||
if let Err(err) =
|
||||
resolve_rhai_widget_attrs(>k_widget_clone.clone().upcast::<gtk4::Widget>(), &props)
|
||||
{
|
||||
eprintln!("Failed to update widget attrs: {:?}", err);
|
||||
}
|
||||
});
|
||||
|
||||
let id = hash_props_and_type(&props, "Image");
|
||||
|
||||
widget_registry
|
||||
.widgets
|
||||
.insert(id, WidgetEntry { update_fn, widget: gtk_widget.clone().upcast() });
|
||||
|
||||
resolve_rhai_widget_attrs(>k_widget.clone().upcast::<gtk4::Widget>(), &props)?;
|
||||
|
||||
Ok(gtk_widget)
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct GtkButtonCtrlData {
|
||||
// button press
|
||||
|
||||
Reference in New Issue
Block a user