Skip to content

Commit

Permalink
Enable wallpaper sink to set individual wallpapers per screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sparten11740 committed Nov 29, 2021
1 parent 03a63af commit 678d096
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 49 deletions.
44 changes: 43 additions & 1 deletion AllMyLights.Test/Connectors/WallpaperSinkTest.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections.Generic;
using System.IO;
using AllMyLights.Connectors.Sinks.Wallpaper;
using AllMyLights.Models.OpenRGB;
using AllMyLights.Platforms;
using Moq;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace AllMyLights.Test
Expand All @@ -23,6 +24,26 @@ public void Should_set_background()
desktopMock.Verify();
}

[Test]
public void Should_set_background_per_screen()
{
var input = JObject.Parse(@"{""0"": ""C:\\absolute\\path\\batman.jpg"", ""1"": ""C:\\absolute\\path\\joker.jpg""}");

var expectedFilePathByScreen = new Dictionary<int, string>()
{
{ 0, @"C:\absolute\path\batman.jpg" },
{ 1, @"C:\absolute\path\joker.jpg" },
};

var desktopMock = new Mock<IDesktop>();
desktopMock.Setup(it => it.SetBackgrounds(expectedFilePathByScreen)).Verifiable();

var sink = new WallpaperSink(new WallpaperSinkOptions(), desktopMock.Object);
sink.Consume(input);

desktopMock.Verify();
}

[Test]
public void Should_do_nothing_on_receiving_the_same_value_twice()
{
Expand Down Expand Up @@ -58,5 +79,26 @@ public void Should_prepend_configured_directory_to_relative_paths()

desktopMock.Verify(it => it.SetBackground(expected));
}

[Test]
public void Should_prepend_configured_directory_to_relative_paths_for_each_display()
{
var input = JObject.Parse(@"{""0"": ""relative\\path\\batman.jpg"", ""1"": ""relative\\path\\alfred.jpg""}");

var expectedFilePathByScreen = new Dictionary<int, string>()
{
{ 0, @"D:\wayne\enterprises\relative\path\batman.jpg" },
{ 1, @"D:\wayne\enterprises\relative\path\alfred.jpg" },
};

var desktopMock = new Mock<IDesktop>();
desktopMock.Setup(it => it.SetBackgrounds(expectedFilePathByScreen)).Verifiable();

var relativeTo = @"D:\wayne\enterprises";
var sink = new WallpaperSink(new WallpaperSinkOptions() { RelativeTo = relativeTo }, desktopMock.Object);
sink.Consume(input);

desktopMock.Verify();
}
}
}
29 changes: 21 additions & 8 deletions AllMyLights.Test/Transformations/MappingTransformationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using AllMyLights.Transformations;
using AllMyLights.Transformations.Mapping;
using Microsoft.Reactive.Testing;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

namespace AllMyLights.Test
Expand All @@ -22,6 +23,18 @@ public void Should_match_simple_string()
.Verify();
}

[Test]
public void Should_match_simple_string_and_return_object()
{
var to = new JObject();

new Validator()
.AddMapping("red", to)
.StartWith("red")
.ExpectOutput(to)
.Verify();
}

[Test]
public void Should_match_regex_and_replace_substitutions()
{
Expand Down Expand Up @@ -61,7 +74,7 @@ private class Validator
};

private string Input { get; set; }
private string Output { get; set; }
private object Output { get; set; }
private bool ShouldReturnEmpty { get; set; } = false;

public Validator StartWith(string input)
Expand All @@ -70,12 +83,12 @@ public Validator StartWith(string input)
return this;
}

public Validator AddMapping(string match, string substitute)
public Validator AddMapping(string match, object to)
{
Options.Mappings.Add(new MappingTransformationOptions.Mapping()
{
From = match,
To = substitute
To = to
});
return this;
}
Expand All @@ -86,7 +99,7 @@ public Validator FailOnMiss()
return this;
}

