-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ccc1b07
Implement PickerHandler in WinUI
jsuarezruiz e79ef31
Merge branch 'main' into winui-picker
jsuarezruiz ce686d7
Enab le nullable in some classes
jsuarezruiz 7804878
Merge branch 'main' into winui-picker
jsuarezruiz c8653ae
Fix build error
jsuarezruiz c9e61fa
Merge branch 'main' into winui-picker
rachelkang 482e58f
Merge branch 'main' into winui-picker
jsuarezruiz 53fdcd5
Register MauiComboBox ResourceDictionary
jsuarezruiz c329234
Merge branch 'main' into winui-picker
jsuarezruiz a57fa47
Updated PickerExtensions
jsuarezruiz 91c58f4
Updated MauiComboBox
jsuarezruiz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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; | ||
} | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Core/src/Platform/Windows/CharacterSpacingExtensions.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,12 @@ | ||
using System; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public static class CharacterSpacingExtensions | ||
{ | ||
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. | ||
} | ||
} | ||
} |
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,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(); | ||
} | ||
} | ||
} |
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,26 @@ | ||
#nullable enable | ||
using Microsoft.UI.Xaml.Controls; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public class MauiComboBox : ComboBox | ||
{ | ||
public MauiComboBox() | ||
{ | ||
DefaultStyleKey = typeof(MauiComboBox); | ||
|
||
DropDownOpened += FormsComboBoxDropDownOpened; | ||
SelectionChanged += FormsComboBoxSelectionChanged; | ||
} | ||
|
||
void FormsComboBoxDropDownOpened(object? sender, object e) | ||
{ | ||
MinWidth = ActualWidth; | ||
} | ||
|
||
void FormsComboBoxSelectionChanged(object? sender, SelectionChangedEventArgs e) | ||
{ | ||
MinWidth = 0; | ||
} | ||
} | ||
} |
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,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 ComboBox nativeComboBox, IPicker picker, IFontManager fontManager) => | ||
rachelkang marked this conversation as resolved.
Show resolved
Hide resolved
|
||
nativeComboBox.UpdateFont(picker.Font, fontManager); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch