Open URL in Family Tree sample on all platforms (desktop, browser, android)

This commit is contained in:
Olivier Azeau 2023-10-07 23:45:34 +02:00
Родитель efd50f393f
Коммит add541511a
7 изменённых файлов: 95 добавлений и 49 удалений

Просмотреть файл

@ -1,5 +1,7 @@
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.Net;
using Avalonia;
using Avalonia.Android;
@ -16,6 +18,14 @@ public class MainActivity : AvaloniaMainActivity<App>
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{
return base.CustomizeAppBuilder(builder)
.WithInterFont();
.WithInterFont()
.SetUrlOpener(OpenUrl);
}
private void OpenUrl(string url)
{
var uri = Uri.Parse (url);
var intent = new Intent (Intent.ActionView, uri);
StartActivity(intent);
}
}

Просмотреть файл

@ -1,20 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<WasmMainJSPath>AppBundle\main.js</WasmMainJSPath>
<OutputType>Exe</OutputType>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<WasmMainJSPath>AppBundle\main.js</WasmMainJSPath>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<WasmExtraFilesToDeploy Include="AppBundle\**"/>
</ItemGroup>
<ItemGroup>
<WasmExtraFilesToDeploy Include="AppBundle\**"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia.Browser" Version="$(AvaloniaVersion)"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Avalonia.Browser" Version="$(AvaloniaVersion)"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\sample\AvaloniaGraphControlSample.csproj"/>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\sample\AvaloniaGraphControlSample.csproj"/>
</ItemGroup>
</Project>

Просмотреть файл

@ -1,4 +1,5 @@
using System.Runtime.Versioning;
using System.Runtime.InteropServices.JavaScript;
using System.Runtime.Versioning;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Browser;
@ -12,6 +13,14 @@ internal partial class Program
.WithInterFont()
.StartBrowserAppAsync("out");
public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>();
public static AppBuilder BuildAvaloniaApp() => AppBuilder
.Configure<App>()
.SetUrlOpener(Interop.OpenUrl);
}
[SupportedOSPlatform("browser")]
internal static partial class Interop
{
[JSImport("globalThis.open")]
public static partial void OpenUrl(string url);
}

Просмотреть файл

@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using Avalonia;
namespace AvaloniaGraphControlSample.Desktop;
@ -17,5 +18,14 @@ class Program
=> AppBuilder.Configure<App>()
.UsePlatformDetect()
.WithInterFont()
.LogToTrace();
.LogToTrace()
.SetUrlOpener(OpenUrl);
private static void OpenUrl(string url)
{
using var proc = new Process();
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.FileName = url;
proc.Start();
}
}

Просмотреть файл

