-
Notifications
You must be signed in to change notification settings - Fork 7
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
dansiegel
wants to merge
10
commits into
main
Choose a base branch
from
dev/ds/window-title
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−134
Open
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9dc0f3d
fix: Window title not shown when packaged
dansiegel bedfa90
chore: fixing Uno Icon property name
dansiegel 4ac1880
chore: update to use Incremental Generator
dansiegel 2b9dbfd
chore: rename generator assembly name
dansiegel 23fc14f
chore: execute before design time targets
dansiegel ebe49aa
chore: adding debug output
dansiegel e17e19f
chore: evaluate unoimage.inputs
dansiegel de91be4
chore: file cleanup
dansiegel f53c8e9
chore: fixing inputs file evaluation
dansiegel ceb76c3
chore: optimize providers
dansiegel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/Resizetizer/Resizetizer.Generators/Resizetizer.Generators.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" /> | ||
<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
115
src/Resizetizer/Resizetizer.Generators/WindowTitleGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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