-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
761 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 0 additions & 60 deletions
60
ShanedlerSamples/Library/KeyboardBehavior/CustomKeyboardController.cs
This file was deleted.
Oops, something went wrong.
122 changes: 122 additions & 0 deletions
122
ShanedlerSamples/Library/KeyboardBehavior/DeviceKeyboard.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
ShanedlerSamples/Library/KeyboardBehavior/KeyPressedEventArgs.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
ShanedlerSamples/Library/KeyboardBehavior/KeyboardBehaviorTrigger.Windows.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.