Skip to content

Commit

Permalink
feat: Add icon and title support from package.appxmanifest
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Jul 31, 2020
1 parent ec6e7c4 commit dc4e7ee
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 29 deletions.
32 changes: 32 additions & 0 deletions src/Uno.UI.Runtime.Skia.Gtk/GTK/GtkHost.cs
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Uno.Extensions;
Expand Down Expand Up @@ -102,9 +104,39 @@ void CreateApp(ApplicationInitializationCallbackParams _)

WUX.Application.Start(CreateApp, _args);

UpdateWindowPropertiesFromPackage();

Gtk.Application.Run();
}

private void UpdateWindowPropertiesFromPackage()
{
if (Windows.ApplicationModel.Package.Current.Logo is Uri uri)
{
var basePath = uri.OriginalString.Replace('\\', Path.DirectorySeparatorChar);
var iconPath = Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, basePath);

if (File.Exists(iconPath))
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
{
this.Log().Warn($"Loading icon file [{iconPath}] from Package.appxmanifest file");
}

GtkHost.Window.SetIconFromFile(iconPath);
}
else
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
{
this.Log().Warn($"Unable to find icon file [{iconPath}] specified in the Package.appxmanifest file.");
}
}
}

Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().Title = Windows.ApplicationModel.Package.Current.DisplayName;
}

