Skip to content

Commit d11290c

Browse files
committed
Lazy load CommandPalette and AboutDialog
1 parent f5a7a4d commit d11290c

File tree

7 files changed

+78
-62
lines changed

7 files changed

+78
-62
lines changed

src/cascadia/LocalTests_TerminalApp/TabTests.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1102,7 +1102,7 @@ namespace TerminalAppLocalTests
11021102
// If you don't do this, the palette will just stay open, and the
11031103
// next time we call _HandleNextTab, we'll continue traversing the
11041104
// MRU list, instead of just hoping one entry.
1105-
page->CommandPalette().Visibility(Visibility::Collapsed);
1105+
page->LoadCommandPalette().Visibility(Visibility::Collapsed);
11061106
});
11071107

11081108
TestOnUIThread([&page]() {
@@ -1123,7 +1123,7 @@ namespace TerminalAppLocalTests
11231123
// If you don't do this, the palette will just stay open, and the
11241124
// next time we call _HandleNextTab, we'll continue traversing the
11251125
// MRU list, instead of just hoping one entry.
1126-
page->CommandPalette().Visibility(Visibility::Collapsed);
1126+
page->LoadCommandPalette().Visibility(Visibility::Collapsed);
11271127
});
11281128

11291129
TestOnUIThread([&page]() {
@@ -1239,7 +1239,7 @@ namespace TerminalAppLocalTests
12391239
VERIFY_ARE_EQUAL(L"a", page->_mruTabs.GetAt(3).Title());
12401240
});
12411241

1242-
const auto palette = winrt::get_self<winrt::TerminalApp::implementation::CommandPalette>(page->CommandPalette());
1242+
const auto palette = winrt::get_self<winrt::TerminalApp::implementation::CommandPalette>(page->LoadCommandPalette());
12431243

12441244
VERIFY_ARE_EQUAL(winrt::TerminalApp::implementation::CommandPaletteMode::TabSwitchMode, palette->_currentMode, L"Verify we are in the tab switcher mode");
12451245
// At this point, the contents of the command palette's _mruTabs list is

src/cascadia/TerminalApp/AboutDialog.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ namespace winrt::TerminalApp::implementation
2929
AboutDialog::AboutDialog()
3030
{
3131
InitializeComponent();
32+
QueueUpdateCheck();
3233
}
3334

3435
winrt::hstring AboutDialog::ApplicationDisplayName()

src/cascadia/TerminalApp/AppActionHandlers.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,10 @@ namespace winrt::TerminalApp::implementation
613613
{
614614
if (const auto& realArgs = args.ActionArgs().try_as<ToggleCommandPaletteArgs>())
615615
{
616-
CommandPalette().EnableCommandPaletteMode(realArgs.LaunchMode());
617-
CommandPalette().Visibility(CommandPalette().Visibility() == Visibility::Visible ?
618-
Visibility::Collapsed :
619-
Visibility::Visible);
616+
const auto p = LoadCommandPalette();
617+
const auto v = p.Visibility() == Visibility::Visible ? Visibility::Collapsed : Visibility::Visible;
618+
p.EnableCommandPaletteMode(realArgs.LaunchMode());
619+
p.Visibility(v);
620620
args.Handled(true);
621621
}
622622
}
@@ -799,9 +799,10 @@ namespace winrt::TerminalApp::implementation
799799
void TerminalPage::_HandleTabSearch(const IInspectable& /*sender*/,
800800
const ActionEventArgs& args)
801801
{
802-
CommandPalette().SetTabs(_tabs, _mruTabs);
803-
CommandPalette().EnableTabSearchMode();
804-
CommandPalette().Visibility(Visibility::Visible);
802+
const auto p = LoadCommandPalette();
803+
p.SetTabs(_tabs, _mruTabs);
804+
p.EnableTabSearchMode();
805+
p.Visibility(Visibility::Visible);
805806

806807
args.Handled(true);
807808
}

