Skip to content

Commit

Permalink
Add "Scale (fit)" background image mode
Browse files Browse the repository at this point in the history
The current "Scale" mode crops out part of the image if the scaled
image is larger than the terminal in any direction. This is useful for
most cases, such as wallpapers repurposed as terminal backgrounds.
However, this may be surprising to users coming from xfce4-terminal,
where the equivalent "Scaled" mode tries to fit the background within
the terminal in both dimensions. This new "Scale (fit)" mode is meant
to emulate this behaviour.
  • Loading branch information
PatchMixolydic committed Feb 22, 2022
1 parent 7a36218 commit 4e6e7d6
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
7 changes: 4 additions & 3 deletions data/gsettings/com.gexperts.Tilix.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,10 @@
<!-- Background Image Options -->
<enum id='com.gexperts.Tilix.Background.Image.Mode'>
<value nick='scale' value='0'/>
<value nick='tile' value='1'/>
<value nick='center' value='2'/>
<value nick='stretch' value='3'/>
<value nick='scale-fit' value='1'/>
<value nick='tile' value='2'/>
<value nick='center' value='3'/>
<value nick='stretch' value='4'/>
</enum>

<!-- Background Image Scaling Options, note these
Expand Down
19 changes: 18 additions & 1 deletion source/gx/gtk/cairo.d
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Pixbuf getWidgetImage(Widget widget, double factor, int width, int height) {
}
}

enum ImageLayoutMode {SCALE, TILE, CENTER, STRETCH};
enum ImageLayoutMode {SCALE, SCALE_FIT, TILE, CENTER, STRETCH};

ImageSurface renderImage(Pixbuf pb, bool alpha = false) {
cairo_format_t format = alpha?cairo_format_t.ARGB32:cairo_format_t.RGB24;
Expand Down Expand Up @@ -172,6 +172,23 @@ void renderImage(Context cr, ImageSurface isSource, int outputWidth, int outputH
cr.getSource().setFilter(scaleMode);
cr.paint();
break;
case ImageLayoutMode.SCALE_FIT:
// Emulate xfce4-terminal's "Scaled" behaviour -- scale the image
// such that it fills the dimension with the shortest length and
// fits both the x and y dimensions.
double xScale = to!double(outputWidth) / to!double(isSource.getWidth());
double yScale = to!double(outputHeight) / to!double(isSource.getHeight());
double scale = min(xScale, yScale);

double xOffset = (outputWidth - (isSource.getWidth() * scale)) / 2.0;
double yOffset = (outputHeight - (isSource.getHeight() * scale)) / 2.0;

cr.translate(xOffset, yOffset);
cr.scale(scale, scale);
cr.setSourceSurface(isSource, 0, 0);
cr.getSource().setFilter(scaleMode);
cr.paint();
break;
case ImageLayoutMode.TILE:
cr.setSourceSurface(isSource, 0, 0);
cr.getSource().setExtend(cairo_extend_t.REPEAT);
Expand Down
3 changes: 3 additions & 0 deletions source/gx/tilix/appwindow.d
Original file line number Diff line number Diff line change
Expand Up @@ -2034,6 +2034,9 @@ public:
case SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_VALUE:
mode = ImageLayoutMode.SCALE;
break;
case SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_FIT_VALUE:
mode = ImageLayoutMode.SCALE_FIT;
break;
case SETTINGS_BACKGROUND_IMAGE_MODE_TILE_VALUE:
mode = ImageLayoutMode.TILE;
break;
Expand Down
2 changes: 1 addition & 1 deletion source/gx/tilix/prefeditor/prefdialog.d
Original file line number Diff line number Diff line change
Expand Up @@ -1199,7 +1199,7 @@ class AppearancePreferences: Box {
gsSettings.reset(SETTINGS_BACKGROUND_IMAGE_KEY);
});

ComboBox cbImageMode = createNameValueCombo([_("Scale"), _("Tile"), _("Center"),_("Stretch")], SETTINGS_BACKGROUND_IMAGE_MODE_VALUES);
ComboBox cbImageMode = createNameValueCombo([_("Scale"), _("Scale (fit)"), _("Tile"), _("Center"),_("Stretch")], SETTINGS_BACKGROUND_IMAGE_MODE_VALUES);
bh.bind(SETTINGS_BACKGROUND_IMAGE_MODE_KEY, cbImageMode, "active-id", GSettingsBindFlags.DEFAULT);

// Background image settings only enabled if transparency is enabled
Expand Down
3 changes: 2 additions & 1 deletion source/gx/tilix/preferences.d
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,11 @@ enum SETTINGS_BACKGROUND_IMAGE_KEY = "background-image";
enum SETTINGS_BACKGROUND_IMAGE_SCALE_KEY = "background-image-scale";
enum SETTINGS_BACKGROUND_IMAGE_MODE_KEY = "background-image-mode";
enum SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_VALUE = "scale";
enum SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_FIT_VALUE = "scale-fit";
enum SETTINGS_BACKGROUND_IMAGE_MODE_TILE_VALUE = "tile";
enum SETTINGS_BACKGROUND_IMAGE_MODE_CENTER_VALUE = "center";
enum SETTINGS_BACKGROUND_IMAGE_MODE_STRETCH_VALUE = "stretch";
immutable string[] SETTINGS_BACKGROUND_IMAGE_MODE_VALUES = [SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_TILE_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_CENTER_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_STRETCH_VALUE];
immutable string[] SETTINGS_BACKGROUND_IMAGE_MODE_VALUES = [SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_SCALE_FIT_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_TILE_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_CENTER_VALUE,SETTINGS_BACKGROUND_IMAGE_MODE_STRETCH_VALUE];

enum SETTINGS_SIDEBAR_RIGHT = "sidebar-on-right";
enum SETTINGS_RECENT_SESSION_FILES_KEY = "recent-session-files";
Expand Down

0 comments on commit 4e6e7d6

Please sign in to comment.