Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dialogs Resource handling #3142

Merged
merged 1 commit into from Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -115,14 +115,6 @@ private void Initialize([CanBeNull] MetroWindow owningWindow, [CanBeNull] MetroD
this.OwningWindow = owningWindow;
this.DialogSettings = this.ConfigureSettings(settings ?? (owningWindow?.MetroDialogOptions ?? new MetroDialogSettings()));

if (this.DialogSettings != null && !this.DialogSettings.SuppressDefaultResources)
{
this.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
}

this.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
this.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml") });
this.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Themes/Dialogs/BaseMetroDialog.xaml") });
if (this.DialogSettings?.CustomResourceDictionary != null)
{
this.Resources.MergedDictionaries.Add(this.DialogSettings.CustomResourceDictionary);
Expand Down Expand Up @@ -154,51 +146,71 @@ private void ThemeManagerIsThemeChanged(object sender, OnThemeChangedEventArgs e
this.HandleThemeChange();
}

private void HandleThemeChange()
private static object TryGetResource(Accent accent, AppTheme theme, string key)
{
if (this.DialogSettings != null)
if (accent == null || theme == null)
{
var windowTheme = DetectTheme(this);
// nothing to do here, we can't found an app style (make sure all custom themes are added!)
return null;
}

if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) || windowTheme == null)
{
return;
}
object themeResource = theme.Resources[key]; //check the theme first

var theme = windowTheme.Item1;
var windowAccent = windowTheme.Item2;
//next check the accent
var accentResource = accent.Resources[key];
if (accentResource != null)
{
return accentResource;
}

return themeResource;
}

internal void HandleThemeChange()
{
var windowTheme = DetectTheme(this);

if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this) || windowTheme == null)
{
return;
}

Accent windowAccent = windowTheme.Item2;
AppTheme theme = windowTheme.Item1;

if (this.DialogSettings != null)
{
switch (this.DialogSettings.ColorScheme)
{
case MetroDialogColorScheme.Theme:
ThemeManager.ChangeAppStyle(this.Resources, windowAccent, theme);
this.SetValue(BackgroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "WhiteColorBrush"));
this.SetValue(ForegroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "BlackBrush"));
this.SetValue(BackgroundProperty, TryGetResource(windowAccent, theme, "WhiteColorBrush"));
this.SetValue(ForegroundProperty, TryGetResource(windowAccent, theme, "BlackBrush"));
break;
case MetroDialogColorScheme.Inverted:
var inverseTheme = ThemeManager.GetInverseAppTheme(theme);
if (inverseTheme == null)
theme = ThemeManager.GetInverseAppTheme(theme);
if (theme == null)
{
throw new InvalidOperationException("The inverse dialog theme only works if the window theme abides the naming convention. " +
"See ThemeManager.GetInverseAppTheme for more infos");
}

ThemeManager.ChangeAppStyle(this.Resources, windowAccent, inverseTheme);
this.SetValue(BackgroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "BlackColorBrush"));
this.SetValue(ForegroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "WhiteColorBrush"));
ThemeManager.ChangeAppStyle(this.Resources, windowAccent, theme);
this.SetValue(BackgroundProperty, TryGetResource(windowAccent, theme, "WhiteColorBrush"));
this.SetValue(ForegroundProperty, TryGetResource(windowAccent, theme, "BlackBrush"));
break;
case MetroDialogColorScheme.Accented:
ThemeManager.ChangeAppStyle(this.Resources, windowAccent, theme);
this.SetValue(BackgroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "HighlightBrush"));
this.SetValue(ForegroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "IdealForegroundColorBrush"));
this.SetValue(BackgroundProperty, TryGetResource(windowAccent, theme, "HighlightBrush"));
this.SetValue(ForegroundProperty, TryGetResource(windowAccent, theme, "IdealForegroundColorBrush"));
break;
}
}

