using EgwControlCenter.App.Components.Pages; using EgwControlCenter.Core; using Microsoft.AspNetCore.Components.WebView.WindowsForms; using Microsoft.Extensions.DependencyInjection; using Microsoft.VisualBasic.Logging; using Microsoft.Win32; 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(); ServiceInit(); CleanOldTrayIcons(); InitBlazorView(); StartTimer(); ForceReload(); } private void CleanOldTrayIcons() { string registryPath = @"HKEY_CURRENT_USER\Control Panel\NotifyIconSettings"; Assembly assembly = Assembly.GetExecutingAssembly(); string startDir = Path.GetDirectoryName(assembly.Location)!; string appPath = assembly.ManifestModule.FullyQualifiedName.Replace("dll", "exe"); RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\NotifyIconSettings", true); if (key != null) { foreach (string subKeyName in key.GetSubKeyNames()) { RegistryKey subKey = key.OpenSubKey(subKeyName); if (subKey != null) { string executablePath = subKey.GetValue("ExecutablePath") as string; // cerco solo le chaivi di EgwControlCenter.App.exe if (!string.IsNullOrEmpty(executablePath)) { if (executablePath.EndsWith("EgwControlCenter.App.exe") || executablePath.Contains("EgwControlCenter")) { // se il path NON E' quello da cui sta girando il software --> elimino chiave registro... if (!executablePath.Equals(appPath)) { key.DeleteSubKey(subKeyName); } } } } } } else { Console.WriteLine("NotifyIconSettings key not found."); } } #endregion Public Constructors #region Private Fields /// /// Classe logger /// private static Logger Log = LogManager.GetCurrentClassLogger(); private bool forceClose = false; private bool showReduced = false; /// /// Periodo di blocco invio nuove notifiche se ci sono + update... /// 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(); /// /// Ultima visualizzazione messaggio... /// 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(); } /// /// Verifica stato windows (minimized/normal) e visibilità con tray... /// private void CheckFormVisibility() { // controllo cosa devo mostrare... if (WindowState == FormWindowState.Minimized) { notifyIcon1.Visible = true; SendToTray(); } else { notifyIcon1.Visible = true; } } /// /// crea menù tray x applicazione /// 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(); } /// /// Chiamata controllo status update /// private void ForceReload() { MainAppControlService.DoFullCheck(true); } private void ServiceInit() { //init preliminare servizio controllo MainAppControlService = new AppControlService(); } private void StartTimer() { 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(MainAppControlService); blazorWebView1.HostPage = "wwwroot\\index.html"; blazorWebView1.Services = services.BuildServiceProvider(); blazorWebView1.RootComponents.Add("#app"); } /// /// Verifica se ci siano update da mostrare come notifica in tray /// /// 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(); } /// /// doppio click su tray icon /// /// /// private void notifyIcon1_DoubleClick(object sender, EventArgs e) { if (WindowState == FormWindowState.Minimized) { DoDisplay(); } else { SendToTray(); } } /// /// Gestisce "andata nel tray" della form /// 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); } /// /// click su menù contestuale in tray /// /// /// 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(); } } } } }