Browse Source

Use user friendly display names in timezone picker

Closes #62
Daniel Chalmers 9 tháng trước cách đây
mục cha
commit
1a3d0b6213

+ 2 - 2
DesktopClock/MainWindow.xaml.cs

@@ -47,7 +47,7 @@ public partial class MainWindow : Window
         InitializeComponent();
         DataContext = this;
 
-        _timeZone = Settings.Default.GetTimeZoneInfo();
+        _timeZone = Settings.Default.TimeZoneInfo;
 
         Settings.Default.PropertyChanged += (s, e) => Dispatcher.Invoke(() => Settings_PropertyChanged(s, e));
 
@@ -218,7 +218,7 @@ public partial class MainWindow : Window
         switch (e.PropertyName)
         {
             case nameof(Settings.Default.TimeZone):
-                _timeZone = Settings.Default.GetTimeZoneInfo();
+                _timeZone = Settings.Default.TimeZoneInfo;
                 UpdateTimeString();
                 break;
 

+ 23 - 16
DesktopClock/Properties/Settings.cs

@@ -247,6 +247,29 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
         }
     }
 
+    /// <summary>
+    /// Proxy for binding to the a timezone.
+    /// </summary>
+    [JsonIgnore]
+    public TimeZoneInfo TimeZoneInfo
+    {
+        get
+        {
+            try
+            {
+                return TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
+            }
+            catch (TimeZoneNotFoundException)
+            {
+                return TimeZoneInfo.Local;
+            }
+        }
+        set
+        {
+            TimeZone = value.Id;
+        }
+    }
+
     #endregion "Properties"
 
     /// <summary>
@@ -349,22 +372,6 @@ public sealed class Settings : INotifyPropertyChanged, IDisposable
         Height = (int)exp;
     }
 
-    /// <summary>
-    /// Gets the time zone selected in settings, or local by default.
-    /// </summary>
-    public TimeZoneInfo GetTimeZoneInfo()
-    {
-
-        try
-        {
-            return TimeZoneInfo.FindSystemTimeZoneById(TimeZone);
-        }
-        catch (TimeZoneNotFoundException)
-        {
-            return TimeZoneInfo.Local;
-        }
-    }
-
     public void Dispose()
     {
         // We don't dispose of the watcher anymore because it would actually hang indefinitely if you had multiple instances of the same clock open.

+ 2 - 1
DesktopClock/SettingsWindow.xaml

@@ -89,7 +89,8 @@
 
                 <TextBlock Text="Time Zone:" />
                 <ComboBox ItemsSource="{Binding TimeZones}"
-                          SelectedItem="{Binding Settings.TimeZone, Mode=TwoWay}" />
+                          SelectedItem="{Binding Settings.TimeZoneInfo, Mode=TwoWay}"
+                          DisplayMemberPath="DisplayName" />
                 <TextBlock Text="A different time zone to be used."
                            FontStyle="Italic"
                            FontSize="10"

+ 2 - 2
DesktopClock/SettingsWindow.xaml.cs

@@ -82,7 +82,7 @@ public partial class SettingsWindowViewModel : ObservableObject
         Settings = settings;
         FontFamilies = GetAllSystemFonts().Distinct().OrderBy(f => f).ToList();
         FontStyles = ["Normal", "Italic", "Oblique"];
-        TimeZones = TimeZoneInfo.GetSystemTimeZones().Select(tz => tz.Id).ToList();
+        TimeZones = TimeZoneInfo.GetSystemTimeZones();
     }
 
     /// <summary>
@@ -98,7 +98,7 @@ public partial class SettingsWindowViewModel : ObservableObject
     /// <summary>
     /// All available time zones reported by the system.
     /// </summary>
-    public IList<string> TimeZones { get; }
+    public IList<TimeZoneInfo> TimeZones { get; }
 
     /// <summary>
     /// Sets the format string in settings.