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

Make equal MAUI & XF Implementations #249

Merged
merged 4 commits into from
Mar 22, 2022
Merged
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
4 changes: 2 additions & 2 deletions src/InputKit.Maui/InputKit.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<SingleProject>true</SingleProject>
<MauiVersion>6.0.200-preview.14.5099</MauiVersion>
<PackageId>InputKit.Maui</PackageId>
<Version>4.0.0-pre.7</Version>
<Version>4.0.0-pre.8</Version>
<DefineConstants Condition="$(TargetFramework.Contains('-windows'))">$(DefineConstants);UWP</DefineConstants>

<!-- NuGet Package Info -->
Expand Down Expand Up @@ -47,6 +47,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Plainer.Maui" Version="1.2.0" />
<PackageReference Include="Plainer.Maui" Version="1.3.0" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/InputKit.Maui/Shared/Controls/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,4 +411,4 @@ public enum CheckType
Material,
Custom = 90
}
}
}
47 changes: 11 additions & 36 deletions src/InputKit.Maui/Shared/Controls/RadioButton.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
using InputKit.Shared.Configuration;
using InputKit.Shared.Layouts;
using Microsoft.Maui;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Input;

namespace InputKit.Shared.Controls;

///-----------------------------------------------------------------------------
/// <summary>
/// Radio Button with Text
/// </summary>
public partial class RadioButton : StatefulStackLayout
public class RadioButton : StatefulStackLayout
{
#region Statics
/// <summary>
Expand Down Expand Up @@ -141,35 +136,28 @@ public bool IsChecked
[System.ComponentModel.TypeConverter(typeof(FontSizeConverter))]
public double TextFontSize { get => lblText.FontSize; set => lblText.FontSize = value; }

/// <summary>
/// Size of Radio Button
/// </summary>
[Obsolete("This feature is obsolete. You can try to use 'CircleImage' to set your own image as circle", false)]
public double CircleSize { get => iconCircle.Height; set => SetCircleSize(value); }


/// <summary>
/// Set your own background image instead of default circle.
/// </summary>
public ImageSource CircleImage { get => (ImageSource)GetValue(CircleImageProperty); set => SetValue(CircleImageProperty, value); }

public ImageSource CheckedImage { get => (ImageSource)GetValue(CheckedImageProperty); set => SetValue(CheckedImageProperty, value); }
//-----------------------------------------------------------------------------

/// <summary>
/// To be added.
/// </summary>
public string FontFamily { get => lblText.FontFamily; set => lblText.FontFamily = value; }
//-----------------------------------------------------------------------------

/// <summary>
/// Color of Radio Button's checked.
/// </summary>
public Color Color { get => (Color)GetValue(ColorProperty); set => SetValue(ColorProperty, value); }
//-----------------------------------------------------------------------------

/// <summary>
/// Color of radio button's outline border
/// </summary>
public Color CircleColor { get => (Color)GetValue(CircleColorProperty); set => SetValue(CircleColorProperty, value); }
//-----------------------------------------------------------------------------

/// <summary>
/// Color of description text of Radio Button
/// </summary>
Expand Down Expand Up @@ -205,7 +193,6 @@ public LabelPosition LabelPosition
public static readonly BindableProperty CommandParameterProperty = BindableProperty.Create(nameof(CommandParameter), typeof(object), typeof(RadioButton), propertyChanged: (bo, ov, nv) => (bo as RadioButton).CommandParameter = nv);
public static readonly BindableProperty IsPressedProperty = BindableProperty.Create(nameof(IsPressed), typeof(bool), typeof(RadioButton), propertyChanged: (bo, ov, nv) => (bo as RadioButton).ApplyIsPressedAction((bool)nv));
public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(RadioButton), propertyChanged: (bo, ov, nv) => (bo as RadioButton).FontFamily = (string)nv);
public static readonly BindableProperty CircleSizeProperty = BindableProperty.Create(nameof(CircleSize), typeof(double), typeof(RadioButton), -1d, propertyChanged: (bo, ov, nv) => (bo as RadioButton).SetCircleSize((double)nv));
public static readonly BindableProperty LabelPositionProperty = BindableProperty.Create(
propertyName: nameof(LabelPosition), declaringType: typeof(RadioButton),
returnType: typeof(LabelPosition), defaultBindingMode: BindingMode.TwoWay,
Expand All @@ -231,34 +218,22 @@ private void ApplyLabelPosition(LabelPosition position)
Children.Add(IconLayout);
}
}
//-----------------------------------------------------------------------------

