2
0
mirror of https://github.com/9ParsonsB/Pulsar.git synced 2025-07-01 08:23:42 -04:00

Notification overhaul and update to avaloniaui 0.10.7

This commit is contained in:
Xjph
2021-10-12 20:45:00 -02:30
parent 840ccad5bf
commit 5159500644
10 changed files with 279 additions and 75 deletions

View File

@ -63,7 +63,6 @@ namespace Observatory.UI.Views
e.Column.CanUserResize = true;
e.Column.CanUserSort = true;
}
private void UITypeChange()
{
var uiPanel = this.Find<Panel>("UIPanel");
@ -109,6 +108,7 @@ namespace Observatory.UI.Views
{
var dataContext = ((ViewModels.BasicUIViewModel)dataGrid.DataContext).BasicUIGrid;
dataContext.CollectionChanged += ScrollToLast;
}
}
@ -200,9 +200,7 @@ namespace Observatory.UI.Views
{
Header = "Popup Notifications",
DataContext = Properties.Core.Default,
Margin = new Thickness(0, 0),
Background = this.Background,
BorderThickness = new Thickness(0)
Margin = new Thickness(0, 0)
};
Grid notificationGrid = new() { Margin = new Thickness(10, 10) };
@ -244,8 +242,13 @@ namespace Observatory.UI.Views
{
Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
{
var notifyWindow = new UI.Views.NotificationView() { DataContext = new UI.ViewModels.NotificationViewModel("Test Notification", "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras suscipit hendrerit libero ac scelerisque.") };
notifyWindow.Show();
var notificationArgs = new NotificationArgs()
{
Title = "Test Notification",
Detail = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras suscipit hendrerit libero ac scelerisque."
};
new NativeNotification.NativePopup().InvokeNativeNotification(notificationArgs);
});
};
@ -448,9 +451,7 @@ namespace Observatory.UI.Views
{
Header = "Voice Notifications",
DataContext = Properties.Core.Default,
Margin = new Thickness(0, 0),
Background = this.Background,
BorderThickness = new Thickness(0)
Margin = new Thickness(0, 0)
};
Grid voiceGrid = new() { Margin = new Thickness(10, 10) };
@ -489,13 +490,6 @@ namespace Observatory.UI.Views
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Properties.Core.Default.VoiceSelected.Length > 0)
{
var speech = new System.Speech.Synthesis.SpeechSynthesizer()
{
Volume = Properties.Core.Default.VoiceVolume,
Rate = Properties.Core.Default.VoiceRate
};
speech.SelectVoice(Properties.Core.Default.VoiceSelected);
List<string> harvardSentences = new()
{
"Oak is strong and also gives shade.",
@ -510,7 +504,10 @@ namespace Observatory.UI.Views
"Move the vat over the hot fire."
};
speech.SpeakAsync("Speech Synthesis Test: " + harvardSentences.OrderBy(s => new Random().NextDouble()).First());
NotificationArgs args = new() { Title = "Speech Synthesis Test", Detail = harvardSentences.OrderBy(s => new Random().NextDouble()).First() };
new NativeNotification.NativeVoice().EnqueueAndAnnounce(args);
}
};

View File

@ -2,25 +2,77 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Layout;
using Avalonia.Markup.Xaml;
using Observatory.UI.ViewModels;
using System;
using System.Timers;
using System.Runtime.InteropServices;
namespace Observatory.UI.Views
{
public partial class NotificationView : Window
{
public NotificationView()
private readonly double scale;
private readonly Timer timer;
private readonly Guid guid;
public NotificationView() : this(default)
{ }
public NotificationView(Guid guid)
{
this.guid = guid;
InitializeComponent();
SystemDecorations = SystemDecorations.None;
ShowInTaskbar = false;
MakeClickThrough(); //Platform specific, currently windows only.
int screen = Properties.Core.Default.NativeNotifyScreen;
int corner = Properties.Core.Default.NativeNotifyCorner;
string font = Properties.Core.Default.NativeNotifyFont;
double scale = Properties.Core.Default.NativeNotifyScale / 100.0;
this.DataContextChanged += NotificationView_DataContextChanged;
scale = Properties.Core.Default.NativeNotifyScale / 100.0;
AdjustText();
AdjustPanel();
AdjustPosition();
timer = new();
timer.Elapsed += CloseNotification;
timer.Interval = Properties.Core.Default.NativeNotifyTimeout;
timer.Start();
#if DEBUG
this.AttachDevTools();
#endif
}
public Guid Guid { get => guid; }
private void NotificationView_DataContextChanged(object sender, EventArgs e)
{
var notification = ((NotificationViewModel)DataContext).Notification;
AdjustText();
AdjustPanel();
AdjustPosition(notification.XPos / 100, notification.YPos / 100);
if (notification.Timeout > 0)
{
timer.Stop();
timer.Interval = notification.Timeout;
timer.Start();
}
else if (notification.Timeout == 0)
{
timer.Stop();
}
}
private void AdjustText()
{
string font = Properties.Core.Default.NativeNotifyFont;
var titleText = this.Find<TextBlock>("Title");
var detailText = this.Find<TextBlock>("Detail");
@ -34,7 +86,10 @@ namespace Observatory.UI.Views
titleText.FontSize *= scale;
detailText.FontSize *= scale;
}
private void AdjustPanel()
{
var textPanel = this.Find<StackPanel>("TextPanel");
Width *= scale;
Height *= scale;
@ -43,8 +98,13 @@ namespace Observatory.UI.Views
var textBorder = this.Find<Border>("TextBorder");
textBorder.BorderThickness *= scale;
}
private void AdjustPosition(double xOverride = -1.0, double yOverride = -1.0)
{
PixelRect screenBounds;
int screen = Properties.Core.Default.NativeNotifyScreen;
int corner = Properties.Core.Default.NativeNotifyCorner;
if (screen == -1 || screen > Screens.All.Count)
screenBounds = Screens.Primary.Bounds;
@ -55,29 +115,29 @@ namespace Observatory.UI.Views
double scaleWidth = Width * displayScale;
double scaleHeight = Height * displayScale;
switch (corner)
if (xOverride >= 0 && yOverride >= 0)
{
default: case 0:
Position = screenBounds.BottomRight - new PixelPoint((int)scaleWidth + 50, (int)scaleHeight + 50);
break;
case 1:
Position = screenBounds.BottomLeft - new PixelPoint(-50, (int)scaleHeight + 50);
break;
case 2:
Position = screenBounds.TopRight - new PixelPoint((int)scaleWidth + 50, -50);
break;
case 3:
Position = screenBounds.TopLeft + new PixelPoint(50, 50);
break;
Position = screenBounds.TopLeft + new PixelPoint(Convert.ToInt32(screenBounds.Width * xOverride), Convert.ToInt32(screenBounds.Height * yOverride));
}
else
{
switch (corner)
{
default:
case 0:
Position = screenBounds.BottomRight - new PixelPoint(Convert.ToInt32(scaleWidth) + 50, Convert.ToInt32(scaleHeight) + 50);
break;
case 1:
Position = screenBounds.BottomLeft - new PixelPoint(-50, Convert.ToInt32(scaleHeight) + 50);
break;
case 2:
Position = screenBounds.TopRight - new PixelPoint(Convert.ToInt32(scaleWidth) + 50, -50);
break;
case 3:
Position = screenBounds.TopLeft + new PixelPoint(50, 50);
break;
}
}
var timer = new System.Timers.Timer();
timer.Elapsed += CloseNotification;
timer.Interval = Properties.Core.Default.NativeNotifyTimeout;
timer.Start();
#if DEBUG
this.AttachDevTools();
#endif
}
private void CloseNotification(object sender, System.Timers.ElapsedEventArgs e)