Skip to content

Commit

Permalink
Add support for custom move step
Browse files Browse the repository at this point in the history
Step is specified as a percentage delta of scaled image size.
Stored as a custom parameter in the key binding section of config file:
```
[keys]
Up = zoom_in 10
```

Resolves to #81.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Dec 23, 2023
1 parent 7fc025b commit 504bf7d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 64 deletions.
16 changes: 8 additions & 8 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ s = animation
F9 = slideshow
F11 = fullscreen
f = fullscreen
Left = step_left
h = step_left
Right = step_right
l = step_right
Up = step_up
k = step_up
Down = step_down
j = step_down
Left = step_left 10
h = step_left 10
Right = step_right 10
l = step_right 10
Up = step_up 10
k = step_up 10
Down = step_down 10
j = step_down 10
equal = zoom_in 10
plus = zoom_in 10
minus = zoom_out 10
Expand Down
13 changes: 7 additions & 6 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ Valid action are:
.IP "\fBanimation\fR: start/stop animation;"
.IP "\fBslideshow\fR: start/stop slideshow;"
.IP "\fBfullscreen\fR: switch fullscreen mode;"
.IP "\fBstep_left\fR: move viewport left;"
.IP "\fBstep_right\fR: move viewport right;"
.IP "\fBstep_up\fR: move viewport up;"
.IP "\fBstep_down\fR: move viewport down;"
.IP "\fBstep_left \fI[PERCENT]\fR\fR: move viewport left, default is 10%;"
.IP "\fBstep_right \fI[PERCENT]\fR\fR: move viewport right, default is 10%;"
.IP "\fBstep_up \fI[PERCENT]\fR\fR: move viewport up, default is 10%;"
.IP "\fBstep_down \fI[PERCENT]\fR\fR: move viewport down, default is 10%;"
.IP "\fBzoom_in\fR \fI[PERCENT]\fR: zoom in, by default increases scale by 10%;"
.IP "\fBzoom_out\fR \fI[PERCENT]\fR: zoom out, by default decreases scale by 10%;"
.IP "\fBzoom_optimal\fR: set to optimal zoom (100% or less to fit to window);"
Expand Down Expand Up @@ -115,10 +115,11 @@ info = yes
font = monospace 14
font-color = #b4b3b2
sway = no
[keys]
q = quit
r = exec rm "%"
R = exec echo "%" > mylist.txt
Up = step_up 5
e = exec echo "%" > mylist.txt
.EE
.\" related man pages
.SH SEE ALSO
Expand Down
47 changes: 14 additions & 33 deletions src/canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -356,43 +356,20 @@ void canvas_print_info(argb_t* wnd, size_t num, const struct info_table* info)
}
}

bool canvas_move(enum canvas_move mv)
bool canvas_move(bool horizontal, ssize_t percent)
{
const size_t scaled_width = ctx.scale * ctx.image.width;
const size_t scaled_height = ctx.scale * ctx.image.height;
const size_t step_x = ctx.window.width / 10;
const size_t step_y = ctx.window.height / 10;
ssize_t prev_x = ctx.image.x;
ssize_t prev_y = ctx.image.y;

switch (mv) {
case cm_center:
ctx.image.x = ctx.window.width / 2 - scaled_width / 2;
ctx.image.y = ctx.window.height / 2 - scaled_height / 2;
break;
case cm_cnt_hor:
ctx.image.x = ctx.window.width / 2 - scaled_width / 2;
break;
case cm_cnt_vert:
ctx.image.y = ctx.window.height / 2 - scaled_height / 2;
break;
case cm_step_left:
ctx.image.x += step_x;
break;
case cm_step_right:
ctx.image.x -= step_x;
break;
case cm_step_up:
ctx.image.y += step_y;
break;
case cm_step_down:
ctx.image.y -= step_y;
break;
const ssize_t old_x = ctx.image.x;
const ssize_t old_y = ctx.image.y;

if (horizontal) {
ctx.image.x += (ctx.window.width / 100) * percent;
} else {
ctx.image.y += (ctx.window.height / 100) * percent;
}

fix_viewport();

return (ctx.image.x != prev_x || ctx.image.y != prev_y);
return (ctx.image.x != old_x || ctx.image.y != old_y);
}

void canvas_zoom(ssize_t percent)
Expand Down Expand Up @@ -452,7 +429,11 @@ void canvas_set_scale(enum canvas_scale sc)
break;
}

canvas_move(cm_center);
// center viewport
ctx.image.x = ctx.window.width / 2 - (ctx.scale * ctx.image.width) / 2;
ctx.image.y = ctx.window.height / 2 - (ctx.scale * ctx.image.height) / 2;

fix_viewport();
}

float canvas_get_scale(void)
Expand Down
16 changes: 3 additions & 13 deletions src/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,6 @@

#include "types.h"

/** Viewport movement. */
enum canvas_move {
cm_center, ///< Center of the image
cm_cnt_hor, ///< Center horizontally
cm_cnt_vert, ///< Center vertically
cm_step_left, ///< One step to the left
cm_step_right, ///< One step to the right
cm_step_up, ///< One step up
cm_step_down ///< One step down
};

/** Scaling operations. */
enum canvas_scale {
cs_fit_or100, ///< Fit to window, but not more than 100%
Expand Down Expand Up @@ -87,10 +76,11 @@ void canvas_print_info(argb_t* wnd, size_t num, const struct info_table* info);

/**
* Move viewport.
* @param mv viewport movement direction
* @param horizontal axis along which to move (false for vertical)
* @param percent percentage increment to current position
* @return true if coordinates were changed
*/
bool canvas_move(enum canvas_move mv);
bool canvas_move(bool horizontal, ssize_t percent);

/**
* Zoom in/out.
Expand Down
35 changes: 31 additions & 4 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,33 @@ static void zoom_image(bool zoom_in, const char* params)
canvas_zoom(percent);
}

/**
* Move viewport.
* @param horizontal axis along which to move (false for vertical)
* @param positive direction (increase/decrease)
* @param params optional move step in percents
*/
static bool move_viewport(bool horizontal, bool positive, const char* params)
{
ssize_t percent = 10;

if (params) {
char* endptr;
const unsigned long val = strtoul(params, &endptr, 0);
if (val != 0 && val <= 1000 && !*endptr) {
percent = val;
} else {
fprintf(stderr, "Invalid move step: \"%s\"\n", params);
}
}

if (!positive) {
percent = -percent;
}

return canvas_move(horizontal, percent);
}

/**
* Animation timer event handler.
*/
Expand Down Expand Up @@ -437,13 +464,13 @@ bool viewer_on_keyboard(xkb_keysym_t key)
ui_set_fullscreen(config.fullscreen);
return false;
case kb_step_left:
return canvas_move(cm_step_left);
return move_viewport(true, true, kbind->params);
case kb_step_right:
return canvas_move(cm_step_right);
return move_viewport(true, false, kbind->params);
case kb_step_up:
return canvas_move(cm_step_up);
return move_viewport(false, true, kbind->params);
case kb_step_down:
return canvas_move(cm_step_down);
return move_viewport(false, false, kbind->params);
case kb_zoom_in:
case kb_zoom_out:
zoom_image(kbind->action == kb_zoom_in, kbind->params);
Expand Down

0 comments on commit 504bf7d

Please sign in to comment.