-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.cs
More file actions
140 lines (122 loc) · 4.61 KB
/
App.cs
File metadata and controls
140 lines (122 loc) · 4.61 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
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
139
140
// Code authored by Dean Edis (DeanTheCoder).
// Anyone is free to copy, modify, use, compile, or distribute this software,
// either in source code form or as a compiled binary, for any purpose.
//
// If you modify the code, please retain this copyright header,
// and consider contributing back to the repository or letting us know
// about your modifications. Your contributions are valued!
//
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND.
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using DTC.Core.Commands;
using ZXJetMen.Services;
using ZXJetMen.Settings;
namespace ZXJetMen;
/// <summary>
/// Wires the desktop lifetime to the overlay window and tray icon.
/// </summary>
/// <remarks>
/// The application has no normal main window chrome, so this class owns the shutdown path users need from the tray or menu bar.
/// </remarks>
public sealed class App : Application
{
private readonly AppSettings m_settings = AppSettings.Instance;
private TrayIcons m_trayIcons;
private TrayIcon m_trayIcon;
private NativeMenuItem m_addJetmanItem;
private NativeMenuItem m_removeJetmanItem;
private NativeMenuItem m_jetmanCountItem;
private NativeMenuItem m_miniModeItem;
public override void Initialize() => AvaloniaXamlLoader.Load(this);
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
var overlayWindow = new OverlayWindow(m_settings.JetmanCount, m_settings.MiniMode);
desktop.MainWindow = overlayWindow;
m_trayIcon = CreateTrayIcon(desktop, overlayWindow);
m_trayIcons = [m_trayIcon];
TrayIcon.SetIcons(this, m_trayIcons);
desktop.Exit += (_, _) =>
{
TrayIcon.SetIcons(this, null);
m_trayIcon?.Dispose();
m_settings.Save();
};
}
base.OnFrameworkInitializationCompleted();
}
private TrayIcon CreateTrayIcon(IClassicDesktopStyleApplicationLifetime desktop, OverlayWindow overlayWindow)
{
m_jetmanCountItem = new NativeMenuItem(GetJetmanCountHeader())
{
IsEnabled = false
};
m_addJetmanItem = new NativeMenuItem("Add Jetman")
{
Command = new RelayCommand(_ => SetJetmanCount(overlayWindow, m_settings.JetmanCount + 1))
};
m_removeJetmanItem = new NativeMenuItem("Remove Jetman")
{
Command = new RelayCommand(_ => SetJetmanCount(overlayWindow, m_settings.JetmanCount - 1))
};
m_miniModeItem = new NativeMenuItem("Mini mode")
{
Command = new RelayCommand(_ => SetMiniMode(overlayWindow, !m_settings.MiniMode)),
ToggleType = NativeMenuItemToggleType.CheckBox
};
var menu = new NativeMenu
{
m_jetmanCountItem,
m_addJetmanItem,
m_removeJetmanItem,
new NativeMenuItemSeparator(),
m_miniModeItem,
new NativeMenuItemSeparator(),
new NativeMenuItem("Exit")
{
ToolTip = "Exit",
Command = new RelayCommand(_ => desktop.Shutdown())
}
};
menu.NeedsUpdate += (_, _) => UpdateJetmanMenu();
var trayIcon = new TrayIcon
{
Icon = IconLoader.LoadWindowIcon(),
IsVisible = true,
Menu = menu,
ToolTipText = "ZXJetMen"
};
if (OperatingSystem.IsMacOS())
{
MacOSProperties.SetIsTemplateIcon(trayIcon, false);
}
UpdateJetmanMenu();
return trayIcon;
}
private void SetJetmanCount(OverlayWindow overlayWindow, int jetmanCount)
{
m_settings.JetmanCount = jetmanCount;
m_settings.Save();
overlayWindow.SetJetmanCount(m_settings.JetmanCount);
UpdateJetmanMenu();
}
private void SetMiniMode(OverlayWindow overlayWindow, bool miniMode)
{
m_settings.MiniMode = miniMode;
m_settings.Save();
overlayWindow.SetMiniMode(m_settings.MiniMode);
UpdateJetmanMenu();
}
private void UpdateJetmanMenu()
{
m_jetmanCountItem.Header = GetJetmanCountHeader();
m_addJetmanItem.IsEnabled = m_settings.JetmanCount < AppSettings.MaxJetmanCount;
m_removeJetmanItem.IsEnabled = m_settings.JetmanCount > AppSettings.MinJetmanCount;
m_miniModeItem.IsChecked = m_settings.MiniMode;
}
private string GetJetmanCountHeader() => $"Jetmen: {m_settings.JetmanCount}";
}