Skip to content

Commit

Permalink
Merge pull request #2032 from MahApps/MetroDialogs-fixes
Browse files Browse the repository at this point in the history
MetroDialogs fixes
  • Loading branch information
punker76 committed Jul 10, 2015
2 parents ec4040e + a52943d commit cd57f61
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 52 deletions.
54 changes: 44 additions & 10 deletions MahApps.Metro/Controls/Dialogs/BaseMetroDialog.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -19,7 +20,7 @@ public abstract class BaseMetroDialog : ContentControl
public static readonly DependencyProperty DialogTopProperty = DependencyProperty.Register("DialogTop", typeof(object), typeof(BaseMetroDialog), new PropertyMetadata(null));
public static readonly DependencyProperty DialogBottomProperty = DependencyProperty.Register("DialogBottom", typeof(object), typeof(BaseMetroDialog), new PropertyMetadata(null));

protected MetroDialogSettings DialogSettings { get; private set; }
public MetroDialogSettings DialogSettings { get; private set; }

/// <summary>
/// Gets/sets the dialog's title.
Expand Down Expand Up @@ -83,11 +84,21 @@ protected BaseMetroDialog()

private void Initialize()
{
this.Loaded += (sender, args) => HandleTheme();
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 (DialogSettings != null && DialogSettings.CustomResourceDictionary != null)
{
this.Resources.MergedDictionaries.Add(DialogSettings.CustomResourceDictionary);
}

this.Loaded += (sender, args) => {
OnLoaded();
HandleTheme();
};
ThemeManager.IsThemeChanged += ThemeManager_IsThemeChanged;
this.Unloaded += BaseMetroDialog_Unloaded;

this.Resources.MergedDictionaries.Add(new ResourceDictionary { Source = new Uri("pack://application:,,,/MahApps.Metro;component/Themes/Dialogs/BaseMetroDialog.xaml") });
}

void BaseMetroDialog_Unloaded(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -119,8 +130,8 @@ private void HandleTheme()
{
case MetroDialogColorScheme.Theme:
ThemeManager.ChangeAppStyle(this.Resources, windowAccent, theme);
this.SetValue(BackgroundProperty, ThemeManager.GetResourceFromAppStyle(OwningWindow ?? Application.Current.MainWindow, "WhiteColorBrush"));
this.SetValue(ForegroundProperty, ThemeManager.GetResourceFromAppStyle(OwningWindow ?? Application.Current.MainWindow, "BlackBrush"));
this.SetValue(BackgroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "WhiteColorBrush"));
this.SetValue(ForegroundProperty, ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "BlackBrush"));
break;
case MetroDialogColorScheme.Inverted:
var inverseTheme = ThemeManager.GetInverseAppTheme(theme);
Expand All @@ -131,16 +142,34 @@ private void HandleTheme()
}

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

if (this.ParentDialogWindow != null)
{
this.ParentDialogWindow.SetValue(BackgroundProperty, this.Background);
var glowBrush = ThemeManager.GetResourceFromAppStyle(this.OwningWindow ?? Application.Current.MainWindow, "AccentColorBrush");
if (glowBrush != null)
{
this.ParentDialogWindow.SetValue(MetroWindow.GlowBrushProperty, glowBrush);
}
}
}

/// <summary>
/// This is called in the loaded event.
/// </summary>
protected virtual void OnLoaded()
{
// nothing here
}

private static Tuple<AppTheme, Accent> DetectTheme(BaseMetroDialog dialog)
Expand Down Expand Up @@ -364,6 +393,11 @@ public MetroDialogSettings()
/// Gets/sets the token to cancel the dialog.
/// </summary>
public CancellationToken CancellationToken { get; set; }

/// <summary>
/// Gets/sets a custom resource dictionary which can contains custom styles, brushes or something else.
/// </summary>
public ResourceDictionary CustomResourceDictionary { get; set; }
}

/// <summary>
Expand Down
8 changes: 5 additions & 3 deletions MahApps.Metro/Controls/Dialogs/DialogManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -482,13 +482,15 @@ private static Window SetupExternalDialogWindow(BaseMetroDialog dialog)
WindowStartupLocation = WindowStartupLocation.CenterScreen,
ShowTitleBar = false,
ShowCloseButton = false,
WindowTransitionsEnabled = false,
Background = dialog.Background
WindowTransitionsEnabled = false
};

try
{
win.GlowBrush = win.TryFindResource("AccentColorBrush") as SolidColorBrush;
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) { }

Expand Down
3 changes: 2 additions & 1 deletion MahApps.Metro/Controls/Dialogs/InputDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ internal Task<string> WaitForButtonPressAsync()
return tcs.Task;
}

