Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add restore recently closed panes #11471

Merged
4 commits merged into from
Jan 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@
"toggleShaderEffects",
"wt",
"quit",
"restoreLastClosed",
"unbound"
],
"type": "string"
Expand Down
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,22 @@ namespace winrt::TerminalApp::implementation
args.Handled(true);
}

void TerminalPage::_HandleRestoreLastClosed(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (_previouslyClosedPanesAndTabs.size() > 0)
{
const auto restoreActions = _previouslyClosedPanesAndTabs.back();
for (const auto& action : restoreActions)
{
_actionDispatch->DoAction(action);
}
_previouslyClosedPanesAndTabs.pop_back();

args.Handled(true);
}
}

void TerminalPage::_HandleCloseWindow(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
Expand Down
16 changes: 16 additions & 0 deletions src/cascadia/TerminalApp/SettingsTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@ namespace winrt::TerminalApp::implementation
settingsUI.UpdateSettings(settings);
}

// Method Description:
// - Creates a list of actions that can be run to recreate the state of this tab
// Arguments:
// - <none>
// Return Value:
// - The list of actions.
std::vector<ActionAndArgs> SettingsTab::BuildStartupActions() const
{
ActionAndArgs action;
action.Action(ShortcutAction::OpenSettings);
OpenSettingsArgs args{ SettingsTarget::SettingsUI };
action.Args(args);

return std::vector{ std::move(action) };
}

// Method Description:
// - Focus the settings UI
// Arguments:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/SettingsTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace winrt::TerminalApp::implementation
void UpdateSettings(Microsoft::Terminal::Settings::Model::CascadiaSettings settings);
void Focus(winrt::Windows::UI::Xaml::FocusState focusState) override;

std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs> BuildStartupActions() const override;

private:
void _MakeTabViewItem() override;
winrt::fire_and_forget _CreateIcon();
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalApp/TabBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace winrt::TerminalApp::implementation

void UpdateTabViewIndex(const uint32_t idx, const uint32_t numTabs);
void SetActionMap(const Microsoft::Terminal::Settings::Model::IActionMapView& actionMap);
virtual std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs> BuildStartupActions() const = 0;

WINRT_CALLBACK(RequestFocusActiveControl, winrt::delegate<void()>);

Expand Down
42 changes: 42 additions & 0 deletions src/cascadia/TerminalApp/TabManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,26 @@ namespace winrt::TerminalApp::implementation
CATCH_LOG();
}

// Method Description:
// - Record the configuration information of the last closed thing .
// - Will occasionally prune the list so it doesn't grow infinitely.
// Arguments:
// - args: the list of actions to take to remake the pane/tab
void TerminalPage::_AddPreviouslyClosedPaneOrTab(std::vector<ActionAndArgs>&& args)
{
// Just make sure we don't get infinitely large, but still
// maintain a large replay buffer.
if (const auto size = _previouslyClosedPanesAndTabs.size(); size > 150)
{
const auto it = _previouslyClosedPanesAndTabs.begin();
// delete 50 at a time so that we don't have to do an erase
// of the buffer every time when at capacity.
_previouslyClosedPanesAndTabs.erase(it, it + (size - 100));
}

Comment on lines +404 to +411
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity... when this has like 149 actions in it and you restore it... does the Terminal UI wildly flicker as it plays all the actions back until it settles? I'm concerned that either someone will interact with it in some way while it's playing back or that it will look horrendous on a slower system as it recreates a bunch of stuff.

Copy link
Contributor Author

@Rosefield Rosefield Jan 10, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To disambiguate, _previouslyClosedPanesAndTabs itself contains vectors of actions, and when you restore the last closed thing it will pop one vector and run all of those. The size limit, whatever it ends up being, shouldn't meaningfully affect the performance of the UI. Technically this erase call will end up calling a memcpy and some destructors, but that should be fast.

To answer your intended question, in the video above I show what it looks like to restore a tab with 7 panes in it. It does take a second for it to resolve everything since it is spawning new processes/terminal controls for each one. Of course that is in a debug build so it might be faster in a release build.

I did another test just quickly (still a debug build, with debugger attached), restoring a tab that had 20 powershell panes in it took about ~6 seconds and the UI was unresponsive during that time. That corresponds to ~40 actions being executed: 20 split panes and some move focus to put the panes in the right spots.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, down the line, we should display a message like "setting up your workspace..." or something like that rather than being unresponsive. That'd be some nice polish instead of sitting with an unresponsive UI haha.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ok. I'm fine with taking it for the sake of getting the useful feature in, but Carlos is right that we should probably add some small polish to it in the future, if someone complains or we feel like it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the case of restoring a single pane, it won't be any slower than if the user did a split pane manually. It is only on the case where someone restores a tab with a bunch of panes on it that it takes some time. There is the benefit of the user (roughly) knowing what they are recreating, so they shouldn't be too surprised if it takes a second.

That being said, this is a fairly naive implementation so there is probably be room for improvement,

_previouslyClosedPanesAndTabs.emplace_back(args);
}

// Method Description:
// - Removes the tab (both TerminalControl and XAML) after prompting for approval
// Arguments:
Expand All @@ -408,6 +428,11 @@ namespace winrt::TerminalApp::implementation
co_return;
}
}

auto t = winrt::get_self<implementation::TabBase>(tab);
auto actions = t->BuildStartupActions();
_AddPreviouslyClosedPaneOrTab(std::move(actions));

_RemoveTab(tab);
}

Expand Down Expand Up @@ -710,6 +735,23 @@ namespace winrt::TerminalApp::implementation
});
}

// Build the list of actions to recreate the closed pane,
zadjii-msft marked this conversation as resolved.
Show resolved Hide resolved
// BuildStartupActions returns the "first" pane and the rest of
// its actions are assuming that first pane has been created first.
// This doesn't handle refocusing anything in particular, the
// result will be that the last pane created is focused. In the
// case of a single pane that is the desired behavior anyways.
auto state = pane->BuildStartupActions(0, 1);
{
ActionAndArgs splitPaneAction{};
splitPaneAction.Action(ShortcutAction::SplitPane);
SplitPaneArgs splitPaneArgs{ SplitDirection::Automatic, state.firstPane->GetTerminalArgsForPane() };
splitPaneAction.Args(splitPaneArgs);

state.args.emplace(state.args.begin(), std::move(splitPaneAction));
}
_AddPreviouslyClosedPaneOrTab(std::move(state.args));

pane->Close();
}
}
Expand Down
17 changes: 3 additions & 14 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1442,20 +1442,9 @@ namespace winrt::TerminalApp::implementation

for (auto tab : _tabs)
{
if (auto terminalTab = _GetTerminalTabImpl(tab))
{
auto tabActions = terminalTab->BuildStartupActions();
actions.insert(actions.end(), std::make_move_iterator(tabActions.begin()), std::make_move_iterator(tabActions.end()));
}
else if (tab.try_as<SettingsTab>())
{
ActionAndArgs action;
action.Action(ShortcutAction::OpenSettings);
OpenSettingsArgs args{ SettingsTarget::SettingsUI };
action.Args(args);

actions.emplace_back(std::move(action));
}
auto t = winrt::get_self<implementation::TabBase>(tab);
auto tabActions = t->BuildStartupActions();
actions.insert(actions.end(), std::make_move_iterator(tabActions.begin()), std::make_move_iterator(tabActions.end()));
}

// if the focused tab was not the last tab, restore that
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ namespace winrt::TerminalApp::implementation
std::optional<int> _rearrangeTo{};
bool _removing{ false };

std::vector<std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs>> _previouslyClosedPanesAndTabs{};

uint32_t _systemRowsToScroll{ DefaultRowsToScroll };

// use a weak reference to prevent circular dependency with AppLogic
Expand Down Expand Up @@ -300,6 +302,7 @@ namespace winrt::TerminalApp::implementation

winrt::fire_and_forget _SetFocusedTab(const winrt::TerminalApp::TabBase tab);
winrt::fire_and_forget _CloseFocusedPane();
void _AddPreviouslyClosedPaneOrTab(std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs>&& args);

winrt::fire_and_forget _RemoveOnCloseRoutine(Microsoft::UI::Xaml::Controls::TabViewItem tabViewItem, winrt::com_ptr<TerminalPage> page);

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace winrt::TerminalApp::implementation
void EnterZoom();
void ExitZoom();

std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs> BuildStartupActions() const;
std::vector<Microsoft::Terminal::Settings::Model::ActionAndArgs> BuildStartupActions() const override;

int GetLeafPaneCount() const noexcept;

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ static constexpr std::string_view OpenSystemMenuKey{ "openSystemMenu" };
static constexpr std::string_view ClearBufferKey{ "clearBuffer" };
static constexpr std::string_view MultipleActionsKey{ "multipleActions" };
static constexpr std::string_view QuitKey{ "quit" };
static constexpr std::string_view RestoreLastClosedKey{ "restoreLastClosed" };

static constexpr std::string_view ActionKey{ "action" };

Expand Down Expand Up @@ -374,6 +375,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ClearBuffer, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::MultipleActions, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::Quit, RS_(L"QuitCommandKey") },
{ ShortcutAction::RestoreLastClosed, RS_(L"RestoreLastClosedCommandKey") },
};
}();

Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@
ON_ALL_ACTIONS(OpenSystemMenu) \
ON_ALL_ACTIONS(ClearBuffer) \
ON_ALL_ACTIONS(MultipleActions) \
ON_ALL_ACTIONS(Quit)
ON_ALL_ACTIONS(Quit) \
ON_ALL_ACTIONS(RestoreLastClosed)

#define ALL_SHORTCUT_ACTIONS_WITH_ARGS \
ON_ALL_ACTIONS_WITH_ARGS(AdjustFontSize) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,4 +478,7 @@
<data name="QuitCommandKey" xml:space="preserve">
<value>Quit the Terminal</value>
</data>
<data name="RestoreLastClosedCommandKey" xml:space="preserve">
<value>Restore the last closed pane or tab</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@
{ "command": "quakeMode", "keys":"win+sc(41)" },
{ "command": "openSystemMenu", "keys": "alt+space" },
{ "command": "quit" },
{ "command": "restoreLastClosed"},

// Tab Management
// "command": "closeTab" is unbound by default.
Expand Down