-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathCascadiaSettings.cpp
1184 lines (1061 loc) · 44.3 KB
/
CascadiaSettings.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "CascadiaSettings.h"
#include "CascadiaSettings.g.cpp"
#include "DefaultTerminal.h"
#include "FileUtils.h"
#include <LibraryResources.h>
#include <VersionHelpers.h>
#include <WtExeUtils.h>
#include <shellapi.h>
#include <til/latch.h>
#include <til/env.h>
using namespace winrt::Microsoft::Terminal;
using namespace winrt::Microsoft::Terminal::Settings;
using namespace winrt::Microsoft::Terminal::Settings::Model::implementation;
using namespace winrt::Microsoft::Terminal::Control;
using namespace winrt::Windows::Foundation::Collections;
using namespace Microsoft::Console;
// Creating a child of a profile requires us to copy certain
// required attributes. This method handles those attributes.
//
// NOTE however that it doesn't call _FinalizeInheritance() for you! Don't forget that!
//
// At the time of writing only one caller needs to call _FinalizeInheritance(),
// which is why this unsafety wasn't further abstracted away.
winrt::com_ptr<Profile> Model::implementation::CreateChild(const winrt::com_ptr<Profile>& parent)
{
// If you add more fields here, make sure to do the same in
// SettingsLoader::_addUserProfileParent().
auto profile = winrt::make_self<Profile>();
profile->Origin(OriginTag::User);
profile->Name(parent->Name());
profile->Guid(parent->Guid());
profile->Hidden(parent->Hidden());
profile->AddLeastImportantParent(parent);
return profile;
}
std::string_view Model::implementation::LoadStringResource(int resourceID)
{
const HINSTANCE moduleInstanceHandle{ wil::GetModuleInstanceHandle() };
const auto resource = FindResourceW(moduleInstanceHandle, MAKEINTRESOURCEW(resourceID), RT_RCDATA);
const auto loaded = LoadResource(moduleInstanceHandle, resource);
const auto sz = SizeofResource(moduleInstanceHandle, resource);
const auto ptr = LockResource(loaded);
return { reinterpret_cast<const char*>(ptr), sz };
}
winrt::hstring CascadiaSettings::Hash() const noexcept
{
return _hash;
}
Model::CascadiaSettings CascadiaSettings::Copy() const
{
const auto settings{ winrt::make_self<CascadiaSettings>() };
// user settings
{
std::vector<Model::Profile> allProfiles;
std::vector<Model::Profile> activeProfiles;
allProfiles.reserve(_allProfiles.Size());
activeProfiles.reserve(_activeProfiles.Size());
// Clone the graph of profiles.
// _baseLayerProfile is part of the graph
// and thus needs to be handled here as well.
{
std::vector<winrt::com_ptr<Profile>> sourceProfiles;
std::vector<winrt::com_ptr<Profile>> targetProfiles;
sourceProfiles.reserve(allProfiles.size());
targetProfiles.reserve(allProfiles.size());
for (const auto& profile : _allProfiles)
{
winrt::com_ptr<Profile> profileImpl;
profileImpl.copy_from(winrt::get_self<Profile>(profile));
sourceProfiles.emplace_back(std::move(profileImpl));
}
// Profiles are basically a directed acyclic graph. Cloning it without creating duplicated nodes,
// requires us to "intern" visited profiles. Thus the "visited" map contains a cache of
// previously cloned profiles/sub-graphs. It maps from source-profile-pointer to cloned-profile.
std::unordered_map<const Profile*, winrt::com_ptr<Profile>> visited;
// I'm just gonna estimate that each profile has 3 parents at most on average:
// * base layer
// * fragment
// * inbox defaults
visited.reserve(sourceProfiles.size() * 3);
// _baseLayerProfile is part of the profile graph.
// In order to get a reference to the clone, we need to copy it explicitly.
settings->_baseLayerProfile = _baseLayerProfile->CopyInheritanceGraph(visited);
Profile::CopyInheritanceGraphs(visited, sourceProfiles, targetProfiles);
for (const auto& profile : targetProfiles)
{
allProfiles.emplace_back(*profile);
if (!profile->Hidden() && !profile->Orphaned())
{
activeProfiles.emplace_back(*profile);
}
}
}
settings->_globals = _globals->Copy();
settings->_allProfiles = winrt::single_threaded_observable_vector(std::move(allProfiles));
settings->_activeProfiles = winrt::single_threaded_observable_vector(std::move(activeProfiles));
}
// load errors
{
std::vector<Model::SettingsLoadWarnings> warnings{ _warnings.Size() };
_warnings.GetMany(0, warnings);
settings->_warnings = winrt::single_threaded_vector(std::move(warnings));
settings->_loadError = _loadError;
settings->_deserializationErrorMessage = _deserializationErrorMessage;
}
// defterm
settings->_currentDefaultTerminal = _currentDefaultTerminal;
return *settings;
}
// Method Description:
// - Finds a profile that matches the given GUID. If there is no profile in this
// settings object that matches, returns nullptr.
// Arguments:
// - guid: the GUID of the profile to return.
// Return Value:
// - a strong reference to the profile matching the given guid, or nullptr
// if there is no match.
Model::Profile CascadiaSettings::FindProfile(const winrt::guid& guid) const noexcept
{
for (const auto& profile : _allProfiles)
{
if (profile.Guid() == guid)
{
return profile;
}
}
return nullptr;
}
// Method Description:
// - Returns an iterable collection of all of our Profiles.
// Arguments:
// - <none>
// Return Value:
// - an iterable collection of all of our Profiles.
IObservableVector<Model::Profile> CascadiaSettings::AllProfiles() const noexcept
{
return _allProfiles;
}
// Method Description:
// - Returns an iterable collection of all of our non-hidden Profiles.
// Arguments:
// - <none>
// Return Value:
// - an iterable collection of all of our Profiles.
IObservableVector<Model::Profile> CascadiaSettings::ActiveProfiles() const noexcept
{
return _activeProfiles;
}
// Method Description:
// - Returns the globally configured keybindings
// Arguments:
// - <none>
// Return Value:
// - the globally configured keybindings
Model::ActionMap CascadiaSettings::ActionMap() const noexcept
{
return _globals->ActionMap();
}
// Method Description:
// - Get a reference to our global settings
// Arguments:
// - <none>
// Return Value:
// - a reference to our global settings
Model::GlobalAppSettings CascadiaSettings::GlobalSettings() const
{
return *_globals;
}
// Method Description:
// - Get a reference to our profiles.defaults object
// Arguments:
// - <none>
// Return Value:
// - a reference to our profile.defaults object
Model::Profile CascadiaSettings::ProfileDefaults() const
{
return *_baseLayerProfile;
}
// Method Description:
// - Create a new profile based off the default profile settings.
// Arguments:
// - <none>
// Return Value:
// - a reference to the new profile
Model::Profile CascadiaSettings::CreateNewProfile()
{
if (_allProfiles.Size() == std::numeric_limits<uint32_t>::max())
{
// Shouldn't really happen
return nullptr;
}
std::wstring newName;
for (uint32_t candidateIndex = 0, count = _allProfiles.Size() + 1; candidateIndex < count; candidateIndex++)
{
// There is a theoretical unsigned integer wraparound, which is OK
newName = fmt::format(FMT_COMPILE(L"Profile {}"), count + candidateIndex);
if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
{
break;
}
}
const auto newProfile = _createNewProfile(newName);
_allProfiles.Append(*newProfile);
_activeProfiles.Append(*newProfile);
return *newProfile;
}
template<typename T>
static bool isProfilesDefaultsOrigin(const T& profile)
{
return profile && profile.Origin() != winrt::Microsoft::Terminal::Settings::Model::OriginTag::ProfilesDefaults;
}
template<typename T>
static bool isProfilesDefaultsOriginSub(const T& sub)
{
return sub && isProfilesDefaultsOrigin(sub.SourceProfile());
}
// Method Description:
// - Duplicate a new profile based off another profile's settings
// - This differs from Profile::Copy because it also copies over settings
// that were not defined in the json (for example, settings that were
// defined in one of the parents)
// - This will not duplicate settings that were defined in profiles.defaults
// however, because we do not want the json blob generated from the new profile
// to contain those settings
// Arguments:
// - source: the Profile object we are duplicating (must not be null)
// Return Value:
// - a reference to the new profile
Model::Profile CascadiaSettings::DuplicateProfile(const Model::Profile& source)
{
THROW_HR_IF_NULL(E_INVALIDARG, source);
auto newName = fmt::format(FMT_COMPILE(L"{} ({})"), source.Name(), RS_(L"CopySuffix"));
// Check if this name already exists and if so, append a number
for (uint32_t candidateIndex = 0, count = _allProfiles.Size() + 1; candidateIndex < count; ++candidateIndex)
{
if (std::none_of(begin(_allProfiles), end(_allProfiles), [&](auto&& profile) { return profile.Name() == newName; }))
{
break;
}
// There is a theoretical unsigned integer wraparound, which is OK
newName = fmt::format(FMT_COMPILE(L"{} ({} {})"), source.Name(), RS_(L"CopySuffix"), candidateIndex + 2);
}
const auto duplicated = _createNewProfile(newName);
#define NEEDS_DUPLICATION(settingName) source.Has##settingName() || isProfilesDefaultsOrigin(source.settingName##OverrideSource())
#define NEEDS_DUPLICATION_SUB(source, settingName) source.Has##settingName() || isProfilesDefaultsOriginSub(source.settingName##OverrideSource())
#define DUPLICATE_SETTING_MACRO(settingName) \
if (NEEDS_DUPLICATION(settingName)) \
{ \
duplicated->settingName(source.settingName()); \
}
#define DUPLICATE_SETTING_MACRO_SUB(source, target, settingName) \
if (NEEDS_DUPLICATION_SUB(source, settingName)) \
{ \
target.settingName(source.settingName()); \
}
// If the source is hidden and the Settings UI creates a
// copy of it we don't want the copy to be hidden as well.
// --> Don't do DUPLICATE_SETTING_MACRO(Hidden);
#define DUPLICATE_PROFILE_SETTINGS(type, name, jsonKey, ...) \
DUPLICATE_SETTING_MACRO(name);
MTSM_PROFILE_SETTINGS(DUPLICATE_PROFILE_SETTINGS)
#undef DUPLICATE_PROFILE_SETTINGS
// These aren't in MTSM_PROFILE_SETTINGS because they're special
DUPLICATE_SETTING_MACRO(TabColor);
DUPLICATE_SETTING_MACRO(Padding);
DUPLICATE_SETTING_MACRO(Icon);
{
const auto font = source.FontInfo();
const auto target = duplicated->FontInfo();
#define DUPLICATE_FONT_SETTINGS(type, name, jsonKey, ...) \
DUPLICATE_SETTING_MACRO_SUB(font, target, name);
MTSM_FONT_SETTINGS(DUPLICATE_FONT_SETTINGS)
#undef DUPLICATE_FONT_SETTINGS
}
{
const auto appearance = source.DefaultAppearance();
const auto target = duplicated->DefaultAppearance();
#define DUPLICATE_APPEARANCE_SETTINGS(type, name, jsonKey, ...) \
DUPLICATE_SETTING_MACRO_SUB(appearance, target, name);
MTSM_APPEARANCE_SETTINGS(DUPLICATE_APPEARANCE_SETTINGS)
#undef DUPLICATE_APPEARANCE_SETTINGS
// These aren't in MTSM_APPEARANCE_SETTINGS because they're special
DUPLICATE_SETTING_MACRO_SUB(appearance, target, Foreground);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, Background);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, SelectionBackground);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, CursorColor);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, Opacity);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, DarkColorSchemeName);
DUPLICATE_SETTING_MACRO_SUB(appearance, target, LightColorSchemeName);
}
// UnfocusedAppearance is treated as a single setting,
// but requires a little more legwork to duplicate properly
if (NEEDS_DUPLICATION(UnfocusedAppearance))
{
// It is alright to simply call CopyAppearance here instead of needing a separate function
// like DuplicateAppearance since UnfocusedAppearance is treated as a single setting.
const auto unfocusedAppearance = AppearanceConfig::CopyAppearance(
winrt::get_self<AppearanceConfig>(source.UnfocusedAppearance()),
winrt::weak_ref<Model::Profile>(*duplicated));
// Make sure to add the default appearance of the duplicated profile as a parent to the duplicate's UnfocusedAppearance
winrt::com_ptr<AppearanceConfig> defaultAppearance;
defaultAppearance.copy_from(winrt::get_self<AppearanceConfig>(duplicated->DefaultAppearance()));
unfocusedAppearance->AddLeastImportantParent(defaultAppearance);
duplicated->UnfocusedAppearance(*unfocusedAppearance);
}
// GH#12120: Check if the connection type isn't just the default value. If
// it is, then we should copy it. The only case this applies right now is
// for the Azure Cloud Shell, which is the only thing that has a non-{}
// guid. The user's version of this profile won't have connectionType set,
// because it inherits the setting from the parent. If we fail to copy it
// here, they won't actually get a Azure shell profile.
if (source.ConnectionType() != winrt::guid{})
{
duplicated->ConnectionType(source.ConnectionType());
}
_allProfiles.Append(*duplicated);
_activeProfiles.Append(*duplicated);
return *duplicated;
}
// Method Description:
// - Gets our list of warnings we found during loading. These are things that we
// knew were bad when we called `_ValidateSettings` last.
// Return Value:
// - a reference to our list of warnings.
IVectorView<Model::SettingsLoadWarnings> CascadiaSettings::Warnings() const
{
return _warnings.GetView();
}
winrt::Windows::Foundation::IReference<Model::SettingsLoadErrors> CascadiaSettings::GetLoadingError() const
{
return _loadError;
}
winrt::hstring CascadiaSettings::GetSerializationErrorMessage() const
{
return _deserializationErrorMessage;
}
// As used by CreateNewProfile and DuplicateProfile this function
// creates a new Profile instance with a random UUID and a given name.
winrt::com_ptr<Profile> CascadiaSettings::_createNewProfile(const std::wstring_view& name) const
{
// Technically there's Utils::CreateV5Uuid which we could use, but I wanted
// truly globally unique UUIDs for profiles created through the settings UI.
GUID guid{};
LOG_IF_FAILED(CoCreateGuid(&guid));
auto profile = CreateChild(_baseLayerProfile);
profile->_FinalizeInheritance();
profile->Guid(guid);
profile->Name(winrt::hstring{ name });
return profile;
}
// Method Description:
// - Attempts to validate this settings structure. If there are critical errors
// found, they'll be thrown as a SettingsLoadError. Non-critical errors, such
// as not finding the default profile, will only result in an error. We'll add
// all these warnings to our list of warnings, and the application can chose
// to display these to the user.
// Arguments:
// - <none>
// Return Value:
// - <none>
void CascadiaSettings::_validateSettings()
{
_validateAllSchemesExist();
_validateMediaResources();
_validateKeybindings();
_validateColorSchemesInCommands();
_validateThemeExists();
_validateProfileEnvironmentVariables();
_validateRegexes();
}
// Method Description:
// - Ensures that every profile has a valid "color scheme" set. If any profile
// has a colorScheme set to a value which is _not_ the name of an actual color
// scheme, we'll set the color table of the profile to something reasonable.
// Arguments:
// - <none>
// Return Value:
// - <none>
// - Appends a SettingsLoadWarnings::UnknownColorScheme to our list of warnings if
// we find any such duplicate.
void CascadiaSettings::_validateAllSchemesExist()
{
const auto colorSchemes = _globals->ColorSchemes();
auto foundInvalidDarkScheme = false;
auto foundInvalidLightScheme = false;
for (const auto& profile : _allProfiles)
{
for (const auto& appearance : std::array{ profile.DefaultAppearance(), profile.UnfocusedAppearance() })
{
if (appearance && !colorSchemes.HasKey(appearance.DarkColorSchemeName()))
{
// Clear the user set dark color scheme. We'll just fallback instead.
appearance.ClearDarkColorSchemeName();
foundInvalidDarkScheme = true;
}
if (appearance && !colorSchemes.HasKey(appearance.LightColorSchemeName()))
{
// Clear the user set light color scheme. We'll just fallback instead.
appearance.ClearLightColorSchemeName();
foundInvalidLightScheme = true;
}
}
}
if (foundInvalidDarkScheme || foundInvalidLightScheme)
{
_warnings.Append(SettingsLoadWarnings::UnknownColorScheme);
}
}
// Method Description:
// - Ensures that all specified images resources (icons and background images) are valid URIs.
// This does not verify that the icon or background image files are encoded as an image.
// Arguments:
// - <none>
// Return Value:
// - <none>
// - Appends a SettingsLoadWarnings::InvalidBackgroundImage to our list of warnings if
// we find any invalid background images.
// - Appends a SettingsLoadWarnings::InvalidIconImage to our list of warnings if
// we find any invalid icon images.
void CascadiaSettings::_validateMediaResources()
{
auto invalidBackground{ false };
auto invalidIcon{ false };
for (auto profile : _allProfiles)
{
if (const auto path = profile.DefaultAppearance().ExpandedBackgroundImagePath(); !path.empty())
{
// Attempt to convert the path to a URI, the ctor will throw if it's invalid/unparseable.
// This covers file paths on the machine, app data, URLs, and other resource paths.
try
{
winrt::Windows::Foundation::Uri imagePath{ path };
}
catch (...)
{
// reset background image path
profile.DefaultAppearance().ClearBackgroundImagePath();
invalidBackground = true;
}
}
if (profile.UnfocusedAppearance())
{
if (const auto path = profile.UnfocusedAppearance().ExpandedBackgroundImagePath(); !path.empty())
{
// Attempt to convert the path to a URI, the ctor will throw if it's invalid/unparseable.
// This covers file paths on the machine, app data, URLs, and other resource paths.
try
{
winrt::Windows::Foundation::Uri imagePath{ path };
}
catch (...)
{
// reset background image path
profile.UnfocusedAppearance().ClearBackgroundImagePath();
invalidBackground = true;
}
}
}
// Anything longer than 2 wchar_t's _isn't_ an emoji or symbol, so treat
// it as an invalid path.
//
// Explicitly just use the Icon here, not the EvaluatedIcon. We don't
// want to blow up if we fell back to the commandline and the
// commandline _isn't an icon_.
// GH #17943: "none" is a special value interpreted as "remove the icon"
static constexpr std::wstring_view HideIconValue{ L"none" };
if (const auto icon = profile.Icon(); icon.size() > 2 && icon != HideIconValue)
{
const auto iconPath{ wil::ExpandEnvironmentStringsW<std::wstring>(icon.c_str()) };
try
{
winrt::Windows::Foundation::Uri imagePath{ iconPath };
}
catch (...)
{
profile.ClearIcon();
invalidIcon = true;
}
}
}
if (invalidBackground)
{
_warnings.Append(SettingsLoadWarnings::InvalidBackgroundImage);
}
if (invalidIcon)
{
_warnings.Append(SettingsLoadWarnings::InvalidIcon);
}
}
// Method Description:
// - Checks if the profiles contain multiple environment variables with the same name, but different
// cases
void CascadiaSettings::_validateProfileEnvironmentVariables()
{
for (const auto& profile : _allProfiles)
{
std::set<std::wstring, til::env_key_sorter> envVarNames{};
if (profile.EnvironmentVariables() == nullptr)
{
continue;
}
for (const auto [key, value] : profile.EnvironmentVariables())
{
const auto iterator = envVarNames.insert(key.c_str());
if (!iterator.second)
{
_warnings.Append(SettingsLoadWarnings::InvalidProfileEnvironmentVariables);
return;
}
}
}
}
static void _validateRegex(const winrt::hstring& regex, IVector<Model::SettingsLoadWarnings>& warnings)
{
try
{
std::wregex{ regex.cbegin(), regex.cend() };
}
catch (std::regex_error)
{
warnings.Append(Model::SettingsLoadWarnings::InvalidRegex);
}
}
static void _validateNTMEntries(const IVector<Model::NewTabMenuEntry>& entries, IVector<Model::SettingsLoadWarnings>& warnings)
{
for (const auto& ntmEntry : entries)
{
if (const auto& folderEntry = ntmEntry.try_as<Model::FolderEntry>())
{
_validateNTMEntries(folderEntry.RawEntries(), warnings);
}
if (const auto& matchProfilesEntry = ntmEntry.try_as<Model::MatchProfilesEntry>())
{
if (const auto nameRegex = matchProfilesEntry.Name(); !nameRegex.empty())
{
_validateRegex(nameRegex, warnings);
}
if (const auto commandlineRegex = matchProfilesEntry.Commandline(); !commandlineRegex.empty())
{
_validateRegex(commandlineRegex, warnings);
}
if (const auto sourceRegex = matchProfilesEntry.Source(); !sourceRegex.empty())
{
_validateRegex(sourceRegex, warnings);
}
}
}
}
void CascadiaSettings::_validateRegexes()
{
_validateNTMEntries(_globals->NewTabMenu(), _warnings);
}
// Method Description:
// - Helper to get the GUID of a profile, given an optional index and a possible
// "profile" value to override that.
// - First, we'll try looking up the profile for the given index. This will
// either get us the GUID of the Nth profile, or the GUID of the default
// profile.
// - Then, if there was a Profile set in the NewTerminalArgs, we'll use that to
// try and look the profile up by either GUID or name.
// Arguments:
// - index: if provided, the index in the list of profiles to get the GUID for.
// If omitted, instead use the default profile's GUID
// - newTerminalArgs: An object that may contain a profile name or GUID to
// actually use. If the Profile value is not a guid, we'll treat it as a name,
// and attempt to look the profile up by name instead.
// Return Value:
// - the GUID of the profile corresponding to this combination of index and NewTerminalArgs
Model::Profile CascadiaSettings::GetProfileForArgs(const Model::NewTerminalArgs& newTerminalArgs) const
{
if (newTerminalArgs)
{
if (const auto name = newTerminalArgs.Profile(); !name.empty())
{
if (auto profile = GetProfileByName(name))
{
return profile;
}
}
if (const auto index = newTerminalArgs.ProfileIndex())
{
if (auto profile = GetProfileByIndex(gsl::narrow<uint32_t>(index.Value())))
{
return profile;
}
else
{
// GH#11114 - Return NOTHING if they asked for a profile index
// outside the range of available profiles.
// Really, the caller should check this beforehand
return nullptr;
}
}
if (const auto commandLine = newTerminalArgs.Commandline(); !commandLine.empty())
{
if (auto profile = _getProfileForCommandLine(commandLine))
{
return profile;
}
}
}
// If the user has access to the "Defaults" profile, and no profile was otherwise specified,
// what we do is dependent on whether there was a commandline.
// If there was a commandline (case 1), we we'll launch in the "Defaults" profile.
// If there wasn't a commandline or there wasn't a NewTerminalArgs (case 2), we'll
// launch in the user's actual default profile.
// Case 2 above could be the result of a "nt" or "sp" invocation that doesn't specify anything.
// TODO GH#10952: Detect the profile based on the commandline (add matching support)
return (!newTerminalArgs || newTerminalArgs.Commandline().empty()) ?
FindProfile(GlobalSettings().DefaultProfile()) :
ProfileDefaults();
}
// The method does some crude command line matching for our console hand-off support.
// If you have hand-off enabled and start PowerShell from the start menu we might be called with
// "C:\Program Files\PowerShell\7\pwsh.exe -WorkingDirectory ~"
// This function then checks all known user profiles for one that's compatible with the commandLine.
// In this case we might have a profile with the command line
// "C:\Program Files\PowerShell\7\pwsh.exe"
// This function will then match this profile return it.
//
// If no matching profile could be found a nullptr will be returned.
Model::Profile CascadiaSettings::_getProfileForCommandLine(const winrt::hstring& commandLine) const
{
// We're going to cache all the command lines we got, as
// NormalizeCommandLine is a relatively heavy operation.
std::call_once(_commandLinesCacheOnce, [this]() {
_commandLinesCache.reserve(_allProfiles.Size());
for (const auto& profile : _allProfiles)
{
if (profile.ConnectionType() != winrt::guid{})
{
continue;
}
const auto cmd = profile.Commandline();
if (cmd.empty())
{
continue;
}
try
{
_commandLinesCache.emplace_back(Profile::NormalizeCommandLine(cmd.c_str()), profile);
}
CATCH_LOG()
}
// We're trying to find the command line with the longest common prefix below.
// Given the commandLine "foo.exe -bar -baz" and these two user profiles:
// * "foo.exe"
// * "foo.exe -bar"
// we want to choose the second one. By sorting the _commandLinesCache in a descending order
// by command line length, we can return from this function the moment we found a matching
// profile as there cannot possibly be any other profile anymore with a longer command line.
std::stable_sort(_commandLinesCache.begin(), _commandLinesCache.end(), [](const auto& lhs, const auto& rhs) {
return lhs.first.size() > rhs.first.size();
});
});
try
{
const auto needle = Profile::NormalizeCommandLine(commandLine.c_str());
// til::starts_with(string, prefix) will always return false if prefix.size() > string.size().
// --> Using binary search we can safely skip all items in _commandLinesCache where .first.size() > needle.size().
const auto end = _commandLinesCache.end();
auto it = std::lower_bound(_commandLinesCache.begin(), end, needle, [&](const auto& lhs, const auto& rhs) {
return lhs.first.size() > rhs.size();
});
// `it` is now at a position where it->first.size() <= needle.size().
// Hopefully we'll now find a command line with matching prefix.
for (; it != end; ++it)
{
const auto& prefix = it->first;
const auto length = gsl::narrow<int>(prefix.size());
if (CompareStringOrdinal(needle.data(), length, prefix.data(), length, TRUE) == CSTR_EQUAL)
{
return it->second;
}
}
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
}
return nullptr;
}
// Method Description:
// - Helper to get a profile given a name that could be a guid or an actual name.
// Arguments:
// - name: a guid string _or_ the name of a profile
// Return Value:
// - the GUID of the profile corresponding to this name
Model::Profile CascadiaSettings::GetProfileByName(const winrt::hstring& name) const
{
// First, try and parse the "name" as a GUID. If it's a
// GUID, and the GUID of one of our profiles, then use that as the
// profile GUID instead. If it's not, then try looking it up as a
// name of a profile. If it's still not that, then just ignore it.
if (!name.empty())
{
// Do a quick heuristic check - is the profile 38 chars long (the
// length of a GUID string), and does it start with '{'? Because if
// it doesn't, it's _definitely_ not a GUID.
if (name.size() == 38 && name[0] == L'{')
{
const auto newGUID{ Utils::GuidFromString(name.c_str()) };
if (auto profile = FindProfile(newGUID))
{
return profile;
}
}
// Here, we were unable to use the profile string as a GUID to
// lookup a profile. Instead, try using the string to look the
// Profile up by name.
for (auto profile : _allProfiles)
{
if (profile.Name() == name)
{
return profile;
}
}
}
return nullptr;
}
// Method Description:
// - Helper to get the profile at the given index in the list of profiles.
// - Returns a nullptr if the index is out of bounds.
// Arguments:
// - index: The profile index in ActiveProfiles()
// Return Value:
// - the Nth profile
Model::Profile CascadiaSettings::GetProfileByIndex(uint32_t index) const
{
return index < _activeProfiles.Size() ? _activeProfiles.GetAt(index) : nullptr;
}
// Method Description:
// - If there were any warnings we generated while parsing the user's
// keybindings, add them to the list of warnings here. If there were warnings
// generated in this way, we'll add a AtLeastOneKeybindingWarning, which will
// act as a header for the other warnings
// - GH#3522
// With variable args to keybindings, it's possible that a user
// set a keybinding without all the required args for an action.
// Display a warning if an action didn't have a required arg.
// This will also catch other keybinding warnings, like from GH#4239.
// Arguments:
// - <none>
// Return Value:
// - <none>
void CascadiaSettings::_validateKeybindings() const
{
const auto keybindingWarnings = _globals->KeybindingsWarnings();
if (!keybindingWarnings.empty())
{
_warnings.Append(SettingsLoadWarnings::AtLeastOneKeybindingWarning);
for (auto warning : keybindingWarnings)
{
_warnings.Append(warning);
}
}
}
// Method Description:
// - Ensures that every "setColorScheme" command has a valid "color scheme" set.
// Arguments:
// - <none>
// Return Value:
// - <none>
// - Appends a SettingsLoadWarnings::InvalidColorSchemeInCmd to our list of warnings if
// we find any command with an invalid color scheme.
void CascadiaSettings::_validateColorSchemesInCommands() const
{
auto foundInvalidScheme{ false };
for (const auto& nameAndCmd : _globals->ActionMap().NameMap())
{
if (_hasInvalidColorScheme(nameAndCmd.Value()))
{
foundInvalidScheme = true;
break;
}
}
if (foundInvalidScheme)
{
_warnings.Append(SettingsLoadWarnings::InvalidColorSchemeInCmd);
}
}
bool CascadiaSettings::_hasInvalidColorScheme(const Model::Command& command) const
{
auto invalid{ false };
if (command.HasNestedCommands())
{
for (const auto& nested : command.NestedCommands())
{
if (_hasInvalidColorScheme(nested.Value()))
{
invalid = true;
break;
}
}
}
else if (const auto& actionAndArgs = command.ActionAndArgs())
{
if (const auto& realArgs = actionAndArgs.Args().try_as<Model::SetColorSchemeArgs>())
{
const auto cmdImpl{ winrt::get_self<Command>(command) };
// no need to validate iterable commands on color schemes
// they will be expanded to commands with a valid scheme name
if (cmdImpl->IterateOn() != ExpandCommandType::ColorSchemes &&
!_globals->ColorSchemes().HasKey(realArgs.SchemeName()))
{
invalid = true;
}
}
}
return invalid;
}
// Method Description:
// - updates all references to that color scheme with the new name
// Arguments:
// - oldName: the original name for the color scheme
// - newName: the new name for the color scheme
// Return Value:
// - <none>
void CascadiaSettings::UpdateColorSchemeReferences(const winrt::hstring& oldName, const winrt::hstring& newName)
{
// update profiles.defaults, if necessary
if (_baseLayerProfile &&
_baseLayerProfile->DefaultAppearance().HasDarkColorSchemeName() &&
_baseLayerProfile->DefaultAppearance().DarkColorSchemeName() == oldName)
{
_baseLayerProfile->DefaultAppearance().DarkColorSchemeName(newName);
}
// NOT else-if, because both could match
if (_baseLayerProfile &&
_baseLayerProfile->DefaultAppearance().HasLightColorSchemeName() &&
_baseLayerProfile->DefaultAppearance().LightColorSchemeName() == oldName)
{
_baseLayerProfile->DefaultAppearance().LightColorSchemeName(newName);
}
// update all profiles referencing this color scheme
for (const auto& profile : _allProfiles)
{
const auto defaultAppearance = profile.DefaultAppearance();
if (defaultAppearance.HasLightColorSchemeName() && defaultAppearance.LightColorSchemeName() == oldName)
{
defaultAppearance.LightColorSchemeName(newName);
}
if (defaultAppearance.HasDarkColorSchemeName() && defaultAppearance.DarkColorSchemeName() == oldName)
{
defaultAppearance.DarkColorSchemeName(newName);
}
if (auto unfocused{ profile.UnfocusedAppearance() })
{
if (unfocused.HasLightColorSchemeName() && unfocused.LightColorSchemeName() == oldName)
{
unfocused.LightColorSchemeName(newName);
}
if (unfocused.HasDarkColorSchemeName() && unfocused.DarkColorSchemeName() == oldName)
{
unfocused.DarkColorSchemeName(newName);
}
}
}
}
winrt::hstring CascadiaSettings::ApplicationDisplayName()
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
return package.DisplayName();
}
CATCH_LOG();
return IsPortableMode() ? RS_(L"ApplicationDisplayNamePortable") : RS_(L"ApplicationDisplayNameUnpackaged");
}
winrt::hstring CascadiaSettings::ApplicationVersion()
{
try
{
const auto package{ winrt::Windows::ApplicationModel::Package::Current() };
const auto version{ package.Id().Version() };
winrt::hstring formatted{ wil::str_printf<std::wstring>(L"%u.%u.%u.%u", version.Major, version.Minor, version.Build, version.Revision) };
return formatted;
}
CATCH_LOG();
// Get the product version the old-fashioned way from the localized version compartment.
//
// We explicitly aren't using VS_FIXEDFILEINFO here, because our build pipeline puts
// a non-standard version number into the localized version field.
// For instance the fixed file info might contain "1.12.2109.13002",
// while the localized field might contain "1.11.210830001-release1.11".
try
{
struct LocalizationInfo
{
WORD language, codepage;
};
// Use the current module instance handle for TerminalApp.dll, nullptr for WindowsTerminal.exe
auto filename{ wil::GetModuleFileNameW<std::wstring>(wil::GetModuleInstanceHandle()) };
auto size{ GetFileVersionInfoSizeExW(0, filename.c_str(), nullptr) };