SystemTrayMenu/UserInterface/AppNotifyIcon.cs

101 lines
3 KiB
C#
Raw Normal View History

// <copyright file="AppNotifyIcon.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
2019-07-05 05:04:14 +12:00
namespace SystemTrayMenu.UserInterface
2019-07-05 05:04:14 +12:00
{
using System;
using System.Drawing;
using System.Windows.Forms;
using SystemTrayMenu.Helper;
using SystemTrayMenu.Utilities;
using Timer = System.Windows.Forms.Timer;
internal class AppNotifyIcon : IDisposable
2019-07-05 05:04:14 +12:00
{
private readonly Timer load = new();
private readonly NotifyIcon notifyIcon = new();
private bool threadsLoading;
private int rotationAngle;
2019-07-05 05:04:14 +12:00
public AppNotifyIcon()
2019-07-05 05:04:14 +12:00
{
notifyIcon.Icon = Resources.StaticResources.LoadingIcon;
2019-07-05 05:04:14 +12:00
load.Tick += Load_Tick;
load.Interval = 15;
notifyIcon.Text = Translator.GetText("SystemTrayMenu");
2019-07-05 05:04:14 +12:00
notifyIcon.Visible = true;
notifyIcon.Icon = Config.GetAppIcon();
AppContextMenu contextMenus = new();
2019-07-05 05:04:14 +12:00
contextMenus.ClickedOpenLog += ClickedOpenLog;
void ClickedOpenLog()
{
OpenLog?.Invoke();
2019-07-05 05:04:14 +12:00
}
notifyIcon.ContextMenuStrip = contextMenus.Create();
notifyIcon.MouseClick += NotifyIcon_MouseClick;
void NotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
VerifyClick(e);
2019-07-05 05:04:14 +12:00
}
2019-07-05 05:04:14 +12:00
notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
VerifyClick(e);
}
}
public event EventHandlerEmpty Click;
public event EventHandlerEmpty OpenLog;
2019-07-05 05:04:14 +12:00
public void Dispose()
{
notifyIcon.Icon = null;
notifyIcon.Dispose();
load.Dispose();
2019-07-05 05:04:14 +12:00
}
public void LoadingStart()
2019-07-05 05:04:14 +12:00
{
threadsLoading = true;
2019-07-05 05:04:14 +12:00
load.Start();
}
public void LoadingStop()
{
Cursor.Current = Cursors.Default;
threadsLoading = false;
2019-07-05 05:04:14 +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)
{
if (threadsLoading)
2019-07-05 05:04:14 +12:00
{
rotationAngle += 5;
using Bitmap bitmapLoading = Resources.StaticResources.LoadingIcon.ToBitmap();
using Bitmap bitmapLoadingRotated = new(ImagingHelper.RotateImage(bitmapLoading, rotationAngle));
IntPtr hIcon = bitmapLoadingRotated.GetHicon();
notifyIcon.Icon = (Icon)Icon.FromHandle(hIcon).Clone();
DllImports.NativeMethods.User32DestroyIcon(hIcon);
2019-07-05 05:04:14 +12:00
}
else
{
notifyIcon.Icon = Config.GetAppIcon();
2019-07-05 05:04:14 +12:00
load.Stop();
}
}
}
}