Skip to content

Commit

Permalink
- update keyboard behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
PureWeen committed Aug 4, 2023
1 parent 35e669a commit e9b0c54
Show file tree
Hide file tree
Showing 17 changed files with 761 additions and 120 deletions.
2 changes: 1 addition & 1 deletion ShanedlerSamples/Library/Common/HostExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static MauiAppBuilder ConfigureMauiWorkarounds(this MauiAppBuilder builde
#if IOS || MACCATALYST
PageHandler.PlatformViewFactory = (handler) =>
{
var vc = new CustomKeyboardController(handler.VirtualView, handler.MauiContext);
var vc = new KeyboardPageViewController(handler.VirtualView, handler.MauiContext);
handler.ViewController = vc;
return (Microsoft.Maui.Platform.ContentView)vc.View.Subviews[0];
};
Expand Down

This file was deleted.

122 changes: 122 additions & 0 deletions ShanedlerSamples/Library/KeyboardBehavior/DeviceKeyboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
using System.Diagnostics;

namespace Maui.FixesAndWorkarounds;

public static partial class DeviceKeyboard
{
static Task<bool> _getKeyboardNavigationEnabledTask;

public static Task<bool> GetKeyboardNavigationEnabledAsync(CancellationToken token = default)
{
if (_getKeyboardNavigationEnabledTask == null || _getKeyboardNavigationEnabledTask.IsCompleted)
_getKeyboardNavigationEnabledTask = GetKeyboardNavigationEnabledTask();

return _getKeyboardNavigationEnabledTask;
}

static async Task<bool> GetKeyboardNavigationEnabledTask(CancellationToken token = default)
{
var command = "defaults read -g AppleKeyboardUIMode";

ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{command}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using Process process = new() { StartInfo = startInfo };

string processOutput = null;
string processError = null;

void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
return;

processOutput = e.Data;
}

void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
return;

processError = e.Data.ToString();
}

process.OutputDataReceived += ProcessOutputDataReceived;
process.ErrorDataReceived += ProcessErrorDataReceived;

try
{
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
await process.WaitForExitAsync(token);
}
finally
{
process.OutputDataReceived -= ProcessOutputDataReceived;
process.ErrorDataReceived -= ProcessErrorDataReceived;
}

if (token.IsCancellationRequested)
return true; // Default to true

if (!string.IsNullOrWhiteSpace(processOutput) && int.TryParse(processOutput, out var result))
return result == 2; // Keyboard navigation enabled

// TODO: Review whether to return an actual enum with unknown type?

return true; // Default to true
}

public static async Task SetKeyboardNavigationEnabledAsync(bool enabled, CancellationToken token = default)
{
var command = $"defaults write -g AppleKeyboardUIMode -int {(enabled ? 2 : 0)}";

ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "/bin/bash",
Arguments = $"-c \"{command}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};

using Process process = new() { StartInfo = startInfo };

string processError = null;

void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.Data))
return;

processError = e.Data.ToString();
}

process.ErrorDataReceived += ProcessErrorDataReceived;

try
{
process.Start();
process.BeginErrorReadLine();
await process.WaitForExitAsync(token);
}
finally
{
process.ErrorDataReceived -= ProcessErrorDataReceived;
}

// TODO: Review whether to throw exception on failure or return updated state
if (!string.IsNullOrWhiteSpace(processError))
throw new Exception("Error setting Keyboard navigation", new Exception(processError));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Maui.FixesAndWorkarounds;

