Skip to content

Commit

Permalink
perf(wasm): Enable trimming for external drag and drop
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Nov 25, 2022
1 parent e127d30 commit 7dbab4f
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions build/cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
"Udemy",
"UNOB",
"winui",
"illinker",
],
"patterns": [
{
Expand Down
12 changes: 11 additions & 1 deletion build/uno.winui.common.targets
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
<_IsUnoPlatform>true</_IsUnoPlatform>
</PropertyGroup>

<!-- Feature configuration -->
<PropertyGroup>
<UnoDragDropExternalSupport Condition="'$(UnoDragDropExternalSupport)'==''">true</UnoDragDropExternalSupport>
</PropertyGroup>
<ItemGroup>

<RuntimeHostConfigurationOption Include="Windows.ApplicationModel.DataTransfer.DragDrop.ExternalSupport"
Value="$(UnoDragDropExternalSupport)"
Trim="true" />
</ItemGroup>

<Target Name="ValidateUnoUIAndUnoWinUIExclusion" BeforeTargets="BeforeBuild">
<Error Code="UNOB0001"
Text="Cannot build with both Uno.WinUI and Uno.UI nuget packages referenced."
Expand Down Expand Up @@ -211,5 +222,4 @@
">
<Error Text="Building for the iOS Simulator requires the use of the static registrar. Make sure that `MtouchExtraArgs` contains `--registrar:static`. See https://github.com/unoplatform/uno/issues/9430 for more details." />
</Target>

</Project>
24 changes: 23 additions & 1 deletion doc/articles/features/using-il-linker-webassembly.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
# Using the IL Linker for WebAssembly
---
uid: articles.features.illinker
---

# Using the IL Linker

### Support for features

In order to improve the size of the application, some platforms are providing the ability to unconditionally disable features if the app is known not to use them.

|Feature|MSBuild property|Description|
|-----|----|---|
|External Drag and Drop|`UnoDragDropExternalSupport`|Enables or disables drag and dropping content from **outside** the app using this property. Drag and Drop **inside** the app is always available when disabled.|

For example, to disable external drag and drop support on WebAssembly, add the following to your csproj:

```xml
<PropertyGroup>
<UnoDragDropExternalSupport>false</UnoDragDropExternalSupport>
</PropertyGroup>
```

## WebAssembly

The [linker step](https://github.com/mono/linker/tree/master/docs) (also known as tree shaking, or IL Trimming) is responsible for the detection and removal of code that may not be used at runtime. This step is particularly important when targeting WebAssembly or native code in general, to reduce significantly the final package size.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
AfterTargets="CoreCompile">

<ItemGroup>
<_SubstitutionFiles Include="@(UnoLinkerSubstitution)" />
<_SubstitutionFiles Include="$(IntermediateOutputPath)\Substitutions\*.Substitutions.xml" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Uno.Foundation/Extensions/LinkerAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#nullable enable

#if NETSTANDARD2_0
#if !NET6_0_OR_GREATER || NETSTANDARD
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/Uno.UI/LinkerDefinition.Wasm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
<type fullname="Windows.UI.Xaml.Input.FocusManager">
<method name="ReceiveFocusNative" />
</type>
<type fullname="Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropExtension">
<method name="OnNativeDropEvent" />
</type>
<type fullname="Windows.UI.Xaml.Application">
<method name="DispatchSuspending" />
<method name="DispatchSystemThemeChange" />
Expand Down Expand Up @@ -92,5 +89,4 @@
<type fullname="System.ComponentModel.*Converter" />
<type fullname="System.*Converter" />
</assembly>

</linker>
13 changes: 13 additions & 0 deletions src/Uno.UI/LinkerSubstitution.Wasm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<linker>
<assembly fullname="Uno.UI">
<type fullname="Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropExtension">

<method signature="System.Boolean get_IsExternalDragAndDropSupported()"
feature="Windows.ApplicationModel.DataTransfer.DragDrop.ExternalSupport"
body="stub"
value="false"
featurevalue="false" />

</type>
</assembly>
</linker>
27 changes: 23 additions & 4 deletions src/Uno.UI/UI/Xaml/DragDrop/DragDropExtension.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
using Uno.Storage.Internal;
using Uno.UI;
using Uno.UI.Xaml;
using System.Diagnostics.CodeAnalysis;

// As IDragDropExtension is internal, the generated registration cannot be used.
// [assembly: ApiExtension(typeof(Windows.ApplicationModel.DataTransfer.DragDrop.Core.IDragDropExtension), typeof(Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropExtension))]
Expand All @@ -42,17 +43,29 @@ internal class DragDropExtension : IDragDropExtension

private static DragDropExtension? _current;

/// <summary>
/// Conditional support for external drag and drop. See the UnoDragDropExternalSupport msbuild property.
/// </summary>
public static bool IsExternalDragAndDropSupported { get; } = true;

public static DragDropExtension GetForCurrentView()
{
// Note: We use the GetForCurrentView naming pattern, but we don't support multi-threading yet
// and the '_current' is actually just static.

if (_current is null && Interlocked.CompareExchange(ref _current, new DragDropExtension(), null) is null)
{
// For now we enable the D&DExtension sync at creation and we don't support disable.
// This allow us to prevent a drop of a content on an app which actually don't support D&D
// (would drive the browser to open the dragged file and "dismiss" the app).
_current.Enable();
if (IsExternalDragAndDropSupported)
{
// For now we enable the D&DExtension sync at creation and we don't support disable.
// This allow us to prevent a drop of a content on an app which actually don't support D&D
// (would drive the browser to open the dragged file and "dismiss" the app).
_current.Enable();
}
else
{
EnableExternalWarning();
}
}

return _current;
Expand All @@ -70,6 +83,7 @@ private DragDropExtension()
?? throw new InvalidOperationException("No CoreDragDropManager available for current thread.");
}

[DynamicDependency(nameof(OnNativeDropEvent))]
private void Enable()
{
if (Interlocked.CompareExchange(ref _isInitialized, 1, 0) == 0)
Expand All @@ -84,6 +98,11 @@ private void Enable()
}
}

private static void EnableExternalWarning()
{
WebAssemblyRuntime.InvokeJS("Windows.ApplicationModel.DataTransfer.DragDrop.Core.DragDropExtension.registerNoOp();");
}

/// <inheritdoc />
void IDragDropExtension.StartNativeDrag(CoreDragInfo info)
{
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI/Uno.UI.Wasm.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
</ItemGroup>

<ItemGroup>
<None Include="LinkerSubstitution.Wasm.xml" />
<UnoLinkerSubstitution Include="LinkerSubstitution.Wasm.xml" />
<EmbeddedResource Include="LinkerDefinition.Wasm.xml">
<LogicalName>$(AssemblyName).xml</LogicalName>
</EmbeddedResource>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
document.removeEventListener("drop", this._dropHandler);
}

public static registerNoOp() {
let notifyDisabled = (evt: DragEvent) => {
evt.dataTransfer.dropEffect = "none";
console.debug("DragAndDrop from external sources is disabled. See the UnoDragDropExternalSupport msbuild property.");

document.removeEventListener("dragenter", notifyDisabled);
};

document.addEventListener("dragenter", notifyDisabled);
}

private dispatchDropEvent(evt: DragEvent): any {
if (evt.type == "dragleave"
&& evt.clientX > 0
Expand Down

0 comments on commit 7dbab4f

Please sign in to comment.