Skip to content

Commit

Permalink
Add Avalonia licenses control
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed Jul 29, 2024
1 parent c02fc70 commit aad8f43
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Nullable>enable</Nullable>
<AvaloniaVersion>11.0.11</AvaloniaVersion>
<AvaloniaVersion>11.1.0</AvaloniaVersion>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Vocup.WinForms/Forms/AboutBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public AboutBox()
{
InitializeComponent();
Icon = Icon.FromHandle(Icons.Info.GetHicon());
AvaloniaControlHost.Content = new MainView { DataContext = new MainViewModel() };
AvaloniaControlHost.Content = new LicensesControl { DataContext = new LicensesViewModel() };
}

private void AboutBox_Load(object sender, EventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion src/Vocup.WinForms/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia;
using Avalonia.ReactiveUI;
using System;
using System.Diagnostics;
using System.Drawing;
Expand Down Expand Up @@ -52,7 +53,7 @@ private static void Main(string[] args)
splash.Show();
Application.DoEvents();

AppBuilder.Configure<App>().UseWin32().UseSkia().SetupWithoutStarting();
AppBuilder.Configure<App>().UseWin32().UseSkia().UseReactiveUI().SetupWithoutStarting();

var serviceScope = InitializeServices();

Expand Down
62 changes: 62 additions & 0 deletions src/Vocup/ViewModels/LicensesViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using ReactiveUI;
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Linq;

namespace Vocup.ViewModels;

public class LicensesViewModel
{
public LicensesViewModel()
{
NavigateToUri = ReactiveCommand.Create<string>(async uri =>
{
await LaunchUri.Handle(new Uri(uri));
});
}

public ReactiveCommand<string, Unit> NavigateToUri { get; }

public Interaction<Uri, bool> LaunchUri { get; } = new();

public List<Component> Components { get; } = [
new()
{
Name = "Avalonia",
License = "",
Url = "https://avaloniaui.net"
},
new()
{
Name = "CsvHelper",
License = "MS-PL; Apache-2.0",
Url = "https://joshclose.github.io/CsvHelper",
},
new()
{
Name = "CsWin32",
License = "MIT",
Url = "https://github.com/microsoft/CsWin32",
},
new()
{
Name = "Losttech Settings",
License = "Apache-2.0",
Url = "https://github.com/losttech/Settings",
},
new()
{
Name = "Office Icons",
License = "CC BY-ND 3.0",
Url = "https://icons8.com/icons/office",
},
];

public class Component
{
public required string Name { get; init; }
public required string License { get; init; }
public required string Url { get; init; }
}
}
2 changes: 1 addition & 1 deletion src/Vocup/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

public class MainViewModel : ViewModelBase
{
public string Greeting => "Welcome to Avalonia!";
public LicensesViewModel Licenses { get; } = new();
}
38 changes: 38 additions & 0 deletions src/Vocup/Views/LicensesControl.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Vocup.ViewModels;assembly=Vocup.Avalonia"
mc:Ignorable="d" d:DesignWidth="327" d:DesignHeight="244"
x:Class="Vocup.Views.LicensesControl"
x:DataType="vm:LicensesViewModel"
Name="control">
<Design.DataContext>
<vm:LicensesViewModel />
</Design.DataContext>

<UserControl.Styles>
<Style Selector="HyperlinkButton.link">
<Setter Property="CornerRadius" Value="0" />
<Setter Property="Padding" Value="0" />
</Style>
<Style Selector="HyperlinkButton.link:pointerover">
<Setter Property="Background" Value="Transparent" />
</Style>
</UserControl.Styles>

<ItemsControl ItemsSource="{Binding Components}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel>
<HyperlinkButton Classes="link" Content="{Binding Name}" Command="{Binding #control.ViewModel.NavigateToUri}" CommandParameter="{Binding Url}">
<ToolTip>
<TextBlock Text="{Binding Url}" />
</ToolTip>
</HyperlinkButton>
<TextBlock Text="{Binding License}" />
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</UserControl>
22 changes: 22 additions & 0 deletions src/Vocup/Views/LicensesControl.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Avalonia.Controls;
using Avalonia.ReactiveUI;
using ReactiveUI;
using Vocup.ViewModels;

namespace Vocup.Views
{
public partial class LicensesControl : ReactiveUserControl<LicensesViewModel>
{
public LicensesControl()
{
InitializeComponent();

this.WhenActivated(d => d(ViewModel.LaunchUri.RegisterHandler(async interaction =>

Check warning on line 14 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Dereference of a possibly null reference.

Check warning on line 14 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 14 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 14 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 14 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
{
var topLevel = TopLevel.GetTopLevel(this);
bool success = await topLevel.Launcher.LaunchUriAsync(interaction.Input);

Check warning on line 17 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Debug)

Dereference of a possibly null reference.

Check warning on line 17 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 17 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 17 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.

Check warning on line 17 in src/Vocup/Views/LicensesControl.axaml.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
interaction.SetOutput(success);
})));
}
}
}
5 changes: 3 additions & 2 deletions src/Vocup/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Vocup.ViewModels"
xmlns:views="clr-namespace:Vocup.Views;assembly=Vocup.Avalonia"
xmlns:vm="clr-namespace:Vocup.ViewModels;assembly=Vocup.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Vocup.Views.MainView"
x:DataType="vm:MainViewModel">
Expand All @@ -12,5 +13,5 @@
<vm:MainViewModel />
</Design.DataContext>

<TextBlock Text="{Binding Greeting}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
<views:LicensesControl DataContext="{Binding Licenses}" />
</UserControl>

0 comments on commit aad8f43

Please sign in to comment.