Skip to content

Commit

Permalink
Add RadioButton handler (dotnet#499)
Browse files Browse the repository at this point in the history
  • Loading branch information
myroot committed Aug 25, 2022
1 parent 26b791a commit ed157f7
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 15 deletions.
Expand Up @@ -51,7 +51,7 @@ public FrameRenderer() : base(null)

Children.Add(_clipperView);

BorderlineColor = NColor.Black;
BorderlineColor = NColor.Transparent;
BorderlineWidth = 1.0.ToScaledPixel();
_viewHandlerWrapper = new ViewHandlerDelegator<Frame>(Mapper, CommandMapper, this);
}
Expand Down Expand Up @@ -141,17 +141,27 @@ void UpdateBackgroundColor()
if (Element == null || _disposed)
return;

BackgroundColor = Element.BackgroundColor.ToNUIColor();
if (Element.BackgroundColor.IsNotDefault())
BackgroundColor = Element.BackgroundColor.ToNUIColor();
else
BackgroundColor = Colors.White.ToNUIColor();
}

void UpdateBackground()
{
if (Element == null || _disposed)
return;

// If BackgroundColor is valid, do not update Background
if (Element.BackgroundColor.IsNotDefault())
return;

var color = ((Paint)Element.Background)?.ToColor();

if (color != null)
BackgroundColor = color.ToNUIColor();
else
BackgroundColor = Colors.White.ToNUIColor();
}

void UpdateBorderColor()
Expand All @@ -162,7 +172,7 @@ void UpdateBorderColor()
if (Element.BorderColor.IsNotDefault())
BorderlineColor = Element.BorderColor.ToNUIColor();
else
BorderlineColor = Tizen.NUI.Color.Black;
BorderlineColor = NColor.Transparent;
}

void UpdateShadow()
Expand Down
117 changes: 117 additions & 0 deletions src/Controls/src/Core/HandlerImpl/RadioButton/RadioButton.Tizen.cs
@@ -0,0 +1,117 @@
using Microsoft.Maui.Controls.Internals;
using Microsoft.Maui.Controls.Shapes;

namespace Microsoft.Maui.Controls
{
public partial class RadioButton
{
static ControlTemplate s_tizenDefaultTemplate;

public static void MapContent(RadioButtonHandler handler, RadioButton radioButton)
{
if (radioButton.ResolveControlTemplate() == null)
radioButton.ControlTemplate = s_tizenDefaultTemplate ?? (s_tizenDefaultTemplate = new ControlTemplate(() => BuildTizenDefaultTemplate()));

RadioButtonHandler.MapContent(handler, radioButton);
}

static View BuildTizenDefaultTemplate()
{
var frame = new Frame
{
HasShadow = false,
Padding = 6
};

BindToTemplatedParent(frame, BackgroundColorProperty, Controls.Frame.BorderColorProperty, Controls.Frame.CornerRadiusProperty, HorizontalOptionsProperty,
MarginProperty, OpacityProperty, RotationProperty, ScaleProperty, ScaleXProperty, ScaleYProperty,
TranslationYProperty, TranslationXProperty, VerticalOptionsProperty);

var grid = new Grid
{
ColumnSpacing = 6,
RowSpacing = 0,
ColumnDefinitions = new ColumnDefinitionCollection {
new ColumnDefinition { Width = GridLength.Auto },
new ColumnDefinition { Width = GridLength.Star }
},
RowDefinitions = new RowDefinitionCollection {
new RowDefinition { Height = GridLength.Auto }
}
};

var normalEllipse = new Ellipse
{
Fill = Brush.Transparent,
Aspect = Stretch.Uniform,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
HeightRequest = 21,
WidthRequest = 21,
StrokeThickness = 2,
Stroke = RadioButtonThemeColor,
InputTransparent = true
};

var checkMark = new Ellipse
{
Fill = RadioButtonCheckMarkThemeColor,
Aspect = Stretch.Uniform,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
HeightRequest = 11,
WidthRequest = 11,
Opacity = 0,
InputTransparent = true
};

var contentPresenter = new ContentPresenter
{
HorizontalOptions = LayoutOptions.Fill,
VerticalOptions = LayoutOptions.Center
};

contentPresenter.SetBinding(BackgroundColorProperty, new Binding(BackgroundColorProperty.PropertyName,
source: RelativeBindingSource.TemplatedParent));

grid.Add(normalEllipse);
grid.Add(checkMark);
grid.Add(contentPresenter, 1, 0);

frame.Content = grid;

INameScope nameScope = new NameScope();
NameScope.SetNameScope(frame, nameScope);
nameScope.RegisterName(TemplateRootName, frame);
nameScope.RegisterName(UncheckedButton, normalEllipse);
nameScope.RegisterName(CheckedIndicator, checkMark);
nameScope.RegisterName("ContentPresenter", contentPresenter);

VisualStateGroupList visualStateGroups = new VisualStateGroupList();

var common = new VisualStateGroup() { Name = "Common" };
common.States.Add(new VisualState() { Name = VisualStateManager.CommonStates.Normal });
common.States.Add(new VisualState() { Name = VisualStateManager.CommonStates.Disabled });

visualStateGroups.Add(common);

var checkedStates = new VisualStateGroup() { Name = "CheckedStates" };

VisualState checkedVisualState = new VisualState() { Name = CheckedVisualState };
checkedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 1 });
checkedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonCheckMarkThemeColor });
checkedStates.States.Add(checkedVisualState);

VisualState uncheckedVisualState = new VisualState() { Name = UncheckedVisualState };
uncheckedVisualState.Setters.Add(new Setter() { Property = OpacityProperty, TargetName = CheckedIndicator, Value = 0 });
uncheckedVisualState.Setters.Add(new Setter() { Property = Shape.StrokeProperty, TargetName = UncheckedButton, Value = RadioButtonThemeColor });
checkedStates.States.Add(uncheckedVisualState);

visualStateGroups.Add(checkedStates);

VisualStateManager.SetVisualStateGroups(frame, visualStateGroups);

return frame;
}
}
}
Expand Up @@ -11,7 +11,7 @@ public partial class RadioButton
public static IPropertyMapper<RadioButton, RadioButtonHandler> ControlsRadioButtonMapper =
new PropertyMapper<RadioButton, RadioButtonHandler>(RadioButtonHandler.Mapper)
{
#if IOS || ANDROID || WINDOWS
#if IOS || ANDROID || WINDOWS || TIZEN
[nameof(IRadioButton.Content)] = MapContent
#endif
};
Expand Down
Expand Up @@ -6136,6 +6136,7 @@ Microsoft.Maui.Controls.TappedEventArgs.TappedEventArgs(object? parameter) -> vo
~static Microsoft.Maui.Controls.PointCollection.implicit operator Microsoft.Maui.Controls.PointCollection(Microsoft.Maui.Graphics.Point[] d) -> Microsoft.Maui.Controls.PointCollection
~static Microsoft.Maui.Controls.RadioButton.ControlsRadioButtonMapper -> Microsoft.Maui.IPropertyMapper<Microsoft.Maui.Controls.RadioButton, Microsoft.Maui.Handlers.RadioButtonHandler>
~static Microsoft.Maui.Controls.RadioButton.DefaultTemplate.get -> Microsoft.Maui.Controls.ControlTemplate
~static Microsoft.Maui.Controls.RadioButton.MapContent(Microsoft.Maui.Handlers.RadioButtonHandler handler, Microsoft.Maui.Controls.RadioButton radioButton) -> void
~static Microsoft.Maui.Controls.RadioButtonGroup.GetGroupName(Microsoft.Maui.Controls.BindableObject b) -> string
~static Microsoft.Maui.Controls.RadioButtonGroup.GetSelectedValue(Microsoft.Maui.Controls.BindableObject bindableObject) -> object
~static Microsoft.Maui.Controls.RadioButtonGroup.SetGroupName(Microsoft.Maui.Controls.BindableObject bindable, string groupName) -> void
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/RadioButton/IRadioButtonHandler.cs
Expand Up @@ -5,7 +5,7 @@
#elif WINDOWS
using PlatformView = Microsoft.UI.Xaml.Controls.RadioButton;
#elif TIZEN
using PlatformView = Tizen.NUI.BaseComponents.View;
using PlatformView = Microsoft.Maui.Platform.ContentViewGroup;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformView = System.Object;
#endif
Expand Down
72 changes: 66 additions & 6 deletions src/Core/src/Handlers/RadioButton/RadioButtonHandler.Tizen.cs
@@ -1,16 +1,76 @@
using NView = Tizen.NUI.BaseComponents.View;
using System;
using Tizen.UIExtensions.NUI;

namespace Microsoft.Maui.Handlers
{
public partial class RadioButtonHandler : ViewHandler<IRadioButton, NView>
public partial class RadioButtonHandler : ViewHandler<IRadioButton, ContentViewGroup>
{
protected override NView CreatePlatformView() => new();
IPlatformViewHandler? _contentHandler;

[MissingMapper]
public static void MapIsChecked(IRadioButtonHandler handler, IRadioButton radioButton) { }
protected override ContentViewGroup CreatePlatformView()
{
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} must be set to create a {nameof(ContentViewGroup)}");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} cannot be null");

return new ContentViewGroup(VirtualView)
{
Focusable = true,
CrossPlatformMeasure = VirtualView.CrossPlatformMeasure,
CrossPlatformArrange = VirtualView.CrossPlatformArrange
};
}

protected override void ConnectHandler(ContentViewGroup platformView)
{
platformView.CrossPlatformMeasure = VirtualView.CrossPlatformMeasure;
platformView.CrossPlatformArrange = VirtualView.CrossPlatformArrange;
platformView.KeyEvent += OnKeyEvent;
base.ConnectHandler(platformView);
}

protected override void DisconnectHandler(ContentViewGroup platformView)
{
platformView.KeyEvent -= OnKeyEvent;
base.DisconnectHandler(platformView);
}

bool OnKeyEvent(object source, Tizen.NUI.BaseComponents.View.KeyEventArgs e)
{
if (e.Key.IsAcceptKeyEvent())
{
VirtualView.IsChecked = !VirtualView.IsChecked;
return true;
}
return false;
}

void UpdateContent()
{
_ = PlatformView ?? throw new InvalidOperationException($"{nameof(PlatformView)} should have been set by base class.");
_ = VirtualView ?? throw new InvalidOperationException($"{nameof(VirtualView)} should have been set by base class.");
_ = MauiContext ?? throw new InvalidOperationException($"{nameof(MauiContext)} should have been set by base class.");

PlatformView.Children.Clear();
_contentHandler?.Dispose();
_contentHandler = null;

if (VirtualView.PresentedContent is IView view)
{
PlatformView.Children.Add(view.ToPlatform(MauiContext));
if (view.Handler is IPlatformViewHandler thandler)
{
_contentHandler = thandler;
}
}
}

public static void MapContent(IRadioButtonHandler handler, IContentView page)
{
(handler as RadioButtonHandler)?.UpdateContent();
}

[MissingMapper]
public static void MapContent(IRadioButtonHandler handler, IRadioButton radioButton) { }
public static void MapIsChecked(IRadioButtonHandler handler, IRadioButton radioButton) { }

