2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-04-05 17:39:39 -04:00
F K 701f6f0b1c
Add new Rendering controls for notifications, add missing forwarding (#47)
Plugins authors can now optionally specify what ways their notifications are rendered (subject, of course, to user preferences/settings). The default is to render notifications in all available channels. Examples:
- Show native pop-up window only (ie. no voice/plugin notifiers)
- Disallow other plugin notifiers

This does not support selection of notifier plugins.

Furthermore, persistent notification updates were not previously being forwarded to anything but the native popup notifier. Now plugins and native voice are also supported (subject to user preferences) and respect the new rendering controls added here. There is currently no concept of closing notifications for the native voice or plugin-based notifiers.
2021-12-18 17:57:23 -03:30

140 lines
4.6 KiB
C#

using Observatory.Framework;
using Observatory.Framework.Files;
using Observatory.Framework.Interfaces;
using Observatory.NativeNotification;
using System;
namespace Observatory.PluginManagement
{
public class PluginCore : IObservatoryCore
{
private readonly NativeVoice NativeVoice;
private readonly NativePopup NativePopup;
public PluginCore()
{
NativeVoice = new();
NativePopup = new();
}
public string Version => System.Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString();
public Status GetStatus()
{
throw new NotImplementedException();
}
public Guid SendNotification(string title, string text)
{
return SendNotification(new NotificationArgs() { Title = title, Detail = text });
}
public Guid SendNotification(NotificationArgs notificationArgs)
{
var guid = Guid.Empty;
if (!LogMonitor.GetInstance.ReadAllInProgress())
{
if (notificationArgs.Rendering.HasFlag(NotificationRendering.PluginNotifier))
{
var handler = Notification;
handler?.Invoke(this, notificationArgs);
}
if (Properties.Core.Default.NativeNotify && notificationArgs.Rendering.HasFlag(NotificationRendering.NativeVisual))
{
guid = NativePopup.InvokeNativeNotification(notificationArgs);
}
if (Properties.Core.Default.VoiceNotify && notificationArgs.Rendering.HasFlag(NotificationRendering.NativeVocal))
{
NativeVoice.EnqueueAndAnnounce(notificationArgs);
}
}
return guid;
}
public void CancelNotification(Guid id)
{
NativePopup.CloseNotification(id);
}
public void UpdateNotification(Guid id, NotificationArgs notificationArgs)
{
if (!LogMonitor.GetInstance.ReadAllInProgress())
{
if (notificationArgs.Rendering.HasFlag(NotificationRendering.PluginNotifier))
{
var handler = Notification;
handler?.Invoke(this, notificationArgs);
}
if (notificationArgs.Rendering.HasFlag(NotificationRendering.NativeVisual))
NativePopup.UpdateNotification(id, notificationArgs);
if (Properties.Core.Default.VoiceNotify && notificationArgs.Rendering.HasFlag(NotificationRendering.NativeVocal))
{
NativeVoice.EnqueueAndAnnounce(notificationArgs);
}
}
}
/// <summary>
/// Adds an item to the datagrid on UI thread to ensure visual update.
/// </summary>
/// <param name="worker"></param>
/// <param name="item"></param>
public void AddGridItem(IObservatoryWorker worker, object item)
{
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
worker.PluginUI.DataGrid.Add(item);
//Hacky removal of original empty object if one was used to populate columns
if (worker.PluginUI.DataGrid.Count == 2)
{
bool allNull = true;
Type itemType = worker.PluginUI.DataGrid[0].GetType();
foreach (var property in itemType.GetProperties())
{
if (property.GetValue(worker.PluginUI.DataGrid[0], null) != null)
{
allNull = false;
break;
}
}
if (allNull)
worker.PluginUI.DataGrid.RemoveAt(0);
}
});
}
public void ClearGrid(IObservatoryWorker worker, object templateItem)
{
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
worker.PluginUI.DataGrid.Add(templateItem);
while (worker.PluginUI.DataGrid.Count > 1)
worker.PluginUI.DataGrid.RemoveAt(0);
});
}
public void ExecuteOnUIThread(Action action)
{
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(action);
}
public System.Net.Http.HttpClient HttpClient
{
get => Observatory.HttpClient.Client;
}
public event EventHandler<NotificationArgs> Notification;
}
}