Skip to content

Commit

Permalink
handle uri action
Browse files Browse the repository at this point in the history
  • Loading branch information
PankajBhojwani committed Jul 4, 2024
1 parent fb7ff3c commit 8bf23bc
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1490,4 +1490,16 @@ namespace winrt::TerminalApp::implementation
_ShowAboutDialog();
args.Handled(true);
}

void TerminalPage::_HandleHandleUri(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (const auto& uriArgs{ args.ActionArgs().try_as<HandleUriArgs>() })
{
if (!uriArgs.Uri().empty())
{
args.Handled(true);
}
}
}
}
41 changes: 40 additions & 1 deletion src/cascadia/TerminalApp/AppCommandlineArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ void AppCommandlineArgs::_buildParser()
_buildMovePaneParser();
_buildSwapPaneParser();
_buildFocusPaneParser();
_buildHandleUriParser();
}

// Method Description:
Expand Down Expand Up @@ -537,6 +538,43 @@ void AppCommandlineArgs::_buildFocusPaneParser()
setupSubcommand(_focusPaneShort);
}

void AppCommandlineArgs::_buildHandleUriParser()
{
_handleUriCommand = _app.add_subcommand("handle-uri", RS_A(L"CmdHandleUriDesc"));

auto setupSubcommand = [this](auto* subcommand) {
// When ParseCommand is called, if this subcommand was provided, this
// callback function will be triggered on the same thread. We can be sure
// that `this` will still be safe - this function just lets us know this
// command was parsed.
subcommand->callback([&, this]() {
wil::WaitForDebuggerPresent(false);
// Build the action from the values we've parsed on the commandline.
const auto cmdlineArgs = _currentCommandline->Args();
winrt::hstring uri;
for (auto i = 0; i < cmdlineArgs.size(); ++i)
{
if (cmdlineArgs[i] == "handle-uri")
{
// the next arg is our uri
if ((i + 1) < cmdlineArgs.size())
{
uri = winrt::to_hstring(cmdlineArgs[i + 1]);
break;
}
}
}
ActionAndArgs handleUriAction{};
handleUriAction.Action(ShortcutAction::HandleUri);
HandleUriArgs args{ uri };
handleUriAction.Args(args);
_startupActions.push_back(handleUriAction);
});
};

setupSubcommand(_handleUriCommand);
}

// Method Description:
// - Add the `NewTerminalArgs` parameters to the given subcommand. This enables
// that subcommand to support all the properties in a NewTerminalArgs.
Expand Down Expand Up @@ -710,7 +748,8 @@ bool AppCommandlineArgs::_noCommandsProvided()
*_focusPaneCommand ||
*_focusPaneShort ||
*_newPaneShort.subcommand ||
*_newPaneCommand.subcommand);
*_newPaneCommand.subcommand ||
*_handleUriCommand);
}

// Method Description:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/AppCommandlineArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ class TerminalApp::AppCommandlineArgs final
CLI::App* _swapPaneCommand;
CLI::App* _focusPaneCommand;
CLI::App* _focusPaneShort;
CLI::App* _handleUriCommand;

// Are you adding a new sub-command? Make sure to update _noCommandsProvided!

Expand Down Expand Up @@ -148,6 +149,7 @@ class TerminalApp::AppCommandlineArgs final
void _buildMovePaneParser();
void _buildSwapPaneParser();
void _buildFocusPaneParser();
void _buildHandleUriParser();
bool _noCommandsProvided();
void _resetStateToDefault();
int _handleExit(const CLI::App& command, const CLI::Error& e);
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,9 @@
<data name="CmdFocusPaneTargetArgDesc" xml:space="preserve">
<value>Focus the pane at the given index</value>
</data>
<data name="CmdHandleUriDesc" xml:space="preserve">
<value>Handle the given URI</value>
</data>
<data name="CmdProfileArgDesc" xml:space="preserve">
<value>Open with the given profile. Accepts either the name or GUID of a profile</value>
</data>
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ static constexpr std::string_view RestartConnectionKey{ "restartConnection" };
static constexpr std::string_view ToggleBroadcastInputKey{ "toggleBroadcastInput" };
static constexpr std::string_view OpenScratchpadKey{ "experimental.openScratchpad" };
static constexpr std::string_view OpenAboutKey{ "openAbout" };
static constexpr std::string_view HandleUriKey{ "handleUri" };

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

