Skip to content

Commit

Permalink
Merge pull request #8450 from MartinZikmund/dev/mazi/launchargs-macos
Browse files Browse the repository at this point in the history
fix!: Args separator on Skia, feat: Pass arguments to `LaunchActivatedEventArgs` on macOS
  • Loading branch information
mergify[bot] authored Apr 14, 2022
2 parents f8fe7ec + f81c108 commit 5e4b71f
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"profiles": {
"SamplesApp.Skia.Gtk": {
"commandName": "Project",
"commandLineArgs": "--_auto-screenshots=C:\\temp\\screenshots",
"commandLineArgs": "",
"nativeDebugging": false
}
}
Expand Down
13 changes: 10 additions & 3 deletions src/Uno.UI.Runtime.Skia.Gtk/GtkHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class GtkHost : ISkiaHost
[ThreadStatic]
private static bool _isDispatcherThread = false;

private readonly string[] _args;
private readonly Func<WUX.Application> _appBuilder;
private static Gtk.Window _window;
private static Gtk.EventBox _eventBox;
Expand All @@ -57,9 +56,17 @@ public class GtkHost : ISkiaHost
? RenderSurfaceType.Software // OpenGL support on macOS is currently broken
: RenderSurfaceType.OpenGL;

/// <summary>
/// Creates a host for a Uno Skia GTK application.
/// </summary>
/// <param name="appBuilder">App builder.</param>
/// <param name="args">Deprecated, value ignored.</param>
/// <remarks>
/// Args are obsolete and will be removed in the future. Environment.CommandLine is used instead
/// to fill LaunchEventArgs.Arguments.
/// </remarks>
public GtkHost(Func<WUX.Application> appBuilder, string[] args)
{
_args = args;
_appBuilder = appBuilder;
}

Expand Down Expand Up @@ -172,7 +179,7 @@ void CreateApp(ApplicationInitializationCallbackParams _)
app.Host = this;
}

WUX.Application.Start(CreateApp, _args);
WUX.Application.StartWithArguments(CreateApp);

UpdateWindowPropertiesFromPackage();

Expand Down
13 changes: 10 additions & 3 deletions src/Uno.UI.Runtime.Skia.Linux.FrameBuffer/FramebufferHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,22 @@ public class FrameBufferHost : ISkiaHost
[ThreadStatic]
private static bool _isDispatcherThread = false;

private string[] _args;
private Func<Application> _appBuilder;
private readonly EventLoop _eventLoop;
private Renderer? _renderer;
private DisplayInformationExtension? _displayInformationExtension;

/// <summary>
/// Creates a host for a Uno Skia FrameBuffer application.
/// </summary>
/// <param name="appBuilder">App builder.</param>
/// <param name="args">Deprecated, value ignored.</param>
/// <remarks>
/// Args are obsolete and will be removed in the future. Environment.CommandLine is used instead
/// to fill LaunchEventArgs.Arguments.
/// </remarks>
public FrameBufferHost(Func<WUX.Application> appBuilder, string[] args)
{
_args = args;
_appBuilder = appBuilder;

_eventLoop = new EventLoop();
Expand Down Expand Up @@ -55,7 +62,7 @@ void CreateApp(ApplicationInitializationCallbackParams _)
_renderer = new Renderer();
_displayInformationExtension!.Renderer = _renderer;

WUX.Application.Start(CreateApp, _args);
WUX.Application.StartWithArguments(CreateApp);
}
}
}
21 changes: 6 additions & 15 deletions src/Uno.UI.Runtime.Skia.Tizen/TizenHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,28 @@ public class TizenHost : ISkiaHost
private readonly TizenApplication _tizenApplication;
private readonly Func<WinUI.Application> _appBuilder;
private readonly TizenWindow _window;
private readonly string[] _args;

public static TizenHost Current => _current;

/// <summary>
/// Creates a WpfHost element to host a Uno-Skia into a WPF application.
/// Creates a host for a Uno Skia Tizen application.
/// </summary>
/// <remarks>
/// If args are omitted, those from Environment.GetCommandLineArgs() will be used.
/// </remarks>
public TizenHost(Func<WinUI.Application> appBuilder, string[] args = null)
/// <param name="appBuilder">App builder.</param>
/// <param name="args">Arguments.</param>
public TizenHost(Func<WinUI.Application> appBuilder, string[]? args = null)
{
Elementary.Initialize();
Elementary.ThemeOverlay();

_current = this;

_appBuilder = appBuilder;
_args = args;


_args ??= Environment
.GetCommandLineArgs()
.Skip(1)
.ToArray();

Windows.UI.Core.CoreDispatcher.DispatchOverride = (d) => EcoreMainloop.PostAndWakeUp(d);
Windows.UI.Core.CoreDispatcher.HasThreadAccessOverride = () => EcoreMainloop.IsMainThread;

_tizenApplication = new TizenApplication(this);
_tizenApplication.Run(_args);
_tizenApplication.Run(args);
}

