Browse Source

Merge branch 'master' into fixes/12482-datagrid-scroll

pull/12732/head
NLNet 3 years ago
committed by GitHub
parent
commit
af9d909298
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 34
      src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlTemplateTargetTypeMetadataTransformer.cs
  2. 42
      src/Windows/Avalonia.Win32/Win32Platform.cs
  3. 26
      src/Windows/Avalonia.Win32/Win32PlatformOptions.cs
  4. 18
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs

34
src/Markup/Avalonia.Markup.Xaml.Loader/CompilerExtensions/Transformers/AvaloniaXamlIlControlTemplateTargetTypeMetadataTransformer.cs

@ -1,6 +1,7 @@
using System.Linq; using System.Linq;
using XamlX.Ast; using XamlX.Ast;
using XamlX.Transform; using XamlX.Transform;
using XamlX.Transform.Transformers;
using XamlX.TypeSystem; using XamlX.TypeSystem;
namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
@ -22,26 +23,19 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
IXamlAstTypeReference targetType; IXamlAstTypeReference targetType;
var templatableBaseType = context.Configuration.TypeSystem.GetType("Avalonia.Controls.Control"); var templatableBaseType = context.Configuration.TypeSystem.GetType("Avalonia.Controls.Control");
if ((tt?.Values.FirstOrDefault() is XamlTypeExtensionNode tn)) targetType = tt?.Values.FirstOrDefault() switch
{
targetType = tn.Value;
}
else
{ {
var parentScope = context.ParentNodes().OfType<AvaloniaXamlIlTargetTypeMetadataNode>() XamlTypeExtensionNode tn => tn.Value,
.FirstOrDefault(); XamlAstTextNode textNode => TypeReferenceResolver.ResolveType(context, textNode.Text, false, textNode, true),
if (parentScope?.ScopeType == AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style) _ when context.ParentNodes()
targetType = parentScope.TargetType; .OfType<AvaloniaXamlIlTargetTypeMetadataNode>()
else if (context.ParentNodes().Skip(1).FirstOrDefault() is XamlAstObjectNode directParentNode .FirstOrDefault() is { ScopeType: AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Style } parentScope => parentScope.TargetType,
&& templatableBaseType.IsAssignableFrom(directParentNode.Type.GetClrType())) _ when context.ParentNodes().Skip(1).FirstOrDefault() is XamlAstObjectNode directParentNode
targetType = directParentNode.Type; && templatableBaseType.IsAssignableFrom(directParentNode.Type.GetClrType()) => directParentNode.Type,
else _ => new XamlAstClrTypeReference(node,
targetType = new XamlAstClrTypeReference(node, templatableBaseType, false)
templatableBaseType, false); };
}
return new AvaloniaXamlIlTargetTypeMetadataNode(on, targetType, return new AvaloniaXamlIlTargetTypeMetadataNode(on, targetType,
AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.ControlTemplate); AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.ControlTemplate);
@ -59,7 +53,7 @@ namespace Avalonia.Markup.Xaml.XamlIl.CompilerExtensions.Transformers
ControlTemplate, ControlTemplate,
Transitions Transitions
} }
public AvaloniaXamlIlTargetTypeMetadataNode(IXamlAstValueNode value, IXamlAstTypeReference targetType, public AvaloniaXamlIlTargetTypeMetadataNode(IXamlAstValueNode value, IXamlAstTypeReference targetType,
ScopeTypes type) ScopeTypes type)
: base(value, value) : base(value, value)

42
src/Windows/Avalonia.Win32/Win32Platform.cs

