|
4 | 4 | using System.Text.RegularExpressions;
|
5 | 5 | using System.Windows;
|
6 | 6 | using System.Windows.Media;
|
7 |
| - |
| 7 | +using System.Windows.Media.Animation; |
| 8 | +using System.Runtime.InteropServices; |
8 | 9 | namespace MaterialDesignThemes.Wpf
|
9 | 10 | {
|
10 | 11 | public class PaletteHelper
|
@@ -115,32 +116,69 @@ public void ReplaceAccentColor(string name)
|
115 | 116 |
|
116 | 117 | ReplaceAccentColor(swatch);
|
117 | 118 | }
|
| 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; |
118 | 138 |
|
| 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; |
119 | 147 | /// <summary>
|
120 | 148 | /// Replaces a certain entry anywhere in the parent dictionary and its merged dictionaries
|
121 | 149 | /// </summary>
|
122 | 150 | /// <param name="entryName">The entry to replace</param>
|
123 | 151 | /// <param name="newValue">The new entry value</param>
|
124 | 152 | /// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
|
125 | 153 | /// <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) |
127 | 155 | {
|
| 156 | + const int DURATION_MS = 500; //Change the value if needed |
128 | 157 | if (parentDictionary == null)
|
129 | 158 | parentDictionary = Application.Current.Resources;
|
130 |
| - |
| 159 | + |
131 | 160 | if (parentDictionary.Contains(entryName))
|
132 | 161 | {
|
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 |
134 | 175 | return true;
|
135 | 176 | }
|
136 |
| - |
137 | 177 | foreach (var dictionary in parentDictionary.MergedDictionaries)
|
138 |
| - { |
139 | 178 | if (ReplaceEntry(entryName, newValue, dictionary))
|
140 | 179 | return true;
|
141 |
| - } |
142 |
| - |
| 180 | + |
143 | 181 | return false;
|
144 | 182 | }
|
145 |
| - } |
| 183 | + } |
146 | 184 | }
|
0 commit comments