Skip to content

Commit

Permalink
Initial work to get unpackaged WinUI apps (#4460)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattleibow authored Feb 16, 2022
1 parent da87755 commit b8bf9f5
Show file tree
Hide file tree
Showing 17 changed files with 162 additions and 24 deletions.
3 changes: 2 additions & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
<MacosTargetFrameworkVersion>10.14</MacosTargetFrameworkVersion>
<AndroidTargetFrameworkVersion>30.0</AndroidTargetFrameworkVersion>
<WindowsTargetFrameworkVersion>10.0.19041</WindowsTargetFrameworkVersion>
<Windows2TargetFrameworkVersion>10.0.20348</Windows2TargetFrameworkVersion>
</PropertyGroup>

<PropertyGroup>
<!-- the real TFMs -->
<MauiPlatforms>net$(_MauiDotNetVersion)-ios;net$(_MauiDotNetVersion)-maccatalyst;net$(_MauiDotNetVersion)-android</MauiPlatforms>
<WindowsMauiPlatforms Condition="'$(WindowsMauiPlatforms)' == ''">net$(_MauiDotNetVersion)-windows$(WindowsTargetFrameworkVersion)</WindowsMauiPlatforms>
<WindowsMauiPlatforms Condition="'$(WindowsMauiPlatforms)' == ''">net$(_MauiDotNetVersion)-windows$(WindowsTargetFrameworkVersion);net$(_MauiDotNetVersion)-windows$(Windows2TargetFrameworkVersion)</WindowsMauiPlatforms>
<MauiPlatforms Condition="'$(IncludeWindowsTargetFrameworks)' == 'true'">$(MauiPlatforms);$(WindowsMauiPlatforms)</MauiPlatforms>

<!-- Work around the IDE not properly handling the NU1703 warning -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PropertyGroup>
<EnablePreviewMsixTooling>true</EnablePreviewMsixTooling>
<WindowsPackageType>MSIX</WindowsPackageType>
<WindowsPackageType Condition=" $(TargetFramework.Contains('-windows10.0.20348')) ">None</WindowsPackageType>
<ApplicationTitle>.NET MAUI Controls</ApplicationTitle>
<ApplicationId>com.microsoft.maui.sample</ApplicationId>
<ApplicationVersion>1</ApplicationVersion>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
{
"profiles": {
"Windows Machine": {
"Windows Machine (on 10.0.19041)": {
"commandName": "MsixPackage",
"nativeDebugging": true
},
"Unpackaged Windows Machine (on 10.0.20348)": {
"commandName": "Project"
}
}
}
5 changes: 4 additions & 1 deletion src/Controls/samples/Controls.Sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,17 @@ public static MauiApp CreateMauiApp()
.ConfigureEssentials(essentials =>
{
essentials
.UseVersionTracking()
.UseMapServiceToken("YOUR-KEY-HERE")
.AddAppAction("test_action", "Test App Action")
.AddAppAction("second_action", "Second App Action")
.OnAppAction(appAction =>
{
Debug.WriteLine($"You seem to have arrived from a special place: {appAction.Title} ({appAction.Id})");
});

// TODO: Unpackaged apps need to know the package ID and local data locations
if (AppInfo.PackagingModel == AppPackagingModel.Packaged)
essentials.UseVersionTracking();
})
.ConfigureLifecycleEvents(events =>
{
Expand Down
18 changes: 8 additions & 10 deletions src/Core/src/Fonts/FontRegistrar.Windows.cs
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
#nullable enable
using System.IO;
using Microsoft.Maui.Essentials;

namespace Microsoft.Maui
{
public partial class FontRegistrar : IFontRegistrar
{
string? LoadNativeAppFont(string font, string filename, string? alias)
{
var root = global::Windows.ApplicationModel.Package.Current.InstalledLocation.Path;

var packagePath = Path.Combine(root, filename);
if (File.Exists(packagePath))
if (FileSystem.AppPackageFileExists(filename))
return $"ms-appx:///{filename}";

packagePath = Path.Combine(root, "Assets", filename);
if (File.Exists(packagePath))
var packagePath = Path.Combine("Assets", filename);
if (FileSystem.AppPackageFileExists(packagePath))
return $"ms-appx:///Assets/{filename}";

packagePath = Path.Combine(root, "Fonts", filename);
if (File.Exists(packagePath))
packagePath = Path.Combine("Fonts", filename);
if (FileSystem.AppPackageFileExists(packagePath))
return $"ms-appx:///Fonts/{filename}";

packagePath = Path.Combine(root, "Assets", "Fonts", filename);
if (File.Exists(packagePath))
packagePath = Path.Combine("Assets", "Fonts", filename);
if (FileSystem.AppPackageFileExists(packagePath))
return $"ms-appx:///Assets/Fonts/{filename}";

// TODO: check other folders as well
Expand Down
2 changes: 2 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,5 +82,7 @@ public AppTheme RequestedTheme
UiMode.NightNo => AppTheme.Light,
_ => AppTheme.Unspecified
};

public AppPackagingModel PackagingModel => AppPackagingModel.Packaged;
}
}
2 changes: 2 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.ios.tvos.watchos.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace Microsoft.Maui.Essentials.Implementations
{
public class AppInfoImplementation : IAppInfo
{
public AppPackagingModel PackagingModel => AppPackagingModel.Packaged;

public string PackageName => GetBundleValue("CFBundleIdentifier");

public string Name => GetBundleValue("CFBundleDisplayName") ?? GetBundleValue("CFBundleName");
Expand Down
2 changes: 2 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ public class AppInfoImplementation : IAppInfo
public void ShowSettingsUI() => throw ExceptionUtils.NotSupportedOrImplementedException;

public AppTheme RequestedTheme => throw ExceptionUtils.NotSupportedOrImplementedException;

public AppPackagingModel PackagingModel => throw ExceptionUtils.NotSupportedOrImplementedException;
}
}
10 changes: 10 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface IAppInfo
void ShowSettingsUI();

AppTheme RequestedTheme { get; }

AppPackagingModel PackagingModel { get; }
}

/// <include file="../../docs/Microsoft.Maui.Essentials/AppInfo.xml" path="Type[@FullName='Microsoft.Maui.Essentials.AppInfo']/Docs" />
Expand All @@ -46,6 +48,8 @@ public static class AppInfo
/// <include file="../../docs/Microsoft.Maui.Essentials/AppInfo.xml" path="//Member[@MemberName='RequestedTheme']/Docs" />
public static AppTheme RequestedTheme => Current.RequestedTheme;

public static AppPackagingModel PackagingModel => Current.PackagingModel;


static IAppInfo? currentImplementation;

Expand All @@ -57,4 +61,10 @@ public static class AppInfo
public static void SetCurrent(IAppInfo? implementation) =>
currentImplementation = implementation;
}

public enum AppPackagingModel
{
Packaged,
Unpackaged,
}
}
2 changes: 2 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ public void PlatformShowSettingsUI()

public AppTheme RequestedTheme
=> AppTheme.Unspecified;

public AppPackagingModel PackagingModel => AppPackagingModel.Packaged;
}
}
20 changes: 20 additions & 0 deletions src/Essentials/src/AppInfo/AppInfo.uwp.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Globalization;
using Windows.ApplicationModel;
#if WINDOWS
Expand All @@ -10,6 +11,21 @@ namespace Microsoft.Maui.Essentials.Implementations
{
public class AppInfoImplementation : IAppInfo
{
static Lazy<bool> _isPackagedAppLazy = new Lazy<bool>(() =>
{
try
{
if (Package.Current != null)
return true;
}
catch
{
// no-op
}

return false;
});

public string PackageName => Package.Current.Id.Name;

public string Name => Package.Current.DisplayName;
Expand All @@ -33,5 +49,9 @@ public void ShowSettingsUI() =>

public AppTheme RequestedTheme =>
Application.Current.RequestedTheme == ApplicationTheme.Dark ? AppTheme.Dark : AppTheme.Light;

public AppPackagingModel PackagingModel => _isPackagedAppLazy.Value
? AppPackagingModel.Packaged
: AppPackagingModel.Unpackaged;
}
}
26 changes: 23 additions & 3 deletions src/Essentials/src/FileSystem/FileSystem.android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,42 @@ static string PlatformCacheDirectory
static string PlatformAppDataDirectory
=> Platform.AppContext.FilesDir.AbsolutePath;

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
static Task<Stream> PlatformOpenAppPackageFileAsync(string filename) =>
Task.FromResult(PlatformOpenAppPackageFile(filename));

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
try
{
using var stream = PlatformOpenAppPackageFile(filename);
return Task.FromResult(true);
}
catch (FileNotFoundException)
{
return Task.FromResult(false);
}
}

static Stream PlatformOpenAppPackageFile(string filename)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));

filename = filename.Replace('\\', Path.DirectorySeparatorChar);
filename = NormalizePath(filename);

try
{
return Task.FromResult(Platform.AppContext.Assets.Open(filename));
return Platform.AppContext.Assets.Open(filename);
}
catch (Java.IO.FileNotFoundException ex)
{
throw new FileNotFoundException(ex.Message, filename, ex);
}
}

static string NormalizePath(string filename) =>
filename.Replace('\\', Path.DirectorySeparatorChar);

internal static Java.IO.File GetEssentialsTemporaryFile(Java.IO.File root, string fileName)
{
// create the directory for all Essentials files
Expand Down
23 changes: 19 additions & 4 deletions src/Essentials/src/FileSystem/FileSystem.ios.tvos.watchos.macos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@ static string PlatformAppDataDirectory
=> GetDirectory(NSSearchPathDirectory.LibraryDirectory);

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult((Stream)File.OpenRead(file));
}

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult(File.Exists(file));
}

static string PlatformGetFullAppPackageFilePath(string filename)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));

filename = filename.Replace('\\', Path.DirectorySeparatorChar);
filename = NormalizePath(filename);

var root = NSBundle.MainBundle.BundlePath;
#if MACCATALYST || MACOS
root = Path.Combine(root, "Contents", "Resources");
root = Path.Combine(root, "Contents", "Resources");
#endif
var file = Path.Combine(root, filename);
return Task.FromResult((Stream)File.OpenRead(file));
return Path.Combine(root, filename);
}

