From 923e3af75d4ee97fba46498ef7569c47d0c1b7aa Mon Sep 17 00:00:00 2001 From: punker76 Date: Wed, 26 Feb 2020 22:15:33 +0100 Subject: [PATCH 01/12] (GH-3753) Simplify ToggleSwitch samples --- .../ExampleViews/ButtonsExample.xaml | 80 +++---------------- 1 file changed, 13 insertions(+), 67 deletions(-) diff --git a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml index 3d3c502caa..f0801a421a 100644 --- a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml +++ b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml @@ -273,78 +273,21 @@ + + - - - - - - - - - - - @@ -395,7 +338,7 @@ @@ -458,7 +401,7 @@ - - - - - \ No newline at end of file diff --git a/src/MahApps.Metro/Styles/Controls.xaml b/src/MahApps.Metro/Styles/Controls.xaml index 2431fd7fde..ac833a316f 100644 --- a/src/MahApps.Metro/Styles/Controls.xaml +++ b/src/MahApps.Metro/Styles/Controls.xaml @@ -34,7 +34,6 @@ - diff --git a/src/MahApps.Metro/Styles/Fonts.xaml b/src/MahApps.Metro/Styles/Fonts.xaml index 11921fbf51..4e560fd63d 100644 --- a/src/MahApps.Metro/Styles/Fonts.xaml +++ b/src/MahApps.Metro/Styles/Fonts.xaml @@ -7,10 +7,8 @@ Segoe UI Light, Lucida Sans Unicode, Verdana Segoe UI Light, Lucida Sans Unicode, Verdana Segoe UI, Lucida Sans Unicode, Verdana - Segoe UI Semibold, Segoe UI, Lucida Sans Unicode, Verdana - Segoe UI Semibold, Segoe UI, Lucida Sans Unicode, Verdana - Segoe UI, Lucida Sans Unicode, Verdana - Segoe UI, Lucida Sans Unicode, Verdana + Segoe UI, Lucida Sans Unicode, Verdana + Segoe UI, Lucida Sans Unicode, Verdana 40 29.333 @@ -33,9 +31,7 @@ 20 - 14.667 - 16 - 14 - 14 + 14 + 14 \ No newline at end of file diff --git a/src/MahApps.Metro/Themes/Generic.xaml b/src/MahApps.Metro/Themes/Generic.xaml index 67e7a0bbca..6200504da1 100644 --- a/src/MahApps.Metro/Themes/Generic.xaml +++ b/src/MahApps.Metro/Themes/Generic.xaml @@ -41,7 +41,6 @@ - - + From adc9644248e3915a76a2739c22bfbb8a001692d7 Mon Sep 17 00:00:00 2001 From: punker76 Date: Fri, 28 Feb 2020 22:44:42 +0100 Subject: [PATCH 03/12] (GH-3753) Fix focusing control on MouseLeftButtonDown event --- src/MahApps.Metro/Controls/ToggleSwitch.cs | 9 ++++----- src/MahApps.Metro/Themes/ToggleSwitch.xaml | 3 +++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/MahApps.Metro/Controls/ToggleSwitch.cs b/src/MahApps.Metro/Controls/ToggleSwitch.cs index a91c4d6e4c..532fd31ccf 100644 --- a/src/MahApps.Metro/Controls/ToggleSwitch.cs +++ b/src/MahApps.Metro/Controls/ToggleSwitch.cs @@ -344,6 +344,7 @@ protected virtual void OnToggled() static ToggleSwitch() { DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitch), new FrameworkPropertyMetadata(typeof(ToggleSwitch))); + EventManager.RegisterClassHandler(typeof(ToggleSwitch), MouseLeftButtonDownEvent, new MouseButtonEventHandler(OnMouseLeftButtonDown), true); } public ToggleSwitch() @@ -389,13 +390,11 @@ private void OnIsEnabledChanged(object sender, DependencyPropertyChangedEventArg this.UpdateVisualStates(true); } - protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) + private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e) { - base.OnMouseLeftButtonDown(e); - - if (!this.IsKeyboardFocusWithin) + if (sender is ToggleSwitch toggle && !toggle.IsKeyboardFocusWithin) { - e.Handled = this.Focus() || e.Handled; + e.Handled = toggle.Focus() || e.Handled; } } diff --git a/src/MahApps.Metro/Themes/ToggleSwitch.xaml b/src/MahApps.Metro/Themes/ToggleSwitch.xaml index 3d4a11ed37..0a422cb9a0 100644 --- a/src/MahApps.Metro/Themes/ToggleSwitch.xaml +++ b/src/MahApps.Metro/Themes/ToggleSwitch.xaml @@ -164,6 +164,9 @@ + + + Date: Fri, 28 Feb 2020 23:31:33 +0100 Subject: [PATCH 04/12] (GH-3753) Codacy/PR Quality Review: toggle only when necessary --- src/MahApps.Metro/Controls/ToggleSwitch.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/MahApps.Metro/Controls/ToggleSwitch.cs b/src/MahApps.Metro/Controls/ToggleSwitch.cs index 532fd31ccf..bfae98ff8e 100644 --- a/src/MahApps.Metro/Controls/ToggleSwitch.cs +++ b/src/MahApps.Metro/Controls/ToggleSwitch.cs @@ -131,9 +131,12 @@ public Thickness ContentPadding private static void OnIsOnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var toggleSwitch = (ToggleSwitch)d; - toggleSwitch.OnToggled(); - toggleSwitch.UpdateVisualStates(true); + if (e.NewValue != e.OldValue) + { + var toggleSwitch = (ToggleSwitch)d; + toggleSwitch.OnToggled(); + toggleSwitch.UpdateVisualStates(true); + } } /// From d0d9f96ec70c3cf60f1ace4828c61aa530943238 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sat, 29 Feb 2020 18:46:16 +0100 Subject: [PATCH 05/12] (GH-3753) Add missing on/off animations --- src/MahApps.Metro/Themes/ToggleSwitch.xaml | 91 ++++++++++++++++++++++ 1 file changed, 91 insertions(+) diff --git a/src/MahApps.Metro/Themes/ToggleSwitch.xaml b/src/MahApps.Metro/Themes/ToggleSwitch.xaml index 0a422cb9a0..b3d22141d2 100644 --- a/src/MahApps.Metro/Themes/ToggleSwitch.xaml +++ b/src/MahApps.Metro/Themes/ToggleSwitch.xaml @@ -12,6 +12,8 @@ 2 154 + 0:0:0.500 + From 214ce60c48b5676c9d608c416dca2341e1b9aae8 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sat, 29 Feb 2020 19:33:37 +0100 Subject: [PATCH 07/12] XamlStyler --- .../MahApps.Metro.Demo/ExampleViews/TextExamples.xaml | 3 +-- src/MahApps.Metro/Styles/Controls.Buttons.xaml | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TextExamples.xaml b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TextExamples.xaml index 78b71570b7..e346e22600 100644 --- a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TextExamples.xaml +++ b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/TextExamples.xaml @@ -594,8 +594,7 @@ - + - + @@ -430,8 +430,8 @@ - + @@ -596,13 +596,13 @@ - + - + @@ -767,8 +767,8 @@ - + From 178a28867fefe5b54a8fbf51651b0b67b2a64a4d Mon Sep 17 00:00:00 2001 From: punker76 Date: Sat, 29 Feb 2020 20:06:26 +0100 Subject: [PATCH 08/12] (GH-3753) Add ToggleSwitchAutomationPeer --- src/MahApps.Metro/Controls/ToggleSwitch.cs | 11 ++++++ .../Controls/ToggleSwitchAutomationPeer.cs | 38 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 src/MahApps.Metro/Controls/ToggleSwitchAutomationPeer.cs diff --git a/src/MahApps.Metro/Controls/ToggleSwitch.cs b/src/MahApps.Metro/Controls/ToggleSwitch.cs index bfae98ff8e..3605136b75 100644 --- a/src/MahApps.Metro/Controls/ToggleSwitch.cs +++ b/src/MahApps.Metro/Controls/ToggleSwitch.cs @@ -1,6 +1,7 @@ using System; using System.ComponentModel; using System.Windows; +using System.Windows.Automation.Peers; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; @@ -559,5 +560,15 @@ private void Toggle() { this.SetCurrentValue(IsOnProperty, !this.IsOn); } + + protected override AutomationPeer OnCreateAutomationPeer() + { + return new ToggleSwitchAutomationPeer(this); + } + + internal void AutomationPeerToggle() + { + this.Toggle(); + } } } \ No newline at end of file diff --git a/src/MahApps.Metro/Controls/ToggleSwitchAutomationPeer.cs b/src/MahApps.Metro/Controls/ToggleSwitchAutomationPeer.cs new file mode 100644 index 0000000000..1ffd5a2298 --- /dev/null +++ b/src/MahApps.Metro/Controls/ToggleSwitchAutomationPeer.cs @@ -0,0 +1,38 @@ +using System.Windows.Automation; +using System.Windows.Automation.Peers; +using JetBrains.Annotations; + +namespace MahApps.Metro.Controls +{ + public class ToggleSwitchAutomationPeer : FrameworkElementAutomationPeer + { + /// Initializes a new instance of the class. + /// The associated with this . + public ToggleSwitchAutomationPeer([NotNull] ToggleSwitch owner) + : base(owner) + { + } + + /// + protected override string GetClassNameCore() + { + return "ToggleSwitch"; + } + + /// + protected override AutomationControlType GetAutomationControlTypeCore() + { + return AutomationControlType.Button; + } + + public ToggleState ToggleState => ((ToggleSwitch)this.Owner).IsOn ? ToggleState.On : ToggleState.Off; + + public void Toggle() + { + if (this.IsEnabled()) + { + ((ToggleSwitch)this.Owner).AutomationPeerToggle(); + } + } + } +} \ No newline at end of file From bd82726ba897a85d7a9c4d0819b726e318bed7a9 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sat, 29 Feb 2020 22:43:31 +0100 Subject: [PATCH 09/12] (GH-3753) Use HeaderedControlHelper.HeaderFontFamily attached property --- src/MahApps.Metro/Controls/ToggleSwitch.cs | 19 ------------------- src/MahApps.Metro/Themes/ToggleSwitch.xaml | 5 +++-- 2 files changed, 3 insertions(+), 21 deletions(-) diff --git a/src/MahApps.Metro/Controls/ToggleSwitch.cs b/src/MahApps.Metro/Controls/ToggleSwitch.cs index 3605136b75..1c48f0e251 100644 --- a/src/MahApps.Metro/Controls/ToggleSwitch.cs +++ b/src/MahApps.Metro/Controls/ToggleSwitch.cs @@ -62,25 +62,6 @@ public class ToggleSwitch : HeaderedContentControl private Thumb SwitchThumb { get; set; } - /// Identifies the dependency property. - public static readonly DependencyProperty HeaderFontFamilyProperty - = DependencyProperty.Register(nameof(HeaderFontFamily), - typeof(FontFamily), - typeof(ToggleSwitch), - new PropertyMetadata(SystemFonts.MessageFontFamily)); - - /// - /// Gets or set the font family of the header content presenter. - /// - [Bindable(true)] - [Localizability(LocalizationCategory.Font)] - [Category(AppName.MahApps)] - public FontFamily HeaderFontFamily - { - get => (FontFamily)this.GetValue(HeaderFontFamilyProperty); - set => this.SetValue(HeaderFontFamilyProperty, value); - } - /// Identifies the dependency property. public static readonly DependencyProperty ContentDirectionProperty = DependencyProperty.Register(nameof(ContentDirection), diff --git a/src/MahApps.Metro/Themes/ToggleSwitch.xaml b/src/MahApps.Metro/Themes/ToggleSwitch.xaml index 044728fcd2..ca82bf42e1 100644 --- a/src/MahApps.Metro/Themes/ToggleSwitch.xaml +++ b/src/MahApps.Metro/Themes/ToggleSwitch.xaml @@ -20,7 +20,7 @@ - + @@ -47,9 +47,10 @@ ContentTemplateSelector="{TemplateBinding HeaderTemplateSelector}" IsHitTestVisible="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" - TextElement.FontFamily="{TemplateBinding HeaderFontFamily}" + TextElement.FontFamily="{TemplateBinding mah:HeaderedControlHelper.HeaderFontFamily}" TextElement.FontSize="{TemplateBinding mah:HeaderedControlHelper.HeaderFontSize}" TextElement.FontWeight="{TemplateBinding mah:HeaderedControlHelper.HeaderFontWeight}" + TextElement.FontStretch="{TemplateBinding mah:HeaderedControlHelper.HeaderFontStretch}" TextElement.Foreground="{DynamicResource MahApps.Brushes.ToggleSwitch.HeaderForeground}" Visibility="Collapsed" /> From cd67bc36fb9d2900b350fe368b71cad0adc8b24a Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 1 Mar 2020 13:11:13 +0100 Subject: [PATCH 10/12] (GH-3753) Add a Command property which will be executed when the IsOnProperty changes --- src/MahApps.Metro/Controls/ToggleSwitch.cs | 116 ++++++++++++++++++++- 1 file changed, 115 insertions(+), 1 deletion(-) diff --git a/src/MahApps.Metro/Controls/ToggleSwitch.cs b/src/MahApps.Metro/Controls/ToggleSwitch.cs index 1c48f0e251..c98c9dbe0e 100644 --- a/src/MahApps.Metro/Controls/ToggleSwitch.cs +++ b/src/MahApps.Metro/Controls/ToggleSwitch.cs @@ -32,7 +32,7 @@ namespace MahApps.Metro.Controls [TemplateVisualState(GroupName = ToggleStatesGroup, Name = DraggingState)] [TemplateVisualState(GroupName = ToggleStatesGroup, Name = OffState)] [TemplateVisualState(GroupName = ToggleStatesGroup, Name = OnState)] - public class ToggleSwitch : HeaderedContentControl + public class ToggleSwitch : HeaderedContentControl, ICommandSource { private const string ContentStatesGroup = "ContentStates"; private const string OffContentState = "OffContent"; @@ -304,6 +304,7 @@ public string OffContentStringFormat typeof(ToggleSwitch), null); + /// Identifies the dependency property. public static readonly DependencyProperty IsPressedProperty = IsPressedPropertyKey.DependencyProperty; [Browsable(false)] @@ -315,6 +316,57 @@ public bool IsPressed protected set => this.SetValue(IsPressedPropertyKey, value); } + /// Identifies the dependency property. + public static readonly DependencyProperty CommandProperty + = DependencyProperty.Register(nameof(Command), + typeof(ICommand), + typeof(ToggleSwitch), + new PropertyMetadata(null, OnCommandChanged)); + + /// + /// Gets or sets a command which will be executed when the changes. + /// + public ICommand Command + { + get => (ICommand)this.GetValue(CommandProperty); + set => this.SetValue(CommandProperty, value); + } + + /// Identifies the dependency property. + public static readonly DependencyProperty CommandParameterProperty + = DependencyProperty.Register(nameof(CommandParameter), + typeof(object), + typeof(ToggleSwitch), + new PropertyMetadata(null)); + + /// + /// Gets or sets the command parameter which will be passed by the Command. + /// + public object CommandParameter + { + get => this.GetValue(CommandParameterProperty); + set => this.SetValue(CommandParameterProperty, value); + } + + /// Identifies the dependency property. + public static readonly DependencyProperty CommandTargetProperty + = DependencyProperty.Register(nameof(CommandTarget), + typeof(IInputElement), + typeof(ToggleSwitch), + new PropertyMetadata(null)); + + /// + /// Gets or sets the element on which to raise the specified Command. + /// + /// + /// Element on which to raise the Command. + /// + public IInputElement CommandTarget + { + get => (IInputElement)this.GetValue(CommandTargetProperty); + set => this.SetValue(CommandTargetProperty, value); + } + /// /// Occurs when "On"/"Off" state changes for this ToggleSwitch. /// @@ -540,6 +592,68 @@ private void UpdateVisualStates(bool useTransitions) private void Toggle() { this.SetCurrentValue(IsOnProperty, !this.IsOn); + + CommandHelpers.ExecuteCommandSource(this); + } + + private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((ToggleSwitch)d).OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue); + } + + private void OnCommandChanged(ICommand oldCommand, ICommand newCommand) + { + if (oldCommand != null) + { + this.UnhookCommand(oldCommand); + } + + if (newCommand != null) + { + this.HookCommand(newCommand); + } + } + + private void UnhookCommand(ICommand command) + { + CanExecuteChangedEventManager.RemoveHandler(command, this.OnCanExecuteChanged); + this.UpdateCanExecute(); + } + + private void HookCommand(ICommand command) + { + CanExecuteChangedEventManager.AddHandler(command, this.OnCanExecuteChanged); + this.UpdateCanExecute(); + } + + private void OnCanExecuteChanged(object sender, EventArgs e) + { + this.UpdateCanExecute(); + } + + private void UpdateCanExecute() + { + this.CanExecute = this.Command == null || CommandHelpers.CanExecuteCommandSource(this); + } + + /// + protected override bool IsEnabledCore => base.IsEnabledCore && this.CanExecute; + + private bool canExecute = true; + + private bool CanExecute + { + get => this.canExecute; + set + { + if (value == this.canExecute) + { + return; + } + + this.canExecute = value; + this.CoerceValue(IsEnabledProperty); + } } protected override AutomationPeer OnCreateAutomationPeer() From 44791a3416258f6dfdbc7e0bb8ca2feb3582c942 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 1 Mar 2020 21:35:44 +0100 Subject: [PATCH 11/12] (GH-3753) Remove RectangleHeightToRadiusConverter --- .../RectangleHeightToRadiusConverter.cs | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 src/MahApps.Metro/Converters/RectangleHeightToRadiusConverter.cs diff --git a/src/MahApps.Metro/Converters/RectangleHeightToRadiusConverter.cs b/src/MahApps.Metro/Converters/RectangleHeightToRadiusConverter.cs deleted file mode 100644 index f9cdd8aa1e..0000000000 --- a/src/MahApps.Metro/Converters/RectangleHeightToRadiusConverter.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Globalization; -using System.Windows; -using System.Windows.Data; -using System.Windows.Markup; - -namespace MahApps.Metro.Converters -{ - [ValueConversion(typeof(double?), typeof(double))] - [MarkupExtensionReturnType(typeof(RectangleHeightToRadiusConverter))] - public class RectangleHeightToRadiusConverter : MarkupConverter - { - private static RectangleHeightToRadiusConverter _instance; - - // Explicit static constructor to tell C# compiler - // not to mark type as beforefieldinit - static RectangleHeightToRadiusConverter() - { - } - - public override object ProvideValue(IServiceProvider serviceProvider) - { - return _instance ?? (_instance = new RectangleHeightToRadiusConverter()); - } - - protected override object Convert(object value, Type targetType, object parameter, CultureInfo culture) - { - var height = value as double?; - return height.GetValueOrDefault(0) / 2d; - } - - protected override object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) - { - return DependencyProperty.UnsetValue; - } - } -} \ No newline at end of file From b4df0cc714a2f6bdc58c0736315f2b09f7b53288 Mon Sep 17 00:00:00 2001 From: punker76 Date: Sun, 1 Mar 2020 22:19:31 +0100 Subject: [PATCH 12/12] (GH-3753) Minor sample update --- .../MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml index 561347204f..3479b88e89 100644 --- a/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml +++ b/src/MahApps.Metro.Samples/MahApps.Metro.Demo/ExampleViews/ButtonsExample.xaml @@ -280,8 +280,8 @@ HorizontalAlignment="Center">