public void Run()
Expand All @@ -101,7 +92,7 @@ void CreateApp(ApplicationInitializationCallbackParams _)
new Windows.Foundation.Size(
_tizenApplication.Window.ScreenSize.Width,
_tizenApplication.Window.ScreenSize.Height));
WinUI.Application.Start(CreateApp, _args);
WinUI.Application.StartWithArguments(CreateApp);
}
}
}
12 changes: 5 additions & 7 deletions src/Uno.UI.Runtime.Skia.Wpf/WpfHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,16 @@ static WpfHost()
/// <summary>
/// Creates a WpfHost element to host a Uno-Skia into a WPF application.
/// </summary>
/// <param name="appBuilder">App builder.</param>
/// <param name="args">Deprecated, value ignored.</param>
/// <remarks>
/// If args are omitted, those from Environment.GetCommandLineArgs() will be used.
/// Args are obsolete and will be removed in the future. Environment.CommandLine is used instead
/// to fill LaunchEventArgs.Arguments.
/// </remarks>
public WpfHost(global::System.Windows.Threading.Dispatcher dispatcher, Func<WinUI.Application> appBuilder, string[] args = null)
{
_current = this;

args ??= Environment
.GetCommandLineArgs()
.Skip(1)
.ToArray();

designMode = DesignerProperties.GetIsInDesignMode(this);

void CreateApp(WinUI.ApplicationInitializationCallbackParams _)
Expand All @@ -108,7 +106,7 @@ void CreateApp(WinUI.ApplicationInitializationCallbackParams _)
Windows.UI.Core.CoreDispatcher.DispatchOverride = d => dispatcher.BeginInvoke(d);
Windows.UI.Core.CoreDispatcher.HasThreadAccessOverride = dispatcher.CheckAccess;

WinUI.Application.Start(CreateApp, args);
WinUI.Application.StartWithArguments(CreateApp);

WinUI.Window.InvalidateRender += () =>
{
Expand Down
30 changes: 30 additions & 0 deletions src/Uno.UI/UI/Xaml/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,5 +445,35 @@ internal static void PropagateResourcesChanged(object instance, ResourceUpdateRe
}
}
}

private static string GetCommandLineArgsWithoutExecutable()
{
var args = Environment.GetCommandLineArgs();
if (args.Length <= 1)
{
return "";
}

// The first "argument" is actually application name, needs to be removed.
// May be wrapped in quotes.

var executable = args[0];
var rawCmd = Environment.CommandLine;

var index = rawCmd.IndexOf(executable);
if (index == 0)
{
rawCmd = rawCmd.Substring(executable.Length);
}
else if (index == 1)
{
// The executable is wrapped in quotes
rawCmd = rawCmd.Substring(executable.Length + 2);
}

// The whitespace on the start side of Arguments
// in UWP is trimmed whereas the ending is not.
return rawCmd.TrimStart();
}
}
}
4 changes: 3 additions & 1 deletion src/Uno.UI/UI/Xaml/Application.macOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ public override void DidFinishLaunching(NSNotification notification)
}
if (!handled)
{
OnLaunched(new LaunchActivatedEventArgs());
var argumentsString = GetCommandLineArgsWithoutExecutable();

OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, argumentsString));
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Uno.UI/UI/Xaml/Application.skia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
using Uno.UI.Xaml;
using Uno.Foundation.Extensibility;


namespace Windows.UI.Xaml
{
public partial class Application : IApplicationEvents
{
private static bool _startInvoked = false;
private static string[] _args;
private static string _arguments = "";

private readonly IApplicationExtension? _applicationExtension;

internal ISkiaHost? Host { get; set; }
Expand All @@ -41,9 +41,9 @@ public Application()
CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, Initialize);
}

internal static void Start(global::Windows.UI.Xaml.ApplicationInitializationCallback callback, string[] args)
internal static void StartWithArguments(global::Windows.UI.Xaml.ApplicationInitializationCallback callback)
{
_args = args;
_arguments = GetCommandLineArgsWithoutExecutable();
Start(callback);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ private void Initialize()

InitializationCompleted();

OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, string.Join(";", _args)));
OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, _arguments));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,23 @@ internal LaunchActivatedEventArgs(ActivationKind kind, string arguments)
#if !__ANDROID__ && !__IOS__
[NotImplemented]
#endif
public ActivationKind Kind
{
get;
}
public ActivationKind Kind { get; } = ActivationKind.Launch;

/// <summary>
/// Defaults to NotRunning, may not be accurate in all cases for all platforms.
/// </summary>
public ApplicationExecutionState PreviousExecutionState { get; }
public ApplicationExecutionState PreviousExecutionState { get; } = ApplicationExecutionState.NotRunning;

[NotImplemented]
public SplashScreen SplashScreen
{
get;
}
public SplashScreen SplashScreen { get; } = new SplashScreen();

[NotImplemented]
public int CurrentlyShownApplicationViewId
{
get;
}
public int CurrentlyShownApplicationViewId { get; }

public string Arguments
{
get;
}
public string Arguments { get; } = "";

[NotImplemented]
public string TileId
{
get;
}
public string TileId { get; } = "App";

public bool PrelaunchActivated => false; // No platform other than UWP supports prelaunch yet.
}
Expand Down

0 comments on commit 5e4b71f

Please sign in to comment.