Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions src/Controls/tests/DeviceTests/Elements/Picker/PickerTests.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using Microsoft.Maui.Graphics;
using Microsoft.Maui.Handlers;
using Microsoft.Maui.Platform;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Xunit;
using WSolidColorBrush = Microsoft.UI.Xaml.Media.SolidColorBrush;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -56,6 +58,179 @@ public async Task VerticalOptionsInitializesCorrectly()
await InvokeOnMainThreadAsync(() => Assert.Equal(UI.Xaml.VerticalAlignment.Bottom, GetPlatformVerticalOptions(handler.PlatformView)));
}

[Fact(DisplayName = "Title maps to PlaceholderText")]
public async Task TitleMapsToPlaceholderText()
{
var picker = new Picker()
{
Title = "Select Option",
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
Assert.Equal("Select Option", handler.PlatformView.PlaceholderText);
});
}

[Fact(DisplayName = "Null title maps to empty PlaceholderText")]
public async Task NullTitleMapsToEmptyPlaceholderText()
{
var picker = new Picker()
{
Title = null,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
Assert.Equal(string.Empty, handler.PlatformView.PlaceholderText);
});
}

[Fact(DisplayName = "Empty title maps to empty PlaceholderText")]
public async Task EmptyTitleMapsToEmptyPlaceholderText()
{
var picker = new Picker()
{
Title = string.Empty,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
Assert.Equal(string.Empty, handler.PlatformView.PlaceholderText);
});
}

[Fact(DisplayName = "TitleColor maps to PlaceholderForeground")]
public async Task TitleColorMapsToPlaceholderForeground()
{
var picker = new Picker()
{
Title = "Select Option",
TitleColor = Colors.Red,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
var placeholderBrush = Assert.IsType<WSolidColorBrush>(handler.PlatformView.PlaceholderForeground);
Assert.Equal(Colors.Red, placeholderBrush.Color.ToColor());
});
Comment thread
mhrastegari marked this conversation as resolved.
}
Comment thread
mhrastegari marked this conversation as resolved.

[Fact(DisplayName = "Null TitleColor clears local PlaceholderForeground")]
public async Task NullTitleColorClearsPlaceholderForegroundLocalValue()
{
var picker = new Picker()
{
Title = "Select Option",
TitleColor = Colors.Red,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
Assert.IsType<WSolidColorBrush>(handler.PlatformView.PlaceholderForeground);

picker.ClearValue(Picker.TitleColorProperty);

Assert.Equal(DependencyProperty.UnsetValue, handler.PlatformView.ReadLocalValue(ComboBox.PlaceholderForegroundProperty));
});
}

[Fact(DisplayName = "Title and TitleColor update after handler creation")]
public async Task TitleAndTitleColorUpdateAfterHandlerCreation()
{
var picker = new Picker()
{
Title = "First",
TitleColor = Colors.Red,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
}
};

var handler = await CreateHandlerAsync<PickerHandler>(picker);

await InvokeOnMainThreadAsync(() =>
{
picker.Title = "Second";
Assert.Equal("Second", handler.PlatformView.PlaceholderText);

picker.TitleColor = Colors.Blue;
var brush = Assert.IsType<WSolidColorBrush>(handler.PlatformView.PlaceholderForeground);
Assert.Equal(Colors.Blue, brush.Color.ToColor());
});
}

[Fact(DisplayName = "CharacterSpacing applies to placeholder TextBlock when Title is set")]
public async Task CharacterSpacingAppliesToPlaceholderWithTitle()
{
const string title = "Select Option";
const double characterSpacingPt = 8d;
var expectedEm = characterSpacingPt.ToEm();

var picker = new Picker()
{
Title = title,
CharacterSpacing = characterSpacingPt,
ItemsSource = new ObservableCollection<string>()
{
"Item 1",
"Item 2"
},
WidthRequest = 200,
HeightRequest = 48
};

await AttachAndRun<PickerHandler>(picker, handler =>
{
var platformView = handler.PlatformView;

Assert.Equal(title, platformView.PlaceholderText);
Assert.Equal(expectedEm, platformView.CharacterSpacing);

var placeholderTextBlock = platformView.GetDescendantByName<TextBlock>("PlaceholderTextBlock");
Assert.NotNull(placeholderTextBlock);
Assert.Equal(title, placeholderTextBlock.Text);
Assert.Equal(expectedEm, placeholderTextBlock.CharacterSpacing);
Comment thread
mhrastegari marked this conversation as resolved.
});
}

