Skip to content

Commit 0d6ae9a

Browse files
committed
Apply my feedback to Klaus' dark mode feature
- Move Experimental URL to DiagnosticIds - Move special dark mode logic from CreateBrushScope to GetSysColorBrush - Remove unnecessary logic in FindNearestColor - Update brushes and pens by poking SystemEvents static directly
1 parent b238485 commit 0d6ae9a

File tree

20 files changed

+115
-97
lines changed

20 files changed

+115
-97
lines changed

src/Microsoft.VisualBasic.Forms/src/Microsoft.VisualBasic.Forms.vbproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<Deterministic>true</Deterministic>
1111
<OptionExplicit>On</OptionExplicit>
1212
<OptionStrict>On</OptionStrict>
13+
<OptionInfer>On</OptionInfer>
1314
<RootNamespace></RootNamespace>
1415
<LangVersion>Latest</LangVersion>
1516
<VBRuntime>None</VBRuntime>

src/Microsoft.VisualBasic.Forms/src/Microsoft/VisualBasic/ApplicationServices/ApplyApplicationDefaultsEventArgs.vb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Namespace Microsoft.VisualBasic.ApplicationServices
2828
Me.ColorMode = colorMode
2929
End Sub
3030

31-
#Enable Warning WFO5000
3231
#Enable Warning WFO5001
3332

3433
''' <summary>
@@ -61,5 +60,4 @@ Namespace Microsoft.VisualBasic.ApplicationServices
6160
Public Property ColorMode As SystemColorMode
6261

6362
End Class
64-
#Disable Warning WFO5001
6563
End Namespace

src/System.Drawing.Common/src/System/Drawing/SystemBrushes.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,6 @@ namespace System.Drawing;
55

66
public static class SystemBrushes
77
{
8-
#if NET9_0_OR_GREATER
9-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
10-
private static bool s_colorSetOnLastAccess = SystemColors.UseAlternativeColorSet;
11-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
12-
#endif
138
private static readonly object s_systemBrushesKey = new();
149

1510
public static Brush ActiveBorder => FromSystemColor(SystemColors.ActiveBorder);
@@ -62,20 +57,7 @@ public static Brush FromSystemColor(Color c)
6257
throw new ArgumentException(SR.Format(SR.ColorNotSystemColor, c.ToString()));
6358
}
6459

65-
#if NET9_0_OR_GREATER
66-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
67-
if (s_colorSetOnLastAccess != SystemColors.UseAlternativeColorSet)
68-
{
69-
s_colorSetOnLastAccess = SystemColors.UseAlternativeColorSet;
70-
71-
// We need to clear the SystemBrushes cache, when the ColorMode had changed.
72-
Gdip.ThreadData.Remove(s_systemBrushesKey);
73-
}
74-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
75-
#endif
76-
77-
if (!Gdip.ThreadData.TryGetValue(s_systemBrushesKey, out object? tempSystemBrushes)
78-
|| tempSystemBrushes is not Brush[] systemBrushes)
60+
if (!Gdip.ThreadData.TryGetValue(s_systemBrushesKey, out object? tempSystemBrushes) || tempSystemBrushes is not Brush[] systemBrushes)
7961
{
8062
systemBrushes = new Brush[(int)KnownColor.WindowText + (int)KnownColor.MenuHighlight - (int)KnownColor.YellowGreen];
8163
Gdip.ThreadData[s_systemBrushesKey] = systemBrushes;

src/System.Drawing.Common/src/System/Drawing/SystemPens.cs

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,6 @@ namespace System.Drawing;
55

66
public static class SystemPens
77
{
8-
#if NET9_0_OR_GREATER
9-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
10-
private static bool s_colorSetOnLastAccess = SystemColors.UseAlternativeColorSet;
11-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
12-
#endif
13-
148
private static readonly object s_systemPensKey = new();
159

1610
public static Pen ActiveBorder => FromSystemColor(SystemColors.ActiveBorder);
@@ -64,20 +58,7 @@ public static Pen FromSystemColor(Color c)
6458
throw new ArgumentException(SR.Format(SR.ColorNotSystemColor, c.ToString()));
6559
}
6660

67-
#if NET9_0_OR_GREATER
68-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
69-
if (s_colorSetOnLastAccess != SystemColors.UseAlternativeColorSet)
70-
{
71-
s_colorSetOnLastAccess = SystemColors.UseAlternativeColorSet;
72-
73-
// We need to clear the SystemBrushes cache, when the ColorMode had changed.
74-
Gdip.ThreadData.Remove(s_systemPensKey);
75-
}
76-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
77-
#endif
78-
79-
if (!Gdip.ThreadData.TryGetValue(s_systemPensKey, out object? tempSystemPens)
80-
|| tempSystemPens is not Pen[] systemPens)
61+
if (!Gdip.ThreadData.TryGetValue(s_systemPensKey, out object? tempSystemPens) || tempSystemPens is not Pen[] systemPens)
8162
{
8263
systemPens = new Pen[(int)KnownColor.WindowText + (int)KnownColor.MenuHighlight - (int)KnownColor.YellowGreen];
8364
Gdip.ThreadData[s_systemPensKey] = systemPens;

src/System.Windows.Forms.Analyzers.CodeFixes.CSharp/Resources/SR.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,4 @@
120120
<data name="AddDesignerSerializationVisibilityCodeFixTitle" xml:space="preserve">
121121
<value>Add DesignerSerializationVisibilityAttribute to property</value>
122122
</data>
123-
</root>
123+
</root>

src/System.Windows.Forms.Analyzers/src/System/Windows/Forms/Analyzers/Diagnostics/DiagnosticIDs.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ internal static class DiagnosticIDs
1919
public const string DisposeModalDialog = "WFO2000";
2020

2121
// Experimental, number group 5000+
22-
public const string ExperimentalVisualStyles = "WFO5000";
2322
public const string ExperimentalDarkMode = "WFO5001";
2423
public const string ExperimentalAsync = "WFO5002";
2524
}

src/System.Windows.Forms.Primitives/src/NativeMethods.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ SelectPalette
599599
SendDlgItemMessage
600600
SendInput
601601
SendMessage
602+
SendMessageCallback
602603
SendMessageTimeout
603604
SetActiveWindow
604605
SetBkColor

src/System.Windows.Forms.Primitives/src/System/Windows/Forms/DeviceContextExtensions.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,22 +116,7 @@ internal static unsafe void DrawLines(this HDC hdc, HPEN hpen, ReadOnlySpan<int>
116116
/// </remarks>
117117
internal static Color FindNearestColor(this HDC hdc, Color color)
118118
{
119-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
120-
if (color.IsSystemColor && SystemColors.UseAlternativeColorSet)
121-
{
122-
// We need to "un-system" the color for nearest color to work correctly in dark mode,
123-
// since GetNearestColor doesn't understand dark mode system colors and internally works
124-
// with the light mode colors.
125-
color = Color.FromArgb(color.ToArgb());
126-
Debug.Assert(!color.IsSystemColor, "Color should not be a system color.");
127-
}
128-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
129-
130-
Color newColor = ColorTranslator.FromWin32(
131-
(int)PInvoke.GetNearestColor(
132-
hdc: hdc,
133-
color: (COLORREF)(uint)ColorTranslator.ToWin32(color)).Value);
134-
119+
Color newColor = ColorTranslator.FromWin32((int)PInvoke.GetNearestColor(hdc, (COLORREF)(uint)ColorTranslator.ToWin32(color)).Value);
135120
return newColor.ToArgb() == color.ToArgb() ? color : newColor;
136121
}
137122

src/System.Windows.Forms.Primitives/src/Windows/Win32/CreateBrushScope.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,9 @@ internal readonly ref struct CreateBrushScope
2828
/// </summary>
2929
public CreateBrushScope(Color color)
3030
{
31-
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
32-
HBRUSH = color.IsSystemColor && !SystemColors.UseAlternativeColorSet
31+
HBRUSH = color.IsSystemColor
3332
? PInvoke.GetSysColorBrush(color)
3433
: PInvoke.CreateSolidBrush(color);
35-
#pragma warning restore SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
36-
3734
ValidateBrushHandle();
3835
}
3936

src/System.Windows.Forms.Primitives/src/Windows/Win32/PInvoke.GetSysColorBrush.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@ internal static partial class PInvoke
1010
/// <inheritdoc cref="GetSysColorBrush(SYS_COLOR_INDEX)"/>
1111
public static HBRUSH GetSysColorBrush(Color systemColor)
1212
{
13+
#pragma warning disable SYSLIB5002 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
14+
bool useSolidBrush = SystemColors.UseAlternativeColorSet;
15+
#pragma warning restore SYSLIB5002
16+
17+
if (useSolidBrush)
18+
{
19+
// We don't have a real system color, so we'll just create a solid brush.
20+
return CreateSolidBrush(systemColor);
21+
}
22+
1323
Debug.Assert(systemColor.IsSystemColor);
1424

1525
// Extract the COLOR value
16-
return PInvoke.GetSysColorBrush((SYS_COLOR_INDEX)(ColorTranslator.ToOle(systemColor) & 0xFF));
26+
return GetSysColorBrush((SYS_COLOR_INDEX)(ColorTranslator.ToOle(systemColor) & 0xFF));
1727
}
1828
}

0 commit comments

Comments
 (0)