Browse Source

Add license dialog in settings page

pull/55/head
Ionite 1 year ago
parent
commit
c7928ba0a5
No known key found for this signature in database
  1. 60
      StabilityMatrix.Avalonia/Controls/BetterContentDialog.cs
  2. 24
      StabilityMatrix.Avalonia/DialogHelper.cs
  3. 5
      StabilityMatrix.Avalonia/Models/AvaloniaResource.cs
  4. 44
      StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs
  5. 1
      StabilityMatrix.Avalonia/Views/SettingsPage.axaml

60
StabilityMatrix.Avalonia/Controls/BetterContentDialog.cs

@ -113,6 +113,15 @@ public class BetterContentDialog : ContentDialog
get => GetValue(MaxDialogWidthProperty);
set => SetValue(MaxDialogWidthProperty, value);
}
public static readonly StyledProperty<double> MaxDialogHeightProperty = AvaloniaProperty.Register<BetterContentDialog, double>(
"MaxDialogHeight");
public double MaxDialogHeight
{
get => GetValue(MaxDialogHeightProperty);
set => SetValue(MaxDialogHeightProperty, value);
}
public BetterContentDialog()
@ -130,26 +139,46 @@ public class BetterContentDialog : ContentDialog
}
// If commands provided, bind OnCanExecuteChanged to hide buttons
if (PrimaryButtonCommand is not null && PrimaryButton is not null)
// otherwise link visibility to IsEnabled
if (PrimaryButton is not null)
{
PrimaryButtonCommand.CanExecuteChanged += (_, _) =>
if (PrimaryButtonCommand is not null)
{
PrimaryButtonCommand.CanExecuteChanged += (_, _) =>
PrimaryButton.IsEnabled = PrimaryButtonCommand.CanExecute(null);
// Also set initial state
PrimaryButton.IsEnabled = PrimaryButtonCommand.CanExecute(null);
// Also set initial state
PrimaryButton.IsEnabled = PrimaryButtonCommand.CanExecute(null);
}
else
{
PrimaryButton.IsVisible = IsPrimaryButtonEnabled && !string.IsNullOrEmpty(PrimaryButtonText);
}
}
if (SecondaryButtonCommand is not null && SecondaryButton is not null)
if (SecondaryButton is not null)
{
SecondaryButtonCommand.CanExecuteChanged += (_, _) =>
if (SecondaryButtonCommand is not null)
{
SecondaryButtonCommand.CanExecuteChanged += (_, _) =>
SecondaryButton.IsEnabled = SecondaryButtonCommand.CanExecute(null);
// Also set initial state
SecondaryButton.IsEnabled = SecondaryButtonCommand.CanExecute(null);
// Also set initial state
SecondaryButton.IsEnabled = SecondaryButtonCommand.CanExecute(null);
}
else
{
SecondaryButton.IsVisible = IsSecondaryButtonEnabled && !string.IsNullOrEmpty(SecondaryButtonText);
}
}
if (CloseButtonCommand is not null && CloseButton is not null)
if (CloseButton is not null)
{
CloseButtonCommand.CanExecuteChanged += (_, _) =>
if (CloseButtonCommand is not null)
{
CloseButtonCommand.CanExecuteChanged += (_, _) =>
CloseButton.IsEnabled = CloseButtonCommand.CanExecute(null);
// Also set initial state
CloseButton.IsEnabled = CloseButtonCommand.CanExecute(null);
// Also set initial state
CloseButton.IsEnabled = CloseButtonCommand.CanExecute(null);
}
}
}
@ -176,11 +205,16 @@ public class BetterContentDialog : ContentDialog
var border = VisualChildren[0] as Border;
var panel = border?.Child as Panel;
var faBorder = panel?.Children[0] as FABorder;
// Set widths
// Set dialog maximums
if (MaxDialogWidth > 0)
{
faBorder!.MaxWidth = MaxDialogWidth;
}
if (MaxDialogHeight > 0)
{
faBorder!.MaxHeight = MaxDialogHeight;
}
var border2 = faBorder?.Child as Border;
// Named Grid 'DialogSpace'

24
StabilityMatrix.Avalonia/DialogHelper.cs

@ -9,6 +9,7 @@ using Avalonia.Data;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using FluentAvalonia.UI.Controls;
using Markdown.Avalonia;
using StabilityMatrix.Avalonia.Controls;
namespace StabilityMatrix.Avalonia;
@ -23,7 +24,7 @@ public static class DialogHelper
string description,
IReadOnlyList<TextBoxField> textFields)
{
Dispatcher.UIThread.CheckAccess();
Dispatcher.UIThread.VerifyAccess();
var stackPanel = new StackPanel();
var grid = new Grid
@ -110,6 +111,27 @@ public static class DialogHelper
DefaultButton = ContentDialogButton.Primary
};
}
/// <summary>
/// Create a generic dialog for showing a markdown document
/// </summary>
public static BetterContentDialog CreateMarkdownDialog(string markdown, string? title = null)
{
Dispatcher.UIThread.VerifyAccess();
var viewer = new MarkdownScrollViewer
{
Markdown = markdown
};
return new BetterContentDialog
{
Title = title,
Content = viewer,
CloseButtonText = "Close",
IsPrimaryButtonEnabled = false,
};
}
}
// Text fields

5
StabilityMatrix.Avalonia/Models/AvaloniaResource.cs

@ -29,6 +29,11 @@ public readonly record struct AvaloniaResource(
{
}
/// <summary>
/// Opens a stream to this resource.
/// </summary>
public Stream Open() => AssetLoader.Open(UriPath);
/// <summary>
/// Extracts this resource to a target file path.
/// </summary>

44
StabilityMatrix.Avalonia/ViewModels/SettingsViewModel.cs

@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls.Notifications;
@ -16,6 +18,7 @@ using StabilityMatrix.Avalonia.Views;
using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Python;
using StabilityMatrix.Core.Services;
using Symbol = FluentIcons.Common.Symbol;
@ -237,6 +240,47 @@ public partial class SettingsViewModel : PageViewModelBase
}
}
[RelayCommand]
private async Task ShowLicensesDialog()
{
try
{
var markdown = GetLicensesMarkdown();
var dialog = DialogHelper.CreateMarkdownDialog(markdown, "Licenses");
dialog.MaxDialogHeight = 600;
await dialog.ShowAsync();
}
catch (Exception e)
{
notificationService.Show("Failed to read licenses information",
$"{e}", NotificationType.Error);
}
}
private static string GetLicensesMarkdown()
{
// Read licenses.json
using var reader = new StreamReader(Assets.LicensesJson.Open());
var licenses = JsonSerializer
.Deserialize<IReadOnlyList<LicenseInfo>>(reader.ReadToEnd()) ??
throw new InvalidOperationException("Failed to read licenses.json");
// Generate markdown
var builder = new StringBuilder();
foreach (var license in licenses)
{
builder.AppendLine($"## [{license.PackageName}]({license.PackageUrl}) by {string.Join(", ", license.Authors)}");
builder.AppendLine();
builder.AppendLine(license.Description);
builder.AppendLine();
builder.AppendLine($"[{license.LicenseUrl}]({license.LicenseUrl})");
builder.AppendLine();
}
return builder.ToString();
}
#endregion
}

1
StabilityMatrix.Avalonia/Views/SettingsPage.axaml

@ -181,6 +181,7 @@
<StackPanel HorizontalAlignment="Left" Orientation="Horizontal">
<Button
Content="License and Open Source Notices"
Command="{Binding ShowLicensesDialogCommand}"
HorizontalAlignment="Left"
Margin="8" />
</StackPanel>

Loading…
Cancel
Save