particle/string: expose info through the new struct particle_info struct

This commit is contained in:
Daniel Eklöf 2019-01-13 10:49:10 +01:00
parent 9f8000b047
commit 7b98ea2b7c
6 changed files with 62 additions and 60 deletions

View file

@ -14,6 +14,7 @@
#include "particles/map.h" #include "particles/map.h"
#include "particles/progress-bar.h" #include "particles/progress-bar.h"
#include "particles/ramp.h" #include "particles/ramp.h"
#include "particles/string.h"
const char * const char *
conf_err_prefix(const keychain_t *chain, const struct yml_node *node) conf_err_prefix(const keychain_t *chain, const struct yml_node *node)
@ -164,8 +165,6 @@ conf_verify_font(keychain_t *chain, const struct yml_node *node)
return conf_verify_dict(chain, node, attrs, sizeof(attrs) / sizeof(attrs[0])); return conf_verify_dict(chain, node, attrs, sizeof(attrs) / sizeof(attrs[0]));
} }
static bool verify_decoration(keychain_t *chain, const struct yml_node *node);
static bool static bool
verify_decoration_stack(keychain_t *chain, const struct yml_node *node) verify_decoration_stack(keychain_t *chain, const struct yml_node *node)
{ {
@ -178,15 +177,15 @@ verify_decoration_stack(keychain_t *chain, const struct yml_node *node)
it.node != NULL; it.node != NULL;
yml_list_next(&it)) yml_list_next(&it))
{ {
if (!verify_decoration(chain, it.node)) if (!conf_verify_decoration(chain, it.node))
return false; return false;
} }
return true; return true;
} }
static bool bool
verify_decoration(keychain_t *chain, const struct yml_node *node) conf_verify_decoration(keychain_t *chain, const struct yml_node *node)
{ {
assert(yml_is_dict(node)); assert(yml_is_dict(node));
@ -292,15 +291,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
{"right-margin", false, &conf_verify_int}, \ {"right-margin", false, &conf_verify_int}, \
{"on-click", false, &conf_verify_string}, {"on-click", false, &conf_verify_string},
static const struct attr_info string[] = {
{"text", true, &conf_verify_string},
{"max", false, &conf_verify_int},
{"font", false, &conf_verify_font},
{"foreground", false, &conf_verify_color},
{"deco", false, &verify_decoration},
COMMON_ATTRS
};
#undef COMMON_ATTRS #undef COMMON_ATTRS
static const struct { static const struct {
@ -312,6 +302,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
{"map", &particle_map}, {"map", &particle_map},
{"progress-bar", &particle_progress_bar}, {"progress-bar", &particle_progress_bar},
{"ramp", &particle_ramp}, {"ramp", &particle_ramp},
{"string", &particle_string},
}; };
static const struct { static const struct {
@ -319,7 +310,6 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
const struct attr_info *attrs; const struct attr_info *attrs;
size_t count; size_t count;
} particles[] = { } particles[] = {
{"string", string, sizeof(string) / sizeof(string[0])},
}; };
for (size_t i = 0; i < sizeof(particles_v2) / sizeof(particles_v2[0]); i++) { for (size_t i = 0; i < sizeof(particles_v2) / sizeof(particles_v2[0]); i++) {

View file

@ -43,3 +43,5 @@ bool conf_verify_font(keychain_t *chain, const struct yml_node *node);
bool conf_verify_particle(keychain_t *chain, const struct yml_node *node); bool conf_verify_particle(keychain_t *chain, const struct yml_node *node);
bool conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node); bool conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node);
bool conf_verify_decoration(keychain_t *chain, const struct yml_node *node);

View file

@ -47,10 +47,14 @@ hex_byte(const char hex[2])
return upper << 4 | lower; return upper << 4 | lower;
} }
static struct rgba struct rgba
color_from_hexstr(const char *hex) conf_to_color(const struct yml_node *node)
{ {
const char *hex = yml_value_as_string(node);
assert(hex != NULL);
assert(strlen(hex) == 8); assert(strlen(hex) == 8);
uint8_t red = hex_byte(&hex[0]); uint8_t red = hex_byte(&hex[0]);
uint8_t green = hex_byte(&hex[2]); uint8_t green = hex_byte(&hex[2]);
uint8_t blue = hex_byte(&hex[4]); uint8_t blue = hex_byte(&hex[4]);
@ -71,8 +75,8 @@ color_from_hexstr(const char *hex)
return rgba; return rgba;
} }
static struct font * struct font *
font_from_config(const struct yml_node *node) conf_to_font(const struct yml_node *node)
{ {
const struct yml_node *family = yml_get_value(node, "family"); const struct yml_node *family = yml_get_value(node, "family");
return font_new(family != NULL ? yml_value_as_string(family) : "monospace"); return font_new(family != NULL ? yml_value_as_string(family) : "monospace");
@ -82,7 +86,7 @@ static struct deco *
deco_background_from_config(const struct yml_node *node) deco_background_from_config(const struct yml_node *node)
{ {
const struct yml_node *color = yml_get_value(node, "color"); const struct yml_node *color = yml_get_value(node, "color");
return deco_background(color_from_hexstr(yml_value_as_string(color))); return deco_background(conf_to_color(color));
} }
static struct deco * static struct deco *
@ -90,10 +94,7 @@ deco_underline_from_config(const struct yml_node *node)
{ {
const struct yml_node *size = yml_get_value(node, "size"); const struct yml_node *size = yml_get_value(node, "size");
const struct yml_node *color = yml_get_value(node, "color"); const struct yml_node *color = yml_get_value(node, "color");
return deco_underline(yml_value_as_int(size), conf_to_color(color));
return deco_underline(
yml_value_as_int(size),
color_from_hexstr(yml_value_as_string(color)));
} }
static struct deco *deco_from_config(const struct yml_node *node); static struct deco *deco_from_config(const struct yml_node *node);
@ -137,30 +138,6 @@ deco_from_config(const struct yml_node *node)
return NULL; return NULL;
} }
static struct particle *
particle_string_from_config(const struct yml_node *node,
const struct font *parent_font,
int left_margin, int right_margin,
const char *on_click_template)
{
const struct yml_node *text = yml_get_value(node, "text");
const struct yml_node *max = yml_get_value(node, "max");
const struct yml_node *font = yml_get_value(node, "font");
const struct yml_node *foreground = yml_get_value(node, "foreground");
struct rgba fg_color = foreground != NULL
? color_from_hexstr(yml_value_as_string(foreground)) :
(struct rgba){1.0, 1.0, 1.0, 1.0};
return particle_string_new(
yml_value_as_string(text),
max != NULL ? yml_value_as_int(max) : 0,
font != NULL ? font_from_config(font) : font_clone(parent_font),
fg_color, left_margin, right_margin, on_click_template);
}
static struct particle * static struct particle *
particle_simple_list_from_config(const struct yml_node *node, particle_simple_list_from_config(const struct yml_node *node,
const struct font *parent_font) const struct font *parent_font)
@ -217,9 +194,8 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font)
else if (strcmp(type, "ramp") == 0) else if (strcmp(type, "ramp") == 0)
ret = particle_ramp.from_conf( ret = particle_ramp.from_conf(
pair.value, parent_font, left, right, on_click_template); pair.value, parent_font, left, right, on_click_template);
else if (strcmp(type, "string") == 0) else if (strcmp(type, "string") == 0)
ret = particle_string_from_config( ret = particle_string.from_conf(
pair.value, parent_font, left, right, on_click_template); pair.value, parent_font, left, right, on_click_template);
else else
assert(false); assert(false);
@ -252,7 +228,7 @@ conf_to_bar(const struct yml_node *bar)
? BAR_TOP : BAR_BOTTOM; ? BAR_TOP : BAR_BOTTOM;
const struct yml_node *background = yml_get_value(bar, "background"); const struct yml_node *background = yml_get_value(bar, "background");
conf.background = color_from_hexstr(yml_value_as_string(background)); conf.background = conf_to_color(background);
/* /*
* Optional attributes * Optional attributes
@ -291,7 +267,7 @@ conf_to_bar(const struct yml_node *bar)
conf.border.width = yml_value_as_int(width); conf.border.width = yml_value_as_int(width);
if (color != NULL) if (color != NULL)
conf.border.color = color_from_hexstr(yml_value_as_string(color)); conf.border.color = conf_to_color(color);
} }
/* Create a default font */ /* Create a default font */
@ -300,7 +276,7 @@ conf_to_bar(const struct yml_node *bar)
const struct yml_node *font_node = yml_get_value(bar, "font"); const struct yml_node *font_node = yml_get_value(bar, "font");
if (font_node != NULL) { if (font_node != NULL) {
font_destroy(font); font_destroy(font);
font = font_from_config(font_node); font = conf_to_font(font_node);
} }
const struct yml_node *left = yml_get_value(bar, "left"); const struct yml_node *left = yml_get_value(bar, "left");

