Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/Avalonia/AvaloniaBuildTasks.targets
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
<AvaloniaXamlIlVerifyIl Condition="'$(AvaloniaXamlIlVerifyIl)' == ''">false</AvaloniaXamlIlVerifyIl>
<AvaloniaXamlIlDebuggerLaunch Condition="'$(AvaloniaXamlIlDebuggerLaunch)' == ''">false</AvaloniaXamlIlDebuggerLaunch>
<AvaloniaXamlVerboseExceptions Condition="'$(AvaloniaXamlVerboseExceptions)' == ''">false</AvaloniaXamlVerboseExceptions>
<AvaloniaXamlCreateSourceInfo Condition="'$(AvaloniaXamlCreateSourceInfo)' == '' AND '$(Configuration)' == 'Debug'">true</AvaloniaXamlCreateSourceInfo>
<AvaloniaXamlCreateSourceInfo Condition="'$(AvaloniaXamlCreateSourceInfo)' == '' AND '$(Configuration)' != 'Debug'">false</AvaloniaXamlCreateSourceInfo>
</PropertyGroup>

<ItemGroup>
Expand Down Expand Up @@ -162,6 +164,7 @@
DelaySign="$(DelaySign)"
SkipXamlCompilation="$(_AvaloniaSkipXamlCompilation)"
DebuggerLaunch="$(AvaloniaXamlIlDebuggerLaunch)"
CreateSourceInfo="$(AvaloniaXamlCreateSourceInfo)"
DefaultCompileBindings="$(AvaloniaUseCompiledBindingsByDefault)"
VerboseExceptions="$(AvaloniaXamlVerboseExceptions)"
AnalyzerConfigFiles="@(EditorConfigFiles)"/>
Expand Down
5 changes: 5 additions & 0 deletions packages/Avalonia/AvaloniaRules.Project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
Description="Allow debug XAML compilation"
Category="Debug" />

<BoolProperty Name="AvaloniaXamlCreateSourceInfo"
DisplayName="Generate XAML Source Info"
Description="When enabled, the XAML compiler embeds SourceInfo metadata (file path, line, and column) into generated code. This allows tools and debuggers to locate the original .axaml source position of a selected element at runtime or in the designer."
Category="Debug" />

<BoolProperty Name="AvaloniaXamlVerboseExceptions"
DisplayName="Report verbose internal exceptions with stack traces"
Description="Also includes inner exceptions"
Expand Down
4 changes: 3 additions & 1 deletion src/Avalonia.Build.Tasks/CompileAvaloniaXamlTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public bool Execute()
ProjectDirectory, VerifyIl, DefaultCompileBindings, outputImportance,
new XamlCompilerDiagnosticsFilter(AnalyzerConfigFiles),
(SignAssembly && !DelaySign) ? AssemblyOriginatorKeyFile : null,
SkipXamlCompilation, DebuggerLaunch, VerboseExceptions);
SkipXamlCompilation, DebuggerLaunch, VerboseExceptions, CreateSourceInfo);

