2021-09-21 10:56:33 +00:00
|
|
|
#include <furi.h>
|
|
|
|
#include "../archive_i.h"
|
|
|
|
#include "archive_browser_view.h"
|
|
|
|
#include "../helpers/archive_browser.h"
|
|
|
|
|
|
|
|
static const char* ArchiveTabNames[] = {
|
|
|
|
[ArchiveTabFavorites] = "Favorites",
|
|
|
|
[ArchiveTabIButton] = "iButton",
|
|
|
|
[ArchiveTabNFC] = "NFC",
|
|
|
|
[ArchiveTabSubGhz] = "Sub-GHz",
|
|
|
|
[ArchiveTabLFRFID] = "RFID LF",
|
2022-02-25 15:22:58 +00:00
|
|
|
[ArchiveTabInfrared] = "Infrared",
|
2022-02-10 13:01:49 +00:00
|
|
|
[ArchiveTabBadUsb] = "Bad USB",
|
|
|
|
[ArchiveTabU2f] = "U2F",
|
2021-09-21 10:56:33 +00:00
|
|
|
[ArchiveTabBrowser] = "Browser"};
|
|
|
|
|
|
|
|
static const Icon* ArchiveItemIcons[] = {
|
|
|
|
[ArchiveFileTypeIButton] = &I_ibutt_10px,
|
|
|
|
[ArchiveFileTypeNFC] = &I_Nfc_10px,
|
|
|
|
[ArchiveFileTypeSubGhz] = &I_sub1_10px,
|
|
|
|
[ArchiveFileTypeLFRFID] = &I_125_10px,
|
2022-02-25 15:22:58 +00:00
|
|
|
[ArchiveFileTypeInfrared] = &I_ir_10px,
|
2022-02-10 13:01:49 +00:00
|
|
|
[ArchiveFileTypeBadUsb] = &I_badusb_10px,
|
|
|
|
[ArchiveFileTypeU2f] = &I_u2f_10px,
|
2021-09-21 10:56:33 +00:00
|
|
|
[ArchiveFileTypeFolder] = &I_dir_10px,
|
|
|
|
[ArchiveFileTypeUnknown] = &I_unknown_10px,
|
2022-04-06 17:44:06 +00:00
|
|
|
[ArchiveFileTypeLoading] = &I_unknown_10px, // TODO loading icon
|
2021-09-21 10:56:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void archive_browser_set_callback(
|
|
|
|
ArchiveBrowserView* browser,
|
|
|
|
ArchiveBrowserViewCallback callback,
|
|
|
|
void* context) {
|
|
|
|
furi_assert(browser);
|
|
|
|
furi_assert(callback);
|
|
|
|
browser->callback = callback;
|
|
|
|
browser->context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void render_item_menu(Canvas* canvas, ArchiveBrowserViewModel* model) {
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_box(canvas, 71, 17, 57, 46);
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
elements_slightly_rounded_frame(canvas, 70, 16, 58, 48);
|
|
|
|
|
|
|
|
string_t menu[MENU_ITEMS];
|
|
|
|
|
|
|
|
string_init_set_str(menu[0], "Run in app");
|
|
|
|
string_init_set_str(menu[1], "Pin");
|
|
|
|
string_init_set_str(menu[2], "Rename");
|
|
|
|
string_init_set_str(menu[3], "Delete");
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
ArchiveFile_t* selected = files_array_get(model->files, model->item_idx - model->array_offset);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2022-02-10 13:01:49 +00:00
|
|
|
if(!archive_is_known_app(selected->type)) {
|
2021-09-21 10:56:33 +00:00
|
|
|
string_set_str(menu[0], "---");
|
|
|
|
string_set_str(menu[1], "---");
|
|
|
|
string_set_str(menu[2], "---");
|
2022-02-10 13:01:49 +00:00
|
|
|
} else {
|
|
|
|
if(model->tab_idx == ArchiveTabFavorites) {
|
|
|
|
string_set_str(menu[2], "Move");
|
2022-04-06 17:44:06 +00:00
|
|
|
string_set_str(menu[3], "---");
|
2022-02-10 13:01:49 +00:00
|
|
|
} else if(selected->is_app) {
|
|
|
|
string_set_str(menu[2], "---");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if((selected->fav) || (model->tab_idx == ArchiveTabFavorites)) {
|
2021-09-21 10:56:33 +00:00
|
|
|
string_set_str(menu[1], "Unpin");
|
|
|
|
}
|
|
|
|
|
|
|
|
for(size_t i = 0; i < MENU_ITEMS; i++) {
|
|
|
|
canvas_draw_str(canvas, 82, 27 + i * 11, string_get_cstr(menu[i]));
|
|
|
|
string_clear(menu[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
canvas_draw_icon(canvas, 74, 20 + model->menu_idx * 11, &I_ButtonRight_4x7);
|
|
|
|
}
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
static void archive_draw_frame(Canvas* canvas, uint16_t idx, bool scrollbar, bool moving) {
|
|
|
|
uint8_t x_offset = moving ? MOVE_OFFSET : 0;
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
canvas_set_color(canvas, ColorBlack);
|
2021-10-12 13:09:34 +00:00
|
|
|
canvas_draw_box(
|
|
|
|
canvas,
|
|
|
|
0 + x_offset,
|
|
|
|
15 + idx * FRAME_HEIGHT,
|
|
|
|
(scrollbar ? 122 : 127) - x_offset,
|
|
|
|
FRAME_HEIGHT);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
2021-10-12 13:09:34 +00:00
|
|
|
canvas_draw_dot(canvas, 0 + x_offset, 15 + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(canvas, 1 + x_offset, 15 + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(canvas, 0 + x_offset, (15 + idx * FRAME_HEIGHT) + 1);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
canvas_draw_dot(canvas, 0 + x_offset, (15 + idx * FRAME_HEIGHT) + 11);
|
2021-09-21 10:56:33 +00:00
|
|
|
canvas_draw_dot(canvas, scrollbar ? 121 : 126, 15 + idx * FRAME_HEIGHT);
|
|
|
|
canvas_draw_dot(canvas, scrollbar ? 121 : 126, (15 + idx * FRAME_HEIGHT) + 11);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_list(Canvas* canvas, ArchiveBrowserViewModel* model) {
|
|
|
|
furi_assert(model);
|
|
|
|
|
|
|
|
size_t array_size = files_array_size(model->files);
|
2022-04-06 17:44:06 +00:00
|
|
|
bool scrollbar = model->item_cnt > 4;
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
for(size_t i = 0; i < MIN(model->item_cnt, MENU_ITEMS); ++i) {
|
2021-09-21 10:56:33 +00:00
|
|
|
string_t str_buff;
|
|
|
|
char cstr_buff[MAX_NAME_LEN];
|
2022-04-06 17:44:06 +00:00
|
|
|
size_t idx = CLAMP(i + model->list_offset, model->item_cnt, 0);
|
|
|
|
uint8_t x_offset = (model->move_fav && model->item_idx == idx) ? MOVE_OFFSET : 0;
|
|
|
|
|
|
|
|
ArchiveFileTypeEnum file_type = ArchiveFileTypeLoading;
|
|
|
|
|
|
|
|
if(archive_is_item_in_array(model, idx)) {
|
|
|
|
ArchiveFile_t* file =
|
|
|
|
files_array_get(model->files, CLAMP(idx - model->array_offset, array_size - 1, 0));
|
|
|
|
strlcpy(cstr_buff, string_get_cstr(file->name), string_size(file->name) + 1);
|
|
|
|
archive_trim_file_path(cstr_buff, archive_is_known_app(file->type));
|
|
|
|
string_init_set_str(str_buff, cstr_buff);
|
|
|
|
file_type = file->type;
|
|
|
|
} else {
|
|
|
|
string_init_set_str(str_buff, "---");
|
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
elements_string_fit_width(
|
|
|
|
canvas, str_buff, (scrollbar ? MAX_LEN_PX - 6 : MAX_LEN_PX) - x_offset);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
if(model->item_idx == idx) {
|
2021-10-12 13:09:34 +00:00
|
|
|
archive_draw_frame(canvas, i, scrollbar, model->move_fav);
|
2021-09-21 10:56:33 +00:00
|
|
|
} else {
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
canvas_draw_icon(canvas, 2 + x_offset, 16 + i * FRAME_HEIGHT, ArchiveItemIcons[file_type]);
|
2021-10-12 13:09:34 +00:00
|
|
|
canvas_draw_str(canvas, 15 + x_offset, 24 + i * FRAME_HEIGHT, string_get_cstr(str_buff));
|
2022-04-06 17:44:06 +00:00
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
string_clear(str_buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(scrollbar) {
|
2022-04-06 17:44:06 +00:00
|
|
|
elements_scrollbar_pos(canvas, 126, 15, 49, model->item_idx, model->item_cnt);
|
2021-09-21 10:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(model->menu) {
|
|
|
|
render_item_menu(canvas, model);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void archive_render_status_bar(Canvas* canvas, ArchiveBrowserViewModel* model) {
|
|
|
|
furi_assert(model);
|
|
|
|
|
|
|
|
const char* tab_name = ArchiveTabNames[model->tab_idx];
|
|
|
|
|
|
|
|
canvas_draw_icon(canvas, 0, 0, &I_Background_128x11);
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_box(canvas, 0, 0, 50, 13);
|
|
|
|
canvas_draw_box(canvas, 107, 0, 20, 13);
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
canvas_draw_frame(canvas, 1, 0, 50, 12);
|
|
|
|
canvas_draw_line(canvas, 0, 1, 0, 11);
|
|
|
|
canvas_draw_line(canvas, 1, 12, 49, 12);
|
|
|
|
canvas_draw_str_aligned(canvas, 26, 9, AlignCenter, AlignBottom, tab_name);
|
|
|
|
|
|
|
|
canvas_draw_frame(canvas, 108, 0, 20, 12);
|
|
|
|
canvas_draw_line(canvas, 107, 1, 107, 11);
|
|
|
|
canvas_draw_line(canvas, 108, 12, 126, 12);
|
|
|
|
|
2021-10-12 13:09:34 +00:00
|
|
|
if(model->move_fav) {
|
|
|
|
canvas_draw_icon(canvas, 111, 4, &I_ButtonUp_7x4);
|
|
|
|
canvas_draw_icon(canvas, 118, 4, &I_ButtonDown_7x4);
|
|
|
|
} else {
|
|
|
|
canvas_draw_icon(canvas, 112, 2, &I_ButtonLeft_4x7);
|
|
|
|
canvas_draw_icon(canvas, 120, 2, &I_ButtonRight_4x7);
|
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorWhite);
|
|
|
|
canvas_draw_dot(canvas, 50, 0);
|
|
|
|
canvas_draw_dot(canvas, 127, 0);
|
|
|
|
|
|
|
|
canvas_set_color(canvas, ColorBlack);
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
void archive_view_render(Canvas* canvas, void* mdl) {
|
|
|
|
ArchiveBrowserViewModel* model = mdl;
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
archive_render_status_bar(canvas, mdl);
|
2021-09-21 10:56:33 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
if(model->item_cnt > 0) {
|
|
|
|
draw_list(canvas, model);
|
2021-09-21 10:56:33 +00:00
|
|
|
} else {
|
|
|
|
canvas_draw_str_aligned(
|
|
|
|
canvas, GUI_DISPLAY_WIDTH / 2, 40, AlignCenter, AlignCenter, "Empty");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
View* archive_browser_get_view(ArchiveBrowserView* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
return browser->view;
|
|
|
|
}
|
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
static bool is_file_list_load_required(ArchiveBrowserViewModel* model) {
|
|
|
|
size_t array_size = files_array_size(model->files);
|
|
|
|
|
|
|
|
if((model->list_loading) || (array_size >= model->item_cnt)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((model->array_offset > 0) &&
|
|
|
|
(model->item_idx < (model->array_offset + FILE_LIST_BUF_LEN / 4))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(((model->array_offset + array_size) < model->item_cnt) &&
|
|
|
|
(model->item_idx > (model->array_offset + array_size - FILE_LIST_BUF_LEN / 4))) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-09-21 10:56:33 +00:00
|
|
|
bool archive_view_input(InputEvent* event, void* context) {
|
|
|
|
furi_assert(event);
|
|
|
|
furi_assert(context);
|
|
|
|
|
|
|
|
ArchiveBrowserView* browser = context;
|
|
|
|
|
|
|
|
bool in_menu;
|
2021-10-12 13:09:34 +00:00
|
|
|
bool move_fav_mode;
|
2021-09-21 10:56:33 +00:00
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
|
|
|
in_menu = model->menu;
|
2021-10-12 13:09:34 +00:00
|
|
|
move_fav_mode = model->move_fav;
|
2021-09-21 10:56:33 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
if(in_menu) {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyUp || event->key == InputKeyDown) {
|
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->menu_idx = ((model->menu_idx - 1) + MENU_ITEMS) % MENU_ITEMS;
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->menu_idx = (model->menu_idx + 1) % MENU_ITEMS;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->key == InputKeyOk) {
|
|
|
|
uint8_t idx;
|
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
|
|
|
idx = model->menu_idx;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
browser->callback(file_menu_actions[idx], browser->context);
|
|
|
|
} else if(event->key == InputKeyBack) {
|
|
|
|
browser->callback(ArchiveBrowserEventFileMenuClose, browser->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(event->key == InputKeyLeft || event->key == InputKeyRight) {
|
2021-10-12 13:09:34 +00:00
|
|
|
if(move_fav_mode) return false;
|
2021-09-21 10:56:33 +00:00
|
|
|
archive_switch_tab(browser, event->key);
|
|
|
|
} else if(event->key == InputKeyBack) {
|
2021-10-12 13:09:34 +00:00
|
|
|
if(move_fav_mode) {
|
|
|
|
browser->callback(ArchiveBrowserEventExitFavMove, browser->context);
|
|
|
|
} else {
|
|
|
|
browser->callback(ArchiveBrowserEventExit, browser->context);
|
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-10-12 13:09:34 +00:00
|
|
|
|
2022-04-06 17:44:06 +00:00
|
|
|
if((event->key == InputKeyUp || event->key == InputKeyDown) &&
|
|
|
|
(event->type == InputTypeShort || event->type == InputTypeRepeat)) {
|
2021-09-21 10:56:33 +00:00
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
2022-04-06 17:44:06 +00:00
|
|
|
if(event->key == InputKeyUp) {
|
|
|
|
model->item_idx =
|
|
|
|
((model->item_idx - 1) + model->item_cnt) % model->item_cnt;
|
|
|
|
if(is_file_list_load_required(model)) {
|
|
|
|
model->list_loading = true;
|
|
|
|
browser->callback(ArchiveBrowserEventLoadPrevItems, browser->context);
|
|
|
|
}
|
|
|
|
if(move_fav_mode) {
|
|
|
|
browser->callback(ArchiveBrowserEventFavMoveUp, browser->context);
|
|
|
|
}
|
|
|
|
} else if(event->key == InputKeyDown) {
|
|
|
|
model->item_idx = (model->item_idx + 1) % model->item_cnt;
|
|
|
|
if(is_file_list_load_required(model)) {
|
|
|
|
model->list_loading = true;
|
|
|
|
browser->callback(ArchiveBrowserEventLoadNextItems, browser->context);
|
|
|
|
}
|
|
|
|
if(move_fav_mode) {
|
|
|
|
browser->callback(ArchiveBrowserEventFavMoveDown, browser->context);
|
2021-09-21 10:56:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
archive_update_offset(browser);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(event->key == InputKeyOk) {
|
|
|
|
ArchiveFile_t* selected = archive_get_current_file(browser);
|
|
|
|
|
|
|
|
if(selected) {
|
|
|
|
bool favorites = archive_get_tab(browser) == ArchiveTabFavorites;
|
|
|
|
bool folder = selected->type == ArchiveFileTypeFolder;
|
|
|
|
|
|
|
|
if(event->type == InputTypeShort) {
|
|
|
|
if(favorites) {
|
2021-10-12 13:09:34 +00:00
|
|
|
if(move_fav_mode) {
|
|
|
|
browser->callback(ArchiveBrowserEventSaveFavMove, browser->context);
|
|
|
|
} else {
|
|
|
|
browser->callback(ArchiveBrowserEventFileMenuRun, browser->context);
|
|
|
|
}
|
2021-09-21 10:56:33 +00:00
|
|
|
} else if(folder) {
|
|
|
|
browser->callback(ArchiveBrowserEventEnterDir, browser->context);
|
|
|
|
} else {
|
|
|
|
browser->callback(ArchiveBrowserEventFileMenuOpen, browser->context);
|
|
|
|
}
|
|
|
|
} else if(event->type == InputTypeLong) {
|
2021-10-12 13:09:34 +00:00
|
|
|
if(move_fav_mode) {
|
|
|
|
browser->callback(ArchiveBrowserEventSaveFavMove, browser->context);
|
|
|
|
} else if(folder || favorites) {
|
2021-09-21 10:56:33 +00:00
|
|
|
browser->callback(ArchiveBrowserEventFileMenuOpen, browser->context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ArchiveBrowserView* browser_alloc() {
|
2022-02-18 19:53:46 +00:00
|
|
|
ArchiveBrowserView* browser = malloc(sizeof(ArchiveBrowserView));
|
2021-09-21 10:56:33 +00:00
|
|
|
browser->view = view_alloc();
|
|
|
|
view_allocate_model(browser->view, ViewModelTypeLocking, sizeof(ArchiveBrowserViewModel));
|
|
|
|
view_set_context(browser->view, browser);
|
|
|
|
view_set_draw_callback(browser->view, (ViewDrawCallback)archive_view_render);
|
|
|
|
view_set_input_callback(browser->view, archive_view_input);
|
|
|
|
|
|
|
|
string_init(browser->path);
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
|
|
|
files_array_init(model->files);
|
2022-04-06 17:44:06 +00:00
|
|
|
idx_last_array_init(model->idx_last);
|
2021-09-21 10:56:33 +00:00
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return browser;
|
|
|
|
}
|
|
|
|
|
|
|
|
void browser_free(ArchiveBrowserView* browser) {
|
|
|
|
furi_assert(browser);
|
|
|
|
|
|
|
|
with_view_model(
|
|
|
|
browser->view, (ArchiveBrowserViewModel * model) {
|
|
|
|
files_array_clear(model->files);
|
2022-04-06 17:44:06 +00:00
|
|
|
idx_last_array_clear(model->idx_last);
|
2021-09-21 10:56:33 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
string_clear(browser->path);
|
|
|
|
|
|
|
|
view_free(browser->view);
|
|
|
|
free(browser);
|
|
|
|
}
|