Skip to content

Commit

Permalink
make calc app cross platform and run in browser.
Browse files Browse the repository at this point in the history
  • Loading branch information
danwalmsley committed Oct 20, 2022
1 parent 5787104 commit 11b925d
Show file tree
Hide file tree
Showing 35 changed files with 416 additions and 63 deletions.
Binary file added .DS_Store
Binary file not shown.
Binary file added Calc.Browser/.DS_Store
Binary file not shown.
5 changes: 5 additions & 0 deletions Calc.Browser/AppBundle/Logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
73 changes: 73 additions & 0 deletions Calc.Browser/AppBundle/app.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
.highlight {
color: white;
font-size: 2.5rem;
display: block;
}

.purple {
color: #8b44ac;
}

.icon {
opacity : 0.05;
height: 35%;
width: 35%;
position : absolute;
background-repeat: no-repeat;
right: 0px;
bottom: 0px;
margin-right: 3%;
margin-bottom: 5%;
z-index: 5000;
background-position: right bottom;
pointer-events: none;
}

#out {
display: flex;
justify-content: center;
align-items: center;
text-align: center;
height: 100vh;
overflow:hidden;
}

#avalonia-splash a{
color: whitesmoke;
text-decoration: none;
}

.center {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}

#avalonia-splash {
position: relative;
height: 100%;
width: 100%;
color: whitesmoke;
background: #1b2a4e;
font-family: 'Nunito', sans-serif;
background-position: center;
background-size: cover;
background-repeat: no-repeat;
justify-content: center;
align-items: center;
}

.splash-close {
animation: fadeout 0.5s linear 1s forwards;
}

@keyframes fadeout {
0% {
opacity:100%;
}
100% {
opacity:0;
visibility: collapse;
}
}
File renamed without changes.
33 changes: 33 additions & 0 deletions Calc.Browser/AppBundle/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!DOCTYPE html>
<html>

<head>
<title>Avalonia UI Playground</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<base href="/" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Nunito:wght@200;300;400;500;600;700&family=Open+Sans:wght@600&display=swap" rel="stylesheet">
<link rel="modulepreload" href="./main.js" />
<link rel="modulepreload" href="./interop.js" />
<link rel="modulepreload" href="./dotnet.js" />
<link rel="modulepreload" href="./avalonia.js" />
<link rel="stylesheet" href="./app.css" />
<link rel="preconnect" href="https://fonts.googleapis.com">
</head>

<body style="margin: 0px">
<div id="out">
<img class="icon" src="Logo.svg" alt="Avalonia Logo"/>
<div id="avalonia-splash">
<div class="center">
<h2 class="purple">
Powered by
<a class="highlight" href="https://www.avaloniaui.net/" target="_blank">Avalonia UI</a></h2>
</div>
</div>
</div>
<script type='module' src="./main.js"></script>
</body>

</html>
16 changes: 16 additions & 0 deletions Calc.Browser/AppBundle/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { dotnet } from './dotnet.js'
import { registerAvaloniaModule } from './avalonia.js';

const is_browser = typeof window != "undefined";
if (!is_browser) throw new Error(`Expected to be running in a browser`);

const dotnetRuntime = await dotnet
.withDiagnosticTracing(false)
.withApplicationArgumentsFromQuery()
.create();

await registerAvaloniaModule(dotnetRuntime);

const config = dotnetRuntime.getConfig();

await dotnetRuntime.runMainAndExit(config.mainAssemblyName, [window.location.search]);
45 changes: 45 additions & 0 deletions Calc.Browser/Calc.Browser.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<RuntimeIdentifier>browser-wasm</RuntimeIdentifier>
<WasmMainJSPath>AppBundle\main.js</WasmMainJSPath>
<OutputType>Exe</OutputType>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<MSBuildEnableWorkloadResolver>true</MSBuildEnableWorkloadResolver>
<WasmBuildNative>true</WasmBuildNative>
<EmccFlags>-sVERBOSE -sERROR_ON_UNDEFINED_SYMBOLS=0</EmccFlags>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
<RunAOTCompilation>true</RunAOTCompilation>
<PublishTrimmed>true</PublishTrimmed>
<TrimMode>full</TrimMode>
<WasmBuildNative>true</WasmBuildNative>
<InvariantGlobalization>true</InvariantGlobalization>
<WasmEnableSIMD>true</WasmEnableSIMD>
<EmccCompileOptimizationFlag>-O3</EmccCompileOptimizationFlag>
<EmccLinkOptimizationFlag>-O3</EmccLinkOptimizationFlag>
</PropertyGroup>

<ItemGroup>
<TrimmerRootDescriptor Include="Roots.xml" />
</ItemGroup>

<ItemGroup>
<WasmExtraFilesToDeploy Include="AppBundle\app.css" />
<WasmExtraFilesToDeploy Include="AppBundle\favicon.ico" />
<WasmExtraFilesToDeploy Include="AppBundle\index.html" />
<WasmExtraFilesToDeploy Include="AppBundle\Logo.svg" />
<WasmExtraFilesToDeploy Include="AppBundle\main.js" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Avalonia.Web" Version="$(AvaloniaVersion)" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Calc\Calc.csproj" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Calc.Browser/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Web;
using Calc;

[assembly:SupportedOSPlatform("browser")]

internal partial class Program
{
private static void Main(string[] args) =>
BuildAvaloniaApp()
.SetupBrowserApp("out");

public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>();
}
43 changes: 43 additions & 0 deletions Calc.Browser/Roots.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<linker>