if (this.ParentDialogWindow != null)
{
this.ParentDialogWindow.SetValue(BackgroundProperty, this.Background);
var glowBrush = ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "AccentColorBrush");
var glowBrush = TryGetResource(windowAccent, theme, "AccentColorBrush");
if (glowBrush != null)
{
this.ParentDialogWindow.SetValue(MetroWindow.GlowBrushProperty, glowBrush);
Expand Down Expand Up @@ -350,7 +362,7 @@ public Task _WaitForCloseAsync()

if (this.DialogSettings.AnimateHide)
{
Storyboard closingStoryboard = this.Resources["DialogCloseStoryboard"] as Storyboard;
Storyboard closingStoryboard = this.TryFindResource("DialogCloseStoryboard") as Storyboard;

if (closingStoryboard == null)
{
Expand Down Expand Up @@ -464,6 +476,7 @@ public MetroDialogSettings()
/// <summary>
/// If set, stops standard resource dictionaries being applied to the dialog.
/// </summary>
[Obsolete("This property will be deleted in the next release.")]
public bool SuppressDefaultResources { get; set; }

/// <summary>
Expand Down
Expand Up @@ -550,16 +550,6 @@ private static Window CreateExternalWindow()
private static Window SetupExternalDialogWindow(BaseMetroDialog dialog)
{
var win = CreateExternalWindow();

try
{
win.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml") });
win.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml") });
win.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml") });
win.SetResourceReference(MetroWindow.GlowBrushProperty, "AccentColorBrush");
}
catch (Exception) { }

win.Width = SystemParameters.PrimaryScreenWidth;
win.MinHeight = SystemParameters.PrimaryScreenHeight / 4.0;
win.SizeToContent = SizeToContent.Height;
Expand All @@ -568,6 +558,8 @@ private static Window SetupExternalDialogWindow(BaseMetroDialog dialog)

win.Content = dialog;

dialog.HandleThemeChange();

EventHandler closedHandler = null;
closedHandler = (sender, args) =>
{
Expand Down
1 change: 1 addition & 0 deletions src/MahApps.Metro/MahApps.Metro/Styles/Controls.xaml
Expand Up @@ -37,6 +37,7 @@

<!-- Theme styles with keys -->
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/WindowButtonCommands.xaml" />
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Themes/Dialogs/BaseMetroDialog.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style x:Key="FloatingMessageContainerStyle" TargetType="{x:Type ContentControl}">
Expand Down
Expand Up @@ -7,10 +7,6 @@
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Buttons.xaml" />
</ResourceDictionary.MergedDictionaries>

<Style BasedOn="{StaticResource SquareButtonStyle}" TargetType="{x:Type Button}">
<Setter Property="controls:ControlsHelper.ContentCharacterCasing" Value="Normal" />
</Style>

<Style x:Key="AccentedDialogSquareButton"
BasedOn="{StaticResource AccentedSquareButtonStyle}"
TargetType="{x:Type ButtonBase}">
Expand Down Expand Up @@ -86,7 +82,12 @@
</ControlTemplate.Triggers>
</ControlTemplate>

<Style TargetType="{x:Type Dialogs:BaseMetroDialog}">
<Style x:Key="MahApps.Metro.Styles.MetroDialog" TargetType="{x:Type Dialogs:BaseMetroDialog}">
<Style.Resources>
<Style BasedOn="{StaticResource SquareButtonStyle}" TargetType="{x:Type Button}">
<Setter Property="controls:ControlsHelper.ContentCharacterCasing" Value="Normal" />
</Style>
</Style.Resources>
<Setter Property="Background" Value="{DynamicResource WhiteColorBrush}" />
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Foreground" Value="{DynamicResource BlackBrush}" />
Expand Down
5 changes: 4 additions & 1 deletion src/MahApps.Metro/MahApps.Metro/Themes/Generic.xaml
@@ -1,6 +1,7 @@
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls">
xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs">

<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colors.xaml" />
Expand Down Expand Up @@ -52,4 +53,6 @@

<Style BasedOn="{StaticResource MahApps.Metro.Styles.MetroHeader}" TargetType="{x:Type Controls:MetroHeader}" />

<Style BasedOn="{StaticResource MahApps.Metro.Styles.MetroDialog}" TargetType="{x:Type Dialogs:BaseMetroDialog}" />

</ResourceDictionary>