Skip to content

Commit

Permalink
Add Multi-Window Support for ThemeService #10
Browse files Browse the repository at this point in the history
  • Loading branch information
ghost1372 committed Dec 7, 2024
1 parent c90a62e commit 11c3dc0
Show file tree
Hide file tree
Showing 7 changed files with 5,418 additions and 8,391 deletions.
13,441 changes: 5,200 additions & 8,241 deletions dev/DevWinUI.Controls/Themes/Generic.xaml

Large diffs are not rendered by default.

33 changes: 30 additions & 3 deletions dev/DevWinUI/Helpers/WindowHelper/WindowHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,35 @@ namespace DevWinUI;

public partial class WindowHelper
{
internal static List<Win32Window> processWindowList = new List<Win32Window>();
internal static Process currentProcess;
internal static List<Win32Window> topLevelWindowList = new List<Win32Window>();
public static List<Window> ActiveWindows { get { return _activeWindows; } }
private static List<Window> _activeWindows = new List<Window>();

public static void TrackWindow(Window window)
{
window.Closed += (sender, args) =>
{
_activeWindows.Remove(window);
};

_activeWindows.AddIfNotExists(window);
}
public static Window GetWindowForElement(UIElement element)
{
if (element.XamlRoot != null)
{
foreach (Window window in _activeWindows)
{
if (element.XamlRoot == window.Content.XamlRoot)
{
return window;
}
}
}
return null;
}
public static void SwitchToThisWindow(Window window)
{
if (window != null)
Expand Down Expand Up @@ -54,7 +83,6 @@ public static void SetWindowCornerRadius(IntPtr hwnd, NativeValues.DWM_WINDOW_CO
}
}
}
internal static List<Win32Window> topLevelWindowList = new List<Win32Window>();
public static IReadOnlyList<Win32Window> GetTopLevelWindows()
{
unsafe
Expand All @@ -74,8 +102,7 @@ static BOOL EnumWindowsCallback(HWND hWnd, LPARAM lParam)
}
}
}
internal static List<Win32Window> processWindowList = new List<Win32Window>();
internal static Process currentProcess;

public static IReadOnlyList<Win32Window> GetProcessWindowList()
{
unsafe
Expand Down
1 change: 0 additions & 1 deletion dev/DevWinUI/Services/ThemeService/IThemeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public interface IThemeService
delegate void ActualThemeChangedEventHandler(FrameworkElement sender, object args);
event ActualThemeChangedEventHandler ActualThemeChanged;

Window Window { get; set; }
SystemBackdrop GetSystemBackdrop();
SystemBackdrop GetSystemBackdrop(BackdropType backdropType);
BackdropType GetBackdropType();
Expand Down
114 changes: 60 additions & 54 deletions dev/DevWinUI/Services/ThemeService/ThemeService.Configs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public partial class ThemeService
{
private void ConfigBackdropBase(BackdropType backdropType, bool force)
{
if (Window == null)
if (WindowHelper.ActiveWindows.Count == 0)
{
return;
}
Expand All @@ -20,9 +20,9 @@ private void ConfigBackdropBase(BackdropType backdropType, bool force)

if (backdropType != BackdropType.None)
{
var systemBackdrop = GetSystemBackdropFromLocalConfig(backdropType, force);
backdropType = GetSystemBackdropFromLocalConfig(backdropType, force);

SetWindowSystemBackdrop(systemBackdrop);
SetWindowSystemBackdrop(backdropType);
}
}

Expand All @@ -41,42 +41,45 @@ private void ConfigTintColorBase(Color color, bool force)
}
private void ConfigTintColorBase()
{
var systemBackdrop = Window.SystemBackdrop;
if (systemBackdrop != null)
foreach (var window in WindowHelper.ActiveWindows)
{
if (IsDarkTheme())
var systemBackdrop = window.SystemBackdrop;
if (systemBackdrop != null)
{
switch (GetBackdropType())
if (IsDarkTheme())
{
case BackdropType.Mica:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_Dark, false);
break;
case BackdropType.MicaAlt:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_MicaAlt_Dark, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigTintColorBase(AcrylicSystemBackdrop.Default_TintColor_Dark, false);
break;
switch (GetBackdropType())
{
case BackdropType.Mica:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_Dark, false);
break;
case BackdropType.MicaAlt:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_MicaAlt_Dark, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigTintColorBase(AcrylicSystemBackdrop.Default_TintColor_Dark, false);
break;
}
}
}
else
{
switch (GetBackdropType())
else
{
case BackdropType.Mica:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_Light, false);
break;
case BackdropType.MicaAlt:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_MicaAlt_Light, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigTintColorBase(AcrylicSystemBackdrop.Default_TintColor_Light, false);
break;
switch (GetBackdropType())
{
case BackdropType.Mica:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_Light, false);
break;
case BackdropType.MicaAlt:
ConfigTintColorBase(MicaSystemBackdrop.Default_TintColor_MicaAlt_Light, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigTintColorBase(AcrylicSystemBackdrop.Default_TintColor_Light, false);
break;
}
}
}

}
}
}
private void ConfigFallbackColorBase(Color color, bool force)
Expand All @@ -95,33 +98,36 @@ private void ConfigFallbackColorBase(Color color, bool force)

private void ConfigFallbackColorBase()
{
var systemBackdrop = Window.SystemBackdrop;
if (systemBackdrop != null)
foreach (var window in WindowHelper.ActiveWindows)
{
if (IsDarkTheme())
var systemBackdrop = window.SystemBackdrop;
if (systemBackdrop != null)
{
switch (GetBackdropType())
if (IsDarkTheme())
{
case BackdropType.MicaAlt:
ConfigFallbackColorBase(MicaSystemBackdrop.Default_FallbackColor_MicaAlt_Dark, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigFallbackColorBase(AcrylicSystemBackdrop.Default_FallbackColor_Dark, false);
break;
switch (GetBackdropType())
{
case BackdropType.MicaAlt:
ConfigFallbackColorBase(MicaSystemBackdrop.Default_FallbackColor_MicaAlt_Dark, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigFallbackColorBase(AcrylicSystemBackdrop.Default_FallbackColor_Dark, false);
break;
}
}
}
else
{
switch (GetBackdropType())
else
{
case BackdropType.MicaAlt:
ConfigFallbackColorBase(MicaSystemBackdrop.Default_FallbackColor_MicaAlt_Light, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigFallbackColorBase(AcrylicSystemBackdrop.Default_FallbackColor_Light, false);
break;
switch (GetBackdropType())
{
case BackdropType.MicaAlt:
ConfigFallbackColorBase(MicaSystemBackdrop.Default_FallbackColor_MicaAlt_Light, false);
break;
case BackdropType.AcrylicThin:
case BackdropType.AcrylicBase:
ConfigFallbackColorBase(AcrylicSystemBackdrop.Default_FallbackColor_Light, false);
break;
}
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions dev/DevWinUI/Services/ThemeService/ThemeService.Get.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public ElementTheme GetActualTheme()

public SystemBackdrop GetSystemBackdrop()
{
return Window.SystemBackdrop;
return WindowHelper.ActiveWindows[0].SystemBackdrop;
}

public SystemBackdrop GetSystemBackdrop(BackdropType backdropType)
Expand Down Expand Up @@ -58,7 +58,7 @@ public SystemBackdrop GetSystemBackdrop(BackdropType backdropType)

public BackdropType GetBackdropType()
{
return GetBackdropType(Window.SystemBackdrop);
return GetBackdropType(WindowHelper.ActiveWindows[0].SystemBackdrop);
}

public BackdropType GetBackdropType(SystemBackdrop systemBackdrop)
Expand All @@ -85,15 +85,15 @@ public BackdropType GetBackdropType(SystemBackdrop systemBackdrop)
}
}

private SystemBackdrop GetSystemBackdropFromLocalConfig(BackdropType backdropType, bool ForceBackdrop)
private BackdropType GetSystemBackdropFromLocalConfig(BackdropType backdropType, bool ForceBackdrop)
{
BackdropType currentBackdrop = backdropType;
if (this.useAutoSave && GlobalData.Config != null)
{
currentBackdrop = GlobalData.Config.BackdropType;
}

return ForceBackdrop ? GetSystemBackdrop(backdropType) : GetSystemBackdrop(currentBackdrop);
return ForceBackdrop ? backdropType : currentBackdrop;
}

private Color GetBackdropTintColorFromLocalConfig(Color color, bool ForceTintColor)
Expand Down
80 changes: 47 additions & 33 deletions dev/DevWinUI/Services/ThemeService/ThemeService.Set.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
namespace DevWinUI;

using Microsoft.UI.Composition.SystemBackdrops;
using Microsoft.UI.Dispatching;

namespace DevWinUI;
public partial class ThemeService
{
private void SetWindowSystemBackdrop(SystemBackdrop systemBackdrop)
private void SetWindowSystemBackdrop(BackdropType backdropType)
{
Window.SystemBackdrop = systemBackdrop;
DispatcherQueue.GetForCurrentThread().TryEnqueue(() =>
{
foreach (var window in WindowHelper.ActiveWindows)
{
window.SystemBackdrop = GetSystemBackdrop(backdropType);
}
});
}

public void SetBackdropType(BackdropType backdropType)
{
var systemBackdrop = GetSystemBackdrop(backdropType);

if (useAutoSave && GlobalData.Config != null)
{
if (GlobalData.Config.BackdropType != backdropType)
{
SetWindowSystemBackdrop(systemBackdrop);
SetWindowSystemBackdrop(backdropType);

GlobalData.Config.BackdropType = backdropType;
GlobalData.Save();
}
}
else
{
SetWindowSystemBackdrop(systemBackdrop);
SetWindowSystemBackdrop(backdropType);
}
}

Expand All @@ -46,44 +54,50 @@ public void SetElementThemeWithoutSave(ElementTheme elementTheme)

public void SetBackdropTintColor(Color color)
{
var systemBackdrop = Window.SystemBackdrop;
if (systemBackdrop != null)
foreach (var window in WindowHelper.ActiveWindows)
{
if (systemBackdrop is MicaSystemBackdrop mica)
{
mica.TintColor = color;
}
else if (systemBackdrop is AcrylicSystemBackdrop acrylic)
var systemBackdrop = window.SystemBackdrop;
if (systemBackdrop != null)
{
acrylic.TintColor = color;
}
if (systemBackdrop is MicaSystemBackdrop mica)
{
mica.TintColor = color;
}
else if (systemBackdrop is AcrylicSystemBackdrop acrylic)
{
acrylic.TintColor = color;
}

if (useAutoSave && GlobalData.Config != null && GlobalData.Config.BackdropTintColor != color)
{
GlobalData.Config.BackdropTintColor = color;
GlobalData.Save();
if (useAutoSave && GlobalData.Config != null && GlobalData.Config.BackdropTintColor != color)
{
GlobalData.Config.BackdropTintColor = color;
GlobalData.Save();
}
}
}
}

public void SetBackdropFallbackColor(Color color)
{
var systemBackdrop = Window.SystemBackdrop;
if (systemBackdrop != null)
foreach (var window in WindowHelper.ActiveWindows)
{
if (systemBackdrop is MicaSystemBackdrop mica)
{
mica.FallbackColor = color;
}
else if (systemBackdrop is AcrylicSystemBackdrop acrylic)
var systemBackdrop = window.SystemBackdrop;
if (systemBackdrop != null)
{
acrylic.FallbackColor = color;
}
if (systemBackdrop is MicaSystemBackdrop mica)
{
mica.FallbackColor = color;
}
else if (systemBackdrop is AcrylicSystemBackdrop acrylic)
{
acrylic.FallbackColor = color;
}

if (useAutoSave && GlobalData.Config != null && GlobalData.Config.BackdropFallBackColor != color)
{
GlobalData.Config.BackdropFallBackColor = color;
GlobalData.Save();
if (useAutoSave && GlobalData.Config != null && GlobalData.Config.BackdropFallBackColor != color)
{
GlobalData.Config.BackdropFallBackColor = color;
GlobalData.Save();
}
}
}
}
Expand Down
Loading

0 comments on commit 11c3dc0

Please sign in to comment.