Skip to content

Commit

Permalink
PRE-MERGE #14939 Add tooltips to the Suggestions Control
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Mar 10, 2023
2 parents bd873fa + 110bb7d commit dbe8e13
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 7 deletions.
76 changes: 73 additions & 3 deletions src/cascadia/TerminalApp/SuggestionsControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <LibraryResources.h>

#include "SuggestionsControl.g.cpp"
#include "../../types/inc/utils.hpp"

using namespace winrt;
using namespace winrt::TerminalApp;
Expand All @@ -19,6 +20,8 @@ using namespace winrt::Windows::Foundation;
using namespace winrt::Windows::Foundation::Collections;
using namespace winrt::Microsoft::Terminal::Settings::Model;

using namespace std::chrono_literals;

namespace winrt::TerminalApp::implementation
{
SuggestionsControl::SuggestionsControl()
Expand Down Expand Up @@ -254,21 +257,87 @@ namespace winrt::TerminalApp::implementation
// - <unused>
// Return Value:
// - <none>
void SuggestionsControl::_selectedCommandChanged(const IInspectable& /*sender*/,
const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
winrt::fire_and_forget SuggestionsControl::_selectedCommandChanged(const IInspectable& /*sender*/,
const Windows::UI::Xaml::RoutedEventArgs& /*args*/)
{
const auto selectedCommand = _filteredActionsView().SelectedItem();
const auto filteredCommand{ selectedCommand.try_as<winrt::TerminalApp::FilteredCommand>() };

// DescriptionTip().IsOpen(false);
_PropertyChangedHandlers(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"SelectedItem" });

if (filteredCommand != nullptr)
{
if (const auto actionPaletteItem{ filteredCommand.Item().try_as<winrt::TerminalApp::ActionPaletteItem>() })
{
_PreviewActionHandlers(*this, actionPaletteItem.Command());
const auto& cmd = actionPaletteItem.Command();
_PreviewActionHandlers(*this, cmd);
const auto description{ cmd.Description() };
if (!description.empty())
{
// If it's already open, then just re-target it and update the content immediately.
if (DescriptionTip().IsOpen())
{
_openTooltip(cmd);
}
else
{
// Otherwise, wait a bit before opening it.
co_await winrt::resume_after(200ms);
co_await wil::resume_foreground(Dispatcher());
_openTooltip(cmd);
DescriptionTip().IsOpen(true);
}
}
else
{
// If there's no description, then just close the tooltip.
DescriptionTip().IsOpen(false);
}
}
}
}

winrt::fire_and_forget SuggestionsControl::_openTooltip(Command cmd)
{
const auto description{ cmd.Description() };

if (!cmd.Description().empty())
{
DescriptionTip().Target(SelectedItem());
DescriptionTip().Title(cmd.Name());

// If you try to put a newline in the Subtitle, it'll _immediately
// close the tooltip_. Instead, we'll need to build up the text as a
// series of runs, and put them in the content.

_toolTipContent().Inlines().Clear();

// First, replace all "\r\n" with "\n"
std::wstring filtered = description.c_str();

// replace all "\r\n" with "\n" in `filtered`
std::wstring::size_type pos = 0;
while ((pos = filtered.find(L"\r\n", pos)) != std::wstring::npos)
{
filtered.erase(pos, 1);
}

// Split the filtered description on '\n`
const auto lines = ::Microsoft::Console::Utils::SplitString(filtered.c_str(), L'\n');
// For each line, build a Run + LineBreak, and add them to the text
// block
for (const auto& line : lines)
{
if (line.empty())
continue;
Documents::Run textRun;
textRun.Text(winrt::hstring{ line });
_toolTipContent().Inlines().Append(textRun);
_toolTipContent().Inlines().Append(Documents::LineBreak{});
}
}
co_return;
}

void SuggestionsControl::_previewKeyDownHandler(const IInspectable& /*sender*/,
Expand Down Expand Up @@ -942,6 +1011,7 @@ namespace winrt::TerminalApp::implementation
void SuggestionsControl::_close()
{
Visibility(Visibility::Collapsed);
DescriptionTip().IsOpen(false);

_PreviewActionHandlers(*this, nullptr);

Expand Down
6 changes: 4 additions & 2 deletions src/cascadia/TerminalApp/SuggestionsControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ namespace winrt::TerminalApp::implementation
void _keyUpHandler(const Windows::Foundation::IInspectable& sender,
const Windows::UI::Xaml::Input::KeyRoutedEventArgs& e);

void _selectedCommandChanged(const Windows::Foundation::IInspectable& sender,
const Windows::UI::Xaml::RoutedEventArgs& args);
winrt::fire_and_forget _selectedCommandChanged(const Windows::Foundation::IInspectable& sender,
const Windows::UI::Xaml::RoutedEventArgs& args);

void _updateUIForStackChange();

Expand Down Expand Up @@ -119,6 +119,8 @@ namespace winrt::TerminalApp::implementation
void _scrollToIndex(uint32_t index);
uint32_t _getNumVisibleItems();

winrt::fire_and_forget _openTooltip(Microsoft::Terminal::Settings::Model::Command cmd);

void _choosingItemContainer(const Windows::UI::Xaml::Controls::ListViewBase& sender, const Windows::UI::Xaml::Controls::ChoosingItemContainerEventArgs& args);
void _containerContentChanging(const Windows::UI::Xaml::Controls::ListViewBase& sender, const Windows::UI::Xaml::Controls::ContainerContentChangingEventArgs& args);
winrt::TerminalApp::PaletteItemTemplateSelector _itemTemplateSelector{ nullptr };
Expand Down
11 changes: 11 additions & 0 deletions src/cascadia/TerminalApp/SuggestionsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,17 @@
ItemsSource="{x:Bind FilteredActions}"
SelectionChanged="_listItemSelectionChanged"
SelectionMode="Single" />
<mux:TeachingTip x:Name="DescriptionTip"
Title=""
IsOpen="False"
PreferredPlacement="Right"
ShouldConstrainToRootBounds="False"
Subtitle="">
<ScrollViewer HorizontalScrollBarVisibility="Hidden"
HorizontalScrollMode="Enabled">
<TextBlock x:Name="_toolTipContent" />
</ScrollViewer>
</mux:TeachingTip>

</Grid>

Expand Down
10 changes: 8 additions & 2 deletions src/cascadia/TerminalSettingsModel/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static constexpr std::string_view ArgsKey{ "args" };
static constexpr std::string_view IterateOnKey{ "iterateOn" };
static constexpr std::string_view CommandsKey{ "commands" };
static constexpr std::string_view KeysKey{ "keys" };
static constexpr std::string_view DescriptionKey{ "description" };

static constexpr std::string_view ProfileNameToken{ "${profile.name}" };
static constexpr std::string_view ProfileIconToken{ "${profile.icon}" };
Expand All @@ -40,6 +41,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
auto command{ winrt::make_self<Command>() };
command->_name = _name;
command->_Description = _Description;
command->_ActionAndArgs = *get_self<implementation::ActionAndArgs>(_ActionAndArgs)->Copy();
command->_keyMappings = _keyMappings;
command->_iconPath = _iconPath;
Expand Down Expand Up @@ -254,7 +256,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

auto nested = false;
JsonUtils::GetValueForKey(json, IterateOnKey, result->_IterateOn);

JsonUtils::GetValueForKey(json, DescriptionKey, result->_Description);
// For iterable commands, we'll make another pass at parsing them once
// the json is patched. So ignore parsing sub-commands for now. Commands
// will only be marked iterable on the first pass.
Expand Down Expand Up @@ -406,7 +408,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
Json::Value cmdJson{ Json::ValueType::objectValue };
JsonUtils::SetValueForKey(cmdJson, IconKey, _iconPath);
JsonUtils::SetValueForKey(cmdJson, NameKey, _name);

JsonUtils::SetValueForKey(cmdJson, DescriptionKey, _Description);
if (_ActionAndArgs)
{
cmdJson[JsonKey(ActionKey)] = ActionAndArgs::ToJson(_ActionAndArgs);
Expand All @@ -426,6 +428,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// First iteration also writes icon and name
JsonUtils::SetValueForKey(cmdJson, IconKey, _iconPath);
JsonUtils::SetValueForKey(cmdJson, NameKey, _name);
JsonUtils::SetValueForKey(cmdJson, DescriptionKey, _Description);
}

if (_ActionAndArgs)
Expand Down Expand Up @@ -656,15 +659,18 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
const auto parseElement = [&](const auto& element) {
winrt::hstring completionText;
winrt::hstring listText;
winrt::hstring tooltipText;
JsonUtils::GetValueForKey(element, "CompletionText", completionText);
JsonUtils::GetValueForKey(element, "ListItemText", listText);
JsonUtils::GetValueForKey(element, "ToolTip", tooltipText);

Model::SendInputArgs args{ winrt::hstring{ fmt::format(L"{}{}", backspaces, completionText.c_str()) } };
Model::ActionAndArgs actionAndArgs{ ShortcutAction::SendInput, args };

auto c = winrt::make_self<Command>();
c->_name = listText;
c->_ActionAndArgs = actionAndArgs;
c->_Description = tooltipText;

// Try to assign a sensible icon based on the result type. These are
// roughly chosen to align with the icons in
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

WINRT_PROPERTY(ExpandCommandType, IterateOn, ExpandCommandType::None);
WINRT_PROPERTY(Model::ActionAndArgs, ActionAndArgs);
WINRT_PROPERTY(winrt::hstring, Description, L"");

private:
Json::Value _originalJson;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/Command.idl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ namespace Microsoft.Terminal.Settings.Model
Command();

String Name { get; };
String Description { get; };

ActionAndArgs ActionAndArgs { get; };
Microsoft.Terminal.Control.KeyChord Keys { get; };
void RegisterKey(Microsoft.Terminal.Control.KeyChord keys);
Expand Down

1 comment on commit dbe8e13

@github-actions
Copy link

Choose a reason for hiding this comment

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

@check-spelling-bot Report

🔴 Please review

See the 📜action log for details.

Unrecognized words (3)

attemtpts
corewindow
curremt

Previously acknowledged words that are now absent CLA cpprest demoable DUMMYUNIONNAME everytime Hirots inthread ntsubauth NTWIN OPENIF OPENLINK PCLIENT PCOBJECT PCUNICODE PNTSTATUS pplx PPROCESS PUNICODE reingest unmark websockets :arrow_right:
To accept ✔️ these unrecognized words as correct and remove the previously acknowledged and now absent words, run the following commands

... in a clone of the git@github.com:microsoft/terminal.git repository
on the dev/migrie/selfhost/1.18 branch (ℹ️ how do I use this?):

curl -s -S -L 'https://raw.githubusercontent.com/check-spelling/check-spelling/v0.0.21/apply.pl' |
perl - 'https://github.com/microsoft/terminal/actions/runs/4392102472/attempts/1'
✏️ Contributor please read this

By default the command suggestion will generate a file named based on your commit. That's generally ok as long as you add the file to your commit. Someone can reorganize it later.

⚠️ The command is written for posix shells. If it doesn't work for you, you can manually add (one word per line) / remove items to expect.txt and the excludes.txt files.

If the listed items are:

  • ... misspelled, then please correct them instead of using the command.
  • ... names, please add them to .github/actions/spelling/allow/names.txt.
  • ... APIs, you can add them to a file in .github/actions/spelling/allow/.
  • ... just things you're using, please add them to an appropriate file in .github/actions/spelling/expect/.
  • ... tokens you only need in one place and shouldn't generally be used, you can add an item in an appropriate file in .github/actions/spelling/patterns/.

See the README.md in each directory for more information.

🔬 You can test your commits without appending to a PR by creating a new branch with that extra change and pushing it to your fork. The check-spelling action will run in response to your push -- it doesn't require an open pull request. By using such a branch, you can limit the number of typos your peers see you make. 😉

If the flagged items are 🤯 false positives

If items relate to a ...

  • binary file (or some other file you wouldn't want to check at all).

    Please add a file path to the excludes.txt file matching the containing file.

    File paths are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your files.

    ^ refers to the file's path from the root of the repository, so ^README\.md$ would exclude README.md (on whichever branch you're using).

  • well-formed pattern.

    If you can write a pattern that would match it,
    try adding it to the patterns.txt file.

    Patterns are Perl 5 Regular Expressions - you can test yours before committing to verify it will match your lines.

    Note that patterns can't match multiline strings.

Please sign in to comment.