private void Dialog_Loaded(object sender, RoutedEventArgs e)
protected override void OnLoaded()
{
this.AffirmativeButtonText = this.DialogSettings.AffirmativeButtonText;
this.NegativeButtonText = this.DialogSettings.NegativeButtonText;
Expand All @@ -129,6 +129,7 @@ private void Dialog_Loaded(object sender, RoutedEventArgs e)
case MetroDialogColorScheme.Accented:
this.PART_NegativeButton.Style = this.FindResource("AccentedDialogHighlightedSquareButton") as Style;
PART_TextBox.SetResourceReference(ForegroundProperty, "BlackColorBrush");
PART_TextBox.SetResourceReference(ControlsHelper.FocusBorderBrushProperty, "TextBoxFocusBorderBrush");
break;
}
}
Expand Down
20 changes: 11 additions & 9 deletions MahApps.Metro/Controls/Dialogs/LoginDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ internal LoginDialog(MetroWindow parentWindow, LoginDialogSettings settings)
UsernameWatermark = settings.UsernameWatermark;
PasswordWatermark = settings.PasswordWatermark;
NegativeButtonButtonVisibility = settings.NegativeButtonVisibility;
if (settings.EnablePasswordPreview)
{
object resource = Application.Current.FindResource("Win8MetroPasswordBox");
if (resource != null && resource.GetType() == typeof(Style))
{
PART_TextBox2.Style = (Style)resource;
}
}
}

internal Task<LoginDialogData> WaitForButtonPressAsync()
Expand Down Expand Up @@ -175,8 +167,18 @@ internal Task<LoginDialogData> WaitForButtonPressAsync()
return tcs.Task;
}

private void Dialog_Loaded(object sender, RoutedEventArgs e)
protected override void OnLoaded()
{
var settings = this.DialogSettings as LoginDialogSettings;
if (settings != null && settings.EnablePasswordPreview)
{
var win8MetroPasswordStyle = this.FindResource("Win8MetroPasswordBox") as Style;
if (win8MetroPasswordStyle != null)
{
PART_TextBox2.Style = win8MetroPasswordStyle;
}
}

this.AffirmativeButtonText = this.DialogSettings.AffirmativeButtonText;
this.NegativeButtonText = this.DialogSettings.NegativeButtonText;

Expand Down
2 changes: 1 addition & 1 deletion MahApps.Metro/Controls/Dialogs/MessageDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private static void SetButtonState(MessageDialog md)
}
}

private void Dialog_Loaded(object sender, RoutedEventArgs e)
protected override void OnLoaded()
{
SetButtonState(this);
}
Expand Down
47 changes: 25 additions & 22 deletions MahApps.Metro/Styles/Controls.PasswordBox.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@
<Converters:StringToVisibilityConverter x:Key="StringToVisibilityConverter" />
<Converters:StringToVisibilityConverter x:Key="StringToOppositeVisibilityConverter" OppositeStringValue="True" />

<Grid x:Key="RevealButtonIcon"
x:Shared="False"
Background="{DynamicResource BlackBrush}"
Opacity="0.8"
Width="16"
Height="16">
<Grid.OpacityMask>
<VisualBrush>
<VisualBrush.Visual>
<Canvas Height="25.8461"
Width="42">
<Path Canvas.Left="17"
Canvas.Top="25.0769"
Stretch="Fill"
Data="F1 M 38,33.1538C 40.6765,33.1538 42.8462,35.3235 42.8462,38C 42.8462,40.6765 40.6765,42.8461 38,42.8461C 35.3235,42.8461 33.1539,40.6765 33.1539,38C 33.1539,35.3235 35.3236,33.1538 38,33.1538 Z M 38,25.0769C 49.3077,25.0769 59,33.1538 59,38C 59,42.8461 49.3077,50.9231 38,50.9231C 26.6923,50.9231 17,42.8461 17,38C 17,33.1538 26.6923,25.0769 38,25.0769 Z M 38,29.1154C 33.0932,29.1154 29.1154,33.0932 29.1154,38C 29.1154,42.9068 33.0932,46.8846 38,46.8846C 42.9068,46.8846 46.8846,42.9068 46.8846,38C 46.8846,33.0932 42.9068,29.1154 38,29.1154 Z "
Fill="Black" />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Grid.OpacityMask>
</Grid>

<!--Button Style for Win8MetroPasswordBox-->
<Style x:Key="RevealButtonStyle" TargetType="{x:Type Button}">
<Setter Property="Template">
Expand All @@ -22,7 +44,7 @@
<Border x:Name="PasswordRevealIconEyeBorder" Background="Transparent" Margin="1,1,3,1" BorderThickness="{TemplateBinding BorderThickness}">
<Rectangle Width="20" Height="15" x:Name="IconEye" VerticalAlignment="Center" Margin="3,0" HorizontalAlignment="Center" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}">
<Rectangle.OpacityMask>
<VisualBrush Stretch="Fill" Visual="{DynamicResource RevealButtonIcon}" />
<VisualBrush Stretch="Fill" Visual="{StaticResource RevealButtonIcon}" />
</Rectangle.OpacityMask>
</Rectangle>
</Border>
Expand All @@ -32,34 +54,15 @@
<Setter TargetName="PasswordRevealIconEyeBorder" Property="Background" Value="Gainsboro" />
</DataTrigger>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsPressed}" Value="True">
<Setter TargetName="PasswordRevealIconEyeBorder" Property="Background" Value="Black" />
<Setter TargetName="IconEye" Property="Fill" Value="White" />
<Setter TargetName="PasswordRevealIconEyeBorder" Property="Background" Value="{DynamicResource BlackBrush}" />
<Setter TargetName="IconEye" Property="Fill" Value="{DynamicResource WhiteBrush}" />
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>

