-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Highlight the search term in the options page #61301
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
Merged
davidwengier
merged 4 commits into
dotnet:main
from
davidwengier:OptionsPageHighlighting
Jun 30, 2022
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
527202d
Highlight the search term in the options page
davidwengier d48fc66
Merge remote-tracking branch 'upstream/main' into OptionsPageHighligh…
davidwengier 699ee2d
Add assert to ensure future controls are searchable
davidwengier 8d641c3
Merge branch 'main' into OptionsPageHighlighting
davidwengier File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
123 changes: 123 additions & 0 deletions
123
src/VisualStudio/Core/Impl/Options/OptionPageSearchHandler.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Windows; | ||
| using System; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Documents; | ||
| using System.Windows.Input; | ||
| using System.Windows.Media; | ||
|
|
||
| namespace Microsoft.VisualStudio.LanguageServices.Implementation.Options | ||
| { | ||
| internal class OptionPageSearchHandler | ||
| { | ||
| public static readonly Brush HighlightForeground = SystemColors.HighlightTextBrush; | ||
| public static readonly Brush HighlightBackground = SystemColors.HighlightBrush; | ||
|
|
||
| private readonly ContentControl _control; | ||
| private readonly string _originalContent; | ||
| private readonly int _accessKeyIndex; | ||
| private readonly string _content; | ||
|
|
||
| public OptionPageSearchHandler(ContentControl control, string originalContent) | ||
| { | ||
| _control = control; | ||
| _originalContent = originalContent; | ||
|
|
||
| // Currently only support one access key, and no underscores | ||
| Debug.Assert(_originalContent.Split('_').Length <= 2); | ||
|
|
||
| _accessKeyIndex = _originalContent.IndexOf('_'); | ||
|
|
||
| // We strip out the access key so it doesn't interupt search, and because we have to handle displaying it ourselves anyway. | ||
| // Since we strip it out, we also don't need to worry about the access key being the character after the underscore | ||
| _content = _originalContent.Replace("_", ""); | ||
| } | ||
|
|
||
| public bool TryHighlightSearchString(string searchTerm) | ||
| { | ||
| var index = _content.IndexOf(searchTerm, StringComparison.CurrentCultureIgnoreCase); | ||
| if (index == -1 || string.IsNullOrWhiteSpace(searchTerm)) | ||
| { | ||
| if (_accessKeyIndex != -1) | ||
| { | ||
| // Unregister and let the content control handle access keys | ||
| AccessKeyManager.Unregister(_content[_accessKeyIndex].ToString(), _control); | ||
| } | ||
|
|
||
| _control.Content = _originalContent; | ||
| return false; | ||
| } | ||
|
|
||
| if (_accessKeyIndex != -1) | ||
| { | ||
| // Because we are overriding the content entirely, we have to handle access keys | ||
| AccessKeyManager.Register(_content[_accessKeyIndex].ToString(), _control); | ||
| } | ||
|
|
||
| _control.Content = CreateHighlightingTextRun(index, searchTerm.Length); | ||
| return true; | ||
| } | ||
|
|
||
| public void EnsureVisible() | ||
| { | ||
| _control.BringIntoView(); | ||
| } | ||
|
|
||
| private TextBlock CreateHighlightingTextRun(int highlightStart, int length) | ||
| { | ||
| var textBlock = new TextBlock(); | ||
| AddTextRun(textBlock, 0, highlightStart, highlight: false); | ||
| AddTextRun(textBlock, highlightStart, length, highlight: true); | ||
|
|
||
| var highlightEnd = highlightStart + length; | ||
| AddTextRun(textBlock, highlightEnd, _content.Length - highlightEnd, highlight: false); | ||
|
|
||
| return textBlock; | ||
| } | ||
|
|
||
| private void AddTextRun(TextBlock textBlock, int start, int length, bool highlight) | ||
| { | ||
| if (length <= 0) | ||
| return; | ||
|
|
||
| // If the access key is in this run, then we actually need to add three runs | ||
| if (_accessKeyIndex >= start && _accessKeyIndex < start + length) | ||
| { | ||
| var firstPartLength = _accessKeyIndex - start; | ||
| var lastPartLength = length - firstPartLength - 1; | ||
|
|
||
| if (firstPartLength > 0) | ||
| textBlock.Inlines.Add(CreateRun(start, firstPartLength, highlight, underline: false)); | ||
|
|
||
| textBlock.Inlines.Add(CreateRun(_accessKeyIndex, 1, highlight, underline: true)); | ||
|
|
||
| if (lastPartLength > 0) | ||
| textBlock.Inlines.Add(CreateRun(_accessKeyIndex + 1, lastPartLength, highlight, underline: false)); | ||
| } | ||
| else | ||
| { | ||
| textBlock.Inlines.Add(CreateRun(start, length, highlight, underline: false)); | ||
| } | ||
| } | ||
|
|
||
| private Run CreateRun(int start, int length, bool highlight, bool underline) | ||
| { | ||
| var run = new Run(_content.Substring(start, length)); | ||
|
|
||
| if (highlight) | ||
| { | ||
| run.Background = HighlightBackground; | ||
| run.Foreground = HighlightForeground; | ||
| } | ||
|
|
||
| if (underline) | ||
| run.TextDecorations.Add(TextDecorations.Underline); | ||
|
|
||
| return run; | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.