Skip to content

Commit 92daa01

Browse files
carlos-zamoraDHowett
authored andcommitted
Display a warning if SUI is unable to write to the settings file (#19027)
Adds logic to display a warning popup if the settings.json is marked as read-only and we try to write to the settings.json file. Previously, this scenario would crash, which definitely isn't right. However, a simple fix of "not-crashing" wouldn't feel right either. This leverages the existing infrastructure to display a warning dialog when we failed to write to the settings file. The main annoyance here is that that popup dialog is located in `TerminalWindow` and is normally triggered from a failed `SettingsLoadEventArgs`. To get around this, `CascadiaSettings::WriteSettingsToDisk()` now returns a boolean to signal if the write was successful; whereas if it fails, a warning is added to the settings object. If we fail to write to disk, the function will return false and we'll raise an event with the settings' warnings to `TerminalPage` which passes it along to `TerminalWindow`. Additionally, this uses `IVectorView<SettingsLoadWarnings>` as opposed to `IVector<SettingsLoadWarnings>` throughout the relevant code. It's more correct as the list of warnings shouldn't be mutable and the warnings from the `CascadiaSettings` object are retrieved in that format. - ✅ Using SUI, save settings when the settings.json is set to read-only Closes #18913 (cherry picked from commit 218c9fb) Service-Card-Id: PVTI_lADOAF3p4s4AmhmQzgb3QSg Service-Version: 1.22
1 parent e21b2c6 commit 92daa01

File tree

15 files changed

+53
-32
lines changed

15 files changed

+53
-32
lines changed

src/cascadia/TerminalApp/AppLogic.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ namespace winrt::TerminalApp::implementation
396396
auto ev = winrt::make_self<SettingsLoadEventArgs>(true,
397397
static_cast<uint64_t>(_settingsLoadedResult),
398398
_settingsLoadExceptionText,
399-
warnings,
399+
warnings.GetView(),
400400
_settings);
401401
SettingsChanged.raise(*this, *ev);
402402
return;
@@ -428,7 +428,7 @@ namespace winrt::TerminalApp::implementation
428428
auto ev = winrt::make_self<SettingsLoadEventArgs>(!initialLoad,
429429
_settingsLoadedResult,
430430
_settingsLoadExceptionText,
431-
warnings,
431+
warnings.GetView(),
432432
_settings);
433433
SettingsChanged.raise(*this, *ev);
434434
}
@@ -656,7 +656,7 @@ namespace winrt::TerminalApp::implementation
656656
auto ev = winrt::make_self<SettingsLoadEventArgs>(false,
657657
_settingsLoadedResult,
658658
_settingsLoadExceptionText,
659-
warnings,
659+
warnings.GetView(),
660660
_settings);
661661

662662
auto window = winrt::make_self<implementation::TerminalWindow>(*ev, _contentManager);

src/cascadia/TerminalApp/SettingsLoadEventArgs.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ namespace winrt::TerminalApp::implementation
1212
WINRT_PROPERTY(bool, Reload, false);
1313
WINRT_PROPERTY(uint64_t, Result, S_OK);
1414
WINRT_PROPERTY(winrt::hstring, ExceptionText, L"");
15-
WINRT_PROPERTY(winrt::Windows::Foundation::Collections::IVector<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>, Warnings, nullptr);
15+
WINRT_PROPERTY(winrt::Windows::Foundation::Collections::IVectorView<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>, Warnings, nullptr);
1616
WINRT_PROPERTY(Microsoft::Terminal::Settings::Model::CascadiaSettings, NewSettings, nullptr);
1717

1818
public:
1919
SettingsLoadEventArgs(bool reload,
2020
uint64_t result,
2121
winrt::hstring exceptionText,
22-
winrt::Windows::Foundation::Collections::IVector<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings> warnings,
22+
winrt::Windows::Foundation::Collections::IVectorView<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings> warnings,
2323
Microsoft::Terminal::Settings::Model::CascadiaSettings newSettings) :
2424
_Reload{ reload },
2525
_Result{ result },

src/cascadia/TerminalApp/TerminalPage.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4097,6 +4097,13 @@ namespace winrt::TerminalApp::implementation
40974097
}
40984098
});
40994099

4100+
sui.ShowLoadWarningsDialog([weakThis{ get_weak() }](auto&& /*s*/, const Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>& warnings) {
4101+
if (auto page{ weakThis.get() })
4102+
{
4103+
page->ShowLoadWarningsDialog.raise(*page, warnings);
4104+
}
4105+
});
4106+
41004107
return *settingsContent;
41014108
}
41024109

src/cascadia/TerminalApp/TerminalPage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ namespace winrt::TerminalApp::implementation
199199
til::typed_event<IInspectable, IInspectable> OpenSystemMenu;
200200
til::typed_event<IInspectable, IInspectable> QuitRequested;
201201
til::typed_event<IInspectable, winrt::Microsoft::Terminal::Control::ShowWindowArgs> ShowWindowChanged;
202+
til::typed_event<Windows::Foundation::IInspectable, Windows::Foundation::Collections::IVectorView<winrt::Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>> ShowLoadWarningsDialog;
202203

203204
til::typed_event<Windows::Foundation::IInspectable, winrt::TerminalApp::RequestMoveContentArgs> RequestMoveContent;
204205
til::typed_event<Windows::Foundation::IInspectable, winrt::TerminalApp::RequestReceiveContentArgs> RequestReceiveContent;

