Compute background dimensions preserving aspect ratio. #6504
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Compute background dimensions preserving original aspect ratio.
The problem I'm trying to solve:
In my config I'm setting many different backgrounds, for all of them I'm typically setting custom
width
andheight
, so for each one of them I have to manually compute the right values based on aspect ratio. And since my images have different aspect ratios I'm find the process a bit tedious.Current Behavior
Currently, wezterm supports size="Cover" which, as the documentation states: "scales the image to the smallest possible size to fill the viewport".
What I'm doing in this changeset?
In this PR I'm adding additional behavior for the mentioned unit, so, If only one of the dimension attributes is set to
Cover
(example height=Cover, width=200), the value of the mentioned attribute will be computed based on the dimension assigned to the other one, and of course, preserving the aspect ratio.BTW, even though I used to do C many (many) years ago, I'm not familiar with rust AT ALL (I'm just learning some bits today after reading your code), so bear with me if things are not good enough.
Are there any other ways to approach this use-case?
Yeah, we could also introduce a new unit type?, example width="auto", but I was not sure so I decided to keep this simple, (to me, the current approach in this PR makes sense and feels like easy to use).
Examples:
1) Compute height based image width:
Here I'm setting an arbitrary width = 550px while I set height=Cover.
Before:
After:
2) Compute correct width based on user provided height:
Before:
After: