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

Better handler implementation #4

Merged
merged 5 commits into from
Apr 6, 2023
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
5 changes: 5 additions & 0 deletions Demo/App.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<CodesignKey>iPhone Distribution: CARDIFF COUNTY COUNCIL (RHSME4M265)</CodesignKey>
</PropertyGroup>

<PropertyGroup Condition="'$(TargetFramework)'=='net7.0-ios'">
<CodesignKey>Apple Development: Ieuan Walker (R4SVVV33HW)</CodesignKey>
<CodesignProvision>VS: WildCard Development</CodesignProvision>
</PropertyGroup>

<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
Expand Down
28 changes: 1 addition & 27 deletions Scr/AppBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace StateButton;
/// <summary>
/// This class contains CustomSwitch <see cref="MauiAppBuilder"/> extensions.
/// </summary>
public static partial class AppBuilderExtensions
public static class AppBuilderExtensions
{
/// <summary>
/// Initializes the Switch control
Expand All @@ -16,32 +16,6 @@ public static MauiAppBuilder UseStateButton(this MauiAppBuilder builder)
{
builder.ConfigureMauiHandlers(h => h.AddHandler<StateButton, StateButtonHandler>());

#if ANDROID
StateButtonHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
if (view is StateButton stateButton)
{
CustomContentViewGroup platformView = (CustomContentViewGroup)handler.PlatformView;

platformView.Pressed1 += (_, e) => stateButton.InternalPressed();
platformView.Released += (_, e) => stateButton.InternalReleased();
platformView.Clicked += (_, e) => stateButton.InternalClicked();
}
});
#elif IOS
StateButtonHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
{
if (view is StateButton stateButton)
{
CustomContentView platformView = (CustomContentView)handler.PlatformView;

platformView.Pressed += (_, e) => stateButton.InternalPressed();
platformView.Released += (_, e) => stateButton.InternalReleased();
platformView.Clicked += (_, e) => stateButton.InternalClicked();
}
});
#endif

return builder;
}
}
89 changes: 0 additions & 89 deletions Scr/Handler/CustomContentViewGroup.Android.cs

This file was deleted.

6 changes: 6 additions & 0 deletions Scr/Handler/IStateButtonHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using Microsoft.Maui.Handlers;

namespace StateButton.Handler;
public interface IStateButtonHandler : IBorderHandler
{
}
2 changes: 1 addition & 1 deletion Scr/Handler/StateButtonHandler.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ protected override ContentViewGroup CreatePlatformView()
{
base.CreatePlatformView();

return new CustomContentViewGroup(Context);
return new CustomContentViewGroup(Context, VirtualView);
}
}
2 changes: 1 addition & 1 deletion Scr/Handler/StateButtonHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

namespace StateButton.Handler;

public partial class StateButtonHandler : BorderHandler
public partial class StateButtonHandler : BorderHandler, IStateButtonHandler
{
}
2 changes: 1 addition & 1 deletion Scr/Handler/StateButtonHandler.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ protected override Microsoft.Maui.Platform.ContentView CreatePlatformView()
{
base.CreatePlatformView();

return new CustomContentView();
return new CustomContentView(VirtualView);
}
}
30 changes: 0 additions & 30 deletions Scr/MultiTargeting.targets

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,33 @@ namespace StateButton.Handler;

public class CustomContentView : Microsoft.Maui.Platform.ContentView
{
public event EventHandler<EventArgs>? Released;
public event EventHandler<EventArgs>? Pressed;
public event EventHandler<EventArgs>? Clicked;

public CustomContentView()
readonly StateButton _stateButton;
public CustomContentView(IBorderView virtualView)
{
AccessibilityTraits = UIAccessibilityTrait.Button;

AddGestureRecognizer(new UITapGestureRecognizer(() => Clicked?.Invoke(this, EventArgs.Empty)));
_stateButton = (StateButton)virtualView;

AddGestureRecognizer(new UITapGestureRecognizer(_stateButton.InvokeClicked));
}

public override void TouchesMoved(NSSet touches, UIEvent? evt)
{
Released?.Invoke(this, EventArgs.Empty);
_stateButton.InvokeReleased();

base.TouchesMoved(touches, evt);
}

public override void TouchesBegan(NSSet touches, UIEvent? evt)
{
Pressed?.Invoke(this, EventArgs.Empty);
_stateButton.InvokePressed();

base.TouchesBegan(touches, evt);
}

public override void TouchesCancelled(NSSet touches, UIEvent? evt)
{
Released?.Invoke(this, EventArgs.Empty);
_stateButton.InvokeReleased();

base.TouchesCancelled(touches, evt);
}
Expand Down
85 changes: 85 additions & 0 deletions Scr/Platform/CustomContentViewGroup.Android.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Views.Accessibility;
using Microsoft.Maui.Platform;

namespace StateButton.Handler;
public class CustomContentViewGroup : ContentViewGroup
{
Rect _rect;
readonly StateButton _stateButton;
public CustomContentViewGroup(Context context, IBorderView virtualView) : base(context)
{
_stateButton = (StateButton)virtualView;

Focusable = true;
Clickable = true;
}

public override bool OnTouchEvent(MotionEvent? e)
{
if(RootView is null)
{
return base.OnTouchEvent(e);
}

switch (e?.Action)
{
case MotionEventActions.Down:
_rect = new Rect(RootView.Left, RootView.Top, RootView.Right, RootView.Bottom);
_stateButton.InvokePressed();
break;

case MotionEventActions.Up:
if (_rect.Contains(RootView.Left + (int)e.GetX(), RootView.Top + (int)e.GetY()))
{
_stateButton.InvokeReleased();
_stateButton.InvokeClicked();
}
else
{
_stateButton.InvokeReleased();
}
break;

case MotionEventActions.Cancel:
_stateButton.InvokeReleased();

break;

case MotionEventActions.Move:
if (!_rect.Contains(RootView.Left + (int)e.GetX(), RootView.Top + (int)e.GetY()))
{
_stateButton.InvokeReleased();
}

break;
}

return base.OnTouchEvent(e);
}

public override bool OnKeyUp([GeneratedEnum] Keycode keyCode, KeyEvent? e)
{
if (keyCode == Keycode.Space || keyCode == Keycode.Enter)
{
_stateButton.InvokeClicked();
return true;
}

return base.OnKeyUp(keyCode, e);
}

public override void OnInitializeAccessibilityNodeInfo(AccessibilityNodeInfo? info)
{
if (info is not null)
{
info.Focusable = true;
info.Clickable = true;
info.ClassName = "android.widget.Button";
}

base.OnInitializeAccessibilityNodeInfo(info);
}
}
15 changes: 13 additions & 2 deletions Scr/StateButton.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">11.0</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'android'">21.0</SupportedOSPlatformVersion>
</PropertyGroup>

Expand Down Expand Up @@ -60,5 +60,16 @@
</MauiXaml>
</ItemGroup>

<Import Project="MultiTargeting.targets" />
<ItemGroup Condition="$(TargetFramework.StartsWith('net7.0-ios')) != true">
<Compile Remove="**\**\*.ios.cs" />
<None Include="**\**\*.ios.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('MonoAndroid')) != true AND $(TargetFramework.StartsWith('net7.0-android')) != true">
<Compile Remove="**\**\*.android.cs" />
<None Include="**\**\*.android.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
<ItemGroup Condition="!($(TargetFramework.StartsWith('net')) == true AND $(TargetFramework.EndsWith('.0')) == true AND $(TargetFramework.Contains('-')) != true)">
<Compile Remove="**\**\*.net.cs" />
<None Include="**\**\*.net.cs" Exclude="$(DefaultItemExcludes);$(DefaultExcludesInProjectFolder)" />
</ItemGroup>
</Project>
Loading