UI - Added desktop notifications API

This commit is contained in:
SpoinkyNL committed 2021-01-10 12:49:36 +01:00
1 parent 883fccef7b
commit 30c4314f1d
15 files changed
+217 -42

No files matched your search

+2 -1
View File
@@ -1,5 +1,6 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Threading.Tasks;
using System.Timers;
@@ -80,7 +81,7 @@ namespace Artemis.UI.Screens
PinSidebar = _settingsService.GetSetting("UI.PinSidebar", false);
AssemblyInformationalVersionAttribute versionAttribute = typeof(RootViewModel).Assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
WindowTitle = $"Artemis {versionAttribute?.InformationalVersion} build {Constants.BuildInfo.BuildNumber}";
WindowTitle = $"Artemis {versionAttribute?.InformationalVersion} build {Constants.BuildInfo.BuildNumberDisplay}";
}
public PluginSetting<bool> PinSidebar { get; }
@@ -279,8 +279,6 @@ namespace Artemis.UI.Screens.Settings.Tabs.General
bool updateFound = await _updateService.OfferUpdatesIfFound();
if (!updateFound)
_messageService.ShowMessage("You are already running the latest Artemis build. (☞゚ヮ゚)☞");
else
_messageService.ShowMessage("You are already running the latest Artemis build. (☞゚ヮ゚)☞");
CanOfferUpdatesIfFound = true;
}
+2 -1
View File
@@ -17,7 +17,8 @@
MenuActivation="LeftOrRightClick"
PopupActivation="DoubleClick"
ToolTipText="Artemis"
DoubleClickCommand="{s:Action TrayBringToForeground}">
DoubleClickCommand="{s:Action TrayBringToForeground}"
TrayBalloonTipClicked="{s:Action OnTrayBalloonTipClicked}">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<MenuItem Header="Home" Command="{s:Action TrayActivateSidebarItem}" CommandParameter="Home">
+80 -14
View File
@@ -1,31 +1,42 @@
using System;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Artemis.Core.Services;
using Artemis.UI.Events;
using Artemis.UI.Screens.Splash;
using Artemis.UI.Services;
using Artemis.UI.Services.Interfaces;
using Artemis.UI.Shared.Services;
using Hardcodet.Wpf.TaskbarNotification;
using MaterialDesignThemes.Wpf;
using Ninject;
using Stylet;
using Icon = System.Drawing.Icon;
namespace Artemis.UI.Screens
{
public class TrayViewModel : Screen, IMainWindowManager
public class TrayViewModel : Screen, IMainWindowProvider, INotificationProvider
{
private readonly IDebugService _debugService;
private readonly IEventAggregator _eventAggregator;
private readonly IKernel _kernel;
private readonly IWindowManager _windowManager;
private bool _canShowRootViewModel;
private SplashViewModel _splashViewModel;
private RootViewModel _rootViewModel;
private SplashViewModel _splashViewModel;
private TaskbarIcon _taskBarIcon;
public TrayViewModel(IKernel kernel,
IWindowManager windowManager,
public TrayViewModel(IKernel kernel,
IWindowManager windowManager,
IWindowService windowService,
IMessageService messageService,
IUpdateService updateService,
IEventAggregator eventAggregator,
ICoreService coreService,
IEventAggregator eventAggregator,
ICoreService coreService,
IDebugService debugService,
ISettingsService settingsService)
{
@@ -35,7 +46,8 @@ namespace Artemis.UI.Screens
_debugService = debugService;
CanShowRootViewModel = true;
windowService.ConfigureMainWindowManager(this);
windowService.ConfigureMainWindowProvider(this);
messageService.ConfigureNotificationProvider(this);
bool autoRunning = Bootstrapper.StartupArguments.Contains("--autorun");
bool showOnAutoRun = settingsService.GetSetting("UI.ShowOnStartup", true).Value;
if (!autoRunning || showOnAutoRun)
@@ -43,8 +55,8 @@ namespace Artemis.UI.Screens
ShowSplashScreen();
coreService.Initialized += (_, _) => TrayBringToForeground();
}
updateService.AutoUpdate();
else
updateService.AutoUpdate();
}
public bool CanShowRootViewModel
@@ -91,6 +103,28 @@ namespace Artemis.UI.Screens
_debugService.ShowDebugger();
}
public void SetTaskbarIcon(UIElement view)
{
_taskBarIcon = (TaskbarIcon) ((ContentControl) view).Content;
}
public void OnTrayBalloonTipClicked(object sender, EventArgs e)
{
if (CanShowRootViewModel)
TrayBringToForeground();
else
{
// Wrestle the main window to the front
Window mainWindow = ((Window) _rootViewModel.View);
if (mainWindow.WindowState == WindowState.Minimized)
mainWindow.WindowState = WindowState.Normal;
mainWindow.Activate();
mainWindow.Topmost = true;
mainWindow.Topmost = false;
mainWindow.Focus();
}
}
private void ShowSplashScreen()
{
Execute.OnUIThread(() =>
@@ -109,12 +143,47 @@ namespace Artemis.UI.Screens
OnMainWindowClosed();
}
#region Implementation of IMainWindowManager
#region Implementation of INotificationProvider
/// <inheritdoc />
public void ShowNotification(string title, string message, PackIconKind icon)
{
Execute.OnUIThread(() =>
{
// Convert the PackIcon to an icon by drawing it on a visual
DrawingVisual drawingVisual = new();
DrawingContext drawingContext = drawingVisual.RenderOpen();
PackIcon packIcon = new() {Kind = icon};
Geometry geometry = Geometry.Parse(packIcon.Data);
// Scale the icon up to fit a 256x256 image and draw it
geometry = Geometry.Combine(geometry, Geometry.Empty, GeometryCombineMode.Union, new ScaleTransform(256 / geometry.Bounds.Right, 256 / geometry.Bounds.Bottom));
drawingContext.DrawGeometry(new SolidColorBrush(Colors.White), null, geometry);
drawingContext.Close();
// Render the visual and add it to a PNG encoder (we want opacity in our icon)
RenderTargetBitmap renderTargetBitmap = new(256, 256, 96, 96, PixelFormats.Pbgra32);
renderTargetBitmap.Render(drawingVisual);
PngBitmapEncoder encoder = new();
encoder.Frames.Add(BitmapFrame.Create(renderTargetBitmap));
// Save the PNG and get an icon handle
using MemoryStream stream = new();
encoder.Save(stream);
Icon convertedIcon = Icon.FromHandle(new Bitmap(stream).GetHicon());
// Show the 'balloon'
_taskBarIcon.ShowBalloonTip(title, message, convertedIcon, true);
});
}
#endregion
#region Implementation of IMainWindowProvider
public bool IsMainWindowOpen { get; private set; }
/// <inheritdoc />
public bool OpenMainWindow()
{
if (CanShowRootViewModel)
@@ -124,17 +193,14 @@ namespace Artemis.UI.Screens
return true;
}
/// <inheritdoc />
public bool CloseMainWindow()
{
_rootViewModel.RequestClose();
return _rootViewModel.ScreenState == ScreenState.Closed;
}
/// <inheritdoc />
public event EventHandler MainWindowOpened;
/// <inheritdoc />
public event EventHandler MainWindowClosed;
protected virtual void OnMainWindowOpened()