Skip to content

Commit a932745

Browse files
committed
* Remove power stuff; not appropriate in here without consideration for rest of library
* Simplify code * Check for frozen brushes * Remove a optional parameter which had crept onto public API on earlier an checkin...no optional params on public surface.
1 parent 6253766 commit a932745

File tree

2 files changed

+33
-65
lines changed

2 files changed

+33
-65
lines changed

MahMaterialDragablzMashUp/PaletteSelectorViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ private static void ApplyBase(bool isDark)
2525

2626
private static void ApplyPrimary(Swatch swatch)
2727
{
28-
new PaletteHelper().ReplacePrimaryColor(swatch, true);
28+
new PaletteHelper().ReplacePrimaryColor(swatch);
2929
}
3030

3131
public ICommand ApplyAccentCommand { get; } = new AnotherCommandImplementation(o => ApplyAccent((Swatch)o));

MaterialDesignThemes.Wpf/PaletteHelper.cs

Lines changed: 32 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public void SetLightDark(bool isDark)
3232

3333
source =
3434
$"pack://application:,,,/MahApps.Metro;component/Styles/Accents/{(isDark ? "BaseDark" : "BaseLight")}.xaml";
35-
var newMahAppsResourceDictionary = new ResourceDictionary() { Source = new Uri(source) };
35+
var newMahAppsResourceDictionary = new ResourceDictionary { Source = new Uri(source) };
3636

3737
Application.Current.Resources.MergedDictionaries.Remove(existingMahAppsResourceDictionary);
3838
Application.Current.Resources.MergedDictionaries.Add(newMahAppsResourceDictionary);
3939
}
4040

41-
public void ReplacePrimaryColor(Swatch swatch, bool mahapps = false)
41+
public void ReplacePrimaryColor(Swatch swatch)
4242
{
4343
if (swatch == null) throw new ArgumentNullException(nameof(swatch));
4444

@@ -60,24 +60,24 @@ public void ReplacePrimaryColor(Swatch swatch, bool mahapps = false)
6060
ReplaceEntry("PrimaryHueDarkBrush", new SolidColorBrush(dark.Color));
6161
ReplaceEntry("PrimaryHueDarkForegroundBrush", new SolidColorBrush(dark.Foreground));
6262

63-
if (mahapps)
64-
{
65-
ReplaceEntry("HighlightBrush", new SolidColorBrush(dark.Color));
66-
ReplaceEntry("AccentColorBrush", new SolidColorBrush(list[5].Color));
67-
ReplaceEntry("AccentColorBrush2", new SolidColorBrush(list[4].Color));
68-
ReplaceEntry("AccentColorBrush3", new SolidColorBrush(list[3].Color));
69-
ReplaceEntry("AccentColorBrush4", new SolidColorBrush(list[2].Color));
70-
ReplaceEntry("WindowTitleColorBrush", new SolidColorBrush(dark.Color));
71-
ReplaceEntry("AccentSelectedColorBrush", new SolidColorBrush(list[5].Foreground));
72-
ReplaceEntry("ProgressBrush", new LinearGradientBrush(dark.Color, list[3].Color, 90.0));
73-
ReplaceEntry("CheckmarkFill", new SolidColorBrush(list[5].Color));
74-
ReplaceEntry("RightArrowFill", new SolidColorBrush(list[5].Color));
75-
ReplaceEntry("IdealForegroundColorBrush", new SolidColorBrush(list[5].Foreground));
76-
ReplaceEntry("IdealForegroundDisabledBrush", new SolidColorBrush(dark.Color) { Opacity = .4 });
77-
}
63+
//mahapps brushes
64+
/*
65+
ReplaceEntry("HighlightBrush", new SolidColorBrush(dark.Color));
66+
ReplaceEntry("AccentColorBrush", new SolidColorBrush(list[5].Color));
67+
ReplaceEntry("AccentColorBrush2", new SolidColorBrush(list[4].Color));
68+
ReplaceEntry("AccentColorBrush3", new SolidColorBrush(list[3].Color));
69+
ReplaceEntry("AccentColorBrush4", new SolidColorBrush(list[2].Color));
70+
ReplaceEntry("WindowTitleColorBrush", new SolidColorBrush(dark.Color));
71+
ReplaceEntry("AccentSelectedColorBrush", new SolidColorBrush(list[5].Foreground));
72+
ReplaceEntry("ProgressBrush", new LinearGradientBrush(dark.Color, list[3].Color, 90.0));
73+
ReplaceEntry("CheckmarkFill", new SolidColorBrush(list[5].Color));
74+
ReplaceEntry("RightArrowFill", new SolidColorBrush(list[5].Color));
75+
ReplaceEntry("IdealForegroundColorBrush", new SolidColorBrush(list[5].Foreground));
76+
ReplaceEntry("IdealForegroundDisabledBrush", new SolidColorBrush(dark.Color) { Opacity = .4 });
77+
*/
7878
}
7979

80-
public void ReplacePrimaryColor(string name, bool mahapps = false)
80+
public void ReplacePrimaryColor(string name)
8181
{
8282
if (name == null) throw new ArgumentNullException(nameof(name));
8383

@@ -87,7 +87,7 @@ public void ReplacePrimaryColor(string name, bool mahapps = false)
8787
if (swatch == null)
8888
throw new ArgumentException($"No such swatch '{name}'", nameof(name));
8989

90-
ReplacePrimaryColor(swatch, mahapps);
90+
ReplacePrimaryColor(swatch);
9191
}
9292

9393
public void ReplaceAccentColor(Swatch swatch)
@@ -116,69 +116,37 @@ public void ReplaceAccentColor(string name)
116116

117117
ReplaceAccentColor(swatch);
118118
}
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;
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+
147120
/// <summary>
148121
/// Replaces a certain entry anywhere in the parent dictionary and its merged dictionaries
149122
/// </summary>
150123
/// <param name="entryName">The entry to replace</param>
151124
/// <param name="newValue">The new entry value</param>
152125
/// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
153-
/// <returns>Weather the value was replaced (true) or not (false)</returns>
154-
private static bool ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null, bool animate = true)
155-
{
156-
const int DURATION_MS = 500; //Change the value if needed
126+
private static void ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null)
127+
{
157128
if (parentDictionary == null)
158129
parentDictionary = Application.Current.Resources;
159130

160131
if (parentDictionary.Contains(entryName))
161132
{
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()
133+
var brush = parentDictionary[entryName] as SolidColorBrush;
134+
if (brush != null && !brush.IsFrozen)
135+
{
136+
var animation = new ColorAnimation
166137
{
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
138+
From = ((SolidColorBrush)parentDictionary[entryName]).Color,
139+
To = ((SolidColorBrush)newValue).Color,
140+
Duration = new Duration(TimeSpan.FromMilliseconds(300))
170141
};
171-
(parentDictionary[entryName] as SolidColorBrush).BeginAnimation(SolidColorBrush.ColorProperty, animation); //Begin the animation
142+
brush.BeginAnimation(SolidColorBrush.ColorProperty, animation);
172143
}
173144
else
174145
parentDictionary[entryName] = newValue; //Set value normally
175-
return true;
176146
}
147+
177148
foreach (var dictionary in parentDictionary.MergedDictionaries)
178-
if (ReplaceEntry(entryName, newValue, dictionary))
179-
return true;
180-
181-
return false;
149+
ReplaceEntry(entryName, newValue, dictionary);
182150
}
183151
}
184152
}

0 commit comments

Comments
 (0)