Skip to content

Commit

Permalink
Remove the padding around checkboxes (#7379)
Browse files Browse the repository at this point in the history
* Remove the padding around checkboxes

* Fix Windows

* Just centre the image and let the OS do the rest
  • Loading branch information
mattleibow committed May 23, 2022
1 parent 0c238bc commit 9dedbf6
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/Core/src/Handlers/CheckBox/CheckBoxHandler.Android.cs
@@ -1,14 +1,15 @@
using Android.Views;
using Android.Widget;
using AndroidX.AppCompat.Widget;
using Google.Android.Material.CheckBox;

namespace Microsoft.Maui.Handlers
{
public partial class CheckBoxHandler : ViewHandler<ICheckBox, AppCompatCheckBox>
{
protected override AppCompatCheckBox CreatePlatformView()
{
var platformCheckBox = new AppCompatCheckBox(Context)
var platformCheckBox = new MaterialCheckBox(Context)
{
SoundEffectsEnabled = false
};
Expand Down
40 changes: 39 additions & 1 deletion src/Core/src/Handlers/CheckBox/CheckBoxHandler.Windows.cs
@@ -1,11 +1,49 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;

namespace Microsoft.Maui.Handlers
{
public partial class CheckBoxHandler : ViewHandler<ICheckBox, CheckBox>
{
protected override CheckBox CreatePlatformView() => new CheckBox();
protected override CheckBox CreatePlatformView()
{
var checkBox = new CheckBox();

AdjustCheckBoxForNoText(checkBox);

return checkBox;
}

static void AdjustCheckBoxForNoText(CheckBox checkBox)
{
checkBox.MinWidth = 0;
checkBox.MinHeight = 0;
checkBox.Padding = new UI.Xaml.Thickness(0);

checkBox.Loaded += OnCheckBoxLoaded;

static void OnCheckBoxLoaded(object sender, RoutedEventArgs e)
{
if (sender is not CheckBox checkBox)
return;

checkBox.Loaded -= OnCheckBoxLoaded;

if (VisualTreeHelper.GetChildrenCount(checkBox) <= 0)
return;

var root = VisualTreeHelper.GetChild(checkBox, 0);
if (root is not Grid rootGrid)
return;

var checkBoxHeight = Application.Current.Resources.TryGet<double>("CheckBoxHeight");
var checkBoxSize = Application.Current.Resources.TryGet<double>("CheckBoxSize");
var margin = (checkBoxHeight - checkBoxSize) / 2.0;

rootGrid.Margin = new UI.Xaml.Thickness(margin);
}
}

protected override void ConnectHandler(CheckBox platformView)
{
Expand Down
9 changes: 8 additions & 1 deletion src/Core/src/Platform/Android/Resources/values/styles.xml
Expand Up @@ -9,6 +9,7 @@
<item name="appBarLayoutStyle">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item>
<item name="bottomNavigationViewStyle">@style/Widget.Design.BottomNavigationView</item>
<item name="materialButtonStyle">@style/MauiMaterialButton</item>
<item name="checkboxStyle">@style/MauiCheckBox</item>
<item name="android:textAllCaps">false</item>
</style>
<style name="Maui.MainTheme.NoActionBar" parent="Maui.MainTheme">
Expand Down Expand Up @@ -52,7 +53,13 @@
<item name="android:insetLeft">0dp</item>
<item name="android:insetRight">0dp</item>
</style>


<style name="MauiCheckBox" parent="Widget.Material3.CompoundButton.CheckBox">
<!-- remove all the min sizes as MAUI manages it -->
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
</style>

<!--
The collectionViewScrollBars style will be used as the default style for ItemsViewRenderer (the base renderer
for CollectionView and CarouselView. We have to use a style to set up the scrollbars because there is currently
Expand Down
17 changes: 13 additions & 4 deletions src/Core/src/Platform/Windows/ResourceDictionaryExtensions.cs
@@ -1,12 +1,21 @@
using System;
#nullable enable
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.UI.Xaml;

namespace Microsoft.Maui.Platform
{
internal static class ResourceDictionaryExtensions
{
public static void AddLibraryResources(this UI.Xaml.ResourceDictionary resources, string key, string uri)
public static T? TryGet<T>(this UI.Xaml.ResourceDictionary? resources, string key)
{
if (resources?.ContainsKey(key) == true && resources[key] is T typed)
return typed;
return default;
}

public static void AddLibraryResources(this UI.Xaml.ResourceDictionary? resources, string key, string uri)
{
if (resources == null)
return;
Expand All @@ -24,7 +33,7 @@ public static void AddLibraryResources(this UI.Xaml.ResourceDictionary resources
}
}

public static void AddLibraryResources<T>(this UI.Xaml.ResourceDictionary resources)
public static void AddLibraryResources<T>(this UI.Xaml.ResourceDictionary? resources)
where T : UI.Xaml.ResourceDictionary, new()
{
var dictionaries = resources?.MergedDictionaries;
Expand Down Expand Up @@ -56,7 +65,7 @@ internal static void RemoveKeys(this UI.Xaml.ResourceDictionary resources, IEnum
}
}

internal static void SetValueForAllKey(this UI.Xaml.ResourceDictionary resources, IEnumerable<string> keys, object value)
internal static void SetValueForAllKey(this UI.Xaml.ResourceDictionary resources, IEnumerable<string> keys, object? value)
{
foreach (string key in keys)
{
Expand Down
16 changes: 2 additions & 14 deletions src/Core/src/Platform/iOS/MauiCheckBox.cs
Expand Up @@ -22,7 +22,6 @@ public class MauiCheckBox : UIButton
Color? _tintColor;
bool _isChecked;
bool _isEnabled;
float _minimumViewSize;
bool _disposed;

public EventHandler? CheckedChanged;
Expand All @@ -31,7 +30,7 @@ public MauiCheckBox()
{
ContentMode = UIViewContentMode.Center;
ImageView.ContentMode = UIViewContentMode.ScaleAspectFit;
HorizontalAlignment = UIControlContentHorizontalAlignment.Left;
HorizontalAlignment = UIControlContentHorizontalAlignment.Center;
VerticalAlignment = UIControlContentVerticalAlignment.Center;
#pragma warning disable CA1416 // TODO: both has [UnsupportedOSPlatform("ios15.0")]
AdjustsImageWhenDisabled = false;
Expand All @@ -46,18 +45,7 @@ void OnTouchUpInside(object? sender, EventArgs e)
CheckedChanged?.Invoke(this, EventArgs.Empty);
}

internal float MinimumViewSize
{
get { return _minimumViewSize; }
set
{
_minimumViewSize = value;
var xOffset = (value - DefaultSize + LineWidth) / 4;
#pragma warning disable CA1416 // TODO: 'ContentEdgeInsets' has [UnsupportedOSPlatform("ios15.0")]
ContentEdgeInsets = new UIEdgeInsets(0, xOffset, 0, 0);
#pragma warning restore CA1416
}
}
internal float MinimumViewSize { get; set; }

public bool IsChecked
{
Expand Down

0 comments on commit 9dedbf6

Please sign in to comment.