improve fscript for locale and theme

This commit is contained in:
lixianjing 2021-11-09 17:05:47 +08:00
parent 45bd180e3b
commit 36420ee41a
6 changed files with 270 additions and 4 deletions

View File

@ -13,6 +13,31 @@
<button focusable="true" on:click="open('button_fscript')" text="Buttons"/>
<button focusable="true" on:click="open('basic_fscript')" text="Basic"/>
<button focusable="true" on:click="open('text_selector_fscript')" text="Text Selector"/>
<button focusable="true" tr_text="<&quot;en&amp;zh&quot;>">
<property name="on:click">
<![CDATA[
a = locale_get()
b = object_get(a, 'language')
if (b == 'en') {
locale_set('zh', 'CN')
} else {
locale_set('en', 'US')
}
]]>
</property>
</button>
<button focusable="true" text="Change Theme">
<property name="on:click">
<![CDATA[
a = theme_get()
if (a == 'default') {
theme_set('dark')
} else {
theme_set('default')
}
]]>
</property>
</button>
<button focusable="true" on:click="quit()" text="Quit"/>
</view>
</window>

View File

@ -2,6 +2,7 @@
2021/11/09
* 修复 mledit oy 计算不正确的问题 (感谢兆坤提供补丁)
* fscript 支持多国语言互译和主题切换(感谢雨欣提供补丁)
* 修复mledit在overwrite模式下鼠标拖动mledit没有一起动的问题。感谢兆坤提供补丁
* 新增了主题改变事件结构,并让 widget\_set\_theme 接口支持无文件系统时切换主题 (感谢雨欣提供补丁)

View File

