awtk/demos/assets.c

86 lines
2.3 KiB
C
Raw Normal View History

2019-10-18 18:18:12 +08:00
#include "awtk.h"
#include "base/assets_manager.h"
2020-03-18 21:36:24 +08:00
#include "assets_default.inc"
#include "assets_dark.inc"
2020-03-16 21:19:20 +08:00
2020-03-18 21:36:24 +08:00
#ifndef APP_THEME
#define APP_THEME "default"
#endif /*APP_THEME*/
2020-03-19 16:44:31 +08:00
ret_t assets_has_theme(const char* name) {
return_value_if_fail(name != NULL, FALSE);
if (tk_str_eq(name, "default")) {
return TRUE;
} else if (tk_str_eq(name, "dark")) {
return TRUE;
} else {
return FALSE;
}
}
static ret_t assets_init_internal(const char* theme) {
2019-10-18 18:18:12 +08:00
assets_manager_t* am = assets_manager();
2020-03-16 21:19:20 +08:00
return_value_if_fail(theme != NULL && am != NULL, RET_BAD_PARAMS);
2019-10-18 18:18:12 +08:00
2020-03-16 21:19:20 +08:00
assets_manager_set_theme(am, theme);
2019-10-18 18:18:12 +08:00
2020-03-16 21:19:20 +08:00
if (tk_str_eq(theme, "default")) {
return assets_init_default();
} else if (tk_str_eq(theme, "dark")) {
return assets_init_dark();
2020-03-18 14:26:26 +08:00
} else {
2020-03-16 21:19:20 +08:00
log_debug("%s not support.\n", theme);
return RET_NOT_IMPL;
}
2020-03-18 21:36:24 +08:00
}
ret_t assets_init(void) {
return assets_init_internal(APP_THEME);
}
2020-03-19 16:44:31 +08:00
static ret_t widget_set_theme_without_file_system(widget_t* widget, const char* name) {
2020-03-18 21:36:24 +08:00
#ifndef WITH_FS_RES
const asset_info_t* info = NULL;
event_t e = event_init(EVT_THEME_CHANGED, NULL);
widget_t* wm = widget_get_window_manager(widget);
font_manager_t* fm = widget_get_font_manager(widget);
image_manager_t* imm = widget_get_image_manager(widget);
assets_manager_t* am = widget_get_assets_manager(widget);
locale_info_t* locale_info = widget_get_locale_info(widget);
2020-03-19 11:11:32 +08:00
return_value_if_fail(am != NULL && name != NULL, RET_BAD_PARAMS);
2020-03-19 16:44:31 +08:00
return_value_if_fail(assets_has_theme(name), RET_BAD_PARAMS);
2020-03-19 11:11:32 +08:00
2020-03-18 21:36:24 +08:00
font_manager_unload_all(fm);
image_manager_unload_all(imm);
assets_manager_clear_all(am);
widget_reset_canvas(widget);
assets_init_internal(name);
locale_info_reload(locale_info);
info = assets_manager_ref(am, ASSET_TYPE_STYLE, "default");
theme_init(theme(), info->data);
assets_manager_unref(assets_manager(), info);
widget_dispatch(wm, &e);
widget_invalidate_force(wm, NULL);
log_debug("theme changed: %s\n", name);
return RET_OK;
2020-03-19 16:44:31 +08:00
#else /*WITH_FS_RES*/
2020-03-18 21:36:24 +08:00
return RET_NOT_IMPL;
#endif /*WITH_FS_RES*/
}
2020-03-19 16:44:31 +08:00
ret_t assets_set_global_theme(const char* name) {
#ifdef WITH_FS_RES
return widget_set_theme(window_manager(), name);
#else /*WITH_FS_RES*/
return widget_set_theme_without_file_system(window_manager(), name);
#endif /*WITH_FS_RES*/
}