mirror of
https://codeberg.org/dnkl/yambar.git
synced 2025-04-20 03:35:41 +02:00
decoration: stack: stacks multiple decorations on top of each other
This commit is contained in:
parent
09cd27b688
commit
a745436ee2
4 changed files with 78 additions and 1 deletions
|
@ -36,6 +36,7 @@ add_executable(f00bar
|
||||||
yml.c yml.h
|
yml.c yml.h
|
||||||
|
|
||||||
decorations/background.c decorations/background.h
|
decorations/background.c decorations/background.h
|
||||||
|
decorations/stack.c decorations/stack.h
|
||||||
decorations/underline.c decorations/underline.h
|
decorations/underline.c decorations/underline.h
|
||||||
|
|
||||||
particles/list.c particles/list.h
|
particles/list.c particles/list.h
|
||||||
|
|
27
config.c
27
config.c
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "decoration.h"
|
#include "decoration.h"
|
||||||
#include "decorations/background.h"
|
#include "decorations/background.h"
|
||||||
|
#include "decorations/stack.h"
|
||||||
#include "decorations/underline.h"
|
#include "decorations/underline.h"
|
||||||
|
|
||||||
#include "particle.h"
|
#include "particle.h"
|
||||||
|
@ -115,6 +116,29 @@ deco_underline_from_config(const struct yml_node *node)
|
||||||
yml_value_as_int(size), color_from_hexstr(yml_value_as_string(color)));
|
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_stack_from_config(const struct yml_node *node)
|
||||||
|
{
|
||||||
|
assert(yml_is_list(node));
|
||||||
|
|
||||||
|
size_t count = yml_list_length(node);
|
||||||
|
assert(count > 0);
|
||||||
|
|
||||||
|
struct deco *decos[count];
|
||||||
|
size_t idx = 0;
|
||||||
|
|
||||||
|
for (struct yml_list_iter it = yml_list_iter(node);
|
||||||
|
it.node != NULL;
|
||||||
|
yml_list_next(&it), idx++)
|
||||||
|
{
|
||||||
|
decos[idx] = deco_from_config(it.node);
|
||||||
|
}
|
||||||
|
|
||||||
|
return deco_stack(decos, count);
|
||||||
|
}
|
||||||
|
|
||||||
static struct deco *
|
static struct deco *
|
||||||
deco_from_config(const struct yml_node *node)
|
deco_from_config(const struct yml_node *node)
|
||||||
{
|
{
|
||||||
|
@ -126,7 +150,6 @@ deco_from_config(const struct yml_node *node)
|
||||||
const struct yml_node *deco_data = it.value;
|
const struct yml_node *deco_data = it.value;
|
||||||
|
|
||||||
assert(yml_is_scalar(deco_type));
|
assert(yml_is_scalar(deco_type));
|
||||||
assert(yml_is_dict(deco_data));
|
|
||||||
|
|
||||||
const char *type = yml_value_as_string(deco_type);
|
const char *type = yml_value_as_string(deco_type);
|
||||||
|
|
||||||
|
@ -134,6 +157,8 @@ deco_from_config(const struct yml_node *node)
|
||||||
return deco_background_from_config(deco_data);
|
return deco_background_from_config(deco_data);
|
||||||
else if (strcmp(type, "underline") == 0)
|
else if (strcmp(type, "underline") == 0)
|
||||||
return deco_underline_from_config(deco_data);
|
return deco_underline_from_config(deco_data);
|
||||||
|
else if (strcmp(type, "stack") == 0)
|
||||||
|
return deco_stack_from_config(deco_data);
|
||||||
else
|
else
|
||||||
assert(false);
|
assert(false);
|
||||||
}
|
}
|
||||||
|
|
45
decorations/stack.c
Normal file
45
decorations/stack.c
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#include "underline.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct private {
|
||||||
|
struct deco **decos;
|
||||||
|
size_t count;
|
||||||
|
};
|
||||||
|
|
||||||
|
static void
|
||||||
|
destroy(struct deco *deco)
|
||||||
|
{
|
||||||
|
struct private *d = deco->private;
|
||||||
|
for (size_t i = 0; i < d->count; i++)
|
||||||
|
d->decos[i]->destroy(d->decos[i]);
|
||||||
|
free(d->decos);
|
||||||
|
free(d);
|
||||||
|
free(deco);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
expose(const struct deco *deco, cairo_t *cr, int x, int y, int width, int height)
|
||||||
|
{
|
||||||
|
const struct private *d = deco->private;
|
||||||
|
for (size_t i = 0; i < d->count; i++)
|
||||||
|
d->decos[i]->expose(d->decos[i], cr, x, y, width, height);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct deco *
|
||||||
|
deco_stack(struct deco *decos[], size_t count)
|
||||||
|
{
|
||||||
|
struct private *priv = malloc(sizeof(*priv));
|
||||||
|
priv->decos = malloc(count * sizeof(priv->decos[0]));
|
||||||
|
priv->count = count;
|
||||||
|
|
||||||
|
for (size_t i = 0; i < count; i++)
|
||||||
|
priv->decos[i] = decos[i];
|
||||||
|
|
||||||
|
struct deco *deco = malloc(sizeof(*deco));
|
||||||
|
deco->private = priv;
|
||||||
|
deco->expose = &expose;
|
||||||
|
deco->destroy = &destroy;
|
||||||
|
|
||||||
|
return deco;
|
||||||
|
}
|
6
decorations/stack.h
Normal file
6
decorations/stack.h
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "../color.h"
|
||||||
|
#include "../decoration.h"
|
||||||
|
|
||||||
|
struct deco *deco_stack(struct deco *decos[], size_t count);
|
Loading…
Add table
Reference in a new issue