Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
r.sorokin committed Jun 21, 2022
0 parents commit 6504f63
Show file tree
Hide file tree
Showing 19 changed files with 893 additions and 0 deletions.
454 changes: 454 additions & 0 deletions HyperText.Avalonia.Example/.gitignore

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions HyperText.Avalonia.Example/App.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:HyperText.Avalonia.Example"
xmlns:avalonia="clr-namespace:HyperText.Avalonia;assembly=HyperText.Avalonia"
x:Class="HyperText.Avalonia.Example.App">
<Application.DataTemplates>
<local:ViewLocator/>
</Application.DataTemplates>

<Application.Styles>
<FluentTheme Mode="Light"/>


</Application.Styles>
</Application>
29 changes: 29 additions & 0 deletions HyperText.Avalonia.Example/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using HyperText.Avalonia.Example.ViewModels;
using HyperText.Avalonia.Example.Views;

namespace HyperText.Avalonia.Example
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}

base.OnFrameworkInitializationCompleted();
}
}
}
Binary file not shown.
33 changes: 33 additions & 0 deletions HyperText.Avalonia.Example/HyperText.Avalonia.Example.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<!--Avalonia doesen't support TrimMode=link currently,but we are working on that https://github.com/AvaloniaUI/Avalonia/issues/6892 -->
<TrimMode>copyused</TrimMode>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
</PropertyGroup>
<ItemGroup>
<Folder Include="Models\" />
<AvaloniaResource Include="Assets\**" />
<None Remove=".gitignore" />
</ItemGroup>
<ItemGroup>
<!--This helps with theme dll-s trimming.
If you will publish your application in self-contained mode with p:PublishTrimmed=true and it will use Fluent theme Default theme will be trimmed from the output and vice versa.
https://github.com/AvaloniaUI/Avalonia/issues/5593 -->
<TrimmableAssembly Include="Avalonia.Themes.Fluent" />
<TrimmableAssembly Include="Avalonia.Themes.Default" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="0.10.15" />
<PackageReference Include="Avalonia.Desktop" Version="0.10.15" />
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="0.10.15" />
<PackageReference Include="Avalonia.ReactiveUI" Version="0.10.15" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HyperText.Avalonia\HyperText.Avalonia.csproj" />
</ItemGroup>
</Project>
23 changes: 23 additions & 0 deletions HyperText.Avalonia.Example/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Avalonia;
using Avalonia.ReactiveUI;
using System;

namespace HyperText.Avalonia.Example
{
class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
// SynchronizationContext-reliant code before AppMain is called: things aren't initialized
// yet and stuff might break.
[STAThread]
public static void Main(string[] args) => BuildAvaloniaApp()
.StartWithClassicDesktopLifetime(args);

// Avalonia configuration, don't remove; also used by visual designer.
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.LogToTrace()
.UseReactiveUI();
}
}
28 changes: 28 additions & 0 deletions HyperText.Avalonia.Example/ViewLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using HyperText.Avalonia.Example.ViewModels;

namespace HyperText.Avalonia.Example
{
public class ViewLocator : IDataTemplate
{
public IControl Build(object data)
{
var name = data.GetType().FullName!.Replace("ViewModel", "View");
var type = Type.GetType(name);

if (type != null)
{
return (Control)Activator.CreateInstance(type)!;
}

return new TextBlock { Text = "Not Found: " + name };
}

public bool Match(object data)
{
return data is ViewModelBase;
}
}
}
16 changes: 16 additions & 0 deletions HyperText.Avalonia.Example/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using HyperText.Avalonia.Models;

namespace HyperText.Avalonia.Example.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
public IEnumerable<HyperlinkContent> HyperlinkContentProvider => new[]
{
new HyperlinkContent
{ Alias = "dedede ", Url = "https://avaloniaui.net/docs/styles/styles" },
new HyperlinkContent { Alias = "edvyydebbvydebvyed" },
new HyperlinkContent { Url = "https://avaloniaui.net/docs/styles/styles" }
};
}
}
8 changes: 8 additions & 0 deletions HyperText.Avalonia.Example/ViewModels/ViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using ReactiveUI;