@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
@ -7,6 +8,12 @@ namespace AvaloniaGraphControlSample
{
public class App : Application
{
private Action<string> _openUrl = _ => {};
public void SetUrlOpener(Action<string> openUrl)
{
_openUrl = openUrl;
}
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
@ -19,18 +26,24 @@ namespace AvaloniaGraphControlSample
{
desktop.MainWindow = new MainWindow()
{
DataContext = new Model()
DataContext = new Model(_openUrl)
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{
singleViewPlatform.MainView = new MainView
{
DataContext = new Model()
DataContext = new Model(_openUrl)
};
}
base.OnFrameworkInitializationCompleted();
}
}
public static class ConfigureApp
{
public static AppBuilder SetUrlOpener(this AppBuilder builder, Action<string> openUrl) =>
builder.AfterSetup(b => ((App)b.Instance!).SetUrlOpener(openUrl));
}
}

Просмотреть файл

@ -14,6 +14,13 @@ namespace AvaloniaGraphControlSample
class Model
{
private readonly Action<string> openUrl;
public Model(Action<string> openUrl)
{
this.openUrl = openUrl;
}
public IEnumerable<NamedGraph> SampleGraphs => new NamedGraph[] {
new SimpleGraph(),
new SimpleOrderedLayoutGraph(),
@ -21,7 +28,7 @@ namespace AvaloniaGraphControlSample
new SimpleWithSubgraph(),
new SimpleOrderedLayoutWithSubgraph(),
new ColoredEdges(),
new FamilyTree(),
new FamilyTree(openUrl),
new StateMachine(Graph.Orientations.Vertical),
new StateMachine(Graph.Orientations.Horizontal),
new StateMachineManyComposites(Graph.Orientations.Vertical),

Просмотреть файл

@ -1,3 +1,4 @@
using System;
using System.Linq;
using Avalonia.Media;
using AvaloniaGraphControl;
@ -6,32 +7,27 @@ namespace AvaloniaGraphControlSample
{
class FamilyMember
{
public FamilyMember(string name, IBrush backgroungColor, string url)
private readonly Action onClick;
public FamilyMember(string name, IBrush backgroungColor, Action onClick)
{
this.onClick = onClick;
Name = name;
BackgroundColor = backgroungColor;
URL = url;
}
public void Navigate()
{
if (System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux))
System.Diagnostics.Process.Start("xdg-open", URL);
else
System.Diagnostics.Process.Start(URL);
}
public void Navigate() => onClick();
public string Name { get; private set; }
public IBrush BackgroundColor { get; private set; }
public string URL { get; private set; }
}
class Male : FamilyMember
{
public Male(string name, string url) : base(name, Brushes.LightSkyBlue, url) { }
public Male(string name, Action onClick) : base(name, Brushes.LightSkyBlue, onClick) { }
}
class Female : FamilyMember
{
public Female(string name, string url) : base(name, Brushes.LightPink, url) { }
public Female(string name, Action onClick) : base(name, Brushes.LightPink, onClick) { }
}
class Family
{
@ -39,21 +35,21 @@ namespace AvaloniaGraphControlSample
class FamilyTree : NamedGraph
{
public FamilyTree() : base("Family Tree")
public FamilyTree(Action<string> openUrl) : base("Family Tree")
{
Orientation = Orientations.Vertical;
var abraham = new Male("Abraham", "https://en.wikipedia.org/wiki/Grampa_Simpson");
var mona = new Female("Mona", "https://en.wikipedia.org/wiki/Mona_Simpson_(The_Simpsons)");
var homer = new Male("Homer", "https://en.wikipedia.org/wiki/Homer_Simpson");
var clancy = new Male("Clancy", "https://en.wikipedia.org/wiki/Simpson_family#Clancy_Bouvier");
var jackie = new Female("Jackie", "https://en.wikipedia.org/wiki/Simpson_family#Jacqueline_Bouvier");
var marge = new Female("Marge", "https://en.wikipedia.org/wiki/Marge_Simpson");
var patty = new Female("Patty", "https://en.wikipedia.org/wiki/Patty_and_Selma");
var selma = new Female("Selma", "https://en.wikipedia.org/wiki/Patty_and_Selma");
var ling = new Female("Ling", "https://en.wikipedia.org/wiki/Simpson_family#Ling_Bouvier");
var bart = new Male("Bart", "https://en.wikipedia.org/wiki/Bart_Simpson");
var lisa = new Female("Lisa", "https://en.wikipedia.org/wiki/Lisa_Simpson");
var maggie = new Female("Maggie", "https://en.wikipedia.org/wiki/Maggie_Simpson");
var abraham = new Male("Abraham", () => openUrl("https://en.wikipedia.org/wiki/Grampa_Simpson"));
var mona = new Female("Mona", () => openUrl("https://en.wikipedia.org/wiki/Mona_Simpson_(The_Simpsons)"));
var homer = new Male("Homer", () => openUrl("https://en.wikipedia.org/wiki/Homer_Simpson"));
var clancy = new Male("Clancy", () => openUrl("https://en.wikipedia.org/wiki/Simpson_family#Clancy_Bouvier"));
var jackie = new Female("Jackie", () => openUrl("https://en.wikipedia.org/wiki/Simpson_family#Jacqueline_Bouvier"));
var marge = new Female("Marge", () => openUrl("https://en.wikipedia.org/wiki/Marge_Simpson"));
var patty = new Female("Patty", () => openUrl("https://en.wikipedia.org/wiki/Patty_and_Selma"));
var selma = new Female("Selma", () => openUrl("https://en.wikipedia.org/wiki/Patty_and_Selma"));
var ling = new Female("Ling", () => openUrl("https://en.wikipedia.org/wiki/Simpson_family#Ling_Bouvier"));
var bart = new Male("Bart", () => openUrl("https://en.wikipedia.org/wiki/Bart_Simpson"));
var lisa = new Female("Lisa", () => openUrl("https://en.wikipedia.org/wiki/Lisa_Simpson"));
var maggie = new Female("Maggie", () => openUrl("https://en.wikipedia.org/wiki/Maggie_Simpson"));
var f1 = new Family();
var f2 = new Family();
var f3 = new Family();