Skip to content

Commit

Permalink
Design time fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Kinnara committed Dec 12, 2019
1 parent b1144c1 commit 5e604fb
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.7.0-preview.191209.0</Version>
<Version>0.7.0-preview.191212.14</Version>
<Authors>Yimeng Wu</Authors>
<Product>ModernWPF UI Library</Product>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
8 changes: 8 additions & 0 deletions ModernWpf.Controls/ModernWpf.Controls.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<PackageId>ModernWpfUI</PackageId>
</PropertyGroup>

<ItemGroup>
<Page Remove="Properties\DesignTimeResources.xaml" />
</ItemGroup>

<ItemGroup>
<None Include="Properties\DesignTimeResources.xaml" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
Expand Down
24 changes: 22 additions & 2 deletions ModernWpf/Controls/XamlControlsResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ public class XamlControlsResources : ResourceDictionary
/// </summary>
public XamlControlsResources()
{
MergedDictionaries.Add(new ResourceDictionary { Source = PackUriHelper.GetAbsoluteUri("ControlsResources.xaml") });
MergedDictionaries.Add(ControlsResources);

if (DesignMode.DesignModeEnabled)
{
_ = CompactResources;
}
}

public bool UseCompactResources
Expand All @@ -23,7 +28,8 @@ public bool UseCompactResources
if (_useCompactResources != value)
{
_useCompactResources = value;
if (_useCompactResources)

if (UseCompactResources)
{
MergedDictionaries.Add(CompactResources);
}
Expand All @@ -35,6 +41,18 @@ public bool UseCompactResources
}
}

internal static ResourceDictionary ControlsResources
{
get
{
if (_controlsResources == null)
{
_controlsResources = new ResourceDictionary { Source = PackUriHelper.GetAbsoluteUri("ControlsResources.xaml") };
}
return _controlsResources;
}
}

internal static ResourceDictionary CompactResources
{
get
Expand All @@ -47,7 +65,9 @@ internal static ResourceDictionary CompactResources
}
}

private static ResourceDictionary _controlsResources;
private static ResourceDictionary _compactResources;

private bool _useCompactResources;
}
}
9 changes: 7 additions & 2 deletions ModernWpf/Helpers/ColorsHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public static void RemoveShades(ResourceDictionary colors)
}

public void UpdateBrushes(ResourceDictionary themeDictionary)
{
UpdateBrushes(themeDictionary, _colors);
}

public static void UpdateBrushes(ResourceDictionary themeDictionary, ResourceDictionary colors)
{
int count = 0;

Expand All @@ -97,9 +102,9 @@ public void UpdateBrushes(ResourceDictionary themeDictionary)
if (entry.Value is SolidColorBrush brush)
{
object colorKey = ThemeResourceHelper.GetColorKey(brush);
if (colorKey != null && _colors.Contains(colorKey))
if (colorKey != null && colors.Contains(colorKey))
{
brush.SetCurrentValue(SolidColorBrush.ColorProperty, (Color)_colors[colorKey]);
brush.SetCurrentValue(SolidColorBrush.ColorProperty, (Color)colors[colorKey]);
count++;
}
}
Expand Down
8 changes: 8 additions & 0 deletions ModernWpf/ModernWpf.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<Resource Include="Transitions\Storyboards\**" />
</ItemGroup>

<ItemGroup>
<Page Remove="Properties\DesignTimeResources.xaml" />
</ItemGroup>

<ItemGroup>
<None Include="Properties\DesignTimeResources.xaml" />
</ItemGroup>

<ItemGroup>
<Compile Update="Strings\Resources.Designer.cs">
<DesignTime>True</DesignTime>
Expand Down
15 changes: 15 additions & 0 deletions ModernWpf/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ static ThemeManager()
{
_actualThemeChangedEventArgs = new RoutedEventArgs(ActualThemeChangedEvent);
MenuDropAlignmentHelper.EnsureStandardPopupAlignment();

if (DesignMode.DesignModeEnabled)
{
_ = GetDefaultThemeDictionary(LightKey);
_ = GetDefaultThemeDictionary(DarkKey);
_ = GetDefaultThemeDictionary(HighContrastKey);
}
}

private ThemeManager()
Expand Down Expand Up @@ -653,6 +660,14 @@ private static void StartOrStopListeningForHighContrastChanges(FrameworkElement

public TypedEventHandler<ThemeManager, object> ActualAccentColorChanged;

internal static void UpdateThemeBrushes(ResourceDictionary colors)
{
foreach (var themeDictionary in _defaultThemeDictionaries.Values)
{
ColorsHelper.UpdateBrushes(themeDictionary, colors);
}
}

internal static ResourceDictionary GetDefaultThemeDictionary(string key)
{
if (!_defaultThemeDictionaries.TryGetValue(key, out ResourceDictionary dictionary))
Expand Down
48 changes: 41 additions & 7 deletions ModernWpf/ThemeResources.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,21 +94,52 @@ private void DesignTimeInit()
private void UpdateDesignTimeSystemColors()
{
Debug.Assert(DesignMode.DesignModeEnabled);
var rd = GetDesignTimeSystemColors();

var colors = GetDesignTimeSystemColors();
if (MergedDictionaries.Count == 0)
{
MergedDictionaries.Add(rd);
MergedDictionaries.Add(colors);
}
else
{
MergedDictionaries[0] = rd;
MergedDictionaries[0] = colors;
}

ThemeManager.UpdateThemeBrushes(colors);
}

private void UpdateDesignTimeThemeDictionary()
{
Debug.Assert(DesignMode.DesignModeEnabled);
ApplyApplicationTheme(RequestedTheme ?? ApplicationTheme.Light);

if (SystemParameters.HighContrast)
{
EnsureHighContrastResources();
updateTo(_highContrastResources);
}
else
{
var appTheme = RequestedTheme ?? ApplicationTheme.Light;
switch (appTheme)
{
case ApplicationTheme.Light:
EnsureLightResources();
updateTo(_lightResources);
break;
case ApplicationTheme.Dark:
EnsureDarkResources();
updateTo(_darkResources);
break;
}
}

void updateTo(ResourceDictionary themeDictionary)
{
MergedDictionaries.RemoveIfNotNull(_lightResources);
MergedDictionaries.RemoveIfNotNull(_darkResources);
MergedDictionaries.RemoveIfNotNull(_highContrastResources);
Merge(themeDictionary);
}
}

private ResourceDictionary GetDesignTimeSystemColors()
Expand Down Expand Up @@ -146,7 +177,6 @@ void ISupportInitialize.BeginInit()

void ISupportInitialize.EndInit()
{
EndInit();
IsInitializePending = false;

//for (int i = MergedDictionaries.Count - 1; i >= 0; i--)
Expand All @@ -158,12 +188,16 @@ void ISupportInitialize.EndInit()
// }
//}

ThemeManager.Current.Initialize();

if (DesignMode.DesignModeEnabled)
{
DesignTimeInit();
}
else
{
ThemeManager.Current.Initialize();
}

EndInit();
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/ThemeResources/Dark.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<m:StaticResource x:Key="ContentControlThemeFontFamily" ResourceKey="{x:Static SystemFonts.MessageFontFamilyKey}" />
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets</FontFamily>
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets,Segoe MDL2 Assets</FontFamily>
<CornerRadius x:Key="ControlCornerRadius">2,2,2,2</CornerRadius>
<CornerRadius x:Key="OverlayCornerRadius">4,4,4,4</CornerRadius>

Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/ThemeResources/HighContrast.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<m:StaticResource x:Key="ContentControlThemeFontFamily" ResourceKey="{x:Static SystemFonts.MessageFontFamilyKey}" />
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets</FontFamily>
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets,Segoe MDL2 Assets</FontFamily>
<CornerRadius x:Key="ControlCornerRadius">2,2,2,2</CornerRadius>
<CornerRadius x:Key="OverlayCornerRadius">4,4,4,4</CornerRadius>

Expand Down
2 changes: 1 addition & 1 deletion ModernWpf/ThemeResources/Light.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
xmlns:sys="clr-namespace:System;assembly=mscorlib">

<m:StaticResource x:Key="ContentControlThemeFontFamily" ResourceKey="{x:Static SystemFonts.MessageFontFamilyKey}" />
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets</FontFamily>
<FontFamily x:Key="SymbolThemeFontFamily">/ModernWpf;component/Assets/#Segoe MDL2 Assets,Segoe MDL2 Assets</FontFamily>
<CornerRadius x:Key="ControlCornerRadius">2,2,2,2</CornerRadius>
<CornerRadius x:Key="OverlayCornerRadius">4,4,4,4</CornerRadius>

Expand Down

0 comments on commit 5e604fb

Please sign in to comment.