Skip to content

Commit

Permalink
chore: refactor various transparency effects so only one can be activ…
Browse files Browse the repository at this point in the history
…e at a time
  • Loading branch information
sophie authored and sophie-gilbert committed Jan 23, 2024
1 parent d87b1f4 commit ffd9550
Show file tree
Hide file tree
Showing 12 changed files with 318 additions and 289 deletions.
140 changes: 0 additions & 140 deletions FoxTunes.UI.Windows/Behaviours/WindowAccentBehaviour.cs

This file was deleted.

This file was deleted.

84 changes: 84 additions & 0 deletions FoxTunes.UI.Windows/Behaviours/WindowAcrylicBlurBehaviour.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 WindowAcrylicBlurBehaviour : WindowBlurProvider
{
public const string ID = "BBBBC45C-11A4-4A2A-83F2-4FFED3C72C3E";

public override string Id
{
get
{
return this.Id;
}
}

public WindowAcrylicBlurBehaviour()
{
this.AccentColors = new Dictionary<IntPtr, Color>();
}

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

public Color AccentColor { get; private set; }

public override void InitializeComponent(ICore core)
{
base.InitializeComponent(core);
this.Configuration.GetElement<TextConfigurationElement>(
WindowAcrylicBlurBehaviourConfiguration.SECTION,
WindowAcrylicBlurBehaviourConfiguration.ACCENT_COLOR
).ConnectValue(value =>
{
if (string.IsNullOrEmpty(value))
{
AccentColor = WindowExtensions.DefaultAccentColor;
}
else
{
AccentColor = value.ToColor();
}
if (this.AccentColors.Any())
{
this.Refresh();
}
});
}

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

public override IEnumerable<ConfigurationSection> GetConfigurationSections()
{
return WindowAcrylicBlurBehaviourConfiguration.GetConfigurationSections();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Collections.Generic;

namespace FoxTunes
{
public static class WindowAcrylicBlurBehaviourConfiguration
{
public const string SECTION = WindowsUserInterfaceConfiguration.SECTION;

public const string TRANSPARENCY_PROVIDER = WindowsUserInterfaceConfiguration.TRANSPARENCY_PROVIDER;

public const string ACCENT_COLOR = "OOOO5DBB-8ACE-4FFC-B975-9131D4D82947";

public static IEnumerable<ConfigurationSection> GetConfigurationSections()
{
yield return new ConfigurationSection(SECTION)
.WithElement(
new SelectionConfigurationElement(TRANSPARENCY_PROVIDER).WithOptions(GetProviders()))
.WithElement(
new TextConfigurationElement(ACCENT_COLOR, Strings.WindowsUserInterfaceConfiguration_AccentColor).DependsOn(SECTION, TRANSPARENCY_PROVIDER, WindowAcrylicBlurBehaviour.ID)
);
}

private static IEnumerable<SelectionConfigurationOption> GetProviders()
{
yield return new SelectionConfigurationOption(WindowAcrylicBlurBehaviour.ID, Strings.WindowAcrylicBlurBehaviour_Name);
}
}
}
66 changes: 12 additions & 54 deletions FoxTunes.UI.Windows/Behaviours/WindowBlurBehaviour.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using FoxTunes.Interfaces;
using System;
using System.Collections.Generic;

namespace FoxTunes
{
Expand All @@ -8,31 +7,21 @@ namespace FoxTunes
//TODO: SetWindowCompositionAttribute is undocumented.
//TODO: Assuming Windows 8.
[PlatformDependency(Major = 6, Minor = 2)]
public class WindowBlurBehaviour : StandardBehaviour, IDisposable
public class WindowBlurBehaviour : WindowBlurProvider
{
public IConfiguration Configuration { get; private set; }
public const string ID = "AAAA8904-827D-449F-A69C-EA57C17852BC";

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)
public override string Id
{
this.Refresh();
get
{
return this.Id;
}
}

protected virtual void Refresh()
protected override void Refresh()
{
if (!this.Transparency.Value)
if (!this.IsEnabled)
{
return;
}
Expand All @@ -42,40 +31,9 @@ protected virtual void Refresh()
}
}

public bool IsDisposed { get; private set; }

public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
public override IEnumerable<ConfigurationSection> GetConfigurationSections()
{
if (this.IsDisposed || !disposing)
{
return;
}
this.OnDisposing();
this.IsDisposed = true;
}

protected virtual void OnDisposing()
{
WindowBase.ActiveChanged -= this.OnActiveChanged;
}

~WindowBlurBehaviour()
{
Logger.Write(this.GetType(), LogLevel.Error, "Component was not disposed: {0}", this.GetType().Name);
try
{
this.Dispose(true);
}
catch
{
//Nothing can be done, never throw on GC thread.
}
return WindowBlurBehaviourConfiguration.GetConfigurationSections();
}
}
}
24 changes: 24 additions & 0 deletions FoxTunes.UI.Windows/Behaviours/WindowBlurBehaviourConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

namespace FoxTunes
{
public static class WindowBlurBehaviourConfiguration
{
public const string SECTION = WindowsUserInterfaceConfiguration.SECTION;

public const string TRANSPARENCY_PROVIDER = WindowsUserInterfaceConfiguration.TRANSPARENCY_PROVIDER;

public static IEnumerable<ConfigurationSection> GetConfigurationSections()
{
yield return new ConfigurationSection(SECTION)
.WithElement(
new SelectionConfigurationElement(TRANSPARENCY_PROVIDER).WithOptions(GetProviders())
);
}

private static IEnumerable<SelectionConfigurationOption> GetProviders()
{
yield return new SelectionConfigurationOption(WindowBlurBehaviour.ID, Strings.WindowBlurBehaviour_Name).Default();
}
}
}
Loading

0 comments on commit ffd9550

Please sign in to comment.