Skip to content

Commit

Permalink
wire up catalyst key events
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Aug 3, 2023
1 parent dfc6fe2 commit 0d8c61a
Show file tree
Hide file tree
Showing 12 changed files with 136 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Shandler.Samples/Shandler.Samples.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>Shandler.Samples</RootNamespace>
<UseMaui>true</UseMaui>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst;net7.0-android;net7.0-ios;net7.0-maccatalyst;net7.0</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0;net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<UseMaui>true</UseMaui>
Expand Down
7 changes: 6 additions & 1 deletion ShanedlerSamples/KeyboardPage.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ShanedlerSamples.KeyboardPage"
xmlns:local="clr-namespace:Maui.FixesAndWorkarounds"
Title="KeyboardPage">

<ContentPage.Behaviors>
<local:KeyboardBehavior></local:KeyboardBehavior>
</ContentPage.Behaviors>
<VerticalStackLayout>
<Entry Text="input field" x:Name="inputField"></Entry>
<Entry Text="input field" x:Name="firstFocusMe" Loaded="OnEntryLoaded"></Entry>
Expand Down
11 changes: 10 additions & 1 deletion ShanedlerSamples/Library/Common/HostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
#if ANDROID || IOS || MACCATALYST || WINDOWS
handlers.AddHandler(typeof(Frame), typeof(CustomFrameRenderer));
#endif
});
#if IOS || MACCATALYST
PageHandler.PlatformViewFactory = (handler) =>
{
var vc = new CustomKeyboardController(handler.VirtualView, handler.MauiContext);
handler.ViewController = vc;
return (Microsoft.Maui.Platform.ContentView)vc.View.Subviews[0];
};
#endif
});

if (addAllWorkaround)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#if IOS || MACCATALYST
using System;
using Foundation;
using Microsoft.Maui.Platform;
using UIKit;

namespace Maui.FixesAndWorkarounds
{
public class CustomKeyboardController : PageViewController
{
static List<KeyboardBehavior> keyboardBehaviors = new List<KeyboardBehavior>();
public CustomKeyboardController(IView page, IMauiContext mauiContext) : base(page, mauiContext)
{
}


public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesBegan(presses, evt);

foreach (var item in keyboardBehaviors)
item.PressesBegan(presses, evt);

}

public override void PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesEnded(presses, evt);

foreach (var item in keyboardBehaviors)
item.PressesEnded(presses, evt);
}

public static void Register(KeyboardBehavior keyboardBehavior)
{
keyboardBehaviors.Add(keyboardBehavior);
}

public static void UnRegister(KeyboardBehavior keyboardBehavior)
{
keyboardBehaviors.Remove(keyboardBehavior);
}
}


}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace Maui.FixesAndWorkarounds
{
public partial class KeyboardBehavior : PlatformBehavior<View>
{
protected override void OnAttachedTo(View bindable, FrameworkElement platformView)
protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView)
{
base.OnAttachedTo(bindable, platformView);
platformView.KeyDown += OnKeyDown;
Expand All @@ -19,7 +19,7 @@ protected override void OnAttachedTo(View bindable, FrameworkElement platformVie
platformView.PreviewKeyUp += OnPreviewKeyUp;
}

protected override void OnDetachedFrom(View bindable, FrameworkElement platformView)
protected override void OnDetachedFrom(VisualElement bindable, FrameworkElement platformView)
{
base.OnDetachedFrom(bindable, platformView);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace Maui.FixesAndWorkarounds
{
public partial class KeyboardBehavior : PlatformBehavior<View>
public partial class KeyboardBehavior : PlatformBehavior<VisualElement>
{
}
}
27 changes: 25 additions & 2 deletions ShanedlerSamples/Library/KeyboardBehavior/KeyboardBehavior.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,34 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Foundation;
using UIKit;

namespace Maui.FixesAndWorkarounds
{
public partial class KeyboardBehavior : PlatformBehavior<View>
public partial class KeyboardBehavior : PlatformBehavior<VisualElement>
{
}
protected override void OnAttachedTo(VisualElement bindable, UIView platformView)
{
base.OnAttachedTo(bindable, platformView);
CustomKeyboardController.Register(this);
}

protected override void OnDetachedFrom(VisualElement bindable, UIView platformView)
{
base.OnDetachedFrom(bindable, platformView);
CustomKeyboardController.UnRegister(this);
}

public void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
{
System.Diagnostics.Debug.WriteLine($"PressesBegan: {evt}");
}

public void PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
{
System.Diagnostics.Debug.WriteLine($"PressesEnded: {evt}");
}
}
}
#endif
7 changes: 5 additions & 2 deletions ShanedlerSamples/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ShanedlerSamples.MainPage"
xmlns:local="clr-namespace:Maui.FixesAndWorkarounds"
Title="First Page">

Title="First Page"
>
<ContentPage.Behaviors>
<local:KeyboardBehavior></local:KeyboardBehavior>
</ContentPage.Behaviors>
<ScrollView>
<VerticalStackLayout
Spacing="25"
Expand Down
35 changes: 25 additions & 10 deletions ShanedlerSamples/Platforms/MacCatalyst/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
using Foundation;
using Maui.FixesAndWorkarounds;
using UIKit;

namespace ShanedlerSamples;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
static List<KeyboardBehavior> keyboardBehaviors = new List<KeyboardBehavior>();
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesBegan(presses, evt);

foreach (var item in keyboardBehaviors)
item.PressesBegan(presses, evt);

public override void PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesBegan(presses, evt);
}
}

public override void PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesEnded(presses, evt);
}
public override void PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
{
base.PressesEnded(presses, evt);

public override UIKeyCommand[] KeyCommands => base.KeyCommands;
foreach (var item in keyboardBehaviors)
item.PressesEnded(presses, evt);
}

public static void Register(KeyboardBehavior keyboardBehavior)
{
keyboardBehaviors.Add(keyboardBehavior);
}

public static void UnRegister(KeyboardBehavior keyboardBehavior)
{
keyboardBehaviors.Remove(keyboardBehavior);
}
}
4 changes: 4 additions & 0 deletions ShanedlerSamples/Platforms/iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using Foundation;
using Maui.FixesAndWorkarounds;
using UIKit;

namespace ShanedlerSamples;

[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();


}
9 changes: 6 additions & 3 deletions ShanedlerSamples/ShanedlerSamples.csproj
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net7.0-android;net7.0-ios;net7.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net7.0-windows10.0.19041.0</TargetFrameworks>
<TargetFrameworks>net8.0-android;net8.0-ios;net8.0-maccatalyst</TargetFrameworks>
<TargetFrameworks Condition="$([MSBuild]::IsOSPlatform('windows'))">$(TargetFrameworks);net8.0-windows10.0.19041.0</TargetFrameworks>
<!-- Uncomment to also build the tizen app. You will need to install tizen by following this: https://github.com/Samsung/Tizen.NET -->
<!-- <TargetFrameworks>$(TargetFrameworks);net7.0-tizen</TargetFrameworks> -->
<!-- <TargetFrameworks>$(TargetFrameworks);net8.0-tizen</TargetFrameworks> -->
<OutputType>Exe</OutputType>
<RootNamespace>ShanedlerSamples</RootNamespace>
<UseMaui>true</UseMaui>
Expand All @@ -30,6 +30,9 @@
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'tizen'">6.5</SupportedOSPlatformVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net8.0-ios|AnyCPU'">
<CreatePackage>false</CreatePackage>
</PropertyGroup>
<ItemGroup>
<!-- App Icon -->
<MauiIcon Include="Resources\AppIcon\appicon.svg" ForegroundFile="Resources\AppIcon\appiconfg.svg" Color="#512BD4" />
Expand Down

0 comments on commit 0d8c61a

Please sign in to comment.