Skip to content

Portable Tool for an Ideal Windows Setup

add auto-scroll for long descriptions

Greedeks4bf22a24ec1a
diff --git a/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml b/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml
index 281af86a..024a8210 100644
--- a/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml
+++ b/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml
@@ -1,14 +1,15 @@
-<UserControl x:Class="GTweak.Assets.UserControl.DescriptionBlock"
+<UserControl x:Class="GTweak.Assets.UserControl.DescriptionBlock"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:GTweak.Assets.UserControl"
- mc:Ignorable="d" IsTabStop="False" KeyboardNavigation.TabNavigation="None"
+ mc:Ignorable="d" IsTabStop="False" KeyboardNavigation.TabNavigation="None" SnapsToDevicePixels="True" UseLayoutRounding="True"
d:DesignHeight="450" d:DesignWidth="800" Loaded="DescriptionBlock_Loaded">
-
<Grid Margin="10">
- <TextBlock x:Name="FunctionDescription" Margin="6" Style="{StaticResource Description}" Text="{Binding DefaultText, RelativeSource={RelativeSource AncestorType=local:DescriptionBlock}}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
+ <ScrollViewer x:Name="Scroller" Margin="6" VerticalScrollBarVisibility="Hidden" HorizontalScrollBarVisibility="Disabled" ClipToBounds="True">
+ <TextBlock x:Name="FunctionDescription" Style="{StaticResource Description}" Text="{Binding DefaultText, RelativeSource={RelativeSource AncestorType=local:DescriptionBlock}}" TextWrapping="Wrap" TextTrimming="CharacterEllipsis" MaxWidth="{Binding ActualWidth, RelativeSource={RelativeSource AncestorType=UserControl}}"/>
+ </ScrollViewer>
<Border Background="{StaticResource Brush_Separator}" Height="30" Width="1" HorizontalAlignment="Left" VerticalAlignment="Top" CornerRadius="0,0,15,15" />
<Border Background="{StaticResource Brush_Separator}" Height="1" Width="30" HorizontalAlignment="Left" VerticalAlignment="Top" CornerRadius="0,15,15,0" />
diff --git a/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml.cs b/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml.cs
index ef1199c2..ce913cd1 100644
--- a/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml.cs
+++ b/.Source/GTweak/Assets/UserControl/DescriptionBlock.xaml.cs
@@ -1,38 +1,106 @@
using System;
+using System.Threading;
+using System.Threading.Tasks;
using System.Windows;
+using System.Windows.Controls;
using GTweak.Utilities.Animation;
+using GTweak.Utilities.Controls;
namespace GTweak.Assets.UserControl
{
public partial class DescriptionBlock
{
+ private CancellationTokenSource _scrollCts;
+
internal string DefaultText
{
get { return (string)GetValue(DefaultTextProperty); }
set { SetValue(DefaultTextProperty, value); }
}
+ public static readonly DependencyProperty DefaultTextProperty =
+ DependencyProperty.Register("DefaultText", typeof(string), typeof(DescriptionBlock), new UIPropertyMetadata(string.Empty));
+
internal string Text
{
- get { return FunctionDescription.Text; }
+ get => FunctionDescription.Text;
set
{
+ _scrollCts?.Cancel();
+ Scroller.BeginAnimation(ScrollViewerBehavior.VerticalOffsetProperty, null);
+ Scroller.ScrollToVerticalOffset(0);
+ Scroller.UpdateLayout();
+
TypewriterAnimation.Create(value, FunctionDescription,
- value.Length <= 50 ? TimeSpan.FromMilliseconds(200) : value.Length <= 200 ? TimeSpan.FromMilliseconds(400) : TimeSpan.FromMilliseconds(550));
+ value.Length <= 50 ? TimeSpan.FromMilliseconds(200) : value.Length <= 200 ? TimeSpan.FromMilliseconds(400) : TimeSpan.FromMilliseconds(550));
+
+ _scrollCts = new CancellationTokenSource();
+ _ = StartAutoScrollAsync(value, _scrollCts.Token);
}
}
- public static readonly DependencyProperty DefaultTextProperty =
- DependencyProperty.Register("DefaultText", typeof(string), typeof(DescriptionBlock), new UIPropertyMetadata(string.Empty));
-
-
public DescriptionBlock()
{
InitializeComponent();
- App.LanguageChanged += delegate { TypewriterAnimation.Create(DefaultText, FunctionDescription, TimeSpan.FromMilliseconds(0)); };
+
+ Loaded += delegate { TypewriterAnimation.Create(DefaultText, FunctionDescription, TimeSpan.FromMilliseconds(300)); };
+ Unloaded += delegate { _scrollCts?.Cancel(); };
+
+ App.LanguageChanged += delegate
+ {
+ _scrollCts?.Cancel();
+ TypewriterAnimation.Create(DefaultText, FunctionDescription, TimeSpan.Zero);
+ };
}
private void DescriptionBlock_Loaded(object sender, RoutedEventArgs e) => TypewriterAnimation.Create(DefaultText, FunctionDescription, TimeSpan.FromMilliseconds(300));
- }
-}
+ private async Task StartAutoScrollAsync(string text, CancellationToken token)
+ {
+ await Task.Delay(2000, token);
+
+ if (text == DefaultText || token.IsCancellationRequested)
+ {
+ return;
+ }
+
+ await Dispatcher.InvokeAsync(() =>
+ {
+ FunctionDescription.UpdateLayout();
+ Scroller.UpdateLayout();
+ Scroller.ScrollToVerticalOffset(0);
+ });
+
+ double maxOffset = Scroller.ScrollableHeight;
+ if (maxOffset <= 0)
+ {
+ return;
+ }
+
+ double durationSeconds = Math.Max(2.0, maxOffset / 20);
+
+ await Dispatcher.InvokeAsync(() => { Scroller.BeginAnimation(ScrollViewerBehavior.VerticalOffsetProperty, FactoryAnimation.CreateIn(0, maxOffset, durationSeconds, useCubicEase: true)); });
+
+ try { await Task.Delay(TimeSpan.FromSeconds(durationSeconds), token); }
+ catch (Exception ex) { ErrorLogging.LogDebug(ex); }
+ }
+
+ private static class ScrollViewerBehavior
+ {
+ internal static readonly DependencyProperty VerticalOffsetProperty =
+ DependencyProperty.RegisterAttached("VerticalOffset", typeof(double), typeof(ScrollViewerBehavior), new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnVerticalOffsetChanged));
+
+ internal static double GetVerticalOffset(ScrollViewer viewer) => (double)viewer.GetValue(VerticalOffsetProperty);
+
+ internal static void SetVerticalOffset(ScrollViewer viewer, double value) => viewer.SetValue(VerticalOffsetProperty, value);
+
+ private static void OnVerticalOffsetChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+ {
+ if (d is ScrollViewer viewer)
+ {
+ viewer.ScrollToVerticalOffset((double)e.NewValue);
+ }
+ }
+ }
+ }
+}
\ No newline at end of file