Skip to content

Allow set application-wide default font #4911

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

Merged
merged 1 commit into from
May 18, 2021

Conversation

RussKie
Copy link
Contributor

@RussKie RussKie commented May 14, 2021

Resolves #3001

Proposed changes

The default font has been updated in .NET Core 3.0 (#656) and documented. However for some users who built their apps in pixel-perfect manner this change has proved to be a significant hurdle in migrating their apps to .NET.

Allow setting application-wide default font in a similar manner we set high dpi or visual styles:

class Program
{
    [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());
    }
}
  • The application-wide default font can only be set before the first window is created by an application.
  • The font will be scaled by the system text scale factor, whenever it is getting changed.

Screenshots

  • Microsoft Sans Serif, 8pt
    image

  • Chiller, 12pt
    image

  • Calibri, 11pt
    image

  • System text scale factor changes
    default-font1

Test methodology

  • several new unit tests
  • manual - see below
Microsoft Reviewers: Open in CodeFlow

@RussKie RussKie requested a review from a team as a code owner May 14, 2021 07:17
@ghost ghost assigned RussKie May 14, 2021
@RussKie RussKie force-pushed the fix_3001_appwide_font branch 2 times, most recently from 12a02bf to 4d78542 Compare May 14, 2021 11:11
@RussKie RussKie force-pushed the fix_3001_appwide_font branch from ac2ff6a to ea025d3 Compare May 16, 2021 10:21
@RussKie

This comment has been minimized.

@RussKie RussKie force-pushed the fix_3001_appwide_font branch from ea025d3 to 6a81c31 Compare May 16, 2021 11:04
@RussKie RussKie added this to the 6.0 Preview5 milestone May 16, 2021
@RussKie RussKie force-pushed the fix_3001_appwide_font branch 2 times, most recently from de99936 to 97d80b2 Compare May 16, 2021 12:58
@kirsan31
Copy link
Contributor

Added handling of the system-wide font scaling events, e.g.:

Wow - nice! 👍 Now we need only #3583 :))))

Copy link
Member

@dreddy-work dreddy-work left a comment

Choose a reason for hiding this comment

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

We may want to cross verify the serialization part for Font with this new change

@RussKie RussKie force-pushed the fix_3001_appwide_font branch from 97d80b2 to 754529b Compare May 17, 2021 21:54
@RussKie RussKie requested a review from Tanya-Solyanik May 18, 2021 00:44
@RussKie RussKie added the waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) label May 18, 2021
@RussKie
Copy link
Contributor Author

RussKie commented May 18, 2021

Tested different font scaling coupled with different DPI scaling.
There is an observable little deviation in our scaled fonts compared to the scaled system fonts in the range of 101-149%, but it doesn't appear to be visually significant.

100% DPI scale factor

  • 100% text scale factor
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=172, Height=25}
    MainForm: Minimum form size: {Width=378, Height=448}
  • 130%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=11.25, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=11.7, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=227, Height=31}
    MainForm: Minimum form size: {Width=488, Height=526}
  • 110%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=9.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=9.900001, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=201, Height=29}
    MainForm: Minimum form size: {Width=436, Height=500}
  • 225%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=20.25, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=20.25, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=382, Height=47}
    MainForm: Minimum form size: {Width=798, Height=751}
  • 175%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=15.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=15.75, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=297, Height=40}
    MainForm: Minimum form size: {Width=628, Height=653}
  • 150%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=13.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=13.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=255, Height=35}
    MainForm: Minimum form size: {Width=544, Height=583}

150% DPI scale factor

  • 100%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=9, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=255, Height=35}
    MainForm: Minimum form size: {Width=550, Height=595}
  • 130%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=11.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=11.7, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=340, Height=42}
    MainForm: Minimum form size: {Width=720, Height=686}
  • 225%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=20.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=20.25, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=568, Height=65}
    MainForm: Minimum form size: {Width=1176, Height=1011}
  • 150%
    MainForm: MessageBoxFont: [Font: Name=Segoe UI, Size=13.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Default font: [Font: Name=Segoe UI, Size=13.5, Units=3, GdiCharSet=1, GdiVerticalFont=False]
    MainForm: Biggest button size: {Width=382, Height=47}
    MainForm: Minimum form size: {Width=804, Height=759}

@RussKie
Copy link
Contributor Author

RussKie commented May 18, 2021

⚠️ Each form had to be closed and reopened after each dpi/text scale changes

Tested with HighDpiMode.PerMonitorV2:

dpi/text current default font (SystemFonts.MessageBoxFont) SetDefaultFont(Calibri, 11)
100% dpi / 100% text image image
100% dpi / 150% text image image
150% dpi / 150% text image image
150% dpi / 100% text image image

@RussKie RussKie removed the waiting-for-testing The PR is awaiting manual testing by the primary team; no action is yet required from the author(s) label May 18, 2021
@RussKie RussKie force-pushed the fix_3001_appwide_font branch from f555233 to 5d3b053 Compare May 18, 2021 06:39
The default font has been updated in .NET Core 3.0 (dotnet#656) and documented.
However for some users who built their apps in pixel-perfect manner this
change has proved to be a significant hurdle in migrating their apps to
.NET.

Allow setting application-wide default font in a similar manner we set
high dpi or visual styles:

    Application.SetDefaultFont(new Font(new FontFamily("Calibri"), 11f));

* The application-wide default font can only be set before the first
window is created by an application.
* The font will be scaled by the system text scale factor, whenever it is
getting changed.

Resolves dotnet#3001
@RussKie RussKie force-pushed the fix_3001_appwide_font branch from 5d3b053 to 9590589 Compare May 18, 2021 06:57
@dreddy-work
Copy link
Member

⚠️ Each form had to be closed and reopened after each dpi/text scale changes

Is this because Font is not applied on the windows after UserPreference settings changed? Did you try that? That would trigger layout and scaling of controls.

@dreddy-work
Copy link
Member

FYI- this can be dual check-in to preview 5 when ready. Had to close preview 5 so tagging it to Preview 6 for now.

@dreddy-work dreddy-work removed this from the 6.0 Preview5 milestone May 18, 2021
@dreddy-work dreddy-work added this to the 6.0 Preview 6 milestone May 18, 2021
@RussKie RussKie merged commit 9d476b6 into dotnet:main May 18, 2021
@RussKie RussKie deleted the fix_3001_appwide_font branch May 18, 2021 23:06
@RussKie RussKie modified the milestones: 6.0 Preview6, 6.0 Preview5 May 18, 2021
RussKie added a commit that referenced this pull request May 18, 2021
The default font has been updated in .NET Core 3.0 (#656) and documented.
However for some users who built their apps in pixel-perfect manner this
change has proved to be a significant hurdle in migrating their apps to
.NET.

Allow setting application-wide default font in a similar manner we set
high dpi or visual styles:

    Application.SetDefaultFont(new Font(new FontFamily("Calibri"), 11f));

* The application-wide default font can only be set before the first
window is created by an application.
* The font will be scaled by the system text scale factor, whenever it is
getting changed.

Resolves #3001
@ghost ghost locked as resolved and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow set font application-wide
5 participants