improve file chooser

This commit is contained in:
lixianjing 2020-06-04 07:15:24 +08:00
parent 4a1554049e
commit bd157e8909
7 changed files with 84 additions and 6 deletions

View File

@ -52,7 +52,7 @@ static ret_t on_file_save(void* ctx, event_t* e) {
file_chooser_t* chooser = file_chooser_create();
emitter_on(EMITTER(chooser), EVT_DONE, tk_on_choose_file_result, ctx);
file_chooser_set_init_dir(chooser, "./");
file_chooser_set_init_dir(chooser, "../");
return file_chooser_choose_file_for_save(chooser);
}
@ -61,7 +61,8 @@ static ret_t on_file_open(void* ctx, event_t* e) {
file_chooser_t* chooser = file_chooser_create();
emitter_on(EMITTER(chooser), EVT_DONE, tk_on_choose_file_result, ctx);
file_chooser_set_init_dir(chooser, "./");
file_chooser_set_init_dir(chooser, "src");
file_chooser_set_filter(chooser, ".c.h.cpp.inc");
return file_chooser_choose_file_for_open(chooser);
}

View File

@ -1,4 +1,10 @@
# 最新动态
* 2020/06/04
* 完善 file_chooser 支持初始目录和过滤规则。
* 2020/06/03
* 完善 path_normalize 函数。
* 2020/06/02
* 完善文档
* 完善 label

View File

@ -514,6 +514,10 @@ bool_t fb_filter_by_ext_names(void* ctx, const void* data) {
const char* ext_names = (const char*)ctx;
const char* p = strrchr(item->name, '.');
if (item->is_dir) {
return TRUE;
}
if (ext_names == NULL) {
return TRUE;
}

View File

@ -33,7 +33,7 @@
#define FB_DATE_TIME_FORMAT "YY-MM-DD hh:mm:ss"
#endif /*FB_DATE_TIME_FORMAT*/
static ret_t file_browser_view_reload(widget_t* widget);
ret_t file_browser_view_reload(widget_t* widget);
ret_t file_browser_view_set_init_dir(widget_t* widget, const char* init_dir) {
file_browser_view_t* file_browser_view = FILE_BROWSER_VIEW(widget);
@ -45,6 +45,26 @@ ret_t file_browser_view_set_init_dir(widget_t* widget, const char* init_dir) {
return RET_OK;
}
ret_t file_browser_view_set_filter(widget_t* widget, const char* filter) {
file_browser_view_t* file_browser_view = FILE_BROWSER_VIEW(widget);
return_value_if_fail(file_browser_view != NULL && filter != NULL, RET_BAD_PARAMS);
file_browser_view->filter = tk_str_copy(file_browser_view->filter, filter);
if (tk_str_eq(filter, STR_FILTER_FILES_ONLY)) {
file_browser_set_filter(file_browser_view->fb, fb_filter_files_only, NULL);
} else if (tk_str_eq(filter, STR_FILTER_DIR_ONLY)) {
file_browser_set_filter(file_browser_view->fb, fb_filter_directories_only, NULL);
} else if(filter != NULL) {
file_browser_set_filter(file_browser_view->fb, fb_filter_by_ext_names,
file_browser_view->filter);
} else {
file_browser_set_filter(file_browser_view->fb, NULL, NULL);
}
return RET_OK;
}
static ret_t file_browser_view_sync_sort(widget_t* widget) {
file_browser_view_t* file_browser_view = FILE_BROWSER_VIEW(widget);
const char* sort_by = file_browser_view->sort_by;
@ -267,7 +287,7 @@ static widget_t* file_browser_view_create_folder_item(widget_t* widget) {
return item;
}
static ret_t file_browser_view_reload(widget_t* widget) {
ret_t file_browser_view_reload(widget_t* widget) {
uint32_t i = 0;
uint32_t nr = 0;
widget_t* item = NULL;
@ -369,7 +389,7 @@ static ret_t file_browser_view_init_ui(widget_t* widget) {
widget_on(template, EVT_CLICK, file_browser_view_on_item_clicked, widget);
file_browser_view->inited = TRUE;
return file_browser_view_reload(widget);
return RET_OK;
}
static ret_t file_browser_view_on_event(widget_t* widget, event_t* e) {

View File

@ -76,6 +76,13 @@ typedef struct _file_browser_view_t {
*
*/
char* init_dir;
/**
* @property {char*} filter
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
*
*/
char* filter;
/**
* @property {bool_t} ignore_hidden_files
@ -157,6 +164,28 @@ widget_t* file_browser_view_cast(widget_t* widget);
*/
ret_t file_browser_view_set_init_dir(widget_t* widget, const char* init_dir);
/**
* @method file_browser_view_set_filter
*
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
* @param {const char*} filter
*> files_only dir_only (.jpg.png.gif)
*
* @return {ret_t} RET_OK表示成功
*/
ret_t file_browser_view_set_filter(widget_t* widget, const char* filter);
/**
* @method file_browser_view_reload
*
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
*
* @return {ret_t} RET_OK表示成功
*/
ret_t file_browser_view_reload(widget_t* widget);
/**
* @method file_browser_view_set_ignore_hidden_files
*
@ -289,6 +318,9 @@ ret_t file_browser_view_register(void);
/*容器控件通常是scrollview*/
#define FILE_BROWSER_VIEW_CONTAINER "container"
#define STR_FILTER_FILES_ONLY "files_only"
#define STR_FILTER_DIR_ONLY "dir_only"
/*public for subclass and runtime type check*/
TK_EXTERN_VTABLE(file_browser_view);

View File

@ -102,9 +102,22 @@ static ret_t file_choose_on_ok(void* ctx, event_t* e) {
ret_t file_chooser_choose(file_chooser_t* chooser) {
widget_t* win = window_open(chooser->ui);
widget_t* file_browser_view = widget_lookup_by_type(win, WIDGET_TYPE_FILE_BROWSER_VIEW, TRUE);
widget_child_on(win, FILE_CHOOSER_OK, EVT_CLICK, file_choose_on_ok, chooser);
widget_child_on(win, FILE_CHOOSER_CANCEL, EVT_CLICK, file_choose_on_click_to_close, chooser);
if (chooser->init_dir != NULL || chooser->filter != NULL) {
if (chooser->filter != NULL) {
file_browser_view_set_filter(file_browser_view, chooser->filter);
}
if (chooser->init_dir != NULL) {
file_browser_view_set_init_dir(file_browser_view, chooser->init_dir);
}
}
file_browser_view_reload(file_browser_view);
if (widget_is_dialog(win)) {
dialog_modal(win);
}
@ -131,6 +144,7 @@ ret_t file_chooser_choose_folder(file_chooser_t* chooser) {
return_value_if_fail(chooser != NULL, RET_BAD_PARAMS);
chooser->ui = FILE_CHOOSER_UI_CHOOSE_FOLDER;
file_chooser_set_filter(chooser, STR_FILTER_DIR_ONLY);
return file_chooser_choose(chooser);
}

View File

@ -106,7 +106,8 @@ ret_t file_chooser_set_init_dir(file_chooser_t* chooser, const char* init_dir);
*
* @annotation ["scriptable"]
* @param {file_chooser_t*} chooser file_chooser对象
* @param {const char*} filter (".jpg.png.gif")
* @param {const char*} filter
*> files_only dir_only (.jpg.png.gif)
*
* @return {ret_t} RET_OK表示成功
*/