public void TakeScreenshot(string filePath)
{
_area.TakeScreenshot(filePath);
Expand Down
Expand Up @@ -10,6 +10,7 @@
using Uno.Foundation.Extensibility;
using Uno.Logging;
using Uno.UI.Runtime.Skia;
using Windows.ApplicationModel;
using Windows.Devices.Input;
using Windows.Foundation;
using Windows.UI.Composition;
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UI.Runtime.Skia.Gtk/GTK/UnoDrawingArea.cs
Expand Up @@ -14,7 +14,7 @@ internal class UnoDrawingArea : Gtk.DrawingArea

public UnoDrawingArea()
{
WUX.Window.Current.InvalidateRender
WUX.Window.InvalidateRender
+= () =>
{
Invalidate();
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UI/UI/Xaml/Application.Skia.cs
Expand Up @@ -26,6 +26,7 @@ public partial class Application : IApplicationEvents
public Application()
{
Current = this;
Package.SetEntryAssembly(this.GetType().Assembly);

if (!ApiExtensibility.CreateInstance(this, out _coreWindowExtension))
{
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/UI/Xaml/Application.wasm.cs
Expand Up @@ -39,7 +39,9 @@ public Application()
{
throw new InvalidOperationException("The application must be started using Application.Start first, e.g. Windows.UI.Xaml.Application.Start(_ => new App());");
}

Current = this;
Package.SetEntryAssembly(this.GetType().Assembly);

CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UI/UI/Xaml/Shapes/ArbitraryShapeBase.Skia.cs
Expand Up @@ -9,10 +9,10 @@ namespace Windows.UI.Xaml.Shapes
{
public abstract partial class ArbitraryShapeBase : Shape
{
#pragma warning disable 649 // unused member
#pragma warning disable 169 // unused member
private float _scaleX;
private float _scaleY;
#pragma warning restore 649 // unused member
#pragma warning restore 169 // unused member

private IDisposable BuildDrawableLayer() { return null; }

Expand Down
52 changes: 31 additions & 21 deletions src/Uno.UI/UI/Xaml/Window.Skia.cs
@@ -1,3 +1,4 @@
#nullable enable
using System;
using System.Diagnostics;
using System.Linq;
Expand All @@ -23,13 +24,13 @@ namespace Windows.UI.Xaml
{
public sealed partial class Window
{
private static Window _current;
private Grid _window;
private static Window? _current;
private Grid? _window;
// private PopupRoot _popupRoot;
// private ScrollViewer _rootScrollViewer;
private Border _rootBorder;
private PopupRoot _popupRoot;
private UIElement _content;
private Border? _rootBorder;
private PopupRoot? _popupRoot;
private UIElement? _content;

public Window()
{
Expand All @@ -45,7 +46,7 @@ public void Init()
CoreWindow.SetInvalidateRender(QueueInvalidateRender);
}

internal Action InvalidateRender;
internal static Action InvalidateRender = () => { };
private bool _renderQueued = false;

internal void QueueInvalidateRender()
Expand Down Expand Up @@ -129,7 +130,7 @@ internal void OnNativeSizeChanged(Size size)

public Compositor Compositor { get; }

internal Grid RootElement => _window;
internal Grid RootElement => _window!;

partial void InternalActivate()
{
Expand All @@ -154,10 +155,13 @@ private void InternalSetContent(UIElement content)
Compositor.RootVisual = _window.Visual;
}

_rootBorder.Child = _content = content;
if (_rootBorder != null)
{
_rootBorder.Child = _content = content;
}
}

private UIElement InternalGetContent() => _content;
private UIElement InternalGetContent() => _content!;

private static Window InternalGetCurrentWindow()
{
Expand All @@ -176,21 +180,27 @@ internal IDisposable OpenPopup(Popup popup)
this.Log().Debug($"Creating popup");
}

var popupPanel = popup.PopupPanel;
_popupRoot.Children.Add(popupPanel);

return new CompositeDisposable(
Disposable.Create(() => {
if (_popupRoot != null)
{
var popupPanel = popup.PopupPanel;
_popupRoot.Children.Add(popupPanel);

if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
return new CompositeDisposable(
Disposable.Create(() =>
{
this.Log().Debug($"Closing popup");
}
_popupRoot.Children.Remove(popupPanel);
}),
VisualTreeHelper.RegisterOpenPopup(popup)
);
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Closing popup");
}
_popupRoot.Children.Remove(popupPanel);
}),
VisualTreeHelper.RegisterOpenPopup(popup)
);
}

return new CompositeDisposable();
}
}
}
73 changes: 73 additions & 0 deletions src/Uno.UWP/ApplicationModel/Package.Other.cs
@@ -1,15 +1,26 @@
#nullable enable
#if !(__IOS__ || __ANDROID__ || __MACOS__)
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Security.Cryptography;
using System.Xml;
using Uno.Extensions;
using Uno.Logging;
using Uno.UI;
using Windows.ApplicationModel.Email.DataProvider;
using Windows.Storage;

namespace Windows.ApplicationModel
{
public partial class Package
{
private const string PackageManifestName = "Package.appxmanifest";
private static Assembly? _entryAssembly;
private string _displayName = "";
private string _logo = "ms-appx://logo";
private bool _manifestParsed;

private bool GetInnerIsDevelopmentMode() => false;

private DateTimeOffset GetInstallDate() => DateTimeOffset.Now;
Expand All @@ -25,6 +36,68 @@ private string GetInstalledLocation()
return Environment.CurrentDirectory;
}
}

public string DisplayName
{
get
{
TryParsePackageManifest();
return _displayName;
}
}

public Uri Logo
{
get
{
TryParsePackageManifest();
return new Uri(_logo, UriKind.RelativeOrAbsolute);
}
}

internal static void SetEntryAssembly(Assembly entryAssembly)
{
_entryAssembly = entryAssembly;
}

private void TryParsePackageManifest()
{
if(_entryAssembly != null && !_manifestParsed)
{
_manifestParsed = true;

var manifest = _entryAssembly.GetManifestResourceStream(PackageManifestName);

if (manifest != null)
{
try
{
var doc = new XmlDocument();
doc.Load(manifest);

var nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("d", "http://schemas.microsoft.com/appx/manifest/foundation/windows10");

_displayName = doc.SelectSingleNode("/d:Package/d:Properties/d:DisplayName", nsmgr)?.InnerText ?? "";
_logo = doc.SelectSingleNode("/d:Package/d:Properties/d:Logo", nsmgr)?.InnerText ?? "";
}
catch (Exception ex)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
{
this.Log().Error($"Failed to read manifest [{PackageManifestName}]", ex);
}
}
}
else
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Skipping manifest reading, unable to find [{PackageManifestName}]");
}
}
}
}
}
}
#endif
8 changes: 3 additions & 5 deletions src/Uno.UWP/ApplicationModel/Package.cs
Expand Up @@ -30,19 +30,17 @@ public partial class Package
[global::Uno.NotImplemented]
public string Description => "";

#if !__ANDROID__ && !__IOS__ && !__MACOS__
[global::Uno.NotImplemented]
public string DisplayName => "";
#endif

[global::Uno.NotImplemented]
public bool IsBundle => false;

[global::Uno.NotImplemented]
public bool IsResourcePackage => false;


#if (__IOS__ || __ANDROID__ || __MACOS__)
[global::Uno.NotImplemented]
public global::System.Uri Logo => new Uri("http://example.com");
#endif

[global::Uno.NotImplemented]
public string PublisherDisplayName => "";
Expand Down
1 change: 1 addition & 0 deletions src/Uno.UWP/UI/ViewManagement/ApplicationView.skia.cs
Expand Up @@ -7,6 +7,7 @@
using System.Globalization;
using Uno.Foundation.Extensibility;
using Uno.Disposables;
using Windows.ApplicationModel;

namespace Windows.UI.ViewManagement
{
Expand Down

0 comments on commit dc4e7ee

Please sign in to comment.