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 ability to save input action from command line #16513

Merged
merged 21 commits into from
Jul 2, 2024

Conversation

e82eric
Copy link
Contributor

@e82eric e82eric commented Jan 1, 2024

Hi wanted to make an attempt at 12857. This still needs work but I think the initial version is ready to be reviewed.

Summary of the Pull Request

Mostly copied from: 6f5b9fb...1cde67a

  • Save to disk
  • If command line is empty use selection
  • Show toast
  • No UI. Trying out the different options now.

PR Checklist

@@ -857,6 +857,18 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
AddAction(*cmd);
}

void ActionMap::AddSendInputAction(winrt::hstring name, winrt::hstring input)
Copy link
Contributor Author

@e82eric e82eric Jan 1, 2024

Choose a reason for hiding this comment

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

I tried to add this in TerminalPage::_HandleSaveTask, the build errors from calling winrt::make(input) from that project defeated me.

- Save to disk
- If command line is empty use selection
- Show toast
@e82eric e82eric force-pushed the save_input_action branch 2 times, most recently from 4ac3c7a to 5d99e7b Compare January 2, 2024 06:12
@e82eric
Copy link
Contributor Author

e82eric commented Jan 2, 2024

WindowsTerminal_SaveInput

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

Just a drive by review for now:

  • structurally, this looks like it hits in all the right places
  • I like the confirmation toast. Great idea.
  • Testing with the commandline mode of the command palette was a clever idea here, really liked that
  • I'm excited about this and definitely need to make some additional time for this review soon

IsLightDismissEnabled="True">
<mux:TeachingTip.Content>
<StackPanel Orientation="Vertical">
<TextBox x:Name="ActionSavedNameText" Text="" />
Copy link
Member

Choose a reason for hiding this comment

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

These may need to be TextBlock's, instead of TextBoxes. Blocks are just for displaying text, while the Box is supposed to be for input. (super intuitive, I know 🙃)

winrt::hstring SaveTaskArgs::GenerateName() const
{
return winrt::hstring{
fmt::format(L"Save Task commandline:{}, name: {}, keyChord {}", Commandline(), Name(), KeyChord())
Copy link
Member

Choose a reason for hiding this comment

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

nit: we may want to omit the arg from the generated name, if the value of the arg is just empty. Like, in your gif, it always shows "keychord ," even when the keys weren't provided yet

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, let me know if that looks ok.

For the toast, I have it showing None when the keyChord is null, is it better to hide the TextBlocks from the toast's content?

Right now if the same name is used I think it will overwrite the existing action, does it make sense to have a --force arg, and fail if there is a existing action for the name and no force arg?

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

More drive-by comments. We discussed possibly holding this from 1.20 (which is gonna be a smaller, more stablization-focused release), and then merge it in for 1.21.

Sorry that we've been slow here. Hopefully in a few weeks, we'll be able to better start moving code again 😄

}
else
{
args.Name(args.GenerateName());
Copy link
Member

Choose a reason for hiding this comment

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

This right here might not be right. In the case that you don't pass a --name explicitly, this ends up setting the SaveTask::Name to something like "Save task commandline: git status". Then later, we'll build the SendInput action, and set it's name to "Save task commandline: git status".

The json for this will end up like

{
    "command": 
    {
        "action": "sendInput",
        "input": "git status"
    },
    "name": "Save Task commandline: git status"
},

I'm pretty sure we'd want to just leave that .Name empty

{
std::wstring formattedString;

if (!Name().empty() && !KeyChord().empty())
Copy link
Member

Choose a reason for hiding this comment

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

You may want to copy the pattern in SplitPaneArgs::GenerateName, which uses a stringstream, and then just adds each successive parameter to the stream (if the param is provided)


if (!Name().empty() && !KeyChord().empty())
{
formattedString = fmt::format(L"Save Task commandline: {}, name: {}, keyChord {}", Commandline(), Name(), KeyChord());
Copy link
Member

Choose a reason for hiding this comment

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

"Save Task" will almost definitely need to be in the resources.resw, and loaded with RS_ (like the other actions)

IsLightDismissEnabled="True">
<mux:TeachingTip.Content>
<StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
<TextBlock x:Name="ActionSavedNameText" Text="" />
Copy link
Member

Choose a reason for hiding this comment

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

Playing with this locally, there's still like, a faint background to these text blocks. I can't quite tell why - maybe there's a weird interaction with the acrylic of the Toast. Any chance you're seeing that? Maybe it's just me 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried a few different settings and wasn't able to notice anything. I am not able to see depths at all, so I may just be missing it.

@zadjii-msft zadjii-msft added this to the Terminal 1.21 milestone Jan 19, 2024
@e82eric
Copy link
Contributor Author

e82eric commented Jan 20, 2024

More drive-by comments. We discussed possibly holding this from 1.20 (which is gonna be a smaller, more stablization-focused release), and then merge it in for 1.21.

Sorry that we've been slow here. Hopefully in a few weeks, we'll be able to better start moving code again 😄

Sounds good! No rush on my end.

@zadjii-msft
Copy link
Member

Notes for team discussion later this week:

  • Overall, I think I'm fine with the ideas in this PR. Namely:
    • the wt save subcommand to save a commandline as a sendInput action (aka, a "task")
    • the saveTask action
    • the toasts
  • There are some annoying things that I think probably need to get done before we can ship this, and I'm volunteering to finish them in post. Namely:

But, this also lines up well with other work that's in progress (#13445 (comment)), so I'd like to get this in and keep iterating.

@zadjii-msft zadjii-msft added the Needs-Discussion Something that requires a team discussion before we can proceed label Mar 18, 2024
@zadjii-msft
Copy link
Member

Discussion feedback:

  • Stick behind velocity
  • change subcommand to x-save, ala MIME types to indicate "this subcommand is experimental"

@e82eric
Copy link
Contributor Author

e82eric commented Mar 20, 2024

I think the key chord parsing got broken with the upstream merge. I think I can take a look tonight.

@e82eric
Copy link
Contributor Author

e82eric commented Mar 21, 2024

I think the key chord parsing got broken with the upstream merge. I think I can take a look tonight.

It looks like I had broken that here. Should be fixed now.

ActionSaved(realArgs.Commandline(), realArgs.Name(), L"None");

@e82eric
Copy link
Contributor Author

e82eric commented Mar 21, 2024

Notes for team discussion later this week:

  • Overall, I think I'm fine with the ideas in this PR. Namely:

    • the wt save subcommand to save a commandline as a sendInput action (aka, a "task")
    • the saveTask action
    • the toasts
  • There are some annoying things that I think probably need to get done before we can ship this, and I'm volunteering to finish them in post. Namely:

But, this also lines up well with other work that's in progress (#13445 (comment)), so I'd like to get this in and keep iterating.

Is this along the line of what you were thinking for not displaying unfilled display params? d4eaec3

Wanted to try to address this one but wasn't sure what the behavior was. Is there a way that I can reproduce it? "should default to _the current window"

@e82eric
Copy link
Contributor Author

e82eric commented Mar 21, 2024

Discussion feedback:

  • Stick behind velocity
  • change subcommand to x-save, ala MIME types to indicate "this subcommand is experimental"

for x-save. is this along the lines of what you were thinking? 50baa7b

For the first, what does velocity mean in this context?

@zadjii-msft
Copy link
Member

Hey sorry about not replying earlier. I was... delayed... getting ready for Build.

BUT THAT'S DONE NOW. NOW IT'S TIME FOR FUN.

I'm just gonna quick leave this link here: dev/migrie/save-input-patches so that I don't lose it.

I was prototyping some other fixes that would make the XAML binding less painful. Admittedly, that branched off probably two or three months ago, so I'm sure it's stale.


for x-save. is this along the lines of what you were thinking? 50baa7b

Exactly that! That's perfect.

For the first, what does velocity mean in this context?

"Velocity" is our hodgepodge'd way of controlling which features go into which branches. It's pretty much all in features.xml. (you already figured this out in e82eric@89a4e14). We just call it that still since that's what it's called in Windows internally.


I'm gonna start looping back on this now, cause this is a pretty important part of the story I think we can tell with #17329. Thanks for all the patience!

@zadjii-msft zadjii-msft removed this from the Terminal v1.21 milestone May 28, 2024
Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

Honestly, my only concerns are over some comments? Yea that's not worth blocking over. (Things I mark with 📝 are meant as notes to myself while reviewing, which may be helpful for other reviewers)

The saving settings on the UI thread thing isn't great, but we should fix that for both the settings UI and this at once. That's not on you.

I'll come through after this and figure out how to yeet this action into the current terminal window using the Monarch. I'm not gonna ask you to try and wrap your head around the fuckiness that is our cross-window communication 😅

Thanks for all your patience here!

@@ -52,6 +52,7 @@ static constexpr std::string_view SwitchToTabKey{ "switchToTab" };
static constexpr std::string_view TabSearchKey{ "tabSearch" };
static constexpr std::string_view ToggleAlwaysOnTopKey{ "toggleAlwaysOnTop" };
static constexpr std::string_view ToggleCommandPaletteKey{ "commandPalette" };
static constexpr std::string_view SaveTaskKey{ "saveTask" };
Copy link
Member

Choose a reason for hiding this comment

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

Okay this is super annoying but I think we collectively decided to change the name of this feature to "snippets" to better align with VS tools. We only decided that in like the last month, so that's on me to actually come through and fix

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Made an attempt at this in the commit below. Not totally sure I got the names right. Let me know if I should add this commit to the PR (totally understand if you want to update it yourself).

e82eric@f5ceac9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Testing this I realized that I have an issue where you can save a snippet without a command line and a validation warning about the incorrect configs pops up after. I think this commit would fix it (It may need to be localized)

e82eric@d6873c1

{
cmd->Name(name);
}
cmd->RegisterKey(keys);
Copy link
Member

Choose a reason for hiding this comment

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

📝: this feels like it'll change after ~#17215, but that shouldn't be too bad

Copy link
Member

Choose a reason for hiding this comment

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

@e82eric FYI I believe that PR did merge now, so this one conflicts now. Let me know if you heed help with the conflict

Copy link
Contributor Author

@e82eric e82eric Jun 11, 2024

Choose a reason for hiding this comment

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

It should be updated now. I ran into 2 questions through.

  1. I copied the last 2 lines from ActionMap::RegisterKeyBinding instead of just calling ActionMap::RegisterKeyBinding to get the name to show up.
    void ActionMap::AddSendInputAction(winrt::hstring name, winrt::hstring input, const Control::KeyChord keys)
    {
        auto newAction = winrt::make<ActionAndArgs>();
        newAction.Action(ShortcutAction::SendInput);
        auto sendInputArgs = winrt::make<SendInputArgs>(input);
        newAction.Args(sendInputArgs);
        auto cmd{ make_self<Command>() };
        cmd->ActionAndArgs(newAction);
        if (!name.empty())
        {
            cmd->Name(name);
        }
        cmd->GenerateID();
        AddAction(*cmd, keys);
    }
  1. I keep wanting to move the above code to TerminalPage::_HandleSaveSnippet but I can't get winrt::make<ActionAndArgs>() to work. Should I keep trying to get that to work, or is it ok to have the one off method in ActionMap?

Orientation="Vertical">
<TextBlock x:Name="ActionSavedNameText"
Visibility="{x:Bind mtu:Converters.StringNotEmptyToVisibility(SavedActionName), Mode=OneWay}">
<Run Text="Name: " />
Copy link
Member

Choose a reason for hiding this comment

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

📝: I think I'm fine with these not being localized, since they're literal params to the action args, and we've got precedent for that in the cmdpal already

void TerminalPage::_HandleSaveTask(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (Feature_SaveTask::IsEnabled())
Copy link
Member

Choose a reason for hiding this comment

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

IMO I'd just do an early-return if the feature isn't enabled, but I'm not sure if that messes with the compiler's ability to just optimize the entire rest of the function out

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 to early return. (Is there a way to tell if the compiler will struggle with this?)

void TerminalPage::_HandleSaveTask(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (Feature_SaveTask::IsEnabled())
Copy link
Member

Choose a reason for hiding this comment

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

Oh also:

Suggested change
if (Feature_SaveTask::IsEnabled())
if constexpr (Feature_SaveTask::IsEnabled())

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.

keyChord = KeyChordSerialization::FromString(winrt::to_hstring(realArgs.KeyChord()));
}
_settings.GlobalSettings().ActionMap().AddSendInputAction(realArgs.Name(), realArgs.Commandline(), keyChord);
_settings.WriteSettingsToDisk();
Copy link
Member

Choose a reason for hiding this comment

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

WriteSettingsToDisk writes the file on this thread. We definitely can't be doing this on the UI thread.

You might be able to just do a

auto saveSettings = [settings=_settings]() -> winrt::fire_and_forget {
  co_await winrt::resume_background();
  settings.WriteSettingsToDisk();
};
saveSettings();

to basically make a lambda that does the fire_and_forget thing, but then also just call it immediately. I haven't tested it, so I'm sure there's spooky coroutine reasons that's a bad idea, but 🤷

Copy link
Member

Choose a reason for hiding this comment

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

Also now that I'm looking at it, the Settings UI itself saves the settings on the UI thread! Egads! I don't think it's worth blocking you over this if we can't even do it right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I gave this a try just so I could get a better understanding of how the fire_and_forget stuff works.

It looked like it worked, the settings were saved to the file, and I was able to see it switch from the window thread to the background thread in the debugger when it did the write to disk.

In the debugger I started getting a AV. Adding a strong ref to settings seemed to prevent that.

auto saveSettings = [settings = _settings]() -> winrt::fire_and_forget {
    const auto strongSettings = settings;
    co_await winrt::resume_background();
    strongSettings.WriteSettingsToDisk();
};
saveSettings();

// Build the NewTab action from the values we've parsed on the commandline.
ActionAndArgs saveAction{};
saveAction.Action(ShortcutAction::SaveTask);
// _getNewTerminalArgs MUST be called before parsing any other options,
Copy link
Member

Choose a reason for hiding this comment

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

a bit of copypasta here 😅

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 to be slightly more generic. // Build the action from the values we've parsed on the commandline.

Does this make sense or should I just remove it?

Comment on lines 559 to 560
// _getNewTerminalArgs MUST be called before parsing any other options,
// as it might clear those options while finding the commandline
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// _getNewTerminalArgs MUST be called before parsing any other options,
// as it might clear those options while finding the commandline
// First, parse out the commandline in the same way that
// _getNewTerminalArgs does 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.

Updated to suggestion inside of commit 2bf8391

@zadjii-msft zadjii-msft removed their assignment Jun 3, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added Issue-Task It's a feature request, but it doesn't really need a major design. Area-Settings Issues related to settings and customizability, for console or terminal Product-Terminal The new Windows Terminal. labels Jun 3, 2024
{
const auto selections{ termControl.SelectedText(true) };
const auto selection = std::accumulate(selections.begin(), selections.end(), std::wstring());
realArgs.Commandline(selection);
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't modify the realArgs.Commandline() but instead extract the hstring and then modify the local reference. Like this:

auto commandline = realArgs.Commandline();
if (commandline.empty() {
    // ...
    commandline = selection;
}

if (commandline.empty()) {
    // ...
}

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

@@ -4184,6 +4184,66 @@ namespace winrt::TerminalApp::implementation
}
}

winrt::fire_and_forget TerminalPage::ActionSaved(winrt::hstring input, winrt::hstring name, winrt::hstring keyChord)
Copy link
Member

Choose a reason for hiding this comment

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

Why put these two functions here instead of having them next to the related code inside AppActionHandlers.cpp? I feel like it should either be all here or all there.

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

Comment on lines 4189 to 4191
auto weakThis{ get_weak() };
co_await wil::resume_foreground(Dispatcher());
if (auto page{ weakThis.get() })
Copy link
Member

Choose a reason for hiding this comment

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

Does this code get called from a background thread?

Copy link
Contributor Author

@e82eric e82eric Jul 1, 2024

Choose a reason for hiding this comment

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

you are right, It was not running on a background thread, before and after the co_await it was on the Window thread. Updated to just use void and removed the co_await.

@zadjii-msft zadjii-msft added this pull request to the merge queue Jul 2, 2024
Merged via the queue into microsoft:main with commit d051f70 Jul 2, 2024
17 checks passed
zadjii-msft added a commit that referenced this pull request Jul 9, 2024
In the spec review, we agreed these didn't really need to be saved to the user's
own settings file. This removes parsing and saving for the
`experimental.saveSnippet` action, but we still have the action _internally_.
This is powered by a new x-macro for "INTERNAL_" actions.

Follow-up from #16513.
github-merge-queue bot pushed a commit that referenced this pull request Jul 10, 2024
In the spec review, we agreed these didn't really need to be saved to
the user's own settings file. This removes parsing and saving for the
`experimental.saveSnippet` action, but we still have the action
_internally_. This is powered by a new x-macro for "INTERNAL_" actions.

Follow-up from #16513.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Issue-Task It's a feature request, but it doesn't really need a major design. Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to save selected text as a sendInput action
3 participants