Skip to content

Introduce ApplyApplicationDefaults Event to the Application Framework #5069

Closed

Description

@KlausLoeffelmann

Rationale

In the WinForms 3.1-time frame, we introduced the SetHighDpiMode method and the HighDpiMode property. The reason for that was that we wanted to change the default HighDpiMode for WinForms Apps to be SystemAware. At the same time, we wanted to provide also a programmatically way to set the DpiMode, since the only remaining option to do that was over the App’s manifest – the App.Config option we were no longer supposed to use for .NET Core Apps. However, setting/changing the HighDpiMode via the manifest requires the App to be recompiled/rolled out anew, which was the reason to introduce the API as an alternative. Since we did not want to support the Visual Basic Application Framework in the time, we did not include new API options for the AF.
We decided however to enable the Application Framework for Visual Basic WinForms App (somewhat late) in the .NET 5-time frame. Unfortunately, it was to late to change the runtime at the time to include some necessary changes for setting the HighDpiMode programmatically in the Application Framework, so we decided to provide this event (and the setting of the correct default value) via an additional template file for Application Framework Apps, which is only active in .NET 5 Apps. We decided, we wanted to implement those events for .NET 6, and then supersede the template implementation by the actual API.

Visual Basic Application Framework Apps ever have had a different way compared to C# to configure the start-up behavior of an WinForms app: While we have Program.cs in C#, there is no equivalent in VB with the Application Framework active. Rather, typical startup behavior is configured over several Application events:

For this reason, setting the HighDpiMode shouldn’t have been an exception conceptionally, so we introduced the ApplyHighDpiMode Application event via the template.

Since then, WinForms also has the necessity to set the default Font. The reason for that is, that we changed the default Font in .NET 3.1. That had an impact on pixel perfect Form designs, which were based on the previous Font settings. To accommodate User request for migration projects not needing to redesign all the Forms which based on the original Font, we decided to introduce the SetDefaultFont method for .NET 6. Again, this setting needs to get applied to the Application object, and since – again – in an AF App there is no Program.cs, we want to introduce an additional Event to set this default value. However, in contrast to C#, we need to make sure not to spam the Event list with yet additional Application events, which is why we decided to take a more holistic approach for AF by introducing the ApplyApplicationDefaults event for the AF. Now, with it, we will introduce a few new Properties to set as application wide default values, but unfortunately also a breaking change: We want to phase out ApplyHighDpiMode!

So, we will have in the first go of the Default values:

  • Font - will be setting the DefaultFont.
  • HighDpiMode - will be setting the HighDpiMode and replace the template-implemented ApplyHighDpiMode Application event. This is only a breaking change, if folks have actually set this value via the Application Events.
  • MinimumSplashScreenDisplayTime - it's a valuable property to set, but could previously only be set by overwriting OnInitialize of the WindowsFormsApplicationBase class. Customers shouldn't have such a hard time to discover how to set this value.

Since we’re sure to add more default properties in the future, this EventArgs class can easily be extended to accommodate more default settings (like Margin, Padding, default values for right-to-left settings, Default Form settings and more.) For the methodology to set default values, we decided that we do not need to be in sync with C#, and the VB Application Framework can have its own identity.

Actual APIs to be added:

This comes together in WindowsFormsApplicationBase, which will be extended by this:

        Public Event ApplyApplicationDefaults As ApplyApplicationDefaultsEventHandler

        ''' <summary>
        ''' We need this, so Project System will have a chance to set this value 
        ''' when we will implement this via a combo box for the VB Application Framework Designer.
        ''' </summary>
        Protected Property HighDpiMode() As Windows.Forms.HighDpiMode

For that, we need a new EventArgs class:

Public Class ApplyApplicationDefaultsEventArgs
    Inherits EventArgs

    Public Property DefaultFont As Font
    Public Property HighDpiMode As System.Windows.Forms.HighDpiMode
    Public Property MinimumSplashScreenDisplayTime As Integer
End Class

And finally, we also need this delegate for the event:

    ''' <summary>
    ''' Signature for the ApplyApplicationDefaults event handler
    ''' </summary>
    <EditorBrowsable(EditorBrowsableState.Advanced)>
    Public Delegate Sub ApplyApplicationDefaultsEventHandler(sender As Object, e As ApplyDefaultsEventArgs)

[UPDATE 7/10/2021: We will get rid of the ApplyHighDpiSetting event and unify that with the ApplyDefaultValues event by having the HighDpiMode property on the ApplyApplicationDefaultsEventArgs. We also introduce the MinimumSplashScreenDisplayTime property.]

Activity

  1. added
    api-suggestion
    on Jun 9, 2021
  2. KlausLoeffelmann commented on Jun 9, 2021

    @KlausLoeffelmann
    MemberAuthor

    @merriemcgaw, @RussKie, @KathleenDollard, @JeremyKuhne - please take a look!
    This is the requirement for updating the .NET 6 templates.

  3. ghost added
    🚧 work in progress
    on Jun 9, 2021
  4. merriemcgaw commented on Jun 10, 2021

    @merriemcgaw
    Contributor

    That sounds good to me. Thanks @KlausLoeffelmann!

  5. changed the title [-]Introduce ApplyHighDpiMode and ApplyApplicationDefaults Events to the Application Framework[/-] [+]Introduce ApplyApplicationDefaults Event to the Application Framework[/+] on Jul 10, 2021
  6. KlausLoeffelmann commented on Jul 10, 2021

    @KlausLoeffelmann
    MemberAuthor

    After reconsidering the scope of the ApplyDefaultValues Application Framework event, this would be the Visual Basic experience:

    image

  7. added
    api-ready-for-review
    on Jul 10, 2021
  8. KathleenDollard commented on Jul 11, 2021

    @KathleenDollard

    I like this simplified approach, but I am concerned about the transition for folks.

    Can we support the MyApplication_HighDpiMode event handler, but perhaps mark it as deprecated or otherwise let folks know to move away from it?

    If MinimumSplashScreenDisplayTime is set in both the MyApplication_ApplyApplicationDefaults and the OnInitialize override, which wins?

  9. KlausLoeffelmann commented on Jul 12, 2021

    @KlausLoeffelmann
    MemberAuthor

    Can we support the MyApplication_HighDpiMode event handler, but perhaps mark it as deprecated or otherwise let folks know to move away from it?

    I thought about that, too. But we'd be introducing a new API as deprecated, which strikes me as a bit peculiar.
    I was thinking about an analyzer, which could help resolving that error?

    If MinimumSplashScreenDisplayTime is set in both the MyApplication_ApplyApplicationDefaults and the OnInitialize override, which wins?

    Then the EventArgs would get prepopulated with the override, but can then be changed - so the event ultimately wins.

  10. paul1956 commented on Jul 12, 2021

    @paul1956
    Contributor

    @KlausLoeffelmann Mistakes happen do the right thing. The same thing happened in System.JSON with Options and they did the right thing in .Net 6.0. It was immediately obvious I made the change that works in .Net 5.0 and 6.0, I guess they backported the changed API.

  11. KlausLoeffelmann commented on Jul 12, 2021

    @KlausLoeffelmann
    MemberAuthor

    I wouldn't even consider it to be a mistake: At the time, we've been introducing ApplyHighDpiMode we hadn't thought about Default values. I had that idea only when we were start thinking of how to address the Font issue in the designer.

    So, am I getting you correctly: You say let's phase ApplyHighDpiMode out, and go with ApplyApplicationDefaults?

  12. paul1956 commented on Jul 12, 2021

    @paul1956
    Contributor

    @KlausLoeffelmann Yes exact. If possible add the API as something I can use in an update to .Net 5 so I can have common source during the transition, Otherwise please provide an example in the error message on what I need to do to have common source with #IF. This is what happened with JsonSerializerOptions DefaultIgnoreCondition and is a great model.

  13. added
    blocking
    on Jul 16, 2021
  14. terrajobst commented on Jul 16, 2021

    @terrajobst
    Contributor

    Since I'm not proficient in VB (and neither are most people on @dotnet/fxdc) I have written the API in C#. Please let me know if I messed it up:

    namespace Microsoft.VisualBasic.ApplicationServices
    {
        public partial class WindowsFormsApplicationBase
        {
            public event ApplyApplicationDefaultsEventHandler ApplyApplicationDefaults;
    
            [EditorBrowsable(EditorBrowsableState.Never)]
            protected HighDpiMode HighDpiMode { get; set; }
    
            // Existing property, but new attribute
            [EditorBrowsable(EditorBrowsableState.Never)]
            public int MinimumSplashScreenDisplayTime { get; set; }
        }
    
        public class ApplyApplicationDefaultsEventArgs : EventArgs
        {
            public Font DefaultFont { get; set; }
            public HighDpiMode HighDpiMode { get; set; }
            public int MinimumSplashScreenDisplayTime { get; set; }
        }
    
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public delegate void ApplyApplicationDefaultsEventHandler(object sender, ApplyDefaultsEventArgs e);
    }
  15. terrajobst commented on Jul 16, 2021

    @terrajobst
    Contributor

    Looks reasonable. I'd just suggest to use a TimeSpan for MinimumSplashScreenDisplayTime as this removes the ambiguity if the unit is milliseconds, seconds, or fortnight per inch squared.

  16. bartonjs commented on Jul 16, 2021

    @bartonjs
    Member

    public delegate void ApplyApplicationDefaultsEventHandler(object sender, ApplyDefaultsEventArgs e);

    FDG says:

    DO use System.EventHandler<T> instead of manually creating new delegates to be used as event handlers.

    So I think the event just needs to be declared as EventHandler<ApplyDefaultsEventArgs>.

  17. terrajobst commented on Jul 16, 2021

    @terrajobst
    Contributor

    Ah yes. Good call.

  18. KlausLoeffelmann commented on Jul 20, 2021

    @KlausLoeffelmann
    MemberAuthor

    We had some problems in the past with generating the stubs for the Visual Basic Handles clause based on generics in Visual Basic, if I remember correctly. We should thoroughly test this, and if that works, I'll make the change!

  19. bartonjs commented on Jul 20, 2021

    @bartonjs
    Member

    Video

    API Review:

    • ApplyApplicationDefaultsEventHandler should be EventHandler<ApplyApplicationDefaults>, but since the rest of the events on this type are custom delegates doing this one that way is OK (and may be needed due to an IDE limitation)
    • Ideally MinimumSplashScreenDisplayTime would be a TimeSpan, but since it's just echoing an existing unitless int property it's OK.
    namespace Microsoft.VisualBasic.ApplicationServices
    {
        public partial class WindowsFormsApplicationBase
        {
            public event EventHandler<ApplyDefaultsEventArgs> ApplyApplicationDefaults;
            
            [EditorBrowsable(EditorBrowsableState.Never)]
            protected HighDpiMode HighDpiMode { get; set; }
    
            // Existing property, but new attribute
            [EditorBrowsable(EditorBrowsableState.Never)]
            public int MinimumSplashScreenDisplayTime { get; set; }
        }
    
        public class ApplyApplicationDefaultsEventArgs : EventArgs
        {
            public Font DefaultFont { get; set; }
            public HighDpiMode HighDpiMode { get; set; }
            public int MinimumSplashScreenDisplayTime { get; set; }
        }
    
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public delegate void ApplyApplicationDefaultsEventHandler(object sender, ApplyDefaultsEventArgs e);
    }
  20. added
    api-approved
    and removed
    api-ready-for-review
    api-suggestion
    blocking
    on Jul 20, 2021
  21. ghost removed
    🚧 work in progress
    on Jul 21, 2021
  22. Philip-Wang01 commented on Aug 27, 2021

    @Philip-Wang01
    Contributor

    Verified the issue with 6.0.100-rc.1.21424.1 build , complete introduced ApplyApplicationDefaults Event to the Application Framework
    image

  23. ghost locked as resolved and limited conversation to collaborators on Jan 28, 2022
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