private void menuItemExit_Click(object sender, System.EventArgs e) { // cleanup NotifyIcon.Remove(); NotifyIcon = null; this.Close(); }
public MainWindow() { InitializeComponent(); if (AvaloniaLocator.Current.GetService <MainWindow>() == null) { AvaloniaLocator.CurrentMutable.Bind <MainWindow>().ToConstant(this); } this.LocalizationService = new LocalizationExtension(); #if DEBUG this.AttachDevTools(); #endif // Set up and configure the notification icon // Get the type of the platform-specific implementation Type type = Implementation.ForType <INotifyIcon>(); if (type != null) { // If we have one, create an instance for it NotifyIcon = (INotifyIcon)Activator.CreateInstance(type); } if (NotifyIcon != null) { NotifyIcon.ToolTipText = "SQRL .NET Client"; NotifyIcon.IconPath = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "resm:SQRLDotNetClientUI.Assets.SQRL_icon_normal_16.png" : RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? @"resm:SQRLDotNetClientUI.Assets.sqrl_icon_normal_256.ico": @"resm:SQRLDotNetClientUI.Assets.sqrl_icon_normal_256_32_icon.ico"; NotifyIcon.DoubleClick += (s, e) => { RestoreWindow(); }; _NotifyIconContextMenu = new ContextMenu(); List <object> menuItems = new List <object>(); menuItems.AddRange(new[] { new MenuItem() { Header = LocalizationService.GetLocalizationValue("NotifyIconContextMenuItemHeaderRestore"), Command = ReactiveCommand.Create(RestoreWindow) }, new MenuItem() { Header = LocalizationService.GetLocalizationValue("NotifyIconContextMenuItemHeaderExit"), Command = ReactiveCommand.Create(Exit) } }); _NotifyIconContextMenu.Items = menuItems; NotifyIcon.ContextMenu = _NotifyIconContextMenu; NotifyIcon.Visible = true; } // Prevent the main window from closing. Just hide it instead // if we have a notify icon, or minimize it otherwise. this.Closing += (s, e) => { if (_reallyClose) { return; } if (NotifyIcon != null) { Log.Information("Hiding main window"); Dispatcher.UIThread.Post(() => { ((Window)s).Hide(); }); NotifyIcon.Visible = true; } else { Log.Information("Minimizing main window"); Dispatcher.UIThread.Post(() => { ((Window)s).WindowState = WindowState.Minimized; }); } e.Cancel = true; }; this.Closed += (s, e) => { // Remove the notify icon when the main window closes if (NotifyIcon != null) { NotifyIcon?.Remove(); } }; }