Skip to content

Commit

Permalink
Cache checkbox locally (#6840)
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen authored May 5, 2022
1 parent edc95a9 commit 228f932
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
6 changes: 5 additions & 1 deletion src/Core/src/Platform/ElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public static IElementHandler ToHandler(this IElement view, IMauiContext context
}

if (handler == null)
throw new Exception($"Handler not found for view {view}.");
throw new HandlerNotFoundException($"Handler not found for view {view}.");

handler.SetMauiContext(context);

Expand All @@ -104,6 +104,10 @@ public static IElementHandler ToHandler(this IElement view, IMauiContext context
{
throw;
}
catch (HandlerNotFoundException)
{
throw;
}
catch (Exception exc)
{
throw new ToPlatformException($"{handler} found for {view} is incompatible", exc);
Expand Down
26 changes: 26 additions & 0 deletions src/Core/src/Platform/HandlerNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Text;

namespace Microsoft.Maui.Platform
{
class HandlerNotFoundException : System.Exception
{
public HandlerNotFoundException()
{
}

public HandlerNotFoundException(string message) : base(message)
{
}

public HandlerNotFoundException(string message, Exception innerException) : base(message, innerException)
{
}

protected HandlerNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
}
34 changes: 20 additions & 14 deletions src/Core/src/Platform/iOS/MauiCheckBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public class MauiCheckBox : UIButton

static UIImage? Checked;
static UIImage? Unchecked;
UIImage? CheckedDisabledAndTinted;
UIImage? UncheckedDisabledAndTinted;

UIAccessibilityTrait _accessibilityTraits;

Color? _tintColor;
Expand Down Expand Up @@ -91,6 +94,8 @@ public Color? CheckBoxTintColor
if (_tintColor == value)
return;

CheckedDisabledAndTinted = null;
UncheckedDisabledAndTinted = null;
_tintColor = value;
CheckBoxTintUIColor = CheckBoxTintColor?.ToPlatform();
}
Expand Down Expand Up @@ -136,43 +141,44 @@ public override bool Enabled
}
}

protected virtual UIImage GetCheckBoximage()
protected virtual UIImage GetCheckBoxImage()
{
// Ideally I would use the static images here but when disabled it always tints them grey
// and I don't know how to make it not tint them gray
if (!Enabled && CheckBoxTintColor != null)
{
if (IsChecked)
return CreateCheckBox(CreateCheckMark()).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
{
return CheckedDisabledAndTinted ??=
CreateCheckBox(CreateCheckMark()).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}

return CreateCheckBox(null).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
return UncheckedDisabledAndTinted ??=
CreateCheckBox(null).ImageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal);
}

if (Checked == null)
Checked = CreateCheckBox(CreateCheckMark()).ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

if (Unchecked == null)
Unchecked = CreateCheckBox(null).ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Checked ??= CreateCheckBox(CreateCheckMark()).ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
Unchecked ??= CreateCheckBox(null).ImageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);

return IsChecked ? Checked : Unchecked;
}

internal virtual UIBezierPath CreateBoxPath(CGRect backgroundRect) => UIBezierPath.FromOval(backgroundRect);
internal virtual UIBezierPath CreateCheckPath() => new UIBezierPath
UIBezierPath CreateBoxPath(CGRect backgroundRect) => UIBezierPath.FromOval(backgroundRect);
UIBezierPath CreateCheckPath() => new UIBezierPath
{
LineWidth = (nfloat)0.077,
LineCapStyle = CGLineCap.Round,
LineJoinStyle = CGLineJoin.Round
};

internal virtual void DrawCheckMark(UIBezierPath path)
void DrawCheckMark(UIBezierPath path)
{
path.MoveTo(new CGPoint(0.72f, 0.22f));
path.AddLineTo(new CGPoint(0.33f, 0.6f));
path.AddLineTo(new CGPoint(0.15f, 0.42f));
}

internal virtual UIImage CreateCheckBox(UIImage? check)
UIImage CreateCheckBox(UIImage? check)
{
UIGraphics.BeginImageContextWithOptions(new CGSize(DefaultSize, DefaultSize), false, 0);
var context = UIGraphics.GetCurrentContext();
Expand Down Expand Up @@ -208,7 +214,7 @@ internal virtual UIImage CreateCheckBox(UIImage? check)
return img;
}

internal UIImage CreateCheckMark()
UIImage CreateCheckMark()
{
UIGraphics.BeginImageContextWithOptions(new CGSize(DefaultSize, DefaultSize), false, 0);
var context = UIGraphics.GetCurrentContext();
Expand Down Expand Up @@ -263,7 +269,7 @@ protected override void Dispose(bool disposing)

void UpdateDisplay()
{
SetImage(GetCheckBoximage(), UIControlState.Normal);
SetImage(GetCheckBoxImage(), UIControlState.Normal);
SetNeedsDisplay();
}

Expand Down

0 comments on commit 228f932

Please sign in to comment.