public Validator ExpectOutput(string output)
public Validator ExpectOutput(object output)
{
Output = output;
return this;
Expand Down Expand Up @@ -115,16 +128,16 @@ public void Verify()
if (ShouldReturnEmpty)
{

var expected = new Recorded<Notification<string>>[] {
OnCompleted<string>(10)
var expected = new Recorded<Notification<object>>[] {
OnCompleted<object>(10)
};
ReactiveAssert.AreElementsEqual(expected, actual.Messages);
}
else
{
var expected = new Recorded<Notification<string>>[] {
var expected = new Recorded<Notification<object>>[] {
OnNext(10, Output),
OnCompleted<string>(10)
OnCompleted<object>(10)
};
ReactiveAssert.AreElementsEqual(expected, actual.Messages);
}
Expand Down
1 change: 0 additions & 1 deletion AllMyLights/Connectors/ConnectorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using AllMyLights.Connectors.Sources.Mqtt;
using AllMyLights.Connectors.Sources.OpenRGB;
using AllMyLights.Extensions;
using AllMyLights.Models.OpenRGB;
using AllMyLights.Platforms;
using MQTTnet;
using MQTTnet.Extensions.ManagedClient;
Expand Down
2 changes: 1 addition & 1 deletion AllMyLights/Connectors/Sinks/SinkOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Runtime.Serialization;
using AllMyLights.Connectors.Sinks.Chroma;
using AllMyLights.Connectors.Sinks.OpenRGB;
using AllMyLights.Connectors.Sinks.Wallpaper;
using AllMyLights.JsonConverters;
using AllMyLights.Models.OpenRGB;
using Newtonsoft.Json;

namespace AllMyLights.Connectors.Sinks
Expand Down
50 changes: 39 additions & 11 deletions AllMyLights/Connectors/Sinks/Wallpaper/WallpaperSink.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reactive.Linq;
using AllMyLights.Models.OpenRGB;
using AllMyLights.Extensions;
using AllMyLights.Platforms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using NLog;

namespace AllMyLights.Connectors.Sinks.Wallpaper
Expand All @@ -22,27 +25,52 @@ public WallpaperSink(WallpaperSinkOptions options, IDesktop desktop): base(optio
{
Desktop = desktop;
RelativeTo = options.RelativeTo;
Next.DistinctUntilChanged().Subscribe((value) =>
Next.DistinctUntilChanged().Subscribe((raw) =>
{
if(value is string)
Logger.Debug(@$"{ToString()} received value {raw})");

if (raw is string value)
{
Logger.Debug(@$"{nameof(WallpaperSink)} received value {value}. Applying background. {(!string.IsNullOrEmpty(RelativeTo) ? $"Relative to: {RelativeTo}" : "")}");
Desktop.SetBackground(PrependRelativeTo(value as string));
} else
Logger.Debug($"Applying background. {RelativeTo?.Let((it) => $"Relative to: {it}") ?? ""}");
Desktop.SetBackground(PrependRelativeTo(value));
return;
}


if (raw is JObject mapping)
{
Logger.Error($"Received value {value} cannot be consumed by {nameof(WallpaperSink)}");
try
{
Logger.Debug("Assuming that received value is mapping of file path to display name.");
var filePathByScreen = mapping.ToObject<Dictionary<int, string>>()
.ToDictionary(pair => pair.Key, pair => PrependRelativeTo(pair.Value));

Logger.Debug(() => $"Activating the following wallpapers: {JsonConvert.SerializeObject(filePathByScreen)}");
Desktop.SetBackgrounds(filePathByScreen);
}
catch(JsonReaderException e)
{
Logger.Error($"The provided mapping is invalid: {e.Message}");
}
catch(Exception e)
{
Logger.Error(e.Message);
}
return;
}



Logger.Error($"Received value {raw} cannot be consumed by {nameof(WallpaperSink)}");
});
}

public override object GetInfo()
{
if (string.IsNullOrEmpty(RelativeTo)) return null;

try
{
string[] files = Directory.GetFiles(RelativeTo);
return $"Available file names under specified directory {RelativeTo}: {string.Join(", ", files.Select((it) => Path.GetFileName(it)))}";
string[] files = !string.IsNullOrEmpty(RelativeTo) ? Directory.GetFiles(RelativeTo) : null;
return new WallpaperSinkInfo(RelativeTo, files, Desktop.GetScreens());
}
catch(Exception e)
{
Expand Down
26 changes: 26 additions & 0 deletions AllMyLights/Connectors/Sinks/Wallpaper/WallpaperSinkInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections.Generic;
using Newtonsoft.Json;

namespace AllMyLights.Connectors.Sinks.Wallpaper
{
public struct WallpaperSinkInfo
{
public WallpaperSinkInfo(
string relativeTo,
IEnumerable<string> imageFiles,
IEnumerable<int> screens)
{
RelativeTo = relativeTo;
ImageFiles = imageFiles;
Screens = screens;
}

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
public string RelativeTo { get; }


[JsonProperty(NullValueHandling = NullValueHandling.Ignore, PropertyName = "Available file names under specified directory")]
public IEnumerable<string> ImageFiles { get; }
public IEnumerable<int> Screens { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using AllMyLights.Connectors.Sinks;

namespace AllMyLights.Models.OpenRGB
namespace AllMyLights.Connectors.Sinks.Wallpaper
{
public class WallpaperSinkOptions: SinkOptions
{
Expand Down
14 changes: 14 additions & 0 deletions AllMyLights/Extensions/Objects.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace AllMyLights.Extensions
{
public static class Objects
{
public static R Let<T, R>(this T self, Func<T, R> block)
{
return block(self);
}
}
}
4 changes: 4 additions & 0 deletions AllMyLights/Platforms/Desktop.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using AllMyLights.Platforms.Windows;
using NLog;
Expand All @@ -19,6 +20,9 @@ public static Desktop GetPlatformInstance()
return null;
}

public abstract IEnumerable<int> GetScreens();

public abstract void SetBackground(string filePath);
public abstract void SetBackgrounds(Dictionary<int, string> filePathByScreen);
}
}
6 changes: 5 additions & 1 deletion AllMyLights/Platforms/IDesktop.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
namespace AllMyLights.Platforms
using System.Collections.Generic;

namespace AllMyLights.Platforms
{
public interface IDesktop
{
void SetBackground(string filePath);
void SetBackgrounds(Dictionary<int, string> filePathByScreen);
IEnumerable<int> GetScreens();
}
}
Loading

0 comments on commit 678d096

Please sign in to comment.