using NetSparkleUpdater.Properties;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace NetSparkleUpdater.UI.WinForms
{
public partial class ToastNotifier : Form
{
private Timer _goUpTimer;
private Timer _goDownTimer;
private Timer _pauseTimer;
private int startPosX;
private int startPosY;
public ToastNotifier(Icon? applicationIcon = null)
{
InitializeComponent();
TopMost = true;
ShowInTaskbar = false;
_goUpTimer = new Timer();
_goUpTimer.Interval = 25;
_goUpTimer.Tick += GoUpTimerTick;
_goDownTimer = new Timer();
_goDownTimer.Interval = 25;
_goDownTimer.Tick += GoDownTimerTick;
_pauseTimer = new Timer();
_pauseTimer.Interval = 15000;
_pauseTimer.Tick += PauseTimerTick;
if (applicationIcon != null)
{
Icon = applicationIcon;
this.Image.Image = Icon.ToBitmap();
}
ClickAction = () => { };
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public Action ClickAction { get; set; }
public List<AppCastItem>? Updates { get; set; }
private void PauseTimerTick(object sender, EventArgs e)
{
_pauseTimer.Stop();
_goDownTimer.Start();
}
protected override void OnLoad(EventArgs e)
{
startPosX = Screen.PrimaryScreen.WorkingArea.Width - Width;
startPosY = Screen.PrimaryScreen.WorkingArea.Height;
SetDesktopLocation(startPosX, startPosY);
base.OnLoad(e);
_goUpTimer.Start();
}
void GoUpTimerTick(object? sender, EventArgs e)
{
startPosY -= 5;
if (startPosY < Screen.PrimaryScreen.WorkingArea.Height - Height)
{
_goUpTimer.Stop();
_pauseTimer.Start();
}
else
{
SetDesktopLocation(startPosX, startPosY);
}
}
private void GoDownTimerTick(object? sender, EventArgs e)
{
startPosY += 5;
if (startPosY > Screen.PrimaryScreen.WorkingArea.Height)
{
_goDownTimer.Stop();
Close();
}
else
{
SetDesktopLocation(startPosX, startPosY);
}
}
private void ToastNotifier_Click(object? sender, EventArgs? e)
{
DialogResult = DialogResult.Yes;
ClickAction?.Invoke();
Close();
}
public void Show(string message, string callToAction, int seconds)
{
_message.Text = message;
_callToAction.Text = callToAction;
_pauseTimer.Interval = 1000 * seconds;
Show();
}
private void callToAction_LinkClicked(object? sender, LinkLabelLinkClickedEventArgs e)
{
this.ToastNotifier_Click(null, null);
}
}
}