public sealed class KeyPressedEventArgs : EventArgs
{
public KeyboardModifiers Modifiers { get; internal set; }
public KeyboardKeys Keys { get; internal set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Maui.FixesAndWorkarounds
{
public partial class KeyboardBehavior : PlatformBehavior<View>
public partial class KeyboardBehavior : PlatformBehavior<VisualElement>
{
protected override void OnAttachedTo(VisualElement bindable, FrameworkElement platformView)
{
Expand Down
9 changes: 9 additions & 0 deletions ShanedlerSamples/Library/KeyboardBehavior/KeyboardBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,14 @@ namespace Maui.FixesAndWorkarounds
{
public partial class KeyboardBehavior : PlatformBehavior<VisualElement>
{
KeyboardBehaviorTriggers _triggers;

public KeyboardBehaviorTriggers Triggers => _triggers ??= new KeyboardBehaviorTriggers();

public event EventHandler<KeyPressedEventArgs> KeyDown;
public event EventHandler<KeyPressedEventArgs> KeyUp;

internal void RaiseKeyDown(KeyPressedEventArgs args) => KeyDown?.Invoke(this, args);
internal void RaiseKeyUp(KeyPressedEventArgs args) => KeyUp?.Invoke(this, args);
}
}
67 changes: 37 additions & 30 deletions ShanedlerSamples/Library/KeyboardBehavior/KeyboardBehavior.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,52 @@ namespace Maui.FixesAndWorkarounds
{
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 bool PressesBegan(NSSet<UIPress> presses, UIPressesEvent evt)
protected override void OnAttachedTo(VisualElement bindable, UIView platformView)
{
System.Diagnostics.Debug.WriteLine($"STARTING PressesBegan");
base.OnAttachedTo(bindable, platformView);

foreach (var item in evt.AllPresses)
if (item is UIPress allPress)
System.Diagnostics.Debug.WriteLine($"PressesBegan: {allPress?.Key}");
var page = GetParentPage(bindable);

System.Diagnostics.Debug.WriteLine($"PressesBegan: {evt}");
if (page == null)
return;

System.Diagnostics.Debug.WriteLine($"FINISHING PressesBegan");
// Register to key press events
if (page.Handler is not IPlatformViewHandler viewHandler ||
viewHandler.ViewController is not KeyboardPageViewController keyboardPageViewController)
return;

return true;
}
keyboardPageViewController.RegisterKeyboardBehavior(this);
}

public bool PressesEnded(NSSet<UIPress> presses, UIPressesEvent evt)
protected override void OnDetachedFrom(VisualElement bindable, UIView platformView)
{
System.Diagnostics.Debug.WriteLine($"STARTING PressesEnded");
foreach (var item in evt.AllPresses)
if (item is UIPress allPress)
System.Diagnostics.Debug.WriteLine($"PressesEnded: {allPress?.Key}");
base.OnDetachedFrom(bindable, platformView);

var page = GetParentPage(bindable);

if (page == null)
return;

// Unregister from key press events
if (page.Handler is not IPlatformViewHandler viewHandler ||
viewHandler.ViewController is not KeyboardPageViewController keyboardPageViewController)
return;

keyboardPageViewController.UnregisterKeyboardBehavior(this);
}

static Page GetParentPage(VisualElement element)
{
if (element is Page)
return element as Page;

Element currentElement = element;

System.Diagnostics.Debug.WriteLine($"PressesEnded: {evt}");
System.Diagnostics.Debug.WriteLine($"STARTING PressesEnded");
while (currentElement != null && currentElement is not Page)
currentElement = currentElement.Parent;

return true;
return currentElement as Page;
}
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#if WINDOWS

namespace Maui.FixesAndWorkarounds;

public sealed partial class KeyboardBehaviorTrigger
{
void SetPlatformModifiers(KeyboardModifiers modifiers)
=> throw new NotImplementedException();

void SetPlatformKeys(KeyboardKeys keys)
=> throw new NotImplementedException();
}

internal class KeyboardBehaviorTriggerComparer : IEqualityComparer<KeyboardBehaviorTrigger>
{
public bool Equals(KeyboardBehaviorTrigger x, KeyboardBehaviorTrigger y)
{
throw new NotImplementedException();
//if (x == null && y == null)
// return true;
//if (x == null || y == null)
// return false;

//return x.PlatformKeys.SequenceEqual(y.PlatformKeys) && x.PlatformModifiers == y.PlatformModifiers;
}

public int GetHashCode(KeyboardBehaviorTrigger obj)
{
throw new NotImplementedException();
//unchecked
//{
// int hash = 17;
// hash = hash * 23 + obj.Keys.GetHashCode();
// hash = hash * 23 + obj.PlatformModifiers.GetHashCode();
// return hash;
//}
}
}
#endif
Loading

0 comments on commit e9b0c54

Please sign in to comment.