-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
CheckBox Handlers #432
Merged
Merged
CheckBox Handlers #432
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
b992023
CheckBoxHandlers
jsuarezruiz 9eec93d
Merge branch 'main' into checkboxhandler
jsuarezruiz 4fc08ac
Merge branch 'main' into checkboxhandler
jsuarezruiz db1428a
Merge branch 'main' into checkboxhandler
jsuarezruiz f3f7c87
Merge branch 'main' into checkboxhandler
jsuarezruiz ab0c004
Merge branch 'main' into checkboxhandler
jsuarezruiz 2838439
Merge branch 'main' into checkboxhandler
jsuarezruiz 6ca7458
Merge branch 'main' into checkboxhandler
jsuarezruiz 6bc2352
Merge branch 'main' into checkboxhandler
jsuarezruiz 8be84ce
Merge branch 'main' into checkboxhandler
jsuarezruiz bc5cb8b
Merge branch 'main' into checkboxhandler
jsuarezruiz 69f2293
Merge branch 'main' into checkboxhandler
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace Microsoft.Maui.Controls | ||
{ | ||
public partial class CheckBox : ICheck | ||
{ | ||
|
||
} | ||
} |
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,8 @@ | ||
namespace Microsoft.Maui | ||
{ | ||
public interface ICheck : IView | ||
{ | ||
bool IsChecked { get; set; } | ||
Color Color { get; } | ||
} | ||
} |
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,57 @@ | ||
using Android.Widget; | ||
using AndroidX.AppCompat.Widget; | ||
|
||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class CheckBoxHandler : AbstractViewHandler<ICheck, AppCompatCheckBox> | ||
{ | ||
CheckedChangeListener ChangeListener { get; } = new CheckedChangeListener(); | ||
|
||
protected override AppCompatCheckBox CreateNativeView() | ||
{ | ||
var nativeCheckBox = new AppCompatCheckBox(Context) | ||
{ | ||
SoundEffectsEnabled = false | ||
}; | ||
|
||
nativeCheckBox.SetClipToOutline(true); | ||
|
||
return nativeCheckBox; | ||
} | ||
|
||
protected override void ConnectHandler(AppCompatCheckBox nativeView) | ||
{ | ||
ChangeListener.Handler = this; | ||
nativeView.SetOnCheckedChangeListener(ChangeListener); | ||
} | ||
|
||
protected override void DisconnectHandler(AppCompatCheckBox nativeView) | ||
{ | ||
ChangeListener.Handler = null; | ||
nativeView.SetOnCheckedChangeListener(null); | ||
} | ||
|
||
void OnCheckedChanged(bool isChecked) | ||
{ | ||
if (VirtualView != null) | ||
VirtualView.IsChecked = isChecked; | ||
} | ||
|
||
internal class CheckedChangeListener : Java.Lang.Object, CompoundButton.IOnCheckedChangeListener | ||
{ | ||
public CheckBoxHandler? Handler { get; set; } | ||
|
||
public CheckedChangeListener() | ||
{ | ||
} | ||
|
||
public void OnCheckedChanged(CompoundButton? nativeCheckBox, bool isChecked) | ||
{ | ||
if (Handler == null || nativeCheckBox == null) | ||
return; | ||
|
||
Handler.OnCheckedChanged(isChecked); | ||
} | ||
} | ||
} | ||
} |
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,9 @@ | ||
using System; | ||
|
||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class CheckBoxHandler : AbstractViewHandler<ICheck, object> | ||
{ | ||
protected override object CreateNativeView() => 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,39 @@ | ||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class CheckBoxHandler | ||
{ | ||
public static PropertyMapper<ICheck, CheckBoxHandler> CheckBoxMapper = new PropertyMapper<ICheck, CheckBoxHandler>(ViewHandler.ViewMapper) | ||
{ | ||
#if MONOANDROID | ||
[nameof(ICheck.BackgroundColor)] = MapBackgroundColor, | ||
#endif | ||
[nameof(ICheck.IsChecked)] = MapIsChecked, | ||
[nameof(ICheck.Color)] = MapColor | ||
}; | ||
|
||
public CheckBoxHandler() : base(CheckBoxMapper) | ||
{ | ||
|
||
} | ||
|
||
public CheckBoxHandler(PropertyMapper mapper) : base(mapper ?? CheckBoxMapper) | ||
{ | ||
|
||
} | ||
#if MONOANDROID | ||
public static void MapBackgroundColor(CheckBoxHandler handler, ICheck check) | ||
{ | ||
handler.TypedNativeView?.UpdateBackgroundColor(check); | ||
} | ||
#endif | ||
public static void MapIsChecked(CheckBoxHandler handler, ICheck check) | ||
{ | ||
handler.TypedNativeView?.UpdateIsChecked(check); | ||
} | ||
|
||
public static void MapColor(CheckBoxHandler handler, ICheck check) | ||
{ | ||
handler.TypedNativeView?.UpdateColor(check); | ||
} | ||
} | ||
} |
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,74 @@ | ||
using System; | ||
|
||
namespace Microsoft.Maui.Handlers | ||
{ | ||
public partial class CheckBoxHandler : AbstractViewHandler<ICheck, NativeCheckBox> | ||
{ | ||
protected virtual float MinimumSize => 44f; | ||
|
||
protected override NativeCheckBox CreateNativeView() | ||
{ | ||
return new NativeCheckBox | ||
{ | ||
MinimumViewSize = MinimumSize | ||
}; | ||
} | ||
|
||
protected override void ConnectHandler(NativeCheckBox nativeView) | ||
{ | ||
base.ConnectHandler(nativeView); | ||
|
||
nativeView.CheckedChanged += OnCheckedChanged; | ||
} | ||
|
||
protected override void DisconnectHandler(NativeCheckBox nativeView) | ||
{ | ||
base.DisconnectHandler(nativeView); | ||
|
||
nativeView.CheckedChanged -= OnCheckedChanged; | ||
} | ||
|
||
public override Size GetDesiredSize(double widthConstraint, double heightConstraint) | ||
{ | ||
var size = base.GetDesiredSize(widthConstraint, heightConstraint); | ||
|
||
var set = false; | ||
|
||
var width = widthConstraint; | ||
var height = heightConstraint; | ||
|
||
if (size.Width == 0) | ||
{ | ||
if (widthConstraint <= 0 || double.IsInfinity(widthConstraint)) | ||
{ | ||
width = MinimumSize; | ||
set = true; | ||
} | ||
} | ||
|
||
if (size.Height == 0) | ||
{ | ||
if (heightConstraint <= 0 || double.IsInfinity(heightConstraint)) | ||
{ | ||
height = MinimumSize; | ||
set = true; | ||
} | ||
} | ||
|
||
if (set) | ||
{ | ||
size = new Size(width, height); | ||
} | ||
|
||
return size; | ||
} | ||
|
||
void OnCheckedChanged(object? sender, EventArgs e) | ||
{ | ||
if (sender is NativeCheckBox nativeView && VirtualView != null) | ||
{ | ||
VirtualView.IsChecked = nativeView.IsChecked; | ||
} | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using Android.Content.Res; | ||
using Android.Graphics; | ||
using AndroidX.AppCompat.Widget; | ||
using AndroidX.Core.Widget; | ||
using AAttribute = Android.Resource.Attribute; | ||
using AColor = Android.Graphics.Color; | ||
using XColor = Microsoft.Maui.Color; | ||
|
||
namespace Microsoft.Maui | ||
{ | ||
public static class CheckBoxExtensions | ||
{ | ||
static readonly int[][] CheckedStates = new int[][] | ||
{ | ||
new int[] { AAttribute.StateEnabled, AAttribute.StateChecked }, | ||
new int[] { AAttribute.StateEnabled, -AAttribute.StateChecked }, | ||
new int[] { -AAttribute.StateEnabled, AAttribute.StateChecked }, | ||
new int[] { -AAttribute.StateEnabled, -AAttribute.StatePressed }, | ||
}; | ||
|
||
public static void UpdateBackgroundColor(this AppCompatCheckBox nativeCheckBox, ICheck check) | ||
{ | ||
if (check.BackgroundColor == XColor.Default) | ||
nativeCheckBox.SetBackgroundColor(AColor.Transparent); | ||
else | ||
nativeCheckBox.SetBackgroundColor(check.BackgroundColor.ToNative()); | ||
} | ||
|
||
public static void UpdateIsChecked(this AppCompatCheckBox nativeCheckBox, ICheck check) | ||
{ | ||
nativeCheckBox.Checked = check.IsChecked; | ||
} | ||
|
||
public static void UpdateColor(this AppCompatCheckBox nativeCheckBox, ICheck check) | ||
{ | ||
// TODO: Delete when implementing the logic to set the system accent color. | ||
XColor accent = XColor.FromHex("#ff33b5e5"); | ||
|
||
var tintColor = check.Color == XColor.Default ? accent.ToNative() : check.Color.ToNative(); | ||
|
||
var tintList = new ColorStateList( | ||
CheckedStates, | ||
new int[] | ||
{ | ||
tintColor, | ||
tintColor, | ||
tintColor, | ||
tintColor | ||
}); | ||
|
||
var tintMode = PorterDuff.Mode.SrcIn; | ||
|
||
CompoundButtonCompat.SetButtonTintList(nativeCheckBox, tintList); | ||
CompoundButtonCompat.SetButtonTintMode(nativeCheckBox, tintMode); | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Microsoft.Maui | ||
{ | ||
public static class CheckBoxExtensions | ||
{ | ||
public static void UpdateIsChecked(this object nothing, ICheck check) | ||
{ | ||
|
||
} | ||
|
||
public static void UpdateColor(this object nothing, ICheck check) | ||
{ | ||
|
||
} | ||
} | ||
} |
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.
Can we remove the Color stuff until we have resolved the Brush question?