-
-
Notifications
You must be signed in to change notification settings - Fork 56
chore: refactor and add localization to Accelerator Service #680
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
Open
United600
wants to merge
9
commits into
main
Choose a base branch
from
United600/accelerator-service
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+8,674
−803
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
42b8802
add localization support to accelerator service and minor fix for mul…
United600 60a1798
replace keyboard accelerator dynamic list with array
United600 f6553ca
refactor keyboard accelerator localization logic
United600 44eb283
move accelerator service to the extensions folder
United600 fa7294a
add keyboard accelerator translations for multiple locales
United600 a3ca9a8
remove localized string resources for numeric keys (0-9)
United600 e026a81
improve accelerator tooltip formatting for right-to-left languages
United600 f4649cb
improve accelerator service comment clarity
United600 f1a7f0a
remove redundant comments about RTL tooltip formatting
United600 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,101 @@ | ||
#nullable enable | ||
|
||
using System.Globalization; | ||
using Screenbox.Helpers; | ||
using Windows.UI.Xaml; | ||
using Windows.UI.Xaml.Controls; | ||
|
||
namespace Screenbox.Extensions; | ||
|
||
/// <summary> | ||
/// Represents a service that provides <see langword="static"/> methods to display a <see cref="ToolTip"/>, | ||
/// with the corresponding key combinations appended. | ||
/// </summary> | ||
/// <remarks>If a control has more than one accelerator defined, only the first is displayed.</remarks> | ||
/// <example> | ||
/// In this example, we specify the ToolTip for a Button. The keyboard accelerator is | ||
/// displayed in the UI element flyout as "Create a new document (Ctrl+Alt+N)". | ||
/// <code lang="xml"> | ||
/// <Button Content="New" local:AcceleratorService.ToolTip="Create a new document"> | ||
/// <Button.KeyboardAccelerators> | ||
/// <KeyboardAccelerator Key="N" Modifiers="Control,Menu" /> | ||
/// </Button.KeyboardAccelerators> | ||
/// </Button> | ||
/// </code> | ||
/// or | ||
/// <code lang="csharp"> | ||
/// var button = new Button { Content = "New" }; | ||
/// AcceleratorService.SetToolTip(button, "Create a new document"); | ||
/// button.KeyboardAccelerators.Add(new KeyboardAccelerator | ||
/// { | ||
/// Key = VirtualKey.N, | ||
/// Modifiers = KeyboardAcceleratorModifiers.Control | KeyboardAcceleratorModifiers.Menu | ||
/// }); | ||
/// </code> | ||
/// </example> | ||
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Foundation.UniversalApiContract), 327680u)] | ||
public sealed class AcceleratorService | ||
{ | ||
private static readonly bool _isHebrew = CultureInfo.CurrentCulture.TwoLetterISOLanguageName is "he"; | ||
|
||
/// <summary> | ||
/// Identifies the AcceleratorService.ToolTip XAML attached property. | ||
/// </summary> | ||
public static readonly DependencyProperty ToolTipProperty = DependencyProperty.RegisterAttached( | ||
"ToolTip", typeof(string), typeof(AcceleratorService), new PropertyMetadata(string.Empty, OnToolTipPropertyChanged)); | ||
|
||
/// <summary> | ||
/// Gets the value of the AcceleratorService.ToolTip XAML attached property for a <see cref="UIElement"/>. | ||
/// </summary> | ||
/// <param name="element">The element from which the property value is read.</param> | ||
/// <returns>The string tooltip content.</returns> | ||
public static string GetToolTip(UIElement element) | ||
{ | ||
return (string)element.GetValue(ToolTipProperty); | ||
} | ||
|
||
/// <summary> | ||
/// Sets the value of the AcceleratorService.ToolTip XAML attached property. | ||
/// </summary> | ||
/// <param name="element">The element to set tooltip content on.</param> | ||
/// <param name="value">The string to set as tooltip content.</param> | ||
public static void SetToolTip(UIElement element, string value) | ||
{ | ||
element.SetValue(ToolTipProperty, value); | ||
} | ||
|
||
private static void OnToolTipPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (d is UIElement element && e.NewValue is string value) | ||
{ | ||
string toolTipString = value; | ||
|
||
if (DeviceInfoHelper.IsKeyboardPresent) | ||
{ | ||
var keyboardAccelerators = element.KeyboardAccelerators; | ||
if (keyboardAccelerators.Count > 0) | ||
{ | ||
var keyboardAccelerator = keyboardAccelerators[0]; | ||
if (keyboardAccelerator.IsEnabled) | ||
{ | ||
string keyboardAcceleratorText = keyboardAccelerator.ToDisplayName(); | ||
if (!string.IsNullOrEmpty(keyboardAcceleratorText)) | ||
{ | ||
// For Hebrew, we enclose the accelerator text in left-to-right (LTR) embedding marks | ||
// to ensure modifiers appear before any translated key. | ||
// Example: הסר+Ctrl+Alt ["Clear"+Ctrl+Alt] becomes Ctrl+Alt+הסר [Ctrl+Alt+"Clear"] | ||
// | ||
// In right-to-left (RTL) languages, the system automatically adjusts the tooltip format for proper readability. | ||
// The default format "{value} ({keyboardAcceleratorText})" is reversed to "({keyboardAcceleratorText}) {value}". | ||
toolTipString = _isHebrew | ||
? $"{value} \u202A({keyboardAcceleratorText})\u202C" | ||
: $"{value} ({keyboardAcceleratorText})"; | ||
} | ||
} | ||
} | ||
} | ||
|
||
ToolTipService.SetToolTip(element, toolTipString); | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,23 +1,39 @@ | ||
using Windows.System.Profile; | ||
using Windows.Devices.Input; | ||
using Windows.System.Profile; | ||
|
||
namespace Screenbox.Helpers; | ||
|
||
/// <summary> | ||
/// Provides <see langword="static"/> helper methods to get information about the system. | ||
/// </summary> | ||
public static partial class DeviceInfoHelper | ||
public static class DeviceInfoHelper | ||
{ | ||
private static readonly KeyboardCapabilities _keyboardCapabilities = new(); | ||
private static readonly TouchCapabilities _touchCapabilities = new(); | ||
|
||
public static readonly string DeviceFamily = AnalyticsInfo.VersionInfo.DeviceFamily; | ||
|
||
/// <summary> | ||
/// Gets whether the current device is a desktop/laptop/tablet. | ||
/// </summary> | ||
/// <returns><see langword="true"/> if the device is a desktop; otherwise, <see langword="false"/>.</returns> | ||
public static bool IsDesktop => DeviceFamily == "Windows.Desktop"; | ||
public static readonly bool IsDesktop = DeviceFamily == "Windows.Desktop"; | ||
|
||
/// <summary> | ||
/// Gets whether the current device is an Xbox. | ||
/// </summary> | ||
/// <returns><see langword="true"/> if the device is an Xbox; otherwise, <see langword="false"/>.</returns> | ||
public static bool IsXbox => DeviceFamily == "Windows.Xbox"; | ||
public static readonly bool IsXbox = (DeviceFamily == "Windows.Xbox") || (DeviceFamily == "Windows.XBoxSRA") || (DeviceFamily == "Windows.XBoxERA"); | ||
|
||
/// <summary> | ||
/// Gets whether the current device has a physical keyboard connected. | ||
/// </summary> | ||
/// <returns><see langword="true"/> if a keyboard is detected; otherwise, <see langword="false"/>.</returns> | ||
public static bool IsKeyboardPresent => _keyboardCapabilities.KeyboardPresent != 0; | ||
|
||
/// <summary> | ||
/// Gets whether the current device has a touch digitizer. | ||
/// </summary> | ||
/// <returns><see langword="true"/> if touch input is supported; otherwise, <see langword="false"/>.</returns> | ||
public static bool IsTouchPresent => _touchCapabilities.TouchPresent != 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The device family names contain inconsistent capitalization. 'XBoxSRA' and 'XBoxERA' should likely be 'XboxSRA' and 'XboxERA' to match the standard Xbox naming convention used in 'Windows.Xbox'.
Copilot uses AI. Check for mistakes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took the values directly from the Microsoft UI Xaml repository:
https://github.com/microsoft/microsoft-ui-xaml/blob/232a16e5ddfc22c9a1b79a2c51abeb9a39a94494/dev/dll/SharedHelpers.cpp#L370