update file browser

This commit is contained in:
r3df0xx 2022-05-27 16:30:54 +03:00
parent 122629935e
commit 6995dd6b13
6 changed files with 19 additions and 33 deletions

View file

@ -48,7 +48,7 @@ FileBrowserApp* file_browser_app_alloc(char* arg) {
app->widget = widget_alloc();
string_init(app->file_path);
app->file_browser = file_browser_alloc(&(app->file_path));
app->file_browser = file_browser_alloc(app->file_path);
file_browser_configure(app->file_browser, "*", true, &I_badusb_10px, true);
view_dispatcher_add_view(

View file

@ -20,12 +20,10 @@ bool file_browser_scene_browser_on_event(void* context, SceneManagerEvent event)
return consumed;
}
static void file_browser_callback(void* context, bool state) {
static void file_browser_callback(void* context) {
FileBrowserApp* app = context;
furi_assert(app);
view_dispatcher_send_custom_event(app->view_dispatcher, SceneManagerEventTypeCustom);
UNUSED(state);
}
void file_browser_scene_browser_on_enter(void* context) {

View file

@ -10,6 +10,7 @@
#include <m-array.h>
#include <gui/elements.h>
#include <furi.h>
#include "toolbox/path.h"
#define LIST_ITEMS 5u
#define MAX_LEN_PX 110
@ -60,13 +61,13 @@ ARRAY_DEF(
struct FileBrowser {
View* view;
BrowserWorker* worker;
char* ext_filter;
const char* ext_filter;
bool skip_assets;
FileBrowserCallback callback;
void* context;
string_t* result_path;
string_ptr result_path;
};
typedef struct {
@ -100,7 +101,7 @@ static void browser_list_load_cb(void* context, uint32_t list_load_offset);
static void browser_list_item_cb(void* context, string_t item_path, bool is_folder, bool is_last);
static void browser_long_load_cb(void* context);
FileBrowser* file_browser_alloc(string_t* result_path) {
FileBrowser* file_browser_alloc(string_ptr result_path) {
furi_assert(result_path);
FileBrowser* browser = malloc(sizeof(FileBrowser));
browser->view = view_alloc();
@ -140,7 +141,7 @@ View* file_browser_get_view(FileBrowser* browser) {
void file_browser_configure(
FileBrowser* browser,
char* extension,
const char* extension,
bool skip_assets,
const Icon* file_icon,
bool hide_ext) {
@ -250,6 +251,7 @@ static void
with_view_model(
browser->view, (FileBrowserModel * model) {
items_array_reset(model->items);
if(is_root) {
model->item_cnt = item_cnt;
model->item_idx = (file_idx > 0) ? file_idx : 0;
@ -383,7 +385,7 @@ static void browser_draw_list(Canvas* canvas, FileBrowserModel* model) {
BrowserItem_t* item = items_array_get(
model->items, CLAMP(idx - model->array_offset, (int32_t)(array_size - 1), 0));
item_type = item->type;
file_browser_worker_get_filename(
path_extract_filename(
item->path, filename, (model->hide_ext) && (item_type == BrowserItemTypeFile));
} else {
string_set_str(filename, "---");
@ -505,9 +507,9 @@ static bool file_browser_view_input_callback(InputEvent* event, void* context) {
file_browser_worker_folder_enter(
browser->worker, selected_item->path, select_index);
} else if(selected_item->type == BrowserItemTypeFile) {
string_set(*(browser->result_path), selected_item->path);
string_set(browser->result_path, selected_item->path);
if(browser->callback) {
browser->callback(browser->context, true);
browser->callback(browser->context);
}
}
}

View file

@ -13,9 +13,9 @@ extern "C" {
#endif
typedef struct FileBrowser FileBrowser;
typedef void (*FileBrowserCallback)(void* context, bool state);
typedef void (*FileBrowserCallback)(void* context);
FileBrowser* file_browser_alloc(string_t* result_path);
FileBrowser* file_browser_alloc(string_ptr result_path);
void file_browser_free(FileBrowser* browser);
@ -23,7 +23,7 @@ View* file_browser_get_view(FileBrowser* browser);
void file_browser_configure(
FileBrowser* browser,
char* extension,
const char* extension,
bool skip_assets,
const Icon* file_icon,
bool hide_ext);

View file

@ -8,6 +8,7 @@
#include <storage/storage.h>
#include <furi.h>
#include <stddef.h>
#include "toolbox/path.h"
#define TAG "BrowserWorker"
@ -149,6 +150,7 @@ static bool browser_folder_init(
(*item_cnt)++;
}
if(total_files_cnt == LONG_LOAD_THRESHOLD) {
// There are too many files in folder and counting them will take some time - send callback to app
if(browser->long_load_cb) {
browser->long_load_cb(browser->cb_ctx);
}
@ -255,7 +257,7 @@ static int32_t browser_worker(void* context) {
string_t filename;
string_init(filename);
if(browser_path_is_file(browser->path_next)) {
file_browser_worker_get_filename(browser->path_next, filename, false);
path_extract_filename(browser->path_next, filename, false);
}
osThreadFlagsSet(furi_thread_get_thread_id(browser->thread), WorkerEvtFolderEnter);
@ -319,21 +321,7 @@ static int32_t browser_worker(void* context) {
return 0;
}
void file_browser_worker_get_filename(string_t path, string_t name, bool trim_ext) {
size_t filename_start = string_search_rchar(path, '/');
if(filename_start > 0) {
filename_start++;
string_set_n(name, path, filename_start, string_size(path) - filename_start);
}
if(trim_ext) {
size_t dot = string_search_rchar(name, '.');
if(dot > 0) {
string_left(name, dot);
}
}
}
BrowserWorker* file_browser_worker_alloc(string_t path, char* filter_ext, bool skip_assets) {
BrowserWorker* file_browser_worker_alloc(string_t path, const char* filter_ext, bool skip_assets) {
BrowserWorker* browser = malloc(sizeof(BrowserWorker));
idx_last_array_init(browser->idx_last);

View file

@ -22,9 +22,7 @@ typedef void (*BrowserWorkerListItemCallback)(
bool is_last);
typedef void (*BrowserWorkerLongLoadCallback)(void* context);
void file_browser_worker_get_filename(string_t path, string_t name, bool trim_ext);
BrowserWorker* file_browser_worker_alloc(string_t path, char* filter_ext, bool skip_assets);
BrowserWorker* file_browser_worker_alloc(string_t path, const char* filter_ext, bool skip_assets);
void file_browser_worker_free(BrowserWorker* browser);