-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
95 lines (82 loc) · 2.65 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
95 lines (82 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using Microsoft.Win32;
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Threading;
using Screen = System.Windows.Forms.Screen;
namespace DesktopClock
{
public partial class MainWindow : INotifyPropertyChanged
{
private readonly TrayIcon trayIcon;
private readonly Settings settings;
private string displayTime = "44:44";
public string DisplayTime
{
get => displayTime;
private set
{
displayTime = value;
OnPropertyChanged();
}
}
public MainWindow()
{
settings = Settings.Load();
InitializeComponent();
trayIcon = new TrayIcon(settings);
trayIcon.ItemClicked += TrayIcon_ItemClicked;
var timer = new DispatcherTimer(DispatcherPriority.Render)
{
Interval = TimeSpan.FromSeconds(1)
};
timer.Tick += (_, _) => SetClockTime();
timer.Start();
SetClockTime();
SystemEvents.DisplaySettingsChanged += (_, _) => SetClockPosition();
}
private void SetClockPosition()
{
var screen = settings.ScreenIndex < Screen.AllScreens.Length ? Screen.AllScreens[settings.ScreenIndex] : Screen.AllScreens[0];
(Left, Top) = settings.ClockPosition switch
{
ClockPosition.TopLeft => (screen.WorkingArea.Left, screen.WorkingArea.Top),
ClockPosition.TopRight => (screen.WorkingArea.Left + screen.WorkingArea.Width - Width, screen.WorkingArea.Top),
ClockPosition.BottomRight => (screen.WorkingArea.Left + screen.WorkingArea.Width - Width, screen.WorkingArea.Top + screen.WorkingArea.Height - Height),
ClockPosition.BottomLeft => (screen.WorkingArea.Left, screen.WorkingArea.Top + screen.WorkingArea.Height - Height),
_ => throw new ArgumentOutOfRangeException($"Unexpected clock position: '{settings.ClockPosition}'.")
};
}
private void SetClockTime()
{
DisplayTime = DateTime.Now.ToString("HH:mm");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
Win32.MakeTransparent(this);
Win32.MakeNoActivate(this);
SetClockPosition();
}
private void TrayIcon_ItemClicked(object? sender, TrayAction action)
{
switch (action)
{
case TrayAction.ChangeScreen:
case TrayAction.ChangePosition:
SetClockPosition();
break;
case TrayAction.Quit:
Close();
break;
default:
throw new ArgumentOutOfRangeException($"Unexpected tray action : '{action}'.");
}
}
public event PropertyChangedEventHandler? PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string? propertyname = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}
}
}