Skip to content

Commit

Permalink
Improve windows behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
dotMorten committed Oct 19, 2023
1 parent e3f168e commit fedca7f
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions AutoSuggestBox/AutoSuggestBoxHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ namespace dotMorten.Maui.Handlers;
/// </summary>
public class AutoSuggestBoxHandler : ViewHandler<IAutoSuggestBox, NativeAutoSuggestBox>
{
#if !WINDOWS
private bool suppressTextChangedEvent;
#endif

/// <summary>
/// Property mapper for the <see cref="AutoSuggestBox"/> control.
/// </summary>
Expand Down Expand Up @@ -79,7 +75,8 @@ protected override void ConnectHandler(NativeAutoSuggestBox platformView)
platformView.EditingDidBegin += Control_EditingDidBegin;
platformView.EditingDidEnd += Control_EditingDidEnd;
#elif WINDOWS
platformView.GotFocus += Control_GotFocus;
platformView.Loaded += PlatformView_Loaded;
platformView.GotFocus += Control_GotFocus;
#endif
}

Expand All @@ -94,10 +91,20 @@ protected override void DisconnectHandler(NativeAutoSuggestBox platformView)
platformView.EditingDidBegin -= Control_EditingDidBegin;
platformView.EditingDidEnd -= Control_EditingDidEnd;
#elif WINDOWS
platformView.GotFocus -= Control_GotFocus;
platformView.Loaded -= PlatformView_Loaded;
platformView.GotFocus -= Control_GotFocus;
#endif
}

#if WINDOWS
private void PlatformView_Loaded(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
// Workaround issue in WinUI where the list doesn't open if you set before load
if(VirtualView.IsSuggestionListOpen)
(sender as NativeAutoSuggestBox).IsSuggestionListOpen = true;
}
#endif

#if __IOS__
private void Control_EditingDidBegin(object sender, EventArgs e)
{
Expand All @@ -108,7 +115,7 @@ private void Control_EditingDidEnd(object sender, EventArgs e)
(VirtualView as VisualElement)?.SetValue(VisualElement.IsFocusedPropertyKey, false);
}
#elif WINDOWS
private void Control_GotFocus(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
private void Control_GotFocus(object sender, Microsoft.UI.Xaml.RoutedEventArgs e)
{
if (VirtualView?.ItemsSource?.Count > 0)
(sender as NativeAutoSuggestBox).IsSuggestionListOpen = true;
Expand All @@ -125,11 +132,9 @@ private void Control_GotFocus(object sender, Microsoft.UI.Xaml.RoutedEventArgs e
protected override NativeAutoSuggestBox CreatePlatformView()
{
#if __ANDROID__
return new Platform.Android.AndroidAutoSuggestBox(this.Context);
#elif __IOS__
return new Platform.iOS.iOSAutoSuggestBox();
#elif WINDOWS
return new Microsoft.UI.Xaml.Controls.AutoSuggestBox();
return new NativeAutoSuggestBox(this.Context);
#elif __IOS__ || WINDOWS
return new NativeAutoSuggestBox();
#else
throw new NotImplementedException();
#endif
Expand Down Expand Up @@ -219,16 +224,16 @@ public static void MapDisplayMemberPath(AutoSuggestBoxHandler handler, IAutoSugg
}

/// <summary>
/// Maps the <see cref="IAutoSuggestBox.IsEnabled"/> property to the native AutoSuggestBox control.
/// Maps the <see cref="IView.IsEnabled"/> property to the native AutoSuggestBox control.
/// </summary>
/// <param name="handler">View handler</param>
/// <param name="autoSuggestBox">IAutoSuggestBox instance</param>
public static void MapIsEnabled(AutoSuggestBoxHandler handler, IAutoSuggestBox autoSuggestBox)
{
#if WINDOWS
handler.PlatformView.IsEnabled = autoSuggestBox.IsEnabled;
handler.PlatformView.IsEnabled = autoSuggestBox.IsEnabled;
#elif __ANDROID__
handler.PlatformView.Enabled = autoSuggestBox.IsEnabled;
handler.PlatformView.Enabled = autoSuggestBox.IsEnabled;
#elif __IOS__
handler.PlatformView.UserInteractionEnabled = autoSuggestBox.IsEnabled;
#endif
Expand All @@ -254,7 +259,10 @@ public static void MapUpdateTextOnSelect(AutoSuggestBoxHandler handler, IAutoSug
public static void MapIsSuggestionListOpen(AutoSuggestBoxHandler handler, IAutoSuggestBox autoSuggestBox)
{
#if WINDOWS || __ANDROID__ || __IOS__
handler.PlatformView.IsSuggestionListOpen = autoSuggestBox.IsSuggestionListOpen;
#if WINDOWS
if (handler.PlatformView.IsLoaded) // Delay until load
#endif
handler.PlatformView.IsSuggestionListOpen = autoSuggestBox.IsSuggestionListOpen;
#endif
}

Expand Down

0 comments on commit fedca7f

Please sign in to comment.