if (res.Success && !res.WrittenFile)
{
Expand Down Expand Up @@ -99,6 +99,8 @@ private static void CopyAndTouch(string source, string destination, bool shouldE

public bool DebuggerLaunch { get; set; }

public bool CreateSourceInfo { get; set; }

public bool VerboseExceptions { get; set; }

public ITaskItem[] AnalyzerConfigFiles { get; set; }
Expand Down
32 changes: 29 additions & 3 deletions src/Avalonia.Build.Tasks/XamlCompilerTaskExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal static CompileResult Compile(IBuildEngine engine,
string[] references, string projectDirectory,
bool verifyIl, bool defaultCompileBindings, MessageImportance logImportance,
XamlCompilerDiagnosticsFilter diagnosticsFilter, string strongNameKey,
bool skipXamlCompilation, bool debuggerLaunch, bool verboseExceptions)
bool skipXamlCompilation, bool debuggerLaunch, bool verboseExceptions, bool createSourceInfo)
{
try
{
Expand All @@ -67,7 +67,7 @@ internal static CompileResult Compile(IBuildEngine engine,
var compileRes = CompileCore(
engine, typeSystem, projectDirectory, verifyIl,
defaultCompileBindings, logImportance, diagnosticsFilter,
debuggerLaunch, verboseExceptions);
debuggerLaunch, verboseExceptions, createSourceInfo);
if (compileRes == null)
return new CompileResult(true);
if (compileRes == false)
Expand Down Expand Up @@ -107,7 +107,8 @@ internal static CompileResult Compile(IBuildEngine engine,
MessageImportance logImportance,
XamlCompilerDiagnosticsFilter diagnosticsFilter,
bool debuggerLaunch,
bool verboseExceptions)
bool verboseExceptions,
bool createSourceInfo)
{
if (debuggerLaunch)
{
Expand Down Expand Up @@ -210,6 +211,7 @@ TypeDefinition AddClass(string name, TypeAttributes extraAttributes = 0)
{
EnableIlVerification = verifyIl,
DefaultCompileBindings = defaultCompileBindings,
CreateSourceInfo = createSourceInfo,
DynamicSetterContainerProvider = new DefaultXamlDynamicSetterContainerProvider(dynamicSettersBuilder)
};

Expand Down Expand Up @@ -477,6 +479,30 @@ bool CompileGroup(IResourceGroup group)
classTypeDefinition.Fields.Add(designLoaderField);
typeSystem.AddCompilerGeneratedAttribute(designLoaderField);

// Add [XamlSourceInfoAttribute] to the generated class.
// Used at design time to locate the original .axaml file,
// since the runtime loader substitutes a fake file name.
if (createSourceInfo && document.FileSource.FilePath is { } filePath)
{
var attrType = typeSystem.FindType("Avalonia.Markup.Xaml.SourceInfo.XamlSourceInfoAttribute");
var attrTypeRef = asm.MainModule.ImportReference(typeSystem.GetTypeReference(attrType));

var attrCtorDef = attrTypeRef.Resolve()
.Methods.First(m => m.IsConstructor
&& m.Parameters.Count == 1
&& m.Parameters[0].ParameterType.FullName == "System.String");

var attrCtorRef = asm.MainModule.ImportReference(attrCtorDef);
var designFilenameAttribute = new CustomAttribute(attrCtorRef);
designFilenameAttribute.ConstructorArguments.Add(
new CustomAttributeArgument(
asm.MainModule.TypeSystem.String,
filePath
)
);
classTypeDefinition.CustomAttributes.Add(designFilenameAttribute);
}

const string TrampolineName = "!XamlIlPopulateTrampoline";
var trampolineMethodWithoutSP = new Lazy<MethodDefinition>(() => CreateTrampolineMethod(false));
var trampolineMethodWithSP = new Lazy<MethodDefinition>(() => CreateTrampolineMethod(true));
Expand Down
4 changes: 4 additions & 0 deletions src/Avalonia.DesignerSupport/Avalonia.DesignerSupport.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
<ItemGroup>
<EmbeddedResource Include="Remote\HtmlTransport\webapp\build\**\*.gz" />
<EmbeddedResource Condition="'$(Configuration)' == 'Debug'" Remove="Remote\HtmlTransport\webapp\build\**\*.map.gz" />
<Compile Include="..\Avalonia.Diagnostics\Diagnostics\SourceNavigator\RiderFileSelector.cs" Link="DesignerSelection\RiderFileSelector.cs" />
<Compile Include="..\Avalonia.Diagnostics\Diagnostics\SourceNavigator\VisualStudioFileSelector.cs" Link="DesignerSelection\VisualStudioFileSelector.cs" />
<Compile Include="..\Avalonia.Diagnostics\Diagnostics\SourceNavigator\VsCodeFileSelection.cs" Link="DesignerSelection\VsCodeFileSelection.cs" />
<ProjectReference Include="..\Markup\Avalonia.Markup.Xaml\Avalonia.Markup.Xaml.csproj" />
<ProjectReference Include="..\Markup\Avalonia.Markup\Avalonia.Markup.csproj" />
<ProjectReference Include="..\Avalonia.Base\Avalonia.Base.csproj" />
<ProjectReference Include="..\Avalonia.Controls\Avalonia.Controls.csproj" />
<PackageReference Include="envdte" Version="17.14.40260" />
</ItemGroup>
</Project>
15 changes: 15 additions & 0 deletions src/Avalonia.DesignerSupport/DesignWindowLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Controls;
using Avalonia.Controls.Embedding.Offscreen;
using Avalonia.Controls.Platform;
using Avalonia.DesignerSupport.DesignerSelection;
using Avalonia.Markup.Xaml;
using Avalonia.Styling;

Expand Down Expand Up @@ -47,6 +48,7 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
});
var style = loaded as IStyle;
var resources = loaded as ResourceDictionary;
bool found = true;
if (style != null)
{
var substitute = Design.GetPreviewWith((AvaloniaObject)style);
Expand All @@ -56,6 +58,7 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
control = substitute;
}
else
{
control = new StackPanel
{
Children =
Expand All @@ -67,6 +70,8 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
new TextBlock {Text = "before setters in your first Style"}
}
};
found = false;
}
}
else if (resources != null)
{
Expand All @@ -77,6 +82,7 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
control = substitute;
}
else
{
control = new StackPanel
{
Children =
Expand All @@ -88,9 +94,14 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
new TextBlock {Text = "in your resource dictionary"}
}
};
found = false;
}
}
else if (loaded is Application)
{
control = new TextBlock { Text = "This file cannot be previewed in design view" };
found = false;
}
else
control = (Control)loaded;

Expand All @@ -104,6 +115,10 @@ public static Window LoadDesignerWindow(string xaml, string assemblyPath, string
offscreenImpl.RenderScaling = renderScaling;

Design.ApplyDesignModeProperties(window, control);
if (found)
{
ElementInspector.Initialize(window);
}

if (!window.IsSet(Window.SizeToContentProperty))
{
Expand Down
Loading