/// <summary>
/// That handles tapps and triggers event, commands etc.
/// </summary>
void Tapped()
{
if (IsDisabled) return;
if (IsDisabled)
{
return;
}

IsChecked = true;
Clicked?.Invoke(this, new EventArgs());
ClickCommand?.Execute(CommandParameter ?? Value);
}

//-----------------------------------------------------------------------------
/// <summary>
/// Sets size of Circle
/// </summary>
[Obsolete("This feature is obsolete. You can try to use 'CircleImage' to set your own image as circle", false)]
void SetCircleSize(double value)
{
if (value < 0)
return;
iconCircle.HeightRequest = CircleSize;
iconCircle.WidthRequest = CircleSize;
iconChecked.WidthRequest = CircleSize;
iconChecked.WidthRequest = CircleSize;
Debug.WriteLine("[InputKit] [RadioButton] - CircleSize is obsolete and doesn't affect now. You can try to set a image source to CircleImage to change circle");
}

void UpdateColors()
{
iconChecked.FillColor = Color;
Expand Down
67 changes: 36 additions & 31 deletions src/Xamarin.Forms.InputKit/Shared/Controls/CheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

namespace Plugin.InputKit.Shared.Controls
{
/// <summary>
/// A checkbox for boolean inputs. It Includes a text inside
/// </summary>

/// <summary>
/// A checkbox for boolean inputs. It Includes a text inside
/// </summary>
Expand All @@ -17,7 +21,7 @@ public partial class CheckBox : StatefulStackLayout, IValidatable
public static GlobalSetting GlobalSetting { get; } = new GlobalSetting
{
BackgroundColor = Color.Transparent,
Color = Color.Accent,
Color = InputKitOptions.GetAccentColor(),
BorderColor = Color.Black,
TextColor = (Color)Label.TextColorProperty.DefaultValue,
Size = 25,
Expand All @@ -27,9 +31,9 @@ public partial class CheckBox : StatefulStackLayout, IValidatable
};

#region Constants
public const string RESOURCE_CHECK = "Plugin.InputKit.Shared.Resources.check.png";
public const string RESOURCE_CROSS = "Plugin.InputKit.Shared.Resources.cross.png";
public const string RESOURCE_STAR = "Plugin.InputKit.Shared.Resources.star.png";
public const string RESOURCE_CHECK = "InputKit.Shared.Resources.check.png";
public const string RESOURCE_CROSS = "InputKit.Shared.Resources.cross.png";
public const string RESOURCE_STAR = "InputKit.Shared.Resources.star.png";
#endregion

#region Fields
Expand All @@ -48,19 +52,21 @@ public partial class CheckBox : StatefulStackLayout, IValidatable
public CheckBox()
{
InitVisualStates();
this.Orientation = StackOrientation.Horizontal;
this.Padding = new Thickness(0, 10);
this.Spacing = 10;
this.frmBackground.Content = boxSelected;

Orientation = StackOrientation.Horizontal;
Padding = new Thickness(0, 10);
Spacing = 10;
frmBackground.Content = boxSelected;
ApplyLabelPosition(LabelPosition);

this.ApplyIsCheckedAction = ApplyIsChecked;
this.ApplyIsPressedAction = ApplyIsPressed;
this.GestureRecognizers.Add(new TapGestureRecognizer
ApplyIsCheckedAction = ApplyIsChecked;
ApplyIsPressedAction = ApplyIsPressed;
GestureRecognizers.Add(new TapGestureRecognizer
{
Command = new Command(() => { if (IsDisabled) return; IsChecked = !IsChecked; ExecuteCommand(); CheckChanged?.Invoke(this, new EventArgs()); ValidationChanged?.Invoke(this, new EventArgs()); }),
});

imgSelected.BackgroundColor = Color.Cyan;

imgSelected.WidthRequest = 15;
}

/// <summary>
Expand Down Expand Up @@ -112,7 +118,7 @@ public CheckBox(string optionName, int key) : this()
/// <summary>
/// Text to display right of CheckBox
/// </summary>
public string Text { get => lblOption.Text; set { lblOption.Text = value; lblOption.IsVisible = !String.IsNullOrEmpty(value); } }
public string Text { get => lblOption.Text; set { lblOption.Text = value; lblOption.IsVisible = !string.IsNullOrEmpty(value); } }

/// <summary>
/// IsChecked Property
Expand All @@ -131,7 +137,7 @@ public bool IsChecked
/// <summary>
/// Gets or sets the checkbutton enabled or not. If checkbox is disabled, checkbox can not be interacted.
/// </summary>
public bool IsDisabled { get => _isEnabled; set { _isEnabled = value; this.Opacity = value ? 0.6 : 1; } }
public bool IsDisabled { get => _isEnabled; set { _isEnabled = value; Opacity = value ? 0.6 : 1; } }

/// <summary>
/// Color of Checkbox checked
Expand Down Expand Up @@ -185,7 +191,7 @@ public Color Color
/// <summary>
/// Checks if entry required and checked
/// </summary>
public bool IsValidated => !this.IsRequired || this.IsChecked;
public bool IsValidated => !IsRequired || IsChecked;
/// <summary>
/// Not available for this control
/// </summary>
Expand Down Expand Up @@ -215,10 +221,10 @@ public LabelPosition LabelPosition

#region BindableProperties
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(CheckBox), Color.Accent, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateColors());
public static readonly BindableProperty ColorProperty = BindableProperty.Create(nameof(Color), typeof(Color), typeof(CheckBox), GlobalSetting.Color, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateColors());
public static readonly BindableProperty TextColorProperty = BindableProperty.Create(nameof(TextColor), typeof(Color), typeof(CheckBox), GlobalSetting.TextColor, propertyChanged: (bo, ov, nv) => (bo as CheckBox).TextColor = (Color)nv);
public static readonly BindableProperty IconColorProperty = BindableProperty.Create(nameof(IconColor), typeof(Color), typeof(CheckBox), GlobalSetting.Color, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateColors());
public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), false, BindingMode.TwoWay, propertyChanged: (bo, ov, nv) => (bo as CheckBox).ApplyIsCheckedAction((bo as CheckBox), (bool)nv));
public static readonly BindableProperty IconColorProperty = BindableProperty.Create(nameof(IconColor), typeof(Color), typeof(CheckBox), Color.Transparent, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateColors());
public static readonly BindableProperty IsCheckedProperty = BindableProperty.Create(nameof(IsChecked), typeof(bool), typeof(CheckBox), false, BindingMode.TwoWay, propertyChanged: (bo, ov, nv) => (bo as CheckBox).ApplyIsCheckedAction(bo as CheckBox, (bool)nv));
public static readonly BindableProperty IsDisabledProperty = BindableProperty.Create(nameof(IsDisabled), typeof(bool), typeof(CheckBox), false, propertyChanged: (bo, ov, nv) => (bo as CheckBox).IsDisabled = (bool)nv);
public static readonly BindableProperty KeyProperty = BindableProperty.Create(nameof(Key), typeof(int), typeof(CheckBox), 0, propertyChanged: (bo, ov, nv) => (bo as CheckBox).Key = (int)nv);
public static readonly BindableProperty TextProperty = BindableProperty.Create(nameof(Text), typeof(string), typeof(CheckBox), "", propertyChanged: (bo, ov, nv) => (bo as CheckBox).Text = (string)nv);
Expand All @@ -229,7 +235,7 @@ public LabelPosition LabelPosition
public static readonly BindableProperty BorderColorProperty = BindableProperty.Create(nameof(BorderColor), typeof(Color), typeof(CheckBox), GlobalSetting.BorderColor, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateBorderColor());
public static readonly BindableProperty FontFamilyProperty = BindableProperty.Create(nameof(FontFamily), typeof(string), typeof(CheckBox), Label.FontFamilyProperty.DefaultValue, propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateFontFamily(nv?.ToString()));
public static readonly BindableProperty CustomIconProperty = BindableProperty.Create(nameof(CustomIcon), typeof(ImageSource), typeof(CheckBox), default(ImageSource), propertyChanged: (bo, ov, nv) => (bo as CheckBox).UpdateType((bo as CheckBox).Type));
public static readonly BindableProperty IsPressedProperty = BindableProperty.Create(nameof(IsPressed), typeof(bool), typeof(CheckBox), propertyChanged: (bo, ov, nv) => (bo as CheckBox).ApplyIsPressedAction((bo as CheckBox), (bool)nv));
public static readonly BindableProperty IsPressedProperty = BindableProperty.Create(nameof(IsPressed), typeof(bool), typeof(CheckBox), propertyChanged: (bo, ov, nv) => (bo as CheckBox).ApplyIsPressedAction(bo as CheckBox, (bool)nv));
public static readonly BindableProperty CornerRadiusProperty = BindableProperty.Create(nameof(CornerRadius), typeof(float), typeof(CheckBox), GlobalSetting.CornerRadius, propertyChanged: (bo, ov, nv) => (bo as CheckBox).frmBackground.CornerRadius = (float)nv);
public static readonly BindableProperty LabelPositionProperty = BindableProperty.Create(
propertyName: nameof(LabelPosition), declaringType: typeof(CheckBox),
Expand Down Expand Up @@ -265,7 +271,7 @@ void ExecuteCommand()

void UpdateBoxBackground()
{
if (this.Type == CheckType.Material)
if (Type == CheckType.Material)
return;

frmBackground.BackgroundColor = BoxBackgroundColor;
Expand All @@ -279,25 +285,22 @@ void UpdateColors()
{
frmBackground.BorderColor = Color;
frmBackground.BackgroundColor = IsChecked ? Color : Color.Transparent;
imgSelected.FillColor = Color.ToSurfaceColor();
}
else
{
frmBackground.BorderColor = IsChecked ? Color : BorderColor;
frmBackground.BackgroundColor = BoxBackgroundColor;
imgSelected.FillColor = IconColor == Color.Transparent ? Color : IconColor;
}

imgSelected.FillColor =
IconColor == frmBackground.BackgroundColor ?
IconColor.ToSurfaceColor() :
IconColor;
}

void UpdateBorderColor()
{
if (this.Type == CheckType.Material)
if (Type == CheckType.Material)
return;

frmBackground.BorderColor = this.BorderColor;
frmBackground.BorderColor = BorderColor;
}

void SetBoxSize(double value)
Expand All @@ -307,7 +310,9 @@ void SetBoxSize(double value)
boxSelected.WidthRequest = value * .6; //old value 0.72
boxSelected.HeightRequest = value * 0.6;
//lblSelected.FontSize = value * 0.72; //old value 0.76 //TODO: Do something to resizing
this.Children[0].MinimumWidthRequest = value * 1.4;

// TODO: Refactor after MAUI update
(this.Children[0] as View).MinimumWidthRequest = value * 1.4;
}

void UpdateType(CheckType _Type)
Expand Down Expand Up @@ -364,7 +369,7 @@ protected virtual void InitVisualStates()
TargetType = typeof(CheckBox),
Setters =
{
new Setter { Property = CheckBox.IsPressedProperty, Value = true }
new Setter { Property = IsPressedProperty, Value = true }
}
},
new VisualState
Expand All @@ -373,7 +378,7 @@ protected virtual void InitVisualStates()
TargetType = typeof(RadioButton),
Setters =
{
new Setter { Property = CheckBox.IsPressedProperty, Value = false }
new Setter { Property = IsPressedProperty, Value = false }
}
}
}
Expand Down
Loading