[theme] refactor theme builder a bit

This commit is contained in:
Milo Turner 2020-02-21 14:14:05 -05:00
parent c3d7804279
commit 09a256ed90
3 changed files with 50 additions and 71 deletions

View File

@ -134,7 +134,7 @@ void ax_set_theme_color(
ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme");
enum ax_color_cat cat;
if (ax__string_to_color_cat(cat_name, &cat) != 0) {
ax->thmb->thm.fatal_err =
ax->thmb->err =
rsprintf(&ax->thmb_rgn, "invalid color category `%s'", cat_name);
} else {
ax__theme_set_color(ax->thmb, cat, rgb);
@ -147,7 +147,7 @@ void ax_set_theme_font(
ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme");
enum ax_font_cat cat;
if (ax__string_to_font_cat(cat_name, &cat) != 0) {
ax->thmb->thm.fatal_err =
ax->thmb->err =
rsprintf(&ax->thmb_rgn, "invalid font category `%s'", cat_name);
} else {
ax__theme_set_font(ax->thmb, cat, fnt_path, fnt_size);
@ -164,8 +164,8 @@ void ax_set_theme_iconset(
int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm)
{
ASSERT_NON_NULL(thm, "theme");
if (thm->fatal_err != NULL) {
ax->err = thm->fatal_err;
if (thm->err != NULL) {
ax->err = thm->err;
return AX_ERR_THEME_INVALID;
}
if (!ax__theme_is_loaded(thm)) {

View File

@ -12,48 +12,33 @@ static const char* default_font_path(struct rgn* rgn)
return "/usr/share/fonts/liberation/LiberationSans-Regular.ttf";
}
static void theme_init(struct ax_theme* thm)
{
thm->fatal_err = NULL;
thm->colors[AX_COLOR_BACKGROUND] = 0xffffff;
thm->colors[AX_COLOR_SURFACE] = 0xffffff;
thm->colors[AX_COLOR_PRIMARY] = 0xff0000;
thm->colors[AX_COLOR_PRIMARY_ALT] = 0xff4444;
thm->colors[AX_COLOR_SECONDARY] = 0x0000ff;
thm->colors[AX_COLOR_SECONDARY_ALT] = 0x4444ff;
thm->colors[AX_COLOR_ON_BACKGROUND] =
thm->colors[AX_COLOR_ON_SURFACE] =
thm->colors[AX_COLOR_ON_PRIMARY] =
thm->colors[AX_COLOR_ON_SECONDARY] = 0x000000;
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
thm->font_path[i] = NULL;
thm->font_handle[i] = NULL;
}
thm->font_size[AX_FONT_H1] = 96;
thm->font_size[AX_FONT_H2] = 60;
thm->font_size[AX_FONT_H3] = 34;
thm->font_size[AX_FONT_H4] = 24;
thm->font_size[AX_FONT_BODY] = 16;
thm->font_size[AX_FONT_BUTTON] = 14;
}
void ax__theme_builder_init(struct ax_theme_builder* thmb, struct rgn* rgn)
{
thmb->rgn = rgn;
thmb->err = NULL;
thmb->dflt_font_path = NULL;
theme_init(&thmb->thm);
}
char* ax__theme_make_error_buf(
struct ax_theme_builder* thmb,
size_t len)
{
char* buf = ralloc_typed(thmb->rgn, char, len);
thmb->thm.fatal_err = buf;
return buf;
thmb->color[AX_COLOR_BACKGROUND] = 0xffffff;
thmb->color[AX_COLOR_SURFACE] = 0xffffff;
thmb->color[AX_COLOR_PRIMARY] = 0xff0000;
thmb->color[AX_COLOR_PRIMARY_ALT] = 0xff4444;
thmb->color[AX_COLOR_SECONDARY] = 0x0000ff;
thmb->color[AX_COLOR_SECONDARY_ALT] = 0x4444ff;
thmb->color[AX_COLOR_ON_BACKGROUND] =
thmb->color[AX_COLOR_ON_SURFACE] =
thmb->color[AX_COLOR_ON_PRIMARY] =
thmb->color[AX_COLOR_ON_SECONDARY] = 0x000000;
thmb->font_size[AX_FONT_H1] = 96;
thmb->font_size[AX_FONT_H2] = 60;
thmb->font_size[AX_FONT_H3] = 34;
thmb->font_size[AX_FONT_H4] = 24;
thmb->font_size[AX_FONT_BODY] = 16;
thmb->font_size[AX_FONT_BUTTON] = 14;
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
thmb->font_path[i] = NULL;
}
}
void ax__theme_set_color(
@ -61,7 +46,7 @@ void ax__theme_set_color(
enum ax_color_cat cat,
int64_t val)
{
thmb->thm.colors[cat] = val;
thmb->color[cat] = val;
}
void ax__theme_set_font(
@ -74,8 +59,8 @@ void ax__theme_set_font(
out_path = &thmb->dflt_font_path;
} else {
ASSERT(cat < AX_FONT__COUNT, "invalid font category");
thmb->thm.font_size[cat] = size;
out_path = &thmb->thm.font_path[cat];
thmb->font_size[cat] = size;
out_path = &thmb->font_path[cat];
}
if (path == NULL) {
*out_path = NULL;
@ -94,28 +79,23 @@ void ax__theme_builder_finish(
}
struct ax_theme* thm = ralloc_typed(dst_rgn, struct ax_theme, 1);
*thm = thmb->thm;
*out_thm = thm;
/* fix strings by copying into dest region */
if (thmb->thm.fatal_err != NULL) {
thmb->thm.fatal_err = rstrdup(dst_rgn, thmb->thm.fatal_err);
if (thmb->err != NULL) {
thm->err = rstrdup(dst_rgn, thmb->err);
return;
}
const char* dflt_font_path;
if (thmb->dflt_font_path != NULL) {
dflt_font_path = rstrdup(dst_rgn, thmb->dflt_font_path);
} else {
dflt_font_path = default_font_path(dst_rgn);
}
const char* dflt_font_path = (thmb->dflt_font_path == NULL) ?
default_font_path(dst_rgn) :
rstrdup(dst_rgn, thmb->dflt_font_path);
for (size_t i = 0; i < AX_FONT__COUNT; i++) {
if (thm->font_path[i] == NULL) {
thm->font_path[i] = dflt_font_path;
} else {
thm->font_path[i] = rstrdup(dst_rgn, thm->font_path[i]);
}
thm->color[i] = thmb->color[i];
thm->font_size[i] = thmb->font_size[i];
thm->font_path[i] = (thm->font_path[i] == NULL) ?
dflt_font_path :
rstrdup(dst_rgn, thm->font_path[i]);
}
}
@ -161,7 +141,7 @@ void ax__debug_theme(struct ax_theme* thm)
const char* str;
#include "../../_build/color.printer.inc"
printf(" color `%s': #%06" PRIx64 "\n",
str, thm->colors[i]);
str, thm->color[i]);
}
printf("--------\n");
for (size_t i = 0; i < AX_FONT__COUNT; i++) {

View File

@ -37,17 +37,20 @@ enum ax_font_cat {
struct ax_font_h; // defined by backend
struct ax_theme {
const char* fatal_err;
int64_t colors[AX_COLOR__COUNT];
const char* font_path[AX_FONT__COUNT];
size_t font_size[AX_FONT__COUNT];
const char* err;
int64_t color[AX_COLOR__COUNT];
size_t font_size[AX_FONT__COUNT];
const char* font_path[AX_FONT__COUNT];
struct ax_font_h* font_handle[AX_FONT__COUNT];
};
struct ax_theme_builder {
struct rgn* rgn;
const char* err;
const char* dflt_font_path;
struct ax_theme thm;
int64_t color[AX_COLOR__COUNT];
size_t font_size[AX_FONT__COUNT];
const char* font_path[AX_FONT__COUNT];
};
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat);
@ -56,10 +59,6 @@ int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat);
void ax__theme_builder_init(
struct ax_theme_builder* thmb, struct rgn* rgn);
char* ax__theme_make_error_buf(
struct ax_theme_builder* thmb,
size_t len);
void ax__theme_set_color(
struct ax_theme_builder* thmb,
enum ax_color_cat cat,
@ -95,7 +94,7 @@ static inline int64_t ax__theme_color(
struct ax_theme* thm,
enum ax_color_cat i)
{
return thm->colors[i];
return thm->color[i];
}
static inline const char* ax__theme_font(