2022-05-27 11:19:21 +00:00
|
|
|
#include "file_browser.h"
|
|
|
|
#include "file_browser_worker.h"
|
2023-02-07 16:33:05 +00:00
|
|
|
|
|
|
|
#include <gui/elements.h>
|
|
|
|
#include <assets_icons.h>
|
|
|
|
#include <toolbox/path.h>
|
|
|
|
|
|
|
|
#include <furi.h>
|
|
|
|
#include <furi_hal_resources.h>
|
|
|
|
|
2022-07-20 10:56:33 +00:00
|
|
|
#include <core/check.h>
|
|
|
|
#include <core/common_defines.h>
|
|
|
|
#include <core/log.h>
|
2022-05-27 11:19:21 +00:00
|
|
|
#include <m-array.h>
|
|
|
|
|
|
|
|
#define LIST_ITEMS 5u
|
|
|
|
#define MAX_LEN_PX 110
|
|
|
|
#define FRAME_HEIGHT 12
|
|
|
|
#define Y_OFFSET 3
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
#define ITEM_LIST_LEN_MAX 50
|
|
|
|
|
|
|
|
#define CUSTOM_ICON_MAX_SIZE 32
|
2022-05-27 11:19:21 +00:00
|
|
|
|
2022-12-20 11:11:52 +00:00
|
|
|
#define SCROLL_INTERVAL (333)
|
|
|
|
#define SCROLL_DELAY (2)
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
typedef enum {
|
|
|
|
BrowserItemTypeLoading,
|
|
|
|
BrowserItemTypeBack,
|
|
|
|
BrowserItemTypeFolder,
|
|
|
|
BrowserItemTypeFile,
|
|
|
|
} BrowserItemType;
|
|
|
|
|
|
|
|
typedef struct {
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* path;
|
2022-05-27 11:19:21 +00:00
|
|
|
BrowserItemType type;
|
2022-09-14 16:11:38 +00:00
|
|
|
uint8_t* custom_icon_data;
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* display_name;
|
2022-05-27 11:19:21 +00:00
|
|
|
} BrowserItem_t;
|
|
|
|
|
|
|
|
static void BrowserItem_t_init(BrowserItem_t* obj) {
|
|
|
|
obj->type = BrowserItemTypeLoading;
|
2022-10-05 15:15:23 +00:00
|
|
|
obj->path = furi_string_alloc();
|
|
|
|
obj->display_name = furi_string_alloc();
|
2022-09-14 16:11:38 +00:00
|
|
|
obj->custom_icon_data = NULL;
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void BrowserItem_t_init_set(BrowserItem_t* obj, const BrowserItem_t* src) {
|
|
|
|
obj->type = src->type;
|
2022-10-05 15:15:23 +00:00
|
|
|
obj->path = furi_string_alloc_set(src->path);
|
|
|
|
obj->display_name = furi_string_alloc_set(src->display_name);
|
2022-09-14 16:11:38 +00:00
|
|
|
if(src->custom_icon_data) {
|
|
|
|
obj->custom_icon_data = malloc(CUSTOM_ICON_MAX_SIZE);
|
|
|
|
memcpy(obj->custom_icon_data, src->custom_icon_data, CUSTOM_ICON_MAX_SIZE);
|
|
|
|
} else {
|
|
|
|
obj->custom_icon_data = NULL;
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void BrowserItem_t_set(BrowserItem_t* obj, const BrowserItem_t* src) {
|
|
|
|
obj->type = src->type;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(obj->path, src->path);
|
|
|
|
furi_string_set(obj->display_name, src->display_name);
|
2022-09-14 16:11:38 +00:00
|
|
|
if(src->custom_icon_data) {
|
|
|
|
memcpy(obj->custom_icon_data, src->custom_icon_data, CUSTOM_ICON_MAX_SIZE);
|
|
|
|
} else {
|
|
|
|
obj->custom_icon_data = NULL;
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void BrowserItem_t_clear(BrowserItem_t* obj) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(obj->path);
|
|
|
|
furi_string_free(obj->display_name);
|
2022-09-14 16:11:38 +00:00
|
|
|
if(obj->custom_icon_data) {
|
|
|
|
free(obj->custom_icon_data);
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ARRAY_DEF(
|
|
|
|
items_array,
|
|
|
|
BrowserItem_t,
|
|
|
|
(INIT(API_2(BrowserItem_t_init)),
|
|
|
|
SET(API_6(BrowserItem_t_set)),
|
|
|
|
INIT_SET(API_6(BrowserItem_t_init_set)),
|
|
|
|
CLEAR(API_2(BrowserItem_t_clear))))
|
|
|
|
|
|
|
|
struct FileBrowser {
|
|
|
|
View* view;
|
|
|
|
BrowserWorker* worker;
|
|
|
|
const char* ext_filter;
|
2022-12-07 14:17:41 +00:00
|
|
|
const char* base_path;
|
2022-05-27 11:19:21 +00:00
|
|
|
bool skip_assets;
|
2022-12-07 10:52:44 +00:00
|
|
|
bool hide_dot_files;
|
2022-09-14 16:11:38 +00:00
|
|
|
bool hide_ext;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
FileBrowserCallback callback;
|
|
|
|
void* context;
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
FileBrowserLoadItemCallback item_callback;
|
|
|
|
void* item_context;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* result_path;
|
2022-12-20 11:11:52 +00:00
|
|
|
FuriTimer* scroll_timer;
|
2022-05-27 11:19:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
items_array_t items;
|
|
|
|
|
|
|
|
bool is_root;
|
|
|
|
bool folder_loading;
|
|
|
|
bool list_loading;
|
|
|
|
uint32_t item_cnt;
|
|
|
|
int32_t item_idx;
|
|
|
|
int32_t array_offset;
|
|
|
|
int32_t list_offset;
|
|
|
|
|
|
|
|
const Icon* file_icon;
|
|
|
|
bool hide_ext;
|
2022-12-20 11:11:52 +00:00
|
|
|
size_t scroll_counter;
|
2023-06-27 09:22:35 +00:00
|
|
|
|
|
|
|
uint32_t button_held_for_ticks;
|
2022-05-27 11:19:21 +00:00
|
|
|
} FileBrowserModel;
|
|
|
|
|
|
|
|
static const Icon* BrowserItemIcons[] = {
|
|
|
|
[BrowserItemTypeLoading] = &I_loading_10px,
|
|
|
|
[BrowserItemTypeBack] = &I_back_10px,
|
|
|
|
[BrowserItemTypeFolder] = &I_dir_10px,
|
|
|
|
[BrowserItemTypeFile] = &I_unknown_10px,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void file_browser_view_draw_callback(Canvas* canvas, void* _model);
|
|
|
|
static bool file_browser_view_input_callback(InputEvent* event, void* context);
|
|
|
|
|
|
|
|
static void
|
|
|
|
browser_folder_open_cb(void* context, uint32_t item_cnt, int32_t file_idx, bool is_root);
|
|
|
|
static void browser_list_load_cb(void* context, uint32_t list_load_offset);
|
2022-10-05 15:15:23 +00:00
|
|
|
static void
|
|
|
|
browser_list_item_cb(void* context, FuriString* item_path, bool is_folder, bool is_last);
|
2022-05-27 11:19:21 +00:00
|
|
|
static void browser_long_load_cb(void* context);
|
|
|
|
|
2022-12-20 11:11:52 +00:00
|
|
|
static void file_browser_scroll_timer_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = context;
|
|
|
|
with_view_model(
|
|
|
|
browser->view, FileBrowserModel * model, { model->scroll_counter++; }, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void file_browser_view_enter_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = context;
|
|
|
|
with_view_model(
|
|
|
|
browser->view, FileBrowserModel * model, { model->scroll_counter = 0; }, true);
|
|
|
|
furi_timer_start(browser->scroll_timer, SCROLL_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void file_browser_view_exit_callback(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = context;
|
|
|
|
furi_timer_stop(browser->scroll_timer);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FileBrowser* file_browser_alloc(FuriString* result_path) {
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_assert(result_path);
|
|
|
|
FileBrowser* browser = malloc(sizeof(FileBrowser));
|
|
|
|
browser->view = view_alloc();
|
|
|
|
view_allocate_model(browser->view, ViewModelTypeLocking, sizeof(FileBrowserModel));
|
|
|
|
view_set_context(browser->view, browser);
|
|
|
|
view_set_draw_callback(browser->view, file_browser_view_draw_callback);
|
|
|
|
view_set_input_callback(browser->view, file_browser_view_input_callback);
|
2022-12-20 11:11:52 +00:00
|
|
|
view_set_enter_callback(browser->view, file_browser_view_enter_callback);
|
|
|
|
view_set_exit_callback(browser->view, file_browser_view_exit_callback);
|
|
|
|
|
|
|
|
browser->scroll_timer =
|
|
|
|
furi_timer_alloc(file_browser_scroll_timer_callback, FuriTimerTypePeriodic, browser);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
browser->result_path = result_path;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { items_array_init(model->items); }, false);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_free(FileBrowser* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
|
2022-12-20 11:11:52 +00:00
|
|
|
furi_timer_free(browser->scroll_timer);
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { items_array_clear(model->items); }, false);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
view_free(browser->view);
|
|
|
|
free(browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
View* file_browser_get_view(FileBrowser* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
return browser->view;
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_configure(
|
|
|
|
FileBrowser* browser,
|
|
|
|
const char* extension,
|
2022-12-07 14:17:41 +00:00
|
|
|
const char* base_path,
|
2022-05-27 11:19:21 +00:00
|
|
|
bool skip_assets,
|
2022-12-07 10:52:44 +00:00
|
|
|
bool hide_dot_files,
|
2022-05-27 11:19:21 +00:00
|
|
|
const Icon* file_icon,
|
|
|
|
bool hide_ext) {
|
|
|
|
furi_assert(browser);
|
|
|
|
|
|
|
|
browser->ext_filter = extension;
|
|
|
|
browser->skip_assets = skip_assets;
|
2022-09-14 16:11:38 +00:00
|
|
|
browser->hide_ext = hide_ext;
|
2022-12-07 14:17:41 +00:00
|
|
|
browser->base_path = base_path;
|
2022-12-07 10:52:44 +00:00
|
|
|
browser->hide_dot_files = hide_dot_files;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
model->file_icon = file_icon;
|
|
|
|
model->hide_ext = hide_ext;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
void file_browser_start(FileBrowser* browser, FuriString* path) {
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_assert(browser);
|
2022-12-07 10:52:44 +00:00
|
|
|
browser->worker = file_browser_worker_alloc(
|
2022-12-07 14:17:41 +00:00
|
|
|
path,
|
|
|
|
browser->base_path,
|
|
|
|
browser->ext_filter,
|
|
|
|
browser->skip_assets,
|
|
|
|
browser->hide_dot_files);
|
2022-05-27 11:19:21 +00:00
|
|
|
file_browser_worker_set_callback_context(browser->worker, browser);
|
|
|
|
file_browser_worker_set_folder_callback(browser->worker, browser_folder_open_cb);
|
|
|
|
file_browser_worker_set_list_callback(browser->worker, browser_list_load_cb);
|
|
|
|
file_browser_worker_set_item_callback(browser->worker, browser_list_item_cb);
|
|
|
|
file_browser_worker_set_long_load_callback(browser->worker, browser_long_load_cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_stop(FileBrowser* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
file_browser_worker_free(browser->worker);
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
items_array_reset(model->items);
|
|
|
|
model->item_cnt = 0;
|
|
|
|
model->item_idx = 0;
|
|
|
|
model->array_offset = 0;
|
|
|
|
model->list_offset = 0;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void file_browser_set_callback(FileBrowser* browser, FileBrowserCallback callback, void* context) {
|
|
|
|
browser->context = context;
|
|
|
|
browser->callback = callback;
|
|
|
|
}
|
|
|
|
|
2022-09-14 16:11:38 +00:00
|
|
|
void file_browser_set_item_callback(
|
|
|
|
FileBrowser* browser,
|
|
|
|
FileBrowserLoadItemCallback callback,
|
|
|
|
void* context) {
|
|
|
|
browser->item_context = context;
|
|
|
|
browser->item_callback = callback;
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
static bool browser_is_item_in_array(FileBrowserModel* model, uint32_t idx) {
|
|
|
|
size_t array_size = items_array_size(model->items);
|
|
|
|
|
|
|
|
if((idx >= (uint32_t)model->array_offset + array_size) ||
|
|
|
|
(idx < (uint32_t)model->array_offset)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool browser_is_list_load_required(FileBrowserModel* model) {
|
|
|
|
size_t array_size = items_array_size(model->items);
|
2022-11-28 18:59:24 +00:00
|
|
|
if((array_size > 0) && (!model->is_root) && (model->array_offset == 0)) {
|
|
|
|
array_size--;
|
|
|
|
}
|
|
|
|
uint32_t item_cnt = (model->is_root) ? (model->item_cnt) : (model->item_cnt - 1);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
if((model->list_loading) || (array_size >= item_cnt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((model->array_offset > 0) &&
|
|
|
|
(model->item_idx < (model->array_offset + ITEM_LIST_LEN_MAX / 4))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(((model->array_offset + array_size) < item_cnt) &&
|
|
|
|
(model->item_idx > (int32_t)(model->array_offset + array_size - ITEM_LIST_LEN_MAX / 4))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_update_offset(FileBrowser* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
uint16_t bounds = model->item_cnt > (LIST_ITEMS - 1) ? 2 : model->item_cnt;
|
|
|
|
|
|
|
|
if((model->item_cnt > (LIST_ITEMS - 1)) &&
|
|
|
|
(model->item_idx >= ((int32_t)model->item_cnt - 1))) {
|
|
|
|
model->list_offset = model->item_idx - (LIST_ITEMS - 1);
|
|
|
|
} else if(model->list_offset < model->item_idx - bounds) {
|
|
|
|
model->list_offset = CLAMP(
|
|
|
|
model->item_idx - (int32_t)(LIST_ITEMS - 2),
|
|
|
|
(int32_t)model->item_cnt - bounds,
|
|
|
|
0);
|
|
|
|
} else if(model->list_offset > model->item_idx - bounds) {
|
|
|
|
model->list_offset =
|
|
|
|
CLAMP(model->item_idx - 1, (int32_t)model->item_cnt - bounds, 0);
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
browser_folder_open_cb(void* context, uint32_t item_cnt, int32_t file_idx, bool is_root) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = (FileBrowser*)context;
|
|
|
|
|
|
|
|
int32_t load_offset = 0;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
items_array_reset(model->items);
|
|
|
|
if(is_root) {
|
|
|
|
model->item_cnt = item_cnt;
|
|
|
|
model->item_idx = (file_idx > 0) ? file_idx : 0;
|
|
|
|
load_offset =
|
|
|
|
CLAMP(model->item_idx - ITEM_LIST_LEN_MAX / 2, (int32_t)model->item_cnt, 0);
|
|
|
|
} else {
|
|
|
|
model->item_cnt = item_cnt + 1;
|
|
|
|
model->item_idx = file_idx + 1;
|
|
|
|
load_offset = CLAMP(
|
|
|
|
model->item_idx - ITEM_LIST_LEN_MAX / 2 - 1, (int32_t)model->item_cnt - 1, 0);
|
|
|
|
}
|
|
|
|
model->array_offset = 0;
|
|
|
|
model->list_offset = 0;
|
|
|
|
model->is_root = is_root;
|
|
|
|
model->list_loading = true;
|
|
|
|
model->folder_loading = false;
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser_update_offset(browser);
|
|
|
|
|
|
|
|
file_browser_worker_load(browser->worker, load_offset, ITEM_LIST_LEN_MAX);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_list_load_cb(void* context, uint32_t list_load_offset) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = (FileBrowser*)context;
|
|
|
|
|
|
|
|
BrowserItem_t back_item;
|
|
|
|
BrowserItem_t_init(&back_item);
|
|
|
|
back_item.type = BrowserItemTypeBack;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
items_array_reset(model->items);
|
|
|
|
model->array_offset = list_load_offset;
|
|
|
|
if(!model->is_root) {
|
|
|
|
if(list_load_offset == 0) {
|
|
|
|
items_array_push_back(model->items, back_item);
|
|
|
|
} else {
|
|
|
|
model->array_offset += 1;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
BrowserItem_t_clear(&back_item);
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
static void
|
|
|
|
browser_list_item_cb(void* context, FuriString* item_path, bool is_folder, bool is_last) {
|
2022-05-27 11:19:21 +00:00
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = (FileBrowser*)context;
|
|
|
|
|
|
|
|
BrowserItem_t item;
|
2022-09-14 16:11:38 +00:00
|
|
|
item.custom_icon_data = NULL;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
if(!is_last) {
|
2022-10-05 15:15:23 +00:00
|
|
|
item.path = furi_string_alloc_set(item_path);
|
|
|
|
item.display_name = furi_string_alloc();
|
2022-09-14 16:11:38 +00:00
|
|
|
if(is_folder) {
|
|
|
|
item.type = BrowserItemTypeFolder;
|
|
|
|
} else {
|
|
|
|
item.type = BrowserItemTypeFile;
|
|
|
|
if(browser->item_callback) {
|
|
|
|
item.custom_icon_data = malloc(CUSTOM_ICON_MAX_SIZE);
|
|
|
|
if(!browser->item_callback(
|
|
|
|
item_path,
|
|
|
|
browser->item_context,
|
|
|
|
&item.custom_icon_data,
|
|
|
|
item.display_name)) {
|
|
|
|
free(item.custom_icon_data);
|
|
|
|
item.custom_icon_data = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
if(furi_string_empty(item.display_name)) {
|
2022-09-14 16:11:38 +00:00
|
|
|
path_extract_filename(
|
|
|
|
item_path,
|
|
|
|
item.display_name,
|
|
|
|
(browser->hide_ext) && (item.type == BrowserItemTypeFile));
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
items_array_push_back(model->items, item);
|
2022-09-14 16:11:38 +00:00
|
|
|
// TODO: calculate if element is visible
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(item.display_name);
|
|
|
|
furi_string_free(item.path);
|
2022-10-08 10:57:53 +00:00
|
|
|
if(item.custom_icon_data) {
|
|
|
|
free(item.custom_icon_data);
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
} else {
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { model->list_loading = false; }, true);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_long_load_cb(void* context) {
|
|
|
|
furi_assert(context);
|
|
|
|
FileBrowser* browser = (FileBrowser*)context;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { model->folder_loading = true; }, true);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar) {
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_box(
|
|
|
|
canvas, 0, Y_OFFSET + idx * FRAME_HEIGHT, (scrollbar ? 122 : 127), FRAME_HEIGHT);
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_dot(canvas, 0, Y_OFFSET + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(canvas, 1, Y_OFFSET + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(canvas, 0, (Y_OFFSET + idx * FRAME_HEIGHT) + 1);
|
|
|
|
|
|
|
|
canvas_draw_dot(canvas, 0, (Y_OFFSET + idx * FRAME_HEIGHT) + (FRAME_HEIGHT - 1));
|
|
|
|
canvas_draw_dot(canvas, scrollbar ? 121 : 126, Y_OFFSET + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(
|
|
|
|
canvas, scrollbar ? 121 : 126, (Y_OFFSET + idx * FRAME_HEIGHT) + (FRAME_HEIGHT - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_draw_loading(Canvas* canvas, FileBrowserModel* model) {
|
|
|
|
UNUSED(model);
|
|
|
|
|
[FL-1962, FL-2464, FL-2465, FL-2466, FL-2560, FL-2637, FL-2595] Ibutton, Infrared, LfRFID GUI fixes (#1392)
* Ibutton, Infrared, LfRFID GUI fixes
* Loading screens update
Co-authored-by: あく <alleteam@gmail.com>
2022-07-17 07:41:16 +00:00
|
|
|
uint8_t x = 128 / 2 - 24 / 2;
|
|
|
|
uint8_t y = 64 / 2 - 24 / 2;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
[FL-1962, FL-2464, FL-2465, FL-2466, FL-2560, FL-2637, FL-2595] Ibutton, Infrared, LfRFID GUI fixes (#1392)
* Ibutton, Infrared, LfRFID GUI fixes
* Loading screens update
Co-authored-by: あく <alleteam@gmail.com>
2022-07-17 07:41:16 +00:00
|
|
|
canvas_draw_icon(canvas, x, y, &A_Loading_24);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void browser_draw_list(Canvas* canvas, FileBrowserModel* model) {
|
|
|
|
uint32_t array_size = items_array_size(model->items);
|
|
|
|
bool show_scrollbar = model->item_cnt > LIST_ITEMS;
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
FuriString* filename;
|
|
|
|
filename = furi_string_alloc();
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
for(uint32_t i = 0; i < MIN(model->item_cnt, LIST_ITEMS); i++) {
|
|
|
|
int32_t idx = CLAMP((uint32_t)(i + model->list_offset), model->item_cnt, 0u);
|
|
|
|
|
|
|
|
BrowserItemType item_type = BrowserItemTypeLoading;
|
2022-09-14 16:11:38 +00:00
|
|
|
uint8_t* custom_icon_data = NULL;
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
if(browser_is_item_in_array(model, idx)) {
|
|
|
|
BrowserItem_t* item = items_array_get(
|
|
|
|
model->items, CLAMP(idx - model->array_offset, (int32_t)(array_size - 1), 0));
|
|
|
|
item_type = item->type;
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(filename, item->display_name);
|
2022-09-14 16:11:38 +00:00
|
|
|
if(item_type == BrowserItemTypeFile) {
|
|
|
|
custom_icon_data = item->custom_icon_data;
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
} else {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(filename, "---");
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(item_type == BrowserItemTypeBack) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(filename, ". .");
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-20 11:11:52 +00:00
|
|
|
size_t scroll_counter = model->scroll_counter;
|
2022-05-27 11:19:21 +00:00
|
|
|
if(model->item_idx == idx) {
|
|
|
|
browser_draw_frame(canvas, i, show_scrollbar);
|
2022-12-20 11:11:52 +00:00
|
|
|
if(scroll_counter < SCROLL_DELAY) {
|
|
|
|
scroll_counter = 0;
|
|
|
|
} else {
|
|
|
|
scroll_counter -= SCROLL_DELAY;
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
} else {
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2022-12-20 11:11:52 +00:00
|
|
|
scroll_counter = 0;
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 12:13:30 +00:00
|
|
|
if(custom_icon_data) { //-V547
|
2022-09-14 16:11:38 +00:00
|
|
|
// Currently only 10*10 icons are supported
|
|
|
|
canvas_draw_bitmap(
|
|
|
|
canvas, 2, Y_OFFSET + 1 + i * FRAME_HEIGHT, 10, 10, custom_icon_data);
|
|
|
|
} else if((item_type == BrowserItemTypeFile) && (model->file_icon)) {
|
2022-05-27 11:19:21 +00:00
|
|
|
canvas_draw_icon(canvas, 2, Y_OFFSET + 1 + i * FRAME_HEIGHT, model->file_icon);
|
|
|
|
} else if(BrowserItemIcons[item_type] != NULL) {
|
|
|
|
canvas_draw_icon(
|
|
|
|
canvas, 2, Y_OFFSET + 1 + i * FRAME_HEIGHT, BrowserItemIcons[item_type]);
|
|
|
|
}
|
2022-12-20 11:11:52 +00:00
|
|
|
elements_scrollable_text_line(
|
|
|
|
canvas,
|
|
|
|
15,
|
|
|
|
Y_OFFSET + 9 + i * FRAME_HEIGHT,
|
|
|
|
(show_scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX),
|
|
|
|
filename,
|
|
|
|
scroll_counter,
|
|
|
|
(model->item_idx != idx));
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(show_scrollbar) {
|
|
|
|
elements_scrollbar_pos(
|
|
|
|
canvas,
|
|
|
|
126,
|
|
|
|
Y_OFFSET,
|
|
|
|
canvas_height(canvas) - Y_OFFSET,
|
|
|
|
model->item_idx,
|
|
|
|
model->item_cnt);
|
|
|
|
}
|
|
|
|
|
2022-12-27 08:39:04 +00:00
|
|
|
uint32_t folder_item_cnt = (model->is_root) ? (model->item_cnt) : (model->item_cnt - 1);
|
|
|
|
if(folder_item_cnt == 0) {
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas,
|
|
|
|
canvas_width(canvas) / 2,
|
|
|
|
canvas_height(canvas) / 2,
|
|
|
|
AlignCenter,
|
|
|
|
AlignCenter,
|
|
|
|
"<Empty>");
|
|
|
|
}
|
|
|
|
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_free(filename);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void file_browser_view_draw_callback(Canvas* canvas, void* _model) {
|
|
|
|
FileBrowserModel* model = _model;
|
|
|
|
|
|
|
|
if(model->folder_loading) {
|
|
|
|
browser_draw_loading(canvas, model);
|
|
|
|
} else {
|
|
|
|
browser_draw_list(canvas, model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool file_browser_view_input_callback(InputEvent* event, void* context) {
|
|
|
|
FileBrowser* browser = context;
|
|
|
|
furi_assert(browser);
|
|
|
|
bool consumed = false;
|
|
|
|
bool is_loading = false;
|
|
|
|
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { is_loading = model->folder_loading; }, false);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
if(is_loading) {
|
|
|
|
return false;
|
|
|
|
} else if(event->key == InputKeyUp || event->key == InputKeyDown) {
|
|
|
|
if(event->type == InputTypeShort || event->type == InputTypeRepeat) {
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2023-06-27 09:22:35 +00:00
|
|
|
int32_t scroll_speed = 1;
|
|
|
|
if(model->button_held_for_ticks > 5) {
|
|
|
|
if(model->button_held_for_ticks % 2) {
|
|
|
|
scroll_speed = 0;
|
|
|
|
} else {
|
|
|
|
scroll_speed = model->button_held_for_ticks > 9 ? 5 : 3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 11:19:21 +00:00
|
|
|
if(event->key == InputKeyUp) {
|
2023-06-27 09:22:35 +00:00
|
|
|
if(model->item_idx < scroll_speed) {
|
2023-06-28 11:46:42 +00:00
|
|
|
model->button_held_for_ticks = 0;
|
|
|
|
model->item_idx = model->item_cnt - 1;
|
|
|
|
} else {
|
|
|
|
model->item_idx =
|
|
|
|
((model->item_idx - scroll_speed) + model->item_cnt) %
|
|
|
|
model->item_cnt;
|
2023-06-27 09:22:35 +00:00
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_is_list_load_required(model)) {
|
|
|
|
model->list_loading = true;
|
|
|
|
int32_t load_offset = CLAMP(
|
|
|
|
model->item_idx - ITEM_LIST_LEN_MAX / 4 * 3,
|
2022-11-28 18:59:24 +00:00
|
|
|
(int32_t)model->item_cnt,
|
2022-05-27 11:19:21 +00:00
|
|
|
0);
|
|
|
|
file_browser_worker_load(
|
|
|
|
browser->worker, load_offset, ITEM_LIST_LEN_MAX);
|
|
|
|
}
|
2022-12-20 11:11:52 +00:00
|
|
|
model->scroll_counter = 0;
|
2023-06-27 09:22:35 +00:00
|
|
|
|
|
|
|
model->button_held_for_ticks += 1;
|
2022-05-27 11:19:21 +00:00
|
|
|
} else if(event->key == InputKeyDown) {
|
2023-06-27 09:22:35 +00:00
|
|
|
int32_t count = model->item_cnt;
|
|
|
|
if(model->item_idx + scroll_speed >= count) {
|
2023-06-28 11:46:42 +00:00
|
|
|
model->button_held_for_ticks = 0;
|
|
|
|
model->item_idx = 0;
|
|
|
|
} else {
|
|
|
|
model->item_idx = (model->item_idx + scroll_speed) % model->item_cnt;
|
2023-06-27 09:22:35 +00:00
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_is_list_load_required(model)) {
|
|
|
|
model->list_loading = true;
|
|
|
|
int32_t load_offset = CLAMP(
|
|
|
|
model->item_idx - ITEM_LIST_LEN_MAX / 4 * 1,
|
2022-11-28 18:59:24 +00:00
|
|
|
(int32_t)model->item_cnt,
|
2022-05-27 11:19:21 +00:00
|
|
|
0);
|
|
|
|
file_browser_worker_load(
|
|
|
|
browser->worker, load_offset, ITEM_LIST_LEN_MAX);
|
|
|
|
}
|
2022-12-20 11:11:52 +00:00
|
|
|
model->scroll_counter = 0;
|
2023-06-27 09:22:35 +00:00
|
|
|
|
|
|
|
model->button_held_for_ticks += 1;
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
true);
|
2022-05-27 11:19:21 +00:00
|
|
|
browser_update_offset(browser);
|
|
|
|
consumed = true;
|
2023-06-27 09:22:35 +00:00
|
|
|
} else if(event->type == InputTypeRelease) {
|
|
|
|
with_view_model(
|
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{ model->button_held_for_ticks = 0; },
|
|
|
|
true);
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyOk) {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
BrowserItem_t* selected_item = NULL;
|
|
|
|
int32_t select_index = 0;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view,
|
|
|
|
FileBrowserModel * model,
|
|
|
|
{
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser_is_item_in_array(model, model->item_idx)) {
|
|
|
|
selected_item =
|
|
|
|
items_array_get(model->items, model->item_idx - model->array_offset);
|
|
|
|
select_index = model->item_idx;
|
|
|
|
if((!model->is_root) && (select_index > 0)) {
|
|
|
|
select_index -= 1;
|
|
|
|
}
|
|
|
|
}
|
2022-10-08 17:38:29 +00:00
|
|
|
},
|
|
|
|
false);
|
2022-05-27 11:19:21 +00:00
|
|
|
|
|
|
|
if(selected_item) {
|
|
|
|
if(selected_item->type == BrowserItemTypeBack) {
|
|
|
|
file_browser_worker_folder_exit(browser->worker);
|
|
|
|
} else if(selected_item->type == BrowserItemTypeFolder) {
|
|
|
|
file_browser_worker_folder_enter(
|
|
|
|
browser->worker, selected_item->path, select_index);
|
|
|
|
} else if(selected_item->type == BrowserItemTypeFile) {
|
2022-10-05 15:15:23 +00:00
|
|
|
furi_string_set(browser->result_path, selected_item->path);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(browser->callback) {
|
|
|
|
browser->callback(browser->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyLeft) {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
bool is_root = false;
|
|
|
|
with_view_model(
|
2022-10-08 17:38:29 +00:00
|
|
|
browser->view, FileBrowserModel * model, { is_root = model->is_root; }, false);
|
2022-05-27 11:19:21 +00:00
|
|
|
if(!is_root) {
|
|
|
|
file_browser_worker_folder_exit(browser->worker);
|
|
|
|
}
|
|
|
|
consumed = true;
|
|
|
|
}
|
2022-11-28 18:59:24 +00:00
|
|
|
} else if(event->key == InputKeyBack) {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
bool is_root = false;
|
|
|
|
with_view_model(
|
|
|
|
browser->view, FileBrowserModel * model, { is_root = model->is_root; }, false);
|
|
|
|
|
|
|
|
if(!is_root && !file_browser_worker_is_in_start_folder(browser->worker)) {
|
|
|
|
consumed = true;
|
2022-12-26 12:13:30 +00:00
|
|
|
file_browser_worker_folder_exit(browser->worker);
|
2022-11-28 18:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-27 11:19:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return consumed;
|
|
|
|
}
|