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

[WIP] Win10 ToggleSwitch #2410

Merged
merged 12 commits into from
Mar 10, 2016
7 changes: 5 additions & 2 deletions MahApps.Metro/Controls/ToggleSwitch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,13 @@ private static void OnIsCheckedChanged(DependencyObject d, DependencyPropertyCha
}
}

public ToggleSwitch()
static ToggleSwitch()
{
DefaultStyleKey = typeof(ToggleSwitch);
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitch), new FrameworkPropertyMetadata(typeof(ToggleSwitch)));
}

public ToggleSwitch()
{
PreviewKeyUp += ToggleSwitch_PreviewKeyUp;
MouseUp += (sender, args) => Keyboard.Focus(this);
}
Expand Down
38 changes: 29 additions & 9 deletions MahApps.Metro/Controls/ToggleSwitchButton.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Shapes;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Data;
Expand All @@ -15,7 +16,7 @@ namespace MahApps.Metro.Controls
[TemplatePart(Name = PART_BackgroundTranslate, Type = typeof(TranslateTransform))]
[TemplatePart(Name = PART_DraggingThumb, Type = typeof(Thumb))]
[TemplatePart(Name = PART_SwitchTrack, Type = typeof(Grid))]
[TemplatePart(Name = PART_ThumbIndicator, Type = typeof(Shape))]
[TemplatePart(Name = PART_ThumbIndicator, Type = typeof(FrameworkElement))]
[TemplatePart(Name = PART_ThumbTranslate, Type = typeof(TranslateTransform))]
public class ToggleSwitchButton : ToggleButton
{
Expand All @@ -28,7 +29,7 @@ public class ToggleSwitchButton : ToggleButton
private TranslateTransform _BackgroundTranslate;
private Thumb _DraggingThumb;
private Grid _SwitchTrack;
private Shape _ThumbIndicator;
private FrameworkElement _ThumbIndicator;
private TranslateTransform _ThumbTranslate;
private readonly PropertyChangeNotifier isCheckedPropertyChangeNotifier;

Expand Down Expand Up @@ -96,9 +97,13 @@ public double ThumbIndicatorWidth
set { SetValue(ThumbIndicatorWidthProperty, value); }
}

static ToggleSwitchButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ToggleSwitchButton), new FrameworkPropertyMetadata(typeof(ToggleSwitchButton)));
}

public ToggleSwitchButton()
{
DefaultStyleKey = typeof(ToggleSwitchButton);
isCheckedPropertyChangeNotifier = new PropertyChangeNotifier(this, ToggleSwitchButton.IsCheckedProperty);
isCheckedPropertyChangeNotifier.ValueChanged += IsCheckedPropertyChangeNotifierValueChanged;
}
Expand All @@ -113,7 +118,7 @@ private void UpdateThumb()
{
if (_ThumbTranslate != null && _SwitchTrack != null && _ThumbIndicator != null)
{
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth) : 0;
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth + _ThumbIndicator.Margin.Left + _ThumbIndicator.Margin.Right) : 0;

_thumbAnimation = new DoubleAnimation();
_thumbAnimation.To = destination;
Expand All @@ -139,7 +144,7 @@ public override void OnApplyTemplate()
_BackgroundTranslate = GetTemplateChild(PART_BackgroundTranslate) as TranslateTransform;
_DraggingThumb = GetTemplateChild(PART_DraggingThumb) as Thumb;
_SwitchTrack = GetTemplateChild(PART_SwitchTrack) as Grid;
_ThumbIndicator = GetTemplateChild(PART_ThumbIndicator) as Shape;
_ThumbIndicator = GetTemplateChild(PART_ThumbIndicator) as FrameworkElement;
_ThumbTranslate = GetTemplateChild(PART_ThumbTranslate) as TranslateTransform;

if (_ThumbIndicator != null && _ThumbTranslate != null && _BackgroundTranslate != null)
Expand All @@ -166,14 +171,28 @@ public override void OnApplyTemplate()
}
}

private void SetIsPressed(bool pressed)
{
// we can't use readonly IsPressedProperty
typeof(ToggleButton).GetMethod("set_IsPressed", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(this, new object[] { pressed });
}

private double? _lastDragPosition;
private bool _isDragging;
void _DraggingThumb_DragStarted(object sender, DragStartedEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
if (!IsPressed)
{
SetIsPressed(true);
}
}
if (_ThumbTranslate != null)
{
_ThumbTranslate.BeginAnimation(TranslateTransform.XProperty, null);
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth) : 0;
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth + _ThumbIndicator.Margin.Left + _ThumbIndicator.Margin.Right) : 0;
_ThumbTranslate.X = destination;
_thumbAnimation = null;
}
Expand All @@ -190,13 +209,14 @@ void _DraggingThumb_DragDelta(object sender, DragDeltaEventArgs e)
if (_SwitchTrack != null && _ThumbIndicator != null)
{
double lastDragPosition = _lastDragPosition.Value;
_ThumbTranslate.X = Math.Min(ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth), Math.Max(0, lastDragPosition + e.HorizontalChange));
_ThumbTranslate.X = Math.Min(ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth + _ThumbIndicator.Margin.Left + _ThumbIndicator.Margin.Right), Math.Max(0, lastDragPosition + e.HorizontalChange));
}
}
}

void _DraggingThumb_DragCompleted(object sender, DragCompletedEventArgs e)
{
SetIsPressed(false);
_lastDragPosition = null;
if (!_isDragging)
{
Expand All @@ -223,7 +243,7 @@ void _SwitchTrack_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (_ThumbTranslate != null && _SwitchTrack != null && _ThumbIndicator != null)
{
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth) : 0;
double destination = IsChecked.GetValueOrDefault() ? ActualWidth - (_SwitchTrack.Margin.Left + _SwitchTrack.Margin.Right + _ThumbIndicator.ActualWidth + _ThumbIndicator.Margin.Left + _ThumbIndicator.Margin.Right) : 0;
_ThumbTranslate.X = destination;
}
}
Expand Down
37 changes: 37 additions & 0 deletions MahApps.Metro/Converters/RectangleHeightToRadiusConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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;
}
}
}
6 changes: 6 additions & 0 deletions MahApps.Metro/MahApps.Metro.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@
<Compile Include="Controls\VisualStates.cs" />
<Compile Include="Controls\WindowCommands.cs" />
<Compile Include="Converters\NullToUnsetValueConverter.cs" />
<Compile Include="Converters\RectangleHeightToRadiusConverter.cs" />
<Compile Include="Converters\ResizeModeMinMaxButtonVisibilityConverter.cs" />
<Compile Include="Converters\StringToVisibilityConverter.cs" />
<Compile Include="Converters\ThicknessBindingConverter.cs" />
Expand Down Expand Up @@ -500,6 +501,11 @@
<Generator>MSBuild:Compile</Generator>
<DependentUpon>Controls.xaml</DependentUpon>
</Page>
<Page Include="Styles\Controls.ToggleSwitch.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<DependentUpon>Controls.xaml</DependentUpon>
</Page>
<Page Include="Styles\Controls.Toolbar.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
6 changes: 6 additions & 0 deletions MahApps.Metro/MahApps.Metro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@
<Compile Include="Converters\BackgroundToForegroundConverter.cs" />
<Compile Include="Converters\ClockDegreeConverter.cs" />
<Compile Include="Converters\NullToUnsetValueConverter.cs" />
<Compile Include="Converters\RectangleHeightToRadiusConverter.cs" />
<Compile Include="Converters\ThicknessBindingConverter.cs" />
<Compile Include="Converters\IsNullConverter.cs" />
<Compile Include="Converters\FontSizeOffsetConverter.cs" />
Expand Down Expand Up @@ -451,6 +452,11 @@
<Generator>MSBuild:Compile</Generator>
<DependentUpon>Controls.xaml</DependentUpon>
</Page>
<Page Include="Styles\Controls.ToggleSwitch.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
<DependentUpon>Controls.xaml</DependentUpon>
</Page>
<Page Include="Styles\Controls.Toolbar.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
Expand Down
11 changes: 10 additions & 1 deletion MahApps.Metro/Styles/Accents/Amber.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="options">

<Color x:Key="HighlightColor">#FFB17807</Color>


<Color x:Key="AccentBaseColor">#FFF0A30A</Color>
<!--80%-->
<Color x:Key="AccentColor">#CCF0A30A</Color>
<!--60%-->
<Color x:Key="AccentColor2">#99F0A30A</Color>
Expand All @@ -13,7 +16,9 @@
<!--20%-->
<Color x:Key="AccentColor4">#33F0A30A</Color>

<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentBaseColorBrush" Color="{StaticResource AccentBaseColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
Expand Down Expand Up @@ -41,4 +46,8 @@
<SolidColorBrush x:Key="MetroDataGrid.FocusBorderBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightBrush" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchBrush.Win10" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchMouseOverBrush.Win10" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorCheckedBrush.Win10" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
</ResourceDictionary>
11 changes: 11 additions & 0 deletions MahApps.Metro/Styles/Accents/BaseDark.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@

<!-- DataGrid brushes -->
<SolidColorBrush x:Key="MetroDataGrid.DisabledHighlightBrush" Color="{StaticResource Gray7}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.PressedBrush.Win10" Color="#FF999999" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffBorderBrush.Win10" Color="#FFCCCCCC" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffMouseOverBorderBrush.Win10" Color="#FFFFFFFF" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffDisabledBorderBrush.Win10" Color="#FF666666" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchDisabledBrush.Win10" Color="#FF444444" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorBrush.Win10" Color="#FFCCCCCC" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorMouseOverBrush.Win10" Color="#FFFFFFFF" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorPressedBrush.Win10" Color="#FFFFFFFF" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorDisabledBrush.Win10" Color="#FF666666" options:Freeze="True" />

</ResourceDictionary>
11 changes: 11 additions & 0 deletions MahApps.Metro/Styles/Accents/BaseLight.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,15 @@

<!-- DataGrid brushes -->
<SolidColorBrush x:Key="MetroDataGrid.DisabledHighlightBrush" Color="{StaticResource Gray7}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.PressedBrush.Win10" Color="#FF666666" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffBorderBrush.Win10" Color="#FF333333" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffMouseOverBorderBrush.Win10" Color="#FF000000" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OffDisabledBorderBrush.Win10" Color="#FF999999" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchDisabledBrush.Win10" Color="#FFCCCCCC" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorBrush.Win10" Color="#FF333333" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorMouseOverBrush.Win10" Color="#FF000000" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorPressedBrush.Win10" Color="#FFFFFFFF" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorDisabledBrush.Win10" Color="#FF999999" options:Freeze="True" />

</ResourceDictionary>
11 changes: 9 additions & 2 deletions MahApps.Metro/Styles/Accents/Blue.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
xmlns:options="http://schemas.microsoft.com/winfx/2006/xaml/presentation/options"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="options">
<!--ACCENT COLORS-->

<Color x:Key="HighlightColor">#FF086F9E</Color>


<Color x:Key="AccentBaseColor">#FF119EDA</Color>
<!--80%-->
<Color x:Key="AccentColor">#CC119EDA</Color>
<!--60%-->
Expand All @@ -15,7 +16,9 @@
<!--20%-->
<Color x:Key="AccentColor4">#33119EDA</Color>

<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentBaseColorBrush" Color="{StaticResource AccentBaseColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
Expand Down Expand Up @@ -43,4 +46,8 @@
<SolidColorBrush x:Key="MetroDataGrid.FocusBorderBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightBrush" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchBrush.Win10" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchMouseOverBrush.Win10" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorCheckedBrush.Win10" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
</ResourceDictionary>
8 changes: 8 additions & 0 deletions MahApps.Metro/Styles/Accents/Brown.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

<Color x:Key="HighlightColor">#FF604220</Color>

<Color x:Key="AccentBaseColor">#FF825A2C</Color>
<!--80%-->
<Color x:Key="AccentColor">#CC825A2C</Color>
<!--60%-->
<Color x:Key="AccentColor2">#99825A2C</Color>
Expand All @@ -14,7 +16,9 @@
<!--20%-->
<Color x:Key="AccentColor4">#33825A2C</Color>

<!-- re-set brushes too -->
<SolidColorBrush x:Key="HighlightBrush" Color="{StaticResource HighlightColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentBaseColorBrush" Color="{StaticResource AccentBaseColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush2" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="AccentColorBrush3" Color="{StaticResource AccentColor3}" options:Freeze="True" />
Expand Down Expand Up @@ -42,4 +46,8 @@
<SolidColorBrush x:Key="MetroDataGrid.FocusBorderBrush" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightBrush" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MetroDataGrid.InactiveSelectionHighlightTextBrush" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />

<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchBrush.Win10" Color="{StaticResource AccentColor}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.OnSwitchMouseOverBrush.Win10" Color="{StaticResource AccentColor2}" options:Freeze="True" />
<SolidColorBrush x:Key="MahApps.Metro.Brushes.ToggleSwitchButton.ThumbIndicatorCheckedBrush.Win10" Color="{StaticResource IdealForegroundColor}" options:Freeze="True" />
</ResourceDictionary>
Loading