awtk/res/assets.inc

88 lines
2.6 KiB
PHP
Raw Normal View History

2020-08-16 08:25:51 +08:00
#include "awtk.h"
#include "base/assets_manager.h"
#include "assets/__assets_default.inc"
2021-02-25 18:14:45 +08:00
#include "assets/__assets_dark.inc"
2020-08-16 08:25:51 +08:00
#ifndef APP_THEME
#define APP_THEME "default"
#endif /*APP_THEME*/
bool_t assets_has_theme(const char* name) {
return_value_if_fail(name != NULL, FALSE);
if (tk_str_eq(name, "default")) {
return TRUE;
2021-02-25 18:14:45 +08:00
} else if (tk_str_eq(name, "dark")) {
return TRUE;
2020-08-16 08:25:51 +08:00
} else {
return FALSE;
}
}
static ret_t assets_init_internal(const char* theme) {
assets_manager_t* am = assets_manager();
return_value_if_fail(theme != NULL && am != NULL, RET_BAD_PARAMS);
assets_manager_set_theme(am, theme);
2020-08-16 08:51:05 +08:00
if (tk_str_eq(theme, "default")) {
2020-08-16 08:25:51 +08:00
return assets_init_default();
2021-02-25 18:14:45 +08:00
} else if (tk_str_eq(theme, "dark")) {
return assets_init_dark();
2020-08-16 08:51:05 +08:00
} else {
2020-08-16 08:25:51 +08:00
log_debug("%s not support.\n", theme);
return RET_NOT_IMPL;
}
}
2022-06-23 17:36:08 +08:00
#if !defined(WITH_FS_RES) || defined(AWTK_WEB)
2020-10-12 21:35:24 +08:00
static ret_t widget_set_theme_without_file_system(widget_t* widget, const char* name) {
2020-08-16 08:25:51 +08:00
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);
2020-08-16 08:51:05 +08:00
locale_info_t* locale_info = widget_get_locale_info(widget);
2020-08-16 08:25:51 +08:00
return_value_if_fail(am != NULL && name != NULL, RET_BAD_PARAMS);
return_value_if_fail(assets_has_theme(name), RET_BAD_PARAMS);
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");
2021-01-29 12:42:14 +08:00
theme_set_theme_data(theme(), info->data);
2020-08-16 08:25:51 +08:00
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;
}
2021-11-09 14:56:49 +08:00
static ret_t on_set_theme_without_file_system(void* ctx, event_t* e) {
theme_change_event_t* evt = theme_change_event_cast(e);
widget_set_theme_without_file_system(window_manager(), evt->name);
return RET_OK;
}
2022-06-23 17:36:08 +08:00
#endif /*!defined(WITH_FS_RES) || defined(AWTK_WEB)*/
2020-08-16 08:25:51 +08:00
2021-11-09 14:56:49 +08:00
ret_t assets_init(void) {
2022-06-23 17:36:08 +08:00
#if !defined(WITH_FS_RES) || defined(AWTK_WEB)
2021-11-09 14:56:49 +08:00
widget_on(window_manager(), EVT_THEME_WILL_CHANGE, on_set_theme_without_file_system, NULL);
2022-06-23 17:36:08 +08:00
#endif /*!defined(WITH_FS_RES) || defined(AWTK_WEB)*/
2021-11-09 14:56:49 +08:00
return assets_init_internal(APP_THEME);
}
2020-08-16 08:25:51 +08:00
ret_t assets_set_global_theme(const char* name) {
return widget_set_theme(window_manager(), name);
}