@ -49,7 +49,6 @@ namespace Avalonia.Win32
public Win32Platform() public Win32Platform()
{ {
SetDpiAwareness();
CreateMessageWindow(); CreateMessageWindow();
_dispatcher = new Win32DispatcherImpl(_hwnd); _dispatcher = new Win32DispatcherImpl(_hwnd);
} }
@ -80,6 +79,9 @@ namespace Avalonia.Win32
public static void Initialize(Win32PlatformOptions options) public static void Initialize(Win32PlatformOptions options)
{ {
s_options = options; s_options = options;
SetDpiAwareness();
var renderTimer = options.ShouldRenderOnUIThread ? new UiThreadRenderTimer(60) : new DefaultRenderTimer(60); var renderTimer = options.ShouldRenderOnUIThread ? new UiThreadRenderTimer(60) : new DefaultRenderTimer(60);
AvaloniaLocator.CurrentMutable AvaloniaLocator.CurrentMutable
@ -264,12 +266,31 @@ namespace Avalonia.Win32
var user32 = LoadLibrary("user32.dll"); var user32 = LoadLibrary("user32.dll");
var method = GetProcAddress(user32, nameof(SetProcessDpiAwarenessContext)); var method = GetProcAddress(user32, nameof(SetProcessDpiAwarenessContext));
var dpiAwareness = Options.DpiAwareness;
if (method != IntPtr.Zero) if (method != IntPtr.Zero)
{ {
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) || if (dpiAwareness == Win32DpiAwareness.Unaware)
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE))
{ {
return; if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_UNAWARE))
{
return;
}
}
else if (dpiAwareness == Win32DpiAwareness.SystemDpiAware)
{
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_SYSTEM_AWARE))
{
return;
}
}
else if (dpiAwareness == Win32DpiAwareness.PerMonitorDpiAware)
{
if (SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) ||
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE))
{
return;
}
} }
} }
@ -278,11 +299,20 @@ namespace Avalonia.Win32
if (method != IntPtr.Zero) if (method != IntPtr.Zero)
{ {
SetProcessDpiAwareness(PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE); var awareness = (dpiAwareness) switch
{
Win32DpiAwareness.Unaware => PROCESS_DPI_AWARENESS.PROCESS_DPI_UNAWARE,
Win32DpiAwareness.SystemDpiAware => PROCESS_DPI_AWARENESS.PROCESS_SYSTEM_DPI_AWARE,
Win32DpiAwareness.PerMonitorDpiAware => PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE,
_ => PROCESS_DPI_AWARENESS.PROCESS_PER_MONITOR_DPI_AWARE,
};
SetProcessDpiAwareness(awareness);
return; return;
} }
SetProcessDPIAware(); if (dpiAwareness != Win32DpiAwareness.Unaware)
SetProcessDPIAware();
} }
} }
} }

26
src/Windows/Avalonia.Win32/Win32PlatformOptions.cs

@ -25,6 +25,27 @@ public enum Win32RenderingMode
Wgl = 3 Wgl = 3
} }
/// <summary>
/// Represents the DPI Awareness for the application.
/// </summary>
public enum Win32DpiAwareness
{
/// <summary>
/// The application is DPI unaware.
/// </summary>
Unaware,
/// <summary>
/// The application is system DPI aware. It will query DPI once and will not adjust to new DPI changes
/// </summary>
SystemDpiAware,
/// <summary>
/// The application is per-monitor DPI aware. It adjust its scale factor whenever DPI changes.
/// </summary>
PerMonitorDpiAware
}
/// <summary> /// <summary>
/// Represents the Win32 window composition mode. /// Represents the Win32 window composition mode.
/// </summary> /// </summary>
@ -137,4 +158,9 @@ public class Win32PlatformOptions
/// and <see cref="CompositionMode"/> only accepts null or <see cref="Win32CompositionMode.RedirectionSurface"/>. /// and <see cref="CompositionMode"/> only accepts null or <see cref="Win32CompositionMode.RedirectionSurface"/>.
/// </summary> /// </summary>
public IPlatformGraphics? CustomPlatformGraphics { get; set; } public IPlatformGraphics? CustomPlatformGraphics { get; set; }
/// <summary>
/// Gets or sets the application's DPI awareness.
/// </summary>
public Win32DpiAwareness DpiAwareness { get; set; } = Win32DpiAwareness.PerMonitorDpiAware;
} }

18
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/ControlTemplateTests.cs

@ -286,6 +286,24 @@ namespace Avalonia.Markup.Xaml.UnitTests.Xaml
Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Result); Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Result);
} }
[Fact]
public void ControlTemplate_With_String_TargetType()
{
var xaml = @"
<ControlTemplate xmlns='https://github.com/avaloniaui'
xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'
TargetType='ContentControl'>
<ContentPresenter Content='{TemplateBinding Content}' />
</ControlTemplate>
";
var template = AvaloniaRuntimeXamlLoader.Parse<ControlTemplate>(xaml);
Assert.Equal(typeof(ContentControl), template.TargetType);
Assert.IsType(typeof(ContentPresenter), template.Build(new ContentControl()).Result);
}
[Fact] [Fact]
public void ControlTemplate_With_Panel_Children_Are_Added() public void ControlTemplate_With_Panel_Children_Are_Added()
{ {

Loading…
Cancel
Save