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 action for opening settings directory in file explorer #17690

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 10 additions & 1 deletion src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,11 +438,20 @@ namespace winrt::TerminalApp::implementation
}
}
}
void TerminalPage::_HandleOpenSettings(const IInspectable& /*sender*/,
void TerminalPage::_HandleOpenSettings(const IInspectable& sender,
const ActionEventArgs& args)
{
if (const auto& realArgs = args.ActionArgs().try_as<OpenSettingsArgs>())
{
if (realArgs.Target() == SettingsTarget::SendInput)
{
if (const auto termControl{ _senderOrActiveControl(sender) })
{
termControl.SendInput(CascadiaSettings::SettingsPath());
args.Handled(true);
return;
}
}
_LaunchSettings(realArgs.Target());
args.Handled(true);
}
Expand Down
46 changes: 46 additions & 0 deletions src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2687,6 +2687,38 @@ namespace winrt::TerminalApp::implementation
return wil::unique_close_clipboard_call{ success };
}

static void _copyToClipboard(const UINT format, const void* src, const size_t bytes)
{
wil::unique_hglobal handle{ THROW_LAST_ERROR_IF_NULL(GlobalAlloc(GMEM_MOVEABLE, bytes)) };

const auto locked = GlobalLock(handle.get());
memcpy(locked, src, bytes);
GlobalUnlock(handle.get());

THROW_LAST_ERROR_IF_NULL(SetClipboardData(format, handle.get()));
handle.release();
}

static void copyToClipboard(wil::zwstring_view text)
{
const auto clipboard = _openClipboard(nullptr);
if (!clipboard)
{
LOG_LAST_ERROR();
return;
}

EmptyClipboard();

if (!text.empty())
{
// As per: https://learn.microsoft.com/en-us/windows/win32/dataxchg/standard-clipboard-formats
// CF_UNICODETEXT: [...] A null character signals the end of the data.
// --> We add +1 to the length. This works because .c_str() is null-terminated.
_copyToClipboard(CF_UNICODETEXT, text.c_str(), (text.size() + 1) * sizeof(wchar_t));
}
}

static winrt::hstring _extractClipboard()
{
// This handles most cases of pasting text as the OS converts most formats to CF_UNICODETEXT automatically.
Expand Down Expand Up @@ -3079,6 +3111,14 @@ namespace winrt::TerminalApp::implementation
}
};

auto openFolder = [](const auto& filePath) {
HINSTANCE res = ShellExecute(nullptr, nullptr, filePath.c_str(), nullptr, nullptr, SW_SHOW);
if (static_cast<int>(reinterpret_cast<uintptr_t>(res)) <= 32)
{
ShellExecute(nullptr, nullptr, L"open", filePath.c_str(), nullptr, SW_SHOW);
}
};

switch (target)
{
case SettingsTarget::DefaultsFile:
Expand All @@ -3087,6 +3127,12 @@ namespace winrt::TerminalApp::implementation
case SettingsTarget::SettingsFile:
openFile(CascadiaSettings::SettingsPath());
break;
case SettingsTarget::FileExplorer:
openFolder(CascadiaSettings::SettingsDirectory());
break;
case SettingsTarget::Clipboard:
copyToClipboard(CascadiaSettings::SettingsPath().c_str());
Copy link
Member

Choose a reason for hiding this comment

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

The .c_str() is unnecessary, if not detrimental, because constructing a string-view from a nullptr is UB.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated. (Sorry, I should have thought that through)

Let me know if I should remove the other 2 actions. Fwiw, the send input one is what I have found most useful, I don't think I have ever actually used the other 2 outside of testing. But that is probably just because of my personal workflows.

break;
case SettingsTarget::AllFiles:
openFile(CascadiaSettings::DefaultSettingsPath());
openFile(CascadiaSettings::SettingsPath());
Expand Down
6 changes: 6 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return RS_(L"OpenBothSettingsFilesCommandKey");
case SettingsTarget::SettingsFile:
return RS_(L"OpenSettingsCommandKey");
case SettingsTarget::SendInput:
return RS_(L"SettingFilePathSendInputCommandKey");
case SettingsTarget::FileExplorer:
return RS_(L"SettingsFileOpenInExplorerCommandKey");
case SettingsTarget::Clipboard:
return RS_(L"SettingsFilePathSendToClipboard");
case SettingsTarget::SettingsUI:
default:
return RS_(L"OpenSettingsUICommandKey");
Expand Down
5 changes: 4 additions & 1 deletion src/cascadia/TerminalSettingsModel/ActionArgs.idl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ namespace Microsoft.Terminal.Settings.Model
SettingsFile = 0,
DefaultsFile,
AllFiles,
SettingsUI
SettingsUI,
FileExplorer,
SendInput,
Clipboard
};

enum MoveTabDirection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,15 @@
<value>Open settings file (JSON)</value>
<comment>{Locked="JSON"}. "JSON" is the extension of the file that will be opened.</comment>
</data>
<data name="SettingFilePathSendInputCommandKey" xml:space="preserve">
<value>Send the settings file path as input to the terminal</value>
</data>
<data name="SettingsFileOpenInExplorerCommandKey" xml:space="preserve">
<value>Open the settings file directory in File Explorer</value>
</data>
<data name="SettingsFilePathSendToClipboard" xml:space="preserve">
<value>Send settings file path to clipboard</value>
</data>
<data name="OpenTabColorPickerCommandKey" xml:space="preserve">
<value>Set the tab color</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,14 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::SplitType)

JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::SettingsTarget)
{
JSON_MAPPINGS(4) = {
JSON_MAPPINGS(7) = {
pair_type{ "settingsFile", ValueType::SettingsFile },
pair_type{ "defaultsFile", ValueType::DefaultsFile },
pair_type{ "allFiles", ValueType::AllFiles },
pair_type{ "settingsUI", ValueType::SettingsUI },
pair_type{ "fileExplorer", ValueType::FileExplorer },
pair_type{ "sendInput", ValueType::SendInput },
pair_type{ "clipboard", ValueType::Clipboard },
};
};

Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@
{ "command": { "action": "openSettings", "target": "settingsUI" }, "id": "Terminal.OpenSettingsUI" },
{ "command": { "action": "openSettings", "target": "settingsFile" }, "id": "Terminal.OpenSettingsFile" },
{ "command": { "action": "openSettings", "target": "defaultsFile" }, "id": "Terminal.OpenDefaultSettingsFile" },
{ "command": { "action": "openSettings", "target": "sendInput" }, "id": "Terminal.SendInputSettingsFile" },
{ "command": { "action": "openSettings", "target": "fileExplorer" }, "id": "Terminal.FileExplorerSettingsFile" },
{ "command": { "action": "openSettings", "target": "clipboard" }, "id": "Terminal.ClipboardSettingsFile" },
{ "command": "find", "id": "Terminal.FindText" },
{ "command": { "action": "findMatch", "direction": "next" }, "id": "Terminal.FindNextMatch" },
{ "command": { "action": "findMatch", "direction": "prev" }, "id": "Terminal.FindPrevMatch" },
Expand Down
Loading