aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/OverlayWindow.xaml.cs
blob: cd0f46ba2e727e65e0e3c7df79305ff56874977f (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Input;
using System.Windows.Navigation;
using System.Windows.Threading;

namespace WarframeClock
{
    /// <summary>
    /// Interaction logic for OverlayWindow.xaml
    /// </summary>
    public partial class OverlayWindow : OverlayWindowBase
    {
        private readonly OverlayWindowViewModel _vm;
        private readonly System.Windows.Forms.NotifyIcon _notifyIcon;
        private readonly DispatcherTimer _updateTimer;

        public OverlayWindow()
        {
            InitializeComponent();
            DataContext = _vm = new OverlayWindowViewModel();

            var iconMenu = new System.Windows.Forms.ContextMenu();
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem(App.VersionString) { Enabled = false });
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Configure",
                (_, __) => _vm.SettingsMode = true));
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Quit",
                (_, __) => ((App)Application.Current).Terminate()));

            _notifyIcon = new System.Windows.Forms.NotifyIcon
            {
                ContextMenu = iconMenu,
                Icon = Properties.Resources.TrayIcon,
                Visible = true
            };

            _updateTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Background,
                UpdateTimerTick, Dispatcher.CurrentDispatcher);
            _updateTimer.Start();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);
            _notifyIcon.Dispose();
            _updateTimer.Stop();
        }

        private void UpdateTimerTick(object sender, EventArgs e)
        {
            _vm.RefreshOverlayText();
            _notifyIcon.Text = $"Warframe Clock - {_vm.OverlayText}";

            AdjustOverlay();
        }

        private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
        {
            Process.Start(e.Uri.AbsoluteUri);
        }

        private void DoneSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            Settings.Save();
            _vm.SettingsMode = false;
        }

        private Point _dragOriginalPosition;
        private bool _dragStarted;
        private Point _dragOffset;

        private void OverlayLabelOverlay_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _dragStarted = true;
            _dragOriginalPosition = OverlayLabelOverlay.TranslatePoint(new Point(0, 0), this);
            _dragOffset = e.GetPosition(this);
            ((UIElement)sender).CaptureMouse();
        }

        private void OverlayLabelOverlay_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (_dragStarted)
            {
                ((UIElement)sender).ReleaseMouseCapture();
                _dragStarted = false;
            }
        }

        private void OverlayLabelOverlay_MouseMove(object sender, MouseEventArgs e)
        {
            if (_dragStarted)
            {
                var p = e.GetPosition(this);
                var newX = _dragOriginalPosition.X + p.X - _dragOffset.X;
                if (newX <= ActualWidth / 2)
                {
                    Settings.DockToX = Settings.AlignmentX.Left;
                    Settings.MarginX = newX;
                }
                else
                {
                    Settings.DockToX = Settings.AlignmentX.Right;
                    Settings.MarginX = ActualWidth - newX - OverlayLabelOverlay.ActualWidth;
                }
                var newY = _dragOriginalPosition.Y + p.Y - _dragOffset.Y;
                if (newY <= ActualHeight / 2)
                {
                    Settings.DockToY = Settings.AlignmentY.Top;
                    Settings.MarginY = newY;
                }
                else
                {
                    Settings.DockToY = Settings.AlignmentY.Bottom;
                    Settings.MarginY = ActualHeight - newY - OverlayLabelOverlay.ActualHeight;
                }

                _vm.RefreshPosition();
            }
        }

        private void FontSettingsButton_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FontDialog
            {
                Font = new System.Drawing.Font(
                    new System.Drawing.FontFamily(Settings.FontFamilyName),
                    (float) (Settings.FontSize * 72.0 / 96.0))
            };
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Settings.FontFamilyName = dialog.Font.FontFamily.Name;
                Settings.FontSize = dialog.Font.SizeInPoints / 72.0 * 96.0;
                _vm.RefreshFont();
            }
        }
    }
}