Skip to content

Commit

Permalink
Can start several apps in a row (fix AbstractRunner statics)
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed May 21, 2024
1 parent 0959ebb commit 98f241d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
49 changes: 32 additions & 17 deletions src/hello_imgui/internal/backend_impls/abstract_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,31 @@ bool _reloadAllDpiResponsiveFonts();
bool ShouldRemoteDisplay();


struct AbstractRunnerStatics
{
std::string lastLoadedLayout;
bool isFirstLayoutSwitch = true;
bool lastHiddenState = false;
double timeLastEvent = -1.;
double lastRefreshTime = 0.;
};

static AbstractRunnerStatics gStatics;

static void ResetAbstractRunnerStatics() { gStatics = AbstractRunnerStatics(); }



AbstractRunner::AbstractRunner(RunnerParams &params_)
: params(params_) {}


AbstractRunner::~AbstractRunner()
{
ResetAbstractRunnerStatics();
}


#ifndef USEHACK
void AbstractRunner::Run()
{
Expand Down Expand Up @@ -449,10 +470,9 @@ void AbstractRunner::LayoutSettings_SwitchLayout(const std::string& layoutName)

// if we previously loaded another layout, save its settings before changing
{
static bool isFirstLayoutSwitch = true;
if (! isFirstLayoutSwitch)
if (! gStatics.isFirstLayoutSwitch)
LayoutSettings_Save();
isFirstLayoutSwitch = false;
gStatics.isFirstLayoutSwitch = false;
}

if (layoutName.empty())
Expand Down Expand Up @@ -481,11 +501,10 @@ void AbstractRunner::LayoutSettings_SwitchLayout(const std::string& layoutName)
// Those Layout_XXX functions are called before ImGui::NewFrame()
void AbstractRunner::LayoutSettings_HandleChanges()
{
static std::string lastLoadedLayout;
if (params.dockingParams.layoutName != lastLoadedLayout)
if (params.dockingParams.layoutName != gStatics.lastLoadedLayout)
{
LayoutSettings_Load();
lastLoadedLayout = params.dockingParams.layoutName;
gStatics.lastLoadedLayout = params.dockingParams.layoutName;
}
}
void AbstractRunner::LayoutSettings_Load()
Expand Down Expand Up @@ -936,23 +955,21 @@ void AbstractRunner::CreateFramesAndRender()
}


static bool lastHiddenState = false;

// v/ At the 4th frame (mIdxFrame >= 3), we finally show the window
if (mIdxFrame == 3)
{
if (params.appWindowParams.hidden)
mBackendWindowHelper->HideWindow(mWindow);
else
mBackendWindowHelper->ShowWindow(mWindow);
lastHiddenState = params.appWindowParams.hidden;
gStatics.lastHiddenState = params.appWindowParams.hidden;
}
// On subsequent frames, we take into account user modifications of appWindowParams.hidden
if (mIdxFrame > 3)
{
if (params.appWindowParams.hidden != lastHiddenState)
if (params.appWindowParams.hidden != gStatics.lastHiddenState)
{
lastHiddenState = params.appWindowParams.hidden;
gStatics.lastHiddenState = params.appWindowParams.hidden;
if (params.appWindowParams.hidden)
mBackendWindowHelper->HideWindow(mWindow);
else
Expand All @@ -976,11 +993,10 @@ void AbstractRunner::CreateFramesAndRender()

// Keep track of the time of the last event,
// by counting the number of events in the input queue
static double timeLastEvent = ImGui::GetTime();
int nbEventsBefore = ImGui::GetCurrentContext()->InputEventsQueue.size();
// If the last event is recent, do not idle
double now = ImGui::GetTime();
bool preventIdling = (now - timeLastEvent < (double)params.fpsIdling.timeActiveAfterLastEvent);
bool preventIdling = (now - gStatics.timeLastEvent < (double)params.fpsIdling.timeActiveAfterLastEvent);

#ifndef __EMSCRIPTEN__
// Idling for non emscripten, where HelloImGui is responsible for the main loop.
Expand All @@ -1004,7 +1020,7 @@ void AbstractRunner::CreateFramesAndRender()

int nbEventsAfter = ImGui::GetCurrentContext()->InputEventsQueue.size();
if (nbEventsAfter > nbEventsBefore)
timeLastEvent = ImGui::GetTime();
gStatics.timeLastEvent = ImGui::GetTime();

} // SCOPED_RELEASE_GIL_ON_MAIN_THREAD end

Expand Down Expand Up @@ -1178,7 +1194,6 @@ bool AbstractRunner::ShallIdleThisFrame_Emscripten()
return false;
}

static double lastRefreshTime = 0.;
double now = Internal::ClockSeconds();

bool shallIdleThisFrame;
Expand All @@ -1191,15 +1206,15 @@ bool AbstractRunner::ShallIdleThisFrame_Emscripten()
else
{
params.fpsIdling.isIdling = true;
if ((now - lastRefreshTime) < 1. / params.fpsIdling.fpsIdle)
if ((now - gStatics.lastRefreshTime) < 1. / params.fpsIdling.fpsIdle)
shallIdleThisFrame = true;
else
shallIdleThisFrame = false;
}
}

if (! shallIdleThisFrame)
lastRefreshTime = now;
gStatics.lastRefreshTime = now;

return shallIdleThisFrame;
}
Expand Down
2 changes: 1 addition & 1 deletion src/hello_imgui/internal/backend_impls/abstract_runner.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class AbstractRunner
{
public:
explicit AbstractRunner(RunnerParams &params_);
virtual ~AbstractRunner() = default;
virtual ~AbstractRunner();

RunnerParams & params;

Expand Down

0 comments on commit 98f241d

Please sign in to comment.