2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="App.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2019-07-15 02:46:11 +12:00
|
|
|
|
namespace SystemTrayMenu
|
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
2022-07-08 01:18:16 +12:00
|
|
|
|
using System.Threading;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System.Windows.Forms;
|
2020-07-08 03:05:19 +12:00
|
|
|
|
using Microsoft.Win32;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.Business;
|
2022-07-08 01:18:16 +12:00
|
|
|
|
using SystemTrayMenu.Helper.Updater;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using SystemTrayMenu.UserInterface;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// App contains the notifyicon, the taskbarform and the menus.
|
|
|
|
|
/// </summary>
|
2020-05-06 12:39:38 +12:00
|
|
|
|
internal class App : IDisposable
|
2019-07-15 02:46:11 +12:00
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private readonly AppNotifyIcon menuNotifyIcon = new();
|
|
|
|
|
private readonly Menus menus = new();
|
2021-11-10 05:41:39 +13:00
|
|
|
|
private readonly TaskbarForm taskbarForm = null;
|
2020-03-16 04:06:18 +13:00
|
|
|
|
|
2020-05-06 12:39:38 +12:00
|
|
|
|
public App()
|
2019-07-15 02:46:11 +12:00
|
|
|
|
{
|
2020-03-17 09:05:52 +13:00
|
|
|
|
AppRestart.BeforeRestarting += Dispose;
|
2022-10-21 04:55:54 +13:00
|
|
|
|
SystemEvents.DisplaySettingsChanged += SystemEvents_DisplaySettingsChanged;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
menus.LoadStarted += menuNotifyIcon.LoadingStart;
|
|
|
|
|
menus.LoadStopped += menuNotifyIcon.LoadingStop;
|
2022-10-21 04:55:54 +13:00
|
|
|
|
menuNotifyIcon.Click += MenuNotifyIcon_Click;
|
2020-03-17 02:45:19 +13:00
|
|
|
|
menuNotifyIcon.OpenLog += Log.OpenLogFile;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
menus.MainPreload();
|
2021-11-10 05:41:39 +13:00
|
|
|
|
|
2022-06-05 04:37:57 +12:00
|
|
|
|
if (Properties.Settings.Default.ShowInTaskbar)
|
|
|
|
|
{
|
|
|
|
|
taskbarForm = new TaskbarForm();
|
2022-10-21 04:55:54 +13:00
|
|
|
|
taskbarForm.FormClosed += TaskbarForm_FormClosed;
|
|
|
|
|
taskbarForm.Deactivate += SetStateNormal;
|
|
|
|
|
taskbarForm.Resize += SetStateNormal;
|
2022-06-05 12:43:18 +12:00
|
|
|
|
taskbarForm.Activated += TasbkarItemActivated;
|
2022-06-05 04:37:57 +12:00
|
|
|
|
}
|
2022-06-05 12:43:18 +12:00
|
|
|
|
|
|
|
|
|
DllImports.NativeMethods.User32ShowInactiveTopmost(taskbarForm);
|
2022-07-08 01:18:16 +12:00
|
|
|
|
|
|
|
|
|
if (Properties.Settings.Default.CheckForUpdates)
|
|
|
|
|
{
|
|
|
|
|
new Thread((obj) => GitHubUpdate.ActivateNewVersionFormOrCheckForUpdates(
|
|
|
|
|
showWhenUpToDate: false))
|
|
|
|
|
.Start();
|
|
|
|
|
}
|
2019-07-15 02:46:11 +12:00
|
|
|
|
}
|
2020-06-29 06:20:19 +12:00
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2022-07-28 07:35:09 +12:00
|
|
|
|
if (taskbarForm?.InvokeRequired == true)
|
|
|
|
|
{
|
2022-08-02 08:22:48 +12:00
|
|
|
|
taskbarForm.Invoke(Dispose);
|
2022-07-28 07:35:09 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-10-21 04:55:54 +13:00
|
|
|
|
AppRestart.BeforeRestarting -= Dispose;
|
|
|
|
|
SystemEvents.DisplaySettingsChanged -= SystemEvents_DisplaySettingsChanged;
|
|
|
|
|
menus.LoadStarted -= menuNotifyIcon.LoadingStart;
|
|
|
|
|
menus.LoadStopped -= menuNotifyIcon.LoadingStop;
|
2022-07-28 07:35:09 +12:00
|
|
|
|
menus.Dispose();
|
2022-10-21 04:55:54 +13:00
|
|
|
|
menuNotifyIcon.Click -= MenuNotifyIcon_Click;
|
|
|
|
|
menuNotifyIcon.OpenLog -= Log.OpenLogFile;
|
2022-07-28 07:35:09 +12:00
|
|
|
|
menuNotifyIcon.Dispose();
|
2022-10-21 04:55:54 +13:00
|
|
|
|
if (taskbarForm != null)
|
|
|
|
|
{
|
|
|
|
|
taskbarForm.FormClosed -= TaskbarForm_FormClosed;
|
|
|
|
|
taskbarForm.Deactivate -= SetStateNormal;
|
|
|
|
|
taskbarForm.Resize -= SetStateNormal;
|
|
|
|
|
taskbarForm.Activated -= TasbkarItemActivated;
|
|
|
|
|
taskbarForm.Dispose();
|
|
|
|
|
}
|
2022-07-28 07:35:09 +12:00
|
|
|
|
}
|
2020-07-08 03:05:19 +12:00
|
|
|
|
}
|
2021-10-26 05:55:34 +13:00
|
|
|
|
|
2022-10-21 04:55:54 +13:00
|
|
|
|
private void SystemEvents_DisplaySettingsChanged(object sender, EventArgs e)
|
2021-10-26 05:55:34 +13:00
|
|
|
|
{
|
2021-12-28 00:22:52 +13:00
|
|
|
|
menus.ReAdjustSizeAndLocation();
|
2021-10-26 05:55:34 +13:00
|
|
|
|
}
|
2022-06-05 12:43:18 +12:00
|
|
|
|
|
2022-10-21 04:55:54 +13:00
|
|
|
|
private void MenuNotifyIcon_Click()
|
|
|
|
|
{
|
|
|
|
|
menus.SwitchOpenClose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TaskbarForm_FormClosed(object sender, FormClosedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
Application.Exit();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-05 12:43:18 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// This ensures that next click on taskbaritem works as activate event/click event.
|
|
|
|
|
/// </summary>
|
2022-10-21 04:55:54 +13:00
|
|
|
|
private void SetStateNormal(object sender, EventArgs e)
|
2022-06-05 12:43:18 +12:00
|
|
|
|
{
|
|
|
|
|
if (Form.ActiveForm == taskbarForm)
|
|
|
|
|
{
|
|
|
|
|
taskbarForm.WindowState = FormWindowState.Normal;
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-10-21 04:55:54 +13:00
|
|
|
|
|
|
|
|
|
private void TasbkarItemActivated(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
SetStateNormal(sender, e);
|
|
|
|
|
taskbarForm.Activate();
|
|
|
|
|
taskbarForm.Focus();
|
|
|
|
|
menus.SwitchOpenCloseByTaskbarItem();
|
|
|
|
|
}
|
2019-07-15 02:46:11 +12:00
|
|
|
|
}
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|