diff --git a/Documentation/build.md b/Documentation/build.md index 2f59146a48..5f75290424 100644 --- a/Documentation/build.md +++ b/Documentation/build.md @@ -60,15 +60,10 @@ git submodule update --init --recursive ### Build native libraries (macOS only) -On macOS it is necessary to build and manually install the respective native libraries using [Xcode](https://developer.apple.com/xcode/). The steps to get this working correctly are: -- (for revisions after 2 Nov 2020) Run `./build.sh GenerateCppHeaders` to generate `avalonia-native.h` from `avn.idl` -- Navigate to the Avalonia/native/Avalonia.Native/src/OSX folder and open the `Avalonia.Native.OSX.xcodeproj` project -- Build the library via the Product->Build menu. This will generate binaries in your local path under ~/Library/Developer/Xcode/DerivedData/Avalonia.Native.OSX-*guid* where "guid" is uniquely generated every time you build. -- Manually install the native library by copying it from the build artifacts folder into the shared dynamic library path: +On macOS it is necessary to build and manually install the respective native libraries using [Xcode](https://developer.apple.com/xcode/). Execute the build script in the root project with the `CompileNative` task. It will build the headers, build the libraries, and place them in the appropriate place to allow .NET to find them at compilation and run time. -``` -cd ~/Library/Developer/Xcode/DerivedData/Avalonia.Native.OSX-[guid]/Build/Products/Debug -cp libAvalonia.Native.OSX.dylib /usr/local/lib/libAvaloniaNative.dylib +```bash +./build.sh CompileNative ``` ### Build and Run Avalonia diff --git a/NuGet.Config b/NuGet.Config index 3abd236d42..7a1f28bea7 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -4,6 +4,6 @@ - + diff --git a/build/ApiDiff.props b/build/ApiDiff.props index 3d322f56d5..fb65ef6e87 100644 --- a/build/ApiDiff.props +++ b/build/ApiDiff.props @@ -1,6 +1,6 @@  - 0.10.0-preview3 + 0.10.0-preview6 $(PackageId) Avalonia diff --git a/build/MicroCom.targets b/build/MicroCom.targets index 3a07950616..b48e377fd4 100644 --- a/build/MicroCom.targets +++ b/build/MicroCom.targets @@ -2,7 +2,7 @@ - + false all true @@ -12,10 +12,10 @@ - + @@ -24,7 +24,7 @@ - + <_AvaloniaPatchComInterop>true diff --git a/build/Rx.props b/build/Rx.props index 8a15ccd6a9..fde1f80ea1 100644 --- a/build/Rx.props +++ b/build/Rx.props @@ -1,5 +1,5 @@  - + diff --git a/native/Avalonia.Native/src/OSX/platformthreading.mm b/native/Avalonia.Native/src/OSX/platformthreading.mm index f93436d157..e83bf53331 100644 --- a/native/Avalonia.Native/src/OSX/platformthreading.mm +++ b/native/Avalonia.Native/src/OSX/platformthreading.mm @@ -101,7 +101,7 @@ public: virtual bool GetCurrentThreadIsLoopThread() override { - return [[NSThread currentThread] isMainThread]; + return [NSThread isMainThread]; } virtual void SetSignaledCallback(IAvnSignaledCallback* cb) override { diff --git a/native/Avalonia.Native/src/OSX/rendertarget.mm b/native/Avalonia.Native/src/OSX/rendertarget.mm index 93a33bbbb0..00b6dab219 100644 --- a/native/Avalonia.Native/src/OSX/rendertarget.mm +++ b/native/Avalonia.Native/src/OSX/rendertarget.mm @@ -2,6 +2,7 @@ #include "rendertarget.h" #import #import +#import #include #include @@ -143,13 +144,17 @@ static IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(IOSurfaceRenderTarget* ta return _layer; } -- (void)resize:(AvnPixelSize)size withScale: (float) scale;{ +- (void)resize:(AvnPixelSize)size withScale: (float) scale{ @synchronized (lock) { if(surface == nil || surface->size.Width != size.Width || surface->size.Height != size.Height || surface->scale != scale) + { surface = [[IOSurfaceHolder alloc] initWithSize:size withScale:scale withOpenGlContext:_glContext.getRaw()]; + + [self updateLayer]; + } } } @@ -159,12 +164,15 @@ static IAvnGlSurfaceRenderTarget* CreateGlRenderTarget(IOSurfaceRenderTarget* ta @synchronized (lock) { if(_layer == nil) return; + [CATransaction begin]; [_layer setContents: nil]; if(surface != nil) { [_layer setContentsScale: surface->scale]; [_layer setContents: (__bridge IOSurface*) surface->surface]; } + [CATransaction commit]; + [CATransaction flush]; } } else diff --git a/nukebuild/Build.cs b/nukebuild/Build.cs index 97647a1c59..8e331edab4 100644 --- a/nukebuild/Build.cs +++ b/nukebuild/Build.cs @@ -107,7 +107,7 @@ partial class Build : NukeBuild .AddProperty("JavaSdkDirectory", GetVariable("JAVA_HOME_8_X64"))) .AddProperty("PackageVersion", Parameters.Version) .AddProperty("iOSRoslynPathHackRequired", true) - .SetToolPath(MsBuildExe.Value) + .SetProcessToolPath(MsBuildExe.Value) .SetConfiguration(Parameters.Configuration) .SetVerbosity(MSBuildVerbosity.Minimal) .Apply(configurator)); @@ -132,10 +132,10 @@ partial class Build : NukeBuild var webappDir = RootDirectory / "src" / "Avalonia.DesignerSupport" / "Remote" / "HtmlTransport" / "webapp"; NpmTasks.NpmInstall(c => c - .SetWorkingDirectory(webappDir) - .SetArgumentConfigurator(a => a.Add("--silent"))); + .SetProcessWorkingDirectory(webappDir) + .SetProcessArgumentConfigurator(a => a.Add("--silent"))); NpmTasks.NpmRun(c => c - .SetWorkingDirectory(webappDir) + .SetProcessWorkingDirectory(webappDir) .SetCommand("dist")); }); @@ -157,7 +157,7 @@ partial class Build : NukeBuild { if (Parameters.IsRunningOnWindows) MsBuildCommon(Parameters.MSBuildSolution, c => c - .SetArgumentConfigurator(a => a.Add("/r")) + .SetProcessArgumentConfigurator(a => a.Add("/r")) .AddTargets("Build") ); @@ -194,7 +194,7 @@ partial class Build : NukeBuild var eventsProject = Path.Combine(eventsDirectory, "Avalonia.ReactiveUI.Events.csproj"); if (Parameters.IsRunningOnWindows) MsBuildCommon(eventsProject, c => c - .SetArgumentConfigurator(a => a.Add("/r")) + .SetProcessArgumentConfigurator(a => a.Add("/r")) .AddTargets("Build") ); else @@ -242,10 +242,10 @@ partial class Build : NukeBuild var webappTestDir = RootDirectory / "tests" / "Avalonia.DesignerSupport.Tests" / "Remote" / "HtmlTransport" / "webapp"; NpmTasks.NpmInstall(c => c - .SetWorkingDirectory(webappTestDir) - .SetArgumentConfigurator(a => a.Add("--silent"))); + .SetProcessWorkingDirectory(webappTestDir) + .SetProcessArgumentConfigurator(a => a.Add("--silent"))); NpmTasks.NpmRun(c => c - .SetWorkingDirectory(webappTestDir) + .SetProcessWorkingDirectory(webappTestDir) .SetCommand("test")); }); diff --git a/nukebuild/_build.csproj b/nukebuild/_build.csproj index 745c727be2..b0380cc92b 100644 --- a/nukebuild/_build.csproj +++ b/nukebuild/_build.csproj @@ -10,7 +10,7 @@ - + diff --git a/samples/ControlCatalog/DecoratedWindow.xaml b/samples/ControlCatalog/DecoratedWindow.xaml index 8e4c97b7f0..5251a2fa55 100644 --- a/samples/ControlCatalog/DecoratedWindow.xaml +++ b/samples/ControlCatalog/DecoratedWindow.xaml @@ -6,25 +6,21 @@ - - - - - - - - - - - + + + + + + + + + - - - - - - + + + + diff --git a/samples/ControlCatalog/MainWindow.xaml b/samples/ControlCatalog/MainWindow.xaml index 97bd88f5e4..6a70bb082f 100644 --- a/samples/ControlCatalog/MainWindow.xaml +++ b/samples/ControlCatalog/MainWindow.xaml @@ -16,47 +16,39 @@ - - - - - - - - - - - - - + + + + + + + + + - - - - - - + + + + - - - - - - - + + + + + diff --git a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs index cf6c771e34..49921fb7f6 100644 --- a/samples/ControlCatalog/Pages/DialogsPage.xaml.cs +++ b/samples/ControlCatalog/Pages/DialogsPage.xaml.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using Avalonia.Controls; using Avalonia.Dialogs; +using Avalonia.Layout; using Avalonia.Markup.Xaml; #pragma warning disable 4014 @@ -112,11 +113,29 @@ namespace ControlCatalog.Pages private Window CreateSampleWindow() { - var window = new Window(); - window.Height = 200; - window.Width = 200; - window.Content = new TextBlock { Text = "Hello world!" }; - window.WindowStartupLocation = WindowStartupLocation.CenterOwner; + Button button; + + var window = new Window + { + Height = 200, + Width = 200, + Content = new StackPanel + { + Spacing = 4, + Children = + { + new TextBlock { Text = "Hello world!" }, + (button = new Button + { + HorizontalAlignment = HorizontalAlignment.Center, + Content = "Click to close" + }) + } + }, + WindowStartupLocation = WindowStartupLocation.CenterOwner + }; + + button.Click += (_, __) => window.Close(); return window; } diff --git a/samples/Sandbox/MainWindow.axaml.cs b/samples/Sandbox/MainWindow.axaml.cs index b7222e043d..3d54036d29 100644 --- a/samples/Sandbox/MainWindow.axaml.cs +++ b/samples/Sandbox/MainWindow.axaml.cs @@ -1,6 +1,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using Avalonia.Win32.WinRT.Composition; namespace Sandbox { diff --git a/src/Avalonia.Base/Data/Core/TypeCastNode.cs b/src/Avalonia.Base/Data/Core/TypeCastNode.cs new file mode 100644 index 0000000000..476fd5527f --- /dev/null +++ b/src/Avalonia.Base/Data/Core/TypeCastNode.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Avalonia.Data.Core +{ + public class TypeCastNode : ExpressionNode + { + public override string Description => $"as {TargetType.FullName}"; + + public Type TargetType { get; } + + public TypeCastNode(Type type) + { + TargetType = type; + } + + protected virtual object Cast(object value) + { + return TargetType.IsInstanceOfType(value) ? value : null; + } + + protected override void StartListeningCore(WeakReference reference) + { + if (reference.TryGetTarget(out object target)) + { + target = Cast(target); + reference = target == null ? NullReference : new WeakReference(target); + } + + base.StartListeningCore(reference); + } + } +} diff --git a/src/Avalonia.Base/Utilities/AvaloniaPropertyValueStore.cs b/src/Avalonia.Base/Utilities/AvaloniaPropertyValueStore.cs index 0238446892..6e52b6770a 100644 --- a/src/Avalonia.Base/Utilities/AvaloniaPropertyValueStore.cs +++ b/src/Avalonia.Base/Utilities/AvaloniaPropertyValueStore.cs @@ -1,5 +1,8 @@ using System; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; + +#nullable enable namespace Avalonia.Utilities { @@ -9,12 +12,14 @@ namespace Avalonia.Utilities /// Stored value type. internal sealed class AvaloniaPropertyValueStore { + // The last item in the list is always int.MaxValue. + private static readonly Entry[] s_emptyEntries = { new Entry { PropertyId = int.MaxValue, Value = default! } }; + private Entry[] _entries; public AvaloniaPropertyValueStore() { - // The last item in the list is always int.MaxValue - _entries = new[] { new Entry { PropertyId = int.MaxValue, Value = default } }; + _entries = s_emptyEntries; } private (int, bool) TryFindEntry(int propertyId) @@ -86,7 +91,7 @@ namespace Avalonia.Utilities return (0, false); } - public bool TryGetValue(AvaloniaProperty property, out TValue value) + public bool TryGetValue(AvaloniaProperty property, [MaybeNull] out TValue value) { (int index, bool found) = TryFindEntry(property.Id); if (!found) @@ -132,7 +137,18 @@ namespace Avalonia.Utilities if (found) { - Entry[] entries = new Entry[_entries.Length - 1]; + var newLength = _entries.Length - 1; + + // Special case - one element left means that value store is empty so we can just reuse our "empty" array. + if (newLength == 1) + { + _entries = s_emptyEntries; + + return; + } + + var entries = new Entry[newLength]; + int ix = 0; for (int i = 0; i < _entries.Length; ++i) diff --git a/src/Avalonia.Base/Utilities/MathUtilities.cs b/src/Avalonia.Base/Utilities/MathUtilities.cs index 2a92e75f58..446b366dc8 100644 --- a/src/Avalonia.Base/Utilities/MathUtilities.cs +++ b/src/Avalonia.Base/Utilities/MathUtilities.cs @@ -5,7 +5,10 @@ namespace Avalonia.Utilities /// /// Provides math utilities not provided in System.Math. /// - public static class MathUtilities +#if !BUILDTASK + public +#endif + static class MathUtilities { // smallest such that 1.0+DoubleEpsilon != 1.0 internal static readonly double DoubleEpsilon = 2.2204460492503131e-016; diff --git a/src/Avalonia.Base/Utilities/StringTokenizer.cs b/src/Avalonia.Base/Utilities/StringTokenizer.cs index a159bfb72e..24e99febb9 100644 --- a/src/Avalonia.Base/Utilities/StringTokenizer.cs +++ b/src/Avalonia.Base/Utilities/StringTokenizer.cs @@ -4,7 +4,10 @@ using static System.Char; namespace Avalonia.Utilities { - public struct StringTokenizer : IDisposable +#if !BUILDTASK + public +#endif + struct StringTokenizer : IDisposable { private const char DefaultSeparatorChar = ','; diff --git a/src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj b/src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj index 94ad4adb7d..90f6abc873 100644 --- a/src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj +++ b/src/Avalonia.Build.Tasks/Avalonia.Build.Tasks.csproj @@ -1,7 +1,7 @@  netstandard2.0 - netstandard2.0;netcoreapp2.0 + netstandard2.0;netcoreapp3.1 exe false tools @@ -45,6 +45,12 @@ Markup/%(RecursiveDir)%(FileName)%(Extension) + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + Markup/%(RecursiveDir)%(FileName)%(Extension) @@ -57,6 +63,33 @@ Markup/%(RecursiveDir)%(FileName)%(Extension) + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + + + Markup/%(RecursiveDir)%(FileName)%(Extension) + diff --git a/src/Avalonia.Build.Tasks/SpanCompat.cs b/src/Avalonia.Build.Tasks/SpanCompat.cs index d5c406293e..f8960f56ec 100644 --- a/src/Avalonia.Build.Tasks/SpanCompat.cs +++ b/src/Avalonia.Build.Tasks/SpanCompat.cs @@ -1,3 +1,4 @@ +#if !NETCOREAPP3_1 namespace System { // This is a hack to enable our span code to work inside MSBuild task without referencing System.Memory @@ -63,6 +64,8 @@ namespace System } public override string ToString() => _length == 0 ? string.Empty : _s.Substring(_start, _length); + + public static implicit operator ReadOnlySpan(char[] arr) => new ReadOnlySpan(new string(arr)); } static class SpanCompatExtensions @@ -71,3 +74,4 @@ namespace System } } +#endif diff --git a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs index 0b9b50e771..6ef8a98fae 100644 --- a/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs +++ b/src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs @@ -46,7 +46,9 @@ namespace Avalonia.Build.Tasks string output, bool verifyIl, MessageImportance logImportance, string strongNameKey, bool patchCom, bool skipXamlCompilation) { - var typeSystem = new CecilTypeSystem(references.Concat(new[] { input }), input); + var typeSystem = new CecilTypeSystem(references + .Where(r => !r.ToLowerInvariant().EndsWith("avalonia.build.tasks.dll")) + .Concat(new[] { input }), input); var asm = typeSystem.TargetAssemblyDefinition; diff --git a/src/Avalonia.Controls/ComboBox.cs b/src/Avalonia.Controls/ComboBox.cs index 02a9daee75..7f2acb58fe 100644 --- a/src/Avalonia.Controls/ComboBox.cs +++ b/src/Avalonia.Controls/ComboBox.cs @@ -173,11 +173,10 @@ namespace Avalonia.Controls ComboBoxItem.ContentTemplateProperty); } - /// - protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) + protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e) { - base.OnAttachedToLogicalTree(e); - this.UpdateSelectionBoxItem(this.SelectedItem); + base.OnAttachedToVisualTree(e); + this.UpdateSelectionBoxItem(SelectedItem); } /// @@ -373,19 +372,22 @@ namespace Avalonia.Controls if (control != null) { - control.Measure(Size.Infinity); - - SelectionBoxItem = new Rectangle + if (VisualRoot is object) { - Width = control.DesiredSize.Width, - Height = control.DesiredSize.Height, - Fill = new VisualBrush + control.Measure(Size.Infinity); + + SelectionBoxItem = new Rectangle { - Visual = control, - Stretch = Stretch.None, - AlignmentX = AlignmentX.Left, - } - }; + Width = control.DesiredSize.Width, + Height = control.DesiredSize.Height, + Fill = new VisualBrush + { + Visual = control, + Stretch = Stretch.None, + AlignmentX = AlignmentX.Left, + } + }; + } } else { diff --git a/src/Avalonia.Controls/GridLength.cs b/src/Avalonia.Controls/GridLength.cs index 57f308d59f..b8418949d9 100644 --- a/src/Avalonia.Controls/GridLength.cs +++ b/src/Avalonia.Controls/GridLength.cs @@ -8,7 +8,10 @@ namespace Avalonia.Controls /// /// Defines the valid units for a . /// - public enum GridUnitType +#if !BUILDTASK + public +#endif + enum GridUnitType { /// /// The row or column is auto-sized to fit its content. @@ -29,7 +32,10 @@ namespace Avalonia.Controls /// /// Holds the width or height of a 's column and row definitions. /// - public struct GridLength : IEquatable +#if !BUILDTASK + public +#endif + struct GridLength : IEquatable { private readonly GridUnitType _type; diff --git a/src/Avalonia.Controls/NativeMenuBar.cs b/src/Avalonia.Controls/NativeMenuBar.cs index 63bb39108f..5d2e1087a7 100644 --- a/src/Avalonia.Controls/NativeMenuBar.cs +++ b/src/Avalonia.Controls/NativeMenuBar.cs @@ -1,7 +1,6 @@ using System; using Avalonia.Controls.Primitives; using Avalonia.Interactivity; -using Avalonia.Styling; namespace Avalonia.Controls { @@ -16,7 +15,7 @@ namespace Avalonia.Controls EnableMenuItemClickForwardingProperty.Changed.Subscribe(args => { var item = (MenuItem)args.Sender; - if (args.NewValue.Equals(true)) + if (args.NewValue.GetValueOrDefault()) item.Click += OnMenuItemClick; else item.Click -= OnMenuItemClick; diff --git a/src/Avalonia.Controls/NativeMenuItem.cs b/src/Avalonia.Controls/NativeMenuItem.cs index a0fec9e677..76197b62ad 100644 --- a/src/Avalonia.Controls/NativeMenuItem.cs +++ b/src/Avalonia.Controls/NativeMenuItem.cs @@ -2,6 +2,7 @@ using System; using System.Windows.Input; using Avalonia.Input; using Avalonia.Media.Imaging; +using Avalonia.Metadata; using Avalonia.Utilities; namespace Avalonia.Controls @@ -62,6 +63,7 @@ namespace Avalonia.Controls public static readonly DirectProperty MenuProperty = AvaloniaProperty.RegisterDirect(nameof(Menu), o => o.Menu, (o, v) => o.Menu = v); + [Content] public NativeMenu Menu { get => _menu; @@ -151,7 +153,7 @@ namespace Avalonia.Controls IsEnabled = _command?.CanExecute(null) ?? true; } - public bool HasClickHandlers => Clicked != null; + public bool HasClickHandlers => Click != null; public ICommand Command { @@ -182,11 +184,21 @@ namespace Avalonia.Controls set { SetValue(CommandParameterProperty, value); } } - public event EventHandler Clicked; + /// + /// Occurs when a is clicked. + /// + public event EventHandler Click; + + [Obsolete("Use Click event.")] + public event EventHandler Clicked + { + add => Click += value; + remove => Click -= value; + } void INativeMenuItemExporterEventsImplBridge.RaiseClicked() { - Clicked?.Invoke(this, new EventArgs()); + Click?.Invoke(this, new EventArgs()); if (Command?.CanExecute(CommandParameter) == true) { diff --git a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs index a54d1ce308..5cbd3698b6 100644 --- a/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs +++ b/src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs @@ -231,7 +231,7 @@ namespace Avalonia.Controls.Platform { var direction = e.Key.ToNavigationDirection(); - if (direction.HasValue) + if (direction?.IsDirectional() == true) { if (item == null && _isContextMenu) { diff --git a/src/Avalonia.Controls/Primitives/ScrollBar.cs b/src/Avalonia.Controls/Primitives/ScrollBar.cs index a7fb7ae08c..d264ed76cc 100644 --- a/src/Avalonia.Controls/Primitives/ScrollBar.cs +++ b/src/Avalonia.Controls/Primitives/ScrollBar.cs @@ -35,7 +35,7 @@ namespace Avalonia.Controls.Primitives /// Defines the property. /// public static readonly StyledProperty VisibilityProperty = - AvaloniaProperty.Register(nameof(Visibility)); + AvaloniaProperty.Register(nameof(Visibility), ScrollBarVisibility.Visible); /// /// Defines the property. diff --git a/src/Avalonia.Controls/Primitives/TemplatedControl.cs b/src/Avalonia.Controls/Primitives/TemplatedControl.cs index d18cf7da71..9c73ff2411 100644 --- a/src/Avalonia.Controls/Primitives/TemplatedControl.cs +++ b/src/Avalonia.Controls/Primitives/TemplatedControl.cs @@ -354,11 +354,14 @@ namespace Avalonia.Controls.Primitives { control.SetValue(TemplatedParentProperty, this); - foreach (var child in control.LogicalChildren) + var children = control.LogicalChildren; + var count = children.Count; + + for (var i = 0; i < count; i++) { - if (child is IControl c) + if (children[i] is IControl child) { - ApplyTemplatedParent(c); + ApplyTemplatedParent(child); } } } diff --git a/src/Avalonia.Controls/Slider.cs b/src/Avalonia.Controls/Slider.cs index 6e08e78813..fe4b24099f 100644 --- a/src/Avalonia.Controls/Slider.cs +++ b/src/Avalonia.Controls/Slider.cs @@ -3,6 +3,7 @@ using Avalonia.Collections; using Avalonia.Controls.Metadata; using Avalonia.Controls.Mixins; using Avalonia.Controls.Primitives; +using Avalonia.Data; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Layout; @@ -94,6 +95,8 @@ namespace Avalonia.Controls Thumb.DragStartedEvent.AddClassHandler((x, e) => x.OnThumbDragStarted(e), RoutingStrategies.Bubble); Thumb.DragCompletedEvent.AddClassHandler((x, e) => x.OnThumbDragCompleted(e), RoutingStrategies.Bubble); + + ValueProperty.OverrideMetadata(new DirectPropertyMetadata(enableDataValidation: true)); } /// @@ -225,6 +228,14 @@ namespace Avalonia.Controls Value = IsSnapToTickEnabled ? SnapToTick(finalValue) : finalValue; } + protected override void UpdateDataValidation(AvaloniaProperty property, BindingValue value) + { + if (property == ValueProperty) + { + DataValidationErrors.SetError(this, value.Error); + } + } + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) { base.OnPropertyChanged(change); diff --git a/src/Avalonia.Input/NavigationDirection.cs b/src/Avalonia.Input/NavigationDirection.cs index 9b9af0b0a6..9bd6a8bb42 100644 --- a/src/Avalonia.Input/NavigationDirection.cs +++ b/src/Avalonia.Input/NavigationDirection.cs @@ -83,7 +83,7 @@ namespace Avalonia.Input /// public static bool IsDirectional(this NavigationDirection direction) { - return direction > NavigationDirection.Previous || + return direction > NavigationDirection.Previous && direction <= NavigationDirection.PageDown; } diff --git a/src/Avalonia.MicroCom/MicroComProxyBase.cs b/src/Avalonia.MicroCom/MicroComProxyBase.cs index 681e278104..140af3e4ef 100644 --- a/src/Avalonia.MicroCom/MicroComProxyBase.cs +++ b/src/Avalonia.MicroCom/MicroComProxyBase.cs @@ -56,7 +56,7 @@ namespace Avalonia.MicroCom { var guid = MicroComRuntime.GetGuidFor(typeof(T)); var rv = QueryInterface(guid, out var ppv); - if (rv != 0) + if (rv == 0) return (T)MicroComRuntime.CreateProxyFor(typeof(T), ppv, true); throw new COMException("QueryInterface failed", rv); } diff --git a/src/Avalonia.MicroCom/MicroComRuntime.cs b/src/Avalonia.MicroCom/MicroComRuntime.cs index 9d2c23f40a..85507674d2 100644 --- a/src/Avalonia.MicroCom/MicroComRuntime.cs +++ b/src/Avalonia.MicroCom/MicroComRuntime.cs @@ -38,13 +38,20 @@ namespace Avalonia.MicroCom public static T CreateProxyFor(IntPtr pObject, bool ownsHandle) => (T)CreateProxyFor(typeof(T), pObject, ownsHandle); public static object CreateProxyFor(Type type, IntPtr pObject, bool ownsHandle) => _factories[type](pObject, ownsHandle); - + + public static IntPtr GetNativeIntPtr(this T obj, bool owned = false) where T : IUnknown + => new IntPtr(GetNativePointer(obj, owned)); public static void* GetNativePointer(T obj, bool owned = false) where T : IUnknown { if (obj == null) return null; if (obj is MicroComProxyBase proxy) + { + if(owned) + proxy.AddRef(); return (void*)proxy.NativePointer; + } + if (obj is IMicroComShadowContainer container) { container.Shadow ??= new MicroComShadow(container); @@ -89,10 +96,27 @@ namespace Avalonia.MicroCom } - public static T CloneReference(T iface) where T : IUnknown + public static T CloneReference(this T iface) where T : IUnknown { + var proxy = (MicroComProxyBase)(object)iface; var ownedPointer = GetNativePointer(iface, true); return CreateProxyFor(ownedPointer, true); } + + public static T QueryInterface(this IUnknown unknown) where T : IUnknown + { + var proxy = (MicroComProxyBase)unknown; + return proxy.QueryInterface(); + } + + public static void UnsafeAddRef(this IUnknown unknown) + { + ((MicroComProxyBase)unknown).AddRef(); + } + + public static void UnsafeRelease(this IUnknown unknown) + { + ((MicroComProxyBase)unknown).Release(); + } } } diff --git a/src/Avalonia.MicroCom/MicroComVtblBase.cs b/src/Avalonia.MicroCom/MicroComVtblBase.cs index e06e2d4934..2f0607c0a8 100644 --- a/src/Avalonia.MicroCom/MicroComVtblBase.cs +++ b/src/Avalonia.MicroCom/MicroComVtblBase.cs @@ -7,10 +7,10 @@ namespace Avalonia.MicroCom public unsafe class MicroComVtblBase { private List _methods = new List(); - [UnmanagedFunctionPointerAttribute(CallingConvention.ThisCall)] + [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)] private delegate int AddRefDelegate(Ccw* ccw); - [UnmanagedFunctionPointerAttribute(CallingConvention.ThisCall)] + [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)] private delegate int QueryInterfaceDelegate(Ccw* ccw, Guid* guid, void** ppv); public static IntPtr Vtable { get; } = new MicroComVtblBase().CreateVTable(); diff --git a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs index b192de95de..2e3408eca5 100644 --- a/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs +++ b/src/Avalonia.Native/AvaloniaNativeMenuExporter.cs @@ -60,7 +60,7 @@ namespace Avalonia.Native Header = "About Avalonia", }; - aboutItem.Clicked += async (sender, e) => + aboutItem.Click += async (sender, e) => { var dialog = new AboutAvaloniaDialog(); diff --git a/src/Avalonia.Native/Extensions.cs b/src/Avalonia.Native/Extensions.cs new file mode 100644 index 0000000000..c0d90f7a85 --- /dev/null +++ b/src/Avalonia.Native/Extensions.cs @@ -0,0 +1,8 @@ +namespace Avalonia.Native +{ + internal static class Extensions + { + public static int AsComBool(this bool b) => b ? 1 : 0; + public static bool FromComBool(this int b) => b != 0; + } +} diff --git a/src/Avalonia.Native/IAvnMenuItem.cs b/src/Avalonia.Native/IAvnMenuItem.cs index e2feffaa33..4c0c81394f 100644 --- a/src/Avalonia.Native/IAvnMenuItem.cs +++ b/src/Avalonia.Native/IAvnMenuItem.cs @@ -24,7 +24,7 @@ namespace Avalonia.Native.Interop.Impl private void UpdateTitle(string title) => SetTitle(title ?? ""); - private void UpdateIsChecked(bool isChecked) => SetIsChecked(isChecked); + private void UpdateIsChecked(bool isChecked) => SetIsChecked(isChecked.AsComBool()); private void UpdateToggleType(NativeMenuItemToggleType toggleType) { diff --git a/src/Avalonia.Native/PlatformThreadingInterface.cs b/src/Avalonia.Native/PlatformThreadingInterface.cs index df69f2eafb..882d5a2ed3 100644 --- a/src/Avalonia.Native/PlatformThreadingInterface.cs +++ b/src/Avalonia.Native/PlatformThreadingInterface.cs @@ -33,9 +33,9 @@ namespace Avalonia.Native _parent = parent; } - public void Signaled(int priority, bool priorityContainsMeaningfulValue) + public void Signaled(int priority, int priorityContainsMeaningfulValue) { - _parent.Signaled?.Invoke(priorityContainsMeaningfulValue ? (DispatcherPriority?)priority : null); + _parent.Signaled?.Invoke(priorityContainsMeaningfulValue.FromComBool() ? (DispatcherPriority?)priority : null); } } @@ -50,7 +50,7 @@ namespace Avalonia.Native _native.SetSignaledCallback(cb); } - public bool CurrentThreadIsLoopThread => _native.CurrentThreadIsLoopThread; + public bool CurrentThreadIsLoopThread => _native.CurrentThreadIsLoopThread.FromComBool(); public event Action Signaled; diff --git a/src/Avalonia.Native/PopupImpl.cs b/src/Avalonia.Native/PopupImpl.cs index 2f98385038..60c552a937 100644 --- a/src/Avalonia.Native/PopupImpl.cs +++ b/src/Avalonia.Native/PopupImpl.cs @@ -50,9 +50,9 @@ namespace Avalonia.Native // NOP on Popup } - bool IAvnWindowEvents.Closing() + int IAvnWindowEvents.Closing() { - return true; + return true.AsComBool(); } void IAvnWindowEvents.WindowStateChanged(AvnWindowState state) diff --git a/src/Avalonia.Native/PredicateCallback.cs b/src/Avalonia.Native/PredicateCallback.cs index 1ed2ae36af..19c470bcb3 100644 --- a/src/Avalonia.Native/PredicateCallback.cs +++ b/src/Avalonia.Native/PredicateCallback.cs @@ -12,9 +12,9 @@ namespace Avalonia.Native _predicate = predicate; } - bool IAvnPredicateCallback.Evaluate() + int IAvnPredicateCallback.Evaluate() { - return _predicate(); + return _predicate().AsComBool(); } } } diff --git a/src/Avalonia.Native/ScreenImpl.cs b/src/Avalonia.Native/ScreenImpl.cs index ae6da01388..7b4a001486 100644 --- a/src/Avalonia.Native/ScreenImpl.cs +++ b/src/Avalonia.Native/ScreenImpl.cs @@ -33,7 +33,7 @@ namespace Avalonia.Native screen.PixelDensity, screen.Bounds.ToAvaloniaPixelRect(), screen.WorkingArea.ToAvaloniaPixelRect(), - screen.Primary); + screen.Primary.FromComBool()); } return result; diff --git a/src/Avalonia.Native/SystemDialogs.cs b/src/Avalonia.Native/SystemDialogs.cs index 0239fc680d..8012813644 100644 --- a/src/Avalonia.Native/SystemDialogs.cs +++ b/src/Avalonia.Native/SystemDialogs.cs @@ -26,7 +26,7 @@ namespace Avalonia.Native if (dialog is OpenFileDialog ofd) { _native.OpenFileDialog(nativeParent, - events, ofd.AllowMultiple, + events, ofd.AllowMultiple.AsComBool(), ofd.Title ?? "", ofd.Directory ?? "", ofd.InitialFileName ?? "", diff --git a/src/Avalonia.Native/WindowImpl.cs b/src/Avalonia.Native/WindowImpl.cs index b42831854d..f60e83efe3 100644 --- a/src/Avalonia.Native/WindowImpl.cs +++ b/src/Avalonia.Native/WindowImpl.cs @@ -42,14 +42,14 @@ namespace Avalonia.Native _parent = parent; } - bool IAvnWindowEvents.Closing() + int IAvnWindowEvents.Closing() { if (_parent.Closing != null) { - return _parent.Closing(); + return _parent.Closing().AsComBool(); } - return true; + return true.AsComBool(); } void IAvnWindowEvents.WindowStateChanged(AvnWindowState state) @@ -69,7 +69,7 @@ namespace Avalonia.Native public void CanResize(bool value) { - _native.SetCanResize(value); + _native.SetCanResize(value.AsComBool()); } public void SetSystemDecorations(Controls.SystemDecorations enabled) @@ -145,7 +145,7 @@ namespace Avalonia.Native { _isExtended = extendIntoClientAreaHint; - _native.SetExtendClientArea(extendIntoClientAreaHint); + _native.SetExtendClientArea(extendIntoClientAreaHint.AsComBool()); InvalidateExtendedMargins(); } @@ -198,7 +198,7 @@ namespace Avalonia.Native public void SetEnabled(bool enable) { - _native.SetEnabled(enable); + _native.SetEnabled(enable.AsComBool()); } } } diff --git a/src/Avalonia.Native/WindowImplBase.cs b/src/Avalonia.Native/WindowImplBase.cs index 20b6b8ecc5..88c3144884 100644 --- a/src/Avalonia.Native/WindowImplBase.cs +++ b/src/Avalonia.Native/WindowImplBase.cs @@ -174,7 +174,7 @@ namespace Avalonia.Native void IAvnWindowBaseEvents.Resized(AvnSize* size) { - if (_parent._native != null) + if (_parent?._native != null) { var s = new Size(size->Width, size->Height); _parent._savedLogicalSize = s; @@ -192,14 +192,14 @@ namespace Avalonia.Native _parent.RawMouseEvent(type, timeStamp, modifiers, point, delta); } - bool IAvnWindowBaseEvents.RawKeyEvent(AvnRawKeyEventType type, uint timeStamp, AvnInputModifiers modifiers, uint key) + int IAvnWindowBaseEvents.RawKeyEvent(AvnRawKeyEventType type, uint timeStamp, AvnInputModifiers modifiers, uint key) { - return _parent.RawKeyEvent(type, timeStamp, modifiers, key); + return _parent.RawKeyEvent(type, timeStamp, modifiers, key).AsComBool(); } - bool IAvnWindowBaseEvents.RawTextInputEvent(uint timeStamp, string text) + int IAvnWindowBaseEvents.RawTextInputEvent(uint timeStamp, string text) { - return _parent.RawTextInputEvent(timeStamp, text); + return _parent.RawTextInputEvent(timeStamp, text).AsComBool(); } @@ -388,7 +388,7 @@ namespace Avalonia.Native public void SetTopmost(bool value) { - _native.SetTopMost(value); + _native.SetTopMost(value.AsComBool()); } public double RenderScaling => _native?.Scaling ?? 1; @@ -456,7 +456,7 @@ namespace Avalonia.Native TransparencyLevel = transparencyLevel; - _native?.SetBlurEnabled(TransparencyLevel >= WindowTransparencyLevel.Blur); + _native?.SetBlurEnabled((TransparencyLevel >= WindowTransparencyLevel.Blur).AsComBool()); TransparencyLevelChanged?.Invoke(TransparencyLevel); } } diff --git a/src/Avalonia.Native/avn.idl b/src/Avalonia.Native/avn.idl index 1d36cce20d..3f33476c3d 100644 --- a/src/Avalonia.Native/avn.idl +++ b/src/Avalonia.Native/avn.idl @@ -1,5 +1,6 @@ @clr-namespace Avalonia.Native.Interop @clr-access internal +@clr-map bool int @cpp-preamble @@ #include "com.h" #include "key.h" diff --git a/src/Avalonia.OpenGL/Egl/EglContext.cs b/src/Avalonia.OpenGL/Egl/EglContext.cs index 249b4d547f..58137dfff0 100644 --- a/src/Avalonia.OpenGL/Egl/EglContext.cs +++ b/src/Avalonia.OpenGL/Egl/EglContext.cs @@ -93,6 +93,14 @@ namespace Avalonia.OpenGL.Egl return MakeCurrent(); } + public IDisposable EnsureLocked() + { + if (IsCurrent) + return Disposable.Empty; + Monitor.Enter(_lock); + return Disposable.Create(() => Monitor.Exit(_lock)); + } + public bool IsSharedWith(IGlContext context) { var c = (EglContext)context; diff --git a/src/Avalonia.Styling/ApiCompatBaseline.txt b/src/Avalonia.Styling/ApiCompatBaseline.txt new file mode 100644 index 0000000000..001e165830 --- /dev/null +++ b/src/Avalonia.Styling/ApiCompatBaseline.txt @@ -0,0 +1,3 @@ +Compat issues with assembly Avalonia.Styling: +MembersMustExist : Member 'public System.IObservable Avalonia.Controls.ResourceNodeExtensions.GetResourceObservable(Avalonia.IStyledElement, System.Object, System.Func)' does not exist in the implementation but it does exist in the contract. +Total Issues: 1 diff --git a/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs b/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs index 250274c39b..be1069a4da 100644 --- a/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs +++ b/src/Avalonia.Styling/Controls/ResourceNodeExtensions.cs @@ -60,7 +60,7 @@ namespace Avalonia.Controls } public static IObservable GetResourceObservable( - this IStyledElement control, + this IResourceHost control, object key, Func? converter = null) { @@ -83,11 +83,11 @@ namespace Avalonia.Controls private class ResourceObservable : LightweightObservableBase { - private readonly IStyledElement _target; + private readonly IResourceHost _target; private readonly object _key; private readonly Func? _converter; - public ResourceObservable(IStyledElement target, object key, Func? converter) + public ResourceObservable(IResourceHost target, object key, Func? converter) { _target = target; _key = key; @@ -147,13 +147,16 @@ namespace Avalonia.Controls { if (_target.Owner is object) { - observer.OnNext(Convert(_target.Owner?.FindResource(_key))); + observer.OnNext(Convert(_target.Owner.FindResource(_key))); } } private void PublishNext() { - PublishNext(Convert(_target.Owner?.FindResource(_key))); + if (_target.Owner is object) + { + PublishNext(Convert(_target.Owner.FindResource(_key))); + } } private void OwnerChanged(object sender, EventArgs e) diff --git a/src/Avalonia.Styling/StyledElement.cs b/src/Avalonia.Styling/StyledElement.cs index 65885ddebe..cc8d91462d 100644 --- a/src/Avalonia.Styling/StyledElement.cs +++ b/src/Avalonia.Styling/StyledElement.cs @@ -1,8 +1,8 @@ using System; +using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.ComponentModel; -using System.Linq; using Avalonia.Animation; using Avalonia.Collections; using Avalonia.Controls; @@ -479,16 +479,16 @@ namespace Avalonia switch (e.Action) { case NotifyCollectionChangedAction.Add: - SetLogicalParent(e.NewItems.Cast()); + SetLogicalParent(e.NewItems); break; case NotifyCollectionChangedAction.Remove: - ClearLogicalParent(e.OldItems.Cast()); + ClearLogicalParent(e.OldItems); break; case NotifyCollectionChangedAction.Replace: - ClearLogicalParent(e.OldItems.Cast()); - SetLogicalParent(e.NewItems.Cast()); + ClearLogicalParent(e.OldItems); + SetLogicalParent(e.NewItems); break; case NotifyCollectionChangedAction.Reset: @@ -702,13 +702,32 @@ namespace Avalonia OnDataContextChanged(EventArgs.Empty); } - private void SetLogicalParent(IEnumerable children) + private void SetLogicalParent(IList children) { - foreach (var i in children) + var count = children.Count; + + for (var i = 0; i < count; i++) + { + var logical = (ILogical) children[i]; + + if (logical.LogicalParent is null) + { + ((ISetLogicalParent)logical).SetParent(this); + } + } + } + + private void ClearLogicalParent(IList children) + { + var count = children.Count; + + for (var i = 0; i < count; i++) { - if (i.LogicalParent == null) + var logical = (ILogical) children[i]; + + if (logical.LogicalParent == this) { - ((ISetLogicalParent)i).SetParent(this); + ((ISetLogicalParent)logical).SetParent(null); } } } @@ -784,17 +803,6 @@ namespace Avalonia } } - private void ClearLogicalParent(IEnumerable children) - { - foreach (var i in children) - { - if (i.LogicalParent == this) - { - ((ISetLogicalParent)i).SetParent(null); - } - } - } - private void NotifyResourcesChanged( ResourcesChangedEventArgs? e = null, bool propagate = true) diff --git a/src/Avalonia.Themes.Default/PathIcon.xaml b/src/Avalonia.Themes.Default/PathIcon.xaml index a2d01f7b5b..039f0ef10c 100644 --- a/src/Avalonia.Themes.Default/PathIcon.xaml +++ b/src/Avalonia.Themes.Default/PathIcon.xaml @@ -2,16 +2,19 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> diff --git a/src/Avalonia.Themes.Default/ToggleSwitch.xaml b/src/Avalonia.Themes.Default/ToggleSwitch.xaml index 9d1c024eb9..f11f77df5a 100644 --- a/src/Avalonia.Themes.Default/ToggleSwitch.xaml +++ b/src/Avalonia.Themes.Default/ToggleSwitch.xaml @@ -5,7 +5,7 @@ 0,0,0,6 6 6 - 154 + 0 0 1 diff --git a/src/Avalonia.Themes.Fluent/PathIcon.xaml b/src/Avalonia.Themes.Fluent/PathIcon.xaml index 0a2c747514..d4952b3571 100644 --- a/src/Avalonia.Themes.Fluent/PathIcon.xaml +++ b/src/Avalonia.Themes.Fluent/PathIcon.xaml @@ -10,16 +10,19 @@ diff --git a/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml b/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml index 2491225a44..45537e41ad 100644 --- a/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml +++ b/src/Avalonia.Themes.Fluent/ToggleSwitch.xaml @@ -5,7 +5,7 @@ 0,0,0,6 6 6 - 154 + 0 diff --git a/src/Avalonia.Visuals/ApiCompatBaseline.txt b/src/Avalonia.Visuals/ApiCompatBaseline.txt index 2d00d82a46..62f4def701 100644 --- a/src/Avalonia.Visuals/ApiCompatBaseline.txt +++ b/src/Avalonia.Visuals/ApiCompatBaseline.txt @@ -1,39 +1,5 @@ Compat issues with assembly Avalonia.Visuals: -MembersMustExist : Member 'public void Avalonia.Media.DrawingContext.DrawGlyphRun(Avalonia.Media.IBrush, Avalonia.Media.GlyphRun, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Media.Typeface Avalonia.Media.FontManager.GetOrAddTypeface(Avalonia.Media.FontFamily, Avalonia.Media.FontStyle, Avalonia.Media.FontWeight)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Media.Typeface Avalonia.Media.FontManager.MatchCharacter(System.Int32, Avalonia.Media.FontStyle, Avalonia.Media.FontWeight, Avalonia.Media.FontFamily, System.Globalization.CultureInfo)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Rect Avalonia.Media.Geometry.GetRenderBounds(Avalonia.Media.Pen)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public System.Boolean Avalonia.Media.Geometry.StrokeContains(Avalonia.Media.Pen, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Rect Avalonia.Media.GlyphRun.Bounds.get()' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.StyledProperty Avalonia.StyledProperty Avalonia.Media.GlyphRunDrawing.BaselineOriginProperty' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Point Avalonia.Media.GlyphRunDrawing.BaselineOrigin.get()' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public void Avalonia.Media.GlyphRunDrawing.BaselineOrigin.set(Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -CannotSealType : Type 'Avalonia.Media.Typeface' is actually (has the sealed modifier) sealed in the implementation but not sealed in the contract. -TypeCannotChangeClassification : Type 'Avalonia.Media.Typeface' is a 'struct' in the implementation but is a 'class' in the contract. -CannotMakeMemberNonVirtual : Member 'public System.Boolean Avalonia.Media.Typeface.Equals(System.Object)' is non-virtual in the implementation but is virtual in the contract. -CannotMakeMemberNonVirtual : Member 'public System.Int32 Avalonia.Media.Typeface.GetHashCode()' is non-virtual in the implementation but is virtual in the contract. -TypesMustExist : Type 'Avalonia.Media.Fonts.FontKey' does not exist in the implementation but it does exist in the contract. -CannotAddAbstractMembers : Member 'public Avalonia.Size Avalonia.Media.TextFormatting.DrawableTextRun.Size' is abstract in the implementation but is missing in the contract. -MembersMustExist : Member 'public Avalonia.Rect Avalonia.Media.TextFormatting.DrawableTextRun.Bounds.get()' does not exist in the implementation but it does exist in the contract. -CannotAddAbstractMembers : Member 'public void Avalonia.Media.TextFormatting.DrawableTextRun.Draw(Avalonia.Media.DrawingContext)' is abstract in the implementation but is missing in the contract. -MembersMustExist : Member 'public void Avalonia.Media.TextFormatting.DrawableTextRun.Draw(Avalonia.Media.DrawingContext, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -CannotAddAbstractMembers : Member 'public Avalonia.Size Avalonia.Media.TextFormatting.DrawableTextRun.Size.get()' is abstract in the implementation but is missing in the contract. -MembersMustExist : Member 'public Avalonia.Rect Avalonia.Media.TextFormatting.ShapedTextCharacters.Bounds.get()' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public void Avalonia.Media.TextFormatting.ShapedTextCharacters.Draw(Avalonia.Media.DrawingContext, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public void Avalonia.Media.TextFormatting.TextLayout.Draw(Avalonia.Media.DrawingContext, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -CannotAddAbstractMembers : Member 'public Avalonia.Media.TextFormatting.TextLineBreak Avalonia.Media.TextFormatting.TextLine.TextLineBreak' is abstract in the implementation but is missing in the contract. -CannotAddAbstractMembers : Member 'public void Avalonia.Media.TextFormatting.TextLine.Draw(Avalonia.Media.DrawingContext)' is abstract in the implementation but is missing in the contract. -MembersMustExist : Member 'public void Avalonia.Media.TextFormatting.TextLine.Draw(Avalonia.Media.DrawingContext, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -MembersMustExist : Member 'public Avalonia.Media.TextFormatting.TextLineBreak Avalonia.Media.TextFormatting.TextLine.LineBreak.get()' does not exist in the implementation but it does exist in the contract. -CannotAddAbstractMembers : Member 'public Avalonia.Media.TextFormatting.TextLineBreak Avalonia.Media.TextFormatting.TextLine.TextLineBreak.get()' is abstract in the implementation but is missing in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public Avalonia.Platform.IDrawingContextLayerImpl Avalonia.Platform.IDrawingContextImpl.CreateLayer(Avalonia.Size)' is present in the implementation but not in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public Avalonia.Platform.IRenderTargetBitmapImpl Avalonia.Platform.IDrawingContextImpl.CreateLayer(Avalonia.Size)' is present in the contract but not in the implementation. -MembersMustExist : Member 'public Avalonia.Platform.IRenderTargetBitmapImpl Avalonia.Platform.IDrawingContextImpl.CreateLayer(Avalonia.Size)' does not exist in the implementation but it does exist in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawGlyphRun(Avalonia.Media.IBrush, Avalonia.Media.GlyphRun)' is present in the implementation but not in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public void Avalonia.Platform.IDrawingContextImpl.DrawGlyphRun(Avalonia.Media.IBrush, Avalonia.Media.GlyphRun, Avalonia.Point)' is present in the contract but not in the implementation. -MembersMustExist : Member 'public void Avalonia.Platform.IDrawingContextImpl.DrawGlyphRun(Avalonia.Media.IBrush, Avalonia.Media.GlyphRun, Avalonia.Point)' does not exist in the implementation but it does exist in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IFontManagerImpl.TryMatchCharacter(System.Int32, Avalonia.Media.FontStyle, Avalonia.Media.FontWeight, Avalonia.Media.FontFamily, System.Globalization.CultureInfo, Avalonia.Media.Fonts.FontKey)' is present in the contract but not in the implementation. -MembersMustExist : Member 'public System.Boolean Avalonia.Platform.IFontManagerImpl.TryMatchCharacter(System.Int32, Avalonia.Media.FontStyle, Avalonia.Media.FontWeight, Avalonia.Media.FontFamily, System.Globalization.CultureInfo, Avalonia.Media.Fonts.FontKey)' does not exist in the implementation but it does exist in the contract. -InterfacesShouldHaveSameMembers : Interface member 'public System.Boolean Avalonia.Platform.IFontManagerImpl.TryMatchCharacter(System.Int32, Avalonia.Media.FontStyle, Avalonia.Media.FontWeight, Avalonia.Media.FontFamily, System.Globalization.CultureInfo, Avalonia.Media.Typeface)' is present in the implementation but not in the contract. -MembersMustExist : Member 'public Avalonia.Utilities.IRef Avalonia.Rendering.RenderLayer.Bitmap.get()' does not exist in the implementation but it does exist in the contract. -Total Issues: 37 +MembersMustExist : Member 'public Avalonia.StyledProperty> Avalonia.StyledProperty> Avalonia.Media.DashStyle.DashesProperty' does not exist in the implementation but it does exist in the contract. +MembersMustExist : Member 'public System.Collections.Generic.IReadOnlyList Avalonia.Media.DashStyle.Dashes.get()' does not exist in the implementation but it does exist in the contract. +MembersMustExist : Member 'public void Avalonia.Media.DashStyle.Dashes.set(System.Collections.Generic.IReadOnlyList)' does not exist in the implementation but it does exist in the contract. +Total Issues: 3 diff --git a/src/Avalonia.Visuals/CornerRadius.cs b/src/Avalonia.Visuals/CornerRadius.cs index c02aacfb4d..037bb16e31 100644 --- a/src/Avalonia.Visuals/CornerRadius.cs +++ b/src/Avalonia.Visuals/CornerRadius.cs @@ -1,6 +1,8 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif using Avalonia.Utilities; namespace Avalonia @@ -8,11 +10,16 @@ namespace Avalonia /// /// Represents the radii of a rectangle's corners. /// - public readonly struct CornerRadius : IEquatable +#if !BUILDTASK + public +#endif + readonly struct CornerRadius : IEquatable { static CornerRadius() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(CornerRadius).IsAssignableFrom(prop.PropertyType)); +#endif } public CornerRadius(double uniformRadius) diff --git a/src/Avalonia.Visuals/Matrix.cs b/src/Avalonia.Visuals/Matrix.cs index 206b842220..2ccfd43f03 100644 --- a/src/Avalonia.Visuals/Matrix.cs +++ b/src/Avalonia.Visuals/Matrix.cs @@ -7,7 +7,10 @@ namespace Avalonia /// /// A 2x3 matrix. /// - public readonly struct Matrix : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Matrix : IEquatable { private readonly double _m11; private readonly double _m12; diff --git a/src/Avalonia.Visuals/Media/Color.cs b/src/Avalonia.Visuals/Media/Color.cs index 16b4f90d57..a57a962db4 100644 --- a/src/Avalonia.Visuals/Media/Color.cs +++ b/src/Avalonia.Visuals/Media/Color.cs @@ -1,17 +1,24 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif namespace Avalonia.Media { /// /// An ARGB color. /// - public readonly struct Color : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Color : IEquatable { static Color() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(Color).IsAssignableFrom(prop.PropertyType)); +#endif } /// @@ -223,7 +230,12 @@ namespace Avalonia.Media if (input.Length == 3 || input.Length == 4) { var extendedLength = 2 * input.Length; + +#if !BUILDTASK Span extended = stackalloc char[extendedLength]; +#else + char[] extended = new char[extendedLength]; +#endif for (int i = 0; i < input.Length; i++) { diff --git a/src/Avalonia.Visuals/Media/DashStyle.cs b/src/Avalonia.Visuals/Media/DashStyle.cs index 1e813edc13..acba2d3615 100644 --- a/src/Avalonia.Visuals/Media/DashStyle.cs +++ b/src/Avalonia.Visuals/Media/DashStyle.cs @@ -1,11 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Collections.Specialized; +using Avalonia.Animation; +using Avalonia.Collections; +using Avalonia.Media.Immutable; + +#nullable enable + namespace Avalonia.Media { - using System; - using System.Collections.Generic; - using System.Linq; - using Avalonia.Animation; - using Avalonia.Media.Immutable; - /// /// Represents the sequence of dashes and gaps that will be applied by a . /// @@ -14,8 +17,8 @@ namespace Avalonia.Media /// /// Defines the property. /// - public static readonly StyledProperty> DashesProperty = - AvaloniaProperty.Register>(nameof(Dashes)); + public static readonly StyledProperty> DashesProperty = + AvaloniaProperty.Register>(nameof(Dashes)); /// /// Defines the property. @@ -23,10 +26,10 @@ namespace Avalonia.Media public static readonly StyledProperty OffsetProperty = AvaloniaProperty.Register(nameof(Offset)); - private static ImmutableDashStyle s_dash; - private static ImmutableDashStyle s_dot; - private static ImmutableDashStyle s_dashDot; - private static ImmutableDashStyle s_dashDotDot; + private static ImmutableDashStyle? s_dash; + private static ImmutableDashStyle? s_dot; + private static ImmutableDashStyle? s_dashDot; + private static ImmutableDashStyle? s_dashDotDot; /// /// Initializes a new instance of the class. @@ -41,9 +44,9 @@ namespace Avalonia.Media /// /// The dashes collection. /// The dash sequence offset. - public DashStyle(IEnumerable dashes, double offset) + public DashStyle(IEnumerable? dashes, double offset) { - Dashes = (IReadOnlyList)dashes?.ToList() ?? Array.Empty(); + Dashes = (dashes as AvaloniaList) ?? new AvaloniaList(dashes ?? Array.Empty()); Offset = offset; } @@ -61,31 +64,27 @@ namespace Avalonia.Media /// /// Represents a dashed . /// - public static IDashStyle Dash => - s_dash ?? (s_dash = new ImmutableDashStyle(new double[] { 2, 2 }, 1)); + public static IDashStyle Dash => s_dash ??= new ImmutableDashStyle(new double[] { 2, 2 }, 1); /// /// Represents a dotted . /// - public static IDashStyle Dot => - s_dot ?? (s_dot = new ImmutableDashStyle(new double[] { 0, 2 }, 0)); + public static IDashStyle Dot => s_dot ??= new ImmutableDashStyle(new double[] { 0, 2 }, 0); /// /// Represents a dashed dotted . /// - public static IDashStyle DashDot => - s_dashDot ?? (s_dashDot = new ImmutableDashStyle(new double[] { 2, 2, 0, 2 }, 1)); + public static IDashStyle DashDot => s_dashDot ??= new ImmutableDashStyle(new double[] { 2, 2, 0, 2 }, 1); /// /// Represents a dashed double dotted . /// - public static IDashStyle DashDotDot => - s_dashDotDot ?? (s_dashDotDot = new ImmutableDashStyle(new double[] { 2, 2, 0, 2, 0, 2 }, 1)); + public static IDashStyle DashDotDot => s_dashDotDot ??= new ImmutableDashStyle(new double[] { 2, 2, 0, 2, 0, 2 }, 1); /// /// Gets or sets the length of alternating dashes and gaps. /// - public IReadOnlyList Dashes + public AvaloniaList Dashes { get => GetValue(DashesProperty); set => SetValue(DashesProperty, value); @@ -100,15 +99,43 @@ namespace Avalonia.Media set => SetValue(OffsetProperty, value); } + IReadOnlyList IDashStyle.Dashes => Dashes; + /// /// Raised when the dash style changes. /// - public event EventHandler Invalidated; + public event EventHandler? Invalidated; /// /// Returns an immutable clone of the . /// /// public ImmutableDashStyle ToImmutable() => new ImmutableDashStyle(Dashes, Offset); + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + + if (change.Property == DashesProperty) + { + var oldValue = change.OldValue.GetValueOrDefault>(); + var newValue = change.NewValue.GetValueOrDefault>(); + + if (oldValue is object) + { + oldValue.CollectionChanged -= DashesChanged; + } + + if (newValue is object) + { + newValue.CollectionChanged += DashesChanged; + } + } + } + + private void DashesChanged(object sender, NotifyCollectionChangedEventArgs e) + { + Invalidated?.Invoke(this, e); + } } } diff --git a/src/Avalonia.Visuals/Media/EllipseGeometry.cs b/src/Avalonia.Visuals/Media/EllipseGeometry.cs index fd73eee69a..bae6dc3d8c 100644 --- a/src/Avalonia.Visuals/Media/EllipseGeometry.cs +++ b/src/Avalonia.Visuals/Media/EllipseGeometry.cs @@ -12,10 +12,28 @@ namespace Avalonia.Media /// public static readonly StyledProperty RectProperty = AvaloniaProperty.Register(nameof(Rect)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty RadiusXProperty = + AvaloniaProperty.Register(nameof(RadiusX)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty RadiusYProperty = + AvaloniaProperty.Register(nameof(RadiusY)); + + /// + /// Defines the property. + /// + public static readonly StyledProperty CenterProperty = + AvaloniaProperty.Register(nameof(Center)); static EllipseGeometry() { - AffectsGeometry(RectProperty); + AffectsGeometry(RectProperty, RadiusXProperty, RadiusYProperty, CenterProperty); } /// @@ -43,6 +61,33 @@ namespace Avalonia.Media set => SetValue(RectProperty, value); } + /// + /// Gets or sets a double that defines the radius in the X-axis of the ellipse. + /// + public double RadiusX + { + get => GetValue(RadiusXProperty); + set => SetValue(RadiusXProperty, value); + } + + /// + /// Gets or sets a double that defines the radius in the Y-axis of the ellipse. + /// + public double RadiusY + { + get => GetValue(RadiusYProperty); + set => SetValue(RadiusYProperty, value); + } + + /// + /// Gets or sets a point that defines the center of the ellipse. + /// + public Point Center + { + get => GetValue(CenterProperty); + set => SetValue(CenterProperty, value); + } + /// public override Geometry Clone() { @@ -54,7 +99,14 @@ namespace Avalonia.Media { var factory = AvaloniaLocator.Current.GetService(); - return factory.CreateEllipseGeometry(Rect); + if (Rect != default) return factory.CreateEllipseGeometry(Rect); + + var originX = Center.X - RadiusX; + var originY = Center.Y - RadiusY; + var width = RadiusX * 2; + var height = RadiusY * 2; + + return factory.CreateEllipseGeometry(new Rect(originX, originY, width, height)); } } } diff --git a/src/Avalonia.Visuals/Media/KnownColors.cs b/src/Avalonia.Visuals/Media/KnownColors.cs index 0887d2c913..fe09f5f538 100644 --- a/src/Avalonia.Visuals/Media/KnownColors.cs +++ b/src/Avalonia.Visuals/Media/KnownColors.cs @@ -8,7 +8,9 @@ namespace Avalonia.Media { private static readonly IReadOnlyDictionary _knownColorNames; private static readonly IReadOnlyDictionary _knownColors; +#if !BUILDTASK private static readonly Dictionary _knownBrushes; +#endif static KnownColors() { @@ -32,14 +34,19 @@ namespace Avalonia.Media _knownColorNames = knownColorNames; _knownColors = knownColors; + +#if !BUILDTASK _knownBrushes = new Dictionary(); +#endif } +#if !BUILDTASK public static ISolidColorBrush GetKnownBrush(string s) { var color = GetKnownColor(s); return color != KnownColor.None ? color.ToBrush() : null; } +#endif public static KnownColor GetKnownColor(string s) { @@ -61,6 +68,7 @@ namespace Avalonia.Media return Color.FromUInt32((uint)color); } +#if !BUILDTASK public static ISolidColorBrush ToBrush(this KnownColor color) { lock (_knownBrushes) @@ -74,6 +82,7 @@ namespace Avalonia.Media return brush; } } +#endif } internal enum KnownColor : uint diff --git a/src/Avalonia.Visuals/Media/PathFigure.cs b/src/Avalonia.Visuals/Media/PathFigure.cs index d0eb67ba39..caf86cb234 100644 --- a/src/Avalonia.Visuals/Media/PathFigure.cs +++ b/src/Avalonia.Visuals/Media/PathFigure.cs @@ -1,3 +1,7 @@ +#nullable enable +using System; +using System.Linq; +using Avalonia.Collections; using Avalonia.Metadata; namespace Avalonia.Media @@ -8,22 +12,36 @@ namespace Avalonia.Media /// Defines the property. /// public static readonly StyledProperty IsClosedProperty - = AvaloniaProperty.Register(nameof(IsClosed), true); + = AvaloniaProperty.Register(nameof(IsClosed), true); + /// /// Defines the property. /// public static readonly StyledProperty IsFilledProperty - = AvaloniaProperty.Register(nameof(IsFilled), true); + = AvaloniaProperty.Register(nameof(IsFilled), true); + /// /// Defines the property. /// - public static readonly DirectProperty SegmentsProperty - = AvaloniaProperty.RegisterDirect(nameof(Segments), f => f.Segments, (f, s) => f.Segments = s); + public static readonly DirectProperty SegmentsProperty + = AvaloniaProperty.RegisterDirect( + nameof(Segments), + f => f.Segments, + (f, s) => f.Segments = s); + /// /// Defines the property. /// public static readonly StyledProperty StartPointProperty - = AvaloniaProperty.Register(nameof(StartPoint)); + = AvaloniaProperty.Register(nameof(StartPoint)); + + internal event EventHandler? SegmentsInvalidated; + + private PathSegments? _segments; + + private IDisposable? _segmentsDisposable; + + private IDisposable? _segmentsPropertiesDisposable; /// /// Initializes a new instance of the class. @@ -33,6 +51,31 @@ namespace Avalonia.Media Segments = new PathSegments(); } + static PathFigure() + { + SegmentsProperty.Changed.AddClassHandler( + (s, e) => + s.OnSegmentsChanged()); + } + + private void OnSegmentsChanged() + { + _segmentsDisposable?.Dispose(); + _segmentsPropertiesDisposable?.Dispose(); + + _segmentsDisposable = _segments?.ForEachItem( + _ => InvalidateSegments(), + _ => InvalidateSegments(), + InvalidateSegments); + + _segmentsPropertiesDisposable = _segments?.TrackItemPropertyChanged(_ => InvalidateSegments()); + } + + private void InvalidateSegments() + { + SegmentsInvalidated?.Invoke(this, EventArgs.Empty); + } + /// /// Gets or sets a value indicating whether this instance is closed. /// @@ -64,7 +107,7 @@ namespace Avalonia.Media /// The segments. /// [Content] - public PathSegments Segments + public PathSegments? Segments { get { return _segments; } set { SetAndRaise(SegmentsProperty, ref _segments, value); } @@ -81,22 +124,23 @@ namespace Avalonia.Media get { return GetValue(StartPointProperty); } set { SetValue(StartPointProperty, value); } } + + public override string ToString() + => $"M {StartPoint} {string.Join(" ", _segments ?? Enumerable.Empty())}{(IsClosed ? "Z" : "")}"; internal void ApplyTo(StreamGeometryContext ctx) { ctx.BeginFigure(StartPoint, IsFilled); - foreach (var segment in Segments) + if (Segments != null) { - segment.ApplyTo(ctx); + foreach (var segment in Segments) + { + segment.ApplyTo(ctx); + } } ctx.EndFigure(IsClosed); } - - private PathSegments _segments; - - public override string ToString() - => $"M {StartPoint} {string.Join(" ", _segments)}{(IsClosed ? "Z" : "")}"; } -} \ No newline at end of file +} diff --git a/src/Avalonia.Visuals/Media/PathGeometry.cs b/src/Avalonia.Visuals/Media/PathGeometry.cs index fbc29aedc8..3d11c19b7d 100644 --- a/src/Avalonia.Visuals/Media/PathGeometry.cs +++ b/src/Avalonia.Visuals/Media/PathGeometry.cs @@ -104,12 +104,26 @@ namespace Avalonia.Media _figuresPropertiesObserver?.Dispose(); _figuresObserver = figures?.ForEachItem( - _ => InvalidateGeometry(), - _ => InvalidateGeometry(), - () => InvalidateGeometry()); + s => + { + s.SegmentsInvalidated += InvalidateGeometryFromSegments; + InvalidateGeometry(); + }, + s => + { + s.SegmentsInvalidated -= InvalidateGeometryFromSegments; + InvalidateGeometry(); + }, + InvalidateGeometry); + _figuresPropertiesObserver = figures?.TrackItemPropertyChanged(_ => InvalidateGeometry()); + } + private void InvalidateGeometryFromSegments(object _, EventArgs __) + { + InvalidateGeometry(); + } public override string ToString() => $"{(FillRule != FillRule.EvenOdd ? "F1 " : "")}{(string.Join(" ", Figures))}"; diff --git a/src/Avalonia.Visuals/Point.cs b/src/Avalonia.Visuals/Point.cs index 7324f5fbd0..6febe9c802 100644 --- a/src/Avalonia.Visuals/Point.cs +++ b/src/Avalonia.Visuals/Point.cs @@ -1,6 +1,8 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif using Avalonia.Utilities; namespace Avalonia @@ -8,11 +10,16 @@ namespace Avalonia /// /// Defines a point. /// - public readonly struct Point : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Point : IEquatable { static Point() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(Point).IsAssignableFrom(prop.PropertyType)); +#endif } /// diff --git a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs index 15e14935ca..1c8a47c4b7 100644 --- a/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs +++ b/src/Avalonia.Visuals/Rendering/DeferredRenderer.cs @@ -99,6 +99,11 @@ namespace Avalonia.Rendering /// public string DebugFramesPath { get; set; } + /// + /// Forces the renderer to only draw frames on the render thread. Makes Paint to wait until frame is rendered + /// + public bool RenderOnlyOnRenderThread { get; set; } + /// public event EventHandler SceneInvalidated; @@ -180,11 +185,44 @@ namespace Avalonia.Rendering /// public void Paint(Rect rect) { - var t = (IRenderLoopTask)this; - if(t.NeedsUpdate) - UpdateScene(); - if(_scene?.Item != null) - Render(true); + if (RenderOnlyOnRenderThread) + { + // Renderer is stopped and doesn't tick on the render thread + // This indicates a bug somewhere in our code + // (currently happens when a window gets minimized with Show desktop on Windows 10) + if(!_running) + return; + + while (true) + { + Scene scene; + bool? updated; + lock (_sceneLock) + { + updated = UpdateScene(); + scene = _scene?.Item; + } + + // Renderer is in invalid state, skip drawing + if(updated == null) + return; + + // Wait for the scene to be rendered or disposed + scene?.Rendered.Wait(); + + // That was an up-to-date scene, we can return immediately + if (updated == true) + return; + } + } + else + { + var t = (IRenderLoopTask)this; + if (t.NeedsUpdate) + UpdateScene(); + if (_scene?.Item != null) + Render(true); + } } /// @@ -270,13 +308,20 @@ namespace Avalonia.Rendering { if (scene?.Item != null) { - var overlay = DrawDirtyRects || DrawFps; - if (DrawDirtyRects) - _dirtyRectsDisplay.Tick(); - if (overlay) - RenderOverlay(scene.Item, ref context); - if (updated || forceComposite || overlay) - RenderComposite(scene.Item, ref context); + try + { + var overlay = DrawDirtyRects || DrawFps; + if (DrawDirtyRects) + _dirtyRectsDisplay.Tick(); + if (overlay) + RenderOverlay(scene.Item, ref context); + if (updated || forceComposite || overlay) + RenderComposite(scene.Item, ref context); + } + finally + { + scene.Item.MarkAsRendered(); + } } } } @@ -559,15 +604,15 @@ namespace Avalonia.Rendering UpdateScene(); } - private void UpdateScene() + private bool? UpdateScene() { Dispatcher.UIThread.VerifyAccess(); lock (_sceneLock) { if (_disposed) - return; + return null; if (_scene?.Item.Generation > _lastSceneId) - return; + return false; } if (_root.IsVisible) { @@ -619,6 +664,8 @@ namespace Avalonia.Rendering SceneInvalidated(this, new SceneInvalidatedEventArgs((IRenderRoot)_root, rect)); } + + return true; } else { @@ -628,6 +675,8 @@ namespace Avalonia.Rendering _scene = null; oldScene?.Dispose(); } + + return null; } } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs index 4f5c97cdff..6a4c532d4a 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/Scene.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Avalonia.Collections.Pooled; using Avalonia.VisualTree; @@ -13,6 +14,7 @@ namespace Avalonia.Rendering.SceneGraph public class Scene : IDisposable { private readonly Dictionary _index; + private readonly TaskCompletionSource _rendered = new TaskCompletionSource(); /// /// Initializes a new instance of the class. @@ -41,6 +43,8 @@ namespace Avalonia.Rendering.SceneGraph root.LayerRoot = root.Visual; } + public Task Rendered => _rendered.Task; + /// /// Gets a value identifying the scene's generation. This is incremented each time the scene is cloned. /// @@ -97,6 +101,7 @@ namespace Avalonia.Rendering.SceneGraph public void Dispose() { + _rendered.TrySetResult(false); foreach (var node in _index.Values) { node.Dispose(); @@ -340,5 +345,7 @@ namespace Avalonia.Rendering.SceneGraph } } } + + public void MarkAsRendered() => _rendered.TrySetResult(true); } } diff --git a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs index 872f69c884..7d5d62a091 100644 --- a/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs +++ b/src/Avalonia.Visuals/Rendering/SceneGraph/SceneBuilder.cs @@ -24,7 +24,8 @@ namespace Avalonia.Rendering.SceneGraph using (var impl = new DeferredDrawingContextImpl(this, scene.Layers)) using (var context = new DrawingContext(impl)) { - Update(context, scene, (VisualNode)scene.Root, scene.Root.Visual.Bounds, true); + var clip = new Rect(scene.Root.Visual.Bounds.Size); + Update(context, scene, (VisualNode)scene.Root, clip, true); } } @@ -77,7 +78,7 @@ namespace Avalonia.Rendering.SceneGraph using (var impl = new DeferredDrawingContextImpl(this, scene.Layers)) using (var context = new DrawingContext(impl)) { - var clip = scene.Root.Visual.Bounds; + var clip = new Rect(scene.Root.Visual.Bounds.Size); if (node.Parent != null) { @@ -174,7 +175,9 @@ namespace Avalonia.Rendering.SceneGraph if (visual.IsVisible) { - var m = Matrix.CreateTranslation(visual.Bounds.Position); + var m = node != scene.Root ? + Matrix.CreateTranslation(visual.Bounds.Position) : + Matrix.Identity; var renderTransform = Matrix.Identity; diff --git a/src/Avalonia.Visuals/Size.cs b/src/Avalonia.Visuals/Size.cs index d87d2c5fc2..8a805dc6c5 100644 --- a/src/Avalonia.Visuals/Size.cs +++ b/src/Avalonia.Visuals/Size.cs @@ -1,6 +1,8 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif using Avalonia.Utilities; namespace Avalonia @@ -8,11 +10,16 @@ namespace Avalonia /// /// Defines a size. /// - public readonly struct Size : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Size : IEquatable { static Size() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(Size).IsAssignableFrom(prop.PropertyType)); +#endif } /// diff --git a/src/Avalonia.Visuals/Thickness.cs b/src/Avalonia.Visuals/Thickness.cs index 6d69c4d9a9..06ebc9bfe7 100644 --- a/src/Avalonia.Visuals/Thickness.cs +++ b/src/Avalonia.Visuals/Thickness.cs @@ -1,6 +1,8 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif using Avalonia.Utilities; namespace Avalonia @@ -8,11 +10,16 @@ namespace Avalonia /// /// Describes the thickness of a frame around a rectangle. /// - public readonly struct Thickness : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Thickness : IEquatable { static Thickness() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(Thickness).IsAssignableFrom(prop.PropertyType)); +#endif } /// diff --git a/src/Avalonia.Visuals/Vector.cs b/src/Avalonia.Visuals/Vector.cs index 2fcf804f14..1b9f5c67d5 100644 --- a/src/Avalonia.Visuals/Vector.cs +++ b/src/Avalonia.Visuals/Vector.cs @@ -1,6 +1,8 @@ using System; using System.Globalization; +#if !BUILDTASK using Avalonia.Animation.Animators; +#endif using Avalonia.Utilities; #nullable enable @@ -10,11 +12,16 @@ namespace Avalonia /// /// Defines a vector. /// - public readonly struct Vector : IEquatable +#if !BUILDTASK + public +#endif + readonly struct Vector : IEquatable { static Vector() { +#if !BUILDTASK Animation.Animation.RegisterAnimator(prop => typeof(Vector).IsAssignableFrom(prop.PropertyType)); +#endif } /// diff --git a/src/Avalonia.Visuals/Visual.cs b/src/Avalonia.Visuals/Visual.cs index 283d9deb52..30073c9966 100644 --- a/src/Avalonia.Visuals/Visual.cs +++ b/src/Avalonia.Visuals/Visual.cs @@ -1,4 +1,5 @@ using System; +using System.Collections; using System.Collections.Specialized; using Avalonia.Collections; using Avalonia.Data; @@ -619,34 +620,30 @@ namespace Avalonia switch (e.Action) { case NotifyCollectionChangedAction.Add: - foreach (Visual v in e.NewItems) - { - v.SetVisualParent(this); - } - + SetVisualParent(e.NewItems, this); break; case NotifyCollectionChangedAction.Remove: - foreach (Visual v in e.OldItems) - { - v.SetVisualParent(null); - } - + SetVisualParent(e.OldItems, null); break; case NotifyCollectionChangedAction.Replace: - foreach (Visual v in e.OldItems) - { - v.SetVisualParent(null); - } - - foreach (Visual v in e.NewItems) - { - v.SetVisualParent(this); - } - + SetVisualParent(e.OldItems, null); + SetVisualParent(e.NewItems, this); break; } } + + private static void SetVisualParent(IList children, Visual parent) + { + var count = children.Count; + + for (var i = 0; i < count; i++) + { + var visual = (Visual) children[i]; + + visual.SetVisualParent(parent); + } + } } } diff --git a/src/Avalonia.X11/X11Window.cs b/src/Avalonia.X11/X11Window.cs index d62e3bed23..b061920d6a 100644 --- a/src/Avalonia.X11/X11Window.cs +++ b/src/Avalonia.X11/X11Window.cs @@ -189,6 +189,11 @@ namespace Avalonia.X11 if (platform.Options.UseDBusMenu) NativeMenuExporter = DBusMenuExporter.TryCreate(_handle); NativeControlHost = new X11NativeControlHost(_platform, this); + DispatcherTimer.Run(() => + { + Paint?.Invoke(default); + return _handle != IntPtr.Zero; + }, TimeSpan.FromMilliseconds(100)); } class SurfaceInfo : EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo @@ -338,7 +343,10 @@ namespace Avalonia.X11 return customRendererFactory.Create(root, loop); return _platform.Options.UseDeferredRendering ? - new DeferredRenderer(root, loop) : + new DeferredRenderer(root, loop) + { + RenderOnlyOnRenderThread = true + } : (IRenderer)new X11ImmediateRendererProxy(root, loop); } diff --git a/src/Avalonia.X11/XI2Manager.cs b/src/Avalonia.X11/XI2Manager.cs index b3a24e6c37..8cdf24cc7b 100644 --- a/src/Avalonia.X11/XI2Manager.cs +++ b/src/Avalonia.X11/XI2Manager.cs @@ -351,7 +351,7 @@ namespace Avalonia.X11 if (state.HasFlag(XModifierMask.Mod4Mask)) Modifiers |= RawInputModifiers.Meta; - Modifiers = ParseButtonState(ev->buttons.MaskLen, ev->buttons.Mask); + Modifiers |= ParseButtonState(ev->buttons.MaskLen, ev->buttons.Mask); Valuators = new Dictionary(); Position = new Point(ev->event_x, ev->event_y); diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/Avalonia.Markup.Xaml.Loader.csproj b/src/Markup/Avalonia.Markup.Xaml.Loader/Avalonia.Markup.Xaml.Loader.csproj index 514556d0b9..db9c414840 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/Avalonia.Markup.Xaml.Loader.csproj +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/Avalonia.Markup.Xaml.Loader.csproj @@ -4,6 +4,7 @@ netstandard2.0 true Avalonia.Markup.Xaml.Loader + $(DefineConstants);XAMLX_INTERNAL diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlFontFamilyAstNode.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlFontFamilyAstNode.cs new file mode 100644 index 0000000000..1f50e661d8 --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlFontFamilyAstNode.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; +using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; +using XamlX.Ast; +using XamlX.Emit; +using XamlX.IL; +using XamlX.TypeSystem; + +namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AstNodes +{ + class AvaloniaXamlIlFontFamilyAstNode: XamlAstNode, IXamlAstValueNode, IXamlAstILEmitableNode + { + private readonly AvaloniaXamlIlWellKnownTypes _types; + private readonly string _text; + + public IXamlAstTypeReference Type { get; } + + public AvaloniaXamlIlFontFamilyAstNode(AvaloniaXamlIlWellKnownTypes types, + string text, + IXamlLineInfo lineInfo) : base(lineInfo) + { + _types = types; + _text = text; + Type = new XamlAstClrTypeReference(lineInfo, types.FontFamily, false); + } + + public XamlILNodeEmitResult Emit(XamlEmitContext context, IXamlILEmitter codeGen) + { + codeGen + .Ldloc(context.ContextLocal) + .Castclass(context.Configuration.TypeMappings.UriContextProvider) + .EmitCall(context.Configuration.TypeMappings.UriContextProvider.FindMethod( + "get_BaseUri", _types.Uri, false)) + .Ldstr(_text) + .Newobj(_types.FontFamilyConstructorUriName); + return XamlILNodeEmitResult.Type(0, _types.FontFamily); + } + } +} diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlGridLengthAstNode.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlGridLengthAstNode.cs new file mode 100644 index 0000000000..218c49512c --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlGridLengthAstNode.cs @@ -0,0 +1,34 @@ +using Avalonia.Controls; +using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; +using XamlX.Ast; +using XamlX.Emit; +using XamlX.IL; + +namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AstNodes +{ + class AvaloniaXamlIlGridLengthAstNode : XamlAstNode, IXamlAstValueNode, IXamlAstILEmitableNode + { + private readonly AvaloniaXamlIlWellKnownTypes _types; + private readonly GridLength _gridLength; + + public AvaloniaXamlIlGridLengthAstNode(IXamlLineInfo lineInfo, AvaloniaXamlIlWellKnownTypes types, GridLength gridLength) : base(lineInfo) + { + _types = types; + _gridLength = gridLength; + + Type = new XamlAstClrTypeReference(lineInfo, types.GridLength, false); + } + + public IXamlAstTypeReference Type { get; } + + public XamlILNodeEmitResult Emit(XamlEmitContext context, IXamlILEmitter codeGen) + { + codeGen + .Ldc_R8(_gridLength.Value) + .Ldc_I4((int)_gridLength.GridUnitType) + .Newobj(_types.GridLengthConstructorValueType); + + return XamlILNodeEmitResult.Type(0, Type.GetClrType()); + } + } +} diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlVectorLikeConstantAstNode.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlVectorLikeConstantAstNode.cs new file mode 100644 index 0000000000..35cc9b3cf2 --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AstNodes/AvaloniaXamlIlVectorLikeConstantAstNode.cs @@ -0,0 +1,54 @@ +using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; +using XamlX; +using XamlX.Ast; +using XamlX.Emit; +using XamlX.IL; +using XamlX.TypeSystem; + +namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AstNodes +{ + class AvaloniaXamlIlVectorLikeConstantAstNode : XamlAstNode, IXamlAstValueNode, IXamlAstILEmitableNode + { + private readonly IXamlConstructor _constructor; + private readonly double[] _values; + + public AvaloniaXamlIlVectorLikeConstantAstNode(IXamlLineInfo lineInfo, AvaloniaXamlIlWellKnownTypes types, IXamlType type, IXamlConstructor constructor, double[] values) : base(lineInfo) + { + var parameters = constructor.Parameters; + + if (parameters.Count != values.Length) + { + throw new XamlTypeSystemException($"Constructor that takes {values.Length} parameters is expected, got {parameters.Count} instead."); + } + + var elementType = types.XamlIlTypes.Double; + + foreach (var parameter in parameters) + { + if (parameter != elementType) + { + throw new XamlTypeSystemException($"Expected parameter of type {elementType}, got {parameter} instead."); + } + } + + _constructor = constructor; + _values = values; + + Type = new XamlAstClrTypeReference(lineInfo, type, false); + } + + public IXamlAstTypeReference Type { get; } + + public XamlILNodeEmitResult Emit(XamlEmitContext context, IXamlILEmitter codeGen) + { + foreach (var value in _values) + { + codeGen.Ldc_R8(value); + } + + codeGen.Newobj(_constructor); + + return XamlILNodeEmitResult.Type(0, Type.GetClrType()); + } + } +} diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguage.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguage.cs index 99ec3744bf..40cfd21a76 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguage.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/AvaloniaXamlIlLanguage.cs @@ -1,7 +1,11 @@ +using System; using System.Collections.Generic; using System.Globalization; using System.Linq; +using Avalonia.Controls; +using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.AstNodes; using Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers; +using Avalonia.Media; using XamlX; using XamlX.Ast; using XamlX.Emit; @@ -166,28 +170,196 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions public static bool CustomValueConverter(AstTransformationContext context, IXamlAstValueNode node, IXamlType type, out IXamlAstValueNode result) { - if (type.FullName == "System.TimeSpan" - && node is XamlAstTextNode tn - && !tn.Text.Contains(":")) - { - var seconds = double.Parse(tn.Text, CultureInfo.InvariantCulture); - result = new XamlStaticOrTargetedReturnMethodCallNode(tn, - type.FindMethod("FromSeconds", type, false, context.Configuration.WellKnownTypes.Double), - new[] - { - new XamlConstantNode(tn, context.Configuration.WellKnownTypes.Double, seconds) - }); + if (!(node is XamlAstTextNode textNode)) + { + result = null; + return false; + } + + var text = textNode.Text; + + var types = context.GetAvaloniaTypes(); + + if (type.FullName == "System.TimeSpan") + { + var tsText = text.Trim(); + + if (!TimeSpan.TryParse(tsText, CultureInfo.InvariantCulture, out var timeSpan)) + { + // // shorthand seconds format (ie. "0.25") + if (!tsText.Contains(":") && double.TryParse(tsText, + NumberStyles.Float | NumberStyles.AllowThousands, + CultureInfo.InvariantCulture, out var seconds)) + timeSpan = TimeSpan.FromSeconds(seconds); + else + throw new XamlX.XamlLoadException($"Unable to parse {text} as a time span", node); + } + + + result = new XamlStaticOrTargetedReturnMethodCallNode(node, + type.FindMethod("FromTicks", type, false, types.Long), + new[] { new XamlConstantNode(node, types.Long, timeSpan.Ticks) }); return true; } + if (type.Equals(types.FontFamily)) + { + result = new AvaloniaXamlIlFontFamilyAstNode(types, text, node); + return true; + } + + if (type.Equals(types.Thickness)) + { + try + { + var thickness = Thickness.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Thickness, types.ThicknessFullConstructor, + new[] { thickness.Left, thickness.Top, thickness.Right, thickness.Bottom }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a thickness", node); + } + } + + if (type.Equals(types.Point)) + { + try + { + var point = Point.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Point, types.PointFullConstructor, + new[] { point.X, point.Y }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a point", node); + } + } + + if (type.Equals(types.Vector)) + { + try + { + var vector = Vector.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Vector, types.VectorFullConstructor, + new[] { vector.X, vector.Y }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a vector", node); + } + } + + if (type.Equals(types.Size)) + { + try + { + var size = Size.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Size, types.SizeFullConstructor, + new[] { size.Width, size.Height }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a size", node); + } + } + + if (type.Equals(types.Matrix)) + { + try + { + var matrix = Matrix.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Matrix, types.MatrixFullConstructor, + new[] { matrix.M11, matrix.M12, matrix.M21, matrix.M22, matrix.M31, matrix.M32 }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a matrix", node); + } + } + + if (type.Equals(types.CornerRadius)) + { + try + { + var cornerRadius = CornerRadius.Parse(text); + + result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.CornerRadius, types.CornerRadiusFullConstructor, + new[] { cornerRadius.TopLeft, cornerRadius.TopRight, cornerRadius.BottomRight, cornerRadius.BottomLeft }); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a corner radius", node); + } + } + + if (type.Equals(types.Color)) + { + if (!Color.TryParse(text, out Color color)) + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a color", node); + } + + result = new XamlStaticOrTargetedReturnMethodCallNode(node, + type.GetMethod( + new FindMethodMethodSignature("FromUInt32", type, types.UInt) { IsStatic = true }), + new[] { new XamlConstantNode(node, types.UInt, color.ToUint32()) }); + + return true; + } + + if (type.Equals(types.GridLength)) + { + try + { + var gridLength = GridLength.Parse(text); + + result = new AvaloniaXamlIlGridLengthAstNode(node, types, gridLength); + + return true; + } + catch + { + throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a grid length", node); + } + } + + if (type.Equals(types.Cursor)) + { + if (TypeSystemHelpers.TryGetEnumValueNode(types.StandardCursorType, text, node, out var enumConstantNode)) + { + var cursorTypeRef = new XamlAstClrTypeReference(node, types.Cursor, false); + + result = new XamlAstNewClrObjectNode(node, cursorTypeRef, types.CursorTypeConstructor, new List { enumConstantNode }); + + return true; + } + } + if (type.FullName == "Avalonia.AvaloniaProperty") { var scope = context.ParentNodes().OfType().FirstOrDefault(); if (scope == null) throw new XamlX.XamlLoadException("Unable to find the parent scope for AvaloniaProperty lookup", node); - if (!(node is XamlAstTextNode text)) - throw new XamlX.XamlLoadException("Property should be a text node", node); - result = XamlIlAvaloniaPropertyHelper.CreateNode(context, text.Text, scope.TargetType, text); + + result = XamlIlAvaloniaPropertyHelper.CreateNode(context, text, scope.TargetType, node ); return true; } diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlTransformInstanceAttachedProperties.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlTransformInstanceAttachedProperties.cs index d78ceeb918..f87e73a783 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlTransformInstanceAttachedProperties.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlTransformInstanceAttachedProperties.cs @@ -22,7 +22,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers var avaloniaObject = context.Configuration.TypeSystem.FindType("Avalonia.AvaloniaObject"); if (avaloniaObject.IsAssignableFrom(targetRef.Type) && avaloniaObject.IsAssignableFrom(declaringRef.Type) - && !targetRef.Type.IsAssignableFrom(declaringRef.Type)) + && !declaringRef.Type.IsAssignableFrom(targetRef.Type)) { // Instance property var clrProp = declaringRef.Type.GetAllProperties().FirstOrDefault(p => p.Name == prop.Name); diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs index 3dec96dc43..f046778429 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlWellKnownTypes.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; +using System.Linq; using XamlX.Emit; using XamlX.IL; using XamlX.Transform; @@ -47,6 +49,29 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers public IXamlType ReflectionBindingExtension { get; } public IXamlType RelativeSource { get; } + public IXamlType UInt { get; } + public IXamlType Long { get; } + public IXamlType Uri { get; } + public IXamlType FontFamily { get; } + public IXamlConstructor FontFamilyConstructorUriName { get; } + public IXamlType Thickness { get; } + public IXamlConstructor ThicknessFullConstructor { get; } + public IXamlType Point { get; } + public IXamlConstructor PointFullConstructor { get; } + public IXamlType Vector { get; } + public IXamlConstructor VectorFullConstructor { get; } + public IXamlType Size { get; } + public IXamlConstructor SizeFullConstructor { get; } + public IXamlType Matrix { get; } + public IXamlConstructor MatrixFullConstructor { get; } + public IXamlType CornerRadius { get; } + public IXamlConstructor CornerRadiusFullConstructor { get; } + public IXamlType GridLength { get; } + public IXamlConstructor GridLengthConstructorValueType { get; } + public IXamlType Color { get; } + public IXamlType StandardCursorType { get; } + public IXamlType Cursor { get; } + public IXamlConstructor CursorTypeConstructor { get; } public AvaloniaXamlIlWellKnownTypes(TransformerConfiguration cfg) { @@ -104,6 +129,33 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers ItemsRepeater = cfg.TypeSystem.GetType("Avalonia.Controls.ItemsRepeater"); ReflectionBindingExtension = cfg.TypeSystem.GetType("Avalonia.Markup.Xaml.MarkupExtensions.ReflectionBindingExtension"); RelativeSource = cfg.TypeSystem.GetType("Avalonia.Data.RelativeSource"); + UInt = cfg.TypeSystem.GetType("System.UInt32"); + Long = cfg.TypeSystem.GetType("System.Int64"); + Uri = cfg.TypeSystem.GetType("System.Uri"); + FontFamily = cfg.TypeSystem.GetType("Avalonia.Media.FontFamily"); + FontFamilyConstructorUriName = FontFamily.GetConstructor(new List { Uri, XamlIlTypes.String }); + + (IXamlType, IXamlConstructor) GetNumericTypeInfo(string name, IXamlType componentType, int componentCount) + { + var type = cfg.TypeSystem.GetType(name); + var ctor = type.GetConstructor(Enumerable.Range(0, componentCount).Select(_ => componentType).ToList()); + + return (type, ctor); + } + + (Thickness, ThicknessFullConstructor) = GetNumericTypeInfo("Avalonia.Thickness", XamlIlTypes.Double, 4); + (Point, PointFullConstructor) = GetNumericTypeInfo("Avalonia.Point", XamlIlTypes.Double, 2); + (Vector, VectorFullConstructor) = GetNumericTypeInfo("Avalonia.Vector", XamlIlTypes.Double, 2); + (Size, SizeFullConstructor) = GetNumericTypeInfo("Avalonia.Size", XamlIlTypes.Double, 2); + (Matrix, MatrixFullConstructor) = GetNumericTypeInfo("Avalonia.Matrix", XamlIlTypes.Double, 6); + (CornerRadius, CornerRadiusFullConstructor) = GetNumericTypeInfo("Avalonia.CornerRadius", XamlIlTypes.Double, 4); + + GridLength = cfg.TypeSystem.GetType("Avalonia.Controls.GridLength"); + GridLengthConstructorValueType = GridLength.GetConstructor(new List { XamlIlTypes.Double, cfg.TypeSystem.GetType("Avalonia.Controls.GridUnitType") }); + Color = cfg.TypeSystem.GetType("Avalonia.Media.Color"); + StandardCursorType = cfg.TypeSystem.GetType("Avalonia.Input.StandardCursorType"); + Cursor = cfg.TypeSystem.GetType("Avalonia.Input.Cursor"); + CursorTypeConstructor = Cursor.GetConstructor(new List { StandardCursorType }); } } diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs index 03ec32b9cf..1974dfe3bc 100644 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/XamlIlBindingPathHelper.cs @@ -203,6 +203,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions .ParentNodes() .OfType() .Where(x => styledElementType.IsAssignableFrom(x.Type.GetClrType())) + .Skip(1) .ElementAtOrDefault(ancestor.Level) ?.Type.GetClrType(); @@ -242,6 +243,16 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions case RawSourceBindingExpressionNode rawSource: nodes.Add(new RawSourcePathElementNode(rawSource.RawSource)); break; + case BindingExpressionGrammar.TypeCastNode typeCastNode: + var castType = GetType(typeCastNode.Namespace, typeCastNode.TypeName); + + if (castType is null) + { + throw new XamlX.XamlParseException($"Unable to resolve cast to type {typeCastNode.Namespace}:{typeCastNode.TypeName} based on XAML tree.", lineInfo); + } + + nodes.Add(new TypeCastPathElementNode(castType)); + break; } } @@ -422,7 +433,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions { codeGen.Ldtype(Type) .Ldc_I4(_level) - .EmitCall(context.GetAvaloniaTypes().CompiledBindingPathBuilder.FindMethod(m => m.Name == "FindAncestor")); + .EmitCall(context.GetAvaloniaTypes().CompiledBindingPathBuilder.FindMethod(m => m.Name == "Ancestor")); } } @@ -608,10 +619,10 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions private readonly IXamlAstValueNode _rawSource; public RawSourcePathElementNode(IXamlAstValueNode rawSource) - :base(rawSource) + : base(rawSource) { _rawSource = rawSource; - + } public IXamlType Type => _rawSource.Type.GetClrType(); @@ -625,6 +636,21 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions } } + class TypeCastPathElementNode : IXamlIlBindingPathElementNode + { + public TypeCastPathElementNode(IXamlType ancestorType) + { + Type = ancestorType; + } + + public IXamlType Type { get; } + + public void Emit(XamlIlEmitContext context, IXamlILEmitter codeGen) + { + codeGen.EmitCall(context.GetAvaloniaTypes().CompiledBindingPathBuilder.FindMethod(m => m.Name == "TypeCast").MakeGenericMethod(new[] { Type })); + } + } + class XamlIlBindingPathNode : XamlAstNode, IXamlIlBindingPathNode, IXamlAstEmitableNode { private readonly List _transformElements; diff --git a/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github b/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github index 5420df861c..ea80a607c5 160000 --- a/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github +++ b/src/Markup/Avalonia.Markup.Xaml.Loader/xamlil.github @@ -1 +1 @@ -Subproject commit 5420df861ce6f2be5ead9efa078fe7242ce88f18 +Subproject commit ea80a607c5e9d8f000160dbbb48c27ed4cfafbc9 diff --git a/src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj b/src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj index 3e15d2f700..217da2d50d 100644 --- a/src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj +++ b/src/Markup/Avalonia.Markup.Xaml/Avalonia.Markup.Xaml.csproj @@ -23,6 +23,7 @@ + diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindingExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindingExtension.cs index aab733cb43..da39920eb3 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindingExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindingExtension.cs @@ -26,6 +26,8 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions { Path = Path, Converter = Converter, + ConverterParameter = ConverterParameter, + TargetNullValue = TargetNullValue, FallbackValue = FallbackValue, Mode = Mode, Priority = Priority, diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs index d627fe3cd3..f6636664c1 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Avalonia.Controls; using Avalonia.Data.Core; using Avalonia.Data.Core.Plugins; @@ -53,6 +54,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings case IStronglyTypedStreamElement stream: node = new StreamNode(stream.CreatePlugin()); break; + case ITypeCastElement typeCast: + node = new StrongTypeCastNode(typeCast.Type, typeCast.Cast); + break; default: throw new InvalidOperationException($"Unknown binding path element type {element.GetType().FullName}"); } @@ -66,6 +70,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings internal SourceMode SourceMode => _elements.Count > 0 && _elements[0] is IControlSourceBindingPathElement ? SourceMode.Control : SourceMode.Data; internal object RawSource { get; } + + public override string ToString() + => string.Concat(_elements.Select(e => e.ToString())); } public class CompiledBindingPathBuilder @@ -126,6 +133,12 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings return this; } + public CompiledBindingPathBuilder TypeCast() + { + _elements.Add(new TypeCastPathElement()); + return this; + } + public CompiledBindingPathBuilder SetRawSource(object rawSource) { _rawSource = rawSource; @@ -157,6 +170,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings public IPropertyInfo Property { get; } public Func, IPropertyInfo, IPropertyAccessor> AccessorFactory { get; } + + public override string ToString() + => $".{Property.Name}"; } internal interface IStronglyTypedStreamElement : ICompiledBindingPathElement @@ -164,6 +180,13 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings IStreamPlugin CreatePlugin(); } + internal interface ITypeCastElement : ICompiledBindingPathElement + { + Type Type { get; } + + Func Cast { get; } + } + internal class TaskStreamPathElement : IStronglyTypedStreamElement { public static readonly TaskStreamPathElement Instance = new TaskStreamPathElement(); @@ -181,6 +204,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings internal class SelfPathElement : ICompiledBindingPathElement, IControlSourceBindingPathElement { public static readonly SelfPathElement Instance = new SelfPathElement(); + + public override string ToString() + => "$self"; } internal class AncestorPathElement : ICompiledBindingPathElement, IControlSourceBindingPathElement @@ -193,6 +219,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings public Type AncestorType { get; } public int Level { get; } + + public override string ToString() + => $"$parent[{AncestorType?.Name},{Level}]"; } internal class VisualAncestorPathElement : ICompiledBindingPathElement, IControlSourceBindingPathElement @@ -217,6 +246,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings public INameScope NameScope { get; } public string Name { get; } + + public override string ToString() + => $"#{Name}"; } internal class ArrayElementPathElement : ICompiledBindingPathElement @@ -229,5 +261,24 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings public int[] Indices { get; } public Type ElementType { get; } + public override string ToString() + => $"[{string.Join(",", Indices)}]"; + } + + internal class TypeCastPathElement : ITypeCastElement + { + private static object TryCast(object obj) + { + if (obj is T result) + return result; + return null; + } + + public Type Type => typeof(T); + + public Func Cast => TryCast; + + public override string ToString() + => $"({Type.FullName})"; } } diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/StrongTypeCastNode.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/StrongTypeCastNode.cs new file mode 100644 index 0000000000..1252ec7eca --- /dev/null +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/StrongTypeCastNode.cs @@ -0,0 +1,18 @@ +using System; +using Avalonia.Data.Core; + +namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings +{ + public class StrongTypeCastNode : TypeCastNode + { + private Func _cast; + + public StrongTypeCastNode(Type type, Func cast) : base(type) + { + _cast = cast; + } + + protected override object Cast(object value) + => _cast(value); + } +} diff --git a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs index 03fd2e60dd..95380be65f 100644 --- a/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs +++ b/src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/DynamicResourceExtension.cs @@ -10,8 +10,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions { public class DynamicResourceExtension : IBinding { - private IStyledElement? _anchor; - private IResourceProvider? _resourceProvider; + private object? _anchor; public DynamicResourceExtension() { @@ -30,12 +29,9 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions if (!(provideTarget.TargetObject is IStyledElement)) { - _anchor = serviceProvider.GetFirstParent(); - - if (_anchor is null) - { - _resourceProvider = serviceProvider.GetFirstParent(); - } + _anchor = serviceProvider.GetFirstParent() ?? + serviceProvider.GetFirstParent() ?? + (object?)serviceProvider.GetFirstParent(); } return this; @@ -52,16 +48,16 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions return null; } - var control = target as IStyledElement ?? _anchor as IStyledElement; + var control = target as IResourceHost ?? _anchor as IResourceHost; if (control != null) { var source = control.GetResourceObservable(ResourceKey, GetConverter(targetProperty)); return InstancedBinding.OneWay(source); } - else if (_resourceProvider is object) + else if (_anchor is IResourceProvider resourceProvider) { - var source = _resourceProvider.GetResourceObservable(ResourceKey, GetConverter(targetProperty)); + var source = resourceProvider.GetResourceObservable(ResourceKey, GetConverter(targetProperty)); return InstancedBinding.OneWay(source); } diff --git a/src/Markup/Avalonia.Markup/Markup/Parsers/BindingExpressionGrammar.cs b/src/Markup/Avalonia.Markup/Markup/Parsers/BindingExpressionGrammar.cs index 8e5631e198..7c362e24cc 100644 --- a/src/Markup/Avalonia.Markup/Markup/Parsers/BindingExpressionGrammar.cs +++ b/src/Markup/Avalonia.Markup/Markup/Parsers/BindingExpressionGrammar.cs @@ -46,6 +46,10 @@ namespace Avalonia.Markup.Parsers state = ParseIndexer(ref r, nodes); break; + case State.TypeCast: + state = ParseTypeCast(ref r, nodes); + break; + case State.ElementName: state = ParseElementName(ref r, nodes); mode = SourceMode.Control; @@ -84,6 +88,11 @@ namespace Avalonia.Markup.Parsers } else if (ParseOpenBrace(ref r)) { + if (PeekOpenBrace(ref r)) + { + return State.TypeCast; + } + return State.AttachedProperty; } else if (PeekOpenBracket(ref r)) @@ -124,6 +133,10 @@ namespace Avalonia.Markup.Parsers { return State.Indexer; } + else if (ParseOpenBrace(ref r)) + { + return State.TypeCast; + } return State.End; } @@ -132,6 +145,11 @@ namespace Avalonia.Markup.Parsers { if (ParseOpenBrace(ref r)) { + if (PeekOpenBrace(ref r)) + { + return State.TypeCast; + } + return State.AttachedProperty; } else @@ -152,6 +170,12 @@ namespace Avalonia.Markup.Parsers { var (ns, owner) = ParseTypeName(ref r); + if(!r.End && r.TakeIf(')')) + { + nodes.Add(new TypeCastNode() { Namespace = ns, TypeName = owner }); + return State.AfterMember; + } + if (r.End || !r.TakeIf('.')) { throw new ExpressionParseException(r.Position, "Invalid attached property name."); @@ -159,6 +183,11 @@ namespace Avalonia.Markup.Parsers var name = r.ParseIdentifier(); + if (name.Length == 0) + { + throw new ExpressionParseException(r.Position, "Attached Property name expected after '.'."); + } + if (r.End || !r.TakeIf(')')) { throw new ExpressionParseException(r.Position, "Expected ')'."); @@ -186,6 +215,39 @@ namespace Avalonia.Markup.Parsers return State.AfterMember; } + private static State ParseTypeCast(ref CharacterReader r, List nodes) + { + bool parseMemberBeforeAddCast = ParseOpenBrace(ref r); + + var (ns, typeName) = ParseTypeName(ref r); + + var result = State.AfterMember; + + if (parseMemberBeforeAddCast) + { + if (!ParseCloseBrace(ref r)) + { + throw new ExpressionParseException(r.Position, "Expected ')'."); + } + + result = ParseBeforeMember(ref r, nodes); + + if(r.Peek == '[') + { + result = ParseIndexer(ref r, nodes); + } + } + + nodes.Add(new TypeCastNode { Namespace = ns, TypeName = typeName }); + + if (r.End || !r.TakeIf(')')) + { + throw new ExpressionParseException(r.Position, "Expected ')'."); + } + + return result; + } + private static State ParseElementName(ref CharacterReader r, List nodes) { var name = r.ParseIdentifier(); @@ -288,11 +350,21 @@ namespace Avalonia.Markup.Parsers return !r.End && r.TakeIf('('); } + private static bool ParseCloseBrace(ref CharacterReader r) + { + return !r.End && r.TakeIf(')'); + } + private static bool PeekOpenBracket(ref CharacterReader r) { return !r.End && r.Peek == '['; } + private static bool PeekOpenBrace(ref CharacterReader r) + { + return !r.End && r.Peek == '('; + } + private static bool ParseStreamOperator(ref CharacterReader r) { return !r.End && r.TakeIf('^'); @@ -322,6 +394,7 @@ namespace Avalonia.Markup.Parsers BeforeMember, AttachedProperty, Indexer, + TypeCast, End, } @@ -343,9 +416,9 @@ namespace Avalonia.Markup.Parsers } } - public interface INode {} + public interface INode { } - public interface ITransformNode {} + public interface ITransformNode { } public class EmptyExpressionNode : INode { } @@ -366,11 +439,11 @@ namespace Avalonia.Markup.Parsers public IList Arguments { get; set; } } - public class NotNode : INode, ITransformNode {} + public class NotNode : INode, ITransformNode { } - public class StreamNode : INode {} + public class StreamNode : INode { } - public class SelfNode : INode {} + public class SelfNode : INode { } public class NameNode : INode { @@ -383,5 +456,11 @@ namespace Avalonia.Markup.Parsers public string TypeName { get; set; } public int Level { get; set; } } + + public class TypeCastNode : INode + { + public string Namespace { get; set; } + public string TypeName { get; set; } + } } } diff --git a/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs b/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs index 1048148c1f..558130e23f 100644 --- a/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs +++ b/src/Markup/Avalonia.Markup/Markup/Parsers/ExpressionParser.cs @@ -59,6 +59,9 @@ namespace Avalonia.Markup.Parsers case BindingExpressionGrammar.NameNode elementName: nextNode = new ElementNameNode(_nameScope, elementName.Name); break; + case BindingExpressionGrammar.TypeCastNode typeCast: + nextNode = ParseTypeCastNode(typeCast); + break; } if (rootNode is null) { @@ -92,6 +95,22 @@ namespace Avalonia.Markup.Parsers return new FindAncestorNode(ancestorType, ancestorLevel); } + private TypeCastNode ParseTypeCastNode(BindingExpressionGrammar.TypeCastNode node) + { + Type castType = null; + if (!(node.Namespace is null) && !(node.TypeName is null)) + { + if (_typeResolver == null) + { + throw new InvalidOperationException("Cannot parse a binding path with a typed Cast without a type resolver. Maybe you can use a LINQ Expression binding path instead?"); + } + + castType = _typeResolver(node.Namespace, node.TypeName); + } + + return new TypeCastNode(castType); + } + private AvaloniaPropertyAccessorNode ParseAttachedProperty(BindingExpressionGrammar.AttachedPropertyNameNode node) { if (_typeResolver == null) diff --git a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs index 44e0c82110..2a79a4bb50 100644 --- a/src/Skia/Avalonia.Skia/DrawingContextImpl.cs +++ b/src/Skia/Avalonia.Skia/DrawingContextImpl.cs @@ -434,7 +434,7 @@ namespace Avalonia.Skia /// public IDrawingContextLayerImpl CreateLayer(Size size) { - return CreateRenderTarget( size); + return CreateRenderTarget(size, true); } /// @@ -673,7 +673,7 @@ namespace Avalonia.Skia private void ConfigureTileBrush(ref PaintWrapper paintWrapper, Size targetSize, ITileBrush tileBrush, IDrawableBitmapImpl tileBrushImage) { var calc = new TileBrushCalculator(tileBrush, tileBrushImage.PixelSize.ToSizeWithDpi(_dpi), targetSize); - var intermediate = CreateRenderTarget(calc.IntermediateSize); + var intermediate = CreateRenderTarget(calc.IntermediateSize, false); paintWrapper.AddDisposable(intermediate); @@ -748,7 +748,7 @@ namespace Avalonia.Skia if (intermediateSize.Width >= 1 && intermediateSize.Height >= 1) { - var intermediate = CreateRenderTarget(intermediateSize); + var intermediate = CreateRenderTarget(intermediateSize, false); using (var ctx = intermediate.CreateDrawingContext(visualBrushRenderer)) { @@ -978,9 +978,10 @@ namespace Avalonia.Skia /// Create new render target compatible with this drawing context. /// /// The size of the render target in DIPs. + /// Whether the render target is being created for a layer. /// Pixel format. /// - private SurfaceRenderTarget CreateRenderTarget(Size size, PixelFormat? format = null) + private SurfaceRenderTarget CreateRenderTarget(Size size, bool isLayer, PixelFormat? format = null) { var pixelSize = PixelSize.FromSizeWithDpi(size, _dpi); var createInfo = new SurfaceRenderTarget.CreateInfo @@ -992,7 +993,8 @@ namespace Avalonia.Skia DisableTextLcdRendering = !_canTextUseLcdRendering, GrContext = _grContext, Gpu = _gpu, - Session = _session + Session = _session, + DisableManualFbo = !isLayer, }; return new SurfaceRenderTarget(createInfo); diff --git a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs index d6f76a2c20..72700fb8fd 100644 --- a/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs +++ b/src/Skia/Avalonia.Skia/PlatformRenderInterface.cs @@ -124,7 +124,8 @@ namespace Avalonia.Skia Width = size.Width, Height = size.Height, Dpi = dpi, - DisableTextLcdRendering = false + DisableTextLcdRendering = false, + DisableManualFbo = true, }; return new SurfaceRenderTarget(createInfo); diff --git a/src/Skia/Avalonia.Skia/SurfaceRenderTarget.cs b/src/Skia/Avalonia.Skia/SurfaceRenderTarget.cs index 6347c64aed..01b7449b64 100644 --- a/src/Skia/Avalonia.Skia/SurfaceRenderTarget.cs +++ b/src/Skia/Avalonia.Skia/SurfaceRenderTarget.cs @@ -51,7 +51,8 @@ namespace Avalonia.Skia _grContext = createInfo.GrContext; _gpu = createInfo.Gpu; - _surface = _gpu?.TryCreateSurface(PixelSize, createInfo.Session); + if (!createInfo.DisableManualFbo) + _surface = _gpu?.TryCreateSurface(PixelSize, createInfo.Session); if (_surface == null) _surface = new SkiaSurfaceWrapper(CreateSurface(createInfo.GrContext, PixelSize.Width, PixelSize.Height, createInfo.Format)); @@ -220,6 +221,8 @@ namespace Avalonia.Skia public ISkiaGpu Gpu; public ISkiaGpuRenderSession Session; + + public bool DisableManualFbo; } } } diff --git a/src/Windows/Avalonia.Win32/.gitignore b/src/Windows/Avalonia.Win32/.gitignore new file mode 100644 index 0000000000..7882fa6540 --- /dev/null +++ b/src/Windows/Avalonia.Win32/.gitignore @@ -0,0 +1 @@ +*.Generated.cs diff --git a/src/Windows/Avalonia.Win32/Avalonia.Win32.csproj b/src/Windows/Avalonia.Win32/Avalonia.Win32.csproj index 5889d919df..fe5f806fbe 100644 --- a/src/Windows/Avalonia.Win32/Avalonia.Win32.csproj +++ b/src/Windows/Avalonia.Win32/Avalonia.Win32.csproj @@ -5,9 +5,11 @@ Avalonia.Win32 + - + + diff --git a/src/Windows/Avalonia.Win32/Composition/CompositionBlurHost.cs b/src/Windows/Avalonia.Win32/Composition/CompositionBlurHost.cs deleted file mode 100644 index 4c090e797c..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/CompositionBlurHost.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Avalonia.Win32 -{ - internal class CompositionBlurHost : IBlurHost - { - Windows.UI.Composition.Visual _blurVisual; - - public CompositionBlurHost(Windows.UI.Composition.Visual blurVisual) - { - _blurVisual = blurVisual; - } - - public void SetBlur(bool enabled) - { - _blurVisual.IsVisible = enabled; - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs b/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs deleted file mode 100644 index e0dfdf282c..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/CompositionConnector.cs +++ /dev/null @@ -1,171 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using Avalonia.Logging; -using Avalonia.OpenGL; -using Avalonia.OpenGL.Angle; -using Avalonia.OpenGL.Egl; -using Windows.UI.Composition; -using Windows.UI.Composition.Interop; -using WinRT; - -namespace Avalonia.Win32 -{ - internal class CompositionConnector - { - private Compositor _compositor; - private Windows.System.DispatcherQueueController _dispatcherQueueController; - private CompositionGraphicsDevice _graphicsDevice; - - internal enum DISPATCHERQUEUE_THREAD_APARTMENTTYPE - { - DQTAT_COM_NONE = 0, - DQTAT_COM_ASTA = 1, - DQTAT_COM_STA = 2 - }; - - internal enum DISPATCHERQUEUE_THREAD_TYPE - { - DQTYPE_THREAD_DEDICATED = 1, - DQTYPE_THREAD_CURRENT = 2, - }; - - [StructLayout(LayoutKind.Sequential)] - internal struct DispatcherQueueOptions - { - public int dwSize; - - [MarshalAs(UnmanagedType.I4)] - public DISPATCHERQUEUE_THREAD_TYPE threadType; - - [MarshalAs(UnmanagedType.I4)] - public DISPATCHERQUEUE_THREAD_APARTMENTTYPE apartmentType; - }; - - [DllImport("coremessaging.dll", EntryPoint = "CreateDispatcherQueueController", CharSet = CharSet.Unicode)] - internal static extern IntPtr CreateDispatcherQueueController(DispatcherQueueOptions options, out IntPtr dispatcherQueueController); - - public static CompositionConnector TryCreate(EglPlatformOpenGlInterface egl) - { - const int majorRequired = 10; - const int buildRequired = 16299; - - var majorInstalled = Win32Platform.WindowsVersion.Major; - var buildInstalled = Win32Platform.WindowsVersion.Build; - - if (majorInstalled >= majorRequired && - buildInstalled >= buildRequired) - { - try - { - return new CompositionConnector(egl); - } - catch (Exception e) - { - Logger.TryGet(LogEventLevel.Error, "WinUIComposition")?.Log(null, "Unable to initialize WinUI compositor: {0}", e); - - return null; - } - } - - var osVersionNotice = $"Windows {majorRequired} Build {buildRequired} is required. Your machine has Windows {majorInstalled} Build {buildInstalled} installed."; - - Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, - $"Unable to initialize WinUI compositor: {osVersionNotice}"); - - return null; - } - - public CompositionConnector(EglPlatformOpenGlInterface egl) - { - EnsureDispatcherQueue(); - - if (_dispatcherQueueController != null) - _compositor = new Compositor(); - - var interop = _compositor.As(); - - var display = egl.Display as AngleWin32EglDisplay; - - _graphicsDevice = interop.CreateGraphicsDevice(display.GetDirect3DDevice()); - } - - public ICompositionDrawingSurfaceInterop InitialiseWindowCompositionTree(IntPtr hwnd, out Windows.UI.Composition.Visual surfaceVisual, out IBlurHost blurHost) - { - var target = CreateDesktopWindowTarget(hwnd); - - var surface = _graphicsDevice.CreateDrawingSurface(new Windows.Foundation.Size(0, 0), - Windows.Graphics.DirectX.DirectXPixelFormat.B8G8R8A8UIntNormalized, - Windows.Graphics.DirectX.DirectXAlphaMode.Premultiplied); - - var surfaceInterop = surface.As(); - - var brush = _compositor.CreateSurfaceBrush(surface); - var visual = _compositor.CreateSpriteVisual(); - - visual.Brush = brush; - visual.RelativeSizeAdjustment = new System.Numerics.Vector2(1, 1); - - var container = _compositor.CreateContainerVisual(); - - target.Root = container; - - var blur = CreateBlur(); - - blurHost = new CompositionBlurHost(blur); - - container.Children.InsertAtTop(blur); - - container.Children.InsertAtTop(visual); - - visual.CompositeMode = CompositionCompositeMode.SourceOver; - - surfaceVisual = container; - - return surfaceInterop; - } - - private SpriteVisual CreateBlur() - { - var blurEffect = new GaussianBlurEffect(new CompositionEffectSourceParameter("backdrop")); - - var backDropBrush = _compositor.CreateBackdropBrush(); - - var saturateEffect = new SaturationEffect(blurEffect); - var satEffectFactory = _compositor.CreateEffectFactory(saturateEffect); - var satBrush = satEffectFactory.CreateBrush(); - - satBrush.SetSourceParameter("backdrop", backDropBrush); - - var visual = _compositor.CreateSpriteVisual(); - visual.IsVisible = false; - - visual.RelativeSizeAdjustment = new System.Numerics.Vector2(1.0f, 1.0f); - visual.Brush = satBrush; - - return visual; - } - - private CompositionTarget CreateDesktopWindowTarget(IntPtr window) - { - var interop = _compositor.As(); - - interop.CreateDesktopWindowTarget(window, false, out var windowTarget); - return Windows.UI.Composition.Desktop.DesktopWindowTarget.FromAbi(windowTarget); - } - - private void EnsureDispatcherQueue() - { - if (_dispatcherQueueController == null) - { - DispatcherQueueOptions options = new DispatcherQueueOptions(); - options.apartmentType = DISPATCHERQUEUE_THREAD_APARTMENTTYPE.DQTAT_COM_NONE; - options.threadType = DISPATCHERQUEUE_THREAD_TYPE.DQTYPE_THREAD_CURRENT; - options.dwSize = Marshal.SizeOf(typeof(DispatcherQueueOptions)); - - CreateDispatcherQueueController(options, out var queue); - _dispatcherQueueController = Windows.System.DispatcherQueueController.FromAbi(queue); - } - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/CompositionEglGlPlatformSurface.cs b/src/Windows/Avalonia.Win32/Composition/CompositionEglGlPlatformSurface.cs deleted file mode 100644 index 441da93787..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/CompositionEglGlPlatformSurface.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using Avalonia.OpenGL; -using Avalonia.OpenGL.Angle; -using Avalonia.OpenGL.Egl; -using Avalonia.OpenGL.Surfaces; -using Windows.UI.Composition.Interop; - -namespace Avalonia.Win32 -{ - internal class CompositionEglGlPlatformSurface : EglGlPlatformSurfaceBase - { - private EglPlatformOpenGlInterface _egl; - private readonly IEglWindowGlPlatformSurfaceInfo _info; - private ICompositionDrawingSurfaceInterop _surfaceInterop; - private Windows.UI.Composition.Visual _surface; - - public CompositionEglGlPlatformSurface(EglPlatformOpenGlInterface egl, IEglWindowGlPlatformSurfaceInfo info) : base() - { - _egl = egl; - _info = info; - } - - public IBlurHost AttachToCompositionTree(CompositionConnector connector, IntPtr hwnd) - { - using (_egl.PrimaryContext.MakeCurrent()) - { - _surfaceInterop = connector.InitialiseWindowCompositionTree(hwnd, out _surface, out var blurHost); - return blurHost; - } - } - - public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget() - { - return new CompositionRenderTarget(_egl, _surface, _surfaceInterop, _info); - } - - class CompositionRenderTarget : EglPlatformSurfaceRenderTargetBase - { - private readonly EglPlatformOpenGlInterface _egl; - private readonly IEglWindowGlPlatformSurfaceInfo _info; - private PixelSize _currentSize; - private readonly ICompositionDrawingSurfaceInterop _surfaceInterop; - private static Guid s_Iid = Guid.Parse("6f15aaf2-d208-4e89-9ab4-489535d34f9c"); - private Windows.UI.Composition.Visual _compositionVisual; - - public CompositionRenderTarget(EglPlatformOpenGlInterface egl, - Windows.UI.Composition.Visual compositionVisual, - ICompositionDrawingSurfaceInterop interopSurface, - IEglWindowGlPlatformSurfaceInfo info) - : base(egl) - { - _egl = egl; - _surfaceInterop = interopSurface; - _info = info; - _currentSize = info.Size; - _compositionVisual = compositionVisual; - - using (_egl.PrimaryContext.MakeCurrent()) - { - _surfaceInterop.Resize(new POINT { X = _info.Size.Width, Y = _info.Size.Height }); - } - - _compositionVisual.Size = new System.Numerics.Vector2(_info.Size.Width, _info.Size.Height); - } - - public override IGlPlatformSurfaceRenderingSession BeginDraw() - { - IntPtr texture; - EglSurface surface; - - using (_egl.PrimaryEglContext.EnsureCurrent()) - { - if (_info.Size != _currentSize) - { - _surfaceInterop.Resize(new POINT { X = _info.Size.Width, Y = _info.Size.Height }); - _compositionVisual.Size = new System.Numerics.Vector2(_info.Size.Width, _info.Size.Height); - _currentSize = _info.Size; - } - - var offset = new POINT(); - - _surfaceInterop.BeginDraw( - IntPtr.Zero, - ref s_Iid, - out texture, ref offset); - - surface = (_egl.Display as AngleWin32EglDisplay).WrapDirect3D11Texture(_egl, texture, offset.X, offset.Y, _info.Size.Width, _info.Size.Height); - } - - return base.BeginDraw(surface, _info, () => { _surfaceInterop.EndDraw(); Marshal.Release(texture); surface.Dispose(); }, true); - } - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/D2DEffects.cs b/src/Windows/Avalonia.Win32/Composition/D2DEffects.cs deleted file mode 100644 index 1c761ee082..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/D2DEffects.cs +++ /dev/null @@ -1,91 +0,0 @@ -using System; - - -namespace Avalonia.Win32 -{ - class D2DEffects - { - public static readonly Guid CLSID_D2D12DAffineTransform = new Guid(0x6AA97485, 0x6354, 0x4CFC, 0x90, 0x8C, 0xE4, 0xA7, 0x4F, 0x62, 0xC9, 0x6C); - - public static readonly Guid CLSID_D2D13DPerspectiveTransform = new Guid(0xC2844D0B, 0x3D86, 0x46E7, 0x85, 0xBA, 0x52, 0x6C, 0x92, 0x40, 0xF3, 0xFB); - - public static readonly Guid CLSID_D2D13DTransform = new Guid(0xE8467B04, 0xEC61, 0x4B8A, 0xB5, 0xDE, 0xD4, 0xD7, 0x3D, 0xEB, 0xEA, 0x5A); - - public static readonly Guid CLSID_D2D1ArithmeticComposite = new Guid(0xFC151437, 0x049A, 0x4784, 0xA2, 0x4A, 0xF1, 0xC4, 0xDA, 0xF2, 0x09, 0x87); - - public static readonly Guid CLSID_D2D1Atlas = new Guid(0x913E2BE4, 0xFDCF, 0x4FE2, 0xA5, 0xF0, 0x24, 0x54, 0xF1, 0x4F, 0xF4, 0x08); - - public static readonly Guid CLSID_D2D1BitmapSource = new Guid(0x5FB6C24D, 0xC6DD, 0x4231, 0x94, 0x4, 0x50, 0xF4, 0xD5, 0xC3, 0x25, 0x2D); - - public static readonly Guid CLSID_D2D1Blend = new Guid(0x81C5B77B, 0x13F8, 0x4CDD, 0xAD, 0x20, 0xC8, 0x90, 0x54, 0x7A, 0xC6, 0x5D); - - public static readonly Guid CLSID_D2D1Border = new Guid(0x2A2D49C0, 0x4ACF, 0x43C7, 0x8C, 0x6A, 0x7C, 0x4A, 0x27, 0x87, 0x4D, 0x27); - - public static readonly Guid CLSID_D2D1Brightness = new Guid(0x8CEA8D1E, 0x77B0, 0x4986, 0xB3, 0xB9, 0x2F, 0x0C, 0x0E, 0xAE, 0x78, 0x87); - - public static readonly Guid CLSID_D2D1ColorManagement = new Guid(0x1A28524C, 0xFDD6, 0x4AA4, 0xAE, 0x8F, 0x83, 0x7E, 0xB8, 0x26, 0x7B, 0x37); - - public static readonly Guid CLSID_D2D1ColorMatrix = new Guid(0x921F03D6, 0x641C, 0x47DF, 0x85, 0x2D, 0xB4, 0xBB, 0x61, 0x53, 0xAE, 0x11); - - public static readonly Guid CLSID_D2D1Composite = new Guid(0x48FC9F51, 0xF6AC, 0x48F1, 0x8B, 0x58, 0x3B, 0x28, 0xAC, 0x46, 0xF7, 0x6D); - - public static readonly Guid CLSID_D2D1ConvolveMatrix = new Guid(0x407F8C08, 0x5533, 0x4331, 0xA3, 0x41, 0x23, 0xCC, 0x38, 0x77, 0x84, 0x3E); - - public static readonly Guid CLSID_D2D1Crop = new Guid(0xE23F7110, 0x0E9A, 0x4324, 0xAF, 0x47, 0x6A, 0x2C, 0x0C, 0x46, 0xF3, 0x5B); - - public static readonly Guid CLSID_D2D1DirectionalBlur = new Guid(0x174319A6, 0x58E9, 0x49B2, 0xBB, 0x63, 0xCA, 0xF2, 0xC8, 0x11, 0xA3, 0xDB); - - public static readonly Guid CLSID_D2D1DiscreteTransfer = new Guid(0x90866FCD, 0x488E, 0x454B, 0xAF, 0x06, 0xE5, 0x04, 0x1B, 0x66, 0xC3, 0x6C); - - public static readonly Guid CLSID_D2D1DisplacementMap = new Guid(0xEDC48364, 0x417, 0x4111, 0x94, 0x50, 0x43, 0x84, 0x5F, 0xA9, 0xF8, 0x90); - - public static readonly Guid CLSID_D2D1DistantDiffuse = new Guid(0x3E7EFD62, 0xA32D, 0x46D4, 0xA8, 0x3C, 0x52, 0x78, 0x88, 0x9A, 0xC9, 0x54); - - public static readonly Guid CLSID_D2D1DistantSpecular = new Guid(0x428C1EE5, 0x77B8, 0x4450, 0x8A, 0xB5, 0x72, 0x21, 0x9C, 0x21, 0xAB, 0xDA); - - public static readonly Guid CLSID_D2D1DpiCompensation = new Guid(0x6C26C5C7, 0x34E0, 0x46FC, 0x9C, 0xFD, 0xE5, 0x82, 0x37, 0x6, 0xE2, 0x28); - - public static readonly Guid CLSID_D2D1Flood = new Guid(0x61C23C20, 0xAE69, 0x4D8E, 0x94, 0xCF, 0x50, 0x07, 0x8D, 0xF6, 0x38, 0xF2); - - public static readonly Guid CLSID_D2D1GammaTransfer = new Guid(0x409444C4, 0xC419, 0x41A0, 0xB0, 0xC1, 0x8C, 0xD0, 0xC0, 0xA1, 0x8E, 0x42); - - public static readonly Guid CLSID_D2D1GaussianBlur = new Guid(0x1FEB6D69, 0x2FE6, 0x4AC9, 0x8C, 0x58, 0x1D, 0x7F, 0x93, 0xE7, 0xA6, 0xA5); - - public static readonly Guid CLSID_D2D1Scale = new Guid(0x9DAF9369, 0x3846, 0x4D0E, 0xA4, 0x4E, 0xC, 0x60, 0x79, 0x34, 0xA5, 0xD7); - - public static readonly Guid CLSID_D2D1Histogram = new Guid(0x881DB7D0, 0xF7EE, 0x4D4D, 0xA6, 0xD2, 0x46, 0x97, 0xAC, 0xC6, 0x6E, 0xE8); - - public static readonly Guid CLSID_D2D1HueRotation = new Guid(0x0F4458EC, 0x4B32, 0x491B, 0x9E, 0x85, 0xBD, 0x73, 0xF4, 0x4D, 0x3E, 0xB6); - - public static readonly Guid CLSID_D2D1LinearTransfer = new Guid(0xAD47C8FD, 0x63EF, 0x4ACC, 0x9B, 0x51, 0x67, 0x97, 0x9C, 0x03, 0x6C, 0x06); - - public static readonly Guid CLSID_D2D1LuminanceToAlpha = new Guid(0x41251AB7, 0x0BEB, 0x46F8, 0x9D, 0xA7, 0x59, 0xE9, 0x3F, 0xCC, 0xE5, 0xDE); - - public static readonly Guid CLSID_D2D1Morphology = new Guid(0xEAE6C40D, 0x626A, 0x4C2D, 0xBF, 0xCB, 0x39, 0x10, 0x01, 0xAB, 0xE2, 0x02); - - public static readonly Guid CLSID_D2D1OpacityMetadata = new Guid(0x6C53006A, 0x4450, 0x4199, 0xAA, 0x5B, 0xAD, 0x16, 0x56, 0xFE, 0xCE, 0x5E); - - public static readonly Guid CLSID_D2D1PointDiffuse = new Guid(0xB9E303C3, 0xC08C, 0x4F91, 0x8B, 0x7B, 0x38, 0x65, 0x6B, 0xC4, 0x8C, 0x20); - - public static readonly Guid CLSID_D2D1PointSpecular = new Guid(0x09C3CA26, 0x3AE2, 0x4F09, 0x9E, 0xBC, 0xED, 0x38, 0x65, 0xD5, 0x3F, 0x22); - - public static readonly Guid CLSID_D2D1Premultiply = new Guid(0x06EAB419, 0xDEED, 0x4018, 0x80, 0xD2, 0x3E, 0x1D, 0x47, 0x1A, 0xDE, 0xB2); - - public static readonly Guid CLSID_D2D1Saturation = new Guid(0x5CB2D9CF, 0x327D, 0x459F, 0xA0, 0xCE, 0x40, 0xC0, 0xB2, 0x08, 0x6B, 0xF7); - - public static readonly Guid CLSID_D2D1Shadow = new Guid(0xC67EA361, 0x1863, 0x4E69, 0x89, 0xDB, 0x69, 0x5D, 0x3E, 0x9A, 0x5B, 0x6B); - - public static readonly Guid CLSID_D2D1SpotDiffuse = new Guid(0x818A1105, 0x7932, 0x44F4, 0xAA, 0x86, 0x08, 0xAE, 0x7B, 0x2F, 0x2C, 0x93); - - public static readonly Guid CLSID_D2D1SpotSpecular = new Guid(0xEDAE421E, 0x7654, 0x4A37, 0x9D, 0xB8, 0x71, 0xAC, 0xC1, 0xBE, 0xB3, 0xC1); - - public static readonly Guid CLSID_D2D1TableTransfer = new Guid(0x5BF818C3, 0x5E43, 0x48CB, 0xB6, 0x31, 0x86, 0x83, 0x96, 0xD6, 0xA1, 0xD4); - - public static readonly Guid CLSID_D2D1Tile = new Guid(0xB0784138, 0x3B76, 0x4BC5, 0xB1, 0x3B, 0x0F, 0xA2, 0xAD, 0x02, 0x65, 0x9F); - - public static readonly Guid CLSID_D2D1Turbulence = new Guid(0xCF2BB6AE, 0x889A, 0x4AD7, 0xBA, 0x29, 0xA2, 0xFD, 0x73, 0x2C, 0x9F, 0xC9); - - public static readonly Guid CLSID_D2D1UnPremultiply = new Guid(0xFB9AC489, 0xAD8D, 0x41ED, 0x99, 0x99, 0xBB, 0x63, 0x47, 0xD1, 0x10, 0xF7); - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/EffectBase.cs b/src/Windows/Avalonia.Win32/Composition/EffectBase.cs deleted file mode 100644 index ca5b15971e..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/EffectBase.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using Windows.Graphics.Effects; -using Windows.Graphics.Effects.Interop; - - -namespace Avalonia.Win32 -{ - abstract class EffectBase : IGraphicsEffect, IGraphicsEffectSource, global::Windows.Graphics.Effects.Interop.IGraphicsEffectD2D1Interop - { - private IGraphicsEffectSource[] _sources; - - public EffectBase(params IGraphicsEffectSource[] sources) - { - _sources = sources; - } - - private IGraphicsEffectSource _source; - - public virtual string Name { get; set; } - - public abstract Guid EffectId { get; } - - public abstract uint PropertyCount { get; } - - public uint SourceCount => (uint)_sources.Length; - - public IGraphicsEffectSource GetSource(uint index) - { - if(index < _sources.Length) - { - return _sources[index]; - } - - return null; - } - - public uint GetNamedPropertyMapping(string name, out GRAPHICS_EFFECT_PROPERTY_MAPPING mapping) - { - throw new NotImplementedException(); - } - - public abstract object GetProperty(uint index); - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/GRAPHICS_EFFECT_PROPERTY_MAPPING.cs b/src/Windows/Avalonia.Win32/Composition/GRAPHICS_EFFECT_PROPERTY_MAPPING.cs deleted file mode 100644 index f5d5fc4ad3..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/GRAPHICS_EFFECT_PROPERTY_MAPPING.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace Windows.Graphics.Effects.Interop -{ - public enum GRAPHICS_EFFECT_PROPERTY_MAPPING - { - GRAPHICS_EFFECT_PROPERTY_MAPPING_UNKNOWN, - GRAPHICS_EFFECT_PROPERTY_MAPPING_DIRECT, - GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORX, - GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORY, - GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORZ, - GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORW, - GRAPHICS_EFFECT_PROPERTY_MAPPING_RECT_TO_VECTOR4, - GRAPHICS_EFFECT_PROPERTY_MAPPING_RADIANS_TO_DEGREES, - GRAPHICS_EFFECT_PROPERTY_MAPPING_COLORMATRIX_ALPHA_MODE, - GRAPHICS_EFFECT_PROPERTY_MAPPING_COLOR_TO_VECTOR3, - GRAPHICS_EFFECT_PROPERTY_MAPPING_COLOR_TO_VECTOR4 - }; -} - diff --git a/src/Windows/Avalonia.Win32/Composition/GaussianBlurEffect.cs b/src/Windows/Avalonia.Win32/Composition/GaussianBlurEffect.cs deleted file mode 100644 index 342e68eeb4..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/GaussianBlurEffect.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using Windows.Graphics.Effects; - -namespace Avalonia.Win32 -{ - class GaussianBlurEffect : EffectBase - { - public GaussianBlurEffect(IGraphicsEffectSource source) : base(source) - { - } - - enum D2D1_GAUSSIANBLUR_OPTIMIZATION - { - D2D1_GAUSSIANBLUR_OPTIMIZATION_SPEED, - D2D1_GAUSSIANBLUR_OPTIMIZATION_BALANCED, - D2D1_GAUSSIANBLUR_OPTIMIZATION_QUALITY, - D2D1_GAUSSIANBLUR_OPTIMIZATION_FORCE_DWORD - }; - - enum D2D1_BORDER_MODE - { - D2D1_BORDER_MODE_SOFT, - D2D1_BORDER_MODE_HARD, - D2D1_BORDER_MODE_FORCE_DWORD - }; - - enum D2D1GaussianBlurProp - { - D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, - D2D1_GAUSSIANBLUR_PROP_OPTIMIZATION, - D2D1_GAUSSIANBLUR_PROP_BORDER_MODE, - D2D1_GAUSSIANBLUR_PROP_FORCE_DWORD - }; - - public override Guid EffectId => D2DEffects.CLSID_D2D1GaussianBlur; - - public override uint PropertyCount => 3; - - public override object GetProperty(uint index) - { - switch ((D2D1GaussianBlurProp)index) - { - case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION: - return 30.0f; - - case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_OPTIMIZATION: - return (uint)D2D1_GAUSSIANBLUR_OPTIMIZATION.D2D1_GAUSSIANBLUR_OPTIMIZATION_BALANCED; - - case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_BORDER_MODE: - return (uint)D2D1_BORDER_MODE.D2D1_BORDER_MODE_HARD; - } - - return null; - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/IBlurHost.cs b/src/Windows/Avalonia.Win32/Composition/IBlurHost.cs deleted file mode 100644 index 6ab470d81c..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/IBlurHost.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Avalonia.Win32 -{ - internal interface IBlurHost - { - void SetBlur(bool enabled); - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/ICompositionDrawingSurfaceInterop.cs b/src/Windows/Avalonia.Win32/Composition/ICompositionDrawingSurfaceInterop.cs deleted file mode 100644 index 2eac796376..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/ICompositionDrawingSurfaceInterop.cs +++ /dev/null @@ -1,152 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using WinRT; - -namespace Windows.UI.Composition.Interop -{ - public struct POINT - { - public int X; - public int Y; - } - - public struct RECT - { - public int left; - public int top; - public int right; - public int bottom; - - public int Width => right - left; - public int Height => bottom - top; - } - - [WindowsRuntimeType] - [Guid("FD04E6E3-FE0C-4C3C-AB19-A07601A576EE")] - public interface ICompositionDrawingSurfaceInterop - { - void BeginDraw(IntPtr updateRect, ref Guid iid, out IntPtr updateObject, ref POINT point); - - void EndDraw(); - - void Resize(POINT sizePixels); - - void ResumeDraw(); - - void Scroll(RECT scrollRect, RECT clipRect, int offsetX, int offsetY); - - void SuspendDraw(); - } -} - -namespace ABI.Windows.UI.Composition.Interop -{ - using global::System; - using global::System.Runtime.InteropServices; - using global::Windows.UI.Composition; - using global::Windows.UI.Composition.Interop; - - [Guid("FD04E6E3-FE0C-4C3C-AB19-A07601A576EE")] - internal class ICompositionDrawingSurfaceInterop : global::Windows.UI.Composition.Interop.ICompositionDrawingSurfaceInterop - - { - [Guid("FD04E6E3-FE0C-4C3C-AB19-A07601A576EE")] - public unsafe struct Vftbl - { - public delegate int _BeginDraw(IntPtr ThisPtr, IntPtr updateRect, ref Guid iid, out IntPtr updateObject, ref POINT updateOffset); - public delegate int _EndDraw(IntPtr ThisPtr); - public delegate int _Resize(IntPtr ThisPtr, POINT sizePixels); - public delegate int _ResumeDraw(IntPtr ThisPtr); - public delegate int _Scroll(IntPtr ThisPtr, RECT scrollRect, RECT clipRect, int offsetX, int offsetY); - public delegate int _SuspendDraw(IntPtr ThisPtr); - - internal global::WinRT.Interop.IUnknownVftbl IUnknownVftbl; - public _BeginDraw BeginDraw; - public _EndDraw EndDraw; - public _Resize Resize; - public _ResumeDraw ResumeDraw; - public _Scroll Scroll; - public _SuspendDraw SuspendDraw; - - public static readonly Vftbl AbiToProjectionVftable; - public static readonly IntPtr AbiToProjectionVftablePtr; - - static Vftbl() - { - AbiToProjectionVftable = new Vftbl - { - IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl, - - BeginDraw = Do_Abi_BeginDraw, - EndDraw = Do_Abi_EndDraw, - Resize = Do_Abi_Resize - - - }; - AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false); - } - - private static int Do_Abi_BeginDraw(IntPtr ThisPtr, IntPtr updateRect, ref Guid iid, out IntPtr updateObject, ref POINT updateOffset) - { - updateObject = IntPtr.Zero; - return 0; - } - - private static int Do_Abi_EndDraw(IntPtr ThisPtr) - { - return 0; - } - - private static int Do_Abi_Resize(IntPtr ThisPtr, POINT sizePixels) - { - return 0; - } - } - internal static ObjectReference FromAbi(IntPtr thisPtr) => ObjectReference.FromAbi(thisPtr); - - public static implicit operator ICompositionDrawingSurfaceInterop(IObjectReference obj) => (obj != null) ? new ICompositionDrawingSurfaceInterop(obj) : null; - protected readonly ObjectReference _obj; - public IObjectReference ObjRef { get => _obj; } - public IntPtr ThisPtr => _obj.ThisPtr; - public ObjectReference AsInterface() => _obj.As(); - public A As() => _obj.AsType(); - - public ICompositionDrawingSurfaceInterop(IObjectReference obj) : this(obj.As()) { } - internal ICompositionDrawingSurfaceInterop(ObjectReference obj) - { - _obj = obj; - } - - public void BeginDraw(IntPtr updateRect, ref Guid iid, out IntPtr updateObject, ref POINT point) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.BeginDraw(ThisPtr, updateRect, ref iid, out updateObject, ref point)); - } - - public void EndDraw() - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.EndDraw(ThisPtr)); - } - - public void Resize(POINT sizePixels) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.Resize(ThisPtr, sizePixels)); - } - - public void ResumeDraw() - { - throw new NotImplementedException(); - } - - public void Scroll(RECT scrollRect, RECT clipRect, int offsetX, int offsetY) - { - throw new NotImplementedException(); - } - - public void SuspendDraw() - { - throw new NotImplementedException(); - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop.cs b/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop.cs deleted file mode 100644 index 1d4cd3450f..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using WinRT; - -namespace Windows.UI.Composition.Desktop -{ - [WindowsRuntimeType] - [Guid("29E691FA-4567-4DCA-B319-D0F207EB6807")] - public interface ICompositorDesktopInterop - { - void CreateDesktopWindowTarget(IntPtr hwndTarget, bool isTopmost, out IntPtr test); - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop1.cs b/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop1.cs deleted file mode 100644 index 1c3f06d679..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/ICompositorDesktopInterop1.cs +++ /dev/null @@ -1,69 +0,0 @@ -using WinRT; - -namespace ABI.Windows.UI.Composition.Desktop -{ - using global::System; - using global::System.Runtime.InteropServices; - - [Guid("29E691FA-4567-4DCA-B319-D0F207EB6807")] - internal class ICompositorDesktopInterop : global::Windows.UI.Composition.Desktop.ICompositorDesktopInterop - - { - [Guid("29E691FA-4567-4DCA-B319-D0F207EB6807")] - public struct Vftbl - { - public delegate int _CreateDesktopWindowTarget(IntPtr thisPtr, IntPtr hwndTarget, byte isTopMost, out IntPtr desktopWindowTarget); - - internal global::WinRT.Interop.IUnknownVftbl IUnknownVftbl; - public _CreateDesktopWindowTarget CreateDesktopWindowTarget; - - - public static readonly Vftbl AbiToProjectionVftable; - public static readonly IntPtr AbiToProjectionVftablePtr; - - static Vftbl() - { - AbiToProjectionVftable = new Vftbl - { - IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl, - CreateDesktopWindowTarget = Do_Abi_Create_Desktop_Window_Target - }; - AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false); - } - - private static int Do_Abi_Create_Desktop_Window_Target(IntPtr thisPtr, IntPtr hwndTarget, byte isTopMost, out IntPtr desktopWindowTarget) - { - try - { - ComWrappersSupport.FindObject(thisPtr).CreateDesktopWindowTarget(hwndTarget, isTopMost != 0, out desktopWindowTarget); - return 0; - } - catch (Exception ex) - { - desktopWindowTarget = IntPtr.Zero; - return Marshal.GetHRForException(ex); - } - } - } - internal static ObjectReference FromAbi(IntPtr thisPtr) => ObjectReference.FromAbi(thisPtr); - - public static implicit operator ICompositorDesktopInterop(IObjectReference obj) => (obj != null) ? new ICompositorDesktopInterop(obj) : null; - protected readonly ObjectReference _obj; - public IObjectReference ObjRef { get => _obj; } - public IntPtr ThisPtr => _obj.ThisPtr; - public ObjectReference AsInterface() => _obj.As(); - public A As() => _obj.AsType(); - public ICompositorDesktopInterop(IObjectReference obj) : this(obj.As()) { } - internal ICompositorDesktopInterop(ObjectReference obj) - { - _obj = obj; - } - - public void CreateDesktopWindowTarget(IntPtr hwndTarget, bool isTopmost, out IntPtr test) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.CreateDesktopWindowTarget(ThisPtr, hwndTarget, isTopmost ? (byte)1 : (byte)0, out test)); - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/ICompositorInterop.cs b/src/Windows/Avalonia.Win32/Composition/ICompositorInterop.cs deleted file mode 100644 index d9b25e497e..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/ICompositorInterop.cs +++ /dev/null @@ -1,139 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using WinRT; - -namespace Windows.UI.Composition.Interop -{ - [WindowsRuntimeType] - [Guid("25297D5C-3AD4-4C9C-B5CF-E36A38512330")] - public interface ICompositorInterop - { - ICompositionSurface CreateCompositionSurfaceForHandle(IntPtr swapChain); - - ICompositionSurface CreateCompositionSurfaceForSwapChain(IntPtr swapChain); - - CompositionGraphicsDevice CreateGraphicsDevice(IntPtr renderingDevice); - } -} - -namespace ABI.Windows.UI.Composition.Interop -{ - using global::System; - using global::System.Runtime.InteropServices; - using global::Windows.UI.Composition; - - [Guid("25297D5C-3AD4-4C9C-B5CF-E36A38512330")] - internal class ICompositorInterop : global::Windows.UI.Composition.Interop.ICompositorInterop - - { - [Guid("25297D5C-3AD4-4C9C-B5CF-E36A38512330")] - public struct Vftbl - { - public delegate int _CreateCompositionSurfaceForHandle(IntPtr ThisPtr, IntPtr swapChain, out IntPtr result); - public delegate int _CreateCompositionSurfaceForSwapChain(IntPtr ThisPtr, IntPtr swapChain, out IntPtr result); - public delegate int _CreateGraphicsDevice(IntPtr ThisPtr, IntPtr renderingDevice, out IntPtr result); - - internal global::WinRT.Interop.IUnknownVftbl IUnknownVftbl; - public _CreateCompositionSurfaceForHandle CreateCompositionSurfaceForHandle; - public _CreateCompositionSurfaceForSwapChain CreateCompositionSurfaceForSwapChain; - public _CreateGraphicsDevice CreateGraphicsDevice; - - - public static readonly Vftbl AbiToProjectionVftable; - public static readonly IntPtr AbiToProjectionVftablePtr; - - static Vftbl() - { - AbiToProjectionVftable = new Vftbl - { - IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl, - - CreateCompositionSurfaceForHandle = Do_Abi_Create_Composition_Surface_For_Handle, - CreateCompositionSurfaceForSwapChain = Do_Abi_Create_Composition_Surface_For_SwapChain, - CreateGraphicsDevice= Do_Abi_Create_Graphics_Device - }; - AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false); - } - - private static int Do_Abi_Create_Composition_Surface_For_Handle(IntPtr thisPtr, IntPtr swapChain, out IntPtr surface) - { - try - { - surface = IntPtr.Zero; - //surface = ComWrappersSupport.FindObject(thisPtr).CreateCompositionSurfaceForHandle(swapChain); - return 0; - } - catch (Exception ex) - { - surface = IntPtr.Zero; - return Marshal.GetHRForException(ex); - } - } - - private static int Do_Abi_Create_Composition_Surface_For_SwapChain(IntPtr thisPtr, IntPtr swapChain, out IntPtr surface) - { - try - { - surface = IntPtr.Zero; - //surface = ComWrappersSupport.FindObject(thisPtr).CreateCompositionSurfaceForSwapChain(swapChain); - return 0; - } - catch (Exception ex) - { - surface = IntPtr.Zero; - return Marshal.GetHRForException(ex); - } - } - - private static int Do_Abi_Create_Graphics_Device(IntPtr thisPtr, IntPtr renderingDevice, out IntPtr graphicsDevice) - { - try - { - graphicsDevice = ComWrappersSupport.FindObject(thisPtr).CreateGraphicsDevice(renderingDevice).ThisPtr; - return 0; - } - catch (Exception ex) - { - graphicsDevice = IntPtr.Zero; - return Marshal.GetHRForException(ex); - } - } - } - internal static ObjectReference FromAbi(IntPtr thisPtr) => ObjectReference.FromAbi(thisPtr); - - public static implicit operator ICompositorInterop(IObjectReference obj) => (obj != null) ? new ICompositorInterop(obj) : null; - protected readonly ObjectReference _obj; - public IObjectReference ObjRef { get => _obj; } - public IntPtr ThisPtr => _obj.ThisPtr; - public ObjectReference AsInterface() => _obj.As(); - public A As() => _obj.AsType(); - public ICompositorInterop(IObjectReference obj) : this(obj.As()) { } - internal ICompositorInterop(ObjectReference obj) - { - _obj = obj; - } - - public ICompositionSurface CreateCompositionSurfaceForHandle(IntPtr swapChain) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.CreateCompositionSurfaceForHandle(ThisPtr, swapChain, out var compositionSurface)); - - return null; - } - - public ICompositionSurface CreateCompositionSurfaceForSwapChain(IntPtr swapChain) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.CreateCompositionSurfaceForSwapChain(ThisPtr, swapChain, out var compositionSurface)); - - return null; - } - - public CompositionGraphicsDevice CreateGraphicsDevice(IntPtr renderingDevice) - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.CreateGraphicsDevice(ThisPtr, renderingDevice, out var graphicsDevice)); - - return CompositionGraphicsDevice.FromAbi(graphicsDevice); - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop.cs b/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop.cs deleted file mode 100644 index 74d3939a98..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Runtime.InteropServices; -using WinRT; - -namespace Windows.Graphics.Effects.Interop -{ - [WindowsRuntimeType] - [Guid("2FC57384-A068-44D7-A331-30982FCF7177")] - public interface IGraphicsEffectD2D1Interop - { - Guid EffectId { get; } - - uint GetNamedPropertyMapping(string name, out GRAPHICS_EFFECT_PROPERTY_MAPPING mapping); - - object GetProperty(uint index); - - uint PropertyCount { get; } - - IGraphicsEffectSource GetSource(uint index); - - uint SourceCount { get; } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop1.cs b/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop1.cs deleted file mode 100644 index 8466b05fb5..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/IGraphicsEffectD2D1Interop1.cs +++ /dev/null @@ -1,217 +0,0 @@ -using WinRT; - -namespace ABI.Windows.Graphics.Effects.Interop -{ - using global::System; - using global::System.Runtime.InteropServices; - - [Guid("2FC57384-A068-44D7-A331-30982FCF7177")] - internal class IGraphicsEffectD2D1Interop : global::Windows.Graphics.Effects.Interop.IGraphicsEffectD2D1Interop - - { - [Guid("2FC57384-A068-44D7-A331-30982FCF7177")] - public struct Vftbl - { - public delegate int _GetEffectId(IntPtr thisPtr, out Guid guid); - public delegate int _GetNamedPropertyMapping(IntPtr thisPtr, IntPtr name, IntPtr index, IntPtr mapping); - public delegate int _GetProperty(IntPtr thisPtr, uint index, out IntPtr value); - public unsafe delegate int _GetPropertyCount(IntPtr thisPtr, uint* count); - public delegate int _GetSource(IntPtr thisPtr, uint index, out IntPtr source); - public delegate int _GetSourceCount(IntPtr thisPtr, out uint count); - - internal global::WinRT.Interop.IUnknownVftbl IUnknownVftbl; - public _GetEffectId GetEffectId; - public _GetNamedPropertyMapping GetNamedPropertyMapping; - public _GetPropertyCount GetPropertyCount; - public _GetProperty GetProperty; - public _GetSource GetSource; - public _GetSourceCount GetSourceCount; - - public static readonly Vftbl AbiToProjectionVftable; - public static readonly IntPtr AbiToProjectionVftablePtr; - - unsafe static Vftbl() - { - AbiToProjectionVftable = new Vftbl - { - IUnknownVftbl = global::WinRT.Interop.IUnknownVftbl.AbiToProjectionVftbl, - GetEffectId = Do_Abi_Get_Effect_Id, - GetNamedPropertyMapping = Do_Abi_Get_Property_Mapping, - GetPropertyCount = Do_Abi_Get_Property_Count, - GetProperty = Do_Abi_Get_Property, - GetSource = Do_Abi_Get_Source, - GetSourceCount = Do_Abi_Get_Source_Count - - }; - AbiToProjectionVftablePtr = Marshal.AllocHGlobal(Marshal.SizeOf()); - Marshal.StructureToPtr(AbiToProjectionVftable, AbiToProjectionVftablePtr, false); - } - - private static int Do_Abi_Get_Effect_Id(IntPtr thisPtr, out Guid guid) - { - guid = default; - - try - { - guid = ComWrappersSupport.FindObject(thisPtr).EffectId; - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - - private static int Do_Abi_Get_Property_Mapping(IntPtr thisPtr, IntPtr name, IntPtr index, IntPtr mapping) - { - try - { - ComWrappersSupport.FindObject(thisPtr).GetNamedPropertyMapping(MarshalString.FromAbi(name), out var mappingResult); - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - - private static int Do_Abi_Get_Property(IntPtr thisPtr, uint index, out IntPtr value) - { - value = default; - - try - { - value = MarshalInspectable.CreateMarshaler( - ComWrappersSupport.FindObject(thisPtr).GetProperty(index)) - .As(Guid.Parse("4BD682DD-7554-40E9-9A9B-82654EDE7E62")) - .GetRef(); - - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - - unsafe private static int Do_Abi_Get_Property_Count(IntPtr thisPtr, uint* count) - { - - try - { - var res = ComWrappersSupport.FindObject(thisPtr).PropertyCount; - - if (count != null) - { - *count = res; - } - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - - private static int Do_Abi_Get_Source(IntPtr thisPtr, uint index, out IntPtr value) - { - value = default; - - try - { - var source = ComWrappersSupport.FindObject(thisPtr).GetSource(index); - - value = MarshalInterface.FromManaged(source); - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - - private static int Do_Abi_Get_Source_Count(IntPtr thisPtr, out uint count) - { - count = default; - - try - { - count = ComWrappersSupport.FindObject(thisPtr).SourceCount; - } - catch (Exception ex) - { - return Marshal.GetHRForException(ex); - } - - return 0; - } - } - internal static ObjectReference FromAbi(IntPtr thisPtr) => ObjectReference.FromAbi(thisPtr); - - public static implicit operator IGraphicsEffectD2D1Interop(IObjectReference obj) => (obj != null) ? new IGraphicsEffectD2D1Interop(obj) : null; - protected readonly ObjectReference _obj; - public IObjectReference ObjRef { get => _obj; } - public IntPtr ThisPtr => _obj.ThisPtr; - - public ObjectReference AsInterface() => _obj.As(); - public A As() => _obj.AsType(); - public IGraphicsEffectD2D1Interop(IObjectReference obj) : this(obj.As()) { } - internal IGraphicsEffectD2D1Interop(ObjectReference obj) - { - _obj = obj; - } - - public Guid EffectId - { - get - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.GetEffectId(ThisPtr, out Guid guid)); - return guid; - } - } - - public uint PropertyCount - { - get - { - unsafe - { - uint count = default; - Marshal.ThrowExceptionForHR(_obj.Vftbl.GetPropertyCount(ThisPtr, &count)); - return count; - } - } - } - - public uint SourceCount - { - get - { - Marshal.ThrowExceptionForHR(_obj.Vftbl.GetSourceCount(ThisPtr, out uint count)); - return count; - } - } - - public uint GetNamedPropertyMapping(string name, out global::Windows.Graphics.Effects.Interop.GRAPHICS_EFFECT_PROPERTY_MAPPING mapping) - { - throw new NotImplementedException(); - } - - public object GetProperty(uint index) - { - // Marshal.ThrowExceptionForHR(_obj.Vftbl.GetProperty(ThisPtr, index, out IntPtr value)); - throw new NotImplementedException(); - } - - public global::Windows.Graphics.Effects.IGraphicsEffectSource GetSource(uint index) - { - throw new NotImplementedException(); - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs b/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs deleted file mode 100644 index 3495829f3a..0000000000 --- a/src/Windows/Avalonia.Win32/Composition/SaturationEffect.cs +++ /dev/null @@ -1,35 +0,0 @@ -using System; -using Windows.Graphics.Effects; - - -namespace Avalonia.Win32 -{ - class SaturationEffect : EffectBase - { - public SaturationEffect(IGraphicsEffectSource source) : base(source) - { - } - - enum D2D1_SATURATION_PROP - { - D2D1_SATURATION_PROP_SATURATION, - D2D1_SATURATION_PROP_FORCE_DWORD - }; - - public override Guid EffectId => D2DEffects.CLSID_D2D1Saturation; - - public override uint PropertyCount => 1; - - public override object GetProperty(uint index) - { - switch ((D2D1_SATURATION_PROP)index) - { - case D2D1_SATURATION_PROP.D2D1_SATURATION_PROP_SATURATION: - return 2.0f; - } - - return null; - } - } -} - diff --git a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs index f88c57cf59..d9f9e7a84e 100644 --- a/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs +++ b/src/Windows/Avalonia.Win32/Interop/UnmanagedMethods.cs @@ -1225,6 +1225,9 @@ namespace Avalonia.Win32.Interop [DllImport("kernel32.dll", SetLastError = true)] public static extern IntPtr LoadLibrary(string fileName); + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr LoadLibraryEx(string fileName, IntPtr hFile, int flags); [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)] public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); @@ -1382,6 +1385,9 @@ namespace Avalonia.Win32.Interop [DllImport("dwmapi.dll")] public static extern int DwmIsCompositionEnabled(out bool enabled); + [DllImport("dwmapi.dll")] + public static extern void DwmFlush(); + [DllImport("dwmapi.dll")] public static extern bool DwmDefWindowProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, ref IntPtr plResult); @@ -1613,6 +1619,12 @@ namespace Avalonia.Win32.Interop public int X; public int Y; } + + public struct SIZE + { + public int X; + public int Y; + } public struct RECT { diff --git a/src/Windows/Avalonia.Win32/Win32GlManager.cs b/src/Windows/Avalonia.Win32/Win32GlManager.cs index 4363b5fd29..b51f80e397 100644 --- a/src/Windows/Avalonia.Win32/Win32GlManager.cs +++ b/src/Windows/Avalonia.Win32/Win32GlManager.cs @@ -3,6 +3,7 @@ using Avalonia.OpenGL; using Avalonia.OpenGL.Angle; using Avalonia.OpenGL.Egl; using Avalonia.Win32.OpenGl; +using Avalonia.Win32.WinRT.Composition; namespace Avalonia.Win32 { @@ -24,13 +25,8 @@ namespace Avalonia.Win32 var egl = EglPlatformOpenGlInterface.TryCreate(() => new AngleWin32EglDisplay()); if (egl is { } && - opts?.UseWindowsUIComposition == true) - { - var compositionConnector = CompositionConnector.TryCreate(egl); - - if (compositionConnector != null) - AvaloniaLocator.CurrentMutable.BindToSelf(compositionConnector); - } + opts?.UseWindowsUIComposition == true) + WinUICompositorConnection.TryCreateAndRegister(egl); return egl; } diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/D2DEffects.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/D2DEffects.cs new file mode 100644 index 0000000000..bef5a55b06 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/D2DEffects.cs @@ -0,0 +1,130 @@ +using System; + +namespace Avalonia.Win32.WinRT.Composition +{ + class D2DEffects + { + public static readonly Guid CLSID_D2D12DAffineTransform = + new Guid(0x6AA97485, 0x6354, 0x4CFC, 0x90, 0x8C, 0xE4, 0xA7, 0x4F, 0x62, 0xC9, 0x6C); + + public static readonly Guid CLSID_D2D13DPerspectiveTransform = + new Guid(0xC2844D0B, 0x3D86, 0x46E7, 0x85, 0xBA, 0x52, 0x6C, 0x92, 0x40, 0xF3, 0xFB); + + public static readonly Guid CLSID_D2D13DTransform = + new Guid(0xE8467B04, 0xEC61, 0x4B8A, 0xB5, 0xDE, 0xD4, 0xD7, 0x3D, 0xEB, 0xEA, 0x5A); + + public static readonly Guid CLSID_D2D1ArithmeticComposite = + new Guid(0xFC151437, 0x049A, 0x4784, 0xA2, 0x4A, 0xF1, 0xC4, 0xDA, 0xF2, 0x09, 0x87); + + public static readonly Guid CLSID_D2D1Atlas = + new Guid(0x913E2BE4, 0xFDCF, 0x4FE2, 0xA5, 0xF0, 0x24, 0x54, 0xF1, 0x4F, 0xF4, 0x08); + + public static readonly Guid CLSID_D2D1BitmapSource = + new Guid(0x5FB6C24D, 0xC6DD, 0x4231, 0x94, 0x4, 0x50, 0xF4, 0xD5, 0xC3, 0x25, 0x2D); + + public static readonly Guid CLSID_D2D1Blend = + new Guid(0x81C5B77B, 0x13F8, 0x4CDD, 0xAD, 0x20, 0xC8, 0x90, 0x54, 0x7A, 0xC6, 0x5D); + + public static readonly Guid CLSID_D2D1Border = + new Guid(0x2A2D49C0, 0x4ACF, 0x43C7, 0x8C, 0x6A, 0x7C, 0x4A, 0x27, 0x87, 0x4D, 0x27); + + public static readonly Guid CLSID_D2D1Brightness = + new Guid(0x8CEA8D1E, 0x77B0, 0x4986, 0xB3, 0xB9, 0x2F, 0x0C, 0x0E, 0xAE, 0x78, 0x87); + + public static readonly Guid CLSID_D2D1ColorManagement = + new Guid(0x1A28524C, 0xFDD6, 0x4AA4, 0xAE, 0x8F, 0x83, 0x7E, 0xB8, 0x26, 0x7B, 0x37); + + public static readonly Guid CLSID_D2D1ColorMatrix = + new Guid(0x921F03D6, 0x641C, 0x47DF, 0x85, 0x2D, 0xB4, 0xBB, 0x61, 0x53, 0xAE, 0x11); + + public static readonly Guid CLSID_D2D1Composite = + new Guid(0x48FC9F51, 0xF6AC, 0x48F1, 0x8B, 0x58, 0x3B, 0x28, 0xAC, 0x46, 0xF7, 0x6D); + + public static readonly Guid CLSID_D2D1ConvolveMatrix = + new Guid(0x407F8C08, 0x5533, 0x4331, 0xA3, 0x41, 0x23, 0xCC, 0x38, 0x77, 0x84, 0x3E); + + public static readonly Guid CLSID_D2D1Crop = + new Guid(0xE23F7110, 0x0E9A, 0x4324, 0xAF, 0x47, 0x6A, 0x2C, 0x0C, 0x46, 0xF3, 0x5B); + + public static readonly Guid CLSID_D2D1DirectionalBlur = + new Guid(0x174319A6, 0x58E9, 0x49B2, 0xBB, 0x63, 0xCA, 0xF2, 0xC8, 0x11, 0xA3, 0xDB); + + public static readonly Guid CLSID_D2D1DiscreteTransfer = + new Guid(0x90866FCD, 0x488E, 0x454B, 0xAF, 0x06, 0xE5, 0x04, 0x1B, 0x66, 0xC3, 0x6C); + + public static readonly Guid CLSID_D2D1DisplacementMap = + new Guid(0xEDC48364, 0x417, 0x4111, 0x94, 0x50, 0x43, 0x84, 0x5F, 0xA9, 0xF8, 0x90); + + public static readonly Guid CLSID_D2D1DistantDiffuse = + new Guid(0x3E7EFD62, 0xA32D, 0x46D4, 0xA8, 0x3C, 0x52, 0x78, 0x88, 0x9A, 0xC9, 0x54); + + public static readonly Guid CLSID_D2D1DistantSpecular = + new Guid(0x428C1EE5, 0x77B8, 0x4450, 0x8A, 0xB5, 0x72, 0x21, 0x9C, 0x21, 0xAB, 0xDA); + + public static readonly Guid CLSID_D2D1DpiCompensation = + new Guid(0x6C26C5C7, 0x34E0, 0x46FC, 0x9C, 0xFD, 0xE5, 0x82, 0x37, 0x6, 0xE2, 0x28); + + public static readonly Guid CLSID_D2D1Flood = + new Guid(0x61C23C20, 0xAE69, 0x4D8E, 0x94, 0xCF, 0x50, 0x07, 0x8D, 0xF6, 0x38, 0xF2); + + public static readonly Guid CLSID_D2D1GammaTransfer = + new Guid(0x409444C4, 0xC419, 0x41A0, 0xB0, 0xC1, 0x8C, 0xD0, 0xC0, 0xA1, 0x8E, 0x42); + + public static readonly Guid CLSID_D2D1GaussianBlur = + new Guid(0x1FEB6D69, 0x2FE6, 0x4AC9, 0x8C, 0x58, 0x1D, 0x7F, 0x93, 0xE7, 0xA6, 0xA5); + + public static readonly Guid CLSID_D2D1Scale = + new Guid(0x9DAF9369, 0x3846, 0x4D0E, 0xA4, 0x4E, 0xC, 0x60, 0x79, 0x34, 0xA5, 0xD7); + + public static readonly Guid CLSID_D2D1Histogram = + new Guid(0x881DB7D0, 0xF7EE, 0x4D4D, 0xA6, 0xD2, 0x46, 0x97, 0xAC, 0xC6, 0x6E, 0xE8); + + public static readonly Guid CLSID_D2D1HueRotation = + new Guid(0x0F4458EC, 0x4B32, 0x491B, 0x9E, 0x85, 0xBD, 0x73, 0xF4, 0x4D, 0x3E, 0xB6); + + public static readonly Guid CLSID_D2D1LinearTransfer = + new Guid(0xAD47C8FD, 0x63EF, 0x4ACC, 0x9B, 0x51, 0x67, 0x97, 0x9C, 0x03, 0x6C, 0x06); + + public static readonly Guid CLSID_D2D1LuminanceToAlpha = + new Guid(0x41251AB7, 0x0BEB, 0x46F8, 0x9D, 0xA7, 0x59, 0xE9, 0x3F, 0xCC, 0xE5, 0xDE); + + public static readonly Guid CLSID_D2D1Morphology = + new Guid(0xEAE6C40D, 0x626A, 0x4C2D, 0xBF, 0xCB, 0x39, 0x10, 0x01, 0xAB, 0xE2, 0x02); + + public static readonly Guid CLSID_D2D1OpacityMetadata = + new Guid(0x6C53006A, 0x4450, 0x4199, 0xAA, 0x5B, 0xAD, 0x16, 0x56, 0xFE, 0xCE, 0x5E); + + public static readonly Guid CLSID_D2D1PointDiffuse = + new Guid(0xB9E303C3, 0xC08C, 0x4F91, 0x8B, 0x7B, 0x38, 0x65, 0x6B, 0xC4, 0x8C, 0x20); + + public static readonly Guid CLSID_D2D1PointSpecular = + new Guid(0x09C3CA26, 0x3AE2, 0x4F09, 0x9E, 0xBC, 0xED, 0x38, 0x65, 0xD5, 0x3F, 0x22); + + public static readonly Guid CLSID_D2D1Premultiply = + new Guid(0x06EAB419, 0xDEED, 0x4018, 0x80, 0xD2, 0x3E, 0x1D, 0x47, 0x1A, 0xDE, 0xB2); + + public static readonly Guid CLSID_D2D1Saturation = + new Guid(0x5CB2D9CF, 0x327D, 0x459F, 0xA0, 0xCE, 0x40, 0xC0, 0xB2, 0x08, 0x6B, 0xF7); + + public static readonly Guid CLSID_D2D1Shadow = + new Guid(0xC67EA361, 0x1863, 0x4E69, 0x89, 0xDB, 0x69, 0x5D, 0x3E, 0x9A, 0x5B, 0x6B); + + public static readonly Guid CLSID_D2D1SpotDiffuse = + new Guid(0x818A1105, 0x7932, 0x44F4, 0xAA, 0x86, 0x08, 0xAE, 0x7B, 0x2F, 0x2C, 0x93); + + public static readonly Guid CLSID_D2D1SpotSpecular = + new Guid(0xEDAE421E, 0x7654, 0x4A37, 0x9D, 0xB8, 0x71, 0xAC, 0xC1, 0xBE, 0xB3, 0xC1); + + public static readonly Guid CLSID_D2D1TableTransfer = + new Guid(0x5BF818C3, 0x5E43, 0x48CB, 0xB6, 0x31, 0x86, 0x83, 0x96, 0xD6, 0xA1, 0xD4); + + public static readonly Guid CLSID_D2D1Tile = + new Guid(0xB0784138, 0x3B76, 0x4BC5, 0xB1, 0x3B, 0x0F, 0xA2, 0xAD, 0x02, 0x65, 0x9F); + + public static readonly Guid CLSID_D2D1Turbulence = + new Guid(0xCF2BB6AE, 0x889A, 0x4AD7, 0xBA, 0x29, 0xA2, 0xFD, 0x73, 0x2C, 0x9F, 0xC9); + + public static readonly Guid CLSID_D2D1UnPremultiply = + new Guid(0xFB9AC489, 0xAD8D, 0x41ED, 0x99, 0x99, 0xBB, 0x63, 0x47, 0xD1, 0x10, 0xF7); + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositedWindow.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositedWindow.cs new file mode 100644 index 0000000000..4ae9c08410 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositedWindow.cs @@ -0,0 +1,99 @@ +using System; +using System.Numerics; +using System.Reactive.Disposables; +using System.Threading; +using Avalonia.MicroCom; +using Avalonia.OpenGL; +using Avalonia.OpenGL.Egl; +using Avalonia.Win32.Interop; + +namespace Avalonia.Win32.WinRT.Composition +{ + public class WinUICompositedWindow : IDisposable + { + private EglContext _syncContext; + private readonly object _pumpLock; + private readonly IVisual _blurVisual; + private ICompositionTarget _compositionTarget; + private IVisual _contentVisual; + private ICompositionDrawingSurfaceInterop _surfaceInterop; + private PixelSize _size; + + private static Guid IID_ID3D11Texture2D = Guid.Parse("6f15aaf2-d208-4e89-9ab4-489535d34f9c"); + private ICompositor _compositor; + + + internal WinUICompositedWindow(EglContext syncContext, + ICompositor compositor, + object pumpLock, + ICompositionTarget compositionTarget, + ICompositionDrawingSurfaceInterop surfaceInterop, + IVisual contentVisual, IVisual blurVisual) + { + _compositor = compositor.CloneReference(); + _syncContext = syncContext; + _pumpLock = pumpLock; + _blurVisual = blurVisual.CloneReference(); + _compositionTarget = compositionTarget.CloneReference(); + _contentVisual = contentVisual.CloneReference(); + _surfaceInterop = surfaceInterop.CloneReference(); + } + + + public void ResizeIfNeeded(PixelSize size) + { + using (_syncContext.EnsureLocked()) + { + if (_size != size) + { + _surfaceInterop.Resize(new UnmanagedMethods.POINT { X = size.Width, Y = size.Height }); + _contentVisual.SetSize(new Vector2(size.Width, size.Height)); + _size = size; + } + } + } + + public unsafe IUnknown BeginDrawToTexture(out PixelPoint offset) + { + if (!_syncContext.IsCurrent) + throw new InvalidOperationException(); + + var iid = IID_ID3D11Texture2D; + void* pTexture; + var off = _surfaceInterop.BeginDraw(null, &iid, &pTexture); + offset = new PixelPoint(off.X, off.Y); + return MicroComRuntime.CreateProxyFor(pTexture, true); + } + + public void EndDraw() + { + if (!_syncContext.IsCurrent) + throw new InvalidOperationException(); + _surfaceInterop.EndDraw(); + } + + public void SetBlur(bool enable) + { + using (_syncContext.EnsureLocked()) + _blurVisual.SetIsVisible(enable ? 1 : 0); + } + + public IDisposable BeginTransaction() + { + Monitor.Enter(_pumpLock); + return Disposable.Create(() => Monitor.Exit(_pumpLock)); + } + + public void Dispose() + { + if (_syncContext == null) + { + _compositor.Dispose(); + _blurVisual.Dispose(); + _contentVisual.Dispose(); + _surfaceInterop.Dispose(); + _compositionTarget.Dispose(); + } + } + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositorConnection.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositorConnection.cs new file mode 100644 index 0000000000..2aa82436f6 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUICompositorConnection.cs @@ -0,0 +1,238 @@ +using System; +using System.Diagnostics; +using System.Numerics; +using System.Runtime.InteropServices; +using System.Threading; +using System.Threading.Tasks; +using Avalonia.Logging; +using Avalonia.MicroCom; +using Avalonia.OpenGL; +using Avalonia.OpenGL.Angle; +using Avalonia.OpenGL.Egl; +using Avalonia.Rendering; +using Avalonia.Win32.Interop; + +namespace Avalonia.Win32.WinRT.Composition +{ + class WinUICompositorConnection : IRenderTimer + { + private readonly EglContext _syncContext; + private IntPtr _queue; + private ICompositor _compositor; + private ICompositor2 _compositor2; + private ICompositor5 _compositor5; + private ICompositorInterop _compositorInterop; + private AngleWin32EglDisplay _angle; + private ICompositionGraphicsDevice _device; + private EglPlatformOpenGlInterface _gl; + private ICompositorDesktopInterop _compositorDesktopInterop; + private ICompositionBrush _blurBrush; + private object _pumpLock = new object(); + + public WinUICompositorConnection(EglPlatformOpenGlInterface gl, object pumpLock) + { + _gl = gl; + _pumpLock = pumpLock; + _syncContext = _gl.PrimaryEglContext; + _angle = (AngleWin32EglDisplay)_gl.Display; + _compositor = NativeWinRTMethods.CreateInstance("Windows.UI.Composition.Compositor"); + _compositor2 = _compositor.QueryInterface(); + _compositor5 = _compositor.QueryInterface(); + _compositorInterop = _compositor.QueryInterface(); + _compositorDesktopInterop = _compositor.QueryInterface(); + using var device = MicroComRuntime.CreateProxyFor(_angle.GetDirect3DDevice(), true); + + _device = _compositorInterop.CreateGraphicsDevice(device); + _blurBrush = CreateBlurBrush(); + + } + + public EglPlatformOpenGlInterface Egl => _gl; + + static bool TryCreateAndRegisterCore(EglPlatformOpenGlInterface angle) + { + var tcs = new TaskCompletionSource(); + var pumpLock = new object(); + var th = new Thread(() => + { + WinUICompositorConnection connect; + try + { + NativeWinRTMethods.CreateDispatcherQueueController(new NativeWinRTMethods.DispatcherQueueOptions + { + apartmentType = NativeWinRTMethods.DISPATCHERQUEUE_THREAD_APARTMENTTYPE.DQTAT_COM_NONE, + dwSize = Marshal.SizeOf(), + threadType = NativeWinRTMethods.DISPATCHERQUEUE_THREAD_TYPE.DQTYPE_THREAD_CURRENT + }); + connect = new WinUICompositorConnection(angle, pumpLock); + AvaloniaLocator.CurrentMutable.BindToSelf(connect); + AvaloniaLocator.CurrentMutable.Bind().ToConstant(connect); + tcs.SetResult(true); + + } + catch (Exception e) + { + tcs.SetException(e); + return; + } + connect.RunLoop(); + }) + { + IsBackground = true + }; + th.SetApartmentState(ApartmentState.STA); + th.Start(); + return tcs.Task.Result; + } + + class RunLoopHandler : IAsyncActionCompletedHandler, IMicroComShadowContainer + { + private readonly WinUICompositorConnection _parent; + private Stopwatch _st = Stopwatch.StartNew(); + + public RunLoopHandler(WinUICompositorConnection parent) + { + _parent = parent; + } + public void Dispose() + { + + } + + public void Invoke(IAsyncAction asyncInfo, AsyncStatus asyncStatus) + { + _parent.Tick?.Invoke(_st.Elapsed); + using var act = _parent._compositor5.RequestCommitAsync(); + act.SetCompleted(this); + } + + public MicroComShadow Shadow { get; set; } + public void OnReferencedFromNative() + { + } + + public void OnUnreferencedFromNative() + { + } + } + + private void RunLoop() + { + { + var st = Stopwatch.StartNew(); + using (var act = _compositor5.RequestCommitAsync()) + act.SetCompleted(new RunLoopHandler(this)); + while (true) + { + UnmanagedMethods.GetMessage(out var msg, IntPtr.Zero, 0, 0); + lock (_pumpLock) + UnmanagedMethods.DispatchMessage(ref msg); + } + } + } + + public static void TryCreateAndRegister(EglPlatformOpenGlInterface angle) + { + const int majorRequired = 10; + const int buildRequired = 17134; + + var majorInstalled = Win32Platform.WindowsVersion.Major; + var buildInstalled = Win32Platform.WindowsVersion.Build; + + if (majorInstalled >= majorRequired && + buildInstalled >= buildRequired) + { + try + { + TryCreateAndRegisterCore(angle); + } + catch (Exception e) + { + Logger.TryGet(LogEventLevel.Error, "WinUIComposition") + ?.Log(null, "Unable to initialize WinUI compositor: {0}", e); + + } + } + + var osVersionNotice = + $"Windows {majorRequired} Build {buildRequired} is required. Your machine has Windows {majorInstalled} Build {buildInstalled} installed."; + + Logger.TryGet(LogEventLevel.Warning, "WinUIComposition")?.Log(null, + $"Unable to initialize WinUI compositor: {osVersionNotice}"); + } + + + public WinUICompositedWindow CreateWindow(IntPtr hWnd) + { + using var sc = _syncContext.EnsureLocked(); + using var desktopTarget = _compositorDesktopInterop.CreateDesktopWindowTarget(hWnd, 0); + using var target = desktopTarget.QueryInterface(); + + using var drawingSurface = _device.CreateDrawingSurface(new UnmanagedMethods.SIZE(), DirectXPixelFormat.B8G8R8A8UIntNormalized, + DirectXAlphaMode.Premultiplied); + using var surface = drawingSurface.QueryInterface(); + using var surfaceInterop = drawingSurface.QueryInterface(); + + using var surfaceBrush = _compositor.CreateSurfaceBrushWithSurface(surface); + using var brush = surfaceBrush.QueryInterface(); + + using var spriteVisual = _compositor.CreateSpriteVisual(); + spriteVisual.SetBrush(brush); + using var visual = spriteVisual.QueryInterface(); + using var visual2 = spriteVisual.QueryInterface(); + using var container = _compositor.CreateContainerVisual(); + using var containerVisual = container.QueryInterface(); + using var containerVisual2 = container.QueryInterface(); + containerVisual2.SetRelativeSizeAdjustment(new Vector2(1, 1)); + using var containerChildren = container.Children; + + target.SetRoot(containerVisual); + + using var blur = CreateBlurVisual(); + + containerChildren.InsertAtTop(blur); + containerChildren.InsertAtTop(visual); + + return new WinUICompositedWindow(_syncContext, _compositor, _pumpLock, target, surfaceInterop, visual, blur); + } + + + private unsafe ICompositionBrush CreateBlurBrush() + { + using var backDropParameterFactory = NativeWinRTMethods.CreateActivationFactory( + "Windows.UI.Composition.CompositionEffectSourceParameter"); + using var backdropString = new HStringInterop("backdrop"); + using var backDropParameter = + backDropParameterFactory.Create(backdropString.Handle); + using var backDropParameterAsSource = backDropParameter.QueryInterface(); + var blurEffect = new WinUIGaussianBlurEffect(backDropParameterAsSource); + using var blurEffectFactory = _compositor.CreateEffectFactory(blurEffect); + using var backdrop = _compositor2.CreateBackdropBrush(); + using var backdropBrush = backdrop.QueryInterface(); + + + var saturateEffect = new SaturationEffect(blurEffect); + using var satEffectFactory = _compositor.CreateEffectFactory(saturateEffect); + using var sat = satEffectFactory.CreateBrush(); + sat.SetSourceParameter(backdropString.Handle, backdropBrush); + return sat.QueryInterface(); + } + + private unsafe IVisual CreateBlurVisual() + { + using var spriteVisual = _compositor.CreateSpriteVisual(); + using var visual = spriteVisual.QueryInterface(); + using var visual2 = spriteVisual.QueryInterface(); + + + spriteVisual.SetBrush(_blurBrush); + visual.SetIsVisible(0); + visual2.SetRelativeSizeAdjustment(new Vector2(1.0f, 1.0f)); + + return visual.CloneReference(); + } + + + public event Action Tick; + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUIEffectBase.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUIEffectBase.cs new file mode 100644 index 0000000000..ea75a2f311 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUIEffectBase.cs @@ -0,0 +1,135 @@ +using System; +using System.Linq; +using System.Runtime.InteropServices; +using Avalonia.MicroCom; + +namespace Avalonia.Win32.WinRT.Composition +{ + abstract class WinUIEffectBase : WinRTInspectable, IGraphicsEffect, IGraphicsEffectSource, IGraphicsEffectD2D1Interop + { + private IGraphicsEffectSource[] _sources; + + public WinUIEffectBase(params IGraphicsEffectSource[] _sources) + { + this._sources = _sources.Select(e => + { + if (e is WinUIEffectBase) + return e; + return e.CloneReference(); + }).ToArray(); + } + + public IntPtr Name => IntPtr.Zero; + + public void SetName(IntPtr name) + { + + } + + public abstract Guid EffectId { get; } + public unsafe void GetNamedPropertyMapping(IntPtr name, uint* index, GRAPHICS_EFFECT_PROPERTY_MAPPING* mapping) => + throw new COMException("Not supported", unchecked((int)0x80004001)); + + public abstract uint PropertyCount { get; } + public abstract IPropertyValue GetProperty(uint index); + + public IGraphicsEffectSource GetSource(uint index) + { + if (_sources == null || index> _sources.Length) + throw new COMException("Invalid index", unchecked((int)0x80070057)); + return _sources[index]; + } + + public uint SourceCount => (uint)(_sources?.Length ?? 0); + + public override void OnUnreferencedFromNative() + { + if (_sources == null) + return; + + /*foreach(var s in _sources) + s.Dispose();*/ + _sources = null; + } + } + + class WinUIGaussianBlurEffect : WinUIEffectBase + { + public WinUIGaussianBlurEffect(IGraphicsEffectSource source) : base(source) + { + } + + enum D2D1_GAUSSIANBLUR_OPTIMIZATION + { + D2D1_GAUSSIANBLUR_OPTIMIZATION_SPEED, + D2D1_GAUSSIANBLUR_OPTIMIZATION_BALANCED, + D2D1_GAUSSIANBLUR_OPTIMIZATION_QUALITY, + D2D1_GAUSSIANBLUR_OPTIMIZATION_FORCE_DWORD + }; + + enum D2D1_BORDER_MODE + { + D2D1_BORDER_MODE_SOFT, + D2D1_BORDER_MODE_HARD, + D2D1_BORDER_MODE_FORCE_DWORD + }; + + enum D2D1GaussianBlurProp + { + D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION, + D2D1_GAUSSIANBLUR_PROP_OPTIMIZATION, + D2D1_GAUSSIANBLUR_PROP_BORDER_MODE, + D2D1_GAUSSIANBLUR_PROP_FORCE_DWORD + }; + + public override Guid EffectId => D2DEffects.CLSID_D2D1GaussianBlur; + + public override uint PropertyCount => 3; + + public override IPropertyValue GetProperty(uint index) + { + switch ((D2D1GaussianBlurProp)index) + { + case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_STANDARD_DEVIATION: + return new WinRTPropertyValue(30.0f); + + case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_OPTIMIZATION: + return new WinRTPropertyValue((uint)D2D1_GAUSSIANBLUR_OPTIMIZATION + .D2D1_GAUSSIANBLUR_OPTIMIZATION_BALANCED); + + case D2D1GaussianBlurProp.D2D1_GAUSSIANBLUR_PROP_BORDER_MODE: + return new WinRTPropertyValue((uint)D2D1_BORDER_MODE.D2D1_BORDER_MODE_HARD); + } + + return null; + } + } + + class SaturationEffect : WinUIEffectBase + { + public SaturationEffect(IGraphicsEffectSource source) : base(source) + { + } + + enum D2D1_SATURATION_PROP + { + D2D1_SATURATION_PROP_SATURATION, + D2D1_SATURATION_PROP_FORCE_DWORD + }; + + public override Guid EffectId => D2DEffects.CLSID_D2D1Saturation; + + public override uint PropertyCount => 1; + + public override IPropertyValue GetProperty(uint index) + { + switch ((D2D1_SATURATION_PROP)index) + { + case D2D1_SATURATION_PROP.D2D1_SATURATION_PROP_SATURATION: + return new WinRTPropertyValue(2.0f); + } + + return null; + } + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs new file mode 100644 index 0000000000..f59d50860a --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/Composition/WinUiCompositedWindowSurface.cs @@ -0,0 +1,118 @@ +using System; +using System.Runtime.InteropServices; +using Avalonia.MicroCom; +using Avalonia.OpenGL.Angle; +using Avalonia.OpenGL.Egl; +using Avalonia.OpenGL.Surfaces; +using Avalonia.Utilities; +using Avalonia.Win32.Interop; + +namespace Avalonia.Win32.WinRT.Composition +{ + internal class WinUiCompositedWindowSurface : EglGlPlatformSurfaceBase, IBlurHost, IDisposable + { + private readonly WinUICompositorConnection _connection; + private EglPlatformOpenGlInterface _egl; + private readonly EglGlPlatformSurfaceBase.IEglWindowGlPlatformSurfaceInfo _info; + private IRef _window; + private bool _enableBlur; + + public WinUiCompositedWindowSurface(WinUICompositorConnection connection, IEglWindowGlPlatformSurfaceInfo info) : base() + { + _connection = connection; + _egl = connection.Egl; + _info = info; + } + + public override IGlPlatformSurfaceRenderTarget CreateGlRenderTarget() + { + using (_egl.PrimaryContext.EnsureCurrent()) + { + if (_window?.Item == null) + { + _window = RefCountable.Create(_connection.CreateWindow(_info.Handle)); + _window.Item.SetBlur(_enableBlur); + } + + return new CompositionRenderTarget(_egl, _window, _info); + } + } + + class CompositionRenderTarget : EglPlatformSurfaceRenderTargetBase + { + private readonly EglPlatformOpenGlInterface _egl; + private readonly IRef _window; + private readonly IEglWindowGlPlatformSurfaceInfo _info; + + public CompositionRenderTarget(EglPlatformOpenGlInterface egl, + IRef window, + IEglWindowGlPlatformSurfaceInfo info) + : base(egl) + { + _egl = egl; + _window = window.Clone(); + _info = info; + _window.Item.ResizeIfNeeded(_info.Size); + } + + public override IGlPlatformSurfaceRenderingSession BeginDraw() + { + var contextLock = _egl.PrimaryEglContext.EnsureCurrent(); + IUnknown texture = null; + EglSurface surface = null; + IDisposable transaction = null; + var success = false; + try + { + if (_window?.Item == null) + throw new ObjectDisposedException(GetType().FullName); + + var size = _info.Size; + transaction = _window.Item.BeginTransaction(); + _window.Item.ResizeIfNeeded(size); + texture = _window.Item.BeginDrawToTexture(out var offset); + + surface = ((AngleWin32EglDisplay) _egl.Display).WrapDirect3D11Texture(_egl, + texture.GetNativeIntPtr(), + offset.X, offset.Y, size.Width, size.Height); + + var res = base.BeginDraw(surface, _info, () => + { + surface?.Dispose(); + texture?.Dispose(); + _window.Item.EndDraw(); + transaction?.Dispose(); + contextLock?.Dispose(); + }, true); + success = true; + return res; + } + finally + { + if (!success) + { + surface?.Dispose(); + texture?.Dispose(); + transaction?.Dispose(); + contextLock.Dispose(); + } + } + } + } + + public void SetBlur(bool enable) + { + _enableBlur = enable; + _window?.Item?.SetBlur(enable); + } + + public void Dispose() + { + using (_egl.PrimaryEglContext.EnsureLocked()) + { + _window?.Dispose(); + _window = null; + } + } + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/IBlurHost.cs b/src/Windows/Avalonia.Win32/WinRT/IBlurHost.cs new file mode 100644 index 0000000000..81c0e3e185 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/IBlurHost.cs @@ -0,0 +1,7 @@ +namespace Avalonia.Win32.WinRT +{ + public interface IBlurHost + { + void SetBlur(bool enable); + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/NativeWinRTMethods.cs b/src/Windows/Avalonia.Win32/WinRT/NativeWinRTMethods.cs new file mode 100644 index 0000000000..087bd2fd43 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/NativeWinRTMethods.cs @@ -0,0 +1,140 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.IO; +using System.Reflection; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.WindowsRuntime; +using System.Threading; +using Avalonia.MicroCom; +using Avalonia.Win32.Interop; + +namespace Avalonia.Win32.WinRT +{ + internal static class NativeWinRTMethods + { + [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", CallingConvention = CallingConvention.StdCall, + PreserveSig = false)] + internal static extern unsafe IntPtr WindowsCreateString( + [MarshalAs(UnmanagedType.LPWStr)] string sourceString, + int length); + + internal static IntPtr WindowsCreateString(string sourceString) + => WindowsCreateString(sourceString, sourceString.Length); + + [DllImport("api-ms-win-core-winrt-string-l1-1-0.dll", + CallingConvention = CallingConvention.StdCall, PreserveSig = false)] + internal static extern unsafe IntPtr WindowsDeleteString(IntPtr hString); + + [DllImport("Windows.UI.Composition", EntryPoint = "DllGetActivationFactory", + CallingConvention = CallingConvention.StdCall, PreserveSig = false)] + private extern static IntPtr GetWindowsUICompositionActivationFactory( + IntPtr activatableClassId); + + internal static IActivationFactory GetWindowsUICompositionActivationFactory(string className) + {//"Windows.UI.Composition.Compositor" + var s = WindowsCreateString(className); + var factory = GetWindowsUICompositionActivationFactory(s); + return MicroComRuntime.CreateProxyFor(factory, true); + } + + [UnmanagedFunctionPointer(CallingConvention.StdCall)] + delegate int GetActivationFactoryDelegate(IntPtr classId, out IntPtr ppv); + + internal static T CreateInstance(string fullName) where T : IUnknown + { + var s = WindowsCreateString(fullName); + EnsureRoInitialized(); + var pUnk = RoActivateInstance(s); + using var unk = MicroComRuntime.CreateProxyFor(pUnk, true); + WindowsDeleteString(s); + return MicroComRuntime.QueryInterface(unk); + } + + internal static TFactory CreateActivationFactory(string fullName) where TFactory : IUnknown + { + var s = WindowsCreateString(fullName); + EnsureRoInitialized(); + var guid = MicroComRuntime.GetGuidFor(typeof(TFactory)); + var pUnk = RoGetActivationFactory(s, ref guid); + using var unk = MicroComRuntime.CreateProxyFor(pUnk, true); + WindowsDeleteString(s); + return MicroComRuntime.QueryInterface(unk); + } + + internal enum DISPATCHERQUEUE_THREAD_APARTMENTTYPE + { + DQTAT_COM_NONE = 0, + DQTAT_COM_ASTA = 1, + DQTAT_COM_STA = 2 + }; + + internal enum DISPATCHERQUEUE_THREAD_TYPE + { + DQTYPE_THREAD_DEDICATED = 1, + DQTYPE_THREAD_CURRENT = 2, + }; + + [StructLayout(LayoutKind.Sequential)] + internal struct DispatcherQueueOptions + { + public int dwSize; + + [MarshalAs(UnmanagedType.I4)] + public DISPATCHERQUEUE_THREAD_TYPE threadType; + + [MarshalAs(UnmanagedType.I4)] + public DISPATCHERQUEUE_THREAD_APARTMENTTYPE apartmentType; + }; + + [DllImport("coremessaging.dll", PreserveSig = false)] + internal static extern IntPtr CreateDispatcherQueueController(DispatcherQueueOptions options); + + internal enum RO_INIT_TYPE + { + RO_INIT_SINGLETHREADED = 0, // Single-threaded application + RO_INIT_MULTITHREADED = 1, // COM calls objects on any thread. + } + + [DllImport("combase.dll", PreserveSig = false)] + private static extern void RoInitialize(RO_INIT_TYPE initType); + + [DllImport("combase.dll", PreserveSig = false)] + private static extern IntPtr RoActivateInstance(IntPtr activatableClassId); + + [DllImport("combase.dll", PreserveSig = false)] + private static extern IntPtr RoGetActivationFactory(IntPtr activatableClassId, ref Guid iid); + + private static bool _initialized; + private static void EnsureRoInitialized() + { + if (_initialized) + return; + RoInitialize(Thread.CurrentThread.GetApartmentState() == ApartmentState.STA ? + RO_INIT_TYPE.RO_INIT_SINGLETHREADED : + RO_INIT_TYPE.RO_INIT_MULTITHREADED); + _initialized = true; + } + } + + class HStringInterop : IDisposable + { + private IntPtr _s; + + public HStringInterop(string s) + { + _s = s == null ? IntPtr.Zero : NativeWinRTMethods.WindowsCreateString(s); + } + + public IntPtr Handle => _s; + + public void Dispose() + { + if (_s != IntPtr.Zero) + { + NativeWinRTMethods.WindowsDeleteString(_s); + _s = IntPtr.Zero; + } + } + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/WinRTColor.cs b/src/Windows/Avalonia.Win32/WinRT/WinRTColor.cs new file mode 100644 index 0000000000..786d698daa --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/WinRTColor.cs @@ -0,0 +1,18 @@ +using System.Runtime.InteropServices; + +namespace Avalonia.Win32.WinRT +{ + [StructLayout(LayoutKind.Sequential, Pack = 1)] + public struct WinRTColor + { + public byte A; + public byte R; + public byte G; + public byte B; + + public static WinRTColor FromArgb(byte a, byte r, byte g, byte b) => new WinRTColor() + { + A = a, R = r, G = g, B = b + }; + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/WinRTInspectable.cs b/src/Windows/Avalonia.Win32/WinRT/WinRTInspectable.cs new file mode 100644 index 0000000000..d2ec957b8e --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/WinRTInspectable.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using Avalonia.MicroCom; + +namespace Avalonia.Win32.WinRT +{ + class WinRTInspectable : IInspectable, IMicroComShadowContainer + { + public virtual void Dispose() + { + + } + + public unsafe void GetIids(ulong* iidCount, Guid** iids) + { + var interfaces = GetType().GetInterfaces().Where(typeof(IUnknown).IsAssignableFrom) + .Select(MicroComRuntime.GetGuidFor).ToArray(); + var mem = (Guid*)Marshal.AllocCoTaskMem(Unsafe.SizeOf() * interfaces.Length); + for (var c = 0; c < interfaces.Length; c++) + mem[c] = interfaces[c]; + *iids = mem; + *iidCount = (ulong) interfaces.Length; + } + + public IntPtr RuntimeClassName => NativeWinRTMethods.WindowsCreateString(GetType().FullName); + public TrustLevel TrustLevel => TrustLevel.BaseTrust; + public MicroComShadow Shadow { get; set; } + public virtual void OnReferencedFromNative() + { + } + + public virtual void OnUnreferencedFromNative() + { + } + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/WinRTPropertyValue.cs b/src/Windows/Avalonia.Win32/WinRT/WinRTPropertyValue.cs new file mode 100644 index 0000000000..684e7ff7b5 --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/WinRTPropertyValue.cs @@ -0,0 +1,89 @@ +using System; +using System.Runtime.InteropServices; + +namespace Avalonia.Win32.WinRT +{ + class WinRTPropertyValue : WinRTInspectable, IPropertyValue + { + public WinRTPropertyValue(float f) + { + Type = PropertyType.Single; + Single = f; + } + + public WinRTPropertyValue(uint u) + { + UInt32 = u; + Type = PropertyType.UInt32; + } + + public PropertyType Type { get; } + public int IsNumericScalar { get; } + public byte UInt8 { get; } + public short Int16 { get; } + public ushort UInt16 { get; } + public int Int32 { get; } + public uint UInt32 { get; } + public long Int64 { get; } + public ulong UInt64 { get; } + public float Single { get; } + public double Double { get; } + public char Char16 { get; } + public int Boolean { get; } + public IntPtr String { get; } + public Guid Guid { get; } + + private static COMException NotImplemented => new COMException("Not supported", unchecked((int)0x80004001)); + + public unsafe void GetDateTime(void* value) => throw NotImplemented; + + public unsafe void GetTimeSpan(void* value) => throw NotImplemented; + + public unsafe void GetPoint(void* value) => throw NotImplemented; + + public unsafe void GetSize(void* value) => throw NotImplemented; + + public unsafe void GetRect(void* value) => throw NotImplemented; + + public unsafe byte* GetUInt8Array(uint* __valueSize) => throw NotImplemented; + + public unsafe short* GetInt16Array(uint* __valueSize) => throw NotImplemented; + + public unsafe ushort* GetUInt16Array(uint* __valueSize) => throw NotImplemented; + + public unsafe int* GetInt32Array(uint* __valueSize) + { + throw NotImplemented; + } + + public unsafe uint* GetUInt32Array(uint* __valueSize) => throw NotImplemented; + + public unsafe long* GetInt64Array(uint* __valueSize) => throw NotImplemented; + + public unsafe ulong* GetUInt64Array(uint* __valueSize) => throw NotImplemented; + + public unsafe float* GetSingleArray(uint* __valueSize) => throw NotImplemented; + + public unsafe double* GetDoubleArray(uint* __valueSize) => throw NotImplemented; + + public unsafe char* GetChar16Array(uint* __valueSize) => throw NotImplemented; + + public unsafe int* GetBooleanArray(uint* __valueSize) => throw NotImplemented; + + public unsafe IntPtr* GetStringArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void** GetInspectableArray(uint* __valueSize) => throw NotImplemented; + + public unsafe Guid* GetGuidArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void* GetDateTimeArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void* GetTimeSpanArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void* GetPointArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void* GetSizeArray(uint* __valueSize) => throw NotImplemented; + + public unsafe void* GetRectArray(uint* __valueSize) => throw NotImplemented; + } +} diff --git a/src/Windows/Avalonia.Win32/WinRT/winrt.idl b/src/Windows/Avalonia.Win32/WinRT/winrt.idl new file mode 100644 index 0000000000..929377999c --- /dev/null +++ b/src/Windows/Avalonia.Win32/WinRT/winrt.idl @@ -0,0 +1,718 @@ +@clr-namespace Avalonia.Win32.WinRT +@clr-access internal +@clr-map FLOAT float +@clr-map HSTRING IntPtr +@clr-map Vector2 System.Numerics.Vector2 +@clr-map Vector3 System.Numerics.Vector3 +@clr-map Quaternion System.Numerics.Quaternion +@clr-map Matrix4x4 System.Numerics.Matrix4x4 +@clr-map RECT Avalonia.Win32.Interop.UnmanagedMethods.RECT +@clr-map SIZE Avalonia.Win32.Interop.UnmanagedMethods.SIZE +@clr-map POINT Avalonia.Win32.Interop.UnmanagedMethods.POINT +@clr-map HWND IntPtr +@clr-map BOOL int +@clr-map DWORD int +@clr-map boolean int +@clr-map BYTE byte +@clr-map INT16 short +@clr-map INT32 int +@clr-map INT64 long +@clr-map UINT16 ushort +@clr-map UINT32 uint +@clr-map UINT64 ulong +@clr-map DOUBLE double +@clr-map GUID System.Guid +@clr-map WCHAR System.Char +@clr-map Color Avalonia.Win32.WinRT.WinRTColor + +enum TrustLevel +{ + BaseTrust, + PartialTrust, + FullTrust +} + +enum DirectXAlphaMode +{ + Unspecified, + Premultiplied, + Straight, + Ignore +} + +enum DirectXPixelFormat +{ + Unknown = 0, + R32G32B32A32Typeless = 1, + R32G32B32A32Float = 2, + R32G32B32A32UInt = 3, + R32G32B32A32Int = 4, + R32G32B32Typeless = 5, + R32G32B32Float = 6, + R32G32B32UInt = 7, + R32G32B32Int = 8, + R16G16B16A16Typeless = 9, + R16G16B16A16Float = 10, + R16G16B16A16UIntNormalized = 11, + R16G16B16A16UInt = 12, + R16G16B16A16IntNormalized = 13, + R16G16B16A16Int = 14, + R32G32Typeless = 15, + R32G32Float = 16, + R32G32UInt = 17, + R32G32Int = 18, + R32G8X24Typeless = 19, + D32FloatS8X24UInt = 20, + R32FloatX8X24Typeless = 21, + X32TypelessG8X24UInt = 22, + R10G10B10A2Typeless = 23, + R10G10B10A2UIntNormalized = 24, + R10G10B10A2UInt = 25, + R11G11B10Float = 26, + R8G8B8A8Typeless = 27, + R8G8B8A8UIntNormalized = 28, + R8G8B8A8UIntNormalizedSrgb = 29, + R8G8B8A8UInt = 30, + R8G8B8A8IntNormalized = 31, + R8G8B8A8Int = 32, + R16G16Typeless = 33, + R16G16Float = 34, + R16G16UIntNormalized = 35, + R16G16UInt = 36, + R16G16IntNormalized = 37, + R16G16Int = 38, + R32Typeless = 39, + D32Float = 40, + R32Float = 41, + R32UInt = 42, + R32Int = 43, + R24G8Typeless = 44, + D24UIntNormalizedS8UInt = 45, + R24UIntNormalizedX8Typeless = 46, + X24TypelessG8UInt = 47, + R8G8Typeless = 48, + R8G8UIntNormalized = 49, + R8G8UInt = 50, + R8G8IntNormalized = 51, + R8G8Int = 52, + R16Typeless = 53, + R16Float = 54, + D16UIntNormalized = 55, + R16UIntNormalized = 56, + R16UInt = 57, + R16IntNormalized = 58, + R16Int = 59, + R8Typeless = 60, + R8UIntNormalized = 61, + R8UInt = 62, + R8IntNormalized = 63, + R8Int = 64, + A8UIntNormalized = 65, + R1UIntNormalized = 66, + R9G9B9E5SharedExponent = 67, + R8G8B8G8UIntNormalized = 68, + G8R8G8B8UIntNormalized = 69, + BC1Typeless = 70, + BC1UIntNormalized = 71, + BC1UIntNormalizedSrgb = 72, + BC2Typeless = 73, + BC2UIntNormalized = 74, + BC2UIntNormalizedSrgb = 75, + BC3Typeless = 76, + BC3UIntNormalized = 77, + BC3UIntNormalizedSrgb = 78, + BC4Typeless = 79, + BC4UIntNormalized = 80, + BC4IntNormalized = 81, + BC5Typeless = 82, + BC5UIntNormalized = 83, + BC5IntNormalized = 84, + B5G6R5UIntNormalized = 85, + B5G5R5A1UIntNormalized = 86, + B8G8R8A8UIntNormalized = 87, + B8G8R8X8UIntNormalized = 88, + R10G10B10XRBiasA2UIntNormalized = 89, + B8G8R8A8Typeless = 90, + B8G8R8A8UIntNormalizedSrgb = 91, + B8G8R8X8Typeless = 92, + B8G8R8X8UIntNormalizedSrgb = 93, + BC6HTypeless = 94, + BC6H16UnsignedFloat = 95, + BC6H16Float = 96, + BC7Typeless = 97, + BC7UIntNormalized = 98, + BC7UIntNormalizedSrgb = 99, + Ayuv = 100, + Y410 = 101, + Y416 = 102, + NV12 = 103, + P010 = 104, + P016 = 105, + Opaque420 = 106, + Yuy2 = 107, + Y210 = 108, + Y216 = 109, + NV11 = 110, + AI44 = 111, + IA44 = 112, + P8 = 113, + A8P8 = 114, + B4G4R4A4UIntNormalized = 115, + P208 = 130, + V208 = 131, + V408 = 132, +} + +[uuid(AF86E2E0-B12D-4c6a-9C5A-D7AA65101E90)] +interface IInspectable : IUnknown +{ + HRESULT GetIids(ulong * iidCount, Guid ** iids); + HRESULT GetRuntimeClassName( [out] IntPtr* className); + HRESULT GetTrustLevel([out] TrustLevel * trustLevel); +} + +enum PropertyType +{ + Empty = 0, + UInt8 = 1, + Int16 = 2, + UInt16 = 3, + Int32 = 4, + UInt32 = 5, + Int64 = 6, + UInt64 = 7, + Single = 8, + Double = 9, + Char16 = 10, + Boolean = 11, + String = 12, + Inspectable = 13, + DateTime = 14, + TimeSpan = 15, + Guid = 16, + Point = 17, + Size = 18, + Rect = 19, + OtherType = 20, + UInt8Array = 1025, + Int16Array = 1026, + UInt16Array = 1027, + Int32Array = 1028, + UInt32Array = 1029, + Int64Array = 1030, + UInt64Array = 1031, + SingleArray = 1032, + DoubleArray = 1033, + Char16Array = 1034, + BooleanArray = 1035, + StringArray = 1036, + InspectableArray = 1037, + DateTimeArray = 1038, + TimeSpanArray = 1039, + GuidArray = 1040, + PointArray = 1041, + SizeArray = 1042, + RectArray = 1043, + OtherTypeArray = 1044 +} + +[uuid(4BD682DD-7554-40E9-9A9B-82654EDE7E62)] +interface IPropertyValue : IInspectable +{ + [propget] HRESULT Type([out] [retval] PropertyType* value); + [propget] HRESULT IsNumericScalar([out] [retval] boolean* value); + HRESULT GetUInt8([out] [retval] BYTE* value); + HRESULT GetInt16([out] [retval] INT16* value); + HRESULT GetUInt16([out] [retval] UINT16* value); + HRESULT GetInt32([out] [retval] INT32* value); + HRESULT GetUInt32([out] [retval] UINT32* value); + HRESULT GetInt64([out] [retval] INT64* value); + HRESULT GetUInt64([out] [retval] UINT64* value); + HRESULT GetSingle([out] [retval] FLOAT* value); + HRESULT GetDouble([out] [retval] DOUBLE* value); + HRESULT GetChar16([out] [retval] WCHAR* value); + HRESULT GetBoolean([out] [retval] boolean* value); + HRESULT GetString([out] [retval] HSTRING* value); + HRESULT GetGuid([out] [retval] GUID* value); + HRESULT GetDateTime( void* value); + HRESULT GetTimeSpan(void* value); + HRESULT GetPoint(void* value); + HRESULT GetSize(void* value); + HRESULT GetRect(void* value); + HRESULT GetUInt8Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] BYTE** value); + HRESULT GetInt16Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] INT16** value); + HRESULT GetUInt16Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] UINT16** value); + HRESULT GetInt32Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] INT32** value); + HRESULT GetUInt32Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] UINT32** value); + HRESULT GetInt64Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] INT64** value); + HRESULT GetUInt64Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] UINT64** value); + HRESULT GetSingleArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] FLOAT** value); + HRESULT GetDoubleArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] DOUBLE** value); + HRESULT GetChar16Array([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] WCHAR** value); + HRESULT GetBooleanArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] boolean** value); + HRESULT GetStringArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] HSTRING** value); + HRESULT GetInspectableArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void*** value); + HRESULT GetGuidArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] GUID** value); + HRESULT GetDateTimeArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void** value); + HRESULT GetTimeSpanArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void** value); + HRESULT GetPointArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void** value); + HRESULT GetSizeArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void** value); + HRESULT GetRectArray([out] UINT32* __valueSize, [out] [size_is(, *__valueSize)] void** value); +} + +enum AsyncStatus { + Started = 0, + Completed, + Canceled, + Error, +} + +[uuid(A4ED5C81-76C9-40BD-8BE6-B1D90FB20AE7)] +interface IAsyncActionCompletedHandler : IUnknown +{ + HRESULT Invoke([in] IAsyncAction* asyncInfo, [in] AsyncStatus asyncStatus); +} + +[uuid(5A648006-843A-4DA9-865B-9D26E5DFAD7B)] +interface IAsyncAction : IInspectable +{ + HRESULT SetCompleted([in] IAsyncActionCompletedHandler* handler); + HRESULT GetCompleted([out] [retval] IAsyncActionCompletedHandler** ppv); + HRESULT GetResults(); +} + +[uuid(603E88E4-A338-4FFE-A457-A5CFB9CEB899)] +interface IDispatcherQueue : IInspectable +{ + //TODO +} + +[uuid(22F34E66-50DB-4E36-A98D-61C01B384D20)] +interface IDispatcherQueueController : IInspectable +{ + [propget] HRESULT DispatcherQueue([out] [retval] IDispatcherQueue** value); + HRESULT ShutdownQueueAsync([out] [retval] IAsyncAction** operation); +} + +[uuid(00000035-0000-0000-C000-000000000046)] +interface IActivationFactory : IInspectable +{ + HRESULT ActivateInstance([out, retval] IntPtr* instance); +} + +[flags] +enum CompositionBatchTypes +{ + None = 0x0, + Animation = 0x1, + Effect = 0x2, + InfiniteAnimation = 0x4, + AllAnimations = 0x5 +} + +[uuid(B403CA50-7F8C-4E83-985F-CC45060036D8)] +interface ICompositor : IInspectable +{ + HRESULT CreateColorKeyFrameAnimation([out] [retval] void** result); + [overload("CreateColorBrush")] HRESULT CreateColorBrush([out] [retval]void** result); + [overload("CreateColorBrush")] HRESULT CreateColorBrushWithColor([in] Color* color, [out] [retval] ICompositionColorBrush** result); + HRESULT CreateContainerVisual([out] [retval] IContainerVisual** result); + HRESULT CreateCubicBezierEasingFunction([in] Vector2 controlPoint1, [in] Vector2 controlPoint2, [out] [retval] void** result); + [overload("CreateEffectFactory")] HRESULT CreateEffectFactory([in] IGraphicsEffect* graphicsEffect, [out] [retval] ICompositionEffectFactory** result); + [overload("CreateEffectFactory")] HRESULT CreateEffectFactoryWithProperties([in] void* graphicsEffect, [in] void* animatableProperties, [out] [retval] void** result); + [overload("CreateExpressionAnimation")] HRESULT CreateExpressionAnimation([out] [retval] void** result); + [overload("CreateExpressionAnimation")] HRESULT CreateExpressionAnimationWithExpression([in] HSTRING expression, [out] [retval] void** result); + [overload("CreateInsetClip")] HRESULT CreateInsetClip([out] [retval] void** result); + [overload("CreateInsetClip")] HRESULT CreateInsetClipWithInsets([in] FLOAT leftInset, [in] FLOAT topInset, [in] FLOAT rightInset, [in] FLOAT bottomInset, [out] [retval] void** result); + HRESULT CreateLinearEasingFunction([out] [retval] void** result); + HRESULT CreatePropertySet([out] [retval] void** result); + HRESULT CreateQuaternionKeyFrameAnimation([out] [retval] void** result); + HRESULT CreateScalarKeyFrameAnimation([out] [retval] void** result); + HRESULT CreateScopedBatch([in] CompositionBatchTypes batchType, [out] [retval] ICompositionScopedBatch** result); + HRESULT CreateSpriteVisual([out] [retval] ISpriteVisual** result); + HRESULT CreateSurfaceBrush([out] [retval] ICompositionSurfaceBrush** result); + HRESULT CreateSurfaceBrushWithSurface([in] ICompositionSurface* surface, + [out] [retval] ICompositionSurfaceBrush** result); + HRESULT CreateTargetForCurrentView([out] [retval] void** result); + HRESULT CreateVector2KeyFrameAnimation([out] [retval] void** result); + HRESULT CreateVector3KeyFrameAnimation([out] [retval] void** result); + HRESULT CreateVector4KeyFrameAnimation([out] [retval] void** result); + HRESULT GetCommitBatch([in] CompositionBatchTypes batchType, [out] [retval] void** result); +} + +[uuid(735081DC-5E24-45DA-A38F-E32CC349A9A0)] +interface ICompositor2 : IInspectable +{ + HRESULT CreateAmbientLight([out] [retval] void** result); + HRESULT CreateAnimationGroup([out] [retval] void** result); + HRESULT CreateBackdropBrush([out] [retval] ICompositionBackdropBrush** result); + HRESULT CreateDistantLight([out] [retval] void** result); + HRESULT CreateDropShadow([out] [retval] void** result); + HRESULT CreateImplicitAnimationCollection([out] [retval] void** result); + HRESULT CreateLayerVisual([out] [retval] void** result); + HRESULT CreateMaskBrush([out] [retval] void** result); + HRESULT CreateNineGridBrush([out] [retval] void** result); + HRESULT CreatePointLight([out] [retval] void** result); + HRESULT CreateSpotLight([out] [retval] void** result); + [overload("CreateStepEasingFunction")] HRESULT CreateStepEasingFunction([out] [retval] void** result); + [overload("CreateStepEasingFunction")] HRESULT CreateStepEasingFunctionWithStepCount([in] INT32 stepCount, [out] [retval] void** result); +} + +[uuid(08E05581-1AD1-4F97-9757-402D76E4233B)] +interface ISpriteVisual : IInspectable +{ + [propget] HRESULT GetBrush([out] [retval] ICompositionBrush** value); + [propput] HRESULT SetBrush([in] ICompositionBrush* value); +} + +[uuid(FD04E6E3-FE0C-4C3C-AB19-A07601A576EE)] +interface ICompositionDrawingSurfaceInterop : IUnknown +{ + HRESULT BeginDraw(RECT* updateRect, Guid* iid, void** updateObject, [out, retval]POINT* updateOffset); + HRESULT EndDraw(); + HRESULT Resize(POINT sizePixels); + HRESULT Scroll(RECT * scrollRect, RECT * clipRect, int offsetX, int offsetY); + HRESULT ResumeDraw(); + HRESULT SuspendDraw(); +}; + +[uuid(A116FF71-F8BF-4C8A-9C98-70779A32A9C8)] +interface ICompositionGraphicsDeviceInterop : IUnknown +{ + HRESULT GetRenderingDevice([out] IUnknown ** value); + HRESULT SetRenderingDevice([out] IUnknown * value); +}; + +[uuid(25297D5C-3AD4-4C9C-B5CF-E36A38512330)] +interface ICompositorInterop : IUnknown +{ + HRESULT CreateCompositionSurfaceForHandle(IntPtr swapChain, [out] ICompositionSurface ** res); + HRESULT CreateCompositionSurfaceForSwapChain(IUnknown* swapChain, [out] ICompositionSurface ** result); + HRESULT CreateGraphicsDevice(IUnknown * renderingDevice, [out] ICompositionGraphicsDevice ** result); +}; + +[uuid(26f496a0-7f38-45fb-88f7-faaabe67dd59)] +interface ISwapChainInterop : IUnknown +{ + HRESULT SetSwapChain(IUnknown * swapChain); +}; + +/* +[uuid(2C9DB356-E70D-4642-8298-BC4AA5B4865C)] +interface ICompositionCapabilitiesInteropFactory : IInspectable +{ + HRESULT GetForWindow(IntPtr hwnd, [out] ICompositionCapabilities ** result); +}*/ + +[uuid(29E691FA-4567-4DCA-B319-D0F207EB6807)] +interface ICompositorDesktopInterop : IUnknown +{ + HRESULT CreateDesktopWindowTarget(HWND hwndTarget, BOOL isTopmost, [out] IDesktopWindowTarget ** result); + HRESULT EnsureOnThread(DWORD threadId); +}; + + +[uuid(35DBF59E-E3F9-45B0-81E7-FE75F4145DC9)] +interface IDesktopWindowTargetInterop : IUnknown +{ + HRESULT GetHWnd([out] IntPtr* value); +}; + +[uuid(37642806-F421-4FD0-9F82-23AE7C776182)] +interface IDesktopWindowContentBridgeInterop : IUnknown +{ + HRESULT Initialize(ICompositor* compositor, HWND parentHwnd); + HRESULT GetHWnd([out] IntPtr* value); + + HRESULT GetAppliedScaleFactor([out] float* value); +}; + +[uuid(FB22C6E1-80A2-4667-9936-DBEAF6EEFE95)] +interface ICompositionGraphicsDevice : IInspectable +{ + HRESULT CreateDrawingSurface([in] SIZE sizePixels, [in] DirectXPixelFormat pixelFormat, + [in] DirectXAlphaMode alphaMode, [out] [retval] ICompositionDrawingSurface** result); + HRESULT AddRenderingDeviceReplaced(void* handler, void* token); + HRESULT RemoveRenderingDeviceReplaced([in] int token); +} + +[uuid(1527540D-42C7-47A6-A408-668F79A90DFB)] +interface ICompositionSurface : IInspectable +{ +} + +[uuid(6329D6CA-3366-490E-9DB3-25312929AC51)] +interface IDesktopWindowTarget : IInspectable +{ + [propget] HRESULT IsTopmost([out] [retval] int* value); +} + + +[uuid(A166C300-FAD0-4D11-9E67-E433162FF49E)] +interface ICompositionDrawingSurface : IInspectable +{ + [propget] HRESULT GetAlphaMode([out] [retval] DirectXAlphaMode* value); + [propget] HRESULT GetPixelFormat([out] [retval] DirectXPixelFormat* value); + [propget] HRESULT GetSize([out] [retval] POINT* value); +} + +enum CompositionBitmapInterpolationMode +{ + NearestNeighbor, + Linear, + MagLinearMinLinearMipLinear, + MagLinearMinLinearMipNearest, + MagLinearMinNearestMipLinear, + MagLinearMinNearestMipNearest, + MagNearestMinLinearMipLinear, + MagNearestMinLinearMipNearest, + MagNearestMinNearestMipLinear, + MagNearestMinNearestMipNearest, +} + +enum CompositionStretch +{ + None, + Fill, + Uniform, + UniformToFill, +} + +[uuid(AD016D79-1E4C-4C0D-9C29-83338C87C162)] +interface ICompositionSurfaceBrush : IInspectable +{ + [propget] HRESULT BitmapInterpolationMode([out] [retval] CompositionBitmapInterpolationMode* value); + [propput] HRESULT BitmapInterpolationMode([in] CompositionBitmapInterpolationMode value); + [propget] HRESULT HorizontalAlignmentRatio([out] [retval] FLOAT* value); + [propput] HRESULT HorizontalAlignmentRatio([in] FLOAT value); + [propget] HRESULT Stretch([out] [retval] CompositionStretch* value); + [propput] HRESULT Stretch([in] CompositionStretch value); + [propget] HRESULT Surface([out] [retval] ICompositionSurface** value); + [propput] HRESULT Surface([in] ICompositionSurface* value); + [propget] HRESULT VerticalAlignmentRatio([out] [retval] FLOAT* value); + [propput] HRESULT VerticalAlignmentRatio([in] FLOAT value); +} + +[uuid(AB0D7608-30C0-40E9-B568-B60A6BD1FB46)] +interface ICompositionBrush : IInspectable +{ +} + +enum CompositionBackfaceVisibility +{ + Inherit, + Visible, + Hidden +} + +enum CompositionBorderMode +{ + Inherit, + Soft, + Hard +} + +enum CompositionCompositeMode +{ + Inherit, + SourceOver, + DestinationInvert, + MinBlend, +} + +[uuid(117E202D-A859-4C89-873B-C2AA566788E3)] +interface IVisual : IInspectable +{ + [propget] HRESULT AnchorPoint([out] [retval] Vector2* value); + [propput] HRESULT AnchorPoint([in] Vector2 value); + [propget] HRESULT BackfaceVisibility([out] [retval] CompositionBackfaceVisibility* value); + [propput] HRESULT BackfaceVisibility([in] CompositionBackfaceVisibility value); + [propget] HRESULT BorderMode([out] [retval] CompositionBorderMode* value); + [propput] HRESULT BorderMode([in] CompositionBorderMode value); + [propget] HRESULT CenterPoint([out] [retval] Vector3* value); + [propput] HRESULT CenterPoint([in] Vector3 value); + [propget] HRESULT Clip([out] [retval]void** value); + [propput] HRESULT Clip([in] void* value); + [propget] HRESULT CompositeMode([out] [retval] CompositionCompositeMode* value); + [propput] HRESULT CompositeMode([in] CompositionCompositeMode value); + [propget] HRESULT IsVisible([out] [retval] boolean* value); + [propput] HRESULT IsVisible([in] boolean value); + [propget] HRESULT Offset([out] [retval] Vector3* value); + [propput] HRESULT Offset([in] Vector3 value); + [propget] HRESULT Opacity([out] [retval] FLOAT* value); + [propput] HRESULT Opacity([in] FLOAT value); + [propget] HRESULT Orientation([out] [retval] Quaternion* value); + [propput] HRESULT Orientation([in] Quaternion value); + [propget] HRESULT Parent([out] [retval] IContainerVisual** value); + [propget] HRESULT RotationAngle([out] [retval] FLOAT* value); + [propput] HRESULT RotationAngle([in] FLOAT value); + [propget] HRESULT RotationAngleInDegrees([out] [retval] FLOAT* value); + [propput] HRESULT RotationAngleInDegrees([in] FLOAT value); + [propget] HRESULT RotationAxis([out] [retval] Vector3* value); + [propput] HRESULT RotationAxis([in] Vector3 value); + [propget] HRESULT Scale([out] [retval] Vector3* value); + [propput] HRESULT Scale([in] Vector3 value); + [propget] HRESULT Size([out] [retval] Vector2* value); + [propput] HRESULT Size([in] Vector2 value); + [propget] HRESULT TransformMatrix([out] [retval] Matrix4x4* value); + [propput] HRESULT TransformMatrix([in] Matrix4x4 value); +} + +[uuid(3052B611-56C3-4C3E-8BF3-F6E1AD473F06)] +interface IVisual2 : IInspectable +{ + [propget] HRESULT ParentForTransform([out] [retval] IVisual** value); + [propput] HRESULT ParentForTransform([in] IVisual* value); + [propget] HRESULT RelativeOffsetAdjustment([out] [retval] Vector3* value); + [propput] HRESULT RelativeOffsetAdjustment([in] Vector3 value); + [propget] HRESULT RelativeSizeAdjustment([out] [retval] Vector2* value); + [propput] HRESULT RelativeSizeAdjustment([in] Vector2 value); +} + +[uuid(02F6BC74-ED20-4773-AFE6-D49B4A93DB32)] +interface IContainerVisual : IInspectable +{ + [propget] HRESULT GetChildren([out] [retval] IVisualCollection** value); +} + +[uuid(8B745505-FD3E-4A98-84A8-E949468C6BCB)] +interface IVisualCollection : IInspectable +{ + [propget] HRESULT GetCount([out] [retval] INT32* value); + HRESULT InsertAbove([in] IVisual* newChild, [in] IVisual* sibling); + HRESULT InsertAtBottom([in] IVisual* newChild); + HRESULT InsertAtTop([in] IVisual* newChild); + HRESULT InsertBelow([in] IVisual* newChild, [in] IVisual* sibling); + HRESULT Remove([in] IVisual* child); + HRESULT RemoveAll(); +} + +[uuid(A1BEA8BA-D726-4663-8129-6B5E7927FFA6)] +interface ICompositionTarget : IInspectable +{ + [propget] HRESULT Root([out] [retval] IVisual** value); + [propput] HRESULT Root([in] IVisual* value); +} + + +[uuid(CB51C0CE-8FE6-4636-B202-861FAA07D8F3)] +interface IGraphicsEffect : IInspectable +{ + [propget] HRESULT Name([out] [retval] HSTRING* name); + [propput] HRESULT Name([in] HSTRING name); +} + +[uuid(2D8F9DDC-4339-4EB9-9216-F9DEB75658A2)] +interface IGraphicsEffectSource : IInspectable +{ +} + +enum GRAPHICS_EFFECT_PROPERTY_MAPPING +{ + GRAPHICS_EFFECT_PROPERTY_MAPPING_UNKNOWN, + GRAPHICS_EFFECT_PROPERTY_MAPPING_DIRECT, + GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORX, + GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORY, + GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORZ, + GRAPHICS_EFFECT_PROPERTY_MAPPING_VECTORW, + GRAPHICS_EFFECT_PROPERTY_MAPPING_RECT_TO_VECTOR4, + GRAPHICS_EFFECT_PROPERTY_MAPPING_RADIANS_TO_DEGREES, + GRAPHICS_EFFECT_PROPERTY_MAPPING_COLORMATRIX_ALPHA_MODE, + GRAPHICS_EFFECT_PROPERTY_MAPPING_COLOR_TO_VECTOR3, + GRAPHICS_EFFECT_PROPERTY_MAPPING_COLOR_TO_VECTOR4 +} + +[uuid(2FC57384-A068-44D7-A331-30982FCF7177)] +interface IGraphicsEffectD2D1Interop : IUnknown +{ + HRESULT GetEffectId([out] Guid* id); + HRESULT GetNamedPropertyMapping(IntPtr name, uint* index, GRAPHICS_EFFECT_PROPERTY_MAPPING* mapping); + HRESULT GetPropertyCount([out] uint* count); + HRESULT GetProperty(uint index, [out]IPropertyValue** value); + HRESULT GetSource(uint index, [out, retval] IGraphicsEffectSource** source); + HRESULT GetSourceCount([retval]uint *count); +}; + +[uuid(858AB13A-3292-4E4E-B3BB-2B6C6544A6EE)] +interface ICompositionEffectSourceParameter : IInspectable +{ + [propget] HRESULT Name([out] [retval] HSTRING* value); +} + +[uuid(B3D9F276-ABA3-4724-ACF3-D0397464DB1C)] +interface ICompositionEffectSourceParameterFactory : IInspectable +{ + HRESULT Create([in] HSTRING name, [out] [retval] ICompositionEffectSourceParameter** instance); +} + +enum CompositionEffectFactoryLoadStatus +{ + Success = 0, + EffectTooComplex = 1, + Pending = 2, + Other = -1 +} + +[uuid(BE5624AF-BA7E-4510-9850-41C0B4FF74DF)] +interface ICompositionEffectFactory : IInspectable +{ + HRESULT CreateBrush([out] [retval] ICompositionEffectBrush** result); + [propget] HRESULT ExtendedError([out] [retval] int* value); + [propget] HRESULT LoadStatus([out] [retval] CompositionEffectFactoryLoadStatus* value); +} + +[uuid(BF7F795E-83CC-44BF-A447-3E3C071789EC)] +interface ICompositionEffectBrush : IInspectable +{ + HRESULT GetSourceParameter([in] HSTRING name, [out] [retval] ICompositionBrush** result); + HRESULT SetSourceParameter([in] HSTRING name, [in] ICompositionBrush* source); +} + +[uuid(C5ACAE58-3898-499E-8D7F-224E91286A5D)] +interface ICompositionBackdropBrush : IInspectable +{ +} + +[uuid(2B264C5E-BF35-4831-8642-CF70C20FFF2F)] +interface ICompositionColorBrush : IInspectable +{ + [propget] HRESULT Color([out] [retval] Color* value); + [propput] HRESULT Color([in] Color value); +} + +[uuid(0D00DAD0-FB07-46FD-8C72-6280D1A3D1DD)] +interface ICompositionScopedBatch : IInspectable +{ + [propget] HRESULT IsActive([out] [retval] boolean* value); + [propget] HRESULT IsEnded([out] [retval] boolean* value); + HRESULT End(); + HRESULT Resume(); + HRESULT Suspend(); + [eventadd] HRESULT AddCompleted([in] void* handler, [out] [retval] int* token); + [eventremove] HRESULT RemoveCompleted([in] int token); +} + +[uuid(48EA31AD-7FCD-4076-A79C-90CC4B852C9B)] +interface ICompositor5 : IInspectable +{ + [propget] HRESULT GetComment([out] [retval] HSTRING* value); + [propput] HRESULT SetComment([in] HSTRING value); + [propget] HRESULT GetGlobalPlaybackRate([out] [retval] FLOAT* value); + [propput] HRESULT SetGlobalPlaybackRate([in] FLOAT value); + HRESULT CreateBounceScalarAnimation([out] [retval] void** result); + HRESULT CreateBounceVector2Animation([out] [retval] void** result); + HRESULT CreateBounceVector3Animation([out] [retval] void** result); + HRESULT CreateContainerShape([out] [retval] void** result); + HRESULT CreateEllipseGeometry([out] [retval] void** result); + HRESULT CreateLineGeometry([out] [retval] void** result); + [overload("CreatePathGeometry")] HRESULT CreatePathGeometry([out] [retval] void** result); + [overload("CreatePathGeometry")] HRESULT CreatePathGeometryWithPath([in] void* path, [out] [retval] void** result); + HRESULT CreatePathKeyFrameAnimation([out] [retval] void** result); + HRESULT CreateRectangleGeometry([out] [retval] void** result); + HRESULT CreateRoundedRectangleGeometry([out] [retval] void** result); + HRESULT CreateShapeVisual([out] [retval] void** result); + [overload("CreateSpriteShape")] HRESULT CreateSpriteShape([out] [retval] void** result); + [overload("CreateSpriteShape")] HRESULT CreateSpriteShapeWithGeometry([in] void* geometry, [out] [retval] void** result); + HRESULT CreateViewBox([out] [retval] void** result); + HRESULT RequestCommitAsync([out] [retval] IAsyncAction** operation); +} diff --git a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs index d770f4b211..78de681403 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs @@ -65,23 +65,10 @@ namespace Avalonia.Win32 return IntPtr.Zero; } - // Based on https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4270-L4337 - // We need to enable parent window before destroying child window to prevent OS from activating a random window behind us. - // This is described here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow#remarks - // Our window closed callback will set enabled state to a correct value after child window gets destroyed. - // We need to verify if parent is still alive (perhaps it got destroyed somehow). - if (_parent != null && IsWindow(_parent._hwnd)) - { - var wasActive = GetActiveWindow() == _hwnd; - - _parent.SetEnabled(true); + BeforeCloseCleanup(false); - // We also need to activate our parent window since again OS might try to activate a window behind if it is not set. - if (wasActive) - { - SetActiveWindow(_parent._hwnd); - } - } + // Used to distinguish between programmatic and regular close requests. + _isCloseRequested = true; break; } diff --git a/src/Windows/Avalonia.Win32/WindowImpl.cs b/src/Windows/Avalonia.Win32/WindowImpl.cs index f594763413..3c09d2b7a0 100644 --- a/src/Windows/Avalonia.Win32/WindowImpl.cs +++ b/src/Windows/Avalonia.Win32/WindowImpl.cs @@ -15,6 +15,8 @@ using Avalonia.Rendering; using Avalonia.Win32.Input; using Avalonia.Win32.Interop; using Avalonia.Win32.OpenGl; +using Avalonia.Win32.WinRT; +using Avalonia.Win32.WinRT.Composition; using static Avalonia.Win32.Interop.UnmanagedMethods; namespace Avalonia.Win32 @@ -81,6 +83,7 @@ namespace Avalonia.Win32 private POINT _maxTrackSize; private WindowImpl _parent; private ExtendClientAreaChromeHints _extendChromeHints = ExtendClientAreaChromeHints.Default; + private bool _isCloseRequested; public WindowImpl() { @@ -107,7 +110,7 @@ namespace Avalonia.Win32 var glPlatform = AvaloniaLocator.Current.GetService(); - var compositionConnector = AvaloniaLocator.Current.GetService(); + var compositionConnector = AvaloniaLocator.Current.GetService(); _isUsingComposition = compositionConnector is { } && glPlatform is EglPlatformOpenGlInterface egl && @@ -121,8 +124,8 @@ namespace Avalonia.Win32 { if (_isUsingComposition) { - var cgl = new CompositionEglGlPlatformSurface(glPlatform as EglPlatformOpenGlInterface, this); - _blurHost = cgl.AttachToCompositionTree(compositionConnector, _hwnd); + var cgl = new WinUiCompositedWindowSurface(compositionConnector, this); + _blurHost = cgl; _gl = cgl; @@ -452,9 +455,14 @@ namespace Avalonia.Win32 if (customRendererFactory != null) return customRendererFactory.Create(root, loop); - return Win32Platform.UseDeferredRendering ? - (IRenderer)new DeferredRenderer(root, loop, rendererLock: _rendererLock) : - new ImmediateRenderer(root); + return Win32Platform.UseDeferredRendering + ? _isUsingComposition + ? new DeferredRenderer(root, loop) + { + RenderOnlyOnRenderThread = true + } + : (IRenderer)new DeferredRenderer(root, loop, rendererLock: _rendererLock) + : new ImmediateRenderer(root); } public void Resize(Size value) @@ -489,6 +497,8 @@ namespace Avalonia.Win32 public void Dispose() { + (_gl as IDisposable)?.Dispose(); + if (_dropTarget != null) { OleContext.Current?.UnregisterDragDrop(Handle); @@ -497,6 +507,13 @@ namespace Avalonia.Win32 if (_hwnd != IntPtr.Zero) { + // Detect if we are being closed programmatically - this would mean that WM_CLOSE was not called + // and we didn't prepare this window for destruction. + if (!_isCloseRequested) + { + BeforeCloseCleanup(true); + } + DestroyWindow(_hwnd); _hwnd = IntPtr.Zero; } @@ -941,6 +958,32 @@ namespace Avalonia.Win32 SetFocus(_hwnd); } } + + private void BeforeCloseCleanup(bool isDisposing) + { + // Based on https://github.com/dotnet/wpf/blob/master/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L4270-L4337 + // We need to enable parent window before destroying child window to prevent OS from activating a random window behind us (or last active window). + // This is described here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-enablewindow#remarks + // We need to verify if parent is still alive (perhaps it got destroyed somehow). + if (_parent != null && IsWindow(_parent._hwnd)) + { + var wasActive = GetActiveWindow() == _hwnd; + + // We can only set enabled state if we are not disposing - generally Dispose happens after enabled state has been set. + // Ignoring this would cause us to enable a window that might be disabled. + if (!isDisposing) + { + // Our window closed callback will set enabled state to a correct value after child window gets destroyed. + _parent.SetEnabled(true); + } + + // We also need to activate our parent window since again OS might try to activate a window behind if it is not set. + if (wasActive) + { + SetActiveWindow(_parent._hwnd); + } + } + } private void MaximizeWithoutCoveringTaskbar() { diff --git a/src/tools/MicroComGenerator/AstParser.cs b/src/tools/MicroComGenerator/AstParser.cs index 732c0496b3..388a8eb018 100644 --- a/src/tools/MicroComGenerator/AstParser.cs +++ b/src/tools/MicroComGenerator/AstParser.cs @@ -13,7 +13,8 @@ namespace MicroComGenerator while (!parser.Eof) { var attrs = ParseLocalAttributes(ref parser); - + if (parser.TryConsume(";")) + continue; if (parser.TryParseKeyword("enum")) idl.Enums.Add(ParseEnum(attrs, ref parser)); else if (parser.TryParseKeyword("struct")) @@ -64,7 +65,7 @@ namespace MicroComGenerator static AstAttributes ParseLocalAttributes(ref TokenParser parser) { var rv = new AstAttributes(); - if (parser.TryConsume("[")) + while (parser.TryConsume("[")) { while (!parser.TryConsume("]") && !parser.Eof) { @@ -78,7 +79,7 @@ namespace MicroComGenerator if (parser.TryConsume(']')) { rv.Add(new AstAttributeNode(ident, null)); - return rv; + break; } // No value, next attribute else if (parser.TryConsume(',')) diff --git a/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs b/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs index 91ece81bd0..adb8faf938 100644 --- a/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs +++ b/src/tools/MicroComGenerator/CSharpGen.InterfaceGen.cs @@ -21,7 +21,7 @@ namespace MicroComGenerator { public string Name; public string NativeType; - + public AstAttributes Attributes { get; set; } public virtual StatementSyntax CreateFixed(StatementSyntax inner) => inner; public virtual void PreMarshal(List body) @@ -161,6 +161,13 @@ namespace MicroComGenerator return type; } + Arg ConvertArg(AstInterfaceMemberArgumentNode node) + { + var arg = ConvertArg(node.Name, node.Type); + arg.Attributes = node.Attributes.Clone(); + return arg; + } + Arg ConvertArg(string name, AstTypeNode type) { type = new AstTypeNode { Name = ConvertNativeType(type.Name), PointerLevel = type.PointerLevel }; @@ -190,12 +197,19 @@ namespace MicroComGenerator List vtblCtor, int num) { // Prepare method information - var args = member.Select(a => ConvertArg(a.Name, a.Type)).ToList(); + if (member.Name == "GetRenderingDevice") + Console.WriteLine(); + var args = member.Select(ConvertArg).ToList(); var returnArg = ConvertArg("__result", member.ReturnType); bool isHresult = member.ReturnType.Name == "HRESULT"; bool isHresultLastArgumentReturn = isHresult && args.Count > 0 - && (args.Last().Name == "ppv" || args.Last().Name == "retOut" || args.Last().Name == "ret") + && (args.Last().Name == "ppv" + || args.Last().Name == "retOut" + || args.Last().Name == "ret" + || args.Last().Attributes.HasAttribute("out") + || args.Last().Attributes.HasAttribute("retval") + ) && ((member.Last().Type.PointerLevel > 0 && !IsInterface(member.Last().Type)) || member.Last().Type.PointerLevel == 2); @@ -334,16 +348,27 @@ namespace MicroComGenerator BlockSyntax backBodyBlock = Block().AddStatements(backPreMarshal.ToArray()).AddStatements(backCallStatement); + var exceptions = new List() + { + CatchClause( + CatchDeclaration(ParseTypeName("System.Exception"), Identifier("__exception__")), null, + Block( + ParseStatement( + "Avalonia.MicroCom.MicroComRuntime.UnhandledException(__target, __exception__);"), + isHresult ? ParseStatement("return unchecked((int)0x80004005u);") + : isVoidReturn ? EmptyStatement() : ParseStatement("return default;") + )) + }; + + if (isHresult) + exceptions.Insert(0, CatchClause( + CatchDeclaration(ParseTypeName("System.Runtime.InteropServices.COMException"), + Identifier("__com_exception__")), + null, Block(ParseStatement("return __com_exception__.ErrorCode;")))); + backBodyBlock = Block( TryStatement( - SingletonList(CatchClause( - CatchDeclaration(ParseTypeName("System.Exception"), Identifier("__exception__")), null, - Block( - ParseStatement( - "Avalonia.MicroCom.MicroComRuntime.UnhandledException(__target, __exception__);"), - isHresult ? ParseStatement("return unchecked((int)0x80004005u);") - : isVoidReturn ? EmptyStatement() : ParseStatement("return default;") - )))) + List(exceptions)) .WithBlock(Block(backBodyBlock)) ); if (isHresult) diff --git a/src/tools/MicroComGenerator/CSharpGen.Utils.cs b/src/tools/MicroComGenerator/CSharpGen.Utils.cs index 3a62220d12..da845b0ecd 100644 --- a/src/tools/MicroComGenerator/CSharpGen.Utils.cs +++ b/src/tools/MicroComGenerator/CSharpGen.Utils.cs @@ -68,10 +68,11 @@ namespace MicroComGenerator bool IsPropertyRewriteCandidate(MethodDeclarationSyntax method) { - if(method.Identifier.Text.Contains("GetScaling")) - Console.WriteLine(); - return (method.Identifier.Text.StartsWith("Get") && - method.ParameterList.Parameters.Count == 0); + + return + method.ReturnType.ToFullString() != "void" + && method.Identifier.Text.StartsWith("Get") + && method.ParameterList.Parameters.Count == 0; } TypeDeclarationSyntax RewriteMethodsToProperties(T decl) where T : TypeDeclarationSyntax diff --git a/src/tools/MicroComGenerator/CSharpGen.cs b/src/tools/MicroComGenerator/CSharpGen.cs index 688036ffc2..ff4c351fd9 100644 --- a/src/tools/MicroComGenerator/CSharpGen.cs +++ b/src/tools/MicroComGenerator/CSharpGen.cs @@ -22,7 +22,11 @@ namespace MicroComGenerator public CSharpGen(AstIdlNode idl) { _idl = idl.Clone(); - new AstRewriter().VisitAst(_idl); + new AstRewriter(_idl.Attributes.Where(a => a.Name == "clr-map") + .Select(x => x.Value.Trim().Split(' ')) + .ToDictionary(x => x[0], x => x[1]) + ).VisitAst(_idl); + _extraUsings = _idl.Attributes.Where(u => u.Name == "clr-using").Select(u => u.Value).ToList(); _namespace = _idl.GetAttribute("clr-namespace"); var visibilityString = _idl.GetAttribute("clr-access"); @@ -37,6 +41,13 @@ namespace MicroComGenerator class AstRewriter : AstVisitor { + private readonly Dictionary _typeMap = new Dictionary(); + + public AstRewriter(Dictionary typeMap) + { + _typeMap = typeMap; + } + void ConvertIntPtr(AstTypeNode type) { if (type.Name == "void" && type.PointerLevel > 0) @@ -60,6 +71,9 @@ namespace MicroComGenerator type.PointerLevel++; type.IsLink = false; } + + if (_typeMap.TryGetValue(type.Name, out var mapped)) + type.Name = mapped; base.VisitType(type); } @@ -80,6 +94,10 @@ namespace MicroComGenerator { if (member.HasAttribute("intptr")) ConvertIntPtr(member.ReturnType); + if (member.HasAttribute("propget") && !member.Name.StartsWith("Get")) + member.Name = "Get" + member.Name; + if (member.HasAttribute("propput") && !member.Name.StartsWith("Set")) + member.Name = "Set" + member.Name; base.VisitInterfaceMember(member); } } @@ -103,7 +121,8 @@ namespace MicroComGenerator NamespaceDeclarationSyntax GenerateEnums(NamespaceDeclarationSyntax ns) { return ns.AddMembers(_idl.Enums.Select(e => - EnumDeclaration(e.Name) + { + var dec = EnumDeclaration(e.Name) .WithModifiers(TokenList(Token(_visibility))) .WithMembers(SeparatedList(e.Select(m => { @@ -111,8 +130,11 @@ namespace MicroComGenerator if (m.Value != null) return member.WithEqualsValue(EqualsValueClause(ParseExpression(m.Value))); return member; - }))) - ).ToArray()); + }))); + if (e.HasAttribute("flags")) + dec = dec.AddAttribute("System.Flags"); + return dec; + }).ToArray()); } NamespaceDeclarationSyntax GenerateStructs(NamespaceDeclarationSyntax ns) diff --git a/src/tools/MicroComGenerator/CppGen.cs b/src/tools/MicroComGenerator/CppGen.cs index 68192ebffe..b053088ca9 100644 --- a/src/tools/MicroComGenerator/CppGen.cs +++ b/src/tools/MicroComGenerator/CppGen.cs @@ -14,7 +14,10 @@ namespace MicroComGenerator name = "unsigned char"; else if(name == "uint") name = "unsigned int"; - return name + new string('*', type.PointerLevel); + + type = type.Clone(); + type.Name = name; + return type.Format(); } public static string GenerateCpp(AstIdlNode idl) diff --git a/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs b/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs index 7170f6d7d4..3493dd0f53 100644 --- a/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs +++ b/tests/Avalonia.Benchmarks/Layout/ControlsBenchmark.cs @@ -17,7 +17,8 @@ namespace Avalonia.Benchmarks.Layout _app = UnitTestApplication.Start( TestServices.StyledWindow.With( renderInterface: new NullRenderingPlatform(), - threadingInterface: new NullThreadingPlatform())); + threadingInterface: new NullThreadingPlatform(), + standardCursorFactory: new NullCursorFactory())); _root = new TestRoot(true, null) { diff --git a/tests/Avalonia.Benchmarks/NullCursorFactory.cs b/tests/Avalonia.Benchmarks/NullCursorFactory.cs new file mode 100644 index 0000000000..012adce0f2 --- /dev/null +++ b/tests/Avalonia.Benchmarks/NullCursorFactory.cs @@ -0,0 +1,14 @@ +using System; +using Avalonia.Input; +using Avalonia.Platform; + +namespace Avalonia.Benchmarks +{ + internal class NullCursorFactory : IStandardCursorFactory + { + public IPlatformHandle GetCursor(StandardCursorType cursorType) + { + return new PlatformHandle(IntPtr.Zero, "null"); + } + } +} diff --git a/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs index f632d85c26..1570205456 100644 --- a/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs +++ b/tests/Avalonia.Benchmarks/NullRenderingPlatform.cs @@ -28,7 +28,7 @@ namespace Avalonia.Benchmarks public IGeometryImpl CreateRectangleGeometry(Rect rect) { - throw new NotImplementedException(); + return new MockStreamGeometryImpl(); } public IStreamGeometryImpl CreateStreamGeometry() diff --git a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs index 783215fb5d..c8a30a42e9 100644 --- a/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs +++ b/tests/Avalonia.Controls.UnitTests/ComboBoxTests.cs @@ -40,6 +40,7 @@ namespace Avalonia.Controls.UnitTests Items = items, SelectedIndex = 0, }; + var root = new TestRoot(target); var rectangle = target.GetValue(ComboBox.SelectionBoxItemProperty) as Rectangle; Assert.NotNull(rectangle); diff --git a/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs b/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs index 45cf28773f..7c48a975ef 100644 --- a/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs +++ b/tests/Avalonia.Markup.UnitTests/Parsers/ExpressionObserverBuilderTests_AttachedProperty.cs @@ -129,7 +129,7 @@ namespace Avalonia.Markup.UnitTests.Parsers { var data = new Class1(); - Assert.Throws(() => ExpressionObserverBuilder.Build(data, "(Owner)", typeResolver: _typeResolver)); + Assert.Throws(() => ExpressionObserverBuilder.Build(data, "(Owner.)", typeResolver: _typeResolver)); } [Fact] diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/BindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/BindingExtensionTests.cs index 9ea2cd643a..20ed22e84f 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/BindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/BindingExtensionTests.cs @@ -84,6 +84,54 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions } } + [Fact] + public void SupportCastToTypeInExpression() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = new TestDataContext + { + StringProperty = "foobar" + }; + + window.DataContext = dataContext; + + Assert.Equal(dataContext.StringProperty, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpression_DifferentTypeEvaluatesToNull() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = "foo"; + + window.DataContext = dataContext; + + Assert.Equal(null, contentControl.Content); + } + } private class FooBar { public object Foo { get; } = null; diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs index 8a82ad048b..1cf9e0877d 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/CompiledBindingExtensionTests.cs @@ -1,11 +1,13 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Globalization; using System.Reactive.Subjects; using System.Text; using System.Threading.Tasks; using Avalonia.Controls; using Avalonia.Controls.Presenters; +using Avalonia.Data.Converters; using Avalonia.Data.Core; using Avalonia.Markup.Data; using Avalonia.UnitTests; @@ -372,7 +374,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions "; - Assert.Throws(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + ThrowsXamlTransformException(() => AvaloniaRuntimeXamlLoader.Load(xaml)); } } @@ -390,7 +392,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions "; - Assert.Throws(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + ThrowsXamlTransformException(() => AvaloniaRuntimeXamlLoader.Load(xaml)); } } @@ -447,7 +449,7 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions "; - Assert.Throws(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + ThrowsXamlTransformException(() => AvaloniaRuntimeXamlLoader.Load(xaml)); } } @@ -597,7 +599,49 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions x:CompileBindings='true'> "; - Assert.Throws(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + ThrowsXamlParseException(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + } + } + + [Fact] + public void SupportParentInPath() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + Assert.Equal("foo", contentControl.Content); + } + } + + [Fact] + public void SupportConverterWithParameter() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var textBlock = window.FindControl("textBlock"); + + window.DataContext = new TestDataContext() { StringProperty = "Foo" }; + + Assert.Equal("Foo+Bar", textBlock.Text); } } @@ -613,26 +657,218 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions x:DataType='local:TestDataContext' x:CompileBindings='notabool'> "; - Assert.Throws(() => AvaloniaRuntimeXamlLoader.Load(xaml)); + ThrowsXamlParseException(() => AvaloniaRuntimeXamlLoader.Load(xaml)); } } - } + [Fact] + public void SupportCastToTypeInExpression() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = new TestDataContext(); + + window.DataContext = dataContext; + + Assert.Equal(dataContext, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpression_DifferentTypeEvaluatesToNull() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = "foo"; + + window.DataContext = dataContext; + + Assert.Equal(null, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpressionWithProperty() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = new TestDataContext + { + StringProperty = "foobar" + }; + + window.DataContext = dataContext; + + Assert.Equal(dataContext.StringProperty, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpressionWithProperty1() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = new TestDataContext + { + StringProperty = "foobar" + }; + + window.DataContext = dataContext; + + Assert.Equal(dataContext.StringProperty, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpressionWithPropertyIndexer() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var data = new TestData() + { + StringProperty = "Foo" + }; + var dataContext = new TestDataContext + { + ObjectsArrayProperty = new object[] { data } + }; + + window.DataContext = dataContext; + + Assert.Equal(data.StringProperty, contentControl.Content); + } + } + + [Fact] + public void SupportCastToTypeInExpressionWithProperty_DifferentTypeEvaluatesToNull() + { + using (UnitTestApplication.Start(TestServices.StyledWindow)) + { + var xaml = @" + + +"; + var window = (Window)AvaloniaRuntimeXamlLoader.Load(xaml); + var contentControl = window.FindControl("contentControl"); + + var dataContext = new TestDataContext + { + StringProperty = "foobar" + }; + + window.DataContext = dataContext; + + Assert.Equal(dataContext.StringProperty, contentControl.Content); + + window.DataContext = "foo"; + + Assert.Equal(null, contentControl.Content); + } + } + + void Throws(string type, Action cb) + { + try + { + cb(); + } + catch (Exception e) when (e.GetType().Name == type) + { + return; + } + + throw new Exception("Expected " + type); + } + + void ThrowsXamlParseException(Action cb) => Throws("XamlParseException", cb); + void ThrowsXamlTransformException(Action cb) => Throws("XamlTransformException", cb); + } + public interface INonIntegerIndexer { - string this[string key] {get; set;} + string this[string key] { get; set; } } public interface INonIntegerIndexerDerived : INonIntegerIndexer - {} + { } public interface IHasProperty { - string StringProperty {get; set; } + string StringProperty { get; set; } } public interface IHasPropertyDerived : IHasProperty - {} + { } + + public class AppendConverter : IValueConverter + { + public static IValueConverter Instance { get; } = new AppendConverter(); + + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + => string.Format("{0}+{1}", value, parameter); + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + => throw new NotImplementedException(); + + } + + public class TestData + { + public string StringProperty { get; set; } + } public class TestDataContext : IHasPropertyDerived { @@ -646,6 +882,8 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions public string[] ArrayProperty { get; set; } + public object[] ObjectsArrayProperty { get; set; } + public List ListProperty { get; set; } = new List(); public NonIntegerIndexer NonIntegerIndexerProperty { get; set; } = new NonIntegerIndexer(); diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs index 47a73cb360..9152131ab3 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/MarkupExtensions/DynamicResourceExtensionTests.cs @@ -345,6 +345,23 @@ namespace Avalonia.Markup.Xaml.UnitTests.MarkupExtensions Assert.Equal(0xff506070, brush.Color.ToUint32()); } + [Fact] + public void DynamicResource_Can_Be_Assigned_To_Resource_Property_In_Application() + { + var xaml = @" + + + #ff506070 + + +"; + + var application = (Application)AvaloniaRuntimeXamlLoader.Load(xaml); + var brush = (SolidColorBrush)application.Resources["brush"]; + + Assert.Equal(0xff506070, brush.Color.ToUint32()); + } [Fact] public void DynamicResource_Can_Be_Assigned_To_ItemTemplate_Property() diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ShapeTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ShapeTests.cs new file mode 100644 index 0000000000..bd577e84f8 --- /dev/null +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ShapeTests.cs @@ -0,0 +1,24 @@ +using Avalonia.Controls; +using Avalonia.Media; +using Xunit; + +namespace Avalonia.Markup.Xaml.UnitTests.Xaml +{ + public class ShapeTests : XamlTestBase + { + [Fact] + public void Can_Specify_DashStyle_In_XAML() + { + var xaml = @" + + + + +"; + + var target = AvaloniaRuntimeXamlLoader.Parse(xaml); + + Assert.NotNull(target); + } + } +} diff --git a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs index 67e46d25c3..77a4932ccc 100644 --- a/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs +++ b/tests/Avalonia.Markup.Xaml.UnitTests/Xaml/XamlIlTests.cs @@ -295,8 +295,27 @@ namespace Avalonia.Markup.Xaml.UnitTests Assert.Equal("Test", templated.Text); } } + + [Fact] + public void Should_Work_With_Base_Property() + { + var parsed = (ListBox)AvaloniaRuntimeXamlLoader.Load(@" + + + + + + +"); + + Assert.NotNull(parsed.ItemTemplate); + } } - + public class XamlIlBugTestsEventHandlerCodeBehind : Window { public object SavedContext; diff --git a/tests/Avalonia.Visuals.UnitTests/Media/PathSegmentTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/PathSegmentTests.cs new file mode 100644 index 0000000000..0737b4dc88 --- /dev/null +++ b/tests/Avalonia.Visuals.UnitTests/Media/PathSegmentTests.cs @@ -0,0 +1,34 @@ +using Avalonia.Media; +using Xunit; + +namespace Avalonia.Visuals.UnitTests.Media +{ + public class PathSegmentTests + { + [Fact] + public void PathSegment_Triggers_Invalidation_On_Property_Change() + { + var targetSegment = new ArcSegment() + { + Size = new Size(10, 10), + Point = new Point(5, 5) + }; + + var target = new PathGeometry + { + Figures = new PathFigures + { + new PathFigure { IsClosed = false, Segments = new PathSegments { targetSegment } } + } + }; + + var changed = false; + + target.Changed += (s, e) => changed = true; + + targetSegment.Size = new Size(20, 20); + + Assert.True(changed); + } + } +} diff --git a/tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs b/tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs index 418ac7576b..c2a1a5f9e4 100644 --- a/tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs +++ b/tests/Avalonia.Visuals.UnitTests/Media/PenTests.cs @@ -1,4 +1,5 @@ -using Avalonia.Media; +using Avalonia.Collections; +using Avalonia.Media; using Avalonia.Media.Immutable; using Xunit; @@ -39,7 +40,20 @@ namespace Avalonia.Visuals.UnitTests.Media var raised = false; target.Invalidated += (s, e) => raised = true; - dashes.Dashes = new[] { 0.1, 0.2 }; + dashes.Dashes = new AvaloniaList { 0.1, 0.2 }; + + Assert.True(raised); + } + + [Fact] + public void Adding_DashStyle_Dashes_Raises_Invalidated() + { + var dashes = new DashStyle(); + var target = new Pen { DashStyle = dashes }; + var raised = false; + + target.Invalidated += (s, e) => raised = true; + dashes.Dashes.Add(0.3); Assert.True(raised); }