don't allow scaling too far up/down, also add wip default image size keybind

This commit is contained in:
speedie 2023-03-03 21:18:25 +01:00
parent 6f8e1c5683
commit 3d4fc5247a
5 changed files with 53 additions and 16 deletions

1
TODO
View file

@ -4,5 +4,4 @@
- Remove padding added for background colors - Remove padding added for background colors
- Add configuration file using (probably) libconfig - Add configuration file using (probably) libconfig
- Fix highlighting for last character in a match - Fix highlighting for last character in a match
- Don't allow image scaling too far up/down
- Add rotate and flip keybinds for the image - Add rotate and flip keybinds for the image

View file

@ -51,6 +51,7 @@ static Key normkeys[] = {
{ 0, XK_minus, setimgsize, {.i = -10 } }, { 0, XK_minus, setimgsize, {.i = -10 } },
{ SHIFT, XK_equal, setimgsize, {.i = +100 } }, { SHIFT, XK_equal, setimgsize, {.i = +100 } },
{ SHIFT, XK_minus, setimgsize, {.i = -100 } }, { SHIFT, XK_minus, setimgsize, {.i = -100 } },
{ 0, XK_d, defaultimg, {0} },
{ 0, XK_t, toggleimg, {0} }, { 0, XK_t, toggleimg, {0} },
{ 0, XK_k, move, {.i = 1 } }, { 0, XK_k, move, {.i = 1 } },
{ 0, XK_j, move, {.i = 2 } }, { 0, XK_j, move, {.i = 2 } },

View file

@ -1,5 +1,5 @@
void void
cleanupimg(void) cleanupimage(void)
{ {
if (image) { if (image) {
imlib_free_image(); imlib_free_image();
@ -9,6 +9,24 @@ cleanupimg(void)
return; return;
} }
void
prepareimage(void)
{
/* values which can be restored later */
if (!imagew || !imageh || !imageg) {
imagew = imagewidth;
imageh = imageheight;
imageg = imagegaps;
}
/* restore values if necessary */
if (!imageheight || !imagewidth || !longestedge) {
imageheight = imageh;
imagewidth = imagew;
imagegaps = imageg;
}
}
void void
drawimage(void) drawimage(void)
{ {
@ -22,11 +40,6 @@ drawimage(void)
if (!lines) return; if (!lines) return;
if (hideimage) return; if (hideimage) return;
if (!imagewidth || !imageheight) {
imagewidth = imageheight = longestedge = imagegaps = 0;
return;
}
if (sel && sel->image && strcmp(sel->image, limg ? limg : "")) { if (sel && sel->image && strcmp(sel->image, limg ? limg : "")) {
if (longestedge) if (longestedge)
loadimagecache(sel->image, &width, &height); loadimagecache(sel->image, &width, &height);
@ -34,6 +47,9 @@ drawimage(void)
imlib_free_image(); imlib_free_image();
image = NULL; image = NULL;
} if (image && longestedge) { } if (image && longestedge) {
prepareimage();
int leftmargin = imagegaps; int leftmargin = imagegaps;
if(mh != bh + height + imagegaps * 2) { if(mh != bh + height + imagegaps * 2) {

View file

@ -4,8 +4,9 @@
#include <openssl/md5.h> #include <openssl/md5.h>
static void setimageopts(void); static void setimageopts(void);
static void cleanupimg(void); static void cleanupimage(void);
static void drawimage(void); static void drawimage(void);
static void prepareimage(void);
static void loadimagecache(const char *file, int *width, int *height); static void loadimagecache(const char *file, int *width, int *height);
static void resizetoimageheight(int imageheight); static void resizetoimageheight(int imageheight);

View file

@ -127,6 +127,10 @@ static struct item *prev, *curr, *next, *sel;
static int mon = -1, screen; static int mon = -1, screen;
static int managed = 0; static int managed = 0;
static int imagew = 0;
static int imageh = 0;
static int imageg = 0;
#if USERTL #if USERTL
static int isrtl = 1; static int isrtl = 1;
#else #else
@ -173,6 +177,7 @@ static void complete(const Arg *arg);
static void savehistory(char *input); static void savehistory(char *input);
static void setimgsize(const Arg *arg); static void setimgsize(const Arg *arg);
static void toggleimg(const Arg *arg); static void toggleimg(const Arg *arg);
static void defaultimg(const Arg *arg);
static void drawmenu(void); static void drawmenu(void);
static void calcoffsets(void); static void calcoffsets(void);
@ -220,16 +225,14 @@ setimgsize(const Arg *arg)
return; return;
#endif #endif
cleanupimg(); /* this makes sure we cannot scale down the image too much */
if (!image && imageheight + arg->i < imageheight || hideimage) return;
cleanupimage();
imageheight += arg->i; imageheight += arg->i;
imagewidth += arg->i; imagewidth += arg->i;
if (!imageheight || !imagewidth || !longestedge) {
imageheight = imagewidth = longestedge = 1;
return;
}
drawmenu(); drawmenu();
drawimage(); drawimage();
} }
@ -241,7 +244,7 @@ toggleimg(const Arg *arg)
return; return;
#endif #endif
cleanupimg(); cleanupimage();
hideimage = !hideimage; hideimage = !hideimage;
@ -249,6 +252,23 @@ toggleimg(const Arg *arg)
drawimage(); drawimage();
} }
void
defaultimg(const Arg *arg)
{
#if !USEIMAGE
return;
#endif
if (hideimage || !image) return;
/* this will cause values to be reset */
imagewidth = 0;
prepareimage();
drawmenu();
drawimage();
}
void void
appenditem(struct item *item, struct item **list, struct item **last) appenditem(struct item *item, struct item **list, struct item **last)
{ {
@ -321,7 +341,7 @@ cleanup(void)
size_t i; size_t i;
#if USEIMAGE #if USEIMAGE
cleanupimg(); cleanupimage();
#endif #endif
XUngrabKey(dpy, AnyKey, AnyModifier, root); XUngrabKey(dpy, AnyKey, AnyModifier, root);