From c4a156fb898372b709d5472d4a0db5cc6737b31f Mon Sep 17 00:00:00 2001 From: Dave Davenport Date: Wed, 14 Jul 2021 17:29:38 +0200 Subject: [Icon] Add a squared option. --- doc/rofi-theme.5 | 3 +++ doc/rofi-theme.5.markdown | 1 + source/widgets/icon.c | 22 ++++++++++++++++++++++ 3 files changed, 26 insertions(+) diff --git a/doc/rofi-theme.5 b/doc/rofi-theme.5 index dc12e4fb..65a26eea 100644 --- a/doc/rofi-theme.5 +++ b/doc/rofi-theme.5 @@ -1399,6 +1399,9 @@ This is an icon widget. The displayed icon can be set with \fB\fCfilename\fR and If the property \fB\fCaction\fR is set, it acts as a button. \fB\fCaction\fR can be set to a keybinding name and completes that action. (see rofi \-show keys for a list). +.PP +If the \fB\fCsquared\fR property is set to \fBfalse\fP the widget height and width are not forced to be equal. + .PP Example: diff --git a/doc/rofi-theme.5.markdown b/doc/rofi-theme.5.markdown index a79ad488..6864b44b 100644 --- a/doc/rofi-theme.5.markdown +++ b/doc/rofi-theme.5.markdown @@ -869,6 +869,7 @@ This is an icon widget. The displayed icon can be set with `filename` and size w If the property `action` is set, it acts as a button. `action` can be set to a keybinding name and completes that action. (see rofi -show keys for a list). +If the `squared` property is set to **false** the widget height and width are not forced to be equal. Example: diff --git a/source/widgets/icon.c b/source/widgets/icon.c index 190041b9..3fe60608 100644 --- a/source/widgets/icon.c +++ b/source/widgets/icon.c @@ -44,6 +44,8 @@ struct _icon // Size of the icon. int size; + int squared; + uint32_t icon_fetch_id; double yalign, xalign; @@ -56,6 +58,15 @@ static int icon_get_desired_height ( widget *widget ) { icon *b = (icon *) widget; int height = b->size; + if ( b->squared == FALSE ){ + if( b->icon ) { + int iconh = cairo_image_surface_get_height ( b->icon ); + int iconw = cairo_image_surface_get_width ( b->icon ); + int icons = MAX ( iconh, iconw ); + double scale = (double) b->size / icons; + height = iconh*scale; + } + } height += widget_padding_get_padding_height ( widget ); return height; } @@ -63,6 +74,15 @@ static int icon_get_desired_width ( widget *widget ) { icon *b = (icon *) widget; int width = b->size; + if ( b->squared == FALSE ){ + if( b->icon ) { + int iconh = cairo_image_surface_get_height ( b->icon ); + int iconw = cairo_image_surface_get_width ( b->icon ); + int icons = MAX ( iconh, iconw ); + double scale = (double) b->size / icons; + width = iconw*scale; + } + } width += widget_padding_get_padding_width ( widget ); return width; } @@ -152,6 +172,8 @@ icon * icon_create ( widget *parent, const char *name ) RofiDistance d = rofi_theme_get_distance ( WIDGET ( b ), "size", b->size ); b->size = distance_get_pixel ( d, ROFI_ORIENTATION_VERTICAL ); + b->squared = rofi_theme_get_boolean ( WIDGET ( b ), "squared", TRUE ); + const char * filename = rofi_theme_get_string ( WIDGET ( b ), "filename", NULL ); if ( filename ) { b->icon_fetch_id = rofi_icon_fetcher_query ( filename, b->size ); -- cgit v1.2.3