forked from CommunityToolkit/Graph-Controls
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Needs artifacts from unoplatform/Uno.WindowsCommunityToolkit#59 Don't forget to add ClientID :)
- Loading branch information
1 parent
dd0aeb4
commit c4e31cf
Showing
10 changed files
with
231 additions
and
9 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.Properties.cs
This file contains 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,53 @@ | ||
// 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.Collections.ObjectModel; | ||
using Microsoft.Graph; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
using Windows.UI.Xaml; | ||
|
||
namespace Microsoft.Toolkit.Graph.Controls | ||
{ | ||
/// <summary> | ||
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>. | ||
/// </summary> | ||
public partial class PeoplePicker | ||
{ | ||
/// <summary> | ||
/// Gets the set of <see cref="Person"/> objects chosen by the user. | ||
/// </summary> | ||
public ObservableCollection<Person> PickedPeople | ||
{ | ||
get { return (ObservableCollection<Person>)GetValue(SelectedPeopleProperty); } | ||
internal set { SetValue(SelectedPeopleProperty, value); } | ||
} | ||
|
||
/// <summary> | ||
/// Identifies the <see cref="PickedPeople"/> dependency property. | ||
/// </summary> | ||
/// <returns> | ||
/// The identifier for the <see cref="PickedPeople"/> dependency property. | ||
/// </returns> | ||
public static readonly DependencyProperty SelectedPeopleProperty = | ||
DependencyProperty.Register(nameof(PickedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>())); | ||
|
||
/// <summary> | ||
/// Gets or sets collection of people suggested by the graph from the user's query. | ||
/// </summary> | ||
public ObservableCollection<Person> SuggestedPeople | ||
{ | ||
get { return (ObservableCollection<Person>)GetValue(SuggestedPeopleProperty); } | ||
set { SetValue(SuggestedPeopleProperty, value); } | ||
} | ||
|
||
/// <summary> | ||
/// Identifies the <see cref="SuggestedPeople"/> dependency property. | ||
/// </summary> | ||
/// <returns> | ||
/// The identifier for the <see cref="SuggestedPeople"/> dependency property. | ||
/// </returns> | ||
public static readonly DependencyProperty SuggestedPeopleProperty = | ||
DependencyProperty.Register(nameof(SuggestedPeople), typeof(ObservableCollection<Person>), typeof(PeoplePicker), new PropertyMetadata(new ObservableCollection<Person>())); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.cs
This file contains 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,114 @@ | ||
// 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; | ||
using Microsoft.Graph; | ||
using Microsoft.Toolkit.Graph.Extensions; | ||
using Microsoft.Toolkit.Graph.Providers; | ||
using Microsoft.Toolkit.Uwp.UI.Controls; | ||
using Microsoft.Toolkit.Uwp.UI.Extensions; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.Toolkit.Graph.Controls | ||
{ | ||
/// <summary> | ||
/// Control which allows user to search for a person or contact within Microsoft Graph. Built on top of <see cref="TokenizingTextBox"/>. | ||
/// </summary> | ||
[TemplatePart(Name = PeoplePickerTokenizingTextBoxName, Type = typeof(TokenizingTextBox))] | ||
public partial class PeoplePicker : Control | ||
{ | ||
private const string PeoplePickerTokenizingTextBoxName = "PART_TokenizingTextBox"; | ||
|
||
private TokenizingTextBox _tokenBox; | ||
|
||
private DispatcherTimer _typeTimer = new DispatcherTimer(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="PeoplePicker"/> class. | ||
/// </summary> | ||
public PeoplePicker() | ||
{ | ||
this.DefaultStyleKey = typeof(PeoplePicker); | ||
} | ||
|
||
/// <inheritdoc/> | ||
protected override void OnApplyTemplate() | ||
{ | ||
base.OnApplyTemplate(); | ||
|
||
if (_tokenBox != null) | ||
{ | ||
_tokenBox.QueryTextChanged -= TokenBox_QueryTextChanged; | ||
_tokenBox.TokenItemAdded -= TokenBox_TokenItemAdded; | ||
_tokenBox.TokenItemRemoved -= TokenBox_TokenItemRemoved; | ||
} | ||
|
||
_tokenBox = GetTemplateChild(PeoplePickerTokenizingTextBoxName) as TokenizingTextBox; | ||
|
||
if (_tokenBox != null) | ||
{ | ||
_tokenBox.QueryTextChanged += TokenBox_QueryTextChanged; | ||
_tokenBox.TokenItemAdded += TokenBox_TokenItemAdded; | ||
_tokenBox.TokenItemRemoved += TokenBox_TokenItemRemoved; | ||
} | ||
} | ||
|
||
private void TokenBox_TokenItemAdded(TokenizingTextBox sender, TokenizingTextBoxItem args) | ||
{ | ||
if (args?.Content is Person person) | ||
{ | ||
PickedPeople.Add(person); | ||
} | ||
} | ||
|
||
private void TokenBox_TokenItemRemoved(TokenizingTextBox sender, TokenItemRemovedEventArgs args) | ||
{ | ||
PickedPeople.Remove(args.Item as Person); | ||
} | ||
|
||
private string _previousQuery; | ||
|
||
private void TokenBox_QueryTextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args) | ||
{ | ||
#if !HAS_UNO | ||
if (!args.CheckCurrent()) | ||
{ | ||
return; | ||
} | ||
#endif | ||
|
||
if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput) | ||
{ | ||
var text = sender.Text; | ||
_typeTimer.Debounce( | ||
async () => | ||
{ | ||
var graph = ProviderManager.Instance.GlobalProvider.Graph; | ||
if (graph != null) | ||
{ | ||
// If empty, clear out | ||
if (string.IsNullOrWhiteSpace(text)) | ||
{ | ||
SuggestedPeople.Clear(); | ||
} | ||
else | ||
{ | ||
SuggestedPeople.Clear(); | ||
|
||
foreach (var contact in (await graph.FindPersonAsync(text)).CurrentPage) | ||
{ | ||
SuggestedPeople.Add(contact); | ||
} | ||
} | ||
|
||
_previousQuery = text; | ||
} | ||
|
||
// TODO: If we don't have Graph connection and just list of Person should we auto-filter here? | ||
}, TimeSpan.FromSeconds(0.3)); | ||
} | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
Microsoft.Toolkit.Graph.Controls/Controls/PeoplePicker/PeoplePicker.xaml
This file contains 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,37 @@ | ||
<ResourceDictionary | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:Microsoft.Toolkit.Graph.Controls" | ||
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"> | ||
|
||
<Style TargetType="local:PeoplePicker"> | ||
<Setter Property="Template"> | ||
<Setter.Value> | ||
<ControlTemplate TargetType="local:PeoplePicker"> | ||
<Border | ||
Background="{TemplateBinding Background}" | ||
BorderBrush="{TemplateBinding BorderBrush}" | ||
BorderThickness="{TemplateBinding BorderThickness}"> | ||
<controls:TokenizingTextBox | ||
x:Name="PART_TokenizingTextBox" | ||
QueryIcon="Find" | ||
PlaceholderText="Start typing a name" | ||
TokenDelimiter=" " | ||
SuggestedItemsSource="{TemplateBinding SuggestedPeople}"> | ||
<controls:TokenizingTextBox.SuggestedItemTemplate> | ||
<DataTemplate> | ||
<local:PersonView PersonDetails="{Binding}" ShowName="True" ShowEmail="True"/> | ||
</DataTemplate> | ||
</controls:TokenizingTextBox.SuggestedItemTemplate> | ||
<controls:TokenizingTextBox.TokenItemTemplate> | ||
<DataTemplate> | ||
<local:PersonView PersonDetails="{Binding}" ShowName="True"/> | ||
</DataTemplate> | ||
</controls:TokenizingTextBox.TokenItemTemplate> | ||
</controls:TokenizingTextBox> | ||
</Border> | ||
</ControlTemplate> | ||
</Setter.Value> | ||
</Setter> | ||
</Style> | ||
</ResourceDictionary> |
This file contains 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 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 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 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 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 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 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