Skip to content

Commit

Permalink
new option -s 5: prefer file icon if size is equal
Browse files Browse the repository at this point in the history
similar to default, but prefer equal-sized icon from file,
for better control over icon content: you can override
X11 property icon by file even if the former matches size exactly.
related: #170, 163.
  • Loading branch information
sagb committed May 11, 2024
1 parent 8ebe69e commit 313591a
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 14 deletions.
6 changes: 4 additions & 2 deletions doc/alttab.1
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.\" generated with Ronn-NG/v0.9.1
.\" http://github.com/apjanke/ronn-ng/tree/0.9.1
.TH "ALTTAB" "1" "July 2023" ""
.TH "ALTTAB" "1" "May 2024" ""
.SH "NAME"
\fBalttab\fR \- the task switcher
.SH "SYNOPSIS"
Expand Down Expand Up @@ -179,7 +179,7 @@ resource: alttab\.icon\.source
.br
default: 2
.IP
Source of icons\. \fINUMBER\fR must be between 0 and 4:
Source of icons\. \fINUMBER\fR must be between 0 and 5:
.IP
\fI0\fR: Use icons from X11 window attributes only\.
.IP
Expand Down Expand Up @@ -208,6 +208,8 @@ Also, alttab scans for icons in legacy directories without freedesktop directory
\fI3\fR: Use icons from files only\.
.IP
\fI4\fR: Don't draw icons\.
.IP
\fI5\fR: Prefer icon from X11 window attributes when it matches requested size better\.
.TP
\fB\-theme\fR \fIname\fR
resource: alttab\.theme
Expand Down
4 changes: 3 additions & 1 deletion doc/alttab.1.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ Most command line options have corresponding X resource, see doc/alttab.ad for e
resource: alttab.icon.source
default: 2

Source of icons. <NUMBER> must be between 0 and 4:
Source of icons. <NUMBER> must be between 0 and 5:

<0>: Use icons from X11 window attributes only.

Expand Down Expand Up @@ -249,6 +249,8 @@ Most command line options have corresponding X resource, see doc/alttab.ad for e

<4>: Don't draw icons.

<5>: Prefer icon from X11 window attributes when it matches requested size better.

* `-theme` <name>:
resource: alttab.theme
default: <hicolor>
Expand Down
3 changes: 2 additions & 1 deletion src/alttab.h
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ typedef struct {
#define ISRC_SIZE 2
#define ISRC_FILES 3
#define ISRC_NONE 4
#define ISRC_MAX 4
#define ISRC_SIZE2 5
#define ISRC_MAX 5
#define ISRC_DEFAULT ISRC_SIZE
int option_iconSrc;
char *option_theme;
Expand Down
7 changes: 5 additions & 2 deletions src/icon.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ int inspectIconMeta(FTSENT * pe)
// new candidate: ix, iy
// best value: g.option_iconW, H
// should we replace the icon?
if (iconMatchBetter(ix, iy, ic->src_w, ic->src_h)) {
if (iconMatchBetter(ix, iy, ic->src_w, ic->src_h, false)) {
strncpy(ic->src_path, pe->fts_path, MAXICONPATHLEN-1);
ic->src_w = ix;
ic->src_h = iy;
Expand Down Expand Up @@ -506,13 +506,16 @@ icon_t *lookupIcon(char *app)
//
// check if new width/height better match icon size option
// assuming square icons
// if equal_prefer_new=true and sizes are equal, then prefer new icon
//
bool iconMatchBetter(int new_w, int new_h, int old_w, int old_h)
bool iconMatchBetter(int new_w, int new_h, int old_w, int old_h, bool equal_prefer_new)
{
int hasdiff, newdiff;

hasdiff = old_h - g.option_iconH;
newdiff = new_h - g.option_iconH;
if (hasdiff == newdiff && equal_prefer_new)
return true;
return
(hasdiff >= 0) ? ((newdiff < 0) ? false : ((newdiff <
hasdiff) ? true : false)
Expand Down
2 changes: 1 addition & 1 deletion src/icon.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ int loadIconContentPNG(icon_t * ic);
int loadIconContentXPM(icon_t * ic);
int loadIconContent(icon_t * ic); // update drawable
icon_t *lookupIcon(char *app); // search app icon in hash
bool iconMatchBetter(int new_w, int new_h, int old_w, int old_h);
bool iconMatchBetter(int new_w, int new_h, int old_w, int old_h, bool equal_prefer_new);
void deleteIconHash(icon_t **ihash);

#endif
23 changes: 16 additions & 7 deletions src/win.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ int addIconFromProperty(WindowInfo * wi)
n += w*h;
continue;
}
if (best == 0 || iconMatchBetter(w, h, best_w, best_h)) {
if (best == 0 || iconMatchBetter(w, h, best_w, best_h, false)) {
best = n;
}
n += w*h;
Expand Down Expand Up @@ -343,11 +343,20 @@ int addIconFromFiles(WindowInfo * wi)
s = tryclass;
while ((s = strchr(s, '/')) != NULL) *s++ = '_';
ic = lookupIcon(tryclass);
if (ic &&
(g.option_iconSrc != ISRC_SIZE
|| iconMatchBetter(ic->src_w, ic->src_h,
wi->icon_w, wi->icon_h))
) {
if (ic && (
(g.option_iconSrc != ISRC_SIZE
&& g.option_iconSrc != ISRC_SIZE2)
|| (g.option_iconSrc == ISRC_SIZE
&& iconMatchBetter(
ic->src_w, ic->src_h,
wi->icon_w, wi->icon_h,
false))
|| (g.option_iconSrc == ISRC_SIZE2
&& iconMatchBetter(
ic->src_w, ic->src_h,
wi->icon_w, wi->icon_h,
true))
)) {
msg(0, "using file icon for %s\n", tryclass);
if (ic->drawable == None) {
msg(1, "loading content for %s\n", ic->app);
Expand Down Expand Up @@ -446,7 +455,7 @@ int addWindowInfo(Window win, int reclevel, int wm_id, unsigned long desktop,
icon_in_x = addIconFromHints(&(WI));
}
if ((opt == ISRC_FALLBACK && !icon_in_x) ||
opt == ISRC_SIZE || opt == ISRC_FILES)
opt == ISRC_SIZE || opt == ISRC_SIZE2 || opt == ISRC_FILES)
addIconFromFiles(&(WI));

// extract icon width/height/depth
Expand Down

0 comments on commit 313591a

Please sign in to comment.