View file

@ -12,5 +12,8 @@ struct bar *conf_to_bar(const struct yml_node *bar);
* Utility functions, for e.g. modules * Utility functions, for e.g. modules
*/ */
struct rgba conf_to_color(const struct yml_node *node);
struct font *conf_to_font(const struct yml_node *node);
struct particle * conf_to_particle( struct particle * conf_to_particle(
const struct yml_node *node, const struct font *parent_font); const struct yml_node *node, const struct font *parent_font);

View file

@ -7,6 +7,7 @@
#define LOG_MODULE "string" #define LOG_MODULE "string"
#define LOG_ENABLE_DBG 1 #define LOG_ENABLE_DBG 1
#include "../log.h" #include "../log.h"
#include "../config.h"
struct private { struct private {
char *text; char *text;
@ -130,10 +131,10 @@ particle_destroy(struct particle *particle)
particle_default_destroy(particle); particle_default_destroy(particle);
} }
struct particle * static struct particle *
particle_string_new(const char *text, size_t max_len, struct font *font, string_new(const char *text, size_t max_len, struct font *font,
struct rgba foreground, int left_margin, int right_margin, struct rgba foreground, int left_margin, int right_margin,
const char *on_click_template) const char *on_click_template)
{ {
struct private *p = malloc(sizeof(*p)); struct private *p = malloc(sizeof(*p));
p->text = strdup(text); p->text = strdup(text);
@ -150,3 +151,35 @@ particle_string_new(const char *text, size_t max_len, struct font *font,
return particle; return particle;
} }
static struct particle *
from_conf(const struct yml_node *node, const struct font *parent_font,
int left_margin, int right_margin, const char *on_click_template)
{
const struct yml_node *text = yml_get_value(node, "text");
const struct yml_node *max = yml_get_value(node, "max");
const struct yml_node *font = yml_get_value(node, "font");
const struct yml_node *foreground = yml_get_value(node, "foreground");
struct rgba fg_color = foreground != NULL
? conf_to_color(foreground) : (struct rgba){1.0, 1.0, 1.0, 1.0};
return string_new(
yml_value_as_string(text),
max != NULL ? yml_value_as_int(max) : 0,
font != NULL ? conf_to_font(font) : font_clone(parent_font),
fg_color, left_margin, right_margin, on_click_template);
}
const struct particle_info particle_string = {
.from_conf = &from_conf,
.attr_count = PARTICLE_COMMON_ATTRS_COUNT + 5,
.attrs = {
{"text", true, &conf_verify_string},
{"max", false, &conf_verify_int},
{"font", false, &conf_verify_font},
{"foreground", false, &conf_verify_color},
{"deco", false, &conf_verify_decoration},
PARTICLE_COMMON_ATTRS,
},
};

View file

@ -1,6 +1,4 @@
#pragma once #pragma once
#include "../particle.h" #include "../particle.h"
struct particle *particle_string_new( extern const struct particle_info particle_string;
const char *text, size_t max_len, struct font *font, struct rgba foreground,
int left_margin, int right_margin, const char *on_click_template);