Skip to content

Commit 736dc66

Browse files
committed
theme update, colors can be changed on the go
1 parent 17be062 commit 736dc66

8 files changed

Lines changed: 291 additions & 171 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@
44
/bin
55
/obj
66
/.vs
7+
.vs/slnx.sqlite
8+
.vs/VSWorkspaceState.json
9+
RacingDSX.csproj.user

Config/Config.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ public class Config
1111
{
1212
public bool DisableAppCheck { get; set; }
1313
public VerboseLevel VerboseLevel { get; set; } = VerboseLevel.Off;
14+
15+
public Theme Theme { get; set; } = Theme.Auto;
16+
1417
public Dictionary<String, Profile> Profiles { get; set; } = new Dictionary<String, Profile>();
1518
[JsonIgnore]
1619
public Profile ActiveProfile { get; set; } = null;

Config/Theme.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RacingDSX.Config
8+
{
9+
public enum Theme
10+
{
11+
Auto = 0,
12+
Light = 1,
13+
Dark = 2,
14+
Plain = 3,
15+
}
16+
}

Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,14 @@
88
using System.Text.Json;
99
using System.Text.Json.Serialization;
1010
using System.Windows.Forms;
11+
using System.Windows.Forms.VisualStyles;
1112

1213
namespace RacingDSX
1314
{
1415

1516
public class Program
1617
{
17-
public const string VERSION = "0.7.3";
18+
public const string VERSION = "0.7.4";
1819

1920
static void Main(string[] args)
2021
{

ThemeManager.cs

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
using Microsoft.Win32;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.ComponentModel;
5+
using System.Drawing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
using System.Windows.Forms;
10+
11+
12+
namespace RacingDSX
13+
{
14+
15+
public class ThemeManager
16+
{
17+
public static AppTheme CurrentTheme { get; set; } = Themes.Dark;
18+
19+
public static bool IsDesignMode =>
20+
LicenseManager.UsageMode == LicenseUsageMode.Designtime;
21+
22+
public static void ApplyTheme(Control control)
23+
{
24+
if (control == null)
25+
return;
26+
27+
Suspend(control);
28+
29+
ApplyToControl(control);
30+
31+
Resume(control);
32+
}
33+
34+
private static void ApplyToControl(Control control)
35+
{
36+
control.BackColor = CurrentTheme.BackColor;
37+
control.ForeColor = CurrentTheme.ForeColor;
38+
39+
if (control is Button button)
40+
{
41+
button.ForeColor = CurrentTheme.ButtonForeColor;
42+
button.BackColor = CurrentTheme.ButtonBackColor;
43+
}
44+
45+
if (control is TextBox || control is ListView || control is ListBox || control is ComboBox)
46+
{
47+
control.ForeColor = CurrentTheme.TextBoxForeColor;
48+
control.BackColor = CurrentTheme.TextBoxBackColor;
49+
}
50+
51+
if (control is ToolStrip toolStrip)
52+
{
53+
ApplyToolStripTheme(toolStrip);
54+
}
55+
56+
foreach (Control child in control.Controls)
57+
{
58+
ApplyToControl(child);
59+
}
60+
}
61+
62+
private static void ApplyToolStripTheme(ToolStrip toolStrip)
63+
{
64+
toolStrip.BackColor = CurrentTheme.SurfaceColor;
65+
toolStrip.ForeColor = CurrentTheme.ForeColor;
66+
67+
foreach (ToolStripItem item in toolStrip.Items)
68+
{
69+
item.BackColor = CurrentTheme.SurfaceColor;
70+
item.ForeColor = CurrentTheme.ForeColor;
71+
}
72+
}
73+
74+
75+
private static void Suspend(Control control)
76+
{
77+
control.SuspendLayout();
78+
79+
foreach (Control child in control.Controls)
80+
{
81+
Suspend(child);
82+
}
83+
}
84+
85+
private static void Resume(Control control)
86+
{
87+
foreach (Control child in control.Controls)
88+
{
89+
Resume(child);
90+
}
91+
92+
control.ResumeLayout();
93+
}
94+
95+
public static bool IsDarkMode()
96+
{
97+
using var key = Registry.CurrentUser.OpenSubKey(
98+
@"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize");
99+
100+
if (key?.GetValue("AppsUseLightTheme") is int value)
101+
{
102+
return value == 0;
103+
}
104+
105+
return false;
106+
}
107+
}
108+
109+
public class AppTheme
110+
{
111+
public Color BackColor { get; set; }
112+
public Color SurfaceColor { get; set; }
113+
public Color ForeColor { get; set; }
114+
public Color BorderColor { get; set; }
115+
public Color AccentColor { get; set; }
116+
public Color ButtonBackColor { get; set; }
117+
public Color ButtonForeColor { get; set; }
118+
public Color TextBoxBackColor { get; set; }
119+
public Color TextBoxForeColor { get; set; }
120+
}
121+
122+
public static class Themes
123+
{
124+
public static readonly AppTheme Plain = new()
125+
{
126+
BackColor = SystemColors.Control,
127+
SurfaceColor = SystemColors.ControlLight,
128+
ForeColor = SystemColors.ControlText,
129+
BorderColor = SystemColors.ControlDark,
130+
AccentColor = SystemColors.Highlight,
131+
ButtonBackColor = SystemColors.Control,
132+
ButtonForeColor = SystemColors.ControlText,
133+
TextBoxBackColor = SystemColors.Window,
134+
TextBoxForeColor = SystemColors.WindowText
135+
};
136+
137+
public static AppTheme Dark = new()
138+
{
139+
BackColor = Color.FromArgb(30, 30, 30),
140+
SurfaceColor = Color.FromArgb(45, 45, 45),
141+
ForeColor = Color.White,
142+
BorderColor = Color.FromArgb(70, 70, 70),
143+
AccentColor = Color.FromArgb(0, 120, 215),
144+
ButtonBackColor = Color.FromArgb(60, 60, 60),
145+
ButtonForeColor = Color.White,
146+
TextBoxBackColor = Color.FromArgb(50, 50, 50),
147+
TextBoxForeColor = Color.White
148+
};
149+
150+
public static AppTheme Light = new()
151+
{
152+
BackColor = Color.White,
153+
SurfaceColor = Color.FromArgb(240, 240, 240),
154+
ForeColor = Color.Black,
155+
BorderColor = Color.LightGray,
156+
AccentColor = Color.FromArgb(0, 120, 215),
157+
ButtonBackColor = Color.FromArgb(230, 230, 230),
158+
ButtonForeColor = Color.Black,
159+
TextBoxBackColor = Color.White,
160+
TextBoxForeColor = Color.Black
161+
};
162+
}
163+
}

0 commit comments

Comments
 (0)