Skip to content

Commit

Permalink
Allow to keep zoom across images
Browse files Browse the repository at this point in the history
Resolves #230.

Signed-off-by: Artem Senichev <[email protected]>
  • Loading branch information
artemsen committed Jan 10, 2025
1 parent e03a030 commit 9081716
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 44 deletions.
2 changes: 2 additions & 0 deletions extra/swayimgrc
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ window = #00000000
transparency = grid
# Default image scale (optimal/fit/width/height/fill/real)
scale = optimal
# Keep absolute zoom across images (yes/no)
keep_zoom = no
# Initial image position
position = center
# Fix position of the image on the window surface (yes/no)
Expand Down
3 changes: 3 additions & 0 deletions extra/swayimgrc.5
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ Default image scale, valid modes are:
\fIfill\fR: crop image to fill the window;
\fIreal\fR: real size (100%).
.\" ----------------------------------------------------------------------------
.IP "\fBkeep_zoom\fR\fR = \fI[yes|no]\fR"
Keep absolute zoom across images, \fIno\fR by default.
.\" ----------------------------------------------------------------------------
.IP "\fBposition\fR = \fIPOSITION\fR"
Initial image position, valid modes are:
.nf
Expand Down
1 change: 1 addition & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static const struct config_default defaults[] = {
{ CFG_VIEWER, CFG_VIEW_WINDOW, "#00000000" },
{ CFG_VIEWER, CFG_VIEW_TRANSP, "grid" },
{ CFG_VIEWER, CFG_VIEW_SCALE, "optimal" },
{ CFG_VIEWER, CFG_VIEW_KEEP_ZM, CFG_NO },
{ CFG_VIEWER, CFG_VIEW_POSITION, "center" },
{ CFG_VIEWER, CFG_VIEW_FIXED, CFG_YES },
{ CFG_VIEWER, CFG_VIEW_AA, "mks13" },
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ struct config {
#define CFG_VIEW_WINDOW "window"
#define CFG_VIEW_TRANSP "transparency"
#define CFG_VIEW_SCALE "scale"
#define CFG_VIEW_KEEP_ZM "keep_zoom"
#define CFG_VIEW_POSITION "position"
#define CFG_VIEW_FIXED "fixed"
#define CFG_VIEW_AA "antialiasing"
Expand Down
101 changes: 57 additions & 44 deletions src/viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ struct viewer {
bool fixed; ///< Fix image position

enum fixed_scale scale_init; ///< Initial scale
bool keep_zoom; ///< Keep absolute zoom across images
enum position position; ///< Initial position
float scale; ///< Current scale factor of the image

Expand Down Expand Up @@ -150,6 +151,56 @@ static void fixup_position(bool force)
}
}

/** Set image position. */
static void set_position(void)
{
const struct image* img = fetcher_current();
const struct pixmap* pm = &img->frames[ctx.frame].pm;
const size_t wnd_width = ui_get_width();
const size_t wnd_height = ui_get_height();

switch (ctx.position) {
case position_top:
ctx.img_y = 0;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_center:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_bottom:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_left:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = 0;
break;
case position_right:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
case position_top_left:
ctx.img_y = 0;
ctx.img_x = 0;
break;
case position_top_right:
ctx.img_y = 0;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
case position_bottom_left:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = 0;
break;
case position_bottom_right:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
}

fixup_position(true);
}

/**
* Move image (viewport).
* @param horizontal axis along which to move (false for vertical)
Expand Down Expand Up @@ -244,46 +295,7 @@ static void scale_image(enum fixed_scale sc)
break;
}

switch (ctx.position) {
case position_top:
ctx.img_y = 0;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_center:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_bottom:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = wnd_width / 2 - (ctx.scale * pm->width) / 2;
break;
case position_left:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = 0;
break;
case position_right:
ctx.img_y = wnd_height / 2 - (ctx.scale * pm->height) / 2;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
case position_top_left:
ctx.img_y = 0;
ctx.img_x = 0;
break;
case position_top_right:
ctx.img_y = 0;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
case position_bottom_left:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = 0;
break;
case position_bottom_right:
ctx.img_y = wnd_height - ctx.scale * pm->height;
ctx.img_x = wnd_width - ctx.scale * pm->width;
break;
}

fixup_position(true);
set_position();
info_update(info_scale, "%.0f%%", ctx.scale * 100);
}

Expand Down Expand Up @@ -417,10 +429,10 @@ static void reset_state(void)
const size_t total_img = image_list_size();

ctx.frame = 0;
ctx.img_x = 0;
ctx.img_y = 0;
ctx.scale = 0;
scale_image(ctx.scale_init);
if (!ctx.keep_zoom || ctx.scale == 0) {
scale_image(ctx.scale_init);
}
set_position();

ui_set_title(img->name);
animation_ctl(true);
Expand Down Expand Up @@ -755,6 +767,7 @@ void viewer_init(const struct config* cfg, struct image* image)
// initial scale and position
ctx.scale_init = config_get_oneof(cfg, CFG_VIEWER, CFG_VIEW_SCALE,
scale_names, ARRAY_SIZE(scale_names));
ctx.keep_zoom = config_get_bool(cfg, CFG_VIEWER, CFG_VIEW_KEEP_ZM);
ctx.position = config_get_oneof(cfg, CFG_VIEWER, CFG_VIEW_POSITION,
position_names, ARRAY_SIZE(position_names));

Expand Down

0 comments on commit 9081716

Please sign in to comment.