protected Task<string> GetPlatformControlText(ComboBox platformView)
{
return InvokeOnMainThreadAsync(() =>
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/Picker/PickerHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static void MapTitle(IPickerHandler handler, IPicker picker)

public static void MapTitleColor(IPickerHandler handler, IPicker picker)
{
handler.PlatformView?.UpdateTitle(picker);
handler.PlatformView?.UpdateTitleColor(picker);
}

public static void MapBackground(IPickerHandler handler, IPicker picker)
Expand Down
38 changes: 33 additions & 5 deletions src/Core/src/Platform/Windows/PickerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ public static class PickerExtensions
{
public static void UpdateTitle(this ComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.Header = string.IsNullOrEmpty(picker.Title) ? null : picker;

nativeComboBox.HeaderTemplate = string.IsNullOrEmpty(picker.Title) ? null :
(UI.Xaml.DataTemplate)UI.Xaml.Application.Current.Resources["ComboBoxHeader"];
nativeComboBox.PlaceholderText = picker.Title ?? string.Empty;
Comment thread
mhrastegari marked this conversation as resolved.
nativeComboBox.UpdatePlaceholderCharacterSpacing();
}

public static void UpdateTitleColor(this ComboBox nativeComboBox, IPicker picker)
{
if (picker.TitleColor is null)
nativeComboBox.ClearValue(ComboBox.PlaceholderForegroundProperty);
else
nativeComboBox.PlaceholderForeground = picker.TitleColor.ToPlatform();
}

public static void UpdateBackground(this ComboBox nativeComboBox, IPicker picker)
Expand Down Expand Up @@ -75,6 +80,29 @@ public static void UpdateSelectedIndex(this ComboBox nativeComboBox, IPicker pic
public static void UpdateCharacterSpacing(this ComboBox nativeComboBox, IPicker picker)
{
nativeComboBox.CharacterSpacing = picker.CharacterSpacing.ToEm();
nativeComboBox.UpdatePlaceholderCharacterSpacing();
}

static void UpdatePlaceholderCharacterSpacing(this ComboBox nativeComboBox)
{
if (nativeComboBox.IsLoaded)
{
ApplyPlaceholderCharacterSpacing(nativeComboBox);
}
else
{
nativeComboBox.OnLoaded(() => ApplyPlaceholderCharacterSpacing(nativeComboBox));
}
}

static void ApplyPlaceholderCharacterSpacing(ComboBox nativeComboBox)
{
var placeholderTextBlock = nativeComboBox.GetDescendantByName<TextBlock>("PlaceholderTextBlock");

if (placeholderTextBlock is not null)
{
placeholderTextBlock.CharacterSpacing = nativeComboBox.CharacterSpacing;
}
}

public static void UpdateFont(this ComboBox nativeComboBox, IPicker picker, IFontManager fontManager) =>
Expand Down Expand Up @@ -107,4 +135,4 @@ internal static void UpdateIsOpen(this ComboBox nativeComboBox, IPicker picker)
nativeComboBox.IsDropDownOpen = picker.IsOpen;
}
}
}
}
13 changes: 0 additions & 13 deletions src/Core/src/Platform/Windows/Styles/MauiComboBoxStyle.xaml

This file was deleted.

1 change: 0 additions & 1 deletion src/Core/src/Platform/Windows/Styles/Resources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
x:Class="Microsoft.Maui.Platform.Resources">

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="MauiComboBoxStyle.xaml" />
<ResourceDictionary Source="MauiSliderStyle.xaml" />
<ResourceDictionary Source="MauiStepperStyle.xaml" />
<ResourceDictionary Source="WindowRootViewStyle.xaml" />
Expand Down
1 change: 1 addition & 0 deletions src/Core/src/PublicAPI/net-windows/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#nullable enable
override Microsoft.Maui.Platform.MauiPasswordTextBox.OnCreateAutomationPeer() -> Microsoft.UI.Xaml.Automation.Peers.AutomationPeer!
static Microsoft.Maui.Platform.PickerExtensions.UpdateTitleColor(this Microsoft.UI.Xaml.Controls.ComboBox! nativeComboBox, Microsoft.Maui.IPicker! picker) -> void
static Microsoft.Maui.GridLength.implicit operator Microsoft.Maui.GridLength(string! value) -> Microsoft.Maui.GridLength
static Microsoft.Maui.SafeAreaEdges.Container.get -> Microsoft.Maui.SafeAreaEdges
Loading