Files
limanapp/EgwControlCenter.App/BlazorForm.cs
T
Samuele Locatelli ab3b32a780 ACC:
- Fix single click sx/dx
- add paginazione se > 5
- test vari componenti razor
- gestione paginazione
2024-09-24 16:39:37 +02:00

279 lines
8.6 KiB
C#

using EgwControlCenter.App.Components.Pages;
using EgwControlCenter.Core;
using Microsoft.AspNetCore.Components.WebView.WindowsForms;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.VisualBasic.Logging;
using NLog;
using System.Data;
using System.Reflection;
using System.Resources;
namespace EgwControlCenter.App
{
public partial class BlazorForm : Form
{
#region Public Constructors
public BlazorForm()
{
InitializeComponent();
FormInit();
InitBlazorView();
ForceReload();
}
#endregion Public Constructors
#region Private Fields
/// <summary>
/// Classe logger
/// </summary>
private static Logger Log = LogManager.GetCurrentClassLogger();
private bool forceClose = false;
private bool showReduced = false;
/// <summary>
/// Periodo di blocco invio nuove notifiche se ci sono + update...
/// </summary>
private int VetoNewNotifySec = 15;
#endregion Private Fields
#region Private Properties
private static AppControlService MainAppControlService { get; set; } = null!;
private string AppName => CurrAssembly.Name ?? "EgalWare ACC";
private System.Reflection.AssemblyName CurrAssembly { get; set; } = new System.Reflection.AssemblyName();
/// <summary>
/// Ultima visualizzazione messaggio...
/// </summary>
private DateTime lastMsqShown { get; set; } = DateTime.MinValue;
#endregion Private Properties
#region Private Methods
private void BlazorForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (forceClose)
{
timerCheck.Stop();
timerCheck.Dispose();
}
else
{
e.Cancel = true;
SendToTray();
}
}
private async void BlazorForm_Load(object sender, EventArgs e)
{
SetPosition();
// aspetto e poi mando a tray...
await Task.Delay(500);
SendToTray();
}
private void BlazorForm_Resize(object sender, EventArgs e)
{
CheckFormVisibility();
}
/// <summary>
/// Verifica stato windows (minimized/normal) e visibilità con tray...
/// </summary>
private void CheckFormVisibility()
{
// controllo cosa devo mostrare...
if (WindowState == FormWindowState.Minimized)
{
notifyIcon1.Visible = true;
SendToTray();
}
else
{
notifyIcon1.Visible = true;
}
}
/// <summary>
/// crea menù tray x applicazione
/// </summary>
private void CreateTrayMenu()
{
// Fix testi menù tray...
trayMenu.Items.Clear();
// SE permessa massimizzazione...
trayMenu.Items.Add("Show EgalWare ACC");
// se è permesso chiudere
trayMenu.Items.Add("Close EgalWare ACC");
}
private void DoDisplay()
{
Show();
WindowState = FormWindowState.Normal;
//SendToBack();
}
/// <summary>
/// Chiamata controllo status update
/// </summary>
private void ForceReload()
{
MainAppControlService.DoFullCheck(true);
}
private void FormInit()
{
//init preliminare servizio controllo
MainAppControlService = new AppControlService();
timerCheck.Interval = (MainAppControlService.RefreshPeriod * 1000);
MainAppControlService.EA_StatusUpdated += MainAppControlService_EA_StatusUpdated;
// sistemo timer
timerCheck.Start();
}
private void InitBlazorView()
{
CurrAssembly = System.Reflection.Assembly.GetExecutingAssembly().GetName();
Log.Trace($"EgalWare's AppControlCenter Init, v.{CurrAssembly.Version}");
// sistemo grafica TRAY ICON
SetupTrayIcon();
CreateTrayMenu();
var services = new ServiceCollection();
services.AddWindowsFormsBlazorWebView();
services.AddSingleton<AppControlService>(MainAppControlService);
blazorWebView1.HostPage = "wwwroot\\index.html";
blazorWebView1.Services = services.BuildServiceProvider();
blazorWebView1.RootComponents.Add<MainBlazor>("#app");
}
/// <summary>
/// Verifica se ci siano update da mostrare come notifica in tray
/// </summary>
/// <exception cref="NotImplementedException"></exception>
private void MainAppControlService_EA_StatusUpdated()
{
// notifica ballontip se ho aggiornamenti...
DateTime adesso = DateTime.Now;
// mostro se non ne ho ricevuti altri da almeno 10 sec...
if (WindowState != FormWindowState.Normal && adesso > lastMsqShown.AddSeconds(VetoNewNotifySec))
{
// salvo che ho mostrato msg
lastMsqShown = adesso;
// mostro notifica...
notifyIcon1.BalloonTipTitle = "EgalWare's App Control Center";
notifyIcon1.BalloonTipText = "Update found!";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Warning;
notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked;
notifyIcon1.ShowBalloonTip(200);
}
}
private void NotifyIcon1_BalloonTipClicked(object? sender, EventArgs e)
{
DoDisplay();
}
/// <summary>
/// doppio click su tray icon
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
DoDisplay();
}
else
{
SendToTray();
}
}
/// <summary>
/// Gestisce "andata nel tray" della form
/// </summary>
private void SendToTray()
{
if (!notifyIcon1.Visible)
{
notifyIcon1.BalloonTipTitle = AppName;
notifyIcon1.BalloonTipText = $"{AppName} running on tray";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.BalloonTipClicked += NotifyIcon1_BalloonTipClicked;
notifyIcon1.Visible = true;
notifyIcon1.ShowBalloonTip(100);
}
WindowState = FormWindowState.Minimized;
Hide();
}
private void SetPosition()
{
// posiziono in basso a sx...
Rectangle workArea = Screen.GetWorkingArea(this);
this.Location = new Point(workArea.Right - Size.Width, workArea.Bottom - Size.Height);
}
private void SetupTrayIcon()
{
// fix icon!
notifyIcon1.Text = $"EgalWare's AppControlCenter | {CurrAssembly.Version}";
}
private void timerCheck_Tick(object sender, EventArgs e)
{
// esegue controllo locale ed eventualmente remoto se scaduto...
MainAppControlService.DoFullCheck(false);
}
/// <summary>
/// click su menù contestuale in tray
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void trayMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e != null && e.ClickedItem != null && !string.IsNullOrEmpty(e.ClickedItem.Text))
{
if (e.ClickedItem.Text.StartsWith("Close"))
{
// chiudo!
forceClose = true;
Close();
}
else if (e.ClickedItem.Text.StartsWith("Show"))
{
Show();
WindowState = FormWindowState.Normal;
}
}
}
#endregion Private Methods
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Right)
{
if (WindowState == FormWindowState.Minimized)
{
DoDisplay();
}
else
{
SendToTray();
}
}
}
}
}