|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +#include "pch.h" |
| 5 | +#include "FuzzySearchPane.h" |
| 6 | + |
| 7 | +using namespace winrt::Windows::Foundation; |
| 8 | +using namespace winrt::Windows::UI::Xaml; |
| 9 | +using namespace winrt::Microsoft::Terminal::Settings::Model; |
| 10 | + |
| 11 | +namespace winrt::TerminalApp::implementation |
| 12 | +{ |
| 13 | + FuzzySearchPane::FuzzySearchPane(const winrt::Microsoft::Terminal::Control::TermControl& control) |
| 14 | + { |
| 15 | + _control = control; |
| 16 | + _root = winrt::Windows::UI::Xaml::Controls::Grid{}; |
| 17 | + // Vertical and HorizontalAlignment are Stretch by default |
| 18 | + |
| 19 | + auto res = Windows::UI::Xaml::Application::Current().Resources(); |
| 20 | + auto bg = res.Lookup(winrt::box_value(L"UnfocusedBorderBrush")); |
| 21 | + _root.Background(bg.try_as<Media::Brush>()); |
| 22 | + |
| 23 | + const Controls::RowDefinition row1; |
| 24 | + row1.Height(GridLengthHelper::FromValueAndType(1, GridUnitType::Star)); |
| 25 | + _root.RowDefinitions().Append(row1); |
| 26 | + |
| 27 | + const Controls::RowDefinition row2; |
| 28 | + row2.Height(GridLengthHelper::Auto()); |
| 29 | + _root.RowDefinitions().Append(row2); |
| 30 | + |
| 31 | + _listBox = Controls::ListBox{}; |
| 32 | + _listBox.Margin({ 10, 10, 10, 10 }); |
| 33 | + _root.Children().Append(_listBox); |
| 34 | + |
| 35 | + _searchBox = Controls::TextBox{}; |
| 36 | + _root.Children().Append(_searchBox); |
| 37 | + |
| 38 | + _searchBox.TextChanged({ this, &FuzzySearchPane::OnTextChanged }); |
| 39 | + _searchBox.KeyDown({ this, &FuzzySearchPane::OnKeyUp }); |
| 40 | + |
| 41 | + Controls::Grid::SetRow(_listBox, 0); |
| 42 | + Controls::Grid::SetRow(_searchBox, 1); |
| 43 | + } |
| 44 | + |
| 45 | + void FuzzySearchPane::OnKeyUp(Windows::Foundation::IInspectable const&, Input::KeyRoutedEventArgs const& e) |
| 46 | + { |
| 47 | + if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Down || e.OriginalKey() == winrt::Windows::System::VirtualKey::Up) |
| 48 | + { |
| 49 | + auto selectedIndex = _listBox.SelectedIndex(); |
| 50 | + |
| 51 | + if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Down) |
| 52 | + { |
| 53 | + selectedIndex++; |
| 54 | + } |
| 55 | + else if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Up) |
| 56 | + { |
| 57 | + selectedIndex--; |
| 58 | + } |
| 59 | + |
| 60 | + if (selectedIndex >= 0 && selectedIndex < static_cast<int32_t>(_listBox.Items().Size())) |
| 61 | + { |
| 62 | + _listBox.SelectedIndex(selectedIndex); |
| 63 | + _listBox.ScrollIntoView(Controls::ListBox().SelectedItem()); |
| 64 | + } |
| 65 | + |
| 66 | + e.Handled(true); |
| 67 | + } |
| 68 | + else if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Enter) |
| 69 | + { |
| 70 | + if (const auto selectedItem = _listBox.SelectedItem()) |
| 71 | + { |
| 72 | + if (const auto listBoxItem = selectedItem.try_as<Controls::ListBoxItem>()) |
| 73 | + { |
| 74 | + if (const auto fuzzyMatch = listBoxItem.DataContext().try_as<winrt::Microsoft::Terminal::Control::FuzzySearchTextLine>()) |
| 75 | + { |
| 76 | + _control.SelectChar(fuzzyMatch.FirstPosition()); |
| 77 | + _control.Focus(FocusState::Programmatic); |
| 78 | + e.Handled(true); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + else if (e.OriginalKey() == winrt::Windows::System::VirtualKey::Escape) |
| 84 | + { |
| 85 | + Close(); |
| 86 | + e.Handled(true); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + void FuzzySearchPane::OnTextChanged(Windows::Foundation::IInspectable const&, Controls::TextChangedEventArgs const&) const |
| 91 | + { |
| 92 | + _listBox.Items().Clear(); |
| 93 | + const auto needle = _searchBox.Text(); |
| 94 | + const auto fuzzySearchResult = _control.FuzzySearch(needle); |
| 95 | + if (fuzzySearchResult.NumberOfResults() > 0) |
| 96 | + { |
| 97 | + for (auto fuzzyMatch : fuzzySearchResult.Results()) |
| 98 | + { |
| 99 | + auto control = Controls::TextBlock{}; |
| 100 | + const auto inlinesCollection = control.Inlines(); |
| 101 | + inlinesCollection.Clear(); |
| 102 | + |
| 103 | + for (const auto& match : fuzzyMatch.Segments()) |
| 104 | + { |
| 105 | + const auto matchText = match.TextSegment(); |
| 106 | + const auto fontWeight = match.IsHighlighted() ? Windows::UI::Text::FontWeights::Bold() : Windows::UI::Text::FontWeights::Normal(); |
| 107 | + |
| 108 | + Media::SolidColorBrush foregroundBrush; |
| 109 | + |
| 110 | + if (match.IsHighlighted()) |
| 111 | + { |
| 112 | + foregroundBrush.Color(Windows::UI::Colors::OrangeRed()); |
| 113 | + } |
| 114 | + else |
| 115 | + { |
| 116 | + foregroundBrush.Color(Windows::UI::Colors::White()); |
| 117 | + } |
| 118 | + |
| 119 | + Documents::Run run; |
| 120 | + run.Text(matchText); |
| 121 | + run.FontWeight(fontWeight); |
| 122 | + run.Foreground(foregroundBrush); |
| 123 | + inlinesCollection.Append(run); |
| 124 | + } |
| 125 | + auto lbi = Controls::ListBoxItem(); |
| 126 | + lbi.DataContext(fuzzyMatch); |
| 127 | + lbi.Content(control); |
| 128 | + _listBox.Items().Append(lbi); |
| 129 | + } |
| 130 | + _listBox.SelectedIndex(0); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + void FuzzySearchPane::UpdateSettings(const CascadiaSettings& /*settings*/) |
| 135 | + { |
| 136 | + // Nothing to do. |
| 137 | + } |
| 138 | + |
| 139 | + winrt::Windows::UI::Xaml::FrameworkElement FuzzySearchPane::GetRoot() |
| 140 | + { |
| 141 | + return _root; |
| 142 | + } |
| 143 | + winrt::Windows::Foundation::Size FuzzySearchPane::MinimumSize() |
| 144 | + { |
| 145 | + return { 1, 1 }; |
| 146 | + } |
| 147 | + void FuzzySearchPane::Focus(winrt::Windows::UI::Xaml::FocusState reason) |
| 148 | + { |
| 149 | + _searchBox.Focus(reason); |
| 150 | + } |
| 151 | + void FuzzySearchPane::Close() |
| 152 | + { |
| 153 | + CloseRequested.raise(*this, nullptr); |
| 154 | + } |
| 155 | + |
| 156 | + INewContentArgs FuzzySearchPane::GetNewTerminalArgs(const BuildStartupKind /* kind */) const |
| 157 | + { |
| 158 | + return BaseContentArgs(L"scratchpad"); |
| 159 | + } |
| 160 | + |
| 161 | + winrt::hstring FuzzySearchPane::Icon() const |
| 162 | + { |
| 163 | + static constexpr std::wstring_view glyph{ L"\xe70b" }; // QuickNote |
| 164 | + return winrt::hstring{ glyph }; |
| 165 | + } |
| 166 | + |
| 167 | + winrt::Windows::UI::Xaml::Media::Brush FuzzySearchPane::BackgroundBrush() |
| 168 | + { |
| 169 | + return _root.Background(); |
| 170 | + } |
| 171 | +} |
0 commit comments