mirror of
https://github.com/lvgl/lvgl.git
synced 2026-06-01 08:54:52 +08:00
feat(file_explorer): add API to use file explorer (#3601)
Co-authored-by: 100ask <team100ask@outlook.com> Co-authored-by: Gabor Kiss-Vamosi <kisvegabor@gmail.com>
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
|
||||
Simple File Explorer
|
||||
"""""""""""""""""""""""""
|
||||
|
||||
.. lv_example:: others/ime/lv_example_file_explorer_1
|
||||
:language: c
|
||||
|
||||
Control File Explorer
|
||||
"""""""""""""""""""""""""
|
||||
|
||||
.. lv_example:: others/ime/lv_example_file_explorer_2
|
||||
:language: c
|
||||
|
||||
Custom sort
|
||||
"""""""""""""""""""""""""
|
||||
|
||||
.. lv_example:: others/ime/lv_example_file_explorer_3
|
||||
:language: c
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* @file lv_example_file_explorer.h
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef LV_EX_FILE_EXPLORER_H
|
||||
#define LV_EX_FILE_EXPLORER_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
|
||||
/*********************
|
||||
* DEFINES
|
||||
*********************/
|
||||
|
||||
/**********************
|
||||
* TYPEDEFS
|
||||
**********************/
|
||||
|
||||
/**********************
|
||||
* GLOBAL PROTOTYPES
|
||||
**********************/
|
||||
void lv_example_file_explorer_1(void);
|
||||
void lv_example_file_explorer_2(void);
|
||||
void lv_example_file_explorer_3(void);
|
||||
|
||||
/**********************
|
||||
* MACROS
|
||||
**********************/
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /*extern "C"*/
|
||||
#endif
|
||||
|
||||
#endif /*LV_EX_FILE_EXPLORER_H*/
|
||||
@@ -0,0 +1,61 @@
|
||||
|
||||
#include "../../lv_examples.h"
|
||||
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
|
||||
|
||||
static void file_explorer_event_handler(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * obj = lv_event_get_target(e);
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
char * cur_path = lv_file_explorer_get_current_path(obj);
|
||||
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
|
||||
uint16_t path_len = strlen(cur_path);
|
||||
uint16_t fn_len = strlen(sel_fn);
|
||||
|
||||
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
|
||||
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
|
||||
|
||||
strcpy(file_info, cur_path);
|
||||
strcat(file_info, sel_fn);
|
||||
|
||||
LV_LOG_USER("%s", file_info);
|
||||
}
|
||||
else LV_LOG_USER("%s%s", cur_path, sel_fn);
|
||||
}
|
||||
}
|
||||
|
||||
void lv_example_file_explorer_1(void)
|
||||
{
|
||||
lv_obj_t * file_explorer = lv_file_explorer_create(lv_scr_act());
|
||||
lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_KIND);
|
||||
|
||||
#if LV_USE_FS_WIN32
|
||||
lv_file_explorer_open_dir(file_explorer, "D:");
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
|
||||
#endif
|
||||
|
||||
#else
|
||||
/* linux */
|
||||
lv_file_explorer_open_dir(file_explorer, "/");
|
||||
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "/home");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "/home/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "/home/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "/home/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "/home/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "/");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
lv_obj_add_event_cb(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,121 @@
|
||||
|
||||
#include "../../lv_examples.h"
|
||||
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
|
||||
|
||||
static void file_explorer_event_handler(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * obj = lv_event_get_target(e);
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
char * cur_path = lv_file_explorer_get_current_path(obj);
|
||||
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
|
||||
uint16_t path_len = strlen(cur_path);
|
||||
uint16_t fn_len = strlen(sel_fn);
|
||||
|
||||
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
|
||||
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
|
||||
|
||||
strcpy(file_info, cur_path);
|
||||
strcat(file_info, sel_fn);
|
||||
|
||||
LV_LOG_USER("%s", file_info);
|
||||
}
|
||||
else LV_LOG_USER("%s%s", cur_path, sel_fn);
|
||||
}
|
||||
}
|
||||
|
||||
static void btn_event_handler(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * btn = lv_event_get_target(e);
|
||||
lv_obj_t * file_explorer = lv_event_get_user_data(e);
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
if(lv_obj_has_state(btn, LV_STATE_CHECKED))
|
||||
lv_obj_add_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
|
||||
else
|
||||
lv_obj_clear_flag(file_explorer, LV_OBJ_FLAG_HIDDEN);
|
||||
}
|
||||
}
|
||||
|
||||
static void dd_event_handler(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * dd = lv_event_get_target(e);
|
||||
lv_obj_t * fe_quick_access_obj = lv_event_get_user_data(e);
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
char buf[32];
|
||||
lv_dropdown_get_selected_str(dd, buf, sizeof(buf));
|
||||
if(strcmp(buf, "NONE") == 0) {
|
||||
lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_NONE);
|
||||
}
|
||||
else if(strcmp(buf, "KIND") == 0) {
|
||||
lv_file_explorer_set_sort(fe_quick_access_obj, LV_EXPLORER_SORT_KIND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lv_example_file_explorer_2(void)
|
||||
{
|
||||
lv_obj_t * file_explorer = lv_file_explorer_create(lv_scr_act());
|
||||
|
||||
#if LV_USE_FS_WIN32
|
||||
lv_file_explorer_open_dir(file_explorer, "D:");
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
|
||||
#endif
|
||||
|
||||
#else
|
||||
/* linux */
|
||||
lv_file_explorer_open_dir(file_explorer, "/");
|
||||
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "/home");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "/home/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "/home/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "/home/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "/home/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "/");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
lv_obj_add_event_cb(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
|
||||
|
||||
/*Quick access status control button*/
|
||||
lv_obj_t * fe_quick_access_obj = lv_file_explorer_get_quick_access_area(file_explorer);
|
||||
lv_obj_t * fe_header_obj = lv_file_explorer_get_header(file_explorer);
|
||||
lv_obj_t * btn = lv_btn_create(fe_header_obj);
|
||||
lv_obj_set_style_radius(btn, 2, 0);
|
||||
lv_obj_set_style_pad_all(btn, 4, 0);
|
||||
lv_obj_align(btn, LV_ALIGN_LEFT_MID, 0, 0);
|
||||
lv_obj_add_flag(btn, LV_OBJ_FLAG_CHECKABLE);
|
||||
|
||||
lv_obj_t * label = lv_label_create(btn);
|
||||
lv_label_set_text(label, LV_SYMBOL_LIST);
|
||||
lv_obj_center(label);
|
||||
|
||||
lv_obj_add_event_cb(btn, btn_event_handler, LV_EVENT_VALUE_CHANGED, fe_quick_access_obj);
|
||||
|
||||
/*Sort control*/
|
||||
static const char * opts = "NONE\n"
|
||||
"KIND";
|
||||
|
||||
lv_obj_t * dd = lv_dropdown_create(fe_header_obj);
|
||||
lv_obj_set_style_radius(dd, 4, 0);
|
||||
lv_obj_set_style_pad_all(dd, 0, 0);
|
||||
lv_obj_set_size(dd, LV_PCT(30), LV_SIZE_CONTENT);
|
||||
lv_dropdown_set_options_static(dd, opts);
|
||||
lv_obj_align(dd, LV_ALIGN_RIGHT_MID, 0, 0);
|
||||
//lv_obj_align_to(dd, btn, LV_ALIGN_OUT_RIGHT_MID, 0, 0);
|
||||
|
||||
lv_obj_add_event_cb(dd, dd_event_handler, LV_EVENT_VALUE_CHANGED, file_explorer);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,107 @@
|
||||
|
||||
#include "../../lv_examples.h"
|
||||
#if LV_USE_TABLE && LV_USE_FILE_EXPLORER && (LV_USE_FS_STDIO || LV_USE_FS_POSIX || LV_USE_FS_WIN32 || LV_USE_FS_FATFS) && LV_BUILD_EXAMPLES
|
||||
|
||||
|
||||
static void exch_table_item(lv_obj_t * tb, int16_t i, int16_t j)
|
||||
{
|
||||
const char * tmp;
|
||||
tmp = lv_table_get_cell_value(tb, i, 0);
|
||||
lv_table_set_cell_value(tb, 0, 2, tmp);
|
||||
lv_table_set_cell_value(tb, i, 0, lv_table_get_cell_value(tb, j, 0));
|
||||
lv_table_set_cell_value(tb, j, 0, lv_table_get_cell_value(tb, 0, 2));
|
||||
|
||||
tmp = lv_table_get_cell_value(tb, i, 1);
|
||||
lv_table_set_cell_value(tb, 0, 2, tmp);
|
||||
lv_table_set_cell_value(tb, i, 1, lv_table_get_cell_value(tb, j, 1));
|
||||
lv_table_set_cell_value(tb, j, 1, lv_table_get_cell_value(tb, 0, 2));
|
||||
}
|
||||
|
||||
|
||||
/*Quick sort 3 way*/
|
||||
static void sort_by_file_kind(lv_obj_t * tb, int16_t lo, int16_t hi)
|
||||
{
|
||||
if(lo >= hi) return;
|
||||
|
||||
int16_t lt = lo;
|
||||
int16_t i = lo + 1;
|
||||
int16_t gt = hi;
|
||||
const char * v = lv_table_get_cell_value(tb, lo, 1);
|
||||
while(i <= gt) {
|
||||
if(strcmp(lv_table_get_cell_value(tb, i, 1), v) < 0)
|
||||
exch_table_item(tb, lt++, i++);
|
||||
else if(strcmp(lv_table_get_cell_value(tb, i, 1), v) > 0)
|
||||
exch_table_item(tb, i, gt--);
|
||||
else
|
||||
i++;
|
||||
}
|
||||
|
||||
sort_by_file_kind(tb, lo, lt - 1);
|
||||
sort_by_file_kind(tb, gt + 1, hi);
|
||||
}
|
||||
|
||||
|
||||
static void file_explorer_event_handler(lv_event_t * e)
|
||||
{
|
||||
lv_event_code_t code = lv_event_get_code(e);
|
||||
lv_obj_t * obj = lv_event_get_target(e);
|
||||
|
||||
if(code == LV_EVENT_VALUE_CHANGED) {
|
||||
char * cur_path = lv_file_explorer_get_current_path(obj);
|
||||
const char * sel_fn = lv_file_explorer_get_selected_file_name(obj);
|
||||
uint16_t path_len = strlen(cur_path);
|
||||
uint16_t fn_len = strlen(sel_fn);
|
||||
|
||||
if((path_len + fn_len) <= LV_FILE_EXPLORER_PATH_MAX_LEN) {
|
||||
char file_info[LV_FILE_EXPLORER_PATH_MAX_LEN];
|
||||
|
||||
strcpy(file_info, cur_path);
|
||||
strcat(file_info, sel_fn);
|
||||
|
||||
LV_LOG_USER("%s", file_info);
|
||||
}
|
||||
else LV_LOG_USER("%s%s", cur_path, sel_fn);
|
||||
}
|
||||
else if(code == LV_EVENT_READY) {
|
||||
lv_obj_t * tb = lv_file_explorer_get_file_table(obj);
|
||||
uint16_t sum = lv_table_get_row_cnt(tb);
|
||||
|
||||
sort_by_file_kind(tb, 0, (sum - 1));
|
||||
}
|
||||
}
|
||||
|
||||
void lv_example_file_explorer_3(void)
|
||||
{
|
||||
lv_obj_t * file_explorer = lv_file_explorer_create(lv_scr_act());
|
||||
/*Before custom sort, please set the default sorting to NONE. The default is NONE.*/
|
||||
lv_file_explorer_set_sort(file_explorer, LV_EXPLORER_SORT_NONE);
|
||||
|
||||
#if LV_USE_FS_WIN32
|
||||
lv_file_explorer_open_dir(file_explorer, "D:");
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "C:/Users/Public/Desktop");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "C:/Users/Public/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "C:/Users/Public/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "C:/Users/Public/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "C:/Users/Public/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "D:");
|
||||
#endif
|
||||
|
||||
#else
|
||||
/* linux */
|
||||
lv_file_explorer_open_dir(file_explorer, "/");
|
||||
|
||||
#if LV_FILE_EXPLORER_QUICK_ACCESS
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_HOME_DIR, "/home");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_VIDEO_DIR, "/home/Videos");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_PICTURES_DIR, "/home/Pictures");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_MUSIC_DIR, "/home/Music");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_DOCS_DIR, "/home/Documents");
|
||||
lv_file_explorer_set_quick_access_path(file_explorer, LV_EXPLORER_FS_DIR, "/");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
lv_obj_add_event_cb(file_explorer, file_explorer_event_handler, LV_EVENT_ALL, NULL);
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -13,6 +13,7 @@ extern "C" {
|
||||
/*********************
|
||||
* INCLUDES
|
||||
*********************/
|
||||
#include "file_explorer/lv_example_file_explorer.h"
|
||||
#include "fragment/lv_example_fragment.h"
|
||||
#include "gridnav/lv_example_gridnav.h"
|
||||
#include "ime/lv_example_ime_pinyin.h"
|
||||
|
||||
Reference in New Issue
Block a user