2020-07-07 07:15:45 +12:00
|
|
|
|
// <copyright file="Fading.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
2020-04-01 07:42:49 +13:00
|
|
|
|
|
2021-10-26 08:45:49 +13:00
|
|
|
|
namespace SystemTrayMenu.Helper
|
2020-04-01 07:42:49 +13:00
|
|
|
|
{
|
2020-07-07 07:15:45 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using SystemTrayMenu.Utilities;
|
|
|
|
|
|
2020-04-01 07:42:49 +13:00
|
|
|
|
public class Fading : IDisposable
|
|
|
|
|
{
|
2021-05-29 08:45:32 +12:00
|
|
|
|
private const int Interval100FPS = 10; // 100fps=>1s/100fps=~10ms
|
2020-04-01 07:42:49 +13:00
|
|
|
|
|
|
|
|
|
private const double StepIn = 0.20;
|
2020-04-30 23:22:47 +12:00
|
|
|
|
private const double StepOut = 0.10;
|
2020-04-01 07:42:49 +13:00
|
|
|
|
private const double Transparent = 0.80;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
private const double TransparentMinus = 0.60; // Transparent - StepIn
|
|
|
|
|
private const double TransparentPlus = 0.85; // Transparent + StepOut
|
2020-04-01 07:42:49 +13:00
|
|
|
|
private const double Shown = 1.00;
|
2020-07-07 07:15:45 +12:00
|
|
|
|
private const double ShownMinus = 0.80; // Shown - StepIn
|
2020-04-01 07:42:49 +13:00
|
|
|
|
|
|
|
|
|
private readonly Timer timer = new Timer();
|
|
|
|
|
private FadingState state = FadingState.Idle;
|
2021-04-17 12:39:48 +12:00
|
|
|
|
private double opacity;
|
|
|
|
|
private bool visible;
|
2020-04-01 07:42:49 +13:00
|
|
|
|
|
|
|
|
|
internal Fading()
|
|
|
|
|
{
|
2021-05-29 08:45:32 +12:00
|
|
|
|
timer.Interval = Interval100FPS;
|
2020-04-08 13:50:52 +12:00
|
|
|
|
timer.Tick += Tick;
|
|
|
|
|
void Tick(object sender, EventArgs e)
|
2020-04-01 07:42:49 +13:00
|
|
|
|
{
|
2020-04-08 13:50:52 +12:00
|
|
|
|
FadeStep();
|
2020-04-01 07:42:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-07 09:37:55 +12:00
|
|
|
|
internal event EventHandlerEmpty Hide;
|
|
|
|
|
|
|
|
|
|
internal event EventHandlerEmpty Show;
|
|
|
|
|
|
|
|
|
|
internal event EventHandler<double> ChangeOpacity;
|
|
|
|
|
|
|
|
|
|
internal enum FadingState
|
|
|
|
|
{
|
|
|
|
|
Idle,
|
|
|
|
|
Show,
|
|
|
|
|
ShowTransparent,
|
|
|
|
|
Hide,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal bool IsHiding => state == FadingState.Hide;
|
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
Dispose(true);
|
|
|
|
|
GC.SuppressFinalize(this);
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 07:42:49 +13:00
|
|
|
|
internal void Fade(FadingState state)
|
|
|
|
|
{
|
|
|
|
|
StartStopTimer(state);
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-08 03:05:19 +12:00
|
|
|
|
protected virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing)
|
|
|
|
|
{
|
|
|
|
|
timer.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-16 03:23:42 +12:00
|
|
|
|
private void StartStopTimer(FadingState newState)
|
2020-04-01 07:42:49 +13:00
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
if (newState == FadingState.Idle)
|
2020-04-01 07:42:49 +13:00
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
state = newState;
|
2020-04-08 13:50:52 +12:00
|
|
|
|
timer.Stop();
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-04-16 03:23:42 +12:00
|
|
|
|
state = newState;
|
2020-04-08 13:50:52 +12:00
|
|
|
|
timer.Start();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void FadeStep()
|
|
|
|
|
{
|
|
|
|
|
switch (state)
|
|
|
|
|
{
|
|
|
|
|
case FadingState.Show:
|
|
|
|
|
if (!visible)
|
|
|
|
|
{
|
|
|
|
|
visible = true;
|
2020-06-29 06:57:03 +12:00
|
|
|
|
Show?.Invoke();
|
2020-04-08 13:50:52 +12:00
|
|
|
|
opacity = 0;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (opacity < ShownMinus)
|
|
|
|
|
{
|
|
|
|
|
opacity += StepIn;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (opacity != Shown)
|
|
|
|
|
{
|
|
|
|
|
opacity = Shown;
|
|
|
|
|
ChangeOpacity?.Invoke(this, Shown);
|
|
|
|
|
StartStopTimer(FadingState.Idle);
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-04-08 13:50:52 +12:00
|
|
|
|
break;
|
|
|
|
|
case FadingState.ShowTransparent:
|
|
|
|
|
if (!visible)
|
|
|
|
|
{
|
|
|
|
|
visible = true;
|
2020-06-29 06:57:03 +12:00
|
|
|
|
Show?.Invoke();
|
2020-04-08 13:50:52 +12:00
|
|
|
|
opacity = 0;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (opacity < TransparentMinus)
|
|
|
|
|
{
|
|
|
|
|
opacity += StepIn;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (opacity > TransparentPlus)
|
|
|
|
|
{
|
|
|
|
|
opacity -= StepOut;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (opacity != Transparent)
|
|
|
|
|
{
|
|
|
|
|
ChangeOpacity?.Invoke(this, Transparent);
|
|
|
|
|
StartStopTimer(FadingState.Idle);
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-04-08 13:50:52 +12:00
|
|
|
|
break;
|
|
|
|
|
case FadingState.Hide:
|
|
|
|
|
if (opacity > StepOut)
|
|
|
|
|
{
|
|
|
|
|
opacity -= StepOut;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
}
|
|
|
|
|
else if (visible)
|
|
|
|
|
{
|
|
|
|
|
opacity = 0;
|
|
|
|
|
ChangeOpacity?.Invoke(this, opacity);
|
|
|
|
|
visible = false;
|
2020-06-29 06:57:03 +12:00
|
|
|
|
Hide?.Invoke();
|
2020-04-08 13:50:52 +12:00
|
|
|
|
StartStopTimer(FadingState.Idle);
|
|
|
|
|
}
|
2020-07-07 07:15:45 +12:00
|
|
|
|
|
2020-04-08 13:50:52 +12:00
|
|
|
|
break;
|
|
|
|
|
case FadingState.Idle:
|
|
|
|
|
default:
|
|
|
|
|
StartStopTimer(FadingState.Idle);
|
|
|
|
|
break;
|
2020-04-01 07:42:49 +13:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|