forked from external/yambar
particle/map: expose info through the new struct particle_info struct
This commit is contained in:
parent
6379b1939f
commit
73b8bf1346
4 changed files with 88 additions and 84 deletions
|
@ -11,6 +11,7 @@
|
||||||
|
|
||||||
#include "particles/empty.h"
|
#include "particles/empty.h"
|
||||||
#include "particles/list.h"
|
#include "particles/list.h"
|
||||||
|
#include "particles/map.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)
|
||||||
|
@ -262,35 +263,6 @@ conf_verify_particle_list_items(keychain_t *chain, const struct yml_node *node)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool
|
|
||||||
verify_map_values(keychain_t *chain, const struct yml_node *node)
|
|
||||||
{
|
|
||||||
if (!yml_is_dict(node)) {
|
|
||||||
LOG_ERR(
|
|
||||||
"%s: must be a dictionary of workspace-name: particle mappings",
|
|
||||||
conf_err_prefix(chain, node));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (struct yml_dict_iter it = yml_dict_iter(node);
|
|
||||||
it.key != NULL;
|
|
||||||
yml_dict_next(&it))
|
|
||||||
{
|
|
||||||
const char *key = yml_value_as_string(it.key);
|
|
||||||
if (key == NULL) {
|
|
||||||
LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key));
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!conf_verify_particle(chain_push(chain, key), it.value))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
chain_pop(chain);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
static bool
|
static bool
|
||||||
conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
|
conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
|
||||||
{
|
{
|
||||||
|
@ -318,13 +290,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 map[] = {
|
|
||||||
{"tag", true, &conf_verify_string},
|
|
||||||
{"values", true, &verify_map_values},
|
|
||||||
{"default", false, &conf_verify_particle},
|
|
||||||
COMMON_ATTRS
|
|
||||||
};
|
|
||||||
|
|
||||||
static const struct attr_info progress_bar[] = {
|
static const struct attr_info progress_bar[] = {
|
||||||
{"tag", true, &conf_verify_string},
|
{"tag", true, &conf_verify_string},
|
||||||
{"length", true, &conf_verify_int},
|
{"length", true, &conf_verify_int},
|
||||||
|
@ -360,6 +325,7 @@ conf_verify_particle_dictionary(keychain_t *chain, const struct yml_node *node)
|
||||||
} particles_v2[] = {
|
} particles_v2[] = {
|
||||||
{"empty", &particle_empty},
|
{"empty", &particle_empty},
|
||||||
{"list", &particle_list},
|
{"list", &particle_list},
|
||||||
|
{"map", &particle_map},
|
||||||
};
|
};
|
||||||
|
|
||||||
static const struct {
|
static const struct {
|
||||||
|
@ -367,7 +333,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[] = {
|
||||||
{"map", map, sizeof(map) / sizeof(map[0])},
|
|
||||||
{"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])},
|
{"progress-bar", progress_bar, sizeof(progress_bar) / sizeof(progress_bar[0])},
|
||||||
{"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])},
|
{"ramp", ramp, sizeof(ramp) / sizeof(ramp[0])},
|
||||||
{"string", string, sizeof(string) / sizeof(string[0])},
|
{"string", string, sizeof(string) / sizeof(string[0])},
|
||||||
|
|
36
config.c
36
config.c
|
@ -161,36 +161,6 @@ particle_string_from_config(const struct yml_node *node,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static struct particle *
|
|
||||||
particle_map_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 *tag = yml_get_value(node, "tag");
|
|
||||||
const struct yml_node *values = yml_get_value(node, "values");
|
|
||||||
const struct yml_node *def = yml_get_value(node, "default");
|
|
||||||
|
|
||||||
struct particle_map particle_map[yml_dict_length(values)];
|
|
||||||
|
|
||||||
size_t idx = 0;
|
|
||||||
for (struct yml_dict_iter it = yml_dict_iter(values);
|
|
||||||
it.key != NULL;
|
|
||||||
yml_dict_next(&it), idx++)
|
|
||||||
{
|
|
||||||
particle_map[idx].tag_value = yml_value_as_string(it.key);
|
|
||||||
particle_map[idx].particle = conf_to_particle(it.value, parent_font);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct particle *default_particle = def != NULL
|
|
||||||
? conf_to_particle(def, parent_font)
|
|
||||||
: NULL;
|
|
||||||
|
|
||||||
return particle_map_new(
|
|
||||||
yml_value_as_string(tag), particle_map, yml_dict_length(values),
|
|
||||||
default_particle, left_margin, right_margin, on_click_template);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct particle *
|
static struct particle *
|
||||||
particle_ramp_from_config(const struct yml_node *node,
|
particle_ramp_from_config(const struct yml_node *node,
|
||||||
const struct font *parent_font,
|
const struct font *parent_font,
|
||||||
|
@ -288,13 +258,13 @@ conf_to_particle(const struct yml_node *node, const struct font *parent_font)
|
||||||
else if (strcmp(type, "list") == 0)
|
else if (strcmp(type, "list") == 0)
|
||||||
ret = particle_list.from_conf(
|
ret = particle_list.from_conf(
|
||||||
pair.value, parent_font, left, right, on_click_template);
|
pair.value, parent_font, left, right, on_click_template);
|
||||||
|
else if (strcmp(type, "map") == 0)
|
||||||
|
ret = particle_map.from_conf(
|
||||||
|
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_config(
|
||||||
pair.value, parent_font, left, right, on_click_template);
|
pair.value, parent_font, left, right, on_click_template);
|
||||||
else if (strcmp(type, "map") == 0)
|
|
||||||
ret = particle_map_from_config(
|
|
||||||
pair.value, parent_font, left, right, on_click_template);
|
|
||||||
else if (strcmp(type, "ramp") == 0)
|
else if (strcmp(type, "ramp") == 0)
|
||||||
ret = particle_ramp_from_config(
|
ret = particle_ramp_from_config(
|
||||||
pair.value, parent_font, left, right, on_click_template);
|
pair.value, parent_font, left, right, on_click_template);
|
||||||
|
|
|
@ -4,6 +4,15 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
|
#define LOG_MODULE "map"
|
||||||
|
#include "../log.h"
|
||||||
|
#include "../config.h"
|
||||||
|
|
||||||
|
struct particle_map {
|
||||||
|
const char *tag_value;
|
||||||
|
struct particle *particle;
|
||||||
|
};
|
||||||
|
|
||||||
struct private {
|
struct private {
|
||||||
char *tag;
|
char *tag;
|
||||||
struct particle *default_particle;
|
struct particle *default_particle;
|
||||||
|
@ -136,11 +145,11 @@ particle_destroy(struct particle *particle)
|
||||||
particle_default_destroy(particle);
|
particle_default_destroy(particle);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct particle *
|
static struct particle *
|
||||||
particle_map_new(const char *tag, const struct particle_map *particle_map,
|
map_new(const char *tag, const struct particle_map *particle_map,
|
||||||
size_t count, struct particle *default_particle,
|
size_t count, struct particle *default_particle,
|
||||||
int left_margin, int right_margin,
|
int left_margin, int right_margin,
|
||||||
const char *on_click_template)
|
const char *on_click_template)
|
||||||
{
|
{
|
||||||
struct particle *particle = particle_common_new(
|
struct particle *particle = particle_common_new(
|
||||||
left_margin, right_margin, on_click_template);
|
left_margin, right_margin, on_click_template);
|
||||||
|
@ -161,3 +170,71 @@ particle_map_new(const char *tag, const struct particle_map *particle_map,
|
||||||
particle->private = priv;
|
particle->private = priv;
|
||||||
return particle;
|
return particle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool
|
||||||
|
verify_map_values(keychain_t *chain, const struct yml_node *node)
|
||||||
|
{
|
||||||
|
if (!yml_is_dict(node)) {
|
||||||
|
LOG_ERR(
|
||||||
|
"%s: must be a dictionary of workspace-name: particle mappings",
|
||||||
|
conf_err_prefix(chain, node));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (struct yml_dict_iter it = yml_dict_iter(node);
|
||||||
|
it.key != NULL;
|
||||||
|
yml_dict_next(&it))
|
||||||
|
{
|
||||||
|
const char *key = yml_value_as_string(it.key);
|
||||||
|
if (key == NULL) {
|
||||||
|
LOG_ERR("%s: key must be a string", conf_err_prefix(chain, it.key));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!conf_verify_particle(chain_push(chain, key), it.value))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
chain_pop(chain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 *tag = yml_get_value(node, "tag");
|
||||||
|
const struct yml_node *values = yml_get_value(node, "values");
|
||||||
|
const struct yml_node *def = yml_get_value(node, "default");
|
||||||
|
|
||||||
|
struct particle_map particle_map[yml_dict_length(values)];
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
for (struct yml_dict_iter it = yml_dict_iter(values);
|
||||||
|
it.key != NULL;
|
||||||
|
yml_dict_next(&it), idx++)
|
||||||
|
{
|
||||||
|
particle_map[idx].tag_value = yml_value_as_string(it.key);
|
||||||
|
particle_map[idx].particle = conf_to_particle(it.value, parent_font);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct particle *default_particle = def != NULL
|
||||||
|
? conf_to_particle(def, parent_font)
|
||||||
|
: NULL;
|
||||||
|
|
||||||
|
return map_new(
|
||||||
|
yml_value_as_string(tag), particle_map, yml_dict_length(values),
|
||||||
|
default_particle, left_margin, right_margin, on_click_template);
|
||||||
|
}
|
||||||
|
|
||||||
|
const struct particle_info particle_map = {
|
||||||
|
.from_conf = &from_conf,
|
||||||
|
.attr_count = PARTICLE_COMMON_ATTRS_COUNT + 3,
|
||||||
|
.attrs = {
|
||||||
|
{"tag", true, &conf_verify_string},
|
||||||
|
{"values", true, &verify_map_values},
|
||||||
|
{"default", false, &conf_verify_particle},
|
||||||
|
PARTICLE_COMMON_ATTRS,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
|
@ -1,12 +1,4 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include "../particle.h"
|
#include "../particle.h"
|
||||||
|
|
||||||
struct particle_map {
|
extern const struct particle_info particle_map;
|
||||||
const char *tag_value;
|
|
||||||
struct particle *particle;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct particle *particle_map_new(
|
|
||||||
const char *tag, const struct particle_map *particle_map, size_t count,
|
|
||||||
struct particle *default_particle, int left_margin, int right_margin,
|
|
||||||
const char *on_click_template);
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue