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

feat: PixelSize TryParse #14979

Merged

Conversation

workgroupengineering
Copy link
Contributor

What does the pull request do?

Add TryParse method to PixelSize

What is the current behavior?

What is the updated/expected behavior with this PR?

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

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

@avaloniaui-bot
Copy link

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

@MrJul
Copy link
Member

MrJul commented Mar 14, 2024

While I have nothing against the API itself, why do we have the ability to provide the separator in TryParse but not in Parse?

In my opinion, I don't think the separator should be able to be specified at all for these methods.
There should be only one supported format, allowing for [Try]Parse(ToString()) to round trip. (Others in the team might have different thoughts on the matter.)

We should also think about consistency here. Why don't we have TryParse for Point, Size, Rect etc? Of course I'm not asking you to add all these to this PR, that's out of scope! But we should really think about having consistent API everywhere, which makes for a better user experience.

@MrJul MrJul added the feature label Mar 14, 2024
src/Avalonia.Base/PixelSize.cs Outdated Show resolved Hide resolved
src/Avalonia.Base/PixelSize.cs Outdated Show resolved Hide resolved
@MrJul
Copy link
Member

MrJul commented Mar 14, 2024

Also, shouldn't we rewrite Parse by using TryParse, avoiding logic duplication?

@workgroupengineering
Copy link
Contributor Author

We should also think about consistency here. Why don't we have TryParse for Point, Size, Rect etc? Of course I'm not asking you to add all these to this PR, that's out of scope! But we should really think about having consistent API everywhere, which makes for a better user experience.

My intention is to gradually add the TryParse method to the other types.

@avaloniaui-bot
Copy link

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

@MrJul
Copy link
Member

MrJul commented Mar 15, 2024

I still don't really like the separator being a parameter. As mentioned before, we should only allow the format currently used in XAML. Also, the separator may be ignored since a whitespace will always act as an extra separator in the current StringTokenizer implementation. This might confuse users: "the separator I've specified isn't respected!".

For more complex types it may even clash with the decimal separator or other special characters from the format. Since we don't allow the culture to be specified (for good reasons, we're parsing a known format here, not accepting localized user input), the separator shouldn't be either.

Pinging @maxkatz6 for a second opinion on this matter.

@avaloniaui-bot
Copy link

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

@hez2010
Copy link
Contributor

hez2010 commented Mar 16, 2024

Maybe you can also provide an overload for ReadOnlySpan<char>?

@maxkatz6
Copy link
Member

ReadOnlySpan overload would be nice in general, but we don't have it anywhere else. Could be a candidate for the future PRs.

Pinging @maxkatz6 for a second opinion on this matter.

I would vote to remove separator parameter. As pixel size parsing is always invariant (with '.' decimal point), and ',' as a separator is well common. If it's ever needed, we can add another overload with separator in the future, but not necessary now.

@maxkatz6
Copy link
Member

Also, CultureInfo.InvariantCulture.TextInfo.ListSeparator exists, if we would need to pass IFormatProvider to Parse method.
Not sure if it's the best application of ListSeparator, but WPF seems to use it for non-invariant cultures: https://github.com/dotnet/wpf/blob/85d908ce8599d12f5e16ce7d78083cfdc2e13af4/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ThicknessConverter.cs#L167C50-L176

@kekekeks
Copy link
Member

For ReadOnlySpan overloads we'll need to implement some shims for netstandard2.0 that would parse integers/doubles for invariant locale without actually allocating a string. Can probably copy-paste some code from BCL.

@avaloniaui-bot
Copy link

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

@MrJul
Copy link
Member

MrJul commented Apr 3, 2024

I would vote to remove separator parameter. As pixel size parsing is always invariant (with '.' decimal point), and ',' as a separator is well common. If it's ever needed, we can add another overload with separator in the future, but not necessary now.

@workgroupengineering Can you please remove the separator argument from this PR?

@avaloniaui-bot
Copy link

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

@maxkatz6 maxkatz6 enabled auto-merge April 4, 2024 00:23
@avaloniaui-bot
Copy link

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

@avaloniaui-bot
Copy link

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

auto-merge was automatically disabled April 5, 2024 11:01

Head branch was pushed to by a user without write access

@MrJul MrJul enabled auto-merge April 5, 2024 11:16
@avaloniaui-bot
Copy link

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

@MrJul MrJul added this pull request to the merge queue Apr 5, 2024
Merged via the queue into AvaloniaUI:master with commit f7bcb5f Apr 5, 2024
10 checks passed
@workgroupengineering workgroupengineering deleted the features/Core/PixeSize branch April 5, 2024 12:27
@maxkatz6 maxkatz6 added the backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch label Apr 20, 2024
maxkatz6 pushed a commit that referenced this pull request Apr 20, 2024
* feat: PixelSize TryParse

* test: Add PixelSize Parse and TryParse

* fix: Address review

* fix: Address Review

* fix: Address review
@maxkatz6 maxkatz6 added backported-11.1.x and removed backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch labels Apr 20, 2024
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.

6 participants