<!-- App -->

<assembly fullname="XamlPlayground" preserve="All" />
<assembly fullname="XamlPlayground.Wasm" preserve="All" />

<!-- Avalonia Themes -->

<assembly fullname="Avalonia.Themes.Fluent" preserve="All" />
<assembly fullname="Avalonia.Themes.Simple" preserve="All" />

<!-- Avalonia Core -->

<assembly fullname="Avalonia" preserve="All" />
<assembly fullname="Avalonia.Base" preserve="All" />
<assembly fullname="Avalonia.Controls" preserve="All" />
<assembly fullname="Avalonia.Dialogs" preserve="All" />
<assembly fullname="Avalonia.Markup" preserve="All" />
<assembly fullname="Avalonia.Markup.Xaml" preserve="All" />
<assembly fullname="Avalonia.Markup.Xaml.Loader" preserve="All" />
<assembly fullname="Avalonia.MicroCom" preserve="All" />

<!-- Avalonia Web -->

<assembly fullname="Avalonia.OpenGL" preserve="All" />
<assembly fullname="Avalonia.Skia" preserve="All" />
<assembly fullname="Avalonia.Web" preserve="All" />

<!-- Avalonia Behaviors -->

<assembly fullname="Avalonia.Xaml.Interactions" preserve="All" />
<assembly fullname="Avalonia.Xaml.Interactivity" preserve="All" />

<!-- AvaloniaEdit -->

<assembly fullname="AvaloniaEdit" preserve="All" />

<!-- AvaloniaEdit -->

<assembly fullname="Octokit" preserve="All" />

</linker>
11 changes: 11 additions & 0 deletions Calc.Browser/runtimeconfig.template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"wasmHostProperties": {
"perHostConfig": [
{
"name": "browser",
"html-path": "index.html",
"Host": "browser"
}
]
}
}
14 changes: 14 additions & 0 deletions Calc.Desktop/Calc.Desktop.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net7.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>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**"/>
<PackageReference Include="Avalonia.Desktop" Version="$(AvaloniaVersion)"/>
<ProjectReference Include="..\Calc\Calc.csproj"/>
</ItemGroup>
</Project>
File renamed without changes.
32 changes: 0 additions & 32 deletions Calc.csproj

This file was deleted.

41 changes: 41 additions & 0 deletions Calc.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calc", "Calc\Calc.csproj", "{2BADC6A0-42BC-4C6D-B97F-62485C051DFC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calc.Desktop", "Calc.Desktop\Calc.Desktop.csproj", "{A1D52ADE-D1A1-4E48-9212-96DA6F69987C}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionFiles", "SolutionFiles", "{7B0CD07C-A43F-41CE-9A81-9DC22B8F2578}"
ProjectSection(SolutionItems) = preProject
global.json = global.json
Directory.Build.props = Directory.Build.props
nuget.config = nuget.config
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Calc.Browser", "Calc.Browser\Calc.Browser.csproj", "{45063CD8-19B9-4241-9C02-2CD904BED0FC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{2BADC6A0-42BC-4C6D-B97F-62485C051DFC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2BADC6A0-42BC-4C6D-B97F-62485C051DFC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2BADC6A0-42BC-4C6D-B97F-62485C051DFC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2BADC6A0-42BC-4C6D-B97F-62485C051DFC}.Release|Any CPU.Build.0 = Release|Any CPU
{A1D52ADE-D1A1-4E48-9212-96DA6F69987C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1D52ADE-D1A1-4E48-9212-96DA6F69987C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1D52ADE-D1A1-4E48-9212-96DA6F69987C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1D52ADE-D1A1-4E48-9212-96DA6F69987C}.Release|Any CPU.Build.0 = Release|Any CPU
{45063CD8-19B9-4241-9C02-2CD904BED0FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{45063CD8-19B9-4241-9C02-2CD904BED0FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{45063CD8-19B9-4241-9C02-2CD904BED0FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{45063CD8-19B9-4241-9C02-2CD904BED0FC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Binary file added Calc/.DS_Store
Binary file not shown.
File renamed without changes.
9 changes: 8 additions & 1 deletion App.axaml.cs → Calc/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ public override void OnFrameworkInitializationCompleted()
{
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
DataContext = new MainWindowViewModel()
};
}
else if (ApplicationLifetime is ISingleViewApplicationLifetime singleView)
{
singleView.MainView = new MainView
{
DataContext = new MainWindowViewModel()
};
}

Expand Down
Binary file added Calc/Assets/avalonia-logo.ico
Binary file not shown.
17 changes: 17 additions & 0 deletions Calc/Calc.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
<TrimmableAssembly Include="Avalonia.Themes.Fluent" />
<TrimmableAssembly Include="Avalonia.Themes.Default" />
<PackageReference Include="Avalonia" Version="$(AvaloniaVersion)" />
<PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="$(AvaloniaVersion)" />
<PackageReference Include="Avalonia.ReactiveUI" Version="$(AvaloniaVersion)" />
<PackageReference Include="Material.Avalonia" Version="3.0.0-avalonia11-preview2" />
<PackageReference Include="Material.Icons.Avalonia" Version="1.2.0" />
<PackageReference Include="XamlNameReferenceGenerator" Version="1.3.4" />
</ItemGroup>
</Project>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 11b925d

Please sign in to comment.