-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
100 lines (80 loc) · 3.32 KB
/
App.xaml.cs
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
using H.NotifyIcon;
using Microsoft.Toolkit.Uwp.Notifications;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace FruitToolbox
{
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public sealed partial class App: Application
{
#region Properties
public static TaskbarIcon TrayIcon { get; private set; }
private static Settings.View SettingsWindow = null;
#endregion
#region Constructors
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
InitializeComponent();
}
#endregion
#region Event Handlers
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
InitializeFunction();
InitializeTrayIcon();
}
private void InitializeTrayIcon()
{
var OpenSettingsCommand = (XamlUICommand)Resources["OpenSettingsCommand"];
OpenSettingsCommand.ExecuteRequested += OpenSettingsCommand_ExecuteRequested;
var ExitApplicationCommand = (XamlUICommand)Resources["ExitApplicationCommand"];
ExitApplicationCommand.ExecuteRequested += ExitApplicationCommand_ExecuteRequested;
TrayIcon = (TaskbarIcon)Resources["TrayIcon"];
TrayIcon.ForceCreate();
}
private static void InitializeFunction()
{
if(!LanguageSwitcher.Core.Start())
{
Settings.Core.LanguageSwitcherEnabled = false;
new ToastContentBuilder()
.AddText("Unable to enable language switcher")
.AddText("Please make sure you have both keyboard languages and IME languages installed")
.Show();
}
Hotkey.Core.Start();
Hotkey.Core.CapsLockSwitchLanguageEvent += LanguageSwitcher.Core.SwapCategoryNoReturn;
Hotkey.Core.LanguageChangeEvent += LanguageSwitcher.Core.UpdateInputLanguageByKeyboard;
Hotkey.Core.RAltUpEvent += LanguageSwitcher.Core.OnRaltUp;
}
private void OpenSettingsCommand_ExecuteRequested(object _, ExecuteRequestedEventArgs args)
{
if(SettingsWindow == null)
{
SettingsWindow = new();
SettingsWindow.Closed += (e, s) => SettingsWindow = null;
}
SettingsWindow.Activate();
}
private void ExitApplicationCommand_ExecuteRequested(object _, ExecuteRequestedEventArgs args)
{
LanguageSwitcher.Core.Stop();
TrayIcon?.Dispose();
Environment.Exit(0);
}
#endregion
}
}