Skip to content

Commit

Permalink
Fix manually tested issue where initial character press of overwritin…
Browse files Browse the repository at this point in the history
…g token wasn't being set to box (aggressive if)

Also realized we would no longer raise the text changed event in that scenario, so changed logic for text changed event.
We'll need to create integration tests for these keyboard driven scenarios in the new test setup from Labs (when it's finished) in 8.0. These would make good keyboard driver tests.
  • Loading branch information
michael-hawker committed Oct 17, 2022
1 parent 070724e commit 637ba21
Showing 1 changed file with 28 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,31 +101,36 @@ private void OnApplyTemplateAutoSuggestBox(AutoSuggestBox auto)
_autoSuggestBox.LostFocus += AutoSuggestBox_LostFocus;

// Setup a binding to the QueryIcon of the Parent if we're the last box.
if (Content is ITokenStringContainer str && str.IsLast)
if (Content is ITokenStringContainer str)
{
// Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
if (Owner.QueryIcon is FontIconSource fis &&
fis.ReadLocalValue(FontIconSource.FontSizeProperty) == DependencyProperty.UnsetValue)
{
// This can be expensive, could we optimize?
// Also, this is changing the FontSize on the IconSource (which could be shared?)
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
}
// We need to set our initial text in all cases.
_autoSuggestBox.Text = str.Text;

var iconBinding = new Binding()
// We only set/bind some properties on the last textbox to mimic the autosuggestbox look
if (str.IsLast)
{
Source = Owner,
Path = new PropertyPath(nameof(Owner.QueryIcon)),
RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.TemplatedParent }
};
// Workaround for https://github.com/microsoft/microsoft-ui-xaml/issues/2568
if (Owner.QueryIcon is FontIconSource fis &&
fis.ReadLocalValue(FontIconSource.FontSizeProperty) == DependencyProperty.UnsetValue)
{
// This can be expensive, could we optimize?
// Also, this is changing the FontSize on the IconSource (which could be shared?)
fis.FontSize = Owner.TryFindResource("TokenizingTextBoxIconFontSize") as double? ?? 16;
}

var iconSourceElement = new IconSourceElement();
var iconBinding = new Binding()
{
Source = Owner,
Path = new PropertyPath(nameof(Owner.QueryIcon)),
RelativeSource = new RelativeSource() { Mode = RelativeSourceMode.TemplatedParent }
};

iconSourceElement.SetBinding(IconSourceElement.IconSourceProperty, iconBinding);
var iconSourceElement = new IconSourceElement();

_autoSuggestBox.QueryIcon = iconSourceElement;
iconSourceElement.SetBinding(IconSourceElement.IconSourceProperty, iconBinding);

_autoSuggestBox.Text = str.Text;
_autoSuggestBox.QueryIcon = iconSourceElement;
}
}
}
}
Expand Down Expand Up @@ -184,19 +189,11 @@ void WaitForLoad(object s, RoutedEventArgs eargs)

private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
var hasDelimiter = !string.IsNullOrEmpty(Owner.TokenDelimiter) && sender.Text?.Contains(Owner.TokenDelimiter) == true;

// Ignore in the case we've been set from the parent and already equal the owning text,
// unless we contain our delimiter.
if (!hasDelimiter && EqualityComparer<string>.Default.Equals(sender.Text, Owner.Text))
if (!EqualityComparer<string>.Default.Equals(sender.Text, Owner.Text))
{
return;
Owner.Text = sender.Text; // Update parent text property, if different
}

var t = sender.Text.Trim();

Owner.Text = sender.Text; // Update parent text property

// Override our programmatic manipulation as we're redirecting input for the user
if (UseCharacterAsUser)
{
Expand All @@ -207,8 +204,10 @@ private void AutoSuggestBox_TextChanged(AutoSuggestBox sender, AutoSuggestBoxTex

Owner.RaiseTextChanged(sender, args);

var t = sender.Text?.Trim() ?? string.Empty;

// Look for Token Delimiters to create new tokens when text changes.
if (hasDelimiter)
if (!string.IsNullOrEmpty(Owner.TokenDelimiter) && t.Contains(Owner.TokenDelimiter))
{
bool lastDelimited = t[t.Length - 1] == Owner.TokenDelimiter[0];

Expand Down

0 comments on commit 637ba21

Please sign in to comment.