Skip to content

Commit 3fd0701

Browse files
committed
optimize app::switch_app ignore same app switch
1 parent ae9435b commit 3fd0701

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

components/basic/include/maix_app.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ namespace maix::app
333333
* @param app_id APP ID which will be started. app_id and idx must have one is valid.
334334
* @param idx APP index. app_id and idx must have one is valid.
335335
* @param start_param string type, will send to app, app can get this param by `app.get_start_param()`
336+
* @attention If app id or idx the same as current app, do nothing.
336337
* @maixpy maix.app.switch_app
337338
*/
338339
void switch_app(const string &app_id, int idx = -1, const std::string &start_param = "");

components/basic/src/maix_app.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -580,26 +580,14 @@ namespace maix::app
580580
log::error("switch app failed, app_id and idx must have one is valid\n");
581581
return;
582582
}
583-
// inform this app to exit, code should check this flag by app::need_exit()
584-
set_exit_flag(true);
585583
vector<APP_Info> &apps_info = get_apps_info();
586-
587-
// write app_id to file, launcher will read this file and start the app
588-
string path = get_app_start_info_path();
589-
FILE *fp = fopen(path.c_str(), "w");
590-
if (fp == NULL)
591-
{
592-
log::error("open app start info file failed: %s", path.c_str());
593-
return;
594-
}
595584
std::string final_app_id = app_id;
596585
std::string final_app_path = "";
597586
if (idx >= 0)
598587
{
599588
if ((size_t)idx >= apps_info.size())
600589
{
601590
log::error("idx error, should < %lld, but %d", apps_info.size(), idx);
602-
fclose(fp);
603591
throw err::Exception(err::ERR_ARGS, "idx error");
604592
}
605593
final_app_id = apps_info[idx].id;
@@ -617,6 +605,23 @@ namespace maix::app
617605
}
618606
}
619607
}
608+
// if switch to current app, just return.
609+
if(final_app_id == app::app_id())
610+
{
611+
return;
612+
}
613+
614+
// inform this app to exit, code should check this flag by app::need_exit()
615+
set_exit_flag(true);
616+
617+
// write app_id to file, launcher will read this file and start the app
618+
string path = get_app_start_info_path();
619+
FILE *fp = fopen(path.c_str(), "w");
620+
if (fp == NULL)
621+
{
622+
log::error("open app start info file failed: %s", path.c_str());
623+
return;
624+
}
620625
fprintf(fp, "%s\n%s\n%s\n", final_app_path.c_str(), final_app_id.c_str(), start_param.c_str());
621626
fclose(fp);
622627
// when this app exit, the launcher will check this file and start the app

0 commit comments

Comments
 (0)