src/cascadia/TerminalApp/TerminalPage.idl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ namespace TerminalApp
103103

104104
event Windows.Foundation.TypedEventHandler<Object, Object> OpenSystemMenu;
105105
event Windows.Foundation.TypedEventHandler<Object, Microsoft.Terminal.Control.ShowWindowArgs> ShowWindowChanged;
106+
event Windows.Foundation.TypedEventHandler<Object, Windows.Foundation.Collections.IVectorView<Microsoft.Terminal.Settings.Model.SettingsLoadWarnings> > ShowLoadWarningsDialog;
106107

107108
event Windows.Foundation.TypedEventHandler<Object, RequestMoveContentArgs> RequestMoveContent;
108109
event Windows.Foundation.TypedEventHandler<Object, RequestReceiveContentArgs> RequestReceiveContent;

src/cascadia/TerminalApp/TerminalWindow.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ namespace winrt::TerminalApp::implementation
217217
_root->SetSettings(_settings, false); // We're on our UI thread right now, so this is safe
218218
_root->Loaded({ get_weak(), &TerminalWindow::_OnLoaded });
219219
_root->Initialized({ get_weak(), &TerminalWindow::_pageInitialized });
220+
_root->ShowLoadWarningsDialog({ get_weak(), &TerminalWindow::_ShowLoadWarningsDialog });
220221
_root->Create();
221222

222223
AppLogic::Current()->SettingsChanged({ get_weak(), &TerminalWindow::UpdateSettingsHandler });
@@ -441,7 +442,7 @@ namespace winrt::TerminalApp::implementation
441442
// validating the settings.
442443
// - Only one dialog can be visible at a time. If another dialog is visible
443444
// when this is called, nothing happens. See ShowDialog for details
444-
void TerminalWindow::_ShowLoadWarningsDialog(const Windows::Foundation::Collections::IVector<SettingsLoadWarnings>& warnings)
445+
void TerminalWindow::_ShowLoadWarningsDialog(const IInspectable&, const Windows::Foundation::Collections::IVectorView<SettingsLoadWarnings>& warnings)
445446
{
446447
auto title = RS_(L"SettingsValidateErrorTitle");
447448
auto buttonText = RS_(L"Ok");
@@ -501,7 +502,7 @@ namespace winrt::TerminalApp::implementation
501502
}
502503
else if (settingsLoadedResult == S_FALSE)
503504
{
504-
_ShowLoadWarningsDialog(_initialLoadResult.Warnings());
505+
_ShowLoadWarningsDialog(nullptr, _initialLoadResult.Warnings());
505506
}
506507
}
507508

@@ -802,7 +803,7 @@ namespace winrt::TerminalApp::implementation
802803
}
803804
else if (args.Result() == S_FALSE)
804805
{
805-
_ShowLoadWarningsDialog(args.Warnings());
806+
_ShowLoadWarningsDialog(nullptr, args.Warnings());
806807
}
807808
else if (args.Result() == S_OK)
808809
{

src/cascadia/TerminalApp/TerminalWindow.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ namespace winrt::TerminalApp::implementation
194194
const winrt::hstring& contentKey,
195195
HRESULT settingsLoadedResult,
196196
const winrt::hstring& exceptionText);
197-
void _ShowLoadWarningsDialog(const Windows::Foundation::Collections::IVector<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>& warnings);
197+
void _ShowLoadWarningsDialog(const IInspectable& sender, const Windows::Foundation::Collections::IVectorView<Microsoft::Terminal::Settings::Model::SettingsLoadWarnings>& warnings);
198198

199199
bool _IsKeyboardServiceEnabled();
200200

src/cascadia/TerminalApp/TerminalWindow.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace TerminalApp
3030
{
3131
Boolean Reload { get; };
3232
UInt64 Result { get; };
33-
IVector<Microsoft.Terminal.Settings.Model.SettingsLoadWarnings> Warnings { get; };
33+
IVectorView<Microsoft.Terminal.Settings.Model.SettingsLoadWarnings> Warnings { get; };
3434
String ExceptionText { get; };
3535

3636
Microsoft.Terminal.Settings.Model.CascadiaSettings NewSettings { get; };

src/cascadia/TerminalSettingsEditor/MainPage.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,10 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
496496
void MainPage::SaveButton_Click(const IInspectable& /*sender*/, const RoutedEventArgs& /*args*/)
497497
{
498498
_settingsClone.LogSettingChanges(false);
499-
_settingsClone.WriteSettingsToDisk();
499+
if (!_settingsClone.WriteSettingsToDisk())
500+
{
501+
ShowLoadWarningsDialog.raise(*this, _settingsClone.Warnings());
502+
}
500503
}
501504

502505
void MainPage::ResetButton_Click(const IInspectable& /*sender*/, const RoutedEventArgs& /*args*/)

src/cascadia/TerminalSettingsEditor/MainPage.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
4545
Windows::Foundation::Collections::IObservableVector<IInspectable> Breadcrumbs() noexcept;
4646

4747
til::typed_event<Windows::Foundation::IInspectable, Model::SettingsTarget> OpenJson;
48+
til::typed_event<Windows::Foundation::IInspectable, Windows::Foundation::Collections::IVectorView<Model::SettingsLoadWarnings>> ShowLoadWarningsDialog;
4849

4950
private:
5051
Windows::Foundation::Collections::IObservableVector<IInspectable> _breadcrumbs;

0 commit comments

Comments
 (0)