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

Implementing IFormattable on KeyGesture #15828

Merged
merged 15 commits into from
Jun 4, 2024

Conversation

IanRawley
Copy link
Contributor

What does the pull request do?

Implements IFormattable on KeyGesture, and the associated KeyGestureFormatInfo class for platform specific KeyGesture formatting information. Follows the general outline provided by @MrJul here: #15393 (comment)

What is the current behavior?

Currently KeyGestures, when printed in user facing scenarios such as menus, are only nicely formatted on Windows, Linux, and MacOS/iOS. Other platforms like Android are left with basic Enum values. As seen in #15392

What is the updated/expected behavior with this PR?

All platforms have more appropriate results displayed in Menus, so as not to confuse users.

How was the solution implemented (if it's not obvious)?

The code for making KeyGesture strings more user-friendly overlapped a lot with the basic KeyGesture.ToString(), so as per the discussion in #15393 IFormattable has been implemented on KeyGesture, and an IFormatProvider implemented to provide the platform specific strings for specific Keys/Modifiers where they do not match the Enum string. For example "." instead of "OemPeriod" or "1" instead of "D1".

A FormatProvider was implemented (KeyGestureFormatInfo), and registered on every platform where PlatformHotkeyConfiguration was also registered using my best guess at the appropriate overrides. These platform registrations will need review, as I don't have or use any iOS/MacOS devices and can't comment on whether these choices were all appropriate. Similarly I wasn't sure what platform AvaloniaNativePlatform represented, but found hints it was an Apple platform so copied from the iOS registrations.

Checklist

Breaking changes

Formatting of menus may have changed on platforms that I don't have access to to test. I have only been able to check Windows, Android, and Browser platforms, all others will need to be checked by someone with familiarity and access.

Obsoletions / Deprecations

Fixed issues

Fixes #15392

@IanRawley
Copy link
Contributor Author

Oh damn. Didn't spot the Converter's ToPlatformString() method was public, and so removing it is a Breaking Change which is upsetting the tests obviously. I'll put it back.

@cla-avalonia
Copy link
Collaborator

cla-avalonia commented May 27, 2024

  • All contributors have signed the CLA.

@IanRawley
Copy link
Contributor Author

@cla-avalonia agree

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0048632-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Key.Left => "←",
Key.Return => "↩",
Key.PageDown => "⇞",
Key.PageUp => "⇟",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did think that looked weird, and doing a bit of searching have found other references to the same. I'll fix those up.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0048634-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

Comment on lines +112 to +119
public string ToString(string? format, IFormatProvider? formatProvider)
{
var formatInfo = format switch
{
null or "" or "g" => KeyGestureFormatInfo.Invariant,
"p" => KeyGestureFormatInfo.GetInstance(formatProvider),
_ => throw new FormatException("Unknown format specifier")
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We probably should consider using CultureInfo passed as IFormatProvider to be used for localization here. And pass it down from IValueConverter. But that's a bigger issue for another day.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I imagine "Up Arrow" is not going to be universally understood by every language on Earth. I'll take a look into how that sort of thing is done, but as you say it's a bigger problem for another day. I'll just take steps to try and make sure I'm not making that other day any harder than it needs to be.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0048712-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

if (platformKeyOverrides == null)
return key.ToString();

return platformKeyOverrides.TryGetValue(key, out string? result) ? result :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: can be simplified to platformKeyOverrides.TryGetValue(key, out var result) || commonKeyOverrides.TryGetValue(key, out result) ? result : key.ToString(), avoiding the nested ternary operators.

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0048767-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@jmacato jmacato added this pull request to the merge queue Jun 4, 2024
Merged via the queue into AvaloniaUI:master with commit 2372590 Jun 4, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Android Menus display InputGesture in a non-friendly way.
7 participants