<Grid x:Key="RevealButtonIcon"
x:Shared="False"
Background="{DynamicResource BlackBrush}"
Opacity="0.8"
Width="16"
Height="16">
<Grid.OpacityMask>
<VisualBrush>
<VisualBrush.Visual>
<Canvas Height="25.8461"
Width="42">
<Path Canvas.Left="17" Canvas.Top="25.0769" Stretch="Fill" Data="F1 M 38,33.1538C 40.6765,33.1538 42.8462,35.3235 42.8462,38C 42.8462,40.6765 40.6765,42.8461 38,42.8461C 35.3235,42.8461 33.1539,40.6765 33.1539,38C 33.1539,35.3235 35.3236,33.1538 38,33.1538 Z M 38,25.0769C 49.3077,25.0769 59,33.1538 59,38C 59,42.8461 49.3077,50.9231 38,50.9231C 26.6923,50.9231 17,42.8461 17,38C 17,33.1538 26.6923,25.0769 38,25.0769 Z M 38,29.1154C 33.0932,29.1154 29.1154,33.0932 29.1154,38C 29.1154,42.9068 33.0932,46.8846 38,46.8846C 42.9068,46.8846 46.8846,42.9068 46.8846,38C 46.8846,33.0932 42.9068,29.1154 38,29.1154 Z "
Fill="Black" />
</Canvas>
</VisualBrush.Visual>
</VisualBrush>
</Grid.OpacityMask>
</Grid>

<!--Win8MetroPasswordBox Style-->
<Style TargetType="{x:Type PasswordBox}" x:Key="Win8MetroPasswordBox">
<Setter Property="ContextMenu" Value="{DynamicResource TextBoxMetroContextMenu}" />
Expand Down
22 changes: 22 additions & 0 deletions MahApps.Metro/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,28 @@ private static void ApplyResourceDictionary(ResourceDictionary newRd, ResourceDi
oldRd.EndInit();
}

/// <summary>
/// Copies all resource keys from one resource to another.
/// </summary>
/// <param name="fromRD">The source resource dictionary.</param>
/// <param name="toRD">The destination resource dictionary.</param>
/// <exception cref="System.ArgumentNullException">
/// fromRD
/// or
/// toRD
/// </exception>
internal static void CopyResource(ResourceDictionary fromRD, ResourceDictionary toRD)
{
if (fromRD == null) throw new ArgumentNullException("fromRD");
if (toRD == null) throw new ArgumentNullException("toRD");

ApplyResourceDictionary(fromRD, toRD);
foreach (var rd in fromRD.MergedDictionaries)
{
CopyResource(rd, toRD);
}
}

/// <summary>
/// Scans the window resources and returns it's accent and theme.
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions MahApps.Metro/Themes/Dialogs/InputDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs"
xmlns:controls="clr-namespace:MahApps.Metro.Controls"
x:Class="MahApps.Metro.Controls.Dialogs.InputDialog"
Loaded="Dialog_Loaded">
x:Class="MahApps.Metro.Controls.Dialogs.InputDialog">
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
Expand Down
3 changes: 1 addition & 2 deletions MahApps.Metro/Themes/Dialogs/LoginDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs"
xmlns:Behaviors="clr-namespace:MahApps.Metro.Behaviours"
x:Class="MahApps.Metro.Controls.Dialogs.LoginDialog"
Loaded="Dialog_Loaded">
x:Class="MahApps.Metro.Controls.Dialogs.LoginDialog">
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
Expand Down
3 changes: 1 addition & 2 deletions MahApps.Metro/Themes/Dialogs/MessageDialog.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
<Dialogs:BaseMetroDialog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs"
x:Class="MahApps.Metro.Controls.Dialogs.MessageDialog"
Loaded="Dialog_Loaded">
x:Class="MahApps.Metro.Controls.Dialogs.MessageDialog">
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="*" />
Expand Down
1 change: 1 addition & 0 deletions samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ private void LaunchRibbonDemo(object sender, RoutedEventArgs e)
private async void ShowDialogOutside(object sender, RoutedEventArgs e)
{
var dialog = (BaseMetroDialog)this.Resources["CustomDialogTest"];
dialog.DialogSettings.ColorScheme = MetroDialogOptions.ColorScheme;
dialog = dialog.ShowDialogExternally();

await TaskEx.Delay(5000);
Expand Down

0 comments on commit cd57f61

Please sign in to comment.