Expand Down
16 changes: 16 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
#include "SelectCommandArgs.g.cpp"
#include "SelectOutputArgs.g.cpp"
#include "ColorSelectionArgs.g.cpp"
#include "HandleUriArgs.g.cpp"

#include <LibraryResources.h>
#include <WtExeUtils.h>
Expand Down Expand Up @@ -1084,4 +1085,19 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
}
return L"";
}

winrt::hstring HandleUriArgs::GenerateName() const
{
const auto str = RS_(L"HandleUri_default_action"); // "Handle Uri: {0}"
if (_Uri)
{
return winrt::hstring{
fmt::format(std::wstring_view{ str }, _Uri.value())
};
}
else
{
return str;
}
}
}
8 changes: 8 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
#include "SelectCommandArgs.g.h"
#include "SelectOutputArgs.g.h"
#include "ColorSelectionArgs.g.h"
#include "HandleUriArgs.g.h"

#include "JsonUtils.h"
#include "HashUtils.h"
Expand Down Expand Up @@ -273,6 +274,10 @@ protected: \
#define SELECT_OUTPUT_ARGS(X) \
X(SelectOutputDirection, Direction, "direction", false, SelectOutputDirection::Previous)

////////////////////////////////////////////////////////////////////////////////
#define HANDLE_URI_ARGS(X) \
X(winrt::hstring, Uri, "uri", false)

////////////////////////////////////////////////////////////////////////////////
#define COLOR_SELECTION_ARGS(X) \
X(winrt::Microsoft::Terminal::Control::SelectionColor, Foreground, "foreground", false, nullptr) \
Expand Down Expand Up @@ -911,6 +916,8 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
ACTION_ARGS_STRUCT(SelectCommandArgs, SELECT_COMMAND_ARGS);
ACTION_ARGS_STRUCT(SelectOutputArgs, SELECT_OUTPUT_ARGS);

ACTION_ARGS_STRUCT(HandleUriArgs, HANDLE_URI_ARGS);

ACTION_ARGS_STRUCT(ColorSelectionArgs, COLOR_SELECTION_ARGS);

}
Expand Down Expand Up @@ -952,4 +959,5 @@ namespace winrt::Microsoft::Terminal::Settings::Model::factory_implementation
BASIC_FACTORY(SuggestionsArgs);
BASIC_FACTORY(SelectCommandArgs);
BASIC_FACTORY(SelectOutputArgs);
BASIC_FACTORY(HandleUriArgs);
}
6 changes: 5 additions & 1 deletion src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -443,5 +443,9 @@ namespace Microsoft.Terminal.Settings.Model
SelectOutputDirection Direction { get; };
}


[default_interface] runtimeclass HandleUriArgs : IActionArgs
{
HandleUriArgs(String uri);
String Uri { get; };
}
}
6 changes: 4 additions & 2 deletions src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@
ON_ALL_ACTIONS(RestartConnection) \
ON_ALL_ACTIONS(ToggleBroadcastInput) \
ON_ALL_ACTIONS(OpenScratchpad) \
ON_ALL_ACTIONS(OpenAbout)
ON_ALL_ACTIONS(OpenAbout) \
ON_ALL_ACTIONS(HandleUri)

#define ALL_SHORTCUT_ACTIONS_WITH_ARGS \
ON_ALL_ACTIONS_WITH_ARGS(AdjustFontSize) \
Expand Down Expand Up @@ -157,4 +158,5 @@
ON_ALL_ACTIONS_WITH_ARGS(Suggestions) \
ON_ALL_ACTIONS_WITH_ARGS(SelectCommand) \
ON_ALL_ACTIONS_WITH_ARGS(SelectOutput) \
ON_ALL_ACTIONS_WITH_ARGS(ColorSelection)
ON_ALL_ACTIONS_WITH_ARGS(ColorSelection) \
ON_ALL_ACTIONS_WITH_ARGS(HandleUri)
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,10 @@
<value>[default]</value>
<comment>This is used in the description of an action which changes the color of selected text, as a placeholder for a color, to indicate that the default (foreground or background) color will be used.</comment>
</data>
<data name="HandleUri_default_action" xml:space="preserve">
<value>Handle Uri: {0}</value>
<comment>This is the description of an action which handles the given URI. {0}: The URI to handle.</comment>
</data>
<data name="ColorSelection_Black" xml:space="preserve">
<value>black</value>
<comment>A color used in the "ColorSelection" action.</comment>
Expand Down

0 comments on commit 8bf23bc

Please sign in to comment.