From 882f53456ca5bf9336f35aad51b9426f65d93a47 Mon Sep 17 00:00:00 2001 From: JosefLitos Date: Sat, 26 Oct 2024 14:01:20 +0200 Subject: [PATCH] feat(#205): add default/global scale action Enables users to change the default scaling mode set in config also during runtime. Resolves #205 Signed-off-by: JosefLitos --- .editorconfig | 6 ++++++ extra/swayimgrc | 1 + extra/swayimgrc.5 | 3 ++- src/action.c | 1 + src/action.h | 1 + src/viewer.c | 32 ++++++++++++++++++++++++++++++-- 6 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0ce2ba6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,6 @@ +root=true + +[*] +indent_style = space +indent_size = 4 +max_line_length = 80 diff --git a/extra/swayimgrc b/extra/swayimgrc index 067c878..467772e 100644 --- a/extra/swayimgrc +++ b/extra/swayimgrc @@ -158,6 +158,7 @@ z = zoom fit Shift+z = zoom fill 0 = zoom real BackSpace = zoom optimal +Alt+s = scale bracketleft = rotate_left bracketright = rotate_right m = flip_vertical diff --git a/extra/swayimgrc.5 b/extra/swayimgrc.5 index c182ae5..95f5122 100644 --- a/extra/swayimgrc.5 +++ b/extra/swayimgrc.5 @@ -280,7 +280,8 @@ Predefined names for mouse scroll: .IP "\fBstep_right\fR \fI[PERCENT]\fR: move viewport right, default is 10%;" .IP "\fBstep_up\fR \fI[PERCENT]\fR: move viewport up, default is 10%;" .IP "\fBstep_down\fR \fI[PERCENT]\fR: move viewport down, default is 10%;" -.IP "\fBzoom\fR \fI[SCALE]\fR: zoom in/out/fix, \fISCALE\fR is one of \fIoptimal\fR, \fIwidth\fR, \fIheight\fR, \fIfit\fR, \fIfill\fR, \fIreal\fR, or percent, e.g. \fI+10\fR;" +.IP "\fBzoom\fR \fI[SCALE]\fR: zoom in/out/fix, \fISCALE\fR is one of \fIviewer.scale\fR modes, or percent, e.g. \fI+10\fR;" +.IP "\fBscale\fR \fI[SCALE]\fR: set default/global scale, \fISCALE\fR is one of \fIviewer.scale\fR modes, cycles through available modes by default;" .IP "\fBrotate_left\fR: rotate image anticlockwise;" .IP "\fBrotate_right\fR: rotate image clockwise;" .IP "\fBflip_vertical\fR: flip image vertically;" diff --git a/src/action.c b/src/action.c index bf5afc2..f298b72 100644 --- a/src/action.c +++ b/src/action.c @@ -37,6 +37,7 @@ static const char* action_names[] = { [action_page_up] = "page_up", [action_page_down] = "page_down", [action_zoom] = "zoom", + [action_scale] = "scale", [action_rotate_left] = "rotate_left", [action_rotate_right] = "rotate_right", [action_flip_vertical] = "flip_vertical", diff --git a/src/action.h b/src/action.h index ff9ec59..9c9537e 100644 --- a/src/action.h +++ b/src/action.h @@ -31,6 +31,7 @@ enum action_type { action_page_up, action_page_down, action_zoom, + action_scale, action_rotate_left, action_rotate_right, action_flip_vertical, diff --git a/src/viewer.c b/src/viewer.c index cf2769b..f50fce3 100644 --- a/src/viewer.c +++ b/src/viewer.c @@ -231,8 +231,6 @@ static void scale_image(enum fixed_scale sc) break; } - ctx.scale_init = sc; - // center viewport ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2; ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2; @@ -297,6 +295,33 @@ static void zoom_image(const char* params) app_redraw(); } +/** + * Set default scale to a fixed scale value and apply it. + * @param params fixed scale to set + */ +static void scale_global(const char* params) +{ + if (params && *params) { + ssize_t fixed_scale = str_index(scale_names, params, 0); + + if (fixed_scale >= 0) { + ctx.scale_init = fixed_scale; + } else { + fprintf(stderr, "Invalid scale operation: \"%s\"\n", params); + return; + } + } else { + // toggle to the next scale + ctx.scale_init++; + if (ctx.scale_init >= sizeof(scale_names) / sizeof(char*)) { + ctx.scale_init = 0; + } + } + + info_update(info_status, "Scale %s", scale_names[ctx.scale_init]); + scale_image(ctx.scale_init); +} + /** * Start/stop animation if image supports it. * @param enable state to set @@ -606,6 +631,9 @@ static void apply_action(const struct action* action) case action_zoom: zoom_image(action->params); break; + case action_scale: + scale_global(action->params); + break; case action_rotate_left: rotate_image(false); break;