aboutsummaryrefslogtreecommitdiffstats
path: root/main.c
diff options
context:
space:
mode:
authorSam Anthony <sam@samanthony.xyz>2025-04-29 14:51:55 -0400
committerSam Anthony <sam@samanthony.xyz>2025-04-29 14:51:55 -0400
commit4700d22680347992f7c0a87715d162658eb6e4ef (patch)
tree950533e250c62f0771ed48d1832b1d5ca3dc94c3 /main.c
parenta7284d1250b3605a471daddb9a214c2fa5978f82 (diff)
downloadvolute-4700d22680347992f7c0a87715d162658eb6e4ef.zip
preserve image aspect ratio
Diffstat (limited to 'main.c')
-rw-r--r--main.c37
1 files changed, 28 insertions, 9 deletions
diff --git a/main.c b/main.c
index 1edaeda..93673c3 100644
--- a/main.c
+++ b/main.c
@@ -74,6 +74,7 @@ static void mass_flow_rate_row(mu_Context *ctx, UI *ui);
static void mass_flow_rate_corrected_row(mu_Context *ctx, UI *ui);
static void comp_select(mu_Context *ctx, UI *ui);
static void comp_img(mu_Context *ctx, UI *ui);
+static void scale_preserve_aspect_ratio(mu_Rect *r, int wmax, int hmax);
static void output_row(mu_Context *ctx, UI *ui, const char *label, w_Select *unit, w_Number outputs[]);
static void hpad(mu_Context *ctx, int w);
static void vpad(mu_Context *ctx, int h);
@@ -396,21 +397,39 @@ comp_select(mu_Context *ctx, UI *ui) {
static void
comp_img(mu_Context *ctx, UI *ui) {
- int w, h;
- mu_Rect r;
+ mu_Rect win, area, img;
- /* Row that covers the rest of the window. */
- r_get_window_size(&w, &h);
- mu_layout_row(ctx, 1, &w, h);
- r = mu_layout_next(ctx);
- r.w = w - r.x - ctx->style->spacing;
- r.h = h - r.y - ctx->style->spacing;
- mu_layout_set_next(ctx, r, 0);
+ win.x = win.y = 0;
+ r_get_window_size(&win.w, &win.h);
+ mu_layout_row(ctx, 1, &win.w, win.h);
+
+ area = mu_layout_next(ctx);
+ area.w = win.w - area.x - ctx->style->padding;
+ area.h = win.h - area.y - ctx->style->padding;
+
+ img.x = area.x;
+ img.y = area.y;
+ r_get_icon_size(ui->comp_img.id, &img.w, &img.h);
+
+ scale_preserve_aspect_ratio(&img, area.w, area.h);
+
+ mu_layout_set_next(ctx, img, 0);
w_image(ctx, &ui->comp_img);
}
static void
+scale_preserve_aspect_ratio(mu_Rect *r, int wmax, int hmax) {
+ double wscale, hscale, scale;
+
+ wscale = (double) wmax / (double) r->w;
+ hscale = (double) hmax / (double) r->h;
+ scale = mu_min(wscale, hscale);
+ r->w *= scale;
+ r->h *= scale;
+}
+
+static void
output_row(mu_Context *ctx, UI *ui, const char *label, w_Select *unit, w_Number outputs[]) {
int i;