Skip to content

Portable Tool for an Ideal Windows Setup

ToggleButton alignment & re animations

Greedekse8d245c9800c
diff --git a/.Source/GTweak/Assets/UserControl/ToggleButton.xaml b/.Source/GTweak/Assets/UserControl/ToggleButton.xaml
index 0a305b54..742d8ffc 100644
--- a/.Source/GTweak/Assets/UserControl/ToggleButton.xaml
+++ b/.Source/GTweak/Assets/UserControl/ToggleButton.xaml
@@ -10,7 +10,7 @@
<StackPanel Orientation="Horizontal">
<Viewbox Margin="0" KeyboardNavigation.IsTabStop="False" MouseLeftButtonUp="Toggle_MouseLeftButtonUp">
<Grid Height="60" VerticalAlignment="Center" Width="Auto" Cursor="Hand">
- <Rectangle x:Name="Back" Cursor="Hand" HorizontalAlignment="Left" Height="56.5" VerticalAlignment="Top" Width="96.5" Margin="2,2,0,0" RadiusY="29" RadiusX="29" KeyboardNavigation.IsTabStop="False" >
+ <Rectangle x:Name="Back" Cursor="Hand" HorizontalAlignment="Left" Height="56" VerticalAlignment="Center" Width="96" RadiusY="28" RadiusX="28" KeyboardNavigation.IsTabStop="False" >
<Rectangle.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{StaticResource Color_ToggleButton_OFF_1}" Offset="1"/>
@@ -18,7 +18,7 @@
</LinearGradientBrush>
</Rectangle.Fill>
</Rectangle>
- <Ellipse x:Name="Dot" Cursor="Hand" HorizontalAlignment="Left" Height="50" VerticalAlignment="Center" Width="50" StrokeThickness="1" KeyboardNavigation.IsTabStop="False" RenderTransformOrigin="1.31,0.51" Margin="6,0,0,0">
+ <Ellipse x:Name="Dot" Cursor="Hand" HorizontalAlignment="Left" Height="50" VerticalAlignment="Center" Width="50" StrokeThickness="1" KeyboardNavigation.IsTabStop="False" Margin="4,0,0,0">
<Ellipse.Fill>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FFE8E8E8" Offset="0.131"/>
@@ -34,7 +34,7 @@
</Ellipse>
</Grid>
</Viewbox>
- <TextBlock x:Name="ToggleText" Text="TextTglButton" Margin="10,2,0,0" HorizontalAlignment="Left" Width="Auto" Style="{StaticResource Text_In}"/>
+ <TextBlock x:Name="ToggleText" Text="TextTglButton" Margin="10,0,0,0" HorizontalAlignment="Left" Width="Auto" Style="{StaticResource Text_In}"/>
</StackPanel>
</Grid>
</UserControl>
diff --git a/.Source/GTweak/Assets/UserControl/ToggleButton.xaml.cs b/.Source/GTweak/Assets/UserControl/ToggleButton.xaml.cs
index a61aac91..6a101911 100644
--- a/.Source/GTweak/Assets/UserControl/ToggleButton.xaml.cs
+++ b/.Source/GTweak/Assets/UserControl/ToggleButton.xaml.cs
@@ -16,17 +16,17 @@ namespace GTweak.Assets.UserControl
/// </summary>
internal event EventHandler ChangedState;
- private readonly Thickness _leftSide = new Thickness(6, 0, 0, 0), _rightSide = new Thickness(45, 0, 0, 0);
private readonly LinearGradientBrush brushOffColor = new LinearGradientBrush(), brushOnColor = new LinearGradientBrush();
private bool _state = false;
+ private readonly Thickness _leftPosition = new Thickness(4, 0, 0, 0), _rightPosition = new Thickness(42, 0, 0, 0);
/// <summary>
/// Changes the state of a Toggle Button with animation
/// </summary>
- internal bool State { get => _state; set { _state = value; AnimationToggle(_state); } }
+ internal bool State { get => _state; set { _state = value; UpdateToggleState(_state); } }
/// <summary>
/// Changes the state of a ToggleButton without animation
/// </summary>
- internal bool StateNA { get => _state; set { _state = value; AnimationToggle(_state, true); } }
+ internal bool StateNA { get => _state; set { _state = value; UpdateToggleState(_state, true); } }
/// <summary>
/// Changes the text for ToggleButton. Accepts: Dynamic and Static Resource, just a string.
/// </summary>
@@ -65,7 +65,7 @@ namespace GTweak.Assets.UserControl
brushOffColor.GradientStops.Add(new GradientStop((Color)FindResource("Color_ToggleButton_OFF_1"), 1.0));
brushOffColor.GradientStops.Add(new GradientStop((Color)FindResource("Color_ToggleButton_OFF_2"), 1.0));
- AnimationToggle(_state, true);
+ UpdateToggleState(_state, true);
}
private void Toggle_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
@@ -77,69 +77,40 @@ namespace GTweak.Assets.UserControl
}
}
- private void AnimationToggle(bool isState, bool isSkipAnimation = false)
+ private void UpdateToggleState(bool isState, bool isSkipAnimation = false)
+ {
+ if (isState && Dot.Margin != _rightPosition)
+ AnimateToggle(_rightPosition, brushOffColor, brushOnColor, "Text", isSkipAnimation);
+ else if (!isState && Dot.Margin != _leftPosition)
+ AnimateToggle(_leftPosition, brushOnColor, brushOffColor, "Text_In", isSkipAnimation);
+ }
+
+ private void AnimateToggle(Thickness targetPosition, Brush fromBrush, Brush toBrush, string textStyle, bool skipAnimation)
{
Dispatcher.Invoke(() =>
{
- ThicknessAnimationUsingKeyFrames thicknessAnimKeyFrames = new ThicknessAnimationUsingKeyFrames();
-
- if (isState && Dot.Margin != _rightSide)
+ ThicknessAnimationUsingKeyFrames marginAnimation = new ThicknessAnimationUsingKeyFrames();
+ marginAnimation.KeyFrames.Add(new EasingThicknessKeyFrame(targetPosition.Equals(_rightPosition) ? _leftPosition : _rightPosition, TimeSpan.Zero)
{
- EasingThicknessKeyFrame fromFrame = new EasingThicknessKeyFrame(_leftSide)
- {
- KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))
- };
-
- EasingThicknessKeyFrame toFrame = new EasingThicknessKeyFrame(_rightSide)
- {
- KeyTime = isSkipAnimation ? KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)) : KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(150))
- };
-
- thicknessAnimKeyFrames.KeyFrames.Add(fromFrame);
- thicknessAnimKeyFrames.KeyFrames.Add(toFrame);
- Timeline.SetDesiredFrameRate(thicknessAnimKeyFrames, 240);
- Dot.BeginAnimation(MarginProperty, thicknessAnimKeyFrames);
-
- BrushAnimation brushanimation = new BrushAnimation()
- {
- From = brushOffColor,
- To = brushOnColor,
- SpeedRatio = 1,
- Duration = isSkipAnimation ? TimeSpan.FromSeconds(0) : TimeSpan.FromMilliseconds(150)
- };
- Timeline.SetDesiredFrameRate(brushanimation, 240);
- Back.BeginAnimation(Shape.FillProperty, brushanimation);
- ToggleText.Style = (Style)FindResource("Text");
- }
-
- else if (!isState && Dot.Margin != _leftSide)
+ EasingFunction = new QuadraticEase()
+ });
+ marginAnimation.KeyFrames.Add(new EasingThicknessKeyFrame(targetPosition, KeyTime.FromTimeSpan(skipAnimation ? TimeSpan.Zero : TimeSpan.FromMilliseconds(150)))
{
- EasingThicknessKeyFrame fromFrame = new EasingThicknessKeyFrame(_rightSide)
- {
- KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))
- };
-
- EasingThicknessKeyFrame toFrame = new EasingThicknessKeyFrame(_leftSide)
- {
- KeyTime = isSkipAnimation ? KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)) : KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(150))
- };
+ EasingFunction = new QuadraticEase()
+ });
+ Timeline.SetDesiredFrameRate(marginAnimation, 240);
+ Dot.BeginAnimation(MarginProperty, marginAnimation);
- thicknessAnimKeyFrames.KeyFrames.Add(fromFrame);
- thicknessAnimKeyFrames.KeyFrames.Add(toFrame);
- Timeline.SetDesiredFrameRate(thicknessAnimKeyFrames, 240);
- Dot.BeginAnimation(MarginProperty, thicknessAnimKeyFrames);
-
- BrushAnimation brushanimation = new BrushAnimation()
- {
- From = brushOnColor,
- To = brushOffColor,
- SpeedRatio = 1,
- Duration = isSkipAnimation ? TimeSpan.FromSeconds(0) : TimeSpan.FromMilliseconds(150)
- };
- Timeline.SetDesiredFrameRate(brushanimation, 240);
- Back.BeginAnimation(Shape.FillProperty, brushanimation);
- ToggleText.Style = (Style)FindResource("Text_In");
- }
+ BrushAnimation brushAnimation = new BrushAnimation
+ {
+ From = fromBrush,
+ To = toBrush,
+ Duration = skipAnimation ? TimeSpan.Zero : TimeSpan.FromMilliseconds(150)
+ };
+ Timeline.SetDesiredFrameRate(brushAnimation, 240);
+ Back.BeginAnimation(Shape.FillProperty, brushAnimation);
+
+ ToggleText.Style = (Style)FindResource(textStyle);
});
}
}
diff --git a/.Source/GTweak/Utilities/Animation/BrushAnimation.cs b/.Source/GTweak/Utilities/Animation/BrushAnimation.cs
index 269ef264..624fe164 100644
--- a/.Source/GTweak/Utilities/Animation/BrushAnimation.cs
+++ b/.Source/GTweak/Utilities/Animation/BrushAnimation.cs
@@ -35,13 +35,17 @@ namespace GTweak.Utilities.Animation
set => SetValue(ToProperty, value);
}
- public override object GetCurrentValue(object defaultOriginValue,
- object defaultDestinationValue, AnimationClock animationClock)
+
+ public override object GetCurrentValue(object defaultOriginValue, object defaultDestinationValue, AnimationClock animationClock)
{
- Brush fromVal = ((Brush)GetValue(FromProperty)).CloneCurrentValue();
- Brush toVal = ((Brush)GetValue(ToProperty)).CloneCurrentValue();
+ QuadraticEase quadraticEase = new QuadraticEase();
+
+ double easedProgress = quadraticEase.Ease(animationClock.CurrentProgress.Value);
+
+ Brush fromVal = ((Brush)GetValue(FromProperty))?.CloneCurrentValue();
+ Brush toVal = ((Brush)GetValue(ToProperty))?.CloneCurrentValue();
- switch (animationClock.CurrentProgress)
+ switch (easedProgress)
{
case 0.0:
return fromVal;
@@ -49,7 +53,7 @@ namespace GTweak.Utilities.Animation
return toVal;
}
- toVal.Opacity = Convert.ToDouble(animationClock.CurrentProgress);
+ toVal.Opacity = easedProgress;
Border borderFrom = new Border();
Border borderTo = new Border();
@@ -64,9 +68,8 @@ namespace GTweak.Utilities.Animation
borderTo.Visibility = Visibility.Visible;
borderFrom.Child = borderTo;
- Brush vb = new VisualBrush(borderFrom);
- return vb;
-
+ return new VisualBrush(borderFrom);
}
+
}
}
diff --git a/.Source/GTweak/Windows/NotificationWindow.xaml.cs b/.Source/GTweak/Windows/NotificationWindow.xaml.cs
index 58354694..39e7d499 100644
--- a/.Source/GTweak/Windows/NotificationWindow.xaml.cs
+++ b/.Source/GTweak/Windows/NotificationWindow.xaml.cs
@@ -70,23 +70,16 @@ namespace GTweak.Windows
DoubleAnimationUsingKeyFrames doubleAnimKeyFrames = new DoubleAnimationUsingKeyFrames();
- EasingDoubleKeyFrame fromFrame = new EasingDoubleKeyFrame(primaryMonitorArea.Right)
+ doubleAnimKeyFrames.KeyFrames.Add(new EasingDoubleKeyFrame(primaryMonitorArea.Right, TimeSpan.Zero)
{
- KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)),
EasingFunction = new QuadraticEase()
- };
-
- EasingDoubleKeyFrame toFrame = new EasingDoubleKeyFrame(primaryMonitorArea.Right - Width - 10)
+ });
+ doubleAnimKeyFrames.KeyFrames.Add(new EasingDoubleKeyFrame(primaryMonitorArea.Right - Width - 10, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)))
{
- KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(200)),
EasingFunction = new QuadraticEase()
- };
-
- doubleAnimKeyFrames.KeyFrames.Add(fromFrame);
- doubleAnimKeyFrames.KeyFrames.Add(toFrame);
+ });
Timeline.SetDesiredFrameRate(doubleAnimKeyFrames, 240);
-
BeginAnimation(Canvas.LeftProperty, doubleAnimKeyFrames);
BeginAnimation(OpacityProperty, FadeAnimation.FadeIn(1, 0.25));
});