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

Dismiss the snippet preview when there is no suggestions #17777

Merged
merged 3 commits into from
Aug 23, 2024
Merged
Changes from all commits
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
42 changes: 38 additions & 4 deletions src/cascadia/TerminalApp/SuggestionsControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ namespace winrt::TerminalApp::implementation
const auto selectedCommand = _filteredActionsView().SelectedItem();
const auto filteredCommand{ selectedCommand.try_as<winrt::TerminalApp::FilteredCommand>() };

_filteredActionsView().ScrollIntoView(selectedCommand);

PropertyChanged.raise(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs{ L"SelectedItem" });

// Make sure to not send the preview if we're collapsed. This can
Expand Down Expand Up @@ -795,14 +797,46 @@ namespace winrt::TerminalApp::implementation
// here will ensure that we can check this case appropriately.
_lastFilterTextWasEmpty = _searchBox().Text().empty();

const auto lastSelectedIndex = _filteredActionsView().SelectedIndex();
const auto lastSelectedIndex = std::max(0, _filteredActionsView().SelectedIndex()); // SelectedIndex will return -1 for "nothing"

_updateFilteredActions();

// In the command line mode we want the user to explicitly select the command
_filteredActionsView().SelectedIndex(std::min<int32_t>(lastSelectedIndex, _filteredActionsView().Items().Size() - 1));
if (const auto newSelectedIndex = _filteredActionsView().SelectedIndex();
newSelectedIndex == -1)
{
// Make sure something stays selected
_scrollToIndex(lastSelectedIndex);
}
else
{
// BODGY: Calling ScrollIntoView on a ListView doesn't always work
// immediately after a change to the items. See:
// https://stackoverflow.com/questions/16942580/why-doesnt-listview-scrollintoview-ever-work
// The SelectionChanged thing we do (in _selectedCommandChanged),
// but because we're also not changing the actual selected item when
// the size of the list grows (it _stays_ selected, so it never
// _changes_), we never get a SelectionChanged.
//
// To mitigate, only in the case of totally clearing out the filter
// (like hitting `esc`), we want to briefly select the 0th item,
// then immediately select the one we want to make visible. That
// will make sure we get a SelectionChanged when the ListView is
// ready, and we can use that to scroll to the right item.
//
// If we do this on _every_ change, then the preview text flickers
// between the 0th item and the correct one.
if (_lastFilterTextWasEmpty)
{
_filteredActionsView().SelectedIndex(0);
}
_scrollToIndex(newSelectedIndex);
}

const auto currentNeedleHasResults{ _filteredActions.Size() > 0 };
if (!currentNeedleHasResults)
{
PreviewAction.raise(*this, nullptr);
}
_noMatchesText().Visibility(currentNeedleHasResults ? Visibility::Collapsed : Visibility::Visible);
if (auto automationPeer{ Automation::Peers::FrameworkElementAutomationPeer::FromElement(_searchBox()) })
{
Expand Down Expand Up @@ -1203,7 +1237,7 @@ namespace winrt::TerminalApp::implementation
if (_direction == TerminalApp::SuggestionsDirection::BottomUp)
{
const auto last = _filteredActionsView().Items().Size() - 1;
_filteredActionsView().SelectedIndex(last);
_scrollToIndex(last);
}
// Move the cursor to the very last position, so it starts immediately
// after the text. This is apparently done by starting a 0-wide
Expand Down
Loading