static string NormalizePath(string filename) =>
filename.Replace('\\', Path.DirectorySeparatorChar);

static string GetDirectory(NSSearchPathDirectory directory)
{
var dirs = NSSearchPath.GetDirectories(directory, NSSearchPathDomain.User);
Expand Down
3 changes: 3 additions & 0 deletions src/Essentials/src/FileSystem/FileSystem.netstandard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ static string PlatformAppDataDirectory

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
=> throw ExceptionUtils.NotSupportedOrImplementedException;

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
=> throw ExceptionUtils.NotSupportedOrImplementedException;
}

/// <include file="../../docs/Microsoft.Maui.Essentials/FileBase.xml" path="Type[@FullName='Microsoft.Maui.Essentials.FileBase']/Docs" />
Expand Down
3 changes: 3 additions & 0 deletions src/Essentials/src/FileSystem/FileSystem.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public static string AppDataDirectory
public static Task<Stream> OpenAppPackageFileAsync(string filename)
=> PlatformOpenAppPackageFileAsync(filename);

public static Task<bool> AppPackageFileExistsAsync(string filename)
=> PlatformAppPackageFileExistsAsync(filename);

internal static class MimeTypes
{
internal const string All = "*/*";
Expand Down
21 changes: 18 additions & 3 deletions src/Essentials/src/FileSystem/FileSystem.tizen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,29 @@ static string PlatformAppDataDirectory
=> Application.Current.DirectoryInfo.Data;

static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult((Stream)File.OpenRead(file));
}

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult(File.Exists(file));
}

static string PlatformGetFullAppPackageFilePath(string filename)
{
if (string.IsNullOrWhiteSpace(filename))
throw new ArgumentNullException(nameof(filename));

filename = filename.Replace('\\', Path.DirectorySeparatorChar);
Stream fs = File.OpenRead(Path.Combine(Application.Current.DirectoryInfo.Resource, filename));
return Task.FromResult(fs);
filename = NormalizePath(filename);

return Path.Combine(Application.Current.DirectoryInfo.Resource, filename);
}

static string NormalizePath(string filename) =>
filename.Replace('\\', Path.DirectorySeparatorChar);
}

public partial class FileBase
Expand Down
40 changes: 39 additions & 1 deletion src/Essentials/src/FileSystem/FileSystem.uwp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,45 @@ static Task<Stream> PlatformOpenAppPackageFileAsync(string filename)
if (filename == null)
throw new ArgumentNullException(nameof(filename));

return Package.Current.InstalledLocation.OpenStreamForReadAsync(NormalizePath(filename));
if (AppInfo.PackagingModel == AppPackagingModel.Packaged)
{
filename = NormalizePath(filename);

return Package.Current.InstalledLocation.OpenStreamForReadAsync(filename);
}
else
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult((Stream)File.OpenRead(file));
}
}

static Task<bool> PlatformAppPackageFileExistsAsync(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return Task.FromResult(File.Exists(file));
}

internal static bool AppPackageFileExists(string filename)
{
var file = PlatformGetFullAppPackageFilePath(filename);
return File.Exists(file);
}

static string PlatformGetFullAppPackageFilePath(string filename)
{
if (filename == null)
throw new ArgumentNullException(nameof(filename));

filename = NormalizePath(filename);

string root;
if (AppInfo.PackagingModel == AppPackagingModel.Packaged)
root = Package.Current.InstalledLocation.Path;
else
root = AppContext.BaseDirectory;

return Path.Combine(root, filename);
}

internal static string NormalizePath(string path)
Expand Down

0 comments on commit b8bf9f5

Please sign in to comment.