notifyIcon refresh

This commit is contained in:
rmbgame
2021-02-28 05:37:45 +08:00
parent 1315199426
commit bfa409da28
4 changed files with 62 additions and 7 deletions
@@ -2,6 +2,7 @@
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using ReactiveUI;
using System.Application.Mvvm;
using System.Application.UI.ViewModels;
using System.Application.UI.Views;
using System.Collections.Generic;
@@ -12,7 +13,7 @@ using AvaloniaApplication = Avalonia.Application;
namespace System.Application.UI
{
public class App : AvaloniaApplication
public class App : AvaloniaApplication, IDisposableHolder
{
public static App Instance => Current is App app ? app : throw new Exception("Impossible");
@@ -50,9 +51,7 @@ namespace System.Application.UI
MessageBoxButton.OK,
MessageBoxImage.Warning);
}
#region NotifyIcon
var notifyIcon = INotifyIcon.Instance;
notifyIcon.ToolTipText = ThisAssembly.AssemblyTrademark;
switch (DI.Platform)
@@ -88,17 +87,34 @@ namespace System.Application.UI
NotifyIconContextMenu.Items = menuItems;
notifyIcon.ContextMenu = NotifyIconContextMenu;
notifyIcon.Visible = true;
notifyIcon.Click += NotifyIcon_Click;
notifyIcon.DoubleClick += NotifyIcon_Click;
this.compositeDisposable.Add(() =>
{
notifyIcon.IconPath = string.Empty;
});
#endregion
desktop.MainWindow = MainWindow;
desktop.Exit += Desktop_Exit;
desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
}
base.OnFrameworkInitializationCompleted();
}
private void Desktop_Exit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
this.compositeDisposable.Dispose();
}
private void NotifyIcon_Click(object? sender, EventArgs e)
{
RestoreMainWindow();
}
public static async void SetClipboardText(string s) => await Current.Clipboard.SetTextAsync(s);
public Window? MainWindow { get; set; }
@@ -135,12 +151,30 @@ namespace System.Application.UI
/// <summary>
/// Exits the app by calling <c>Shutdown()</c> on the <c>IClassicDesktopStyleApplicationLifetime</c>.
/// </summary>
public static void Exit()
public static void Shutdown()
{
if (Current.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.Shutdown(0);
}
}
/// <summary>
/// Exits the app by calling <c>Shutdown()</c> on the <c>IClassicDesktopStyleApplicationLifetime</c>.
/// </summary>
public static void Exit()
{
Shutdown();
}
#region IDisposable members
public readonly CompositeDisposable compositeDisposable = new CompositeDisposable();
ICollection<IDisposable> IDisposableHolder.CompositeDisposable => this.compositeDisposable;
void IDisposable.Dispose()
{
this.compositeDisposable.Dispose();
GC.SuppressFinalize(this);
}
#endregion
}
}
@@ -2,6 +2,7 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using System.Application.UI.ViewModels;
using System.ComponentModel;
namespace System.Application.UI.Views
{
@@ -15,6 +16,13 @@ namespace System.Application.UI.Views
#endif
}
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
base.OnClosed(e);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
@@ -8,7 +8,7 @@ namespace System.Application.Mvvm
/// <summary>
/// 提供用于一次处理多个IDisposable对象的功能
/// </summary>
public class CompositeDisposable : IDisposable, ICollection<IDisposable>
public class CompositeDisposable : IDisposable, ICollection<IDisposable>, IEnumerable<IDisposable>, IEnumerable
{
readonly object _lockObject = new object();
readonly List<IDisposable> _targetLists;
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace System.Application.Mvvm
{
public interface IDisposableHolder : IDisposable
{
ICollection<IDisposable> CompositeDisposable { get; }
}
}