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

Allow set font application-wide #3001

Open
RussKie opened this issue Mar 21, 2020 · 9 comments
Open

Allow set font application-wide #3001

RussKie opened this issue Mar 21, 2020 · 9 comments
Assignees
Milestone

Comments

@RussKie
Copy link
Member

@RussKie RussKie commented Mar 21, 2020

Is your feature request related to a problem? Please describe.

The default font has been updated in .NET Core 3.0 (#656) and documented. However for some users there is still an element of surprise when they migrate their apps to .NET Core.
We've received several questions regarding different sizes of forms (e.g. #1122, #1827, etc.).

Whilst the new default font is here to stay, some users may wish to retain the original font (e.g. due to a design of their app). However for an application with more than a handful of forms, setting the original font may be tedious and cumbersome exercise.

Describe the solution you'd like

Add the ability to set an application-wide font, similar to SetHighDpiMode() or SetCompatibleTextRenderingDefault() methods.

  • This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.
  • Any form that doesn't explicitly specify its own font, must inherit the application default font.

The signature:

public class Application
{
	void SetDefaultFont(Font font);
}

The callsite:

        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

+           Application.SetDefaultFont(new Font(new FontFamily("Microsoft Sans Serif"), 8f));

            Application.Run(new Form1());
        }

Will this feature affect UI controls?

No

@merriemcgaw

This comment has been minimized.

Copy link
Member

@merriemcgaw merriemcgaw commented Mar 27, 2020

We should totally do this for .NET 5, I think this is a great idea.

@terrajobst

This comment has been minimized.

Copy link
Member

@terrajobst terrajobst commented Mar 27, 2020

  • This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.

Are you going to throw an exception when the SetDefaultFont is called after Run? If so, I'd throw InvalidOperationException.

@RussKie

This comment has been minimized.

Copy link
Member Author

@RussKie RussKie commented Mar 27, 2020

  • This method must be "run only once" kind, i.e. a user may not be allowed to invoke it once an app has started.

Are you going to throw an exception when the SetDefaultFont is called after Run? If so, I'd throw InvalidOperationException.

I propose we follow the same pattern set by SetCompatibleTextRenderingDefault() method and throw if SetDefaultFont() is called after the application has started:

public static void SetCompatibleTextRenderingDefault(bool defaultValue)
{
if (NativeWindow.AnyHandleCreated)
{
throw new InvalidOperationException(SR.Win32WindowAlreadyCreated);
}
Control.UseCompatibleTextRenderingDefault = defaultValue;
}

The rationale behind it, if a form has been created, it may have been laid out and rendered using one font, the font may have been cached etc., and setting a new font could be detrimental from both performance and UX.

@bartonjs

This comment has been minimized.

Copy link
Member

@bartonjs bartonjs commented Mar 27, 2020

Is there already a way to get the default font? (Get/set symmetry)

@weltkante

This comment has been minimized.

Copy link
Contributor

@weltkante weltkante commented Mar 27, 2020

probably Control.DefaultFont ?

@terrajobst

This comment has been minimized.

Copy link
Member

@terrajobst terrajobst commented Mar 27, 2020

Might be worthwhile to add a corresponding GetDefaultFont() to Application so that folks can just tweak parts, like:

[STAThread]
static void Main()
{
    Application.SetHighDpiMode(HighDpiMode.SystemAware);
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    // Set default to 10pt
    var font = Application.GetDefaultFont();
    font = new Font(font.FontFamily, 10f);
    Application.SetDefaultFont(font);

    Application.Run(new Form1());
}

It would be odd if users had to call Control.DefaultFont to do this.

@weltkante

This comment was marked as resolved.

Copy link
Contributor

@weltkante weltkante commented Mar 27, 2020

I don't think fonts are mutable, you have to construct a new one. (Not arguing against Application.GetDefaultFont for discoverability.)

@terrajobst

This comment was marked as resolved.

Copy link
Member

@terrajobst terrajobst commented Mar 27, 2020

I don't think fonts are mutable, you have to construct a new one. (Not arguing against Application.GetDefaultFont for discoverability.)

Fine, be like that ;-) Fixed sample.

@RussKie

This comment has been minimized.

Copy link
Member Author

@RussKie RussKie commented Mar 30, 2020

The rationale behind this FR is to allow users to set the original font, that may have been used to design the app (i.e. Font-scaling pixel-perfect design scenario), to help with a migration from .NET Framework to .NET Core/.NET.
To this end, users are expected to set both FontFamily and FontSize explicitly, and not leverage the current default font.

I don't feel, that leveraging the default font to change its properties fits the above scenario. And, to be honest, I'm struggling to think of a use-case that would require this.
Do you have a use-case that would necessitate this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.