[MissingMapper]
public static void MapTextColor(IRadioButtonHandler handler, ITextStyle textStyle) { }
Expand Down
2 changes: 1 addition & 1 deletion src/Core/src/Handlers/RadioButton/RadioButtonHandler.cs
Expand Up @@ -5,7 +5,7 @@
#elif WINDOWS
using PlatformView = Microsoft.UI.Xaml.Controls.RadioButton;
#elif TIZEN
using PlatformView = Tizen.NUI.BaseComponents.View;
using PlatformView = Microsoft.Maui.Platform.ContentViewGroup;
#elif (NETSTANDARD || !PLATFORM) || (NET6_0_OR_GREATER && !IOS && !ANDROID && !TIZEN)
using PlatformView = System.Object;
#endif
Expand Down
8 changes: 5 additions & 3 deletions src/Core/src/PublicAPI/net-tizen/PublicAPI.Shipped.txt
Expand Up @@ -454,7 +454,7 @@ Microsoft.Maui.Handlers.IProgressBarHandler
Microsoft.Maui.Handlers.IProgressBarHandler.PlatformView.get -> Tizen.UIExtensions.NUI.GraphicsView.ProgressBar!
Microsoft.Maui.Handlers.IProgressBarHandler.VirtualView.get -> Microsoft.Maui.IProgress!
Microsoft.Maui.Handlers.IRadioButtonHandler
Microsoft.Maui.Handlers.IRadioButtonHandler.PlatformView.get -> Tizen.NUI.BaseComponents.View!
Microsoft.Maui.Handlers.IRadioButtonHandler.PlatformView.get -> Microsoft.Maui.Platform.ContentViewGroup!
Microsoft.Maui.Handlers.IRadioButtonHandler.VirtualView.get -> Microsoft.Maui.IRadioButton!
Microsoft.Maui.Handlers.IRefreshViewHandler
Microsoft.Maui.Handlers.IRefreshViewHandler.PlatformView.get -> Tizen.NUI.BaseComponents.View!
Expand Down Expand Up @@ -1896,7 +1896,9 @@ override Microsoft.Maui.Handlers.PickerHandler.ConnectHandler(Tizen.UIExtensions
override Microsoft.Maui.Handlers.PickerHandler.CreatePlatformView() -> Tizen.UIExtensions.NUI.Entry!
override Microsoft.Maui.Handlers.PickerHandler.DisconnectHandler(Tizen.UIExtensions.NUI.Entry! platformView) -> void
override Microsoft.Maui.Handlers.ProgressBarHandler.CreatePlatformView() -> Tizen.UIExtensions.NUI.GraphicsView.ProgressBar!
override Microsoft.Maui.Handlers.RadioButtonHandler.CreatePlatformView() -> Tizen.NUI.BaseComponents.View!
override Microsoft.Maui.Handlers.RadioButtonHandler.CreatePlatformView() -> Microsoft.Maui.Platform.ContentViewGroup!
override Microsoft.Maui.Handlers.RadioButtonHandler.ConnectHandler(Microsoft.Maui.Platform.ContentViewGroup! platformView) -> void
override Microsoft.Maui.Handlers.RadioButtonHandler.DisconnectHandler(Microsoft.Maui.Platform.ContentViewGroup! platformView) -> void
override Microsoft.Maui.Handlers.RefreshViewHandler.CreatePlatformView() -> Tizen.NUI.BaseComponents.View!
override Microsoft.Maui.Handlers.ScrollViewHandler.ConnectHandler(Tizen.UIExtensions.NUI.ScrollView! platformView) -> void
override Microsoft.Maui.Handlers.ScrollViewHandler.CreatePlatformView() -> Tizen.UIExtensions.NUI.ScrollView!
Expand Down Expand Up @@ -2214,7 +2216,7 @@ static Microsoft.Maui.Handlers.ProgressBarHandler.MapProgress(Microsoft.Maui.Han
static Microsoft.Maui.Handlers.ProgressBarHandler.MapProgressColor(Microsoft.Maui.Handlers.IProgressBarHandler! handler, Microsoft.Maui.IProgress! progress) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.CommandMapper -> Microsoft.Maui.CommandMapper<Microsoft.Maui.IRadioButton!, Microsoft.Maui.Handlers.IRadioButtonHandler!>!
static Microsoft.Maui.Handlers.RadioButtonHandler.MapCharacterSpacing(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.ITextStyle! textStyle) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.MapContent(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.IRadioButton! radioButton) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.MapContent(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.IContentView! page) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.MapCornerRadius(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.IRadioButton! radioButton) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.MapFont(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.ITextStyle! textStyle) -> void
static Microsoft.Maui.Handlers.RadioButtonHandler.MapIsChecked(Microsoft.Maui.Handlers.IRadioButtonHandler! handler, Microsoft.Maui.IRadioButton! radioButton) -> void
Expand Down

0 comments on commit ed157f7

Please sign in to comment.