src/cascadia/TerminalApp/TabManagement.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -600,13 +600,14 @@ namespace winrt::TerminalApp::implementation
600600
}
601601
else
602602
{
603-
CommandPalette().SetTabs(_tabs, _mruTabs);
603+
const auto p = LoadCommandPalette();
604+
p.SetTabs(_tabs, _mruTabs);
604605

605606
// Otherwise, set up the tab switcher in the selected mode, with
606607
// the given ordering, and make it visible.
607-
CommandPalette().EnableTabSwitcherMode(index, tabSwitchMode);
608-
CommandPalette().Visibility(Visibility::Visible);
609-
CommandPalette().SelectNextItem(bMoveRight);
608+
p.EnableTabSwitcherMode(index, tabSwitchMode);
609+
p.Visibility(Visibility::Visible);
610+
p.SelectNextItem(bMoveRight);
610611
}
611612
}
612613

@@ -915,7 +916,11 @@ namespace winrt::TerminalApp::implementation
915916
}
916917
}
917918

918-
CommandPalette().Visibility(Visibility::Collapsed);
919+
if (const auto p = CommandPaletteElement())
920+
{
921+
p.Visibility(Visibility::Collapsed);
922+
}
923+
919924
_UpdateTabView();
920925
}
921926

@@ -944,7 +949,7 @@ namespace winrt::TerminalApp::implementation
944949
void TerminalPage::_UpdatedSelectedTab(const winrt::TerminalApp::TabBase& tab)
945950
{
946951
// Unfocus all the tabs.
947-
for (auto tab : _tabs)
952+
for (const auto& tab : _tabs)
948953
{
949954
tab.Focus(FocusState::Unfocused);
950955
}
@@ -964,7 +969,8 @@ namespace winrt::TerminalApp::implementation
964969
// When the tab switcher is eventually dismissed, the focus will
965970
// get tossed back to the focused terminal control, so we don't
966971
// need to worry about focus getting lost.
967-
if (CommandPalette().Visibility() != Visibility::Visible)
972+
const auto p = CommandPaletteElement();
973+
if (!p || p.Visibility() != Visibility::Visible)
968974
{
969975
tab.Focus(FocusState::Programmatic);
970976
_UpdateMRUTab(tab);

src/cascadia/TerminalApp/TerminalPage.cpp

+47-42
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,14 @@ namespace winrt::TerminalApp::implementation
113113

114114
_settings = settings;
115115

116-
// Make sure to _UpdateCommandsForPalette before
117-
// _RefreshUIForSettingsReload. _UpdateCommandsForPalette will make
118-
// sure the KeyChordText of Commands is updated, which needs to
119-
// happen before the Settings UI is reloaded and tries to re-read
120-
// those values.
121-
_UpdateCommandsForPalette();
122-
CommandPalette().SetActionMap(_settings.ActionMap());
116+
// Make sure to call SetCommands before _RefreshUIForSettingsReload.
117+
// SetCommands will make sure the KeyChordText of Commands is updated, which needs
118+
// to happen before the Settings UI is reloaded and tries to re-read those values.
119+
if (const auto p = CommandPaletteElement())
120+
{
121+
p.SetCommands(_settings.GlobalSettings().ActionMap().ExpandedCommands());
122+
p.SetActionMap(_settings.ActionMap());
123+
}
123124

124125
if (needRefreshUI)
125126
{
@@ -256,20 +257,6 @@ namespace winrt::TerminalApp::implementation
256257

257258
_UpdateTabWidthMode();
258259

259-
// When the visibility of the command palette changes to "collapsed",
260-
// the palette has been closed. Toss focus back to the currently active
261-
// control.
262-
CommandPalette().RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [this](auto&&, auto&&) {
263-
if (CommandPalette().Visibility() == Visibility::Collapsed)
264-
{
265-
_FocusActiveControl(nullptr, nullptr);
266-
}
267-
});
268-
CommandPalette().DispatchCommandRequested({ this, &TerminalPage::_OnDispatchCommandRequested });
269-
CommandPalette().CommandLineExecutionRequested({ this, &TerminalPage::_OnCommandLineExecutionRequested });
270-
CommandPalette().SwitchToTabRequested({ this, &TerminalPage::_OnSwitchToTabRequested });
271-
CommandPalette().PreviewAction({ this, &TerminalPage::_PreviewActionHandler });
272-
273260
// Settings AllowDependentAnimations will affect whether animations are
274261
// enabled application-wide, so we don't need to check it each time we
275262
// want to create an animation.
@@ -685,7 +672,6 @@ namespace winrt::TerminalApp::implementation
685672
// Notes link, send feedback link and privacy policy link.
686673
void TerminalPage::_ShowAboutDialog()
687674
{
688-
AboutDialog().QueueUpdateCheck();
689675
_ShowDialogHelper(L"AboutDialog");
690676
}
691677

@@ -1334,8 +1320,9 @@ namespace winrt::TerminalApp::implementation
13341320
void TerminalPage::_CommandPaletteButtonOnClick(const IInspectable&,
13351321
const RoutedEventArgs&)
13361322
{
1337-
CommandPalette().EnableCommandPaletteMode(CommandPaletteLaunchMode::Action);
1338-
CommandPalette().Visibility(Visibility::Visible);
1323+
auto p = LoadCommandPalette();
1324+
p.EnableCommandPaletteMode(CommandPaletteLaunchMode::Action);
1325+
p.Visibility(Visibility::Visible);
13391326
}
13401327

13411328
// Method Description:
@@ -1351,7 +1338,7 @@ namespace winrt::TerminalApp::implementation
13511338
}
13521339

13531340
// Method Description:
1354-
// - Called when the users pressed keyBindings while CommandPalette is open.
1341+
// - Called when the users pressed keyBindings while CommandPaletteElement is open.
13551342
// - As of GH#8480, this is also bound to the TabRowControl's KeyUp event.
13561343
// That should only fire when focus is in the tab row, which is hard to
13571344
// do. Notably, that's possible:
@@ -1422,7 +1409,7 @@ namespace winrt::TerminalApp::implementation
14221409
return;
14231410
}
14241411

1425-
if (const auto p = CommandPalette(); p.Visibility() == Visibility::Visible && cmd.ActionAndArgs().Action() != ShortcutAction::ToggleCommandPalette)
1412+
if (const auto p = CommandPaletteElement(); p.Visibility() == Visibility::Visible && cmd.ActionAndArgs().Action() != ShortcutAction::ToggleCommandPalette)
14261413
{
14271414
p.Visibility(Visibility::Collapsed);
14281415
}
@@ -1746,6 +1733,40 @@ namespace winrt::TerminalApp::implementation
17461733
}
17471734
return nullptr;
17481735
}
1736+
1737+
CommandPalette TerminalPage::LoadCommandPalette()
1738+
{
1739+
if (const auto p = CommandPaletteElement())
1740+
{
1741+
return p;
1742+
}
1743+
1744+
return _loadCommandPaletteSlowPath();
1745+
}
1746+
1747+
CommandPalette TerminalPage::_loadCommandPaletteSlowPath()
1748+
{
1749+
const auto p = FindName(L"CommandPaletteElement").as<CommandPalette>();
1750+
1751+
p.SetCommands(_settings.GlobalSettings().ActionMap().ExpandedCommands());
1752+
p.SetActionMap(_settings.ActionMap());
1753+
1754+
// When the visibility of the command palette changes to "collapsed",
1755+
// the palette has been closed. Toss focus back to the currently active control.
1756+
p.RegisterPropertyChangedCallback(UIElement::VisibilityProperty(), [this](auto&&, auto&&) {
1757+
if (CommandPaletteElement().Visibility() == Visibility::Collapsed)
1758+
{
1759+
_FocusActiveControl(nullptr, nullptr);
1760+
}
1761+
});
1762+
p.DispatchCommandRequested({ this, &TerminalPage::_OnDispatchCommandRequested });
1763+
p.CommandLineExecutionRequested({ this, &TerminalPage::_OnCommandLineExecutionRequested });
1764+
p.SwitchToTabRequested({ this, &TerminalPage::_OnSwitchToTabRequested });
1765+
p.PreviewAction({ this, &TerminalPage::_PreviewActionHandler });
1766+
1767+
return p;
1768+
}
1769+
17491770
// Method Description:
17501771
// - Warn the user that they are about to close all open windows, then
17511772
// signal that we want to close everything.
@@ -3141,21 +3162,6 @@ namespace winrt::TerminalApp::implementation
31413162
}
31423163
}
31433164

3144-
// Method Description:
3145-
// - Repopulates the list of commands in the command palette with the
3146-
// current commands in the settings. Also updates the keybinding labels to
3147-
// reflect any matching keybindings.
3148-
// Arguments:
3149-
// - <none>
3150-
// Return Value:
3151-
// - <none>
3152-
void TerminalPage::_UpdateCommandsForPalette()
3153-
{
3154-
// Update the command palette when settings reload
3155-
const auto& expanded{ _settings.GlobalSettings().ActionMap().ExpandedCommands() };
3156-
CommandPalette().SetCommands(expanded);
3157-
}
3158-
31593165
// Method Description:
31603166
// - Sets the initial actions to process on startup. We'll make a copy of
31613167
// this list, and process these actions when we're loaded.
@@ -4827,5 +4833,4 @@ namespace winrt::TerminalApp::implementation
48274833
// _RemoveTab will make sure to null out the _stashed.draggedTab
48284834
_RemoveTab(*_stashed.draggedTab);
48294835
}
4830-
48314836
}

src/cascadia/TerminalApp/TerminalPage.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ namespace winrt::TerminalApp::implementation
118118
winrt::hstring ApplicationDisplayName();
119119
winrt::hstring ApplicationVersion();
120120

121+
CommandPalette LoadCommandPalette();
121122
winrt::fire_and_forget RequestQuit();
122123
winrt::fire_and_forget CloseWindow(bool bypassDialog);
123124

@@ -274,6 +275,7 @@ namespace winrt::TerminalApp::implementation
274275

275276
winrt::Microsoft::Terminal::TerminalConnection::ConptyConnection::NewConnection_revoker _newConnectionRevoker;
276277

278+
__declspec(noinline) CommandPalette _loadCommandPaletteSlowPath();
277279
winrt::Windows::Foundation::IAsyncOperation<winrt::Windows::UI::Xaml::Controls::ContentDialogResult> _ShowDialogHelper(const std::wstring_view& name);
278280

279281
void _ShowAboutDialog();
@@ -312,7 +314,6 @@ namespace winrt::TerminalApp::implementation
312314
void _UpdateTabIcon(TerminalTab& tab);
313315
void _UpdateTabView();
314316
void _UpdateTabWidthMode();
315-
void _UpdateCommandsForPalette();
316317
void _SetBackgroundImage(const winrt::Microsoft::Terminal::Settings::Model::IAppearanceConfig& newAppearance);
317318

318319
void _DuplicateFocusedTab();

src/cascadia/TerminalApp/TerminalPage.xaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@
9797
-->
9898

9999
<local:AboutDialog x:Name="AboutDialog"
100-
Grid.Row="2" />
100+
Grid.Row="2"
101+
x:Load="False" />
101102

102103
<ContentDialog x:Name="QuitDialog"
103104
x:Uid="QuitDialog"
@@ -167,9 +168,10 @@
167168
</TextBlock>
168169
</ContentDialog>
169170

170-
<local:CommandPalette x:Name="CommandPalette"
171+
<local:CommandPalette x:Name="CommandPaletteElement"
171172
Grid.Row="2"
172173
VerticalAlignment="Stretch"
174+
x:Load="False"
173175
PreviewKeyDown="_KeyDownHandler"
174176
Visibility="Collapsed" />
175177

0 commit comments

Comments
 (0)