@ -676,6 +676,141 @@ widget_eval(widget, path.prop)
</row>
```
### 5.15 locale\_get
> 获取本地化信息(国家和语言)
----------------------------
#### 原型
```js
locale_get() => object
locale_get(widget) => object
```
* widget 参数:'self' 表示当前控件,'parent' 表示当前控件的父控件,'window' 表示当前的窗口,'window\_manager' 表示窗口管理器。
* 不传入 widget 参数时,默认使用 'self' 当前控件。
返回的本地化信息对象包含以下属性:
| 属性名称 | 属性类型 | 说明 |
| -------- | -------- | ------------------ |
| language | string | 语言。如zh |
| country | string | 国家或地区。如CN |
### 示例
> 获取当前控件的本地化信息
```
obj = locale_get()
print(object_get(obj, 'country'))
print(object_get(obj, 'language'))
```
> 获取窗口管理器的本地化信息
```
obj = locale_get('window_manager')
print(object_get(obj, 'country'))
print(object_get(obj, 'language'))
```
获取当前控件的父控件或当前窗口的本地化信息用法类似,此处不多赘述。
### 5.16 locale\_set
> 设置本地化信息(国家和语言)
----------------------------
#### 原型
```js
locale_set(language, country)
locale_set(widget, language, country)
```
* widget 参数:'self' 表示当前控件,'parent' 表示当前控件的父控件,'window' 表示当前的窗口,'window\_manager' 表示窗口管理器。
* 不传入 widget 参数时,默认使用 'self' 当前控件。
### 示例
> 设置当前控件使用 "zh_CN" 语言
```
locale_set('zh', 'CN')
```
> 设置窗口管理器使用 "en_US" 语言
```
locale_set('window_manager', 'en', 'US')
```
设置当前控件的父控件或当前窗口的本地化信息用法类似,此处不多赘述。
### 5.17 theme\_get
> 获取当前主题
----------------------------
#### 原型
```js
theme_get() => string
theme_get(widget) => string
```
* widget 参数:'self' 表示当前控件,'parent' 表示当前控件的父控件,'window' 表示当前的窗口,'window\_manager' 表示窗口管理器。
* 不传入 widget 参数时,默认使用 'self' 当前控件。
### 示例
> 获取当前控件的主题
```
print(theme_get())
```
> 获取窗口管理器的主题
```
print(theme_get('window_manager'))
```
获取当前控件的父控件或当前窗口的主题用法类似,此处不多赘述。
### 5.18 theme\_set
> 设置本地化信息(国家和语言)
----------------------------
#### 原型
```js
theme_set(theme_name)
theme_set(widget, theme_name)
```
* widget 参数:'self' 表示当前控件,'parent' 表示当前控件的父控件,'window' 表示当前的窗口,'window\_manager' 表示窗口管理器。
* 不传入 widget 参数时,默认使用 'self' 当前控件。
### 示例
> 设置当前控件使用 "dark" 主题
```
theme_set('dark')
```
> 设置窗口管理器使用 "dark" 主题
```
theme_set('window_manager', 'dark')
```
设置当前控件的父控件或当前窗口的主题用法类似,此处不多赘述。
### 示例参考
* https://github.com/zlgopen/awtk/blob/master/design/default/ui/main_fscript.xml

View File

@ -1,5 +1,5 @@
TK_CONST_DATA_ALIGN(const unsigned char ui_main_fscript[]) = {
0x04,0x00,0x01,0x01,0x73,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x6d,0x61,0x69,0x6e,0x5f,0x66,0x73,0x63,
0x04,0x00,0x01,0x01,0x5c,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x6d,0x61,0x69,0x6e,0x5f,0x66,0x73,0x63,
0x72,0x69,0x70,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x12,0x12,0x22,0x11,0x77,0x69,0x6e,0x64,0x6f,0x77,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
@ -56,6 +56,30 @@ TK_CONST_DATA_ALIGN(const unsigned char ui_main_fscript[]) = {
0x65,0x6c,0x65,0x63,0x74,0x6f,0x72,0x00,0x00,0x00,0x62,0x75,0x74,0x74,0x6f,0x6e,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x66,0x6f,
0x63,0x75,0x73,0x61,0x62,0x6c,0x65,0x00,0x74,0x72,0x75,0x65,0x00,0x6f,0x6e,0x3a,0x63,0x6c,0x69,0x63,
0x6b,0x00,0x71,0x75,0x69,0x74,0x28,0x29,0x00,0x74,0x65,0x78,0x74,0x00,0x51,0x75,0x69,0x74,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/*1187*/
0x63,0x75,0x73,0x61,0x62,0x6c,0x65,0x00,0x74,0x72,0x75,0x65,0x00,0x74,0x72,0x5f,0x74,0x65,0x78,0x74,
0x00,0x3c,0x22,0x65,0x6e,0x26,0x7a,0x68,0x22,0x3e,0x00,0x6f,0x6e,0x3a,0x63,0x6c,0x69,0x63,0x6b,0x00,
0x61,0x20,0x3d,0x20,0x6c,0x6f,0x63,0x61,0x6c,0x65,0x5f,0x67,0x65,0x74,0x28,0x29,0x0a,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x62,0x20,0x3d,0x20,0x6f,0x62,0x6a,0x65,0x63,0x74,0x5f,0x67,0x65,0x74,0x28,
0x61,0x2c,0x20,0x27,0x6c,0x61,0x6e,0x67,0x75,0x61,0x67,0x65,0x27,0x29,0x0a,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x69,0x66,0x20,0x28,0x62,0x20,0x3d,0x3d,0x20,0x27,0x65,0x6e,0x27,0x29,0x20,0x7b,0x0a,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x6c,0x6f,0x63,0x61,0x6c,0x65,0x5f,0x73,0x65,0x74,
0x28,0x27,0x7a,0x68,0x27,0x2c,0x20,0x27,0x43,0x4e,0x27,0x29,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x7d,0x20,0x65,0x6c,0x73,0x65,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x6c,0x6f,0x63,0x61,0x6c,0x65,0x5f,0x73,0x65,0x74,0x28,0x27,0x65,0x6e,0x27,0x2c,0x20,0x27,0x55,0x53,
0x27,0x29,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0x00,0x00,0x00,0x62,0x75,0x74,0x74,0x6f,
0x6e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x66,0x6f,0x63,0x75,0x73,0x61,0x62,0x6c,0x65,0x00,0x74,0x72,0x75,0x65,0x00,0x74,0x65,
0x78,0x74,0x00,0x43,0x68,0x61,0x6e,0x67,0x65,0x20,0x54,0x68,0x65,0x6d,0x65,0x00,0x6f,0x6e,0x3a,0x63,
0x6c,0x69,0x63,0x6b,0x00,0x61,0x20,0x3d,0x20,0x74,0x68,0x65,0x6d,0x65,0x5f,0x67,0x65,0x74,0x28,0x29,
0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x69,0x66,0x20,0x28,0x61,0x20,0x3d,0x3d,0x20,0x27,0x64,
0x65,0x66,0x61,0x75,0x6c,0x74,0x27,0x29,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x74,0x68,0x65,0x6d,0x65,0x5f,0x73,0x65,0x74,0x28,0x27,0x64,0x61,0x72,0x6b,0x27,0x29,0x0a,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0x20,0x65,0x6c,0x73,0x65,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x68,0x65,0x6d,0x65,0x5f,0x73,0x65,0x74,0x28,0x27,0x64,0x65,0x66,
0x61,0x75,0x6c,0x74,0x27,0x29,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x7d,0x00,0x00,0x00,0x62,
0x75,0x74,0x74,0x6f,0x6e,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x66,0x6f,0x63,0x75,0x73,0x61,0x62,0x6c,0x65,0x00,0x74,0x72,0x75,
0x65,0x00,0x6f,0x6e,0x3a,0x63,0x6c,0x69,0x63,0x6b,0x00,0x71,0x75,0x69,0x74,0x28,0x29,0x00,0x74,0x65,
0x78,0x74,0x00,0x51,0x75,0x69,0x74,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,};/*1676*/

View File

@ -18,6 +18,7 @@
#include "tkc/utf8.h"
#include "tkc/fscript.h"
#include "tkc/tokenizer.h"
#include "tkc/object_default.h"
#include "base/enums.h"
#include "base/main_loop.h"
#include "base/window.h"
@ -414,6 +415,82 @@ static ret_t func_widget_send_key(fscript_t* fscript, fscript_args_t* args, valu
return RET_OK;
}
static ret_t func_locale_get(fscript_t* fscript, fscript_args_t* args, value_t* result) {
object_t* obj = NULL;
locale_info_t* info = NULL;
widget_t* widget = WIDGET(object_get_prop_pointer(fscript->obj, STR_PROP_SELF));
if (args->size > 0) {
widget = to_widget(fscript, args->args);
}
FSCRIPT_FUNC_CHECK(widget != NULL, RET_BAD_PARAMS);
info = widget_get_locale_info(widget);
FSCRIPT_FUNC_CHECK(info != NULL, RET_BAD_PARAMS);
obj = object_default_create();
FSCRIPT_FUNC_CHECK(obj != NULL, RET_OOM);
object_set_prop_str(obj, "language", info->language);
object_set_prop_str(obj, "country", info->country);
value_set_object(result, obj);
result->free_handle = TRUE;
return RET_OK;
}
static ret_t func_locale_set(fscript_t* fscript, fscript_args_t* args, value_t* result) {
locale_info_t* info = NULL;
const char* language = NULL;
const char* country = NULL;
widget_t* widget = WIDGET(object_get_prop_pointer(fscript->obj, STR_PROP_SELF));
FSCRIPT_FUNC_CHECK(args->size >= 2, RET_BAD_PARAMS);
if (args->size > 2) {
widget = to_widget(fscript, args->args);
language = value_str(args->args + 1);
country = value_str(args->args + 2);
} else {
language = value_str(args->args);
country = value_str(args->args + 1);
}
FSCRIPT_FUNC_CHECK(widget != NULL && language != NULL && country != NULL, RET_BAD_PARAMS);
info = widget_get_locale_info(widget);
FSCRIPT_FUNC_CHECK(info != NULL, RET_BAD_PARAMS);
value_set_bool(result, locale_info_change(info, language, country) == RET_OK);
return RET_OK;
}
static ret_t func_theme_get(fscript_t* fscript, fscript_args_t* args, value_t* result) {
assets_manager_t* am = NULL;
widget_t* widget = WIDGET(object_get_prop_pointer(fscript->obj, STR_PROP_SELF));
if (args->size > 0) {
widget = to_widget(fscript, args->args);
}
FSCRIPT_FUNC_CHECK(widget != NULL, RET_BAD_PARAMS);
am = widget_get_assets_manager(widget);
FSCRIPT_FUNC_CHECK(am != NULL, RET_BAD_PARAMS);
value_dup_str(result, am->theme);
return RET_OK;
}
static ret_t func_theme_set(fscript_t* fscript, fscript_args_t* args, value_t* result) {
const char* name = NULL;
widget_t* widget = WIDGET(object_get_prop_pointer(fscript->obj, STR_PROP_SELF));
FSCRIPT_FUNC_CHECK(args->size >= 1, RET_BAD_PARAMS);
if (args->size == 1) {
name = value_str(args->args);
} else {
widget = to_widget(fscript, args->args);
name = value_str(args->args + 1);
}
FSCRIPT_FUNC_CHECK(widget != NULL && name != NULL, RET_BAD_PARAMS);
value_set_bool(result, widget_set_theme(widget, name) == RET_OK);
return RET_OK;
}
FACTORY_TABLE_BEGIN(s_ext_widget)
FACTORY_TABLE_ENTRY("open", func_window_open)
FACTORY_TABLE_ENTRY("close", func_window_close)
@ -430,6 +507,10 @@ FACTORY_TABLE_ENTRY("widget_destroy", func_widget_destroy)
FACTORY_TABLE_ENTRY("start_timer", func_widget_add_timer)
FACTORY_TABLE_ENTRY("stop_timer", func_widget_remove_timer)
FACTORY_TABLE_ENTRY("send_key", func_widget_send_key)
FACTORY_TABLE_ENTRY("locale_get", func_locale_get)
FACTORY_TABLE_ENTRY("locale_set", func_locale_set)
FACTORY_TABLE_ENTRY("theme_get", func_theme_get)
FACTORY_TABLE_ENTRY("theme_set", func_theme_set)
FACTORY_TABLE_END()
ret_t fscript_widget_register(void) {