Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Window title not shown when packaged #309

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
30 changes: 7 additions & 23 deletions src/.nuspec/Uno.Resizetizer.targets
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.GenerateWasmSplashAssets_v0"/>

<UsingTask
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.WindowIconGeneratorTask_V0"/>

<UsingTask
AssemblyFile="$(_UnoResizetizerTaskAssemblyName)"
TaskName="Uno.Resizetizer.CleanupAssetsTask_v0"/>
Expand Down Expand Up @@ -589,25 +585,13 @@
</ItemGroup>
</Target>

<Target Name="_GenerateWindowUnoIconExtension"
Condition="'$(_UnoResizetizerIsCompatibleApp)' == 'True'"
BeforeTargets="Build;CoreCompile;XamlPreCompile"
Inputs="@(UnoIcon)"
Outputs="@(_WindowIconExtension)">

<PropertyGroup>
<WindowTitle Condition="'$(WindowTitle)' == ''">$([MSBuild]::ValueOrDefault('$(ApplicationTitle)', '$(AssemblyName)'))</WindowTitle>
</PropertyGroup>

<WindowIconGeneratorTask_v0
IntermediateOutputDirectory="$(_UnoResizetizerIntermediateOutputRoot)"
WindowTitle="$(WindowTitle)"
UnoIcons="@(UnoIcon)">
<Output ItemName="GeneratedFile"
TaskParameter="GeneratedClass"/>
<Output ItemName="FilesWrite"
TaskParameter="GeneratedClass"/>
</WindowIconGeneratorTask_v0>
<Target Name="SetUnoImage"
BeforeTargets="GenerateMSBuildEditorConfigFileShouldRun;GenerateMSBuildEditorConfigFileCore">
<CreateProperty Value="@(UnoIcon, ',')">
<Output
TaskParameter="Value"
PropertyName="UnoResizetizerIcon" />
</CreateProperty>
</Target>

<Target Name="_AddResizetizerWindowIconExtensions"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<IsPackable>false</IsPackable>
<DevelopmentDependency>true</DevelopmentDependency>
<BuildOutputTargetFolder>analyzers/dotnet/cs</BuildOutputTargetFolder>
<LangVersion>latest</LangVersion>
<IncludeBuildOutput>false</IncludeBuildOutput>
<PolySharpIncludeGeneratedTypes>
System.Runtime.CompilerServices.IsExternalInit;
System.Diagnostics.CodeAnalysis.NotNullWhenAttribute;
</PolySharpIncludeGeneratedTypes>
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AvantiPoint.CodeGenHelpers" Version="1.6.20">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.10.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a too recent. We should really be using the oldest version we can and bump only if there is a specific new API we need

<PackageReference Include="PolySharp" Version="1.14.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

</Project>
115 changes: 115 additions & 0 deletions src/Resizetizer/Resizetizer.Generators/WindowTitleGenerator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System.IO;
using CodeGenHelpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

namespace Resizetizer.Generators;

[Generator(LanguageNames.CSharp)]
internal sealed class WindowTitleGenerator : ISourceGenerator
dansiegel marked this conversation as resolved.
Show resolved Hide resolved
{
public void Execute(GeneratorExecutionContext context)
{
var options = context.AnalyzerConfigOptions.GlobalOptions;
if (!GetProperty(options, "IsUnoHead") || !HasUnoIcon(options, out var unoIcon))
{
return;
}

GenerateLegacyNamespaceCompat(context);

GenerateWindowTitleExtension(context, unoIcon);
}

public void Initialize(GeneratorInitializationContext context)
{
}

private static void GenerateWindowTitleExtension(GeneratorExecutionContext context, string unoIcon)
{
var options = context.AnalyzerConfigOptions.GlobalOptions;
var rootNamespace = GetPropertyValue(options, "RootNamespace");
var iconName = Path.GetFileNameWithoutExtension(unoIcon);
var windowTitle = GetPropertyValue(options, "ApplicationTitle");
if (string.IsNullOrEmpty(windowTitle))
{
windowTitle = context.Compilation.AssemblyName!;
}

var builder = CodeBuilder.Create(rootNamespace)
.AddClass("WindowExtensions")
.MakeStaticClass()
.WithSummary(@"Extension methods for the <see cref=""global::Microsoft.UI.Xaml.Window"" /> class.");

builder.AddMethod("SetWindowIcon")
.AddParameter("this global::Microsoft.UI.Xaml.Window", "window")
.MakeStaticMethod()
.MakePublicMethod()
.WithSummary(@"This will set the Window Icon for the given <see cref=""global::Microsoft.UI.Xaml.Window"" /> using the provided UnoIcon.")
.WithBody(w =>
{
w.AppendUnindentedLine("#if WINDOWS && !HAS_UNO");
w.AppendLine("var hWnd = global::WinRT.Interop.WindowNative.GetWindowHandle(window);");
w.NewLine();
w.AppendLine("// Retrieve the WindowId that corresponds to hWnd.");
w.AppendLine("global::Microsoft.UI.WindowId windowId = global::Microsoft.UI.Win32Interop.GetWindowIdFromWindow(hWnd);");
w.NewLine();
w.AppendLine("// Lastly, retrieve the AppWindow for the current (XAML) WinUI 3 window.");
w.AppendLine("global::Microsoft.UI.Windowing.AppWindow appWindow = global::Microsoft.UI.Windowing.AppWindow.GetFromWindowId(windowId);");
w.AppendLine($@"appWindow.SetIcon(""{iconName}.ico"");");
w.NewLine();
w.AppendLine("// Set the Window Title Only if it has the Default WinUI Desktop value and we are running Unpackaged");
// We're no longer checking for IsPackaged as this seems to be needed when Packaged as well.
w.If(@"appWindow.Title == ""WinUI Desktop""")
.WithBody(b =>
{
b.AppendLine($@"appWindow.Title = ""{windowTitle}"";");
})
.EndIf();
w.AppendUnindentedLine("#endif");
});

//builder.AddMethod("IsPackaged")
// .WithReturnType("bool")
// .MakePrivateMethod()
// .MakeStaticMethod()
// .WithBody(w =>
// {
// using (w.Block("try"))
// {
// w.AppendLine("return global::Windows.ApplicationModel.Package.Current != null;");
// }
// using (w.Block("catch"))
// {
// w.AppendLine("return false;");
// }
// });

AddSource(context, builder);
}

private static string GetPropertyValue(AnalyzerConfigOptions options, string key) =>
options.TryGetValue($"build_property.{key}", out var value) ? value : string.Empty;

private static bool GetProperty(AnalyzerConfigOptions options, string key) =>
bool.TryParse(GetPropertyValue(options, key), out var result) && result;

private static bool HasUnoIcon(AnalyzerConfigOptions options, out string unoIcon)
{
unoIcon = GetPropertyValue(options, "UnoIcon");
return !string.IsNullOrEmpty(unoIcon) && !unoIcon.Contains(",");
}

private static void GenerateLegacyNamespaceCompat(GeneratorExecutionContext context)
{
var builder = CodeBuilder.Create("Uno.Resizetizer")
.AddClass("__LegacyResizetizerSupport__")
.WithSummary("This is added to ensure the Uno.Resizetizer namespace is present to avoid breaking any applications.")
.MakeStaticClass();

AddSource(context, builder);
}

private static void AddSource(GeneratorExecutionContext context, ClassBuilder builder) =>
context.AddSource($"{builder.FullyQualifiedName}.g.cs", builder.Build());
}
18 changes: 17 additions & 1 deletion src/Resizetizer/src/Resizetizer.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
Expand Down Expand Up @@ -72,6 +72,13 @@
<None Include="$(UnoNuspecDirectory)Uno.Resizetizer.windows.skia.targets" LinkBase="build" Pack="true" PackagePath="build/$(PackageId).windows.skia.targets" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Resizetizer.Generators\Resizetizer.Generators.csproj">
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
<OutputItemType>Analyzer</OutputItemType>
</ProjectReference>
</ItemGroup>

<Import Project="../Directory.UnoMetadata.targets" />

<!-- Gets the Runtime Assets that need to be copied to the NuGet -->
Expand Down Expand Up @@ -119,4 +126,13 @@
Condition="Exists('$(NuGetPackageRoot)$(PackageId.ToLowerInvariant())') And '$(OS)' != 'Windows_NT'" />
</Target>

<Target Name="BundleGenerators" BeforeTargets="Pack;_LoadPackInputItems;_GetPackageFiles">
<ItemGroup>
<None Include="..\Resizetizer.Generators\bin\$(Configuration)\netstandard2.0\Resizetizer.Generators.dll"
Pack="true"
PackagePath="analyzers\dotnet\cs\Resizetizer.Generators.dll"
Visible="false" />
</ItemGroup>
</Target>

</Project>
108 changes: 0 additions & 108 deletions src/Resizetizer/src/WindowIconGeneratorTask.cs

This file was deleted.

8 changes: 8 additions & 0 deletions src/Resizetizer/uno.resizetizer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
..\.nuspec\Uno.Resizetizer.targets = ..\.nuspec\Uno.Resizetizer.targets
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Resizetizer.Generators", "Resizetizer.Generators\Resizetizer.Generators.csproj", "{9CDB1CAA-293A-434C-A092-29D0EE03496D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
DEBUG_RESIZETIZER|Any CPU = DEBUG_RESIZETIZER|Any CPU
Expand All @@ -31,6 +33,12 @@ Global
{7695AB69-4414-4539-8172-A78D8460F663}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7695AB69-4414-4539-8172-A78D8460F663}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7695AB69-4414-4539-8172-A78D8460F663}.Release|Any CPU.Build.0 = Release|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.DEBUG_RESIZETIZER|Any CPU.ActiveCfg = Debug|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.DEBUG_RESIZETIZER|Any CPU.Build.0 = Debug|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9CDB1CAA-293A-434C-A092-29D0EE03496D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading