Explorar o código

Scale size the same in menu and mouse scroll

We can now consistently go smaller or bigger than before while maintaining the same accuracy regardless of the size.

More options for larger or smaller screen scales, such as a 4K monitor at 100%.
Daniel Chalmers hai 1 ano
pai
achega
c18bfb968e

+ 26 - 0
DesktopClock/LogScaleConverter.cs

@@ -0,0 +1,26 @@
+using System;
+using System.Globalization;
+using System.Windows.Data;
+using System.Windows.Markup;
+
+namespace DesktopClock;
+
+/// <summary>
+/// https://stackoverflow.com/a/13850984.
+/// </summary>
+public class LogScaleConverter : MarkupExtension, IValueConverter
+{
+    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
+    {
+        double x = (int)value;
+        return Math.Log(x);
+    }
+
+    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
+    {
+        var x = (double)value;
+        return Math.Exp(x);
+    }
+
+    public override object ProvideValue(IServiceProvider serviceProvider) => this;
+}

+ 12 - 8
DesktopClock/MainWindow.xaml

@@ -59,11 +59,9 @@
 						<TextBlock Text="Size: " />
 
 						<Slider Width="80"
-						        AutoToolTipPlacement="TopLeft"
-						        IsSnapToTickEnabled="True"
-						        Maximum="240"
-						        Minimum="16"
-						        Value="{Binding Height, Source={x:Static p:Settings.Default}, Mode=TwoWay}" />
+						        Maximum="{x:Static local:MainWindow.MaxSizeLog}"
+						        Minimum="{x:Static local:MainWindow.MinSizeLog}"
+						        Value="{Binding Height, Converter={local:LogScaleConverter}, Source={x:Static p:Settings.Default}, Mode=TwoWay}" />
 					</StackPanel>
 				</MenuItem.Header>
 			</MenuItem>
@@ -112,11 +110,17 @@
 
 			<Separator />
 
-			<MenuItem Command="{Binding NewClockCommand}" Header="_New clock..." IsEnabled="{x:Static p:Settings.CanBeSaved}" />
+			<MenuItem Command="{Binding NewClockCommand}"
+			          Header="_New clock..."
+			          IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
-			<MenuItem Command="{Binding CountdownWizardCommand}" Header="_Countdown to..." IsEnabled="{x:Static p:Settings.CanBeSaved}" />
+			<MenuItem Command="{Binding CountdownWizardCommand}"
+			          Header="_Countdown to..."
+			          IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
-			<MenuItem Command="{Binding OpenSettingsCommand}" Header="Advanced _settings" IsEnabled="{x:Static p:Settings.CanBeSaved}" />
+			<MenuItem Command="{Binding OpenSettingsCommand}"
+			          Header="Advanced _settings"
+			          IsEnabled="{x:Static p:Settings.CanBeSaved}" />
 
 			<MenuItem Command="{Binding CheckForUpdatesCommand}" Header="Check for _updates" />
 

+ 11 - 3
DesktopClock/MainWindow.xaml.cs

@@ -37,6 +37,9 @@ public partial class MainWindow : Window
     [ObservableProperty]
     private string _currentTimeOrCountdownString;
 
+    public static readonly double MaxSizeLog = 7;
+    public static readonly double MinSizeLog = 2.5;
+
     public MainWindow()
     {
         InitializeComponent();
@@ -330,11 +333,16 @@ public partial class MainWindow : Window
     {
         if (Keyboard.Modifiers == ModifierKeys.Control)
         {
-            // Scale size based on scroll amount, with one notch on a default PC mouse being a change of 15%.
+            // Amount of scroll that occurred and whether it was positive or negative.
             var steps = e.Delta / (double)Mouse.MouseWheelDeltaForOneLine;
-            var change = Settings.Default.Height * steps * 0.15;
 
-            Settings.Default.Height = (int)Math.Min(Math.Max(Settings.Default.Height + change, 16), 160);
+            // Convert the height, adjust it, then convert back in the same way as the slider.
+            var newHeightLog = Math.Log(Settings.Default.Height) + (steps * 0.15);
+            var newHeightLogClamped = Math.Min(Math.Max(newHeightLog, MinSizeLog), MaxSizeLog);
+            var exp = Math.Exp(newHeightLogClamped);
+
+            // Apply the new height as an integer to make it easier for the user.
+            Settings.Default.Height = (int)exp;
         }
     }