Skip to content

Commit

Permalink
Add default/global scale action
Browse files Browse the repository at this point in the history
Enables users to change the default scaling mode set in config also during runtime.

Resolves #205

Signed-off-by: Josef Litos <[email protected]>
  • Loading branch information
litoj authored Oct 26, 2024
1 parent 1018ee3 commit 5d26bc6
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 3 deletions.
1 change: 1 addition & 0 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -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;"
Expand Down
1 change: 1 addition & 0 deletions src/action.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 30 additions & 2 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 >= ARRAY_SIZE(scale_names)) {
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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 5d26bc6

Please sign in to comment.