2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="AppNotifyIcon.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-03-28 12:42:32 +13:00
|
|
|
|
namespace SystemTrayMenu.UserInterface
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.Helper;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
using Timer = System.Windows.Forms.Timer;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
internal class AppNotifyIcon : IDisposable
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2021-11-17 12:13:46 +13:00
|
|
|
|
private readonly Timer load = new();
|
|
|
|
|
private readonly NotifyIcon notifyIcon = new();
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private bool threadsLoading;
|
2021-10-11 03:44:06 +13:00
|
|
|
|
private int rotationAngle;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
2020-07-07 07:15:45 +12:00
|
|
|
|
public AppNotifyIcon()
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2021-10-26 08:45:49 +13:00
|
|
|
|
notifyIcon.Icon = Resources.StaticResources.LoadingIcon;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
load.Tick += Load_Tick;
|
2021-10-11 03:44:06 +13:00
|
|
|
|
load.Interval = 15;
|
2020-07-07 09:37:55 +12:00
|
|
|
|
notifyIcon.Text = Translator.GetText("SystemTrayMenu");
|
2019-07-05 05:04:14 +12:00
|
|
|
|
notifyIcon.Visible = true;
|
2021-11-18 11:46:43 +13:00
|
|
|
|
notifyIcon.Icon = Config.GetAppIcon();
|
2021-06-07 00:46:48 +12:00
|
|
|
|
|
2021-11-17 12:13:46 +13:00
|
|
|
|
AppContextMenu contextMenus = new();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
|
|
|
|
|
contextMenus.ClickedOpenLog += ClickedOpenLog;
|
|
|
|
|
void ClickedOpenLog()
|
|
|
|
|
{
|
2020-06-29 06:56:03 +12:00
|
|
|
|
OpenLog?.Invoke();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contextMenus.ClickedRestart += ClickedRestart;
|
|
|
|
|
void ClickedRestart()
|
|
|
|
|
{
|
2020-06-29 06:56:03 +12:00
|
|
|
|
Restart?.Invoke();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contextMenus.ClickedExit += ClickedExit;
|
|
|
|
|
void ClickedExit()
|
|
|
|
|
{
|
2020-06-29 06:56:03 +12:00
|
|
|
|
Exit?.Invoke();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notifyIcon.ContextMenuStrip = contextMenus.Create();
|
|
|
|
|
notifyIcon.MouseClick += NotifyIcon_MouseClick;
|
|
|
|
|
void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
VerifyClick(e);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2019-07-05 05:04:14 +12:00
|
|
|
|
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
|
|
|
|
|
void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
|
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
VerifyClick(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
public event EventHandlerEmpty Click;
|
|
|
|
|
|
|
|
|
|
public event EventHandlerEmpty OpenLog;
|
|
|
|
|
|
|
|
|
|
public event EventHandlerEmpty Restart;
|
|
|
|
|
|
|
|
|
|
public event EventHandlerEmpty Exit;
|
|
|
|
|
|
2019-07-05 05:04:14 +12:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
notifyIcon.Icon = null;
|
|
|
|
|
notifyIcon.Dispose();
|
2020-03-25 02:39:43 +13:00
|
|
|
|
load.Dispose();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
|
2020-06-21 03:38:21 +12:00
|
|
|
|
public void LoadingStart()
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2020-06-21 03:38:21 +12:00
|
|
|
|
threadsLoading = true;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
load.Start();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void LoadingStop()
|
|
|
|
|
{
|
2021-05-14 01:20:40 +12:00
|
|
|
|
Cursor.Current = Cursors.Default;
|
2020-06-21 03:38:21 +12:00
|
|
|
|
threadsLoading = false;
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
2020-04-16 03:23:42 +12:00
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
private void VerifyClick(MouseEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (e.Button == MouseButtons.Left)
|
|
|
|
|
{
|
|
|
|
|
Click?.Invoke();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-05 05:04:14 +12:00
|
|
|
|
private void Load_Tick(object sender, EventArgs e)
|
|
|
|
|
{
|
2020-06-21 03:38:21 +12:00
|
|
|
|
if (threadsLoading)
|
2019-07-05 05:04:14 +12:00
|
|
|
|
{
|
2021-10-11 04:33:54 +13:00
|
|
|
|
rotationAngle += 5;
|
2021-10-26 08:45:49 +13:00
|
|
|
|
using Bitmap bitmapLoading = Resources.StaticResources.LoadingIcon.ToBitmap();
|
2021-11-17 12:13:46 +13:00
|
|
|
|
using Bitmap bitmapLoadingRotated = new(ImagingHelper.RotateImage(bitmapLoading, rotationAngle));
|
2021-10-14 04:17:33 +13:00
|
|
|
|
IntPtr hIcon = bitmapLoadingRotated.GetHicon();
|
|
|
|
|
notifyIcon.Icon = (Icon)Icon.FromHandle(hIcon).Clone();
|
|
|
|
|
DllImports.NativeMethods.User32DestroyIcon(hIcon);
|
2019-07-05 05:04:14 +12:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-11-18 11:46:43 +13:00
|
|
|
|
notifyIcon.Icon = Config.GetAppIcon();
|
2019-07-05 05:04:14 +12:00
|
|
|
|
load.Stop();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|