mirror of
https://github.com/UberGuidoZ/Flipper.git
synced 2026-09-18 14:51:30 +00:00
Removing FAP files (use FlipC or Official store!)
This commit is contained in:
+481
@@ -0,0 +1,481 @@
|
||||
#include "file_select.h"
|
||||
#include <gui/elements.h>
|
||||
#include <storage/storage.h>
|
||||
|
||||
#define FILENAME_COUNT 4
|
||||
|
||||
struct FileSelect {
|
||||
// public
|
||||
View* view;
|
||||
Storage* fs_api;
|
||||
const char* path;
|
||||
const char* extension;
|
||||
|
||||
bool init_completed;
|
||||
|
||||
FileSelectCallback callback;
|
||||
void* context;
|
||||
|
||||
char* buffer;
|
||||
uint8_t buffer_size;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
FuriString* filename[FILENAME_COUNT];
|
||||
uint8_t position;
|
||||
|
||||
uint16_t first_file_index;
|
||||
uint16_t file_count;
|
||||
|
||||
} FileSelectModel;
|
||||
|
||||
bool file_select_fill_strings(FileSelect* file_select);
|
||||
bool file_select_fill_count(FileSelect* file_select);
|
||||
static bool file_select_init_inner(FileSelect* file_select);
|
||||
|
||||
static void file_select_draw_callback(Canvas* canvas, void* _model) {
|
||||
FileSelectModel* model = _model;
|
||||
|
||||
FuriString* string_buff;
|
||||
const uint8_t item_height = 16;
|
||||
const uint8_t item_width = 123;
|
||||
const uint8_t text_max_width = 115;
|
||||
|
||||
canvas_clear(canvas);
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
|
||||
if(model->file_count) {
|
||||
for(uint8_t i = 0; i < MIN(FILENAME_COUNT, model->file_count); i++) {
|
||||
if(i == model->position) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
canvas_draw_box(canvas, 0, (i * item_height) + 1, item_width, item_height - 2);
|
||||
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
canvas_draw_dot(canvas, 0, (i * item_height) + 1);
|
||||
canvas_draw_dot(canvas, 0, (i * item_height) + item_height - 2);
|
||||
canvas_draw_dot(canvas, item_width - 1, (i * item_height) + 1);
|
||||
canvas_draw_dot(canvas, item_width - 1, (i * item_height) + item_height - 2);
|
||||
} else {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
}
|
||||
|
||||
string_buff = furi_string_alloc_set(model->filename[i]);
|
||||
elements_string_fit_width(canvas, string_buff, text_max_width);
|
||||
canvas_draw_str(
|
||||
canvas, 6, (i * item_height) + item_height - 4, furi_string_get_cstr(string_buff));
|
||||
|
||||
furi_string_free(string_buff);
|
||||
}
|
||||
} else {
|
||||
canvas_draw_str(canvas, 6, item_height, "Empty folder");
|
||||
}
|
||||
elements_scrollbar(canvas, model->first_file_index + model->position, model->file_count);
|
||||
}
|
||||
|
||||
static bool file_select_input_callback(InputEvent* event, void* context) {
|
||||
FileSelect* file_select = (FileSelect*)context;
|
||||
bool consumed = false;
|
||||
|
||||
if((event->type == InputTypeShort) | (event->type == InputTypeRepeat)) {
|
||||
if(!file_select->init_completed) {
|
||||
if(!file_select_init_inner(file_select)) {
|
||||
file_select->callback(false, file_select->context);
|
||||
}
|
||||
} else if(event->key == InputKeyUp) {
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
if(model->position == 0) {
|
||||
if(model->first_file_index == 0) {
|
||||
// wrap
|
||||
int16_t max_first_file_index = model->file_count - FILENAME_COUNT;
|
||||
model->position = MIN(FILENAME_COUNT - 1, model->file_count - 1);
|
||||
model->first_file_index =
|
||||
max_first_file_index < 0 ? 0 : max_first_file_index;
|
||||
} else {
|
||||
model->first_file_index--;
|
||||
}
|
||||
} else if(model->position == 1) {
|
||||
if(model->first_file_index == 0) {
|
||||
model->position--;
|
||||
} else {
|
||||
model->first_file_index--;
|
||||
}
|
||||
} else {
|
||||
model->position--;
|
||||
}
|
||||
},
|
||||
true);
|
||||
consumed = true;
|
||||
if(!file_select_fill_strings(file_select)) {
|
||||
file_select->callback(false, file_select->context);
|
||||
}
|
||||
} else if(event->key == InputKeyDown) {
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
uint16_t max_first_file_index = model->file_count > FILENAME_COUNT ?
|
||||
model->file_count - FILENAME_COUNT :
|
||||
0;
|
||||
|
||||
if(model->position >= MIN(FILENAME_COUNT - 1, model->file_count - 1)) {
|
||||
if(model->first_file_index >= max_first_file_index) {
|
||||
// wrap
|
||||
model->position = 0;
|
||||
model->first_file_index = 0;
|
||||
} else {
|
||||
model->first_file_index++;
|
||||
}
|
||||
} else if(model->position >= (FILENAME_COUNT - 2)) {
|
||||
if(model->first_file_index >= max_first_file_index) {
|
||||
model->position++;
|
||||
} else {
|
||||
model->first_file_index++;
|
||||
}
|
||||
} else {
|
||||
model->position++;
|
||||
}
|
||||
},
|
||||
true);
|
||||
consumed = true;
|
||||
if(!file_select_fill_strings(file_select)) {
|
||||
file_select->callback(false, file_select->context);
|
||||
}
|
||||
} else if(event->key == InputKeyOk) {
|
||||
if(file_select->callback != NULL) {
|
||||
size_t files = 0;
|
||||
if(file_select->buffer) {
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
files = model->file_count;
|
||||
strlcpy(
|
||||
file_select->buffer,
|
||||
furi_string_get_cstr(model->filename[model->position]),
|
||||
file_select->buffer_size);
|
||||
},
|
||||
false);
|
||||
};
|
||||
if(files > 0) {
|
||||
file_select->callback(true, file_select->context);
|
||||
}
|
||||
}
|
||||
consumed = true;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
static bool file_select_init_inner(FileSelect* file_select) {
|
||||
bool result = false;
|
||||
if(file_select->path && file_select->extension && file_select->fs_api) {
|
||||
if(file_select_fill_count(file_select)) {
|
||||
if(file_select_fill_strings(file_select)) {
|
||||
file_select->init_completed = true;
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
FileSelect* file_select_alloc() {
|
||||
FileSelect* file_select = malloc(sizeof(FileSelect));
|
||||
file_select->view = view_alloc();
|
||||
file_select->fs_api = furi_record_open("storage");
|
||||
|
||||
view_set_context(file_select->view, file_select);
|
||||
view_allocate_model(file_select->view, ViewModelTypeLockFree, sizeof(FileSelectModel));
|
||||
view_set_draw_callback(file_select->view, file_select_draw_callback);
|
||||
view_set_input_callback(file_select->view, file_select_input_callback);
|
||||
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
|
||||
model->filename[i] = furi_string_alloc();
|
||||
}
|
||||
|
||||
model->first_file_index = 0;
|
||||
model->file_count = 0;
|
||||
},
|
||||
false);
|
||||
|
||||
return file_select;
|
||||
}
|
||||
|
||||
void file_select_free(FileSelect* file_select) {
|
||||
furi_assert(file_select);
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
|
||||
furi_string_free(model->filename[i]);
|
||||
}
|
||||
},
|
||||
false);
|
||||
view_free(file_select->view);
|
||||
free(file_select);
|
||||
furi_record_close("storage");
|
||||
}
|
||||
|
||||
View* file_select_get_view(FileSelect* file_select) {
|
||||
furi_assert(file_select);
|
||||
return file_select->view;
|
||||
}
|
||||
|
||||
void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context) {
|
||||
file_select->context = context;
|
||||
file_select->callback = callback;
|
||||
}
|
||||
|
||||
void file_select_set_filter(FileSelect* file_select, const char* path, const char* extension) {
|
||||
furi_assert(file_select);
|
||||
file_select->path = path;
|
||||
file_select->extension = extension;
|
||||
}
|
||||
|
||||
void file_select_set_result_buffer(FileSelect* file_select, char* buffer, uint8_t buffer_size) {
|
||||
file_select->buffer = buffer;
|
||||
file_select->buffer_size = buffer_size;
|
||||
|
||||
if(file_select->buffer) {
|
||||
strlcpy(file_select->buffer, "", file_select->buffer_size);
|
||||
}
|
||||
}
|
||||
|
||||
bool file_select_init(FileSelect* file_select) {
|
||||
if(!file_select_init_inner(file_select)) {
|
||||
file_select->callback(false, file_select->context);
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
static bool filter_file(FileSelect* file_select, FileInfo* file_info, char* name) {
|
||||
bool result = false;
|
||||
|
||||
if((file_info->flags & FSF_DIRECTORY)) {
|
||||
if(strcmp(file_select->extension, "*") == 0) {
|
||||
result = true;
|
||||
} else if(strstr(name, file_select->extension) != NULL) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool file_select_fill_strings(FileSelect* file_select) {
|
||||
furi_assert(file_select);
|
||||
furi_assert(file_select->fs_api);
|
||||
furi_assert(file_select->path);
|
||||
furi_assert(file_select->extension);
|
||||
|
||||
FileInfo file_info;
|
||||
File* directory = storage_file_alloc(file_select->fs_api);
|
||||
|
||||
uint8_t string_counter = 0;
|
||||
uint16_t file_counter = 0;
|
||||
const uint8_t name_length = 100;
|
||||
char* name = malloc(name_length);
|
||||
uint16_t first_file_index = 0;
|
||||
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{ first_file_index = model->first_file_index; },
|
||||
false);
|
||||
|
||||
if(!storage_dir_open(directory, file_select->path)) {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
if(!storage_dir_read(directory, &file_info, name, name_length)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(storage_file_get_error(directory) == FSE_OK) {
|
||||
if(filter_file(file_select, &file_info, name)) {
|
||||
if(file_counter >= first_file_index) {
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
furi_string_set(model->filename[string_counter], name);
|
||||
|
||||
if(strcmp(file_select->extension, "*") != 0) {
|
||||
furi_string_replace_all_str(
|
||||
model->filename[string_counter], file_select->extension, "");
|
||||
}
|
||||
},
|
||||
true);
|
||||
string_counter++;
|
||||
|
||||
if(string_counter >= FILENAME_COUNT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
file_counter++;
|
||||
}
|
||||
} else {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool file_select_fill_count(FileSelect* file_select) {
|
||||
furi_assert(file_select);
|
||||
furi_assert(file_select->fs_api);
|
||||
furi_assert(file_select->path);
|
||||
furi_assert(file_select->extension);
|
||||
|
||||
FileInfo file_info;
|
||||
File* directory = storage_file_alloc(file_select->fs_api);
|
||||
|
||||
uint16_t file_counter = 0;
|
||||
const uint8_t name_length = 100;
|
||||
char* name = malloc(name_length);
|
||||
|
||||
if(!storage_dir_open(directory, file_select->path)) {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
if(!storage_dir_read(directory, &file_info, name, name_length)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(storage_file_get_error(directory) == FSE_OK) {
|
||||
if(filter_file(file_select, &file_info, name)) {
|
||||
file_counter++;
|
||||
}
|
||||
} else {
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
with_view_model(
|
||||
file_select->view, FileSelectModel * model, { model->file_count = file_counter; }, false);
|
||||
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return true;
|
||||
}
|
||||
|
||||
void file_select_set_selected_file_internal(FileSelect* file_select, const char* filename) {
|
||||
furi_assert(file_select);
|
||||
furi_assert(filename);
|
||||
furi_assert(file_select->fs_api);
|
||||
furi_assert(file_select->path);
|
||||
furi_assert(file_select->extension);
|
||||
|
||||
if(strlen(filename) == 0) return;
|
||||
|
||||
FileInfo file_info;
|
||||
File* directory = storage_file_alloc(file_select->fs_api);
|
||||
|
||||
const uint8_t name_length = 100;
|
||||
char* name = malloc(name_length);
|
||||
uint16_t file_position = 0;
|
||||
bool file_found = false;
|
||||
|
||||
FuriString* filename_str = furi_string_alloc_set_str(filename);
|
||||
if(strcmp(file_select->extension, "*") != 0) {
|
||||
furi_string_cat_str(filename_str, file_select->extension);
|
||||
}
|
||||
|
||||
if(!storage_dir_open(directory, file_select->path)) {
|
||||
furi_string_free(filename_str);
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return;
|
||||
}
|
||||
|
||||
while(1) {
|
||||
if(!storage_dir_read(directory, &file_info, name, name_length)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if(storage_file_get_error(directory) == FSE_OK) {
|
||||
if(filter_file(file_select, &file_info, name)) {
|
||||
if(strcmp(furi_string_get_cstr(filename_str), name) == 0) {
|
||||
file_found = true;
|
||||
break;
|
||||
}
|
||||
|
||||
file_position++;
|
||||
}
|
||||
} else {
|
||||
furi_string_free(filename_str);
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(file_found) {
|
||||
with_view_model(
|
||||
file_select->view,
|
||||
FileSelectModel * model,
|
||||
{
|
||||
uint16_t max_first_file_index =
|
||||
model->file_count > FILENAME_COUNT ? model->file_count - FILENAME_COUNT : 0;
|
||||
|
||||
model->first_file_index = file_position;
|
||||
|
||||
if(model->first_file_index > 0) {
|
||||
model->first_file_index -= 1;
|
||||
}
|
||||
|
||||
if(model->first_file_index >= max_first_file_index) {
|
||||
model->first_file_index = max_first_file_index;
|
||||
}
|
||||
|
||||
model->position = file_position - model->first_file_index;
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
furi_string_free(filename_str);
|
||||
storage_dir_close(directory);
|
||||
storage_file_free(directory);
|
||||
free(name);
|
||||
}
|
||||
|
||||
void file_select_set_selected_file(FileSelect* file_select, const char* filename) {
|
||||
file_select_set_selected_file_internal(file_select, filename);
|
||||
|
||||
if(!file_select_fill_strings(file_select)) {
|
||||
file_select->callback(false, file_select->context);
|
||||
}
|
||||
}
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @file file_select.h
|
||||
* GUI: FileSelect view module API
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct FileSelect FileSelect;
|
||||
|
||||
typedef void (*FileSelectCallback)(bool result, void* context);
|
||||
|
||||
FileSelect* file_select_alloc();
|
||||
|
||||
void file_select_free(FileSelect* file_select);
|
||||
View* file_select_get_view(FileSelect* file_select);
|
||||
|
||||
void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context);
|
||||
void file_select_set_filter(FileSelect* file_select, const char* path, const char* extension);
|
||||
void file_select_set_result_buffer(FileSelect* file_select, char* buffer, uint8_t buffer_size);
|
||||
bool file_select_init(FileSelect* file_select);
|
||||
void file_select_set_selected_file(FileSelect* file_select, const char* filename);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
+419
@@ -0,0 +1,419 @@
|
||||
#include "variable_item_list_ex.h"
|
||||
#include "gui/canvas.h"
|
||||
#include <m-array.h>
|
||||
#include <furi.h>
|
||||
#include <gui/elements.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct VariableItemEx {
|
||||
const char* label;
|
||||
uint8_t current_value_index;
|
||||
FuriString* current_value_text;
|
||||
uint8_t values_count;
|
||||
VariableItemExChangeCallback change_callback;
|
||||
void* context;
|
||||
int32_t callback_index;
|
||||
};
|
||||
|
||||
ARRAY_DEF(VariableItemExArray, VariableItemEx, M_POD_OPLIST);
|
||||
|
||||
struct VariableItemListEx {
|
||||
View* view;
|
||||
VariableItemListExEnterCallback callback;
|
||||
void* context;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
VariableItemExArray_t items;
|
||||
uint8_t position;
|
||||
uint8_t window_position;
|
||||
} VariableItemListExModel;
|
||||
|
||||
static void variable_item_list_ex_process_up(VariableItemListEx* variable_item_list);
|
||||
static void variable_item_list_ex_process_down(VariableItemListEx* variable_item_list);
|
||||
static void variable_item_list_ex_process_left(VariableItemListEx* variable_item_list);
|
||||
static void variable_item_list_ex_process_right(VariableItemListEx* variable_item_list);
|
||||
static void variable_item_list_ex_process_ok(VariableItemListEx* variable_item_list);
|
||||
|
||||
static void variable_item_list_ex_draw_callback(Canvas* canvas, void* _model) {
|
||||
VariableItemListExModel* model = _model;
|
||||
|
||||
const uint8_t item_height = 16;
|
||||
const uint8_t item_width = 123;
|
||||
|
||||
canvas_clear(canvas);
|
||||
|
||||
uint8_t position = 0;
|
||||
VariableItemExArray_it_t it;
|
||||
|
||||
canvas_set_font(canvas, FontSecondary);
|
||||
for(VariableItemExArray_it(it, model->items); !VariableItemExArray_end_p(it);
|
||||
VariableItemExArray_next(it)) {
|
||||
uint8_t item_position = position - model->window_position;
|
||||
uint8_t items_on_screen = 4;
|
||||
uint8_t y_offset = 0;
|
||||
|
||||
if(item_position < items_on_screen) {
|
||||
const VariableItemEx* item = VariableItemExArray_cref(it);
|
||||
uint8_t item_y = y_offset + (item_position * item_height);
|
||||
uint8_t item_text_y = item_y + item_height - 4;
|
||||
|
||||
if(position == model->position) {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
elements_slightly_rounded_box(canvas, 0, item_y + 1, item_width, item_height - 2);
|
||||
canvas_set_color(canvas, ColorWhite);
|
||||
} else {
|
||||
canvas_set_color(canvas, ColorBlack);
|
||||
}
|
||||
|
||||
canvas_draw_str(canvas, 6, item_text_y, item->label);
|
||||
|
||||
if(item->current_value_index > 0) {
|
||||
canvas_draw_str(canvas, 73, item_text_y, "<");
|
||||
}
|
||||
|
||||
canvas_draw_str_aligned(
|
||||
canvas,
|
||||
(115 + 73) / 2 + 1,
|
||||
item_text_y,
|
||||
AlignCenter,
|
||||
AlignBottom,
|
||||
furi_string_get_cstr(item->current_value_text));
|
||||
|
||||
if(item->current_value_index < (item->values_count - 1)) {
|
||||
canvas_draw_str(canvas, 115, item_text_y, ">");
|
||||
}
|
||||
}
|
||||
|
||||
position++;
|
||||
}
|
||||
|
||||
elements_scrollbar(canvas, model->position, VariableItemExArray_size(model->items));
|
||||
}
|
||||
|
||||
void variable_item_list_ex_set_selected_item(VariableItemListEx* variable_item_list, uint8_t index) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
uint8_t position = index;
|
||||
if(position >= VariableItemExArray_size(model->items)) {
|
||||
position = 0;
|
||||
}
|
||||
|
||||
model->position = position;
|
||||
model->window_position = position;
|
||||
|
||||
if(model->window_position > 0) {
|
||||
model->window_position -= 1;
|
||||
}
|
||||
|
||||
if(VariableItemExArray_size(model->items) <= 4) {
|
||||
model->window_position = 0;
|
||||
} else {
|
||||
if(model->window_position >= (VariableItemExArray_size(model->items) - 4)) {
|
||||
model->window_position = (VariableItemExArray_size(model->items) - 4);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
uint8_t variable_item_list_ex_get_selected_item_index(VariableItemListEx* variable_item_list) {
|
||||
VariableItemListExModel* model = view_get_model(variable_item_list->view);
|
||||
uint8_t idx = model->position;
|
||||
view_commit_model(variable_item_list->view, false);
|
||||
return idx;
|
||||
}
|
||||
|
||||
static bool variable_item_list_ex_input_callback(InputEvent* event, void* context) {
|
||||
VariableItemListEx* variable_item_list = context;
|
||||
furi_assert(variable_item_list);
|
||||
bool consumed = false;
|
||||
|
||||
if(event->type == InputTypeShort) {
|
||||
switch(event->key) {
|
||||
case InputKeyUp:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_up(variable_item_list);
|
||||
break;
|
||||
case InputKeyDown:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_down(variable_item_list);
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_left(variable_item_list);
|
||||
break;
|
||||
case InputKeyRight:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_right(variable_item_list);
|
||||
break;
|
||||
case InputKeyOk:
|
||||
variable_item_list_ex_process_ok(variable_item_list);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if(event->type == InputTypeRepeat) {
|
||||
switch(event->key) {
|
||||
case InputKeyUp:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_up(variable_item_list);
|
||||
break;
|
||||
case InputKeyDown:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_down(variable_item_list);
|
||||
break;
|
||||
case InputKeyLeft:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_left(variable_item_list);
|
||||
break;
|
||||
case InputKeyRight:
|
||||
consumed = true;
|
||||
variable_item_list_ex_process_right(variable_item_list);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return consumed;
|
||||
}
|
||||
|
||||
void variable_item_list_ex_process_up(VariableItemListEx* variable_item_list) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
uint8_t items_on_screen = 4;
|
||||
if(model->position > 0) {
|
||||
model->position--;
|
||||
if(((model->position - model->window_position) < 1) &&
|
||||
model->window_position > 0) {
|
||||
model->window_position--;
|
||||
}
|
||||
} else {
|
||||
model->position = VariableItemExArray_size(model->items) - 1;
|
||||
if(model->position > (items_on_screen - 1)) {
|
||||
model->window_position = model->position - (items_on_screen - 1);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
void variable_item_list_ex_process_down(VariableItemListEx* variable_item_list) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
uint8_t items_on_screen = 4;
|
||||
if(model->position < (VariableItemExArray_size(model->items) - 1)) {
|
||||
model->position++;
|
||||
if((model->position - model->window_position) > (items_on_screen - 2) &&
|
||||
model->window_position <
|
||||
(VariableItemExArray_size(model->items) - items_on_screen)) {
|
||||
model->window_position++;
|
||||
}
|
||||
} else {
|
||||
model->position = 0;
|
||||
model->window_position = 0;
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
VariableItemEx* variable_item_list_ex_get_selected_item(VariableItemListExModel* model) {
|
||||
VariableItemEx* item = NULL;
|
||||
|
||||
VariableItemExArray_it_t it;
|
||||
uint8_t position = 0;
|
||||
for(VariableItemExArray_it(it, model->items); !VariableItemExArray_end_p(it);
|
||||
VariableItemExArray_next(it)) {
|
||||
if(position == model->position) {
|
||||
break;
|
||||
}
|
||||
position++;
|
||||
}
|
||||
|
||||
item = VariableItemExArray_ref(it);
|
||||
|
||||
furi_assert(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
void variable_item_list_ex_process_left(VariableItemListEx* variable_item_list) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
VariableItemEx* item = variable_item_list_ex_get_selected_item(model);
|
||||
if(item->current_value_index > 0) {
|
||||
item->current_value_index--;
|
||||
if(item->change_callback) {
|
||||
item->change_callback(item);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
void variable_item_list_ex_process_right(VariableItemListEx* variable_item_list) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
VariableItemEx* item = variable_item_list_ex_get_selected_item(model);
|
||||
if(item->current_value_index < (item->values_count - 1)) {
|
||||
item->current_value_index++;
|
||||
if(item->change_callback) {
|
||||
item->change_callback(item);
|
||||
}
|
||||
}
|
||||
},
|
||||
true);
|
||||
}
|
||||
|
||||
void variable_item_list_ex_process_ok(VariableItemListEx* variable_item_list) {
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
if(variable_item_list->callback) {
|
||||
const VariableItemEx* variable_item =
|
||||
VariableItemExArray_cget(model->items, model->position);
|
||||
variable_item_list->callback(
|
||||
variable_item_list->context, variable_item->callback_index);
|
||||
}
|
||||
},
|
||||
false);
|
||||
}
|
||||
|
||||
VariableItemListEx* variable_item_list_ex_alloc() {
|
||||
VariableItemListEx* variable_item_list = malloc(sizeof(VariableItemListEx));
|
||||
variable_item_list->view = view_alloc();
|
||||
view_set_context(variable_item_list->view, variable_item_list);
|
||||
view_allocate_model(
|
||||
variable_item_list->view, ViewModelTypeLocking, sizeof(VariableItemListExModel));
|
||||
view_set_draw_callback(variable_item_list->view, variable_item_list_ex_draw_callback);
|
||||
view_set_input_callback(variable_item_list->view, variable_item_list_ex_input_callback);
|
||||
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
VariableItemExArray_init(model->items);
|
||||
model->position = 0;
|
||||
model->window_position = 0;
|
||||
},
|
||||
true);
|
||||
|
||||
return variable_item_list;
|
||||
}
|
||||
|
||||
void variable_item_list_ex_free(VariableItemListEx* variable_item_list) {
|
||||
furi_assert(variable_item_list);
|
||||
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
VariableItemExArray_it_t it;
|
||||
for(VariableItemExArray_it(it, model->items); !VariableItemExArray_end_p(it);
|
||||
VariableItemExArray_next(it)) {
|
||||
furi_string_free(VariableItemExArray_ref(it)->current_value_text);
|
||||
}
|
||||
VariableItemExArray_clear(model->items);
|
||||
},
|
||||
false);
|
||||
view_free(variable_item_list->view);
|
||||
free(variable_item_list);
|
||||
}
|
||||
|
||||
void variable_item_list_ex_reset(VariableItemListEx* variable_item_list) {
|
||||
furi_assert(variable_item_list);
|
||||
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
VariableItemExArray_it_t it;
|
||||
for(VariableItemExArray_it(it, model->items); !VariableItemExArray_end_p(it);
|
||||
VariableItemExArray_next(it)) {
|
||||
furi_string_free(VariableItemExArray_ref(it)->current_value_text);
|
||||
}
|
||||
VariableItemExArray_reset(model->items);
|
||||
},
|
||||
false);
|
||||
}
|
||||
|
||||
View* variable_item_list_ex_get_view(VariableItemListEx* variable_item_list) {
|
||||
furi_assert(variable_item_list);
|
||||
return variable_item_list->view;
|
||||
}
|
||||
|
||||
VariableItemEx* variable_item_list_ex_add(
|
||||
VariableItemListEx* variable_item_list,
|
||||
const char* label,
|
||||
uint8_t values_count,
|
||||
VariableItemExChangeCallback change_callback,
|
||||
void* context,
|
||||
int32_t callback_index) {
|
||||
VariableItemEx* item = NULL;
|
||||
furi_assert(label);
|
||||
furi_assert(variable_item_list);
|
||||
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
item = VariableItemExArray_push_new(model->items);
|
||||
item->label = label;
|
||||
item->values_count = values_count;
|
||||
item->change_callback = change_callback;
|
||||
item->context = context;
|
||||
item->current_value_index = 0;
|
||||
item->current_value_text = furi_string_alloc();
|
||||
item->callback_index = callback_index;
|
||||
},
|
||||
true);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
void variable_item_list_ex_set_enter_callback(
|
||||
VariableItemListEx* variable_item_list,
|
||||
VariableItemListExEnterCallback callback,
|
||||
void* context) {
|
||||
furi_assert(callback);
|
||||
with_view_model(
|
||||
variable_item_list->view,
|
||||
VariableItemListExModel * model,
|
||||
{
|
||||
UNUSED(model);
|
||||
variable_item_list->callback = callback;
|
||||
variable_item_list->context = context;
|
||||
},
|
||||
false);
|
||||
}
|
||||
|
||||
void variable_item_ex_set_current_value_index(VariableItemEx* item, uint8_t current_value_index) {
|
||||
item->current_value_index = current_value_index;
|
||||
}
|
||||
|
||||
void variable_item_ex_set_values_count(VariableItemEx* item, uint8_t values_count) {
|
||||
item->values_count = values_count;
|
||||
}
|
||||
|
||||
void variable_item_ex_set_current_value_text(VariableItemEx* item, const char* current_value_text) {
|
||||
furi_string_set(item->current_value_text, current_value_text);
|
||||
}
|
||||
|
||||
uint8_t variable_item_ex_get_current_value_index(VariableItemEx* item) {
|
||||
return item->current_value_index;
|
||||
}
|
||||
|
||||
void* variable_item_ex_get_context(VariableItemEx* item) {
|
||||
return item->context;
|
||||
}
|
||||
+117
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* @file variable_item_list.h
|
||||
* GUI: VariableItemList view module API
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <gui/view.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct VariableItemListEx VariableItemListEx;
|
||||
typedef struct VariableItemEx VariableItemEx;
|
||||
typedef void (*VariableItemExChangeCallback)(VariableItemEx* item);
|
||||
typedef void (*VariableItemListExEnterCallback)(void* context, uint32_t index);
|
||||
|
||||
/** Allocate and initialize VariableItemList
|
||||
*
|
||||
* @return VariableItemList*
|
||||
*/
|
||||
VariableItemListEx* variable_item_list_ex_alloc();
|
||||
|
||||
/** Deinitialize and free VariableItemList
|
||||
*
|
||||
* @param variable_item_list VariableItemList instance
|
||||
*/
|
||||
void variable_item_list_ex_free(VariableItemListEx* variable_item_list);
|
||||
|
||||
/** Clear all elements from list
|
||||
*
|
||||
* @param variable_item_list VariableItemList instance
|
||||
*/
|
||||
void variable_item_list_ex_reset(VariableItemListEx* variable_item_list);
|
||||
|
||||
/** Get VariableItemList View instance
|
||||
*
|
||||
* @param variable_item_list VariableItemList instance
|
||||
*
|
||||
* @return View instance
|
||||
*/
|
||||
View* variable_item_list_ex_get_view(VariableItemListEx* variable_item_list);
|
||||
|
||||
/** Add item to VariableItemList
|
||||
*
|
||||
* @param variable_item_list VariableItemList instance
|
||||
* @param label item name
|
||||
* @param values_count item values count
|
||||
* @param change_callback called on value change in gui
|
||||
* @param context item context
|
||||
*
|
||||
* @return VariableItemEx* item instance
|
||||
*/
|
||||
VariableItemEx* variable_item_list_ex_add(
|
||||
VariableItemListEx* variable_item_list,
|
||||
const char* label,
|
||||
uint8_t values_count,
|
||||
VariableItemExChangeCallback change_callback,
|
||||
void* context,
|
||||
int32_t callback_index);
|
||||
|
||||
/** Set enter callback
|
||||
*
|
||||
* @param variable_item_list VariableItemList instance
|
||||
* @param callback VariableItemListEnterCallback instance
|
||||
* @param context pointer to context
|
||||
*/
|
||||
void variable_item_list_ex_set_enter_callback(
|
||||
VariableItemListEx* variable_item_list,
|
||||
VariableItemListExEnterCallback callback,
|
||||
void* context);
|
||||
|
||||
void variable_item_list_ex_set_selected_item(VariableItemListEx* variable_item_list, uint8_t index);
|
||||
|
||||
uint8_t variable_item_list_ex_get_selected_item_index(VariableItemListEx* variable_item_list);
|
||||
|
||||
/** Set item current selected index
|
||||
*
|
||||
* @param item VariableItemEx* instance
|
||||
* @param current_value_index The current value index
|
||||
*/
|
||||
void variable_item_ex_set_current_value_index(VariableItemEx* item, uint8_t current_value_index);
|
||||
|
||||
/** Set number of values for item
|
||||
*
|
||||
* @param item VariableItemEx* instance
|
||||
* @param values_count The new values count
|
||||
*/
|
||||
void variable_item_ex_set_values_count(VariableItemEx* item, uint8_t values_count);
|
||||
|
||||
/** Set item current selected text
|
||||
*
|
||||
* @param item VariableItemEx* instance
|
||||
* @param current_value_text The current value text
|
||||
*/
|
||||
void variable_item_ex_set_current_value_text(VariableItemEx* item, const char* current_value_text);
|
||||
|
||||
/** Get item current selected index
|
||||
*
|
||||
* @param item VariableItemEx* instance
|
||||
*
|
||||
* @return uint8_t current selected index
|
||||
*/
|
||||
uint8_t variable_item_ex_get_current_value_index(VariableItemEx* item);
|
||||
|
||||
/** Get item context
|
||||
*
|
||||
* @param item VariableItemEx* instance
|
||||
*
|
||||
* @return void* item context
|
||||
*/
|
||||
void* variable_item_ex_get_context(VariableItemEx* item);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user