Skip to content

Commit

Permalink
Handle key chord parse failure
Browse files Browse the repository at this point in the history
  • Loading branch information
e82eric committed Jan 2, 2024
1 parent dd46338 commit 8bec5ec
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
18 changes: 16 additions & 2 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,13 +1258,27 @@ namespace winrt::TerminalApp::implementation
winrt::Microsoft::Terminal::Control::KeyChord keyChord = nullptr;
if (!realArgs.KeyChord().empty())
{
keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
try
{
keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
_settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), realArgs.Commandline(), keyChord);
_settings.WriteSettingsToDisk();
ActionSaved(realArgs.Commandline(), realArgs.Name(), KeyChordSerialization::ToString(keyChord));
}
catch (const winrt::hresult_error& ex)
{
auto code = ex.code();
auto message = ex.message();
ActionSaveFailed(message);
args.Handled(true);
return;
}
}

_settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), realArgs.Commandline(), keyChord);

_settings.WriteSettingsToDisk();
ActionSaved(realArgs.Commandline(), realArgs.Name(), KeyChordSerialization::ToString(keyChord));

args.Handled(true);
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -560,16 +560,6 @@ void AppCommandlineArgs::_buildSaveParser()
// as it might clear those options while finding the commandline
SaveTaskArgs args{};

if (!_saveInputName.empty())
{
winrt::hstring hString = winrt::to_hstring(_saveInputName);
args.Name(hString);
}
else
{
args.Name(args.GenerateName());
}

if (!_commandline.empty())
{
std::ostringstream cmdlineBuffer;
Expand Down Expand Up @@ -599,6 +589,16 @@ void AppCommandlineArgs::_buildSaveParser()
{
args.KeyChord(winrt::to_hstring(_keyChordOption));
}

if (!_saveInputName.empty())
{
winrt::hstring hString = winrt::to_hstring(_saveInputName);
args.Name(hString);
}
else
{
args.Name(args.GenerateName());
}

saveAction.Args(args);
_startupActions.push_back(saveAction);
Expand Down
29 changes: 29 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4235,6 +4235,35 @@ namespace winrt::TerminalApp::implementation
}
}

winrt::fire_and_forget TerminalPage::ActionSaveFailed(winrt::hstring message)
{
auto weakThis{ get_weak() };
co_await wil::resume_foreground(Dispatcher());
if (auto page{ weakThis.get() })
{
// If we haven't ever loaded the TeachingTip, then do so now and
// create the toast for it.
if (page->_actionSaveFailedToast == nullptr)
{
if (auto tip{ page->FindName(L"ActionSaveFailedToast").try_as<MUX::Controls::TeachingTip>() })
{
page->_actionSaveFailedToast = std::make_shared<Toast>(tip);
// Make sure to use the weak ref when setting up this
// callback.
tip.Closed({ page->get_weak(), &TerminalPage::_FocusActiveControl });
}
}
_UpdateTeachingTipTheme(ActionSaveFailedToast().try_as<winrt::Windows::UI::Xaml::FrameworkElement>());

ActionSaveFailedMessage().Text(message);

if (page->_actionSaveFailedToast != nullptr)
{
page->_actionSaveFailedToast->Open();
}
}
}

// Method Description:
// - Called when an attempt to rename the window has failed. This will open
// the toast displaying a message to the user that the attempt to rename
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ namespace winrt::TerminalApp::implementation

winrt::fire_and_forget IdentifyWindow();
winrt::fire_and_forget ActionSaved(winrt::hstring input, winrt::hstring name, winrt::hstring keyChord);
winrt::fire_and_forget ActionSaveFailed(winrt::hstring message);
winrt::fire_and_forget RenameFailed();
winrt::fire_and_forget ShowTerminalWorkingDirectory();

Expand Down Expand Up @@ -262,6 +263,7 @@ namespace winrt::TerminalApp::implementation

std::shared_ptr<Toast> _windowIdToast{ nullptr };
std::shared_ptr<Toast> _actionSavedToast{ nullptr };
std::shared_ptr<Toast> _actionSaveFailedToast{ nullptr };
std::shared_ptr<Toast> _windowRenameFailedToast{ nullptr };
std::shared_ptr<Toast> _windowCwdToast{ nullptr };

Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -233,5 +233,14 @@
</StackPanel>
</mux:TeachingTip.Content>
</mux:TeachingTip>
<mux:TeachingTip x:Name="ActionSaveFailedToast"
x:Uid="ActionSaveFailedToast"
Title="Action Save Failed"
x:Load="False"
IsLightDismissEnabled="True">
<mux:TeachingTip.Content>
<TextBox x:Name="ActionSaveFailedMessage" Text="" />
</mux:TeachingTip.Content>
</mux:TeachingTip>
</Grid>
</Page>
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,7 +945,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return winrt::hstring{
fmt::format(L"Save Task commandline:{}, name: {}, keyChord {}", Commandline(), Name(), KeyChord())
};
}
}

static winrt::hstring _FormatColorString(const Control::SelectionColor& selectionColor)
{
Expand Down

0 comments on commit 8bec5ec

Please sign in to comment.