Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions DesktopClock.Tests/SystemClockTimerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public void Countdown_ReadsWholeSecondsWhereverTheTickLands(string now, string e
{
var second = TickAt(now);

var result = TimeStringFormatter.Format(second, second.DateTime, TimeZoneInfo.Utc, Target.DateTime, null, "{mm}:{ss}", CultureInfo.InvariantCulture);
var result = TimeStringFormatter.Format(second, TimeZoneInfo.Utc, Target.DateTime, null, "{mm}:{ss}", CultureInfo.InvariantCulture);

Assert.Equal(expected, result);
}
Expand All @@ -32,7 +32,7 @@ public void DefaultCountdown_ReadsWholeSecondsWhereverTheTickLands(string now, s
{
var second = TickAt(now);

var result = TimeStringFormatter.Format(second, second.DateTime, TimeZoneInfo.Utc, Target.DateTime, null, null, CultureInfo.InvariantCulture);
var result = TimeStringFormatter.Format(second, TimeZoneInfo.Utc, Target.DateTime, null, null, CultureInfo.InvariantCulture);

Assert.Equal(expected, result);
}
Expand All @@ -43,7 +43,7 @@ public void Countdown_StandardFormatHasNoFractionOfASecond()
// Issue #106: the "t" format showed values like 00:04:58.9870000.
var second = TickAt("12:00:01.013");

var result = TimeStringFormatter.Format(second, second.DateTime, TimeZoneInfo.Utc, Target.DateTime, null, "t", CultureInfo.InvariantCulture);
var result = TimeStringFormatter.Format(second, TimeZoneInfo.Utc, Target.DateTime, null, "t", CultureInfo.InvariantCulture);

Assert.Equal("00:04:59", result);
}
Expand Down
10 changes: 0 additions & 10 deletions DesktopClock.Tests/TimeStringFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public void Format_UsesFormatWhenNoCountdown()

var result = TimeStringFormatter.Format(
now,
now.DateTime,
timeZone,
default,
"HH:mm",
Expand All @@ -32,7 +31,6 @@ public void Format_UsesFormatProviderForStandardFormats()

var result = TimeStringFormatter.Format(
now,
now.DateTime,
TimeZoneInfo.Utc,
default,
"d",
Expand All @@ -51,7 +49,6 @@ public void Format_UsesTokenizedCountdownFormat()

var result = TimeStringFormatter.Format(
now,
nowDateTime,
TimeZoneInfo.Utc,
countdownTo,
"HH:mm",
Expand All @@ -71,7 +68,6 @@ public void Format_UsesCountdownFormatWhenProvided()

var result = TimeStringFormatter.Format(
now,
nowDateTime,
TimeZoneInfo.Utc,
countdownTo,
"HH:mm",
Expand All @@ -90,7 +86,6 @@ public void Format_UsesRelativePhraseWhenCountdownFormatMissing()

var result = TimeStringFormatter.Format(
now,
nowDateTime,
TimeZoneInfo.Utc,
countdownTo,
"HH:mm",
Expand All @@ -115,7 +110,6 @@ public void Format_HumanizedCountdownIsRelativeToNow(int hoursFromNow, string ex

var result = TimeStringFormatter.Format(
now,
now.DateTime,
TimeZoneInfo.Utc,
countdownTo,
"HH:mm",
Expand All @@ -133,7 +127,6 @@ public void Format_ConvertsUsingSelectedTimeZone()

var result = TimeStringFormatter.Format(
now,
now.DateTime,
timeZone,
default,
"HH:mm",
Expand All @@ -152,7 +145,6 @@ public void Format_WeekTokenUsesSelectedTimeZone()

var result = TimeStringFormatter.Format(
now,
now.DateTime,
timeZone,
default,
"{weekYear}-W{week}",
Expand All @@ -169,7 +161,6 @@ public void Format_InvalidClockFormatShowsBriefErrorMessage()

var result = TimeStringFormatter.Format(
now,
now.DateTime,
TimeZoneInfo.Utc,
default,
"{Q}",
Expand All @@ -187,7 +178,6 @@ public void Format_InvalidCountdownFormatShowsBriefErrorMessage()

var result = TimeStringFormatter.Format(
now,
nowDateTime,
TimeZoneInfo.Utc,
nowDateTime.AddMinutes(30),
"HH:mm",
Expand Down
3 changes: 0 additions & 3 deletions DesktopClock/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,6 @@ public static void SetRunOnStartup(bool runOnStartup)

private static string GetSha256Hash(string text)
{
if (string.IsNullOrEmpty(text))
return string.Empty;

using var sha = new System.Security.Cryptography.SHA256Managed();
var textData = System.Text.Encoding.UTF8.GetBytes(text);
var hash = sha.ComputeHash(textData);
Expand Down
32 changes: 2 additions & 30 deletions DesktopClock/CountdownTargetEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using DesktopClock.Properties;

Expand Down Expand Up @@ -57,37 +56,10 @@ private void BuildPresetButtons()
{
foreach (var (name, getTarget) in Presets)
{
var title = new TextBlock
{
Text = name,
FontWeight = FontWeights.SemiBold,
};

var example = new TextBlock
{
Text = getTarget().ToString(FormatPresets.ShortDateTimePattern(CultureInfo.CurrentCulture)),
FontSize = 12,
TextTrimming = TextTrimming.CharacterEllipsis,
};
example.SetResourceReference(ForegroundProperty, "TextSecondaryBrush");

var content = new StackPanel();
content.Children.Add(title);
content.Children.Add(example);

var button = new Button
{
Content = content,
MaxWidth = 280,
HorizontalContentAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0, 0, 8, 4),
};
AutomationProperties.SetName(button, name);
var example = getTarget().ToString(FormatPresets.ShortDateTimePattern(CultureInfo.CurrentCulture));

// Saved as a wall-clock time like a typed date, since a local one is saved with its UTC offset and would move after a time zone change.
button.Click += (_, _) => Settings.Default.CountdownTo = DateTime.SpecifyKind(getTarget(), DateTimeKind.Unspecified);

PresetsPanel.Children.Add(button);
PresetButton.Add(PresetsPanel, name, example, () => Settings.Default.CountdownTo = DateTime.SpecifyKind(getTarget(), DateTimeKind.Unspecified));
}
}

Expand Down
37 changes: 4 additions & 33 deletions DesktopClock/FormatEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Globalization;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
Expand Down Expand Up @@ -110,37 +109,9 @@ private void BuildPresetButtons()

foreach (var (name, format) in presets)
{
var title = new TextBlock
{
Text = name,
FontWeight = FontWeights.SemiBold,
};

var example = new TextBlock
{
Text = FormatPreview(format),
FontSize = 12,
TextTrimming = TextTrimming.CharacterEllipsis,
};
example.SetResourceReference(ForegroundProperty, "TextSecondaryBrush");
var toolTip = string.IsNullOrEmpty(format) ? Loc.Get("AutomaticTooltip") : format;
var example = PresetButton.Add(PresetsPanel, name, FormatPreview(format), () => SetFormat(format), toolTip);
_presetExamples.Add((example, format));

var content = new StackPanel();
content.Children.Add(title);
content.Children.Add(example);

var button = new Button
{
Content = content,
MaxWidth = 280,
HorizontalContentAlignment = HorizontalAlignment.Left,
Margin = new Thickness(0, 0, 8, 4),
ToolTip = string.IsNullOrEmpty(format) ? Loc.Get("AutomaticTooltip") : format,
};
AutomationProperties.SetName(button, name);
button.Click += (_, _) => SetFormat(format);

PresetsPanel.Children.Add(button);
}
}

Expand Down Expand Up @@ -220,14 +191,14 @@ private string FormatPreview(string format)

if (Mode == FormatEditorMode.Clock)
{
return TimeStringFormatter.Format(now, now.DateTime, timeZone, default, format, string.Empty, culture);
return TimeStringFormatter.Format(now, timeZone, default, format, string.Empty, culture);
}

// Preview against the real countdown target, or a sample one when none is set.
var countdownTo = Settings.Default.CountdownTo != default ?
Settings.Default.CountdownTo :
now.DateTime.AddDays(3).AddHours(4).AddMinutes(30).AddSeconds(10);

return TimeStringFormatter.Format(now, now.DateTime, timeZone, countdownTo, string.Empty, format, culture);
return TimeStringFormatter.Format(now, timeZone, countdownTo, string.Empty, format, culture);
}
}
43 changes: 15 additions & 28 deletions DesktopClock/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public partial class MainWindow : Window
private FullscreenHideManager _fullscreenHideManager;
private DesktopPinner _desktopPinner;
private readonly WheelNotches _wheelNotches = new();
private readonly PropertyChangedEventHandler _settingsPropertyChanged;

/// <summary>
/// The current date and time in the selected time zone, or the formatted countdown text.
Expand All @@ -52,8 +51,7 @@ public MainWindow()

_timeZone = Settings.Default.TimeZoneInfo;

_settingsPropertyChanged = (s, e) => Dispatcher.Invoke(() => Settings_PropertyChanged(s, e));
Settings.Default.PropertyChanged += _settingsPropertyChanged;
Settings.Default.PropertyChanged += Settings_PropertyChanged;

ApplyWindowVisibilitySettings();

Expand Down Expand Up @@ -137,9 +135,6 @@ private void SystemEvents_TimeChanged(object sender, EventArgs e)
/// </summary>
private void ThemeManager_ThemeChanged(object sender, EventArgs e)
{
if (_trayIcon == null)
return;

// A new instance (x:Shared="False") resolves its DynamicResource colors against the current palette when it next opens.
var menu = Resources["MainContextMenu"] as ContextMenu;
menu.DataContext = this;
Expand Down Expand Up @@ -231,15 +226,14 @@ public void Exit()

protected override void OnClosed(EventArgs e)
{
Settings.Default.PropertyChanged -= _settingsPropertyChanged;
Settings.Default.PropertyChanged -= Settings_PropertyChanged;
ThemeManager.ThemeChanged -= ThemeManager_ThemeChanged;
SystemEvents.TimeChanged -= SystemEvents_TimeChanged;
SystemEvents.UserPreferenceChanged -= SystemEvents_UserPreferenceChanged;

_systemClockTimer.SecondChanged -= SystemClockTimer_SecondChanged;
_systemClockTimer.Dispose();

_trayIcon?.Dispose();
_soundPlayer?.Dispose();
_desktopPinner?.Dispose();

Expand All @@ -254,23 +248,20 @@ protected override void OnActivated(EventArgs e)

private void ConfigureTrayIcon()
{
if (_trayIcon == null)
// Construct the tray icon from the XAML resources.
_trayIcon = Resources["TrayIcon"] as TaskbarIcon;
_trayIcon.ContextMenu = Resources["MainContextMenu"] as ContextMenu;
_trayIcon.ContextMenu.DataContext = this;
try
{
// Construct the tray icon from the XAML resources.
_trayIcon = Resources["TrayIcon"] as TaskbarIcon;
_trayIcon.ContextMenu = Resources["MainContextMenu"] as ContextMenu;
_trayIcon.ContextMenu.DataContext = this;
try
{
_trayIcon.ForceCreate(enablesEfficiencyMode: false);
}
catch (InvalidOperationException)
{
// The taskbar isn't there yet, such as early in sign-in, and the icon is added once it starts.
}

_trayIcon.TrayLeftMouseDoubleClick += (_, _) => ShowClock();
_trayIcon.ForceCreate(enablesEfficiencyMode: false);
}
catch (InvalidOperationException)
{
// The taskbar isn't there yet, such as early in sign-in, and the icon is added once it starts.
}

_trayIcon.TrayLeftMouseDoubleClick += (_, _) => ShowClock();
}

/// <summary>
Expand Down Expand Up @@ -460,7 +451,6 @@ private void UpdateTimeString(DateTimeOffset second)
{
CurrentTimeOrCountdownString = TimeStringFormatter.Format(
second,
second.DateTime,
_timeZone,
Settings.Default.CountdownTo,
Settings.Default.Format,
Expand Down Expand Up @@ -563,13 +553,10 @@ private void Window_Closing(object sender, CancelEventArgs e)
Settings.Default.LastDisplay = CurrentTimeOrCountdownString;
Settings.Default.Placement = this.GetPlacement();

// Stop the file watcher before saving.
Settings.Default.Dispose();

if (Settings.CanBeSaved)
Settings.Default.Save();

_trayIcon?.Dispose();
_trayIcon.Dispose();
}

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
Expand Down
Loading
Loading