From 517818f3568ed5804237549fdb6ca05093343c66 Mon Sep 17 00:00:00 2001 From: Neucrack Date: Wed, 7 Aug 2024 18:47:23 +0800 Subject: [PATCH] optimize app::switch_app ignore same app switch --- components/basic/include/maix_app.hpp | 1 + components/basic/src/maix_app.cpp | 29 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/components/basic/include/maix_app.hpp b/components/basic/include/maix_app.hpp index 2ccb34a8..98ec31f1 100644 --- a/components/basic/include/maix_app.hpp +++ b/components/basic/include/maix_app.hpp @@ -333,6 +333,7 @@ namespace maix::app * @param app_id APP ID which will be started. app_id and idx must have one is valid. * @param idx APP index. app_id and idx must have one is valid. * @param start_param string type, will send to app, app can get this param by `app.get_start_param()` + * @attention If app id or idx the same as current app, do nothing. * @maixpy maix.app.switch_app */ void switch_app(const string &app_id, int idx = -1, const std::string &start_param = ""); diff --git a/components/basic/src/maix_app.cpp b/components/basic/src/maix_app.cpp index 66e37a94..89c564e1 100644 --- a/components/basic/src/maix_app.cpp +++ b/components/basic/src/maix_app.cpp @@ -580,18 +580,7 @@ namespace maix::app log::error("switch app failed, app_id and idx must have one is valid\n"); return; } - // inform this app to exit, code should check this flag by app::need_exit() - set_exit_flag(true); vector &apps_info = get_apps_info(); - - // write app_id to file, launcher will read this file and start the app - string path = get_app_start_info_path(); - FILE *fp = fopen(path.c_str(), "w"); - if (fp == NULL) - { - log::error("open app start info file failed: %s", path.c_str()); - return; - } std::string final_app_id = app_id; std::string final_app_path = ""; if (idx >= 0) @@ -599,7 +588,6 @@ namespace maix::app if ((size_t)idx >= apps_info.size()) { log::error("idx error, should < %lld, but %d", apps_info.size(), idx); - fclose(fp); throw err::Exception(err::ERR_ARGS, "idx error"); } final_app_id = apps_info[idx].id; @@ -617,6 +605,23 @@ namespace maix::app } } } + // if switch to current app, just return. + if(final_app_id == app_id()) + { + return; + } + + // inform this app to exit, code should check this flag by app::need_exit() + set_exit_flag(true); + + // write app_id to file, launcher will read this file and start the app + string path = get_app_start_info_path(); + FILE *fp = fopen(path.c_str(), "w"); + if (fp == NULL) + { + log::error("open app start info file failed: %s", path.c_str()); + return; + } fprintf(fp, "%s\n%s\n%s\n", final_app_path.c_str(), final_app_id.c_str(), start_param.c_str()); fclose(fp); // when this app exit, the launcher will check this file and start the app