Skip to content

Commit 6bb050c

Browse files
committed
Stop loading early if window is closed
1 parent ee02884 commit 6bb050c

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

CelestiaWinUI/MainWindow.xaml.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ namespace winrt::CelestiaWinUI::implementation
167167
renderer = CelestiaRenderer(appSettings.EnableMSAA(), [weak_this{ get_weak() }, resourcePath, configPath, locale, layoutDirection, defaultSettings](int32_t)
168168
{
169169
auto strong_this{ weak_this.get() };
170-
return strong_this == nullptr ? false : strong_this->StartEngine(resourcePath, configPath, locale, layoutDirection, defaultSettings);
170+
return (strong_this == nullptr || strong_this->isClosed) ? false : strong_this->StartEngine(resourcePath, configPath, locale, layoutDirection, defaultSettings);
171171
});
172172
renderer.SetCorePointer(appCore.Pointer());
173173
renderer.SetSurface(GLView(), scale);
@@ -186,13 +186,14 @@ namespace winrt::CelestiaWinUI::implementation
186186
hstring localeDirectory = PathHelper::Combine(resourcePath, L"locale");
187187
CelestiaAppCore::SetLocaleDirectory(localeDirectory, locale);
188188

189+
auto ref = get_strong(); // Keep it alive
189190
bool loadSuccess = appCore.StartSimulation(configPath, extraPaths, [weak_this{ get_weak() }](hstring const status) {
190191
auto strong_this{ weak_this.get() };
191-
if (strong_this == nullptr) return;
192+
if (strong_this == nullptr || strong_this->isClosed) return;
192193
strong_this->DispatcherQueue().TryEnqueue(Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ strong_this->get_weak() }, status]()
193194
{
194195
auto strong_this{ weak_this.get() };
195-
if (strong_this == nullptr) return;
196+
if (strong_this == nullptr || strong_this->isClosed) return;
196197
auto textToDisplay = to_hstring(fmt::sprintf(to_string(LocalizationHelper::Localize(L"Loading: %s", L"Celestia initialization, loading file")), to_string(status)));
197198
strong_this->LoadingText().Text(textToDisplay);
198199
});
@@ -201,6 +202,9 @@ namespace winrt::CelestiaWinUI::implementation
201202
{
202203
if (resourcePath != defaultResourcePath || configPath != defaultConfigFilePath)
203204
{
205+
if (isClosed)
206+
return false;
207+
204208
// Try to restore originial settings
205209
DispatcherQueue().TryEnqueue(Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak() }]()
206210
{
@@ -212,11 +216,11 @@ namespace winrt::CelestiaWinUI::implementation
212216
CelestiaAppCore::SetLocaleDirectory(PathHelper::Combine(defaultResourcePath, L"locale"), locale);
213217
if (!appCore.StartSimulation(defaultConfigFilePath, extraPaths, [weak_this{ get_weak() }](hstring const status) {
214218
auto strong_this{ weak_this.get() };
215-
if (strong_this == nullptr) return;
219+
if (strong_this == nullptr || strong_this->isClosed) return;
216220
strong_this->DispatcherQueue().TryEnqueue(Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ strong_this->get_weak() }, status]()
217221
{
218222
auto strong_this{ weak_this.get() };
219-
if (strong_this == nullptr) return;
223+
if (strong_this == nullptr || strong_this->isClosed) return;
220224
auto textToDisplay = to_hstring(fmt::sprintf(to_string(LocalizationHelper::Localize(L"Loading: %s", L"Celestia initialization, loading file")), to_string(status)));
221225
strong_this->LoadingText().Text(textToDisplay);
222226
});
@@ -293,10 +297,13 @@ namespace winrt::CelestiaWinUI::implementation
293297
appCore.SetRenderFont(PathHelper::Combine(pathPrefix, regularFont), regularFontIndex, 9, CelestiaFontStyle::Normal);
294298
appCore.SetRenderFont(PathHelper::Combine(pathPrefix, boldFont), boldFontIndex, 15, CelestiaFontStyle::Large);
295299

300+
if (isClosed)
301+
return false;
302+
296303
DispatcherQueue().TryEnqueue(Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak() }, resourcePath]()
297304
{
298305
auto strong_this{ weak_this.get() };
299-
if (strong_this == nullptr) return;
306+
if (strong_this == nullptr || strong_this->isClosed) return;
300307
strong_this->UpdateScale(true);
301308
strong_this->LoadingText().Visibility(Visibility::Collapsed);
302309
strong_this->resourceManager = ResourceManager(strong_this->extraAddonFolder, strong_this->extraScriptFolder);
@@ -311,10 +318,13 @@ namespace winrt::CelestiaWinUI::implementation
311318

312319
readyForInput = true;
313320

321+
if (isClosed)
322+
return false;
323+
314324
DispatcherQueue().TryEnqueue(Microsoft::UI::Dispatching::DispatcherQueuePriority::Normal, [weak_this{ get_weak() }]()
315325
{
316326
auto strong_this{ weak_this.get() };
317-
if (strong_this == nullptr) return;
327+
if (strong_this == nullptr || strong_this->isClosed) return;
318328
strong_this->OpenFileOrURL();
319329
});
320330
return true;

CelestiaWinUI/MainWindow.xaml.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#pragma once
1111

12+
#include <atomic>
1213
#include "MainWindow.g.h"
1314

1415
namespace winrt::CelestiaWinUI::implementation
@@ -45,7 +46,7 @@ namespace winrt::CelestiaWinUI::implementation
4546
Windows::Foundation::Uri urlToOpen{ nullptr };
4647
bool isXbox{ false };
4748
bool isGLViewFocused{ false };
48-
bool isClosed{ false };
49+
std::atomic<bool> isClosed{ false };
4950

5051
bool StartEngine(hstring const resourcePath, hstring const& configPath, hstring const& locale, CelestiaComponent::CelestiaLayoutDirection layoutDirection, Windows::Data::Json::JsonObject const& defaultSettings);
5152
Windows::Foundation::IAsyncAction CreateExtraFolders();

0 commit comments

Comments
 (0)