improve file browser view

This commit is contained in:
lixianjing 2020-01-11 08:31:00 +08:00
parent 3b6957c732
commit 4d7a72370a
6 changed files with 192 additions and 9 deletions

View File

@ -397,7 +397,7 @@
#include "assets/default/inc/images/battery_0.data"
#include "assets/default/inc/images/middle_on.data"
#include "assets/default/inc/images/num_7.data"
#endif/*WITH_STB_IMAGE*/
#endif /*WITH_STB_IMAGE*/
#ifdef WITH_VGCANVAS
#include "assets/default/inc/images/ball.bsvg"
#include "assets/default/inc/images/girl.bsvg"
@ -405,13 +405,13 @@
#include "assets/default/inc/images/pointer_4.bsvg"
#include "assets/default/inc/images/pointer_1.bsvg"
#include "assets/default/inc/images/pointer.bsvg"
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#if defined(WITH_TRUETYPE_FONT)
#include "assets/default/inc/fonts/default.res"
#else/*WITH_TRUETYPE_FONT*/
#else /*WITH_TRUETYPE_FONT*/
#include "assets/default/inc/fonts/default.data"
#endif/*WITH_TRUETYPE_FONT*/
#endif/*WITH_FS_RES*/
#endif /*WITH_TRUETYPE_FONT*/
#endif /*WITH_FS_RES*/
ret_t assets_init(void) {
assets_manager_t* am = assets_manager();
@ -701,7 +701,7 @@ ret_t assets_init(void) {
assets_manager_add(am, image_pointer_4);
assets_manager_add(am, image_pointer_1);
assets_manager_add(am, image_pointer);
#endif/*WITH_VGCANVAS*/
#endif /*WITH_VGCANVAS*/
#endif
tk_init_assets();

View File

@ -373,6 +373,32 @@ int fb_compare_by_name(const void* a, const void* b) {
return fb_dir_first(aa, bb);
}
int fb_compare_by_type(const void* a, const void* b) {
fb_item_t* aa = (fb_item_t*)a;
fb_item_t* bb = (fb_item_t*)b;
if (aa->is_reg_file && bb->is_reg_file) {
const char* atype = strrchr(aa->name, '.');
const char* btype = strrchr(bb->name, '.');
if (atype == NULL) {
return -1;
}
if (btype == NULL) {
return 1;
}
return strcasecmp(atype + 1, btype + 1);
}
if (aa->is_dir && bb->is_dir) {
return strcmp(aa->name, bb->name);
}
return fb_dir_first(aa, bb);
}
int fb_compare_by_size(const void* a, const void* b) {
fb_item_t* aa = (fb_item_t*)a;
fb_item_t* bb = (fb_item_t*)b;
@ -406,6 +432,32 @@ int fb_compare_by_name_dec(const void* a, const void* b) {
return fb_dir_first(aa, bb);
}
int fb_compare_by_type_dec(const void* a, const void* b) {
fb_item_t* aa = (fb_item_t*)a;
fb_item_t* bb = (fb_item_t*)b;
if (aa->is_reg_file && bb->is_reg_file) {
const char* atype = strrchr(aa->name, '.');
const char* btype = strrchr(bb->name, '.');
if (atype == NULL) {
return 1;
}
if (btype == NULL) {
return -1;
}
return -strcasecmp(atype + 1, btype + 1);
}
if (aa->is_dir && bb->is_dir) {
return -strcmp(aa->name, bb->name);
}
return fb_dir_first(aa, bb);
}
int fb_compare_by_size_dec(const void* a, const void* b) {
fb_item_t* aa = (fb_item_t*)a;
fb_item_t* bb = (fb_item_t*)b;
@ -446,6 +498,14 @@ ret_t file_browser_sort_by_name(file_browser_t* fb, bool_t ascending) {
return file_browser_sort(fb);
}
ret_t file_browser_sort_by_type(file_browser_t* fb, bool_t ascending) {
return_value_if_fail(fb != NULL, RET_BAD_PARAMS);
fb->compare = ascending ? fb_compare_by_type : fb_compare_by_type_dec;
return file_browser_sort(fb);
}
ret_t file_browser_sort_by_size(file_browser_t* fb, bool_t ascending) {
return_value_if_fail(fb != NULL, RET_BAD_PARAMS);

View File

@ -285,6 +285,17 @@ ret_t file_browser_set_compare(file_browser_t* fb, tk_compare_t compare);
*/
ret_t file_browser_sort_by_name(file_browser_t* fb, bool_t ascending);
/**
* @method file_browser_sort_by_type
*
*
* @param {file_browser_t*} fb file browser对象
* @param {bool_t} ascending
*
* @return {ret_t} RET_OK表示成功
*/
ret_t file_browser_sort_by_type(file_browser_t* fb, bool_t ascending);
/**
* @method file_browser_sort_by_size
*
@ -355,9 +366,11 @@ bool_t fb_filter_by_ext_names(void* ctx, const void* data);
bool_t fb_filter_directories_only(void* ctx, const void* data);
int fb_compare_by_name(const void* a, const void* b);
int fb_compare_by_type(const void* a, const void* b);
int fb_compare_by_size(const void* a, const void* b);
int fb_compare_by_mtime(const void* a, const void* b);
int fb_compare_by_name_dec(const void* a, const void* b);
int fb_compare_by_type_dec(const void* a, const void* b);
int fb_compare_by_size_dec(const void* a, const void* b);
int fb_compare_by_mtime_dec(const void* a, const void* b);

View File

@ -24,6 +24,7 @@
#include "file_browser_view.h"
#define SORT_BY_NAME "name"
#define SORT_BY_TYPE "type"
#define SORT_BY_SIZE "size"
#define SORT_BY_MTIME "mtime"
@ -52,6 +53,8 @@ static ret_t file_browser_view_sync_sort(widget_t* widget) {
file_browser_sort_by_name(file_browser_view->fb, sort_ascending);
} else if (tk_str_eq(sort_by, SORT_BY_MTIME)) {
file_browser_sort_by_mtime(file_browser_view->fb, sort_ascending);
} else if (tk_str_eq(sort_by, SORT_BY_TYPE)) {
file_browser_sort_by_type(file_browser_view->fb, sort_ascending);
}
}

View File

@ -58,7 +58,7 @@ typedef struct _file_browser_view_t {
/**
* @property {char*} sort_by
* @annotation ["set_prop","get_prop","readable","persitent","design","scriptable"]
* (name, size, mtime)
* (name, size, mtime, type)
*/
char* sort_by;
@ -150,10 +150,10 @@ ret_t file_browser_view_set_sort_ascending(widget_t* widget, bool_t sort_ascendi
/**
* @method file_browser_view_set_sort_by
* (name, size, mtime)
* (name, size, mtime, type)
* @annotation ["scriptable"]
* @param {widget_t*} widget widget对象
* @param {const char*} sort_by (name, size, mtime)
* @param {const char*} sort_by (name, size, mtime, type)
*
* @return {ret_t} RET_OK表示成功
*/

View File

@ -217,3 +217,110 @@ TEST(FileBrowser, sort_by_name) {
file_browser_cleanup(fb);
}
TEST(FileBrowser, compare_by_size) {
fb_item_t a;
fb_item_t b;
memset(&a, 0x00, sizeof(a));
memset(&b, 0x00, sizeof(b));
a.size = 100;
a.name = "a.txt";
a.is_reg_file = TRUE;
b.size = 200;
b.name = "b.txt";
b.is_reg_file = TRUE;
ASSERT_EQ(fb_compare_by_size(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_size(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_size(&a, &a) == 0, TRUE);
ASSERT_EQ(fb_compare_by_size_dec(&a, &b) > 0, TRUE);
ASSERT_EQ(fb_compare_by_size_dec(&b, &a) < 0, TRUE);
ASSERT_EQ(fb_compare_by_size_dec(&a, &a) == 0, TRUE);
}
TEST(FileBrowser, compare_by_mtime) {
fb_item_t a;
fb_item_t b;
memset(&a, 0x00, sizeof(a));
memset(&b, 0x00, sizeof(b));
a.mtime = 100;
a.name = "a.txt";
a.is_reg_file = TRUE;
b.mtime = 200;
b.name = "b.txt";
b.is_reg_file = TRUE;
ASSERT_EQ(fb_compare_by_mtime(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_mtime(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_mtime(&a, &a) == 0, TRUE);
ASSERT_EQ(fb_compare_by_mtime_dec(&a, &b) > 0, TRUE);
ASSERT_EQ(fb_compare_by_mtime_dec(&b, &a) < 0, TRUE);
ASSERT_EQ(fb_compare_by_mtime_dec(&a, &a) == 0, TRUE);
}
TEST(FileBrowser, compare_by_name) {
fb_item_t a;
fb_item_t b;
memset(&a, 0x00, sizeof(a));
memset(&b, 0x00, sizeof(b));
a.name = "a.jpg";
a.is_reg_file = TRUE;
b.name = "b.txt";
b.is_reg_file = TRUE;
ASSERT_EQ(fb_compare_by_name(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_name(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_name(&a, &a) == 0, TRUE);
ASSERT_EQ(fb_compare_by_name_dec(&a, &b) > 0, TRUE);
ASSERT_EQ(fb_compare_by_name_dec(&b, &a) < 0, TRUE);
ASSERT_EQ(fb_compare_by_name_dec(&a, &a) == 0, TRUE);
}
TEST(FileBrowser, compare_by_type) {
fb_item_t a;
fb_item_t b;
memset(&a, 0x00, sizeof(a));
memset(&b, 0x00, sizeof(b));
a.name = "a.jpg";
a.is_reg_file = TRUE;
b.name = "b.txt";
b.is_reg_file = TRUE;
ASSERT_EQ(fb_compare_by_type(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_type(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_type(&a, &a) == 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&a, &b) > 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&b, &a) < 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&a, &a) == 0, TRUE);
}
TEST(FileBrowser, compare_dir_file) {
fb_item_t a;
fb_item_t b;
memset(&a, 0x00, sizeof(a));
memset(&b, 0x00, sizeof(b));
a.name = "b";
a.is_dir = TRUE;
b.name = "a";
b.is_reg_file = TRUE;
ASSERT_EQ(fb_compare_by_type(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_type(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_type(&a, &a) == 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&a, &b) < 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&b, &a) > 0, TRUE);
ASSERT_EQ(fb_compare_by_type_dec(&a, &a) == 0, TRUE);
}