mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-11 06:09:08 -07:00
Completely new text viewer app (proper newlines)
Code borrowed from archive app show scene, will be used to replace that
This commit is contained in:
30
applications/external/text_viewer/scenes/text_viewer_scene.c
vendored
Normal file
30
applications/external/text_viewer/scenes/text_viewer_scene.c
vendored
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "text_viewer_scene.h"
|
||||
|
||||
// Generate scene on_enter handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_enter,
|
||||
void (*const text_viewer_on_enter_handlers[])(void*) = {
|
||||
#include "text_viewer_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_event,
|
||||
bool (*const text_viewer_on_event_handlers[])(void* context, SceneManagerEvent event) = {
|
||||
#include "text_viewer_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers array
|
||||
#define ADD_SCENE(prefix, name, id) prefix##_scene_##name##_on_exit,
|
||||
void (*const text_viewer_on_exit_handlers[])(void* context) = {
|
||||
#include "text_viewer_scene_config.h"
|
||||
};
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Initialize scene handlers configuration structure
|
||||
const SceneManagerHandlers text_viewer_scene_handlers = {
|
||||
.on_enter_handlers = text_viewer_on_enter_handlers,
|
||||
.on_event_handlers = text_viewer_on_event_handlers,
|
||||
.on_exit_handlers = text_viewer_on_exit_handlers,
|
||||
.scene_num = TextViewerSceneNum,
|
||||
};
|
||||
29
applications/external/text_viewer/scenes/text_viewer_scene.h
vendored
Normal file
29
applications/external/text_viewer/scenes/text_viewer_scene.h
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <gui/scene_manager.h>
|
||||
|
||||
// Generate scene id and total number
|
||||
#define ADD_SCENE(prefix, name, id) TextViewerScene##id,
|
||||
typedef enum {
|
||||
#include "text_viewer_scene_config.h"
|
||||
TextViewerSceneNum,
|
||||
} TextViewerScene;
|
||||
#undef ADD_SCENE
|
||||
|
||||
extern const SceneManagerHandlers text_viewer_scene_handlers;
|
||||
|
||||
// Generate scene on_enter handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_enter(void*);
|
||||
#include "text_viewer_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_event handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) \
|
||||
bool prefix##_scene_##name##_on_event(void* context, SceneManagerEvent event);
|
||||
#include "text_viewer_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
|
||||
// Generate scene on_exit handlers declaration
|
||||
#define ADD_SCENE(prefix, name, id) void prefix##_scene_##name##_on_exit(void* context);
|
||||
#include "text_viewer_scene_config.h"
|
||||
#undef ADD_SCENE
|
||||
1
applications/external/text_viewer/scenes/text_viewer_scene_config.h
vendored
Normal file
1
applications/external/text_viewer/scenes/text_viewer_scene_config.h
vendored
Normal file
@@ -0,0 +1 @@
|
||||
ADD_SCENE(text_viewer, show, Show)
|
||||
132
applications/external/text_viewer/scenes/text_viewer_scene_show.c
vendored
Normal file
132
applications/external/text_viewer/scenes/text_viewer_scene_show.c
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
#include "../text_viewer.h"
|
||||
|
||||
#define SHOW_MAX_FILE_SIZE 8000
|
||||
|
||||
void text_viewer_scene_show_widget_callback(GuiButtonType result, InputType type, void* context) {
|
||||
furi_assert(context);
|
||||
TextViewer* app = (TextViewer*)context;
|
||||
if(type == InputTypeShort) {
|
||||
view_dispatcher_send_custom_event(app->view_dispatcher, result);
|
||||
}
|
||||
}
|
||||
|
||||
static bool text_show_read_lines(File* file, FuriString* str_result) {
|
||||
//furi_string_reset(str_result);
|
||||
uint8_t buffer[SHOW_MAX_FILE_SIZE];
|
||||
|
||||
uint16_t read_count = storage_file_read(file, buffer, SHOW_MAX_FILE_SIZE);
|
||||
if(storage_file_get_error(file) != FSE_OK) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for(uint16_t i = 0; i < read_count; i++) {
|
||||
furi_string_push_back(str_result, buffer[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void text_viewer_scene_show_on_enter(void* context) {
|
||||
furi_assert(context);
|
||||
TextViewer* app = context;
|
||||
|
||||
FuriString* buffer;
|
||||
buffer = furi_string_alloc();
|
||||
|
||||
Storage* storage = furi_record_open(RECORD_STORAGE);
|
||||
File* file = storage_file_alloc(storage);
|
||||
|
||||
FileInfo fileinfo;
|
||||
FS_Error error = storage_common_stat(storage, furi_string_get_cstr(app->path), &fileinfo);
|
||||
if(error == FSE_OK) {
|
||||
if((fileinfo.size < SHOW_MAX_FILE_SIZE) && (fileinfo.size > 2)) {
|
||||
bool ok = storage_file_open(
|
||||
file, furi_string_get_cstr(app->path), FSAM_READ, FSOM_OPEN_EXISTING);
|
||||
if(ok) {
|
||||
if(!text_show_read_lines(file, buffer)) {
|
||||
goto text_file_read_err;
|
||||
}
|
||||
if(!furi_string_size(buffer)) {
|
||||
goto text_file_read_err;
|
||||
}
|
||||
|
||||
storage_file_seek(file, 0, true);
|
||||
|
||||
widget_add_text_scroll_element(
|
||||
app->widget, 0, 0, 128, 64, furi_string_get_cstr(buffer));
|
||||
|
||||
} else {
|
||||
text_file_read_err:
|
||||
widget_add_text_box_element(
|
||||
app->widget,
|
||||
0,
|
||||
0,
|
||||
128,
|
||||
64,
|
||||
AlignLeft,
|
||||
AlignCenter,
|
||||
"\e#Error:\nStorage file open error\e#",
|
||||
false);
|
||||
}
|
||||
storage_file_close(file);
|
||||
} else if(fileinfo.size < 2) {
|
||||
widget_add_text_box_element(
|
||||
app->widget,
|
||||
0,
|
||||
0,
|
||||
128,
|
||||
64,
|
||||
AlignLeft,
|
||||
AlignCenter,
|
||||
"\e#Error:\nFile is too small\e#",
|
||||
false);
|
||||
} else {
|
||||
widget_add_text_box_element(
|
||||
app->widget,
|
||||
0,
|
||||
0,
|
||||
128,
|
||||
64,
|
||||
AlignLeft,
|
||||
AlignCenter,
|
||||
"\e#Error:\nFile is too large to show\e#",
|
||||
false);
|
||||
}
|
||||
} else {
|
||||
widget_add_text_box_element(
|
||||
app->widget,
|
||||
0,
|
||||
0,
|
||||
128,
|
||||
64,
|
||||
AlignLeft,
|
||||
AlignCenter,
|
||||
"\e#Error:\nFile system error\e#",
|
||||
false);
|
||||
}
|
||||
|
||||
furi_string_free(buffer);
|
||||
|
||||
storage_file_free(file);
|
||||
furi_record_close(RECORD_STORAGE);
|
||||
|
||||
view_dispatcher_switch_to_view(app->view_dispatcher, TextViewerViewWidget);
|
||||
}
|
||||
|
||||
bool text_viewer_scene_show_on_event(void* context, SceneManagerEvent event) {
|
||||
furi_assert(context);
|
||||
TextViewer* app = (TextViewer*)context;
|
||||
|
||||
if(event.type == SceneManagerEventTypeCustom) {
|
||||
scene_manager_previous_scene(app->scene_manager);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void text_viewer_scene_show_on_exit(void* context) {
|
||||
furi_assert(context);
|
||||
TextViewer* app = (TextViewer*)context;
|
||||
|
||||
widget_reset(app->widget);
|
||||
}
|
||||
Reference in New Issue
Block a user