SystemTrayMenu/Business/App.xaml.cs
Peter Kirmeier de483c874c Refactored resource file management..
Remove use of resx file
Resource files are marked as "Resource" instead
Resources are loaded via resource dictionaries (either from code behind or XAML directly through binding)
Reduce amount of required image conversions

Also fix overlay of 50 percent transparency (is now rendered half transparent correct instead of adding "white fog")
Remove obsolete image as rendering is done directly in code

Also fix rendering of link indicator at correct image position

Also remove setting Icons of all Windows (as default is application icon anyway)
2023-08-12 02:33:11 +02:00

98 lines
2.9 KiB
C#

// <copyright file="App.xaml.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
namespace SystemTrayMenu
{
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Threading;
using SystemTrayMenu.Business;
using SystemTrayMenu.Helpers;
using SystemTrayMenu.Helpers.Updater;
using SystemTrayMenu.Properties;
using SystemTrayMenu.Utilities;
/// <summary>
/// App contains the notifyicon, the taskbarform and the menus.
/// </summary>
public partial class App : Application, IDisposable
{
private Menus? menus;
private JoystickHelper? joystickHelper;
private bool isDisposed;
public App()
{
InitializeComponent();
AppRestart.BeforeRestarting += Dispose;
Activated += (_, _) => IsActiveApp = true;
Deactivated += (_, _) => IsActiveApp = false;
Startup += (_, _) =>
{
menus = new();
menus.Startup();
if (Settings.Default.SupportGamepad)
{
joystickHelper = new();
joystickHelper.KeyPressed += menus.KeyPressed;
}
if (Settings.Default.CheckForUpdates)
{
_ = Dispatcher.InvokeAsync(
() => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(showWhenUpToDate: false),
DispatcherPriority.ApplicationIdle);
}
};
}
internal static bool IsActiveApp { get; private set; }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
/// <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);
}
}
protected virtual void Dispose(bool disposing)
{
if (!isDisposed)
{
if (joystickHelper != null)
{
if (menus != null)
{
joystickHelper.KeyPressed -= menus.KeyPressed;
}
joystickHelper.Dispose();
}
menus?.Dispose();
isDisposed = true;
}
}
}
}