2022-10-23 11:02:31 +13:00
|
|
|
|
// <copyright file="App.xaml.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu
|
|
|
|
|
{
|
|
|
|
|
using System;
|
2023-08-12 12:33:11 +12:00
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.IO;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Threading;
|
|
|
|
|
using SystemTrayMenu.Business;
|
2023-05-19 07:25:03 +12:00
|
|
|
|
using SystemTrayMenu.Helpers;
|
2023-04-17 00:17:33 +12:00
|
|
|
|
using SystemTrayMenu.Helpers.Updater;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
using SystemTrayMenu.Properties;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// App contains the notifyicon, the taskbarform and the menus.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class App : Application, IDisposable
|
|
|
|
|
{
|
2023-08-12 12:33:11 +12:00
|
|
|
|
private Menus? menus;
|
2023-05-19 07:25:03 +12:00
|
|
|
|
private JoystickHelper? joystickHelper;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
private bool isDisposed;
|
|
|
|
|
|
|
|
|
|
public App()
|
|
|
|
|
{
|
2023-08-12 12:33:11 +12:00
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
AppRestart.BeforeRestarting += Dispose;
|
|
|
|
|
|
2023-05-16 08:34:17 +12:00
|
|
|
|
Activated += (_, _) => IsActiveApp = true;
|
|
|
|
|
Deactivated += (_, _) => IsActiveApp = false;
|
|
|
|
|
Startup += (_, _) =>
|
2022-10-23 11:02:31 +13:00
|
|
|
|
{
|
2023-08-12 12:33:11 +12:00
|
|
|
|
menus = new();
|
2023-05-19 07:25:03 +12:00
|
|
|
|
menus.Startup();
|
|
|
|
|
|
|
|
|
|
if (Settings.Default.SupportGamepad)
|
2023-05-16 08:34:17 +12:00
|
|
|
|
{
|
2023-05-19 07:25:03 +12:00
|
|
|
|
joystickHelper = new();
|
|
|
|
|
joystickHelper.KeyPressed += menus.KeyPressed;
|
2023-05-16 08:34:17 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Settings.Default.CheckForUpdates)
|
|
|
|
|
{
|
|
|
|
|
_ = Dispatcher.InvokeAsync(
|
|
|
|
|
() => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(showWhenUpToDate: false),
|
|
|
|
|
DispatcherPriority.ApplicationIdle);
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-10-23 11:02:31 +13:00
|
|
|
|
}
|
|
|
|
|
|
2023-05-16 08:34:17 +12:00
|
|
|
|
internal static bool IsActiveApp { get; private set; }
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-12 12:33:11 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Loads an Icon from the application's Resources.
|
|
|
|
|
/// Note: Only allowed to be called after App's Startup event.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="resourceName">Absolute file path from root directory.</param>
|
|
|
|
|
/// <returns>New Icon object.</returns>
|
|
|
|
|
internal static Icon LoadIconFromResource(string resourceName)
|
|
|
|
|
{
|
|
|
|
|
using (Stream stream = GetResourceStream(new("pack://application:,,,/" + resourceName, UriKind.Absolute)).Stream)
|
|
|
|
|
{
|
|
|
|
|
return new(stream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-23 11:02:31 +13:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (!isDisposed)
|
|
|
|
|
{
|
2023-05-19 07:25:03 +12:00
|
|
|
|
if (joystickHelper != null)
|
|
|
|
|
{
|
2023-08-12 12:33:11 +12:00
|
|
|
|
if (menus != null)
|
|
|
|
|
{
|
|
|
|
|
joystickHelper.KeyPressed -= menus.KeyPressed;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 07:25:03 +12:00
|
|
|
|
joystickHelper.Dispose();
|
|
|
|
|
}
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
2023-08-12 12:33:11 +12:00
|
|
|
|
menus?.Dispose();
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
isDisposed = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|