mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-06-22 18:25:38 +02:00
particles/icon: init
Introduce a new icon particle. It follows the icon spec (https://specifications.freedesktop.org/icon-theme-spec/latest/index.html). Rendering logic is taken from fuzzel (using nanosvg + libpng), while loading logic is taken from sway. Standard usage is with `use-tag = false` which expands the provided string template and then loads the string as the icon name. There are settings to manually override the base paths, themes, etc. The second usage which is required for tray support is a special icon tag that transfers raw pixmaps. With `use-tag = true` it first expands the string, and then uses that output to find an icon pixmap tag. To reduce memory usage, themes are reference counted so they can be passed down the configuration stack without having to load them in multiple times. For programmability, a fallback particle can be specified if no icon/tag is found `fallback: ...`. And the new icon pixmap tag can be existence checked in map conditions using `+{tag_name}`. Future work to be done in follow up diffs: 1. Icon caching. Currently performs an icon lookup on each instantiation & a render on each refresh. 2. Theme caching. Changing theme directories results in a new "theme collection" being created resulting in the possibility of duplicated theme loading.
This commit is contained in:
parent
1a323c6d21
commit
6113f9b94e
51 changed files with 8473 additions and 40 deletions
206
particles/icon.c
Normal file
206
particles/icon.c
Normal file
|
@ -0,0 +1,206 @@
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define LOG_MODULE "particles/icon"
|
||||
#define LOG_ENABLE_DBG 0
|
||||
#include "../config-verify.h"
|
||||
#include "../config.h"
|
||||
#include "../icon.h"
|
||||
#include "../log.h"
|
||||
#include "../particle.h"
|
||||
#include "../plugin.h"
|
||||
#include "../tag.h"
|
||||
|
||||
struct private
|
||||
{
|
||||
char *text;
|
||||
bool use_tag;
|
||||
struct particle *fallback;
|
||||
};
|
||||
|
||||
struct eprivate {
|
||||
struct exposable *exposable;
|
||||
struct icon icon;
|
||||
pixman_image_t *rasterized;
|
||||
};
|
||||
|
||||
static void
|
||||
exposable_destroy(struct exposable *exposable)
|
||||
{
|
||||
struct eprivate *e = exposable->private;
|
||||
if (e->exposable)
|
||||
e->exposable->destroy(e->exposable);
|
||||
|
||||
reset_icon(&e->icon);
|
||||
free(e);
|
||||
exposable_default_destroy(exposable);
|
||||
}
|
||||
|
||||
static int
|
||||
begin_expose(struct exposable *exposable)
|
||||
{
|
||||
struct eprivate *e = exposable->private;
|
||||
|
||||
if (e->icon.type == ICON_NONE) {
|
||||
if (e->exposable) {
|
||||
exposable->width = e->exposable->begin_expose(e->exposable);
|
||||
} else {
|
||||
exposable->width = 0;
|
||||
}
|
||||
} else {
|
||||
assert(e->exposable == NULL);
|
||||
exposable->width = exposable->particle->right_margin + exposable->particle->left_margin;
|
||||
exposable->width += exposable->particle->icon_size;
|
||||
}
|
||||
|
||||
return exposable->width;
|
||||
}
|
||||
|
||||
static void
|
||||
expose(const struct exposable *exposable, pixman_image_t *pix, int x, int y, int height)
|
||||
{
|
||||
exposable_render_deco(exposable, pix, x, y, height);
|
||||
|
||||
const struct eprivate *e = exposable->private;
|
||||
const struct particle *p = exposable->particle;
|
||||
|
||||
int target_size = p->icon_size;
|
||||
double baseline = (double)y + (double)(height - target_size) / 2.0;
|
||||
|
||||
if (e->exposable != NULL) {
|
||||
assert(e->icon.type == ICON_NONE);
|
||||
e->exposable->expose(e->exposable, pix, x, y, height);
|
||||
} else {
|
||||
render_icon(&e->icon, x, baseline, target_size, pix);
|
||||
}
|
||||
}
|
||||
|
||||
static struct exposable *
|
||||
instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||
{
|
||||
struct private *p = (struct private *)particle->private;
|
||||
struct eprivate *e = calloc(1, sizeof(*e));
|
||||
|
||||
char *name = tags_expand_template(p->text, tags);
|
||||
e->icon.type = ICON_NONE;
|
||||
e->exposable = NULL;
|
||||
char *icon_path = NULL;
|
||||
if (strlen(name) == 0) {
|
||||
LOG_WARN("No icon name/tag available");
|
||||
goto fallback;
|
||||
}
|
||||
|
||||
// TODO: An icon cache
|
||||
if (p->use_tag) {
|
||||
const struct icon_tag *tag = icon_tag_for_name(tags, name);
|
||||
|
||||
if (!tag) {
|
||||
LOG_WARN("No icon tag for %s", name);
|
||||
goto fallback;
|
||||
}
|
||||
|
||||
struct icon_pixmaps *pixmaps = tag->pixmaps(tag);
|
||||
icon_from_pixmaps(&e->icon, pixmaps);
|
||||
goto out;
|
||||
}
|
||||
|
||||
int min_size = 0, max_size = 0;
|
||||
icon_path = find_icon(particle->themes->themes, particle->basedirs->basedirs, name, particle->icon_size,
|
||||
particle->icon_themes->strings, &min_size, &max_size);
|
||||
if (icon_path) {
|
||||
int len = strlen(icon_path);
|
||||
if ((icon_path[len - 3] == 's' && icon_from_svg(&e->icon, icon_path))
|
||||
|| (icon_path[len - 3] == 'p' && icon_from_png(&e->icon, icon_path))) {
|
||||
LOG_DBG("Loaded %s", icon_path);
|
||||
goto out;
|
||||
}
|
||||
|
||||
LOG_WARN("Failed to load icon path, not png or svg: %s", icon_path);
|
||||
goto fallback;
|
||||
}
|
||||
LOG_WARN("Icon not found: %s", name);
|
||||
|
||||
fallback:
|
||||
if (p->fallback) {
|
||||
e->exposable = p->fallback->instantiate(p->fallback, tags);
|
||||
assert(e->exposable != NULL);
|
||||
}
|
||||
|
||||
out:
|
||||
free(icon_path);
|
||||
free(name);
|
||||
|
||||
struct exposable *exposable = exposable_common_new(particle, tags);
|
||||
exposable->private = e;
|
||||
exposable->destroy = &exposable_destroy;
|
||||
exposable->begin_expose = &begin_expose;
|
||||
exposable->expose = &expose;
|
||||
return exposable;
|
||||
}
|
||||
|
||||
static void
|
||||
particle_destroy(struct particle *particle)
|
||||
{
|
||||
struct private *p = particle->private;
|
||||
if (p->fallback)
|
||||
p->fallback->destroy(p->fallback);
|
||||
free(p->text);
|
||||
free(p);
|
||||
particle_default_destroy(particle);
|
||||
}
|
||||
|
||||
static struct particle *
|
||||
icon_new(struct particle *common, const char *name, bool use_tag, struct particle *fallback)
|
||||
{
|
||||
struct private *p = calloc(1, sizeof(*p));
|
||||
p->text = strdup(name);
|
||||
p->use_tag = use_tag;
|
||||
p->fallback = fallback;
|
||||
|
||||
common->private = p;
|
||||
common->destroy = &particle_destroy;
|
||||
common->instantiate = &instantiate;
|
||||
return common;
|
||||
}
|
||||
|
||||
static struct particle *
|
||||
from_conf(const struct yml_node *node, struct particle *common)
|
||||
{
|
||||
const struct yml_node *name = yml_get_value(node, "name");
|
||||
const struct yml_node *use_tag_node = yml_get_value(node, "use-tag");
|
||||
const struct yml_node *_fallback = yml_get_value(node, "fallback");
|
||||
|
||||
struct particle *fallback = NULL;
|
||||
bool use_tag = false;
|
||||
|
||||
if (use_tag_node)
|
||||
use_tag = yml_value_as_bool(use_tag_node);
|
||||
if (_fallback)
|
||||
fallback = conf_to_particle(_fallback, (struct conf_inherit){common->font, common->font_shaping, common->themes,
|
||||
common->basedirs, common->icon_themes,
|
||||
common->icon_size, common->foreground});
|
||||
|
||||
return icon_new(common, yml_value_as_string(name), use_tag, fallback);
|
||||
}
|
||||
|
||||
static bool
|
||||
verify_conf(keychain_t *chain, const struct yml_node *node)
|
||||
{
|
||||
static const struct attr_info attrs[] = {
|
||||
{"name", true, &conf_verify_string},
|
||||
{"use-tag", false, &conf_verify_bool},
|
||||
{"fallback", false, &conf_verify_particle},
|
||||
PARTICLE_COMMON_ATTRS,
|
||||
};
|
||||
|
||||
return conf_verify_dict(chain, node, attrs);
|
||||
}
|
||||
|
||||
const struct particle_iface particle_icon_iface = {
|
||||
.verify_conf = &verify_conf,
|
||||
.from_conf = &from_conf,
|
||||
};
|
||||
|
||||
#if defined(CORE_PLUGINS_AS_SHARED_LIBRARIES)
|
||||
extern const struct particle_iface iface __attribute__((weak, alias("particle_icon_iface")));
|
||||
#endif
|
|
@ -190,8 +190,9 @@ from_conf(const struct yml_node *node, struct particle *common)
|
|||
|
||||
size_t idx = 0;
|
||||
for (struct yml_list_iter it = yml_list_iter(items); it.node != NULL; yml_list_next(&it), idx++) {
|
||||
parts[idx]
|
||||
= conf_to_particle(it.node, (struct conf_inherit){common->font, common->font_shaping, common->foreground});
|
||||
parts[idx] = conf_to_particle(it.node, (struct conf_inherit){common->font, common->font_shaping, common->themes,
|
||||
common->basedirs, common->icon_themes,
|
||||
common->icon_size, common->foreground});
|
||||
}
|
||||
|
||||
return particle_list_new(common, parts, count, left_spacing, right_spacing);
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
#include "tag.h"
|
||||
#include "yml.h"
|
||||
#include <assert.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -85,6 +87,11 @@ str_condition(const char *tag_value, const char *cond_value, enum map_op op)
|
|||
static bool
|
||||
eval_comparison(const struct map_condition *map_cond, const struct tag_set *tags)
|
||||
{
|
||||
if (map_cond->op == MAP_OP_ICON_TAG) {
|
||||
// Do the fallback check if MAP_OP_SELF for if an icon tag exists.
|
||||
return icon_tag_for_name(tags, map_cond->tag) != NULL;
|
||||
}
|
||||
|
||||
const struct tag *tag = tag_for_name(tags, map_cond->tag);
|
||||
if (tag == NULL) {
|
||||
LOG_WARN("tag %s not found", map_cond->tag);
|
||||
|
@ -170,6 +177,7 @@ free_map_condition(struct map_condition *c)
|
|||
free(c->value);
|
||||
/* FALLTHROUGH */
|
||||
case MAP_OP_SELF:
|
||||
case MAP_OP_ICON_TAG:
|
||||
free(c->tag);
|
||||
break;
|
||||
case MAP_OP_AND:
|
||||
|
@ -374,8 +382,15 @@ from_conf(const struct yml_node *node, struct particle *common)
|
|||
|
||||
struct particle_map particle_map[yml_dict_length(conditions)];
|
||||
|
||||
struct conf_inherit inherited
|
||||
= {.font = common->font, .font_shaping = common->font_shaping, .foreground = common->foreground};
|
||||
struct conf_inherit inherited = {
|
||||
.font = common->font,
|
||||
.font_shaping = common->font_shaping,
|
||||
.foreground = common->foreground,
|
||||
.basedirs = common->basedirs,
|
||||
.themes = common->themes,
|
||||
.icon_themes = common->icon_themes,
|
||||
.icon_size = common->icon_size,
|
||||
};
|
||||
|
||||
size_t idx = 0;
|
||||
for (struct yml_dict_iter it = yml_dict_iter(conditions); it.key != NULL; yml_dict_next(&it), idx++) {
|
||||
|
|
|
@ -8,6 +8,7 @@ enum map_op {
|
|||
MAP_OP_GE,
|
||||
MAP_OP_GT,
|
||||
MAP_OP_SELF,
|
||||
MAP_OP_ICON_TAG,
|
||||
MAP_OP_NOT,
|
||||
|
||||
MAP_OP_AND,
|
||||
|
|
|
@ -72,6 +72,7 @@ void yyerror(const char *s);
|
|||
&& yylval.op = MAP_OP_AND; return BOOL_OP;
|
||||
\|\| yylval.op = MAP_OP_OR; return BOOL_OP;
|
||||
~ return NOT;
|
||||
\+ return AT;
|
||||
\( return L_PAR;
|
||||
\) return R_PAR;
|
||||
[ \t\n] ;
|
||||
|
|
|
@ -21,7 +21,7 @@ void yyerror(const char *str);
|
|||
enum map_op op;
|
||||
}
|
||||
|
||||
%token WORD STRING CMP_OP L_PAR R_PAR
|
||||
%token WORD STRING CMP_OP L_PAR R_PAR AT
|
||||
%left BOOL_OP
|
||||
%precedence NOT
|
||||
|
||||
|
@ -33,29 +33,35 @@ void yyerror(const char *str);
|
|||
result: condition { MAP_CONDITION_PARSE_RESULT = $<condition>1; };
|
||||
|
||||
condition:
|
||||
AT WORD {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>2;
|
||||
$<condition>$->op = MAP_OP_ICON_TAG;
|
||||
}
|
||||
|
|
||||
WORD {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = MAP_OP_SELF;
|
||||
}
|
||||
|
|
||||
WORD CMP_OP WORD {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = $<op>2;
|
||||
$<condition>$->value = $<str>3;
|
||||
$<condition>$->value = $<str>3;
|
||||
}
|
||||
|
|
||||
WORD CMP_OP STRING {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->tag = $<str>1;
|
||||
$<condition>$->op = $<op>2;
|
||||
$<condition>$->value = $<str>3;
|
||||
$<condition>$->value = $<str>3;
|
||||
}
|
||||
|
|
||||
L_PAR condition R_PAR { $<condition>$ = $<condition>2; }
|
||||
|
|
||||
NOT condition {
|
||||
NOT condition {
|
||||
$<condition>$ = malloc(sizeof(struct map_condition));
|
||||
$<condition>$->cond1 = $<condition>2;
|
||||
$<condition>$->op = MAP_OP_NOT;
|
||||
|
@ -84,6 +90,7 @@ token_to_str(yysymbol_kind_t tkn)
|
|||
case YYSYMBOL_L_PAR: return "(";
|
||||
case YYSYMBOL_R_PAR: return ")";
|
||||
case YYSYMBOL_NOT: return "~";
|
||||
case YYSYMBOL_AT: return "+";
|
||||
default: return yysymbol_name(tkn);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,7 +20,7 @@ pfiles = pgen.process('map.y')
|
|||
|
||||
map_parser = declare_dependency(sources: [pfiles, lfiles], include_directories: '.')
|
||||
|
||||
particle_sdk = declare_dependency(dependencies: [pixman, tllist, fcft])
|
||||
particle_sdk = declare_dependency(dependencies: [pixman, tllist, fcft, nanosvg])
|
||||
|
||||
dynlist_lib = build_target(
|
||||
'dynlist', 'dynlist.c', 'dynlist.h', dependencies: particle_sdk,
|
||||
|
@ -40,6 +40,7 @@ deps = {
|
|||
'progress-bar': [],
|
||||
'ramp': [],
|
||||
'string': [],
|
||||
'icon': [],
|
||||
}
|
||||
|
||||
particles = []
|
||||
|
|
|
@ -296,6 +296,10 @@ from_conf(const struct yml_node *node, struct particle *common)
|
|||
.font = common->font,
|
||||
.font_shaping = common->font_shaping,
|
||||
.foreground = common->foreground,
|
||||
.basedirs = common->basedirs,
|
||||
.themes = common->themes,
|
||||
.icon_themes = common->icon_themes,
|
||||
.icon_size = common->icon_size,
|
||||
};
|
||||
|
||||
return progress_bar_new(common, yml_value_as_string(tag), yml_value_as_int(length),
|
||||
|
|
|
@ -199,8 +199,9 @@ from_conf(const struct yml_node *node, struct particle *common)
|
|||
|
||||
size_t idx = 0;
|
||||
for (struct yml_list_iter it = yml_list_iter(items); it.node != NULL; yml_list_next(&it), idx++) {
|
||||
parts[idx]
|
||||
= conf_to_particle(it.node, (struct conf_inherit){common->font, common->font_shaping, common->foreground});
|
||||
parts[idx] = conf_to_particle(it.node, (struct conf_inherit){common->font, common->font_shaping, common->themes,
|
||||
common->basedirs, common->icon_themes,
|
||||
common->icon_size, common->foreground});
|
||||
}
|
||||
|
||||
long min_v = min != NULL ? yml_value_as_int(min) : 0;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue