mirror of
https://github.com/9ParsonsB/Pulsar.git
synced 2025-07-01 16:33:43 -04:00
Notification overhaul and update to avaloniaui 0.10.7
This commit is contained in:
52
ObservatoryCore/NativeNotification/NativePopup.cs
Normal file
52
ObservatoryCore/NativeNotification/NativePopup.cs
Normal file
@ -0,0 +1,52 @@
|
||||
using Observatory.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Observatory.UI.Views;
|
||||
using Observatory.UI.ViewModels;
|
||||
|
||||
namespace Observatory.NativeNotification
|
||||
{
|
||||
public class NativePopup
|
||||
{
|
||||
private Dictionary<Guid, NotificationView> notifications;
|
||||
|
||||
public NativePopup()
|
||||
{
|
||||
notifications = new();
|
||||
}
|
||||
|
||||
public Guid InvokeNativeNotification(NotificationArgs notificationArgs)
|
||||
{
|
||||
var notificationGuid = Guid.NewGuid();
|
||||
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
|
||||
{
|
||||
var notifyWindow = new NotificationView(notificationGuid) { DataContext = new NotificationViewModel(notificationArgs) };
|
||||
notifyWindow.Closed += NotifyWindow_Closed;
|
||||
notifications.Add(notificationGuid, notifyWindow);
|
||||
notifyWindow.Show();
|
||||
});
|
||||
|
||||
return notificationGuid;
|
||||
}
|
||||
|
||||
private void NotifyWindow_Closed(object sender, EventArgs e)
|
||||
{
|
||||
var notification = (NotificationView)sender;
|
||||
|
||||
if (notifications.ContainsKey(notification.Guid))
|
||||
notifications.Remove(notification.Guid);
|
||||
}
|
||||
|
||||
public void CloseNotification(Guid guid)
|
||||
{
|
||||
if (notifications.ContainsKey(guid))
|
||||
notifications[guid].Close();
|
||||
}
|
||||
|
||||
public void UpdateNotification(Guid guid, NotificationArgs notificationArgs)
|
||||
{
|
||||
if (notifications.ContainsKey(guid))
|
||||
notifications[guid].DataContext = new NotificationViewModel(notificationArgs);
|
||||
}
|
||||
}
|
||||
}
|
74
ObservatoryCore/NativeNotification/NativeVoice.cs
Normal file
74
ObservatoryCore/NativeNotification/NativeVoice.cs
Normal file
@ -0,0 +1,74 @@
|
||||
using Observatory.Framework;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Speech.Synthesis;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Observatory.NativeNotification
|
||||
{
|
||||
public class NativeVoice
|
||||
{
|
||||
private Queue<NotificationArgs> notificationEvents;
|
||||
private bool processing;
|
||||
|
||||
public NativeVoice()
|
||||
{
|
||||
notificationEvents = new();
|
||||
processing = false;
|
||||
}
|
||||
|
||||
public void EnqueueAndAnnounce(NotificationArgs eventArgs)
|
||||
{
|
||||
notificationEvents.Enqueue(eventArgs);
|
||||
|
||||
if (!processing)
|
||||
{
|
||||
processing = true;
|
||||
ProcessQueueAsync();
|
||||
}
|
||||
}
|
||||
|
||||
private async void ProcessQueueAsync()
|
||||
{
|
||||
await Task.Factory.StartNew(ProcessQueue);
|
||||
}
|
||||
|
||||
private void ProcessQueue()
|
||||
{
|
||||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
|
||||
{
|
||||
var speech = new SpeechSynthesizer()
|
||||
{
|
||||
Volume = Properties.Core.Default.VoiceVolume,
|
||||
Rate = Properties.Core.Default.VoiceRate
|
||||
};
|
||||
speech.SelectVoice(Properties.Core.Default.VoiceSelected);
|
||||
|
||||
while (notificationEvents.Any())
|
||||
{
|
||||
var notification = notificationEvents.Dequeue();
|
||||
|
||||
if (notification.TitleSsml?.Length > 0)
|
||||
{
|
||||
speech.SpeakSsml(notification.TitleSsml);
|
||||
}
|
||||
else
|
||||
{
|
||||
speech.Speak(notification.Title);
|
||||
}
|
||||
|
||||
if (notification.DetailSsml?.Length > 0)
|
||||
{
|
||||
speech.SpeakSsml(notification.DetailSsml);
|
||||
}
|
||||
else
|
||||
{
|
||||
speech.Speak(notification.Detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
processing = false;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user