-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.axaml.cs
More file actions
170 lines (143 loc) · 4.56 KB
/
Copy pathApp.axaml.cs
File metadata and controls
170 lines (143 loc) · 4.56 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using ClaudeUsageMonitor.Services;
namespace ClaudeUsageMonitor;
public class App : Application
{
public AppSettings Settings { get; } = AppSettings.Load();
public override void Initialize() => AvaloniaXamlLoader.Load(this);
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
if (!UsageService.CredentialsExist())
{
var icons = TrayIcon.GetIcons(this);
if (icons != null)
{
foreach (var icon in icons)
{
icon.IsVisible = false;
}
}
desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
desktop.MainWindow = new NoCredentialsWindow();
desktop.MainWindow.Show();
base.OnFrameworkInitializationCompleted();
return;
}
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
desktop.MainWindow = new MainWindow();
desktop.MainWindow.Show();
}
var menu = FindMinimizeMenuItem();
menu?.IsChecked = Settings.MinimizeToTray;
base.OnFrameworkInitializationCompleted();
}
private NativeMenuItem? FindMinimizeMenuItem()
{
var icons = TrayIcon.GetIcons(this);
if (icons == null || icons.Count == 0 || icons[0].Menu == null)
{
return null;
}
foreach (var item in icons[0].Menu!.Items)
{
if (item is NativeMenuItem { Header: "Minimize to tray" } m)
{
return m;
}
}
return null;
}
private void OnTrayIconClicked(object? sender, EventArgs e) => ShowMainWindow();
private void OnMinimizeToTrayMenuClicked(object? sender, EventArgs e)
{
if (sender is not NativeMenuItem item)
{
return;
}
Settings.MinimizeToTray = !Settings.MinimizeToTray;
item.IsChecked = Settings.MinimizeToTray;
Settings.Save();
}
private void OnQuitMenuClicked(object? sender, EventArgs e)
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.Shutdown();
}
}
private void OnChartsMenuClicked(object? sender, EventArgs e) => ShowChartsWindow();
private async void OnSettingsMenuClicked(object? sender, EventArgs e) =>
await ShowSettingsWindow();
public void ShowChartsWindow()
{
if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
return;
}
var database = desktop.MainWindow is MainWindow main
? main.Database
: new UsageDatabase();
var window = new ChartsWindow(database);
window.Show();
}
public async System.Threading.Tasks.Task ShowSettingsWindow()
{
if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
return;
}
var dialog = new SettingsWindow(Settings);
var owner = desktop.MainWindow;
if (owner != null && owner.IsVisible)
{
await dialog.ShowDialog(owner);
}
else
{
dialog.Show();
var tcs = new System.Threading.Tasks.TaskCompletionSource();
dialog.Closed += (_, _) => tcs.TrySetResult();
await tcs.Task;
}
if (!dialog.Saved)
{
return;
}
var minimizeMenu = FindMinimizeMenuItem();
if (minimizeMenu != null)
{
minimizeMenu.IsChecked = Settings.MinimizeToTray;
}
if (desktop.MainWindow is MainWindow main)
{
main.OnSettingsChanged();
}
}
private void ShowMainWindow()
{
if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
return;
}
Dispatcher.UIThread.Post(() =>
{
var window = desktop.MainWindow ??= new MainWindow();
if (!window.IsVisible)
{
window.Show();
}
if (window.WindowState == WindowState.Minimized)
{
window.WindowState = WindowState.Normal;
}
window.Activate();
});
}
}