Skip to content

Commit

Permalink
add NightLight strength value, refactors
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Jul 10, 2023
1 parent e05c729 commit 2748ef5
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 29 deletions.
23 changes: 10 additions & 13 deletions Artemis.MediaInfo/Artemis.MediaInfo.csproj
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0-windows10.0.17763.0</TargetFramework>
<TargetFramework>net7.0-windows10.0.17763.0</TargetFramework>
<Platforms>x64</Platforms>
<PlatformTarget>x64</PlatformTarget>
<IncludeBuildOutput>false</IncludeBuildOutput>
<Nullable>enable</Nullable>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="6.0.0" />
<PackageReference Include="ArtemisRGB.UI.Shared" IncludeAssets="compile;build;buildTransitive" Version="1.2023.111.4" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />

<PackageReference Update="@(PackageReference)" IncludeAssets="compile;build" />

<PackageReference Include="Dubya.WindowsMediaController" Version="2.4.0" IncludeAssets="compile;lib:runtime" />
<Reference Include="Artemis.Core">
<HintPath>..\..\Artemis\src\Artemis.Core\bin\net6.0\Artemis.Core.dll</HintPath>
<Private>false</Private>
</Reference>
<PackageReference Include="ArtemisRGB.UI.Shared" IncludeAssets="compile" Version="1.2023.522.4" />
<PackageReference Include="Microsoft.Windows.Compatibility" Version="7.0.4" />
<PackageReference Include="Dubya.WindowsMediaController" Version="2.5.0" />
<PackageReference Include="System.Windows.Extensions" IncludeAssets="native" Version="7.0.0" />
</ItemGroup>

<ItemGroup>
<Content Include="plugin.json" CopyToOutputDirectory="PreserveNewest" />
<PackageReference Include="ArtemisRGB.Plugins.BuildTask" Version="1.1.0" />
<PackageReference Include="ArtemisRGB.Plugins.BuildTask" Version="2.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
6 changes: 4 additions & 2 deletions Artemis.MediaInfo/DataModels/WindowsInfoDataModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Artemis.Core.ColorScience;
using Artemis.Core.Modules;
using Artemis.Core.Modules;
using SkiaSharp;

namespace Artemis.MediaInfo.DataModels;
Expand All @@ -11,6 +10,9 @@ public class WindowsInfoDataModel : DataModel

[DataModelProperty(Name = "Night Lights Enabled")]
public bool NightLightsEnabled { get; set; }

[DataModelProperty(Name = "Night Light Strength")]
public double NightLightsStrength { get; set; }

[DataModelProperty(Name = "Desktop Accent Color")]
public SKColor AccentColor { get; set; } = SKColor.Empty;
Expand Down
6 changes: 2 additions & 4 deletions Artemis.MediaInfo/MediaInfoModule.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;
using Artemis.Core;
using Artemis.Core;
using Artemis.Core.Modules;
using System.Collections.Generic;
using System.Linq;
using Artemis.MediaInfo.DataModels;
using Windows.Media.Control;
using Artemis.MediaInfo.MediaWatch;
using static WindowsMediaController.MediaManager;
using static Artemis.MediaInfo.MediaInfoHelper;
using static Artemis.MediaInfo.Utils.MediaInfoHelper;

namespace Artemis.MediaInfo;

Expand Down
5 changes: 4 additions & 1 deletion Artemis.MediaInfo/MediaWatch/MediaWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Artemis.MediaInfo.MediaWatch;

/// <summary>
/// Wrapping class that it's only dependency is Dubya.WindowsMediaController
/// </summary>
public class MediaWatcher
{
private readonly MediaManager _mediaManager = new();
Expand Down Expand Up @@ -65,7 +68,7 @@ private void MediaManagerOnOnFocusedSessionChanged(MediaSession? mediaSession)
_currentSession = mediaSession;
_currentSession.OnPlaybackStateChanged += MediaSession_OnPlaybackStateChanged;
}
FocusedMediaChanged?.Invoke(this, new FocusedMediaChangedEventArgs(mediaSession, null));
FocusedMediaChanged?.Invoke(this, new FocusedMediaChangedEventArgs(mediaSession, mediaSession?.ControlSession.GetPlaybackInfo()));
}

private void MediaManager_OnSessionOpened(MediaSession mediaSession)
Expand Down
8 changes: 1 addition & 7 deletions Artemis.MediaInfo/Utils/MediaInfoHelper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
using System;
using System.Threading.Tasks;
using Windows.Media.Control;
using Windows.Storage.Streams;
using Artemis.Core.ColorScience;
using SkiaSharp;

namespace Artemis.MediaInfo;
namespace Artemis.MediaInfo.Utils;

public static class MediaInfoHelper
{
internal static async Task<ColorSwatch> ReadMediaColors(GlobalSystemMediaTransportControlsSessionMediaProperties mediaProperties)
{
return await ReadMediaColors(mediaProperties.Thumbnail);
}

internal static async Task<ColorSwatch> ReadMediaColors(IRandomAccessStreamReference thumbnail)
{
var imageStream = await thumbnail.OpenReadAsync();
Expand Down
31 changes: 30 additions & 1 deletion Artemis.MediaInfo/WindowsInfoModule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using Artemis.Core.Modules;
using Artemis.MediaInfo.DataModels;
using Artemis.MediaInfo.Utils;
Expand All @@ -13,13 +14,20 @@ public class WindowsInfoModule : Module<WindowsInfoDataModel>
@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CloudStore\\" +
@"Store\\DefaultAccount\\Current\\default$windows.data.bluelightreduction.bluelightreductionstate\\" +
@"windows.data.bluelightreduction.bluelightreductionstate", "Data");
private readonly RegistryWatcher _nightLightSettingsWatcher = new(WatchedRegistry.CurrentUser,
@"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\CloudStore\\" +
@"Store\\DefaultAccount\\Current\\default$windows.data.bluelightreduction.settings\\" +
@"windows.data.bluelightreduction.settings", "Data");
private readonly RegistryWatcher _accentColorWatcher = new(WatchedRegistry.CurrentUser,
@"SOFTWARE\\Microsoft\\Windows\\DWM", "AccentColor");

public override void Enable()
{
_nightLightStateWatcher.RegistryChanged += UpdateNightLight;
_nightLightStateWatcher.StartWatching();

_nightLightSettingsWatcher.RegistryChanged += NightLightSettingsWatcherOnRegistryChanged;
_nightLightSettingsWatcher.StartWatching();

_accentColorWatcher.RegistryChanged += UpdateAccentColor;
_accentColorWatcher.StartWatching();
Expand Down Expand Up @@ -86,6 +94,19 @@ private void UpdateAccentColor(object? sender, RegistryChangedEventArgs registry
}
}

private void NightLightSettingsWatcherOnRegistryChanged(object? sender, RegistryChangedEventArgs e)
{
var data = e.Data;
if (data is null)
{
DataModel.NightLightsStrength = 0;
return;
}

var byteData = (byte[])data;
DataModel.NightLightsStrength = ParseNightLightStrength(byteData);
}

private static SKColor ParseDWordColor(int color)
{
var a = (byte)((color >> 24) & 0xFF);
Expand All @@ -95,4 +116,12 @@ private static SKColor ParseDWordColor(int color)

return new SKColor(r, g, b, a);
}

private static double ParseNightLightStrength(byte[] data)
{
const int min = 4832;
const int max = 26056;
var value = BitConverter.ToInt16(data, 35);
return 1 - (double)(value - min) / (max - min);
}
}
2 changes: 1 addition & 1 deletion Artemis.MediaInfo/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"Main": "Artemis.MediaInfo.dll",
"Name": "Media Info",
"Repository": "https://github.com/Aytackydln/Artemis.MediaInfo",
"Version": "1.2.1.0",
"Version": "1.3.0.0",
"Platforms":"Windows"
}

0 comments on commit 2748ef5

Please sign in to comment.