aboutsummaryrefslogtreecommitdiffstats
path: root/WarframeClock/App.xaml.cs
blob: 8b7025d371c216b3620a63ab045dd192ae5ea568 (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
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Threading;

namespace WarframeClock
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        public static readonly string VersionString = "Warframe Clock Overlay 2019-07-20";
        public static readonly string WebsiteUriString = "https://poepoe.org/warframe/clock/";
        private static readonly bool DEBUG = false;
        public static readonly Uri WebsiteUri = new Uri(WebsiteUriString);

        public static readonly AppData AppData = new AppData();
        private string _debugTracePath;

        private OverlayWindowViewModel _vm;
        private DispatcherTimer _updateTimer;
        private System.Windows.Forms.NotifyIcon _notifyIcon;

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            if (DEBUG)
            {
                _debugTracePath = Path.GetTempFileName();
                Trace.Listeners.Add(new TextWriterTraceListener(File.Create(_debugTracePath)));
            }

            // Prepare ViewModel(?) and a timer updating it
            _vm = new OverlayWindowViewModel();
            _updateTimer = new DispatcherTimer(TimeSpan.FromMilliseconds(100), DispatcherPriority.Background,
                (_, __) => _vm.Refresh(), Dispatcher.CurrentDispatcher);
            _updateTimer.Start();

            // Prepare System Tray icon
            var iconMenu = new System.Windows.Forms.ContextMenu();
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem(VersionString) { Enabled = false });
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Configure",
                (_, __) => new SettingsWindow(_vm).Show()));
            iconMenu.MenuItems.Add(new System.Windows.Forms.MenuItem("Quit",
                (_, __) => Terminate()));

            _notifyIcon = new System.Windows.Forms.NotifyIcon
            {
                ContextMenu = iconMenu,
                Icon = WarframeClock.Properties.Resources.TrayIcon,
                Text = $"Warframe Clock - {VersionString}",
                Visible = true
            };
            _notifyIcon.Click += (_, __) => MainWindow.Show();

            // Last, start the main window
            if (Settings.Standalone)
                new StandaloneOverlayWindow(_vm).Show();
            else
                new OverlayWindow(_vm).Show();
        }

        internal void Terminate()
        {
            _updateTimer.Stop();
            MainWindow?.Close();
            if (DEBUG)
            {
                Trace.Flush();
                MessageBox.Show("Debug log file is located at: " + _debugTracePath);
            }
        }
    }
}