namespace HyperText.Avalonia.Example.ViewModels
{
public class ViewModelBase : ReactiveObject
{
}
}
33 changes: 33 additions & 0 deletions HyperText.Avalonia.Example/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="using:HyperText.Avalonia.Example.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="clr-namespace:HyperText.Avalonia.Controls;assembly=HyperText.Avalonia"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="HyperText.Avalonia.Example.Views.MainWindow"
Icon="/Assets/avalonia-logo.ico"
Title="HyperText.Avalonia.Example">

<Design.DataContext>
<vm:MainWindowViewModel />
</Design.DataContext>


<Grid RowDefinitions="Auto,*">
<ItemsControl x:Name="myItems" Items="{Binding HyperlinkContentProvider}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:Hyperlink Text="{Binding Alias}" Url="{Binding Url}" />
</DataTemplate>
</ItemsControl.ItemTemplate>

</ItemsControl>
<TextBox Grid.Row="1">xcfsdfsd</TextBox>
</Grid>
</Window>
12 changes: 12 additions & 0 deletions HyperText.Avalonia.Example/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Avalonia.Controls;

namespace HyperText.Avalonia.Example.Views
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
}
22 changes: 22 additions & 0 deletions HyperText.Avalonia.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HyperText.Avalonia", "HyperText.Avalonia\HyperText.Avalonia.csproj", "{650FA614-AE11-4493-A209-FF83C0ADA91C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HyperText.Avalonia.Example", "HyperText.Avalonia.Example\HyperText.Avalonia.Example.csproj", "{78920BF1-F9A2-444E-B007-AE80ED2EC61D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{650FA614-AE11-4493-A209-FF83C0ADA91C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{650FA614-AE11-4493-A209-FF83C0ADA91C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{650FA614-AE11-4493-A209-FF83C0ADA91C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{650FA614-AE11-4493-A209-FF83C0ADA91C}.Release|Any CPU.Build.0 = Release|Any CPU
{78920BF1-F9A2-444E-B007-AE80ED2EC61D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78920BF1-F9A2-444E-B007-AE80ED2EC61D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78920BF1-F9A2-444E-B007-AE80ED2EC61D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78920BF1-F9A2-444E-B007-AE80ED2EC61D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
28 changes: 28 additions & 0 deletions HyperText.Avalonia/Controls/Hyperlink.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using HyperText.Avalonia.Extensions;

namespace HyperText.Avalonia.Controls
{
public class Hyperlink : TextBlock
{
public static readonly DirectProperty<Hyperlink, string> UrlProperty
= AvaloniaProperty.RegisterDirect<Hyperlink, string>(nameof(Url), o => o.Url, (o, v) => o.Url = v);

private string _url;

public string Url
{
get => _url;
set => SetAndRaise(UrlProperty, ref _url, value);
}

protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (!string.IsNullOrEmpty(Url))
Url.OpenUrl();
}
}
}
9 changes: 9 additions & 0 deletions HyperText.Avalonia/Exceptions/InvalidUrlException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace HyperText.Avalonia.Exceptions
{
public class InvalidUrlException : Exception
{
public InvalidUrlException(string message) : base(message)
{
}
}
}
40 changes: 40 additions & 0 deletions HyperText.Avalonia/Extensions/OpenUrl.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using HyperText.Avalonia.Exceptions;

namespace HyperText.Avalonia.Extensions
{
public static class OpenUrlExtension
{
private static bool IsValidUrl(string url)
{
if (string.IsNullOrWhiteSpace(url)) return false;
if (!Uri.IsWellFormedUriString(url, UriKind.Absolute)) return false;
if (!Uri.TryCreate(url, UriKind.Absolute, out var tmp)) return false;
return tmp.Scheme == Uri.UriSchemeHttp || tmp.Scheme == Uri.UriSchemeHttps;
}

public static void OpenUrl(this string url)
{
if (!IsValidUrl(url)) throw new InvalidUrlException("invalid url: " + url);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
//https://stackoverflow.com/a/2796367/241446
using var proc = new Process { StartInfo = { UseShellExecute = true, FileName = url } };
proc.Start();

return;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
Process.Start("x-www-browser", url);
return;
}

if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) throw new InvalidUrlException("invalid url: " + url);
Process.Start("open", url);
return;
}
}
}
Loading

0 comments on commit 6504f63

Please sign in to comment.