Skip to content

Commit b7960eb

Browse files
PR fixes
1 parent e77f48b commit b7960eb

File tree

2 files changed

+46
-33
lines changed

2 files changed

+46
-33
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System.ComponentModel;
2+
3+
namespace ElectronNET.API.Entities
4+
{
5+
/// <summary>
6+
/// Defines the ThemeSourceMode enumeration.
7+
/// </summary>
8+
public enum ThemeSourceMode
9+
{
10+
/// <summary>
11+
/// Operating system default.
12+
/// </summary>
13+
[Description("system")]
14+
System,
15+
16+
/// <summary>
17+
/// Light theme.
18+
/// </summary>
19+
[Description("light")]
20+
Light,
21+
22+
/// <summary>
23+
/// Dark theme.
24+
/// </summary>
25+
[Description("dark")]
26+
Dark
27+
}
28+
}

ElectronNET.API/NativeTheme.cs

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Threading.Tasks;
3+
using ElectronNET.API.Entities;
4+
using ElectronNET.API.Extensions;
35

46
namespace ElectronNET.API
57
{
@@ -33,24 +35,9 @@ internal static NativeTheme Instance
3335
}
3436

3537
/// <summary>
36-
/// Checks if the new ThemeSource is valid.
37-
/// </summary>
38-
/// <param name="themeSource">The new ThemeSource to check.</param>
39-
/// <returns>True, if is a valid ThemeSource.</returns>
40-
internal bool IsValidThemeSource(string themeSource)
41-
{
42-
var result =
43-
string.Equals(themeSource, "dark", StringComparison.OrdinalIgnoreCase) ||
44-
string.Equals(themeSource, "light", StringComparison.OrdinalIgnoreCase) ||
45-
string.Equals(themeSource, "system", StringComparison.OrdinalIgnoreCase);
46-
47-
return result;
48-
}
49-
50-
/// <summary>
51-
/// Setting this property to 'system' will remove the override and everything will be reset to the OS default. By default 'ThemeSource' is 'system'.
38+
/// Setting this property to <see cref="ThemeSourceMode.System"/> will remove the override and everything will be reset to the OS default. By default 'ThemeSource' is <see cref="ThemeSourceMode.System"/>.
5239
/// <para/>
53-
/// Settings this property to 'dark' will have the following effects:
40+
/// Settings this property to <see cref="ThemeSourceMode.Dark"/> will have the following effects:
5441
/// <list type="bullet">
5542
/// <item>
5643
/// <description><see cref="ShouldUseDarkColorsAsync"/> will be <see langword="true"/> when accessed</description>
@@ -69,10 +56,10 @@ internal bool IsValidThemeSource(string themeSource)
6956
/// </item>
7057
/// </list>
7158
/// <para/>
72-
/// Settings this property to 'light' will have the following effects:
59+
/// Settings this property to <see cref="ThemeSourceMode.Light"/> will have the following effects:
7360
/// <list type="bullet">
7461
/// <item>
75-
/// <description><see cref="ShouldUseDarkColorsAsync"/> will be <see langword="false"/> false when accessed</description>
62+
/// <description><see cref="ShouldUseDarkColorsAsync"/> will be <see langword="false"/> when accessed</description>
7663
/// </item>
7764
/// <item>
7865
/// <description>Any UI Electron renders on Linux and Windows including context menus, devtools, etc. will use the light UI.</description>
@@ -91,42 +78,40 @@ internal bool IsValidThemeSource(string themeSource)
9178
/// <para/>
9279
/// <list type="bullet">
9380
/// <item>
94-
/// <description>Follow OS: SetThemeSource("system");</description>
81+
/// <description>Follow OS: SetThemeSource(ThemeSourceMode.System);</description>
9582
/// </item>
9683
/// <item>
97-
/// <description>Dark Mode: SetThemeSource("dark");</description>
84+
/// <description>Dark Mode: SetThemeSource(ThemeSourceMode.Dark);</description>
9885
/// </item>
9986
/// <item>
100-
/// <description>Light Mode: SetThemeSource("light");</description>
87+
/// <description>Light Mode: SetThemeSource(ThemeSourceMode.Light);</description>
10188
/// </item>
10289
/// </list>
10390
/// Your application should then always use <see cref="ShouldUseDarkColorsAsync"/> to determine what CSS to apply.
10491
/// </summary>
10592
/// <param name="themeSource">The new ThemeSource.</param>
106-
public void SetThemeSource(string themeSource)
93+
public void SetThemeSource(ThemeSourceMode themeSourceMode)
10794
{
108-
// Check for supported themeSource, otherwise it sets the default
109-
if (!IsValidThemeSource(themeSource))
110-
{
111-
themeSource = "system";
112-
}
95+
var themeSource = themeSourceMode.GetDescription();
11396

114-
BridgeConnector.Socket.Emit("nativeTheme-themeSource", themeSource.ToLower());
97+
BridgeConnector.Socket.Emit("nativeTheme-themeSource", themeSource);
11598
}
11699

117100
/// <summary>
118-
/// A <see cref="string"/> property that can be 'system', 'light' or 'dark'. It is used to override (<seealso cref="SetThemeSource"/>) and
101+
/// A <see cref="ThemeSourceMode"/> property that can be <see cref="ThemeSourceMode.System"/>, <see cref="ThemeSourceMode.Light"/> or <see cref="ThemeSourceMode.Dark"/>. It is used to override (<seealso cref="SetThemeSource"/>) and
119102
/// supercede the value that Chromium has chosen to use internally.
120103
/// </summary>
121-
public Task<string> GetThemeSourceAsync()
104+
public Task<ThemeSourceMode> GetThemeSourceAsync()
122105
{
123-
var taskCompletionSource = new TaskCompletionSource<string>();
106+
var taskCompletionSource = new TaskCompletionSource<ThemeSourceMode>();
124107

125108
BridgeConnector.Socket.On("nativeTheme-themeSource-getCompleted", (themeSource) =>
126109
{
127110
BridgeConnector.Socket.Off("nativeTheme-themeSource-getCompleted");
128111

129-
taskCompletionSource.SetResult((string)themeSource);
112+
var themeSourceValue = (ThemeSourceMode)Enum.Parse(typeof(ThemeSourceMode), (string)themeSource, true);
113+
114+
taskCompletionSource.SetResult(themeSourceValue);
130115
});
131116

132117
BridgeConnector.Socket.Emit("nativeTheme-themeSource-get");

0 commit comments

Comments
 (0)