mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-23 04:25:42 +02:00
particle/ramp: new particle, selects one particle from a list
The ramp particle is configured with a tag name and a list of particles. At instantiation time, a single particle is selected from the list, depending on the tag value and it's minimum and maximum values. I.e. this particle acts kind of like a progress bar.
This commit is contained in:
parent
2218bd1583
commit
47b36bdd35
4 changed files with 101 additions and 3 deletions
|
@ -30,6 +30,7 @@ add_executable(f00bar
|
||||||
particles/string.c particles/string.h
|
particles/string.c particles/string.h
|
||||||
particles/list.c particles/list.h
|
particles/list.c particles/list.h
|
||||||
particles/map.c particles/map.h
|
particles/map.c particles/map.h
|
||||||
|
particles/ramp.c particles/ramp.h
|
||||||
|
|
||||||
modules/battery/battery.c modules/battery/battery.h
|
modules/battery/battery.c modules/battery/battery.h
|
||||||
modules/i3/i3.c modules/i3/i3.h
|
modules/i3/i3.c modules/i3/i3.h
|
||||||
|
|
30
config.c
30
config.c
|
@ -11,6 +11,7 @@
|
||||||
#include "particles/string.h"
|
#include "particles/string.h"
|
||||||
#include "particles/list.h"
|
#include "particles/list.h"
|
||||||
#include "particles/map.h"
|
#include "particles/map.h"
|
||||||
|
#include "particles/ramp.h"
|
||||||
|
|
||||||
#include "module.h"
|
#include "module.h"
|
||||||
#include "modules/battery/battery.h"
|
#include "modules/battery/battery.h"
|
||||||
|
@ -168,10 +169,8 @@ particle_list_from_config(const struct yml_node *node,
|
||||||
parts[idx] = particle_from_config(it.node, parent_font);
|
parts[idx] = particle_from_config(it.node, parent_font);
|
||||||
}
|
}
|
||||||
|
|
||||||
struct particle *list = particle_list_new(
|
return particle_list_new(
|
||||||
parts, count, left_spacing, right_spacing, left_margin, right_margin);
|
parts, count, left_spacing, right_spacing, left_margin, right_margin);
|
||||||
|
|
||||||
return list;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct particle *
|
static struct particle *
|
||||||
|
@ -205,6 +204,29 @@ particle_map_from_config(const struct yml_node *node, const struct font *parent_
|
||||||
default_particle);
|
default_particle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static struct particle *
|
||||||
|
particle_ramp_from_config(const struct yml_node *node, const struct font *parent_font)
|
||||||
|
{
|
||||||
|
const struct yml_node *tag = yml_get_value(node, "tag");
|
||||||
|
const struct yml_node *items = yml_get_value(node, "items");
|
||||||
|
|
||||||
|
assert(yml_is_scalar(tag));
|
||||||
|
assert(yml_is_list(items));
|
||||||
|
|
||||||
|
size_t count = yml_list_length(items);
|
||||||
|
struct particle *parts[count];
|
||||||
|
|
||||||
|
size_t idx = 0;
|
||||||
|
for (struct yml_list_iter it = yml_list_iter(items);
|
||||||
|
it.node != NULL;
|
||||||
|
yml_list_next(&it), idx++)
|
||||||
|
{
|
||||||
|
parts[idx] = particle_from_config(it.node, parent_font);
|
||||||
|
}
|
||||||
|
|
||||||
|
return particle_ramp_new(yml_value_as_string(tag), parts, count);
|
||||||
|
}
|
||||||
|
|
||||||
static struct particle *
|
static struct particle *
|
||||||
particle_from_config(const struct yml_node *node, const struct font *parent_font)
|
particle_from_config(const struct yml_node *node, const struct font *parent_font)
|
||||||
{
|
{
|
||||||
|
@ -220,6 +242,8 @@ particle_from_config(const struct yml_node *node, const struct font *parent_font
|
||||||
return particle_list_from_config(pair.value, parent_font);
|
return particle_list_from_config(pair.value, parent_font);
|
||||||
else if (strcmp(type, "map") == 0)
|
else if (strcmp(type, "map") == 0)
|
||||||
return particle_map_from_config(pair.value, parent_font);
|
return particle_map_from_config(pair.value, parent_font);
|
||||||
|
else if (strcmp(type, "ramp") == 0)
|
||||||
|
return particle_ramp_from_config(pair.value, parent_font);
|
||||||
else
|
else
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
68
particles/ramp.c
Normal file
68
particles/ramp.c
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
#include "ramp.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
struct ramp {
|
||||||
|
char *tag;
|
||||||
|
struct particle **particles;
|
||||||
|
size_t count;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
particle_destroy(struct particle *particle)
|
||||||
|
{
|
||||||
|
struct ramp *ramp = particle->private;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < ramp->count; i++)
|
||||||
|
ramp->particles[i]->destroy(ramp->particles[i]);
|
||||||
|
|
||||||
|
free(ramp->tag);
|
||||||
|
free(ramp->particles);
|
||||||
|
free(ramp);
|
||||||
|
free(particle);
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct exposable *
|
||||||
|
instantiate(const struct particle *particle, const struct tag_set *tags)
|
||||||
|
{
|
||||||
|
const struct ramp *ramp = particle->private;
|
||||||
|
const struct tag *tag = tag_for_name(tags, ramp->tag);
|
||||||
|
assert(tag != NULL);
|
||||||
|
|
||||||
|
long value = tag->as_int(tag);
|
||||||
|
long min = tag->min(tag);
|
||||||
|
long max = tag->max(tag);
|
||||||
|
|
||||||
|
double progress = (double)value / (max - min);
|
||||||
|
|
||||||
|
size_t idx = progress * ramp->count;
|
||||||
|
printf("value: %lu, min: %lu, max: %lu, progress: %f, idx: %zu\n",
|
||||||
|
value, min, max, progress, idx);
|
||||||
|
assert(idx >= 0 && idx < ramp->count);
|
||||||
|
|
||||||
|
struct particle *p = ramp->particles[idx];
|
||||||
|
return p->instantiate(p, tags);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct particle *
|
||||||
|
particle_ramp_new(const char *tag, struct particle *particles[], size_t count)
|
||||||
|
{
|
||||||
|
struct particle *particle = particle_common_new(0, 0);
|
||||||
|
particle->destroy = &particle_destroy;
|
||||||
|
particle->instantiate = &instantiate;
|
||||||
|
|
||||||
|
struct ramp *ramp = malloc(sizeof(*ramp));
|
||||||
|
ramp->tag = strdup(tag);
|
||||||
|
ramp->particles = calloc(count, sizeof(ramp->particles[0]));
|
||||||
|
ramp->count = count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++)
|
||||||
|
ramp->particles[i] = particles[i];
|
||||||
|
|
||||||
|
particle->private = ramp;
|
||||||
|
return particle;
|
||||||
|
}
|
5
particles/ramp.h
Normal file
5
particles/ramp.h
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
#include "../particle.h"
|
||||||
|
|
||||||
|
struct particle *particle_ramp_new(
|
||||||
|
const char *tag, struct particle *particles[], size_t count);
|
Loading…
Add table
Reference in a new issue