Skip to content
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

Implement PickerHandler in WinUI #779

Merged
merged 11 commits into from
Apr 27, 2021
6 changes: 6 additions & 0 deletions src/Compatibility/Core/src/WinUI/PickerRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ void ControlOnOpenAnimationCompleted(object sender, EventArgs eventArgs)
}
}

[PortHandler]
void OnControlSelectionChanged(object sender, WSelectionChangedEventArgs e)
{
if (Element != null)
Expand Down Expand Up @@ -173,11 +174,13 @@ void StartAnimationRefresh()
});
}

[PortHandler]
void UpdateCharacterSpacing()
{
Control.CharacterSpacing = Element.CharacterSpacing.ToEm();
}

[PortHandler]
void UpdateFont()
{
if (Control == null)
Expand Down Expand Up @@ -211,17 +214,20 @@ void UpdateFont()
_fontApplied = true;
}

[PortHandler]
void UpdateSelectedIndex()
{
Control.SelectedIndex = Element.SelectedIndex;
}

[PortHandler]
void UpdateTextColor()
{
Color color = Element.TextColor;
Control.Foreground = color.IsDefault() ? (_defaultBrush ?? Maui.ColorExtensions.ToNative(color)) : Maui.ColorExtensions.ToNative(color);
}

[PortHandler]
void UpdateTitle()
{
Control.Header = null;
Expand Down
74 changes: 60 additions & 14 deletions src/Core/src/Handlers/Picker/PickerHandler.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
using System;
using Microsoft.UI.Xaml.Controls;
#nullable enable
using WBrush = Microsoft.UI.Xaml.Media.Brush;
using WSelectionChangedEventArgs = Microsoft.UI.Xaml.Controls.SelectionChangedEventArgs;

namespace Microsoft.Maui.Handlers
{
public partial class PickerHandler : ViewHandler<IPicker, ComboBox>
public partial class PickerHandler : ViewHandler<IPicker, MauiComboBox>
{
protected override ComboBox CreateNativeView() => new ComboBox();
WBrush? _defaultForeground;

[MissingMapper]
public static void MapTitle(PickerHandler handler, IPicker view) { }
protected override MauiComboBox CreateNativeView()
{
var nativePicker = new MauiComboBox();

if (VirtualView != null)
nativePicker.ItemsSource = ((LockableObservableListWrapper)VirtualView.Items)._list;

return nativePicker;
}

[MissingMapper]
public static void MapSelectedIndex(PickerHandler handler, IPicker view) { }
protected override void ConnectHandler(MauiComboBox nativeView)
{
nativeView.SelectionChanged += OnControlSelectionChanged;
}

[MissingMapper]
public static void MapCharacterSpacing(PickerHandler handler, IPicker view) { }
protected override void DisconnectHandler(MauiComboBox nativeView)
{
nativeView.SelectionChanged -= OnControlSelectionChanged;
}

[MissingMapper]
public static void MapFont(PickerHandler handler, IPicker view) { }
protected override void SetupDefaults(MauiComboBox nativeView)
{
_defaultForeground = nativeView.Foreground;

[MissingMapper]
public static void MapTextColor(PickerHandler handler, IPicker view) { }
base.SetupDefaults(nativeView);
}

public static void MapTitle(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateTitle(picker);
}

public static void MapSelectedIndex(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateSelectedIndex(picker);
}

public static void MapCharacterSpacing(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateCharacterSpacing(picker);
}

public static void MapFont(PickerHandler handler, IPicker picker)
{
var fontManager = handler.GetRequiredService<IFontManager>();

handler.NativeView?.UpdateFont(picker, fontManager);
}

public static void MapTextColor(PickerHandler handler, IPicker picker)
{
handler.NativeView?.UpdateTextColor(picker, handler._defaultForeground);
}

[MissingMapper]
public static void MapHorizontalTextAlignment(PickerHandler handler, IPicker view) { }

void OnControlSelectionChanged(object? sender, WSelectionChangedEventArgs e)
{
if (VirtualView != null && NativeView != null)
VirtualView.SelectedIndex = NativeView.SelectedIndex;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public static int ToEm(this double pt)
return Convert.ToInt32(pt * 0.0624f * 1000); // Coefficient for converting Pt to Em. The value is uniform spacing between characters, in units of 1/1000 of an em.
}
}
}
}
27 changes: 27 additions & 0 deletions src/Core/src/Platform/Windows/ColorConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using Microsoft.UI;
using WBrush = Microsoft.UI.Xaml.Media.Brush;
using WSolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;

namespace Microsoft.Maui
{
public sealed class ColorConverter : UI.Xaml.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var color = (Graphics.Color)value;
var defaultColorKey = (string)parameter;

WBrush defaultBrush = defaultColorKey != null ?
(WBrush)UI.Xaml.Application.Current.Resources[defaultColorKey] :
new WSolidColorBrush(Colors.Transparent);

return color.IsDefault() ? defaultBrush : color.ToNative();
}

public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
26 changes: 26 additions & 0 deletions src/Core/src/Platform/Windows/MauiCombobox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#nullable enable
using Microsoft.UI.Xaml.Controls;

namespace Microsoft.Maui
{
public class MauiComboBox : ComboBox
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File name should also be MauiComboBox.cs

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

{
public MauiComboBox()
{
DefaultStyleKey = typeof(MauiComboBox);

DropDownOpened += OnMauiComboBoxDropDownOpened;
SelectionChanged += OnMauiComboBoxSelectionChanged;
}

void OnMauiComboBoxDropDownOpened(object? sender, object e)
{
MinWidth = ActualWidth;
}

void OnMauiComboBoxSelectionChanged(object? sender, SelectionChangedEventArgs e)
{
MinWidth = 0;
}
}
}
43 changes: 43 additions & 0 deletions src/Core/src/Platform/Windows/PickerExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#nullable enable
using Microsoft.Maui.Graphics;
using Microsoft.UI.Xaml.Controls;
using WBrush = Microsoft.UI.Xaml.Media.Brush;

namespace Microsoft.Maui
{
public static class PickerExtensions
{
public static void UpdateTitle(this MauiComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.Header = null;

nativeComboBox.HeaderTemplate = string.IsNullOrEmpty(picker.Title) ? null :
(UI.Xaml.DataTemplate)UI.Xaml.Application.Current.Resources["ComboBoxHeader"];

nativeComboBox.DataContext = picker;
}
public static void UpdateTextColor(this MauiComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.UpdateTextColor(picker, null);
}

public static void UpdateTextColor(this MauiComboBox nativeComboBox, IPicker picker, WBrush? defaultForeground)
{
Color color = picker.TextColor;
nativeComboBox.Foreground = color.IsDefault() ? (defaultForeground ?? color.ToNative()) : color.ToNative();
}

public static void UpdateSelectedIndex(this MauiComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.SelectedIndex = picker.SelectedIndex;
}

public static void UpdateCharacterSpacing(this MauiComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.CharacterSpacing = picker.CharacterSpacing.ToEm();
}

public static void UpdateFont(this MauiComboBox nativeComboBox, IPicker picker, IFontManager fontManager) =>
nativeComboBox.UpdateFont(picker.Font, fontManager);
}
}
Loading