-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
Copy pathAppLogic.cpp
726 lines (646 loc) · 28.2 KB
/
AppLogic.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#include "pch.h"
#include "AppLogic.h"
#include "../inc/WindowingBehavior.h"
#include "AppLogic.g.cpp"
#include "FindTargetWindowResult.g.cpp"
#include "SettingsLoadEventArgs.h"
#include <LibraryResources.h>
#include <WtExeUtils.h>
#include <wil/token_helpers.h>
#include "../../types/inc/utils.hpp"
using namespace winrt::Windows::ApplicationModel;
using namespace winrt::Windows::ApplicationModel::DataTransfer;
using namespace winrt::Windows::UI::Xaml;
using namespace winrt::Windows::UI::Xaml::Controls;
using namespace winrt::Windows::UI::Core;
using namespace winrt::Windows::System;
using namespace winrt::Microsoft::Terminal;
using namespace winrt::Microsoft::Terminal::Control;
using namespace winrt::Microsoft::Terminal::Settings::Model;
using namespace ::TerminalApp;
namespace winrt
{
namespace MUX = Microsoft::UI::Xaml;
using IInspectable = Windows::Foundation::IInspectable;
}
////////////////////////////////////////////////////////////////////////////////
// Error message handling. This is in this file rather than with the warnings in
// TerminalWindow, because the error text might be:
// * A error we defined here
// * An error from deserializing the json
// * Any other fatal error loading the settings
// So all we pass on is the actual text of the error, rather than the
// combination of things that might have caused an error.
// !!! IMPORTANT !!!
// Make sure that these keys are in the same order as the
// SettingsLoadWarnings/Errors enum is!
static const std::array settingsLoadErrorsLabels{
USES_RESOURCE(L"NoProfilesText"),
USES_RESOURCE(L"AllProfilesHiddenText")
};
static_assert(settingsLoadErrorsLabels.size() == static_cast<size_t>(SettingsLoadErrors::ERRORS_SIZE));
// Function Description:
// - General-purpose helper for looking up a localized string for a
// warning/error. First will look for the given key in the provided map of
// keys->strings, where the values in the map are ResourceKeys. If it finds
// one, it will lookup the localized string from that ResourceKey.
// - If it does not find a key, it'll return an empty string
// Arguments:
// - key: the value to use to look for a resource key in the given map
// - map: A map of keys->Resource keys.
// Return Value:
// - the localized string for the given type, if it exists.
template<typename T>
winrt::hstring _GetMessageText(uint32_t index, const T& keys)
{
if (index < keys.size())
{
return GetLibraryResourceString(til::at(keys, index));
}
return {};
}
// Function Description:
// - Gets the text from our ResourceDictionary for the given
// SettingsLoadError. If there is no such text, we'll return nullptr.
// - The warning should have an entry in settingsLoadErrorsLabels.
// Arguments:
// - error: the SettingsLoadErrors value to get the localized text for.
// Return Value:
// - localized text for the given error
static winrt::hstring _GetErrorText(SettingsLoadErrors error)
{
return _GetMessageText(static_cast<uint32_t>(error), settingsLoadErrorsLabels);
}
static constexpr std::wstring_view StartupTaskName = L"StartTerminalOnLoginTask";
namespace winrt::TerminalApp::implementation
{
// Function Description:
// - Get the AppLogic for the current active Xaml application, or null if there isn't one.
// Return value:
// - A pointer (bare) to the AppLogic, or nullptr. The app logic outlives all other objects,
// unless the application is in a terrible way, so this is "safe."
AppLogic* AppLogic::Current() noexcept
try
{
if (auto currentXamlApp{ winrt::Windows::UI::Xaml::Application::Current().try_as<winrt::TerminalApp::App>() })
{
if (auto appLogicPointer{ winrt::get_self<AppLogic>(currentXamlApp.Logic()) })
{
return appLogicPointer;
}
}
return nullptr;
}
catch (...)
{
LOG_CAUGHT_EXCEPTION();
return nullptr;
}
// Method Description:
// - Returns the settings currently in use by the entire Terminal application.
// - IMPORTANT! This can throw! Make sure to try/catch this, so that the
// LocalTests don't crash (because their Application::Current() won't be a
// AppLogic)
// Throws:
// - HR E_INVALIDARG if the app isn't up and running.
const CascadiaSettings AppLogic::CurrentAppSettings()
{
auto appLogic{ ::winrt::TerminalApp::implementation::AppLogic::Current() };
THROW_HR_IF_NULL(E_INVALIDARG, appLogic);
return appLogic->GetSettings();
}
AppLogic::AppLogic()
{
// For your own sanity, it's better to do setup outside the ctor.
// If you do any setup in the ctor that ends up throwing an exception,
// then it might look like App just failed to activate, which will
// cause you to chase down the rabbit hole of "why is App not
// registered?" when it definitely is.
// The TerminalPage has to be constructed during our construction, to
// make sure that there's a terminal page for callers of
// SetTitleBarContent
_isElevated = ::Microsoft::Console::Utils::IsRunningElevated();
_canDragDrop = ::Microsoft::Console::Utils::CanUwpDragDrop();
_reloadSettings = std::make_shared<ThrottledFuncTrailing<>>(winrt::Windows::System::DispatcherQueue::GetForCurrentThread(), std::chrono::milliseconds(100), [weakSelf = get_weak()]() {
if (auto self{ weakSelf.get() })
{
self->ReloadSettings();
}
});
_languageProfileNotifier = winrt::make_self<LanguageProfileNotifier>([this]() {
_reloadSettings->Run();
});
// Do this here, rather than at the top of main. This will prevent us from
// including this variable in the vars we serialize in the
// Remoting::CommandlineArgs up in HandleCommandlineArgs.
_setupFolderPathEnvVar();
}
// Method Description:
// - Called around the codebase to discover if Terminal is running elevated
// Arguments:
// - <none> - reports internal state
// Return Value:
// - True if elevated, false otherwise.
bool AppLogic::IsRunningElevated() const noexcept
{
return _isElevated;
}
bool AppLogic::CanDragDrop() const noexcept
{
return _canDragDrop;
}
// Method Description:
// - Build the UI for the terminal app. Before this method is called, it
// should not be assumed that the TerminalApp is usable. The Settings
// should be loaded before this is called, either with LoadSettings or
// GetLaunchDimensions (which will call LoadSettings)
// Arguments:
// - <none>
// Return Value:
// - <none>
void AppLogic::Create()
{
// Assert that we've already loaded our settings. We have to do
// this as a MTA, before the app is Create()'d
WINRT_ASSERT(_loadedInitialSettings);
_ApplyLanguageSettingChange();
TraceLoggingWrite(
g_hTerminalAppProvider,
"AppCreated",
TraceLoggingDescription("Event emitted when the application is started"),
TraceLoggingBool(_settings.GlobalSettings().ShowTabsInTitlebar(), "TabsInTitlebar"),
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
}
// Method Description:
// - Attempt to load the settings. If we fail for any reason, returns an error.
// Return Value:
// - S_OK if we successfully parsed the settings, otherwise an appropriate HRESULT.
[[nodiscard]] HRESULT AppLogic::_TryLoadSettings() noexcept
{
auto hr = E_FAIL;
try
{
auto newSettings = CascadiaSettings::LoadAll();
if (newSettings.GetLoadingError())
{
_settingsLoadExceptionText = _GetErrorText(newSettings.GetLoadingError().Value());
return E_INVALIDARG;
}
else if (!newSettings.GetSerializationErrorMessage().empty())
{
_settingsLoadExceptionText = newSettings.GetSerializationErrorMessage();
return E_INVALIDARG;
}
_warnings.clear();
for (uint32_t i = 0; i < newSettings.Warnings().Size(); i++)
{
_warnings.push_back(newSettings.Warnings().GetAt(i));
}
_hasSettingsStartupActions = false;
const auto startupActions = newSettings.GlobalSettings().StartupActions();
if (!startupActions.empty())
{
_settingsAppArgs.FullResetState();
ExecuteCommandlineArgs args{ newSettings.GlobalSettings().StartupActions() };
auto result = _settingsAppArgs.ParseArgs(args);
if (result == 0)
{
_hasSettingsStartupActions = true;
// Validation also injects new-tab command if implicit new-tab was provided.
_settingsAppArgs.ValidateStartupCommands();
}
else
{
_warnings.push_back(SettingsLoadWarnings::FailedToParseStartupActions);
}
}
_settings = std::move(newSettings);
hr = _warnings.empty() ? S_OK : S_FALSE;
}
catch (const winrt::hresult_error& e)
{
hr = e.code();
_settingsLoadExceptionText = e.message();
LOG_HR(hr);
}
catch (...)
{
hr = wil::ResultFromCaughtException();
LOG_HR(hr);
}
return hr;
}
bool AppLogic::HasSettingsStartupActions() const noexcept
{
return _hasSettingsStartupActions;
}
// Call this function after loading your _settings.
// It handles any CPU intensive settings updates (like updating the Jumplist)
// which should thus only occur if the settings file actually changed.
void AppLogic::_ProcessLazySettingsChanges()
{
const auto hash = _settings.Hash();
const auto applicationState = ApplicationState::SharedInstance();
const auto cachedHash = applicationState.SettingsHash();
// The hash might be empty if LoadAll() failed and we're dealing with the defaults settings object.
// In that case we can just wait until the user fixed their settings or CascadiaSettings fixed
// itself and either will soon trigger a settings reload.
if (hash.empty() || hash == cachedHash)
{
return;
}
Jumplist::UpdateJumplist(_settings);
applicationState.SettingsHash(hash);
}
// Method Description:
// - Registers for changes to the settings folder and upon a updated settings
// profile calls ReloadSettings().
// Arguments:
// - <none>
// Return Value:
// - <none>
void AppLogic::_RegisterSettingsChange()
{
const std::filesystem::path settingsPath{ std::wstring_view{ CascadiaSettings::SettingsPath() } };
_reader.create(
settingsPath.parent_path().c_str(),
false,
// We want file modifications, AND when files are renamed to be
// settings.json. This second case will oftentimes happen with text
// editors, who will write a temp file, then rename it to be the
// actual file you wrote. So listen for that too.
wil::FolderChangeEvents::FileName | wil::FolderChangeEvents::LastWriteTime,
[this, settingsBasename = settingsPath.filename()](wil::FolderChangeEvent, PCWSTR fileModified) {
// DO NOT create a static reference to ApplicationState::SharedInstance here.
//
// ApplicationState::SharedInstance already caches its own
// static ref. If _we_ keep a static ref to the member in
// AppState, then our reference will keep ApplicationState alive
// after the `ActionToStringMap` gets cleaned up. Then, when we
// try to persist the actions in the window state, we won't be
// able to. We'll try to look up the action and the map just
// won't exist. We'll explode, even though the Terminal is
// tearing down anyways. So we'll just die, but still invoke
// WinDBG's post-mortem debugger, who won't be able to attach to
// the process that's already exiting.
//
// So DON'T ~give a mouse a cookie~ take a static ref here.
const winrt::hstring modifiedBasename{ std::filesystem::path{ fileModified }.filename().c_str() };
if (modifiedBasename == settingsBasename)
{
_reloadSettings->Run();
}
});
}
void AppLogic::_ApplyLanguageSettingChange() noexcept
try
{
if (!IsPackaged())
{
return;
}
using ApplicationLanguages = winrt::Windows::Globalization::ApplicationLanguages;
// NOTE: PrimaryLanguageOverride throws if this instance is unpackaged.
const auto primaryLanguageOverride = ApplicationLanguages::PrimaryLanguageOverride();
const auto language = _settings.GlobalSettings().Language();
if (primaryLanguageOverride != language)
{
ApplicationLanguages::PrimaryLanguageOverride(language);
}
}
CATCH_LOG()
fire_and_forget AppLogic::_ApplyStartupTaskStateChange()
try
{
// First, make sure we're running in a packaged context. This method
// won't work, and will crash mysteriously if we're running unpackaged.
if (!IsPackaged())
{
co_return;
}
const auto tryEnableStartupTask = _settings.GlobalSettings().StartOnUserLogin();
const auto task = co_await StartupTask::GetAsync(StartupTaskName);
switch (task.State())
{
case StartupTaskState::Disabled:
if (tryEnableStartupTask)
{
co_await task.RequestEnableAsync();
}
break;
case StartupTaskState::DisabledByUser:
// TODO: GH#6254: define UX for other StartupTaskStates
break;
case StartupTaskState::Enabled:
if (!tryEnableStartupTask)
{
task.Disable();
}
break;
}
}
CATCH_LOG();
// Method Description:
// - Reloads the settings from the settings.json file.
// - When this is called the first time, this initializes our settings. See
// CascadiaSettings for more details. Additionally hooks up our callbacks
// for keybinding events to the keybindings object.
// - NOTE: when called initially, this must be called from a MTA if we're
// running as a packaged application. The Windows.Storage APIs require a
// MTA. If this isn't happening during startup, it'll need to happen on
// a background thread.
void AppLogic::ReloadSettings()
{
// Attempt to load our settings.
// If it fails,
// - don't change the settings (and don't actually apply the new settings)
// - don't persist them.
// - display a loading error
_settingsLoadedResult = _TryLoadSettings();
const auto initialLoad = !_loadedInitialSettings;
_loadedInitialSettings = true;
if (FAILED(_settingsLoadedResult))
{
if (initialLoad)
{
_settings = CascadiaSettings::LoadDefaults();
}
else
{
auto warnings{ winrt::multi_threaded_vector<SettingsLoadWarnings>() };
for (auto&& warn : _warnings)
{
warnings.Append(warn);
}
auto ev = winrt::make_self<SettingsLoadEventArgs>(true,
static_cast<uint64_t>(_settingsLoadedResult),
_settingsLoadExceptionText,
warnings,
_settings);
SettingsChanged.raise(*this, *ev);
return;
}
}
else
{
_settings.LogSettingChanges(true);
}
if (initialLoad)
{
// Register for directory change notification.
_RegisterSettingsChange();
return;
}
// Here, we successfully reloaded the settings, and created a new
// TerminalSettings object.
_ApplyLanguageSettingChange();
_ApplyStartupTaskStateChange();
_ProcessLazySettingsChanges();
auto warnings{ winrt::multi_threaded_vector<SettingsLoadWarnings>() };
for (auto&& warn : _warnings)
{
warnings.Append(warn);
}
auto ev = winrt::make_self<SettingsLoadEventArgs>(!initialLoad,
_settingsLoadedResult,
_settingsLoadExceptionText,
warnings,
_settings);
SettingsChanged.raise(*this, *ev);
}
// This is a continuation of AppLogic::Create() and includes the more expensive parts.
void AppLogic::NotifyRootInitialized()
{
if (_notifyRootInitializedCalled.exchange(true, std::memory_order_relaxed))
{
return;
}
// Both LoadSettings and ReloadSettings are supposed to call this function,
// but LoadSettings skips it, so that the UI starts up faster.
// Now that the UI is present we can do them with a less significant UX impact.
_ApplyStartupTaskStateChange();
_ProcessLazySettingsChanges();
FILETIME creationTime, exitTime, kernelTime, userTime, now;
if (GetThreadTimes(GetCurrentThread(), &creationTime, &exitTime, &kernelTime, &userTime))
{
static constexpr auto asInteger = [](const FILETIME& f) {
ULARGE_INTEGER i;
i.LowPart = f.dwLowDateTime;
i.HighPart = f.dwHighDateTime;
return i.QuadPart;
};
static constexpr auto asSeconds = [](uint64_t v) {
return v * 1e-7f;
};
GetSystemTimeAsFileTime(&now);
const auto latency = asSeconds(asInteger(now) - asInteger(creationTime));
TraceLoggingWrite(
g_hTerminalAppProvider,
"AppInitialized",
TraceLoggingDescription("Event emitted once the app is initialized"),
TraceLoggingFloat32(latency, "latency"),
TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES),
TelemetryPrivacyDataTag(PDT_ProductAndServiceUsage));
}
}
// Method Description:
// - Returns a pointer to the global shared settings.
[[nodiscard]] CascadiaSettings AppLogic::GetSettings() const noexcept
{
return _settings;
}
// Method Description:
// - Parse the given commandline args in an attempt to find the specified
// window. The rest of the args are ignored for now (they'll be handled
// whenever the commandline gets to the window it was intended for).
// - Note that this function will only ever be called by the monarch. A
// return value of `0` in this case does not mean "run the commandline in
// _this_ process", rather it means "run the commandline in the current
// process", whoever that may be.
// Arguments:
// - args: an array of strings to process as a commandline. These args can contain spaces
// Return Value:
// - 0: We should handle the args "in the current window".
// - WindowingBehaviorUseNew: We should handle the args in a new window
// - WindowingBehaviorUseExisting: We should handle the args "in
// the current window ON THIS DESKTOP"
// - WindowingBehaviorUseAnyExisting: We should handle the args "in the current
// window ON ANY DESKTOP"
// - anything else: We should handle the commandline in the window with the given ID.
TerminalApp::FindTargetWindowResult AppLogic::FindTargetWindow(array_view<const winrt::hstring> args)
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
ReloadSettings();
}
return AppLogic::_doFindTargetWindow(args, _settings.GlobalSettings().WindowingBehavior());
}
// The main body of this function is a static helper, to facilitate unit-testing
TerminalApp::FindTargetWindowResult AppLogic::_doFindTargetWindow(array_view<const winrt::hstring> args,
const Microsoft::Terminal::Settings::Model::WindowingMode& windowingBehavior)
{
::TerminalApp::AppCommandlineArgs appArgs;
const auto result = appArgs.ParseArgs(args);
if (result == 0)
{
if (!appArgs.GetExitMessage().empty())
{
return winrt::make<FindTargetWindowResult>(WindowingBehaviorUseNone);
}
const std::string parsedTarget{ appArgs.GetTargetWindow() };
// If the user did not provide any value on the commandline,
// then lookup our windowing behavior to determine what to do
// now.
if (parsedTarget.empty())
{
auto windowId = WindowingBehaviorUseNew;
switch (windowingBehavior)
{
case WindowingMode::UseNew:
windowId = WindowingBehaviorUseNew;
break;
case WindowingMode::UseExisting:
windowId = WindowingBehaviorUseExisting;
break;
case WindowingMode::UseAnyExisting:
windowId = WindowingBehaviorUseAnyExisting;
break;
}
return winrt::make<FindTargetWindowResult>(windowId);
}
// Here, the user _has_ provided a window-id on the commandline.
// What is it? Let's start by checking if it's an int, for the
// window's ID:
try
{
auto windowId = ::base::saturated_cast<int32_t>(std::stoi(parsedTarget));
// If the user provides _any_ negative number, then treat it as
// -1, for "use a new window".
if (windowId < 0)
{
windowId = -1;
}
// Hooray! This is a valid integer. The set of possible values
// here is {-1, 0, ℤ+}. Let's return that window ID.
return winrt::make<FindTargetWindowResult>(windowId);
}
catch (...)
{
// Value was not a valid int. It could be any other string to
// use as a title though!
//
// First, check the reserved keywords:
if (parsedTarget == NewWindow)
{
return winrt::make<FindTargetWindowResult>(WindowingBehaviorUseNew);
}
else if (parsedTarget == MostRecentlyUsedWindow)
{
return winrt::make<FindTargetWindowResult>(WindowingBehaviorUseExisting);
}
else
{
// The string they provided wasn't an int, it wasn't "new"
// or "last", so whatever it is, that's the name they get.
winrt::hstring winrtName{ til::u8u16(parsedTarget) };
return winrt::make<FindTargetWindowResult>(WindowingBehaviorUseName, winrtName);
}
}
}
// Any unsuccessful parse will result in _no_ window. We will indicate
// to the caller that they shouldn't make a window. They can still find
// the commandline failed to parse and choose to display the message
// box.
//
// This will also work for the case where the user specifies an invalid
// commandline in conjunction with `-w 0`.
return winrt::make<FindTargetWindowResult>(WindowingBehaviorUseNone);
}
Windows::Foundation::Collections::IMapView<Microsoft::Terminal::Control::KeyChord, Microsoft::Terminal::Settings::Model::Command> AppLogic::GlobalHotkeys()
{
return _settings.GlobalSettings().ActionMap().GlobalHotkeys();
}
Microsoft::Terminal::Settings::Model::Theme AppLogic::Theme()
{
return _settings.GlobalSettings().CurrentTheme();
}
bool AppLogic::IsolatedMode()
{
if (!_loadedInitialSettings)
{
ReloadSettings();
}
return _settings.GlobalSettings().IsolatedMode();
}
bool AppLogic::RequestsTrayIcon()
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
ReloadSettings();
}
const auto& globals{ _settings.GlobalSettings() };
return globals.AlwaysShowNotificationIcon() ||
globals.MinimizeToNotificationArea();
}
bool AppLogic::AllowHeadless()
{
if (!_loadedInitialSettings)
{
// Load settings if we haven't already
ReloadSettings();
}
return _settings.GlobalSettings().AllowHeadless();
}
TerminalApp::TerminalWindow AppLogic::CreateNewWindow()
{
if (_settings == nullptr)
{
ReloadSettings();
}
auto warnings{ winrt::multi_threaded_vector<SettingsLoadWarnings>() };
for (auto&& warn : _warnings)
{
warnings.Append(warn);
}
auto ev = winrt::make_self<SettingsLoadEventArgs>(false,
_settingsLoadedResult,
_settingsLoadExceptionText,
warnings,
_settings);
auto window = winrt::make_self<implementation::TerminalWindow>(*ev, _contentManager);
if (_hasSettingsStartupActions)
{
window->SetSettingsStartupArgs(_settingsAppArgs.GetStartupActions());
}
return *window;
}
winrt::TerminalApp::ContentManager AppLogic::ContentManager()
{
return _contentManager;
}
bool AppLogic::ShouldUsePersistedLayout() const
{
return _settings.GlobalSettings().ShouldUsePersistedLayout();
}
TerminalApp::ParseCommandlineResult AppLogic::GetParseCommandlineMessage(array_view<const winrt::hstring> args)
{
::TerminalApp::AppCommandlineArgs _appArgs;
const auto r = _appArgs.ParseArgs(args);
return TerminalApp::ParseCommandlineResult{ winrt::to_hstring(_appArgs.GetExitMessage()), r };
}
// Function Description
// * Adds a `WT_SETTINGS_DIR` env var to our own environment block, that
// points at our settings directory. This allows portable installs to
// refer to files in the portable install using %WT_SETTINGS_DIR%
void AppLogic::_setupFolderPathEnvVar()
{
std::wstring path{ CascadiaSettings::SettingsPath() };
auto folderPath = path.substr(0, path.find_last_of(L"\\"));
SetEnvironmentVariableW(L"WT_SETTINGS_DIR", folderPath.c_str());
}
}