فهرست منبع

add confirm exit action

Oliver Fabel 1 سال پیش
والد
کامیت
7c09ff36cf
6فایلهای تغییر یافته به همراه74 افزوده شده و 48 حذف شده
  1. 17 14
      upython.c
  2. 12 8
      upython.h
  3. 8 6
      upython_cli.c
  4. 1 1
      upython_file.c
  5. 1 1
      upython_repl.c
  6. 35 18
      upython_splash.c

+ 17 - 14
upython.c

@@ -1,11 +1,5 @@
-#include <malloc.h>
-
 #include <furi.h>
-#include <gui/gui.h>
-#include <dialogs/dialogs.h>
 #include <storage/storage.h>
-#include <cli/cli.h>
-#include <cli/cli_vcp.h>
 
 #include <mp_flipper_runtime.h>
 #include <mp_flipper_compiler.h>
@@ -15,41 +9,50 @@
 Action action = ActionNone;
 FuriString* file_path = NULL;
 
+void upython_reset_file_path() {
+    furi_string_set(file_path, APP_ASSETS_PATH("upython"));
+}
+
 int32_t upython(void* args) {
-    mp_flipper_cli_register(args);
+    upython_cli_register(args);
 
     do {
         switch(action) {
         case ActionNone:
-            action = mp_flipper_splash_screen();
+            action = upython_splash_screen();
 
             break;
         case ActionOpen:
-            if(mp_flipper_select_python_file(file_path)) {
+            if(upython_select_python_file(file_path)) {
                 action = ActionExec;
             } else {
-                furi_string_set(file_path, APP_ASSETS_PATH("upython"));
+                upython_reset_file_path();
+
+                action = ActionNone;
             }
 
             break;
         case ActionRepl:
             break;
         case ActionExec:
-            mp_flipper_file_execute(file_path);
+            upython_file_execute(file_path);
 
-            furi_string_set(file_path, APP_ASSETS_PATH("upython"));
+            upython_reset_file_path();
 
             action = ActionNone;
 
             break;
         case ActionExit:
+            action = upython_confirm_exit_action() ? ActionTerm : ActionNone;
+            break;
+        case ActionTerm:
             break;
         }
 
         furi_delay_ms(1);
-    } while(action != ActionExit);
+    } while(action != ActionTerm);
 
-    mp_flipper_cli_unregister(args);
+    upython_cli_unregister(args);
 
     return 0;
 }

+ 12 - 8
upython.h

@@ -11,20 +11,24 @@ typedef enum {
     ActionOpen,
     ActionRepl,
     ActionExec,
-    ActionExit
+    ActionExit,
+    ActionTerm
 } Action;
 
 extern Action action;
 extern FuriString* file_path;
 
-Action mp_flipper_splash_screen();
-bool mp_flipper_select_python_file(FuriString* file_path);
+void upython_reset_file_path();
 
-void mp_flipper_cli_register(void* args);
-void mp_flipper_cli_unregister(void* args);
+Action upython_splash_screen();
+bool upython_confirm_exit_action();
+bool upython_select_python_file(FuriString* file_path);
 
-void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx);
+void upython_cli_register(void* args);
+void upython_cli_unregister(void* args);
 
-void mp_flipper_repl_execute(Cli* cli);
+void upython_cli(Cli* cli, FuriString* args, void* ctx);
 
-void mp_flipper_file_execute(FuriString* file);
+void upython_repl_execute(Cli* cli);
+
+void upython_file_execute(FuriString* file);

+ 8 - 6
upython_cli.c

@@ -2,7 +2,7 @@
 
 #include "upython.h"
 
-void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx) {
+void upython_cli(Cli* cli, FuriString* args, void* ctx) {
     UNUSED(ctx);
 
     if(action != ActionNone) {
@@ -14,7 +14,7 @@ void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx) {
     if(furi_string_empty(args)) {
         action = ActionRepl;
 
-        mp_flipper_repl_execute(cli);
+        upython_repl_execute(cli);
 
         action = ActionNone;
     } else {
@@ -24,7 +24,7 @@ void mp_flipper_cli(Cli* cli, FuriString* args, void* ctx) {
     }
 }
 
-void mp_flipper_cli_register(void* args) {
+void upython_cli_register(void* args) {
     if(args != NULL) {
         file_path = furi_string_alloc_set_str(args);
 
@@ -32,19 +32,21 @@ void mp_flipper_cli_register(void* args) {
 
         return;
     } else {
-        file_path = furi_string_alloc_set_str(APP_ASSETS_PATH("upython"));
+        file_path = furi_string_alloc();
+
+        upython_reset_file_path();
 
         action = ActionNone;
     }
 
     Cli* cli = furi_record_open(RECORD_CLI);
 
-    cli_add_command(cli, CLI, CliCommandFlagParallelSafe, mp_flipper_cli, NULL);
+    cli_add_command(cli, CLI, CliCommandFlagParallelSafe, upython_cli, NULL);
 
     furi_record_close(RECORD_CLI);
 }
 
-void mp_flipper_cli_unregister(void* args) {
+void upython_cli_unregister(void* args) {
     furi_string_free(file_path);
 
     if(args != NULL) {

+ 1 - 1
upython_file.c

@@ -8,7 +8,7 @@
 
 #include "upython.h"
 
-void mp_flipper_file_execute(FuriString* file) {
+void upython_file_execute(FuriString* file) {
     size_t stack;
 
     const char* path = furi_string_get_cstr(file);

+ 1 - 1
upython_repl.c

@@ -243,7 +243,7 @@ inline static bool continue_with_input(mp_flipper_repl_context_t* context) {
     return true;
 }
 
-void mp_flipper_repl_execute(Cli* cli) {
+void upython_repl_execute(Cli* cli) {
     size_t stack;
 
     const size_t heap_size = memmgr_get_free_heap() * 0.1;

+ 35 - 18
upython_splash.c

@@ -13,23 +13,6 @@
 #include "upython.h"
 #include "upython_icons.h"
 
-bool mp_flipper_select_python_file(FuriString* file_path) {
-    DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
-
-    DialogsFileBrowserOptions browser_options;
-
-    dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
-
-    browser_options.hide_ext = false;
-    browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
-
-    bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
-
-    furi_record_close(RECORD_DIALOGS);
-
-    return result;
-}
-
 static void on_input(const void* event, void* ctx) {
     UNUSED(ctx);
 
@@ -53,7 +36,41 @@ static void on_input(const void* event, void* ctx) {
     }
 }
 
-Action mp_flipper_splash_screen() {
+bool upython_confirm_exit_action() {
+    DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
+
+    DialogMessage* message = dialog_message_alloc();
+
+    dialog_message_set_text(message, "Close uPython?", 64, 32, AlignCenter, AlignCenter);
+    dialog_message_set_buttons(message, "Yes", NULL, "No");
+
+    DialogMessageButton button = dialog_message_show(dialogs, message);
+
+    dialog_message_free(message);
+
+    furi_record_close(RECORD_DIALOGS);
+
+    return button == DialogMessageButtonLeft;
+}
+
+bool upython_select_python_file(FuriString* file_path) {
+    DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
+
+    DialogsFileBrowserOptions browser_options;
+
+    dialog_file_browser_set_basic_options(&browser_options, "py", NULL);
+
+    browser_options.hide_ext = false;
+    browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
+
+    bool result = dialog_file_browser_show(dialogs, file_path, file_path, &browser_options);
+
+    furi_record_close(RECORD_DIALOGS);
+
+    return result;
+}
+
+Action upython_splash_screen() {
     if(action != ActionNone) {
         return action;
     }