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.App;
using Android.Content;
using Android.Content.PM; using Android.Content.PM;
using Android.Net;
using Avalonia; using Avalonia;
using Avalonia.Android; using Avalonia.Android;
@ -16,6 +18,14 @@ public class MainActivity : AvaloniaMainActivity<App>
protected override AppBuilder CustomizeAppBuilder(AppBuilder builder) protected override AppBuilder CustomizeAppBuilder(AppBuilder builder)
{ {
return base.CustomizeAppBuilder(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"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net7.0</TargetFramework> <TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier> <RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<WasmMainJSPath>AppBundle\main.js</WasmMainJSPath> <WasmMainJSPath>AppBundle\main.js</WasmMainJSPath>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
</PropertyGroup> <AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<WasmExtraFilesToDeploy Include="AppBundle\**"/> <WasmExtraFilesToDeploy Include="AppBundle\**"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Avalonia.Browser" Version="$(AvaloniaVersion)"/> <PackageReference Include="Avalonia.Browser" Version="$(AvaloniaVersion)"/>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\sample\AvaloniaGraphControlSample.csproj"/> <ProjectReference Include="..\sample\AvaloniaGraphControlSample.csproj"/>
</ItemGroup> </ItemGroup>
</Project> </Project>

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

@ -1,4 +1,5 @@
using System.Runtime.Versioning; using System.Runtime.InteropServices.JavaScript;
using System.Runtime.Versioning;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia; using Avalonia;
using Avalonia.Browser; using Avalonia.Browser;
@ -12,6 +13,14 @@ internal partial class Program
.WithInterFont() .WithInterFont()
.StartBrowserAppAsync("out"); .StartBrowserAppAsync("out");
public static AppBuilder BuildAvaloniaApp() public static AppBuilder BuildAvaloniaApp() => AppBuilder
=> AppBuilder.Configure<App>(); .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;
using System.Diagnostics;
using Avalonia; using Avalonia;
namespace AvaloniaGraphControlSample.Desktop; namespace AvaloniaGraphControlSample.Desktop;
@ -17,5 +18,14 @@ class Program
=> AppBuilder.Configure<App>() => AppBuilder.Configure<App>()
.UsePlatformDetect() .UsePlatformDetect()
.WithInterFont() .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;
using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml; using Avalonia.Markup.Xaml;
@ -7,6 +8,12 @@ namespace AvaloniaGraphControlSample
{ {
public class App : Application public class App : Application
{ {
private Action<string> _openUrl = _ => {};
public void SetUrlOpener(Action<string> openUrl)
{
_openUrl = openUrl;
}
public override void Initialize() public override void Initialize()
{ {
AvaloniaXamlLoader.Load(this); AvaloniaXamlLoader.Load(this);
@ -19,18 +26,24 @@ namespace AvaloniaGraphControlSample
{ {
desktop.MainWindow = new MainWindow() desktop.MainWindow = new MainWindow()
{ {
DataContext = new Model() DataContext = new Model(_openUrl)
}; };
} }
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform) else if (ApplicationLifetime is ISingleViewApplicationLifetime singleViewPlatform)
{ {
singleViewPlatform.MainView = new MainView singleViewPlatform.MainView = new MainView
{ {
DataContext = new Model() DataContext = new Model(_openUrl)
}; };
} }
base.OnFrameworkInitializationCompleted(); 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 class Model
{ {
private readonly Action<string> openUrl;
public Model(Action<string> openUrl)
{
this.openUrl = openUrl;
}
public IEnumerable<NamedGraph> SampleGraphs => new NamedGraph[] { public IEnumerable<NamedGraph> SampleGraphs => new NamedGraph[] {
new SimpleGraph(), new SimpleGraph(),
new SimpleOrderedLayoutGraph(), new SimpleOrderedLayoutGraph(),
@ -21,7 +28,7 @@ namespace AvaloniaGraphControlSample
new SimpleWithSubgraph(), new SimpleWithSubgraph(),
new SimpleOrderedLayoutWithSubgraph(), new SimpleOrderedLayoutWithSubgraph(),
new ColoredEdges(), new ColoredEdges(),
new FamilyTree(), new FamilyTree(openUrl),
new StateMachine(Graph.Orientations.Vertical), new StateMachine(Graph.Orientations.Vertical),
new StateMachine(Graph.Orientations.Horizontal), new StateMachine(Graph.Orientations.Horizontal),
new StateMachineManyComposites(Graph.Orientations.Vertical), new StateMachineManyComposites(Graph.Orientations.Vertical),

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

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