Skip to content

Commit 6253766

Browse files
committed
Merge branch 'PaletteHelper-fade-animation' of https://github.com/SuicSoft/MaterialDesignInXamlToolkit into SuicSoft-PaletteHelper-fade-animation
2 parents a27c227 + 76ee7cd commit 6253766

File tree

1 file changed

+47
-9
lines changed

1 file changed

+47
-9
lines changed

MaterialDesignThemes.Wpf/PaletteHelper.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Text.RegularExpressions;
55
using System.Windows;
66
using System.Windows.Media;
7-
7+
using System.Windows.Media.Animation;
8+
using System.Runtime.InteropServices;
89
namespace MaterialDesignThemes.Wpf
910
{
1011
public class PaletteHelper
@@ -115,32 +116,69 @@ public void ReplaceAccentColor(string name)
115116

116117
ReplaceAccentColor(swatch);
117118
}
119+
#region Pinvoke
120+
public struct PowerState
121+
{
122+
public ACLineStatus ACLineStatus;
123+
public byte BatteryFlag;
124+
public Byte BatteryLifePercent;
125+
public Byte Reserved1;
126+
public Int32 BatteryLifeTime;
127+
public Int32 BatteryFullLifeTime;
128+
}
129+
[DllImport("Kernel32", EntryPoint = "GetSystemPowerStatus")]
130+
private static extern bool GetSystemPowerStatusRef(PowerState sps);
131+
132+
#endregion
133+
public static PowerState GetPowerState()
134+
{
135+
PowerState state = new PowerState();
136+
if (GetSystemPowerStatusRef(state))
137+
return state;
118138

139+
throw new ApplicationException("Unable to get power state");
140+
}
141+
// Note: Underlying type of byte to match Win32 header
142+
public enum ACLineStatus : byte
143+
{
144+
Offline = 0, Online = 1, Unknown = 255
145+
}
146+
public static bool DisableAnimationOnBattery = true;
119147
/// <summary>
120148
/// Replaces a certain entry anywhere in the parent dictionary and its merged dictionaries
121149
/// </summary>
122150
/// <param name="entryName">The entry to replace</param>
123151
/// <param name="newValue">The new entry value</param>
124152
/// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
125153
/// <returns>Weather the value was replaced (true) or not (false)</returns>
126-
private static bool ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null)
154+
private static bool ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null, bool animate = true)
127155
{
156+
const int DURATION_MS = 500; //Change the value if needed
128157
if (parentDictionary == null)
129158
parentDictionary = Application.Current.Resources;
130-
159+
131160
if (parentDictionary.Contains(entryName))
132161
{
133-
parentDictionary[entryName] = newValue;
162+
bool battery = GetPowerState().ACLineStatus == ACLineStatus.Online | !DisableAnimationOnBattery;
163+
if (animate & parentDictionary[entryName] != null & battery & parentDictionary[entryName] as SolidColorBrush != null) //Fade animation is enabled , type is solidcolorbrush and value is not null.
164+
{
165+
ColorAnimation animation = new ColorAnimation()
166+
{
167+
From = ((SolidColorBrush)parentDictionary[entryName]).Color,//The old color
168+
To = ((SolidColorBrush)newValue).Color, //The new color
169+
Duration = new Duration(new TimeSpan(0,0,0,0,DURATION_MS)) //Set the duration
170+
};
171+
(parentDictionary[entryName] as SolidColorBrush).BeginAnimation(SolidColorBrush.ColorProperty, animation); //Begin the animation
172+
}
173+
else
174+
parentDictionary[entryName] = newValue; //Set value normally
134175
return true;
135176
}
136-
137177
foreach (var dictionary in parentDictionary.MergedDictionaries)
138-
{
139178
if (ReplaceEntry(entryName, newValue, dictionary))
140179
return true;
141-
}
142-
180+
143181
return false;
144182
}
145-
}
183+
}
146184
}

0 commit comments

Comments
 (0)