Skip to content

Define a pattern for default values that used to be constants proportional to the default system font. #8599

Closed

Description

@Tanya-Solyanik

related bugs

The background

Some control properties are decorated with a DefaultValueAttribute. When control is created in the designer, and the property had not been initialized yet, property browser reads attribute value and shows it in the property browser. If the user changes such a value in the property browser, design time serialization writes the user-specified value into the InitializeComponent method in the code-behind file. If the user does not assign specific value to such a property in the property browser, this property will not be serialized into the code-behind file, as controlled on line 1176 by the type description provider. Default value is also used to reset property value in the property browser.

At the runtime, properties that are not assigned to in the InitializeComponent method, are initialized to default runtime values.

Design time components assume that the runtime default value is the same as the design time one i.e., in the case when the attribute is used, the same as the attribute value. If this assumption is not upheld, the reset option in the property browser will not work, and InitializeComponent will explicitly serialize default values instead of letting the control to pick up the runtime default on the initialization.

The issue

In some cases, the default runtime value is dependent on the application font. For example, Height or Width properties. Some of these values had been calculated and hardcoded before we have changed the default font. Currently some of the design time default values defined in the attribute are out of sync with the runtime default values that are explicitly assigned in the constructor.

Example

Design time default value -

[DefaultValue(22)]
[NotifyParentProperty(true)]
[SRCategory(nameof(SR.CatAppearance))]
[SRDescription(nameof(SR.DataGridView_RowHeightDescr))]
public int Height
{
get => Thickness;

Runtime default -

Thickness = Control.DefaultFont.Height + 9;

The runtime value is dynamically calculated based on the default font while the designer assumes a constant value.

What happens

  1. when designer is shown, control is instantiated, and the runtime default value is assigned to the property.
  2. when control is serialized, TypeDescriptor compares the actual property value (from the runtime default) against the value in the attribute, sees that they are different and adds a line that sets this value to the InitalizeComponent.
    Problem: User had not edited this value and expects that height is picked up from the font, user changes the control font on the startup and the height is not appropriate for the new font.

Suggestion

Use a different pattern to supply the default value, like what ambient properties are already using. See line 1170.

  1. remove the default value attribute from properties that have dynamic default values.
  2. add Reset<Property> and ShouldSerialize<Property> methods that compare actual value against the dynamic runtime default.

This is a breaking change

Per https://github.com/dotnet/runtime/blob/main/docs/coding-guidelines/breaking-change-rules.md#attributes. But I consider this case a grey area because the functionality is preserved, and change is unobservable to the user unless they enumerate the attributes. If they enumerate the attributes, they'll have to look up the breaking changes docs. If default value functionality is consumed through the TypeDescriptor infrastructure, for serialization purposes, there will be no change.

A pseudocode representation of the pattern.

before:

namespace System.Windows.Forms;

public class SomeControl : Control
{
    private const int DefaultHeight = 22;

    public SomeControl()
    {
        Height = DefaultHeight;
    }

    [DefaultValue(DefaultHeight)] 
    public int Height {set; get;}
}

after:

namespace System.Windows.Forms;

public class SomeControl : Control
{
    public SomeControl()
    {
        Height = DefaultHeight;
    }

    private int DefaultFont { get; }

    public int Height 
    {
          // Calculate the height to accommodate text that uses the default font, i.e. use Control.DefaultFont.Height   
         set; get;
    }

    private void ResetHeight()
    {
        Height = DefaultHeight;
    }

    private bool ShouldSerializeHeight()
    {
        return Height != DefaultHeight;
    }
}

Activity

  1. added
    untriaged
    and removed
    untriaged
    on Feb 9, 2023
  2. changed the title [-]Define a pattern for default values that used to be proportional to the default system font.[/-] [+]Define a pattern for default values that used to be constants proportional to the default system font.[/+] on Feb 9, 2023
  3. added
    api-ready-for-review
    on Feb 9, 2023
  4. Tanya-Solyanik commented on Feb 10, 2023

    @Tanya-Solyanik
    ContributorAuthor

    breaking change doc that covers all attributes removals that follow this pattern - dotnet/docs#33954

  5. Tanya-Solyanik commented on Feb 10, 2023

    @Tanya-Solyanik
    ContributorAuthor

    Seeking opinion of the API review board - dotnet/runtime#81937

  6. added
    blocking
    on Feb 14, 2023
  7. kirsan31 commented on Feb 14, 2023

    @kirsan31
    Contributor

    @Tanya-Solyanik
    Ate you shure that #8593 have the same root?

    In some cases, the default runtime value is dependent on the application font.

    properties that have dynamic default values

    In my understanding, there are no dynamic defaults values 🤔

  8. Tanya-Solyanik commented on Feb 14, 2023

    @Tanya-Solyanik
    ContributorAuthor

    @kirsan31
    I used the term "dynamic default value" to describe properties like this one -

    Thickness = Control.DefaultFont.Height + 9;

  9. kirsan31 commented on Feb 14, 2023

    @kirsan31
    Contributor

    @Tanya-Solyanik
    Yea, I got it. I mean that here:

    pic2

    we do not seem to have such properties.

  10. Tanya-Solyanik commented on Feb 15, 2023

    @Tanya-Solyanik
    ContributorAuthor

    @kirsan31 - sure, I will investigate this bug after the attribute removal is approved. So far, the root cause seems to be that the font is not recognized as the default one.

  11. added this to the 8.0 Preview2 milestone on Feb 21, 2023
  12. terrajobst commented on Feb 23, 2023

    @terrajobst
    Contributor

    Video

    Looks good as proposed.

    We discussed whether the methods ResetXxx() and ShouldSerializeXxx() should be public. We concluded that they shouldn't be because they are indirectly used by TypeDescriptor, which is the way the designer (and other serializers) should interact with these types.

    It looks like we might want to consider an analyzer that checks for an assignment of ItemHeight and instructs the user to delete the line. Alternatively, the designer could me made aware of which properties are DPI dependent and doesn't persist those values to hardcoded values into the designer generated code. It sounds like this is planned for a later day.

  13. added
    api-approved
    and removed
    api-ready-for-review
    on Feb 23, 2023
  14. removed
    blocking
    on Feb 23, 2023
  15. RussKie commented on Feb 23, 2023

    @RussKie
    Contributor

    @terrajobst you really need to adjust your microphone settings - your typing is deafening, makes it hard to hear speakers.

    @Tanya-Solyanik I've been toying with analyzers for InitializeComponent, you can see those in https://github.com/RussKie/WindowsFormsAnalyzers/. Feel free to borrow anything useful from there. I'm sure @Youssef1313 could help too, if we ask him nicely 😉

  16. terrajobst commented on Feb 23, 2023

    @terrajobst
    Contributor

    @terrajobst you really need to adjust your microphone settings - your typing is deafening, makes it hard to hear speakers.

    Apologies. It's not that bad normally; I'm currently in Olia's office because mine is being remodeled. Reason I didn't mute myself was silly: it's a hardware button and earlier folks complained that there was no audio so I wasn't sure whether this was caused by muting myself. Audio is hard :-)

  17. ghost removed this from the 8.0 Preview2 milestone on Feb 24, 2023
  18. ghost locked as resolved and limited conversation to collaborators on Mar 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approved

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions