|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Linq; |
| 8 | +using Microsoft.Toolkit.Uwp.UI; |
| 9 | +using Microsoft.Toolkit.Uwp.UI.Controls; |
| 10 | +using Windows.System; |
| 11 | +using Windows.UI; |
| 12 | +using Windows.UI.Text; |
| 13 | +using Windows.UI.Xaml; |
| 14 | +using Windows.UI.Xaml.Controls; |
| 15 | +using Windows.UI.Xaml.Controls.Primitives; |
| 16 | + |
| 17 | +namespace Microsoft.Toolkit.Uwp.SampleApp.SamplePages |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// An empty page that can be used on its own or navigated to within a Frame. |
| 21 | + /// </summary> |
| 22 | + public sealed partial class RichSuggestBoxPage : Page, IXamlRenderListener |
| 23 | + { |
| 24 | + private readonly List<SampleEmailDataType> _emailSamples = new List<SampleEmailDataType>() |
| 25 | + { |
| 26 | + new SampleEmailDataType() { FirstName = "Marcus", FamilyName = "Perryman" }, |
| 27 | + new SampleEmailDataType() { FirstName = "Michael", FamilyName = "Hawker" }, |
| 28 | + new SampleEmailDataType() { FirstName = "Matt", FamilyName = "Lacey" }, |
| 29 | + new SampleEmailDataType() { FirstName = "Alexandre", FamilyName = "Chohfi" }, |
| 30 | + new SampleEmailDataType() { FirstName = "Filip", FamilyName = "Wallberg" }, |
| 31 | + new SampleEmailDataType() { FirstName = "Shane", FamilyName = "Weaver" }, |
| 32 | + new SampleEmailDataType() { FirstName = "Vincent", FamilyName = "Gromfeld" }, |
| 33 | + new SampleEmailDataType() { FirstName = "Sergio", FamilyName = "Pedri" }, |
| 34 | + new SampleEmailDataType() { FirstName = "Alex", FamilyName = "Wilber" }, |
| 35 | + new SampleEmailDataType() { FirstName = "Allan", FamilyName = "Deyoung" }, |
| 36 | + new SampleEmailDataType() { FirstName = "Adele", FamilyName = "Vance" }, |
| 37 | + new SampleEmailDataType() { FirstName = "Grady", FamilyName = "Archie" }, |
| 38 | + new SampleEmailDataType() { FirstName = "Megan", FamilyName = "Bowen" }, |
| 39 | + new SampleEmailDataType() { FirstName = "Ben", FamilyName = "Walters" }, |
| 40 | + new SampleEmailDataType() { FirstName = "Debra", FamilyName = "Berger" }, |
| 41 | + new SampleEmailDataType() { FirstName = "Emily", FamilyName = "Braun" }, |
| 42 | + new SampleEmailDataType() { FirstName = "Christine", FamilyName = "Cline" }, |
| 43 | + new SampleEmailDataType() { FirstName = "Enrico", FamilyName = "Catteneo" }, |
| 44 | + new SampleEmailDataType() { FirstName = "Davit", FamilyName = "Badalyan" }, |
| 45 | + new SampleEmailDataType() { FirstName = "Diego", FamilyName = "Siciliani" }, |
| 46 | + new SampleEmailDataType() { FirstName = "Raul", FamilyName = "Razo" }, |
| 47 | + new SampleEmailDataType() { FirstName = "Miriam", FamilyName = "Graham" }, |
| 48 | + new SampleEmailDataType() { FirstName = "Lynne", FamilyName = "Robbins" }, |
| 49 | + new SampleEmailDataType() { FirstName = "Lydia", FamilyName = "Holloway" }, |
| 50 | + new SampleEmailDataType() { FirstName = "Nestor", FamilyName = "Wilke" }, |
| 51 | + new SampleEmailDataType() { FirstName = "Patti", FamilyName = "Fernandez" }, |
| 52 | + new SampleEmailDataType() { FirstName = "Pradeep", FamilyName = "Gupta" }, |
| 53 | + new SampleEmailDataType() { FirstName = "Joni", FamilyName = "Sherman" }, |
| 54 | + new SampleEmailDataType() { FirstName = "Isaiah", FamilyName = "Langer" }, |
| 55 | + new SampleEmailDataType() { FirstName = "Irvin", FamilyName = "Sayers" }, |
| 56 | + new SampleEmailDataType() { FirstName = "Tung", FamilyName = "Huynh" }, |
| 57 | + }; |
| 58 | + |
| 59 | + private readonly List<SampleDataType> _samples = new List<SampleDataType>() |
| 60 | + { |
| 61 | + new SampleDataType() { Text = "Account", Icon = Symbol.Account }, |
| 62 | + new SampleDataType() { Text = "Add Friend", Icon = Symbol.AddFriend }, |
| 63 | + new SampleDataType() { Text = "Attach", Icon = Symbol.Attach }, |
| 64 | + new SampleDataType() { Text = "Attach Camera", Icon = Symbol.AttachCamera }, |
| 65 | + new SampleDataType() { Text = "Audio", Icon = Symbol.Audio }, |
| 66 | + new SampleDataType() { Text = "Block Contact", Icon = Symbol.BlockContact }, |
| 67 | + new SampleDataType() { Text = "Calculator", Icon = Symbol.Calculator }, |
| 68 | + new SampleDataType() { Text = "Calendar", Icon = Symbol.Calendar }, |
| 69 | + new SampleDataType() { Text = "Camera", Icon = Symbol.Camera }, |
| 70 | + new SampleDataType() { Text = "Contact", Icon = Symbol.Contact }, |
| 71 | + new SampleDataType() { Text = "Favorite", Icon = Symbol.Favorite }, |
| 72 | + new SampleDataType() { Text = "Link", Icon = Symbol.Link }, |
| 73 | + new SampleDataType() { Text = "Mail", Icon = Symbol.Mail }, |
| 74 | + new SampleDataType() { Text = "Map", Icon = Symbol.Map }, |
| 75 | + new SampleDataType() { Text = "Phone", Icon = Symbol.Phone }, |
| 76 | + new SampleDataType() { Text = "Pin", Icon = Symbol.Pin }, |
| 77 | + new SampleDataType() { Text = "Rotate", Icon = Symbol.Rotate }, |
| 78 | + new SampleDataType() { Text = "Rotate Camera", Icon = Symbol.RotateCamera }, |
| 79 | + new SampleDataType() { Text = "Send", Icon = Symbol.Send }, |
| 80 | + new SampleDataType() { Text = "Tags", Icon = Symbol.Tag }, |
| 81 | + new SampleDataType() { Text = "UnFavorite", Icon = Symbol.UnFavorite }, |
| 82 | + new SampleDataType() { Text = "UnPin", Icon = Symbol.UnPin }, |
| 83 | + new SampleDataType() { Text = "Zoom", Icon = Symbol.Zoom }, |
| 84 | + new SampleDataType() { Text = "ZoomIn", Icon = Symbol.ZoomIn }, |
| 85 | + new SampleDataType() { Text = "ZoomOut", Icon = Symbol.ZoomOut }, |
| 86 | + }; |
| 87 | + |
| 88 | + private RichSuggestBox _rsb; |
| 89 | + private RichSuggestBox _tsb; |
| 90 | + private DispatcherQueue _dispatcherQueue; |
| 91 | + |
| 92 | + public RichSuggestBoxPage() |
| 93 | + { |
| 94 | + this.InitializeComponent(); |
| 95 | + this._dispatcherQueue = DispatcherQueue.GetForCurrentThread(); |
| 96 | + Loaded += (sender, e) => { this.OnXamlRendered(this); }; |
| 97 | + } |
| 98 | + |
| 99 | + public void OnXamlRendered(FrameworkElement control) |
| 100 | + { |
| 101 | + if (this._rsb != null) |
| 102 | + { |
| 103 | + this._rsb.SuggestionChosen -= this.SuggestingBox_OnSuggestionChosen; |
| 104 | + this._rsb.SuggestionRequested -= this.SuggestingBox_OnSuggestionRequested; |
| 105 | + } |
| 106 | + |
| 107 | + if (this._tsb != null) |
| 108 | + { |
| 109 | + this._tsb.SuggestionChosen -= this.SuggestingBox_OnSuggestionChosen; |
| 110 | + this._tsb.SuggestionRequested -= this.SuggestingBox_OnSuggestionRequested; |
| 111 | + this._tsb.TokenPointerOver -= this.SuggestingBox_OnTokenPointerOver; |
| 112 | + } |
| 113 | + |
| 114 | + if (control.FindChild("SuggestingBox") is RichSuggestBox rsb) |
| 115 | + { |
| 116 | + this._rsb = rsb; |
| 117 | + this._rsb.SuggestionChosen += this.SuggestingBox_OnSuggestionChosen; |
| 118 | + this._rsb.SuggestionRequested += this.SuggestingBox_OnSuggestionRequested; |
| 119 | + } |
| 120 | + |
| 121 | + if (control.FindChild("PlainTextSuggestingBox") is RichSuggestBox tsb) |
| 122 | + { |
| 123 | + this._tsb = tsb; |
| 124 | + this._tsb.SuggestionChosen += this.SuggestingBox_OnSuggestionChosen; |
| 125 | + this._tsb.SuggestionRequested += this.SuggestingBox_OnSuggestionRequested; |
| 126 | + this._tsb.TokenPointerOver += this.SuggestingBox_OnTokenPointerOver; |
| 127 | + } |
| 128 | + |
| 129 | + if (control.FindChild("TokenListView1") is ListView tls1) |
| 130 | + { |
| 131 | + tls1.ItemsSource = this._rsb?.Tokens; |
| 132 | + } |
| 133 | + |
| 134 | + if (control.FindChild("TokenListView2") is ListView tls2) |
| 135 | + { |
| 136 | + tls2.ItemsSource = this._tsb?.Tokens; |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + private void SuggestingBox_OnTokenPointerOver(RichSuggestBox sender, RichSuggestTokenPointerOverEventArgs args) |
| 141 | + { |
| 142 | + var flyout = (Flyout)FlyoutBase.GetAttachedFlyout(sender); |
| 143 | + var pointerPosition = args.CurrentPoint.Position; |
| 144 | + |
| 145 | + if (flyout?.Content is ContentPresenter cp && sender.TextDocument.Selection.Type != SelectionType.Normal && |
| 146 | + (!flyout.IsOpen || cp.Content != args.Token.Item)) |
| 147 | + { |
| 148 | + this._dispatcherQueue.TryEnqueue(() => |
| 149 | + { |
| 150 | + cp.Content = args.Token.Item; |
| 151 | + flyout.ShowAt(sender, new FlyoutShowOptions |
| 152 | + { |
| 153 | + Position = pointerPosition, |
| 154 | + ExclusionRect = sender.GetRectFromRange(args.Range), |
| 155 | + ShowMode = FlyoutShowMode.TransientWithDismissOnPointerMoveAway, |
| 156 | + }); |
| 157 | + }); |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + private void SuggestingBox_OnSuggestionChosen(RichSuggestBox sender, SuggestionChosenEventArgs args) |
| 162 | + { |
| 163 | + if (args.Prefix == "#") |
| 164 | + { |
| 165 | + args.Format.BackgroundColor = Colors.DarkOrange; |
| 166 | + args.Format.ForegroundColor = Colors.OrangeRed; |
| 167 | + args.Format.Bold = FormatEffect.On; |
| 168 | + args.Format.Italic = FormatEffect.On; |
| 169 | + args.DisplayText = ((SampleDataType)args.SelectedItem).Text; |
| 170 | + } |
| 171 | + else |
| 172 | + { |
| 173 | + args.DisplayText = ((SampleEmailDataType)args.SelectedItem).DisplayName; |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private void SuggestingBox_OnSuggestionRequested(RichSuggestBox sender, SuggestionRequestedEventArgs args) |
| 178 | + { |
| 179 | + if (args.Prefix == "#") |
| 180 | + { |
| 181 | + sender.ItemsSource = |
| 182 | + this._samples.Where(x => x.Text.Contains(args.QueryText, StringComparison.OrdinalIgnoreCase)); |
| 183 | + } |
| 184 | + else |
| 185 | + { |
| 186 | + sender.ItemsSource = |
| 187 | + this._emailSamples.Where(x => x.DisplayName.Contains(args.QueryText, StringComparison.OrdinalIgnoreCase)); |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments