Skip to content

Commit

Permalink
Change theme handling
Browse files Browse the repository at this point in the history
So far when loading a theme it also overwrote the preferences the user
set.

Lengthy discussion can be found at
#1077

Now we use `/theme load themename` to load the [colours] part of a
themem only.

`/theme full-load themename` will load the complete theme including
preferences set in there.

Regards #1077
  • Loading branch information
jubalh committed Jan 29, 2020
1 parent 5c41c5b commit cd80b6c
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 17 deletions.
27 changes: 27 additions & 0 deletions src/command/cmd_ac.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ cmd_ac_init(void)

theme_ac = autocomplete_new();
autocomplete_add(theme_ac, "load");
autocomplete_add(theme_ac, "full-load");
autocomplete_add(theme_ac, "list");
autocomplete_add(theme_ac, "colours");
autocomplete_add(theme_ac, "properties");
Expand Down Expand Up @@ -2414,23 +2415,49 @@ static char*
_theme_autocomplete(ProfWin *window, const char *const input, gboolean previous)
{
char *result = NULL;

if (strncmp(input, "/theme load ", 12) == 0) {
if (theme_load_ac == NULL) {
theme_load_ac = autocomplete_new();
GSList *themes = theme_list();
GSList *curr = themes;

while (curr) {
autocomplete_add(theme_load_ac, curr->data);
curr = g_slist_next(curr);
}

g_slist_free_full(themes, g_free);
autocomplete_add(theme_load_ac, "default");
}

result = autocomplete_param_with_ac(input, "/theme load", theme_load_ac, TRUE, previous);
if (result) {
return result;
}
}

if (strncmp(input, "/theme full-load ", 17) == 0) {
if (theme_load_ac == NULL) {
theme_load_ac = autocomplete_new();
GSList *themes = theme_list();
GSList *curr = themes;

while (curr) {
autocomplete_add(theme_load_ac, curr->data);
curr = g_slist_next(curr);
}

g_slist_free_full(themes, g_free);
autocomplete_add(theme_load_ac, "default");
}

result = autocomplete_param_with_ac(input, "/theme full-load", theme_load_ac, TRUE, previous);
if (result) {
return result;
}
}

result = autocomplete_param_with_ac(input, "/theme", theme_ac, TRUE, previous);
if (result) {
return result;
Expand Down
10 changes: 6 additions & 4 deletions src/command/cmd_defs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2136,15 +2136,17 @@ static struct cmd_t command_defs[] =
CMD_SYN(
"/theme list",
"/theme load <theme>",
"/theme full-load <theme>",
"/theme colours",
"/theme properties")
CMD_DESC(
"Load a theme, includes colours and UI options.")
CMD_ARGS(
{ "list", "List all available themes." },
{ "load <theme>", "Load the specified theme. 'default' will reset to the default theme." },
{ "colours", "Show colour values as rendered by the terminal." },
{ "properties", "Show colour settings for current theme." })
{ "list", "List all available themes." },
{ "load <theme>", "Load colours from specified theme. 'default' will reset to the default theme." },
{ "full-load <theme>", "Same as 'load' but will also load preferences set in the theme, not just colours." },
{ "colours", "Show colour values as rendered by the terminal." },
{ "properties", "Show colour settings for current theme." })
CMD_EXAMPLES(
"/theme list",
"/theme load forest")
Expand Down
19 changes: 11 additions & 8 deletions src/command/cmd_funcs.c
Original file line number Diff line number Diff line change
Expand Up @@ -804,7 +804,7 @@ _account_set_theme(char *account_name, char *theme)
ProfAccount *account = accounts_get_account(session_get_account_name());
if (account) {
if (g_strcmp0(account->name, account_name) == 0) {
theme_load(theme);
theme_load(theme, false);
ui_load_colours();
if (prefs_get_boolean(PREF_ROSTER)) {
ui_show_roster();
Expand Down Expand Up @@ -1234,13 +1234,13 @@ cmd_disconnect(ProfWin *window, const char *const command, gchar **args)

char *theme = prefs_get_string(PREF_THEME);
if (theme) {
gboolean res = theme_load(theme);
gboolean res = theme_load(theme, false);
prefs_free_string(theme);
if (!res) {
theme_load("default");
theme_load("default", false);
}
} else {
theme_load("default");
theme_load("default", false);
}
ui_load_colours();
if (prefs_get_boolean(PREF_ROSTER)) {
Expand Down Expand Up @@ -1699,17 +1699,20 @@ cmd_prefs(ProfWin *window, const char *const command, gchar **args)
gboolean
cmd_theme(ProfWin *window, const char *const command, gchar **args)
{
// 'full-load' means to load the theme including the settings (not just [colours])
gboolean fullload = (g_strcmp0(args[0], "full-load") == 0);

// list themes
if (g_strcmp0(args[0], "list") == 0) {
GSList *themes = theme_list();
cons_show_themes(themes);
g_slist_free_full(themes, g_free);

// load a theme
} else if (g_strcmp0(args[0], "load") == 0) {
} else if (g_strcmp0(args[0], "load") == 0 || fullload) {
if (args[1] == NULL) {
cons_bad_cmd_usage(command);
} else if (theme_load(args[1])) {
} else if (theme_load(args[1], fullload)) {
ui_load_colours();
prefs_set_string(PREF_THEME, args[1]);
if (prefs_get_boolean(PREF_ROSTER)) {
Expand Down Expand Up @@ -8619,12 +8622,12 @@ cmd_color(ProfWin *window, const char *const command, gchar **args)

char *theme = prefs_get_string(PREF_THEME);
if (theme) {
gboolean res = theme_load(theme);
gboolean res = theme_load(theme, false);

if (res) {
cons_show("Theme reloaded: %s", theme);
} else {
theme_load("default");
theme_load("default", false);
}

prefs_free_string(theme);
Expand Down
8 changes: 5 additions & 3 deletions src/config/theme.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ theme_init(const char *const theme_name)
g_hash_table_insert(defaults, strdup("untrusted"), strdup("red"));
g_hash_table_insert(defaults, strdup("cmd.wins.unread"), strdup("default"));

_load_preferences();
//_load_preferences();
}

gboolean
Expand All @@ -174,12 +174,14 @@ theme_exists(const char *const theme_name)
}

gboolean
theme_load(const char *const theme_name)
theme_load(const char *const theme_name, gboolean load_theme_prefs)
{
color_pair_cache_reset();

if (_theme_load_file(theme_name)) {
_load_preferences();
if (load_theme_prefs) {
_load_preferences();
}
return TRUE;
} else {
return FALSE;
Expand Down
2 changes: 1 addition & 1 deletion src/config/theme.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ typedef enum {

void theme_init(const char *const theme_name);
void theme_init_colours(void);
gboolean theme_load(const char *const theme_name);
gboolean theme_load(const char *const theme_name, gboolean load_theme_prefs);
gboolean theme_exists(const char *const theme_name);
GSList* theme_list(void);
void theme_close(void);
Expand Down
2 changes: 1 addition & 1 deletion src/ui/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ void
ui_handle_login_account_success(ProfAccount *account, gboolean secured)
{
if (account->theme) {
if (theme_load(account->theme)) {
if (theme_load(account->theme, false)) {
ui_load_colours();
if (prefs_get_boolean(PREF_ROSTER)) {
ui_show_roster();
Expand Down

0 comments on commit cd80b6c

Please sign in to comment.