-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtilities.cs
171 lines (148 loc) · 6.21 KB
/
Utilities.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
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
171
namespace utilities_cs {
class Program {
public const string Version = "v1.14";
public const BuildMode buildMode = BuildMode.FrameworkDependent;
public static string UtilitiesCsFolder = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"utilities-cs"
);
/// <summary>
/// The main starting point of the program.
/// </summary>
[STAThread]
static void Main(string[] args) {
RegisterCommands.RegisterAllRCommands();
RegisterCommands.RegisterAllFCommands();
#if UTILITIES_DEBUG
//* Debug Mode
string? copied_text = UtilitiesAppContext.Utilities(args);
if (copied_text != null) { Console.WriteLine(copied_text); }
#else
Utils.NotifCheck(
true,
[
"Opened utilities-cs.",
"I am now in your system tray, right click me and press Exit to exit.",
"3"
], "utilities-csOpen"
);
CheckForUpdates();
Application.EnableVisualStyles();
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetCompatibleTextRenderingDefault(false);
var app = new UtilitiesAppContext();
Application.ApplicationExit += delegate {
Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.History.Clear();
app.Exit();
};
Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.OnActivated += toastArgs => {
string key = toastArgs.Argument.Split("=")[0];
string value = toastArgs.Argument.Split("=")[1];
List<KeyValuePair<string, object>>? userInput =
toastArgs.UserInput.Count > 0 ? [.. toastArgs.UserInput]
: null;
switch (key) {
case "update": Update.HandleOnActivatedToast(value); break;
case "spam": Spam.HandleOnActivatedToast(value, userInput); break;
case "remind":
if (value == "dismiss") {
Microsoft.Toolkit.Uwp.Notifications.ToastNotificationManagerCompat.History.Remove("reminder");
} break;
}
};
Application.Run(app);
#endif
}
public enum BuildMode {
FrameworkDependent,
SelfContained
}
async static void CheckForUpdates() {
await Task.Run(
() => {
Thread.Sleep(4000);
Update.Check(alertEvenIfUpdateIsNotRequired: false);
}
);
}
}
public partial class UtilitiesAppContext : ApplicationContext {
/// <summary>
/// The main method that is called when a command is executed.
/// </summary>
/// <param name="args">The arguments for the command being run.</param>
/// <returns>Returns null or a string of the output of the command.</returns>
public static string? Utilities(string[] args) {
string? output = Command.ExecuteCommand(args);
if (output != null) { return output; } else { return null; }
}
private readonly NotifyIcon trayIcon;
/// <summary>
/// Current settings of the program.
/// </summary>
public static SettingsJSON CurrentSettings = SettingsModification.GetSettings();
/// <summary>
/// Constructor for the application context.
/// </summary>
public UtilitiesAppContext() {
//* making keyboard hook for ctrl + f8
HookManager.AddHook(
"utilities-cs-ctrlF8",
[ModifierKeys.Control],
Keys.F8,
async () => {
int hotkeyDelay = CurrentSettings.CopyingHotkeyDelay;
bool pressEscape = CurrentSettings.PressEscape;
SendKeys.Send("^a");
Thread.Sleep(hotkeyDelay);
SendKeys.Send("^c");
Thread.Sleep(hotkeyDelay);
if (pressEscape) { SendKeys.Send("{ESC}"); }
string[] args = Clipboard.GetText().Split(" ");
await Task.Run(() => Utilities(args));
},
onFail: () => {
Utils.NotifCheck(
true,
[
"Exception",
@"App is already running, multiple instances are not supported.",
"4"
], "utilitiesHotkeyError"
); Exit();
}
);
//* creating tray icon
trayIcon = new NotifyIcon() { Text = "utilities-cs" };
var menu = new ContextMenuStrip();
menu.Items.Add(
"Wiki...", null, delegate {
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(
"cmd", $"/c start https://github.com/prokenz101/utilities-cs/wiki/Utilities-Wiki"
) { CreateNoWindow = true });
}
);
menu.Items.Add(
"Settings...", null, delegate {
Utils.NotifCheck(
true,
["Opening settings.json...", "Opening settings.json on your default editor.", "3"],
"utiliitesOpeningSettings"
); SettingsModification.OpenSettingsJSON();
}
);
menu.Items.Add("Exit", null, delegate { Exit(); });
trayIcon.Icon = Icon.ExtractAssociatedIcon(System.Diagnostics.Process.GetCurrentProcess().MainModule!.FileName!);
trayIcon.ContextMenuStrip = menu;
trayIcon.Visible = true;
}
/// <summary>
/// Exits the application.
/// </summary>
public void Exit() {
trayIcon.Visible = false;
HookManager.UnregisterAllHooks();
Application.Exit();
}
}
}