Skip to content

Commit 169abc2

Browse files
implement NativeTheme API with shouldUseDarkColors
1 parent 12a3f26 commit 169abc2

File tree

8 files changed

+89
-1
lines changed

8 files changed

+89
-1
lines changed

ElectronNET.API/Electron.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,10 @@ public static class Electron
7878
/// Allows you to execute native Lock and Unlock process.
7979
/// </summary>
8080
public static PowerMonitor PowerMonitor { get { return PowerMonitor.Instance; } }
81+
82+
/// <summary>
83+
/// Read and respond to changes in Chromium's native color theme.
84+
/// </summary>
85+
public static NativeTheme NativeTheme { get { return NativeTheme.Instance; } }
8186
}
8287
}

ElectronNET.API/NativeTheme.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Threading.Tasks;
2+
3+
namespace ElectronNET.API
4+
{
5+
/// <summary>
6+
/// Read and respond to changes in Chromium's native color theme.
7+
/// </summary>
8+
public sealed class NativeTheme
9+
{
10+
private static NativeTheme _nativeTheme;
11+
private static object _syncRoot = new object();
12+
13+
internal NativeTheme() { }
14+
15+
internal static NativeTheme Instance
16+
{
17+
get
18+
{
19+
if (_nativeTheme == null)
20+
{
21+
lock (_syncRoot)
22+
{
23+
if (_nativeTheme == null)
24+
{
25+
_nativeTheme = new NativeTheme();
26+
}
27+
}
28+
}
29+
30+
return _nativeTheme;
31+
}
32+
}
33+
34+
/// <summary>
35+
/// A `Boolean` for if the OS / Chromium currently has a dark mode enabled or is
36+
/// being instructed to show a dark-style UI.If you want to modify this value you
37+
/// should use `themeSource` below.
38+
/// </summary>
39+
/// <returns></returns>
40+
public Task<bool> ShouldUseDarkColorsAsync()
41+
{
42+
var taskCompletionSource = new TaskCompletionSource<bool>();
43+
44+
BridgeConnector.Socket.On("nativeTheme-shouldUseDarkColors-completed", (shouldUseDarkColors) => {
45+
BridgeConnector.Socket.Off("nativeTheme-shouldUseDarkColors-completed");
46+
47+
taskCompletionSource.SetResult((bool)shouldUseDarkColors);
48+
});
49+
50+
BridgeConnector.Socket.Emit("nativeTheme-shouldUseDarkColors");
51+
52+
return taskCompletionSource.Task;
53+
}
54+
}
55+
}

ElectronNET.CLI/Commands/Actions/DeployEmbeddedElectronFiles.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public static void Do(string tempPath)
3131
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "autoUpdater.js", "api.");
3232
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "browserView.js", "api.");
3333
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "powerMonitor.js", "api.");
34+
EmbeddedFileHelper.DeployEmbeddedFile(hostApiFolder, "nativeTheme.js", "api.");
3435

3536
string splashscreenFolder = Path.Combine(tempPath, "splashscreen");
3637
if (Directory.Exists(splashscreenFolder) == false)

ElectronNET.CLI/ElectronNET.CLI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
<EmbeddedResource Include="..\ElectronNET.Host\api\autoUpdater.js" Link="ElectronHost\api\autoUpdater.js" />
7373
<EmbeddedResource Include="..\ElectronNET.Host\api\browserView.js" Link="ElectronHost\api\browserView.js" />
7474
<EmbeddedResource Include="..\ElectronNET.Host\api\powerMonitor.js" Link="ElectronHost\api\powerMonitor.js" />
75+
<EmbeddedResource Include="..\ElectronNET.Host\api\nativeTheme.js" Link="ElectronHost\api\nativeTheme.js" />
7576
</ItemGroup>
7677

7778
<ItemGroup>

ElectronNET.Host/api/nativeTheme.js

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ElectronNET.Host/api/nativeTheme.js.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { nativeTheme } from 'electron';
2+
let electronSocket;
3+
4+
export = (socket: SocketIO.Socket) => {
5+
electronSocket = socket;
6+
7+
socket.on('nativeTheme-shouldUseDarkColors', () => {
8+
const shouldUseDarkColors = nativeTheme.shouldUseDarkColors;
9+
10+
electronSocket.emit('nativeTheme-shouldUseDarkColors-completed', shouldUseDarkColors);
11+
});
12+
};

ElectronNET.Host/main.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let globalShortcut, shellApi, screen, clipboard, autoUpdater;
1010
let commandLine, browserView;
1111
let powerMonitor;
1212
let splashScreen, hostHook;
13-
let mainWindowId;
13+
let mainWindowId, nativeTheme;
1414

1515
let manifestJsonFileName = 'electron.manifest.json';
1616
let watchable = false;
@@ -154,6 +154,7 @@ function startSocketApiBridge(port) {
154154
delete require.cache[require.resolve('./api/clipboard')];
155155
delete require.cache[require.resolve('./api/browserView')];
156156
delete require.cache[require.resolve('./api/powerMonitor')];
157+
delete require.cache[require.resolve('./api/nativeTheme')];
157158
});
158159

159160
global['electronsocket'] = socket;
@@ -176,6 +177,7 @@ function startSocketApiBridge(port) {
176177
clipboard = require('./api/clipboard')(socket);
177178
browserView = require('./api/browserView')(socket);
178179
powerMonitor = require('./api/powerMonitor')(socket);
180+
nativeTheme = require('./api/nativeTheme')(socket);
179181

180182
try {
181183
const hostHookScriptFilePath = path.join(__dirname, 'ElectronHostHook', 'index.js');

0 commit comments

Comments
 (0)