implement NativeTheme API with shouldUseDarkColors

This commit is contained in:
Gregor Biswanger
2020-05-13 14:16:36 +02:00
parent 12a3f2689e
commit 169abc2376
8 changed files with 89 additions and 1 deletions

View File

@@ -78,5 +78,10 @@
/// Allows you to execute native Lock and Unlock process.
/// </summary>
public static PowerMonitor PowerMonitor { get { return PowerMonitor.Instance; } }
/// <summary>
/// Read and respond to changes in Chromium's native color theme.
/// </summary>
public static NativeTheme NativeTheme { get { return NativeTheme.Instance; } }
}
}

View File

@@ -0,0 +1,55 @@
using System.Threading.Tasks;
namespace ElectronNET.API
{
/// <summary>
/// Read and respond to changes in Chromium's native color theme.
/// </summary>
public sealed class NativeTheme
{
private static NativeTheme _nativeTheme;
private static object _syncRoot = new object();
internal NativeTheme() { }
internal static NativeTheme Instance
{
get
{
if (_nativeTheme == null)
{
lock (_syncRoot)
{
if (_nativeTheme == null)
{
_nativeTheme = new NativeTheme();
}
}
}
return _nativeTheme;
}
}
/// <summary>
/// A `Boolean` for if the OS / Chromium currently has a dark mode enabled or is
/// being instructed to show a dark-style UI.If you want to modify this value you
/// should use `themeSource` below.
/// </summary>
/// <returns></returns>
public Task<bool> ShouldUseDarkColorsAsync()
{
var taskCompletionSource = new TaskCompletionSource<bool>();
BridgeConnector.Socket.On("nativeTheme-shouldUseDarkColors-completed", (shouldUseDarkColors) => {
BridgeConnector.Socket.Off("nativeTheme-shouldUseDarkColors-completed");
taskCompletionSource.SetResult((bool)shouldUseDarkColors);
});
BridgeConnector.Socket.Emit("nativeTheme-shouldUseDarkColors");
return taskCompletionSource.Task;
}
}
}