Allow user theme selection via config file

Relates to: #9
This commit is contained in:
Johannes Marbach 2021-11-14 13:30:23 +01:00
parent 5c3ad9bf9a
commit deff1ef4e9
8 changed files with 106 additions and 34 deletions

View file

@ -73,9 +73,12 @@ static bool parse_bool(const char *value, bool *result);
static void init_opts(ul_config_opts *opts) {
opts->general.animations = false;
opts->textarea.obscured = true;
opts->keyboard.autohide = true;
opts->keyboard.layout_id = SQ2LV_LAYOUT_US;
opts->keyboard.popovers = false;
opts->textarea.obscured = true;
opts->theme.default_id = UL_THEMES_THEME_BREEZY_DARK;
opts->theme.alternate_id = UL_THEMES_THEME_BREEZY_LIGHT;
}
static void parse_file(const char *path, ul_config_opts *opts) {
@ -93,12 +96,6 @@ static int parsing_handler(void* user_data, const char* section, const char* key
return 1;
}
}
} else if (strcmp(section, "textarea") == 0) {
if (strcmp(key, "obscured") == 0) {
if (parse_bool(value, &(opts->textarea.obscured))) {
return 1;
}
}
} else if (strcmp(section, "keyboard") == 0) {
if (strcmp(key, "autohide") == 0) {
if (parse_bool(value, &(opts->keyboard.autohide))) {
@ -115,6 +112,26 @@ static int parsing_handler(void* user_data, const char* section, const char* key
return 1;
}
}
} else if (strcmp(section, "textarea") == 0) {
if (strcmp(key, "obscured") == 0) {
if (parse_bool(value, &(opts->textarea.obscured))) {
return 1;
}
}
} else if (strcmp(section, "theme") == 0) {
if (strcmp(key, "default") == 0) {
ul_themes_theme_id_t id = ul_themes_find_theme_with_name(value);
if (id != UL_THEMES_THEME_NONE) {
opts->theme.default_id = id;
return 1;
}
} else if (strcmp(key, "alternate") == 0) {
ul_themes_theme_id_t id = ul_themes_find_theme_with_name(value);
if (id != UL_THEMES_THEME_NONE) {
opts->theme.alternate_id = id;
return 1;
}
}
}
ul_log(UL_LOG_LEVEL_ERROR, "Ignoring invalid config value \"%s\" for key \"%s\" in section \"%s\"", value, key, section);

View file

@ -21,6 +21,8 @@
#ifndef UL_CONFIG_H
#define UL_CONFIG_H
#include "themes.h"
#include "sq2lv_layouts.h"
#include <stdbool.h>
@ -33,14 +35,6 @@ typedef struct {
bool animations;
} ul_config_opts_general;
/**
* Options related to the password textarea
*/
typedef struct {
/* If true, disguise the entered text with dots */
bool obscured;
} ul_config_opts_textarea;
/**
* Options related to the keyboard
*/
@ -53,16 +47,36 @@ typedef struct {
bool popovers;
} ul_config_opts_keyboard;
/**
* Options related to the password textarea
*/
typedef struct {
/* If true, disguise the entered text with dots */
bool obscured;
} ul_config_opts_textarea;
/**
* Options related to the theme
*/
typedef struct {
/* Default theme */
ul_themes_theme_id_t default_id;
/* Alternate theme */
ul_themes_theme_id_t alternate_id;
} ul_config_opts_theme;
/**
* Options parsed from config file(s)
*/
typedef struct {
/* General options */
ul_config_opts_general general;
/* Options related to the password textarea */
ul_config_opts_textarea textarea;
/* Options related to the keyboard */
ul_config_opts_keyboard keyboard;
/* Options related to the password textarea */
ul_config_opts_textarea textarea;
/* Options related to the theme */
ul_config_opts_theme theme;
} ul_config_opts;
/**

2
main.c
View file

@ -173,7 +173,7 @@ static void toggle_theme(void) {
}
static void set_theme(bool is_alternate) {
ul_theme_apply(is_alternate ? &ul_themes_breezy_light : &ul_themes_breezy_dark);
ul_theme_apply(&(ul_themes_themes[is_alternate ? conf_opts.theme.alternate_id : conf_opts.theme.default_id]));
}
static void toggle_pw_btn_clicked_cb(lv_event_t *event) {

View file

@ -66,7 +66,7 @@ static bool are_styles_initialised = false;
*
* @param theme theme to derive the styles from
*/
static void init_styles(ul_theme *theme);
static void init_styles(const ul_theme *theme);
/**
* Initialise or reset a style.
@ -95,7 +95,7 @@ static void keyboard_draw_part_begin_cb(lv_event_t *event);
* Static functions
*/
static void init_styles(ul_theme *theme) {
static void init_styles(const ul_theme *theme) {
reset_style(&(styles.widget));
lv_style_set_text_font(&(styles.widget), &font_32);
@ -322,7 +322,7 @@ void ul_theme_prepare_keyboard(lv_obj_t *keyboard) {
lv_obj_add_event_cb(keyboard, keyboard_draw_part_begin_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);
}
void ul_theme_apply(ul_theme *theme) {
void ul_theme_apply(const ul_theme *theme) {
if (!theme) {
ul_log(UL_LOG_LEVEL_ERROR, "Could not apply theme from NULL pointer");
return;

View file

@ -173,6 +173,7 @@ typedef struct {
/* Full theme */
typedef struct {
char *name;
ul_theme_window window;
ul_theme_header header;
ul_theme_keyboard keyboard;
@ -195,6 +196,6 @@ void ul_theme_prepare_keyboard(lv_obj_t *keyboard);
*
* @param theme the theme to apply
*/
void ul_theme_apply(ul_theme *theme);
void ul_theme_apply(const ul_theme *theme);
#endif /* UL_THEME_H */

View file

@ -22,10 +22,12 @@
/**
* Breezy light (based on KDE Breeze color palette, see https://develop.kde.org/hig/style/color/default/)
* Static variables
*/
ul_theme ul_themes_breezy_light = {
/* Breezy light (based on KDE Breeze color palette, see https://develop.kde.org/hig/style/color/default/) */
static const ul_theme ul_themes_breezy_light = {
.name = "breezy-light",
.window = {
.bg_color = 0xeff0f1
},
@ -166,11 +168,9 @@ ul_theme ul_themes_breezy_light = {
};
/**
* Breezy dark (based on KDE Breeze Dark color palette, see https://develop.kde.org/hig/style/color/dark/)
*/
ul_theme ul_themes_breezy_dark = {
/* Breezy dark (based on KDE Breeze Dark color palette, see https://develop.kde.org/hig/style/color/dark/) */
static const ul_theme ul_themes_breezy_dark = {
.name = "breezy-dark",
.window = {
.bg_color = 0x31363b
},
@ -309,3 +309,23 @@ ul_theme ul_themes_breezy_dark = {
}
}
};
/**
* Public interface
*/
const int ul_themes_num_themes = 2;
const ul_theme ul_themes_themes[] = {
ul_themes_breezy_light,
ul_themes_breezy_dark
};
ul_themes_theme_id_t ul_themes_find_theme_with_name(const char *name) {
for (int i = 0; i < ul_themes_num_themes; ++i) {
if (strcmp(ul_themes_themes[i].name, name) == 0) {
return i;
}
}
return UL_THEMES_THEME_NONE;
}

View file

@ -23,7 +23,23 @@
#include "theme.h"
extern ul_theme ul_themes_breezy_light;
extern ul_theme ul_themes_breezy_dark;
/* Theme IDs, values can be used as indexes into the ul_themes_themes array */
typedef enum {
UL_THEMES_THEME_NONE = -1,
UL_THEMES_THEME_BREEZY_LIGHT = 0,
UL_THEMES_THEME_BREEZY_DARK = 1
} ul_themes_theme_id_t;
/* Themes */
extern const int ul_themes_num_themes;
extern const ul_theme ul_themes_themes[];
/**
* Find the first theme with a given name.
*
* @param name theme name
* @return ID of the first matching theme or UL_THEMES_THEME_NONE if no theme matched
*/
ul_themes_theme_id_t ul_themes_find_theme_with_name(const char *name);
#endif /* UL_THEMES_H */

View file

@ -1,10 +1,14 @@
[general]
animations=true
[textarea]
obscured=true
[keyboard]
autohide=true
layout=de
popovers=true
[textarea]
obscured=true
[theme]
default=breezy-dark
alternate=breezy-light