Skip to content

Commit

Permalink
feat: set window accent color from album artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
misha-gilbert authored and sophie-gilbert committed Jan 23, 2024
1 parent 50aa705 commit bcc55dd
Show file tree
Hide file tree
Showing 16 changed files with 448 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Interop;
using System.Windows.Threading;

namespace FoxTunes
{
Expand Down
2 changes: 1 addition & 1 deletion FoxTunes.Core/ComponentScanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public static bool HasPlatform(Type type)
{
foreach (var dependency in dependencies)
{
var version = new Version(dependency.Major, dependency.Minor);
var version = new Version(dependency.Major, dependency.Minor, dependency.Build);
if (Environment.OSVersion.Version < version)
{
Logger.Write(typeof(ComponentScanner), LogLevel.Debug, "Not loading component \"{0}\": Requires platform {1}.{2}.", type.FullName, dependency.Major, dependency.Minor);
Expand Down
2 changes: 2 additions & 0 deletions FoxTunes.Core/PlatformDependencyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public PlatformDependencyAttribute()

public int Minor { get; set; }

public int Build { get; set; }

public ProcessorArchitecture Architecture { get; set; }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public SelectionProperties() : base(false)

public IPlaylistManager PlaylistManager { get; private set; }

public IPlaybackManager PlaybackManager { get; private set; }

public ILibraryHierarchyBrowser LibraryHierarchyBrowser { get; private set; }

public IPlaylistBrowser PlaylistBrowser { get; private set; }
Expand Down Expand Up @@ -265,6 +267,8 @@ protected override void InitializeComponent(ICore core)
this.PlaylistManager = core.Managers.Playlist;
this.PlaylistManager.SelectedPlaylistChanged += this.OnSelectedPlaylistChanged;
this.PlaylistManager.SelectedItemsChanged += this.OnSelectedItemsChanged;
this.PlaybackManager = core.Managers.Playback;
this.PlaybackManager.CurrentStreamChanged += this.OnCurrentStreamChanged;
this.LibraryHierarchyBrowser = core.Components.LibraryHierarchyBrowser;
this.PlaylistBrowser = core.Components.PlaylistBrowser;
this.SignalEmitter = core.Components.SignalEmitter;
Expand Down Expand Up @@ -344,6 +348,22 @@ protected virtual void OnSelectedItemsChanged(object sender, EventArgs e)
this.Dispatch(() => this.Refresh(playlistItems));
}

protected virtual void OnCurrentStreamChanged(object sender, EventArgs e)
{
var outputStream = this.PlaybackManager.CurrentStream;
if (outputStream == null)
{
return;
}
var playlistItems = new[] { outputStream.PlaylistItem };
if (object.ReferenceEquals(this.FileDatasSource, playlistItems))
{
return;
}
this.FileDatasSource = playlistItems;
this.Dispatch(() => this.Refresh(playlistItems));
}

protected virtual Task OnSignal(object sender, ISignal signal)
{
switch (signal.Name)
Expand Down
1 change: 0 additions & 1 deletion FoxTunes.UI.Windows.MiniPlayer/ViewModel/Mini.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using FoxTunes.Interfaces;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
Expand Down
4 changes: 4 additions & 0 deletions FoxTunes.UI.Windows.Themes/TransparentTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ namespace FoxTunes
{
[Component(ID)]
[WindowsUserInterfaceDependency]
//TODO: Not sure of the exact required platform.
//TODO: SetWindowCompositionAttribute is undocumented.
//TODO: Assuming Windows 8.
[PlatformDependency(Major = 6, Minor = 2)]
public class TransparentTheme : ThemeBase, IConfigurableComponent
{
public const string ID = "191C2E5B-4732-4CC7-BC31-4CC040DCFEC9";
Expand Down
99 changes: 99 additions & 0 deletions FoxTunes.UI.Windows/Behaviours/WindowAccentBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
using FoxTunes.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;

namespace FoxTunes
{
[WindowsUserInterfaceDependency]
//Requires Windows 11 22H2.
[PlatformDependency(Major = 6, Minor = 2, Build = 22621)]
public class WindowAccentBehaviour : StandardBehaviour
{
public WindowAccentBehaviour()
{
this.AccentColors = new Dictionary<IntPtr, Color>();
}

public IDictionary<IntPtr, Color> AccentColors { get; private set; }

public IConfiguration Configuration { get; private set; }

public BooleanConfigurationElement Transparency { get; private set; }

public TextConfigurationElement AccentColor { get; private set; }

public override void InitializeComponent(ICore core)
{
WindowBase.ActiveChanged += this.OnActiveChanged;
this.Configuration = core.Components.Configuration;
this.Transparency = this.Configuration.GetElement<BooleanConfigurationElement>(
WindowsUserInterfaceConfiguration.SECTION,
WindowsUserInterfaceConfiguration.TRANSPARENCY
);
this.AccentColor = this.Configuration.GetElement<TextConfigurationElement>(
WindowsUserInterfaceConfiguration.SECTION,
WindowsUserInterfaceConfiguration.ACCENT_COLOR
);
this.AccentColor.ValueChanged += this.OnValueChanged;
base.InitializeComponent(core);
}

protected virtual void OnActiveChanged(object sender, EventArgs e)
{
this.Refresh();
}

protected virtual void OnValueChanged(object sender, EventArgs e)
{
this.Refresh();
}

protected virtual void Refresh()
{
if (!this.Transparency.Value)
{
return;
}
var color = default(Color);
if (!string.IsNullOrEmpty(this.AccentColor.Value))
{
color = this.AccentColor.Value.ToColor();
}
else
{
color = WindowExtensions.DefaultAccentColor;
}
this.Refresh(color);
}

protected virtual void Refresh(Color color)
{
var windows = new HashSet<IntPtr>();
foreach (var window in WindowBase.Active)
{
windows.Add(window.Handle);
var currentColor = default(Color);
if (AccentColors.TryGetValue(window.Handle, out currentColor) && currentColor == color)
{
continue;
}
this.Refresh(window, color);
this.AccentColors[window.Handle] = color;
}
foreach (var handle in AccentColors.Keys.ToArray())
{
if (!windows.Contains(handle))
{
AccentColors.Remove(handle);
}
}
}

protected virtual void Refresh(WindowBase window, Color color)
{
WindowExtensions.EnableAcrylicBlur(window.Handle, color);
}
}
}
45 changes: 45 additions & 0 deletions FoxTunes.UI.Windows/Behaviours/WindowBlurBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using FoxTunes.Interfaces;
using System;

namespace FoxTunes
{
[WindowsUserInterfaceDependency]
//TODO: Not sure of the exact required platform.
//TODO: SetWindowCompositionAttribute is undocumented.
//TODO: Assuming Windows 8.
[PlatformDependency(Major = 6, Minor = 2)]
public class WindowBlurBehaviour : StandardBehaviour
{
public IConfiguration Configuration { get; private set; }

public BooleanConfigurationElement Transparency { get; private set; }

public override void InitializeComponent(ICore core)
{
WindowBase.ActiveChanged += this.OnActiveChanged;
this.Configuration = core.Components.Configuration;
this.Transparency = this.Configuration.GetElement<BooleanConfigurationElement>(
WindowsUserInterfaceConfiguration.SECTION,
WindowsUserInterfaceConfiguration.TRANSPARENCY
);
base.InitializeComponent(core);
}

protected virtual void OnActiveChanged(object sender, EventArgs e)
{
this.Refresh();
}

protected virtual void Refresh()
{
if (!this.Transparency.Value)
{
return;
}
foreach (var window in WindowBase.Active)
{
WindowExtensions.EnableBlur(window.Handle);
}
}
}
}
Loading

0 comments on commit bcc55dd

Please sign in to comment.