@@ -47,7 +47,7 @@ private static bool IsValidCandidate(SyntaxNode syntaxNode)
4747 {
4848 return classDeclaration . BaseList ? . Types . Any ( baseType => baseType . Type is IdentifierNameSyntax identifier &&
4949 ( identifier . Identifier . Text == SpecificationWindowName || identifier . Identifier . Text == SpecificationControlName ) ) == true ;
50- }
50+ }
5151 return false ;
5252 }
5353
@@ -72,10 +72,7 @@ private static (ClassDeclarationSyntax Class, SpecificationType Type)? GetCandid
7272 }
7373 }
7474
75- if ( type != SpecificationType . None )
76- return ( classDeclaration , type ) ;
77-
78- return null ;
75+ return type != SpecificationType . None ? ( classDeclaration , type ) : null ;
7976 }
8077
8178 /// <summary>
@@ -89,41 +86,43 @@ private static string GenerateClass(string namespaceName, string className, Spec
8986 {
9087 // Namespace
9188 var namespaceDeclaration = SyntaxFactory . NamespaceDeclaration ( SyntaxFactory . ParseName ( namespaceName ) )
89+ . WithLeadingTrivia ( SourceGeneratorHelper . GetLicenceHeader ( ) )
9290 . NormalizeWhitespace ( ) ;
9391
9492 // Usings
95- var usings = new [ ]
96- {
97- SyntaxFactory . UsingDirective ( SyntaxFactory . ParseName ( "System.Windows" ) ) ,
98- SyntaxFactory . UsingDirective ( SyntaxFactory . ParseName ( "Microsoft.UI.Xaml" ) ) ,
99- SyntaxFactory . UsingDirective ( SyntaxFactory . ParseName ( "Microsoft.UI.Xaml.Controls" ) )
100- } ;
93+ var usings = Array . Empty < UsingDirectiveSyntax > ( ) ;
10194
10295 // Field declaration: private IRealTimeLayoutService RTLayoutService;
10396 var fieldDeclaration = SyntaxFactory . FieldDeclaration (
10497 SyntaxFactory . VariableDeclaration (
105- SyntaxFactory . IdentifierName ( "IRealTimeLayoutService" ) )
106- . AddVariables ( SyntaxFactory . VariableDeclarator ( "RTLayoutService" ) ) )
107- . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PrivateKeyword ) ) ;
98+ SyntaxFactory . IdentifierName ( ServiceInterfaceName ) )
99+ . AddVariables ( SyntaxFactory . VariableDeclarator ( ServiceVariableName )
100+ . WithInitializer ( SyntaxFactory . EqualsValueClause (
101+ SyntaxFactory . ParseExpression ( $ "Ioc.Default.GetRequiredService<{ ServiceInterfaceName } >()") ) ) ) )
102+ . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PrivateKeyword ) , SyntaxFactory . Token ( SyntaxKind . ReadOnlyKeyword ) )
103+ . AddAttributeLists ( SourceGeneratorHelper . GetAttributeForField ( nameof ( RealTimeLayoutGenerator ) ) ) ;
108104
109105 // Method: InitializeContentLayout
110106 var initializeContentLayoutMethod = SyntaxFactory . MethodDeclaration (
111107 SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( SyntaxKind . VoidKeyword ) ) ,
112108 "InitializeContentLayout" )
113109 . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) )
114- . WithBody ( SyntaxFactory . Block ( CreateInitializeContentLayoutBody ( type ) ) ) ;
110+ . WithBody ( SyntaxFactory . Block ( CreateContentLayoutBody ( type , isInitialize : true ) ) )
111+ . AddAttributeLists ( SourceGeneratorHelper . GetAttributeForMethod ( nameof ( RealTimeLayoutGenerator ) ) ) ;
115112
116113 // Method: UpdateContentLayout
117114 var updateContentLayoutMethod = SyntaxFactory . MethodDeclaration (
118115 SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( SyntaxKind . VoidKeyword ) ) ,
119116 "UpdateContentLayout" )
120117 . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) )
121- . WithBody ( SyntaxFactory . Block ( CreateUpdateContentLayoutBody ( type ) ) ) ;
118+ . WithBody ( SyntaxFactory . Block ( CreateContentLayoutBody ( type ) ) )
119+ . AddAttributeLists ( SourceGeneratorHelper . GetAttributeForMethod ( nameof ( RealTimeLayoutGenerator ) ) ) ;
122120
123121 // Class declaration
124122 var classDeclaration = SyntaxFactory . ClassDeclaration ( className )
125123 . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PublicKeyword ) , SyntaxFactory . Token ( SyntaxKind . PartialKeyword ) )
126- . AddMembers ( fieldDeclaration , initializeContentLayoutMethod , updateContentLayoutMethod ) ;
124+ . AddMembers ( fieldDeclaration , initializeContentLayoutMethod , updateContentLayoutMethod )
125+ . AddAttributeLists ( SourceGeneratorHelper . GetAttributeForField ( nameof ( RealTimeLayoutGenerator ) ) ) ;
127126
128127 // Add class to namespace
129128 namespaceDeclaration = namespaceDeclaration . AddMembers ( classDeclaration ) ;
@@ -133,44 +132,66 @@ private static string GenerateClass(string namespaceName, string className, Spec
133132 . AddMembers ( namespaceDeclaration )
134133 . NormalizeWhitespace ( ) ;
135134
136- return compilationUnit . ToFullString ( ) ;
135+ return SyntaxFactory . SyntaxTree ( compilationUnit , encoding : Encoding . UTF8 ) . GetText ( ) . ToString ( ) ;
137136 }
138137
139138 /// <summary>
140- /// Creates the body statements for the InitializeContentLayout method.
139+ /// Creates a collection of statements for updating the content layout body.
140+ /// Depending on the specification type, it will update the title bar and content layout.
141+ /// If the <paramref name="isInitialize"/> flag is set to true, a callback is added for updating the content layout.
141142 /// </summary>
142- /// <param name="type">The type of the specification (Window or Control).</param>
143- /// <returns>A collection of statements for the method body.</returns>
144- private static IEnumerable < StatementSyntax > CreateInitializeContentLayoutBody ( SpecificationType type )
145- {
146- var statements = new List < StatementSyntax >
147- {
148-
149- SyntaxFactory . ParseStatement ( "RTLayoutService = Ioc.Default.GetRequiredService<IRealTimeLayoutService>();" )
150- } ;
151-
152- if ( type == SpecificationType . Window )
153- statements . Add ( SyntaxFactory . ParseStatement ( "RTLayoutService.UpdateTitleBar(this);" ) ) ;
154-
155- statements . Add ( SyntaxFactory . ParseStatement ( "RTLayoutService.UpdateContent(this);" ) ) ;
156- statements . Add ( SyntaxFactory . ParseStatement ( "RTLayoutService.AddCallback(this, UpdateContentLayout);" ) ) ;
157-
158- return statements ;
159- }
160-
161- /// <summary>
162- /// Creates the body statements for the UpdateContentLayout method.
163- /// </summary>
164- /// <param name="type">The type of the specification (Window or Control).</param>
165- /// <returns>A collection of statements for the method body.</returns>
166- private static IEnumerable < StatementSyntax > CreateUpdateContentLayoutBody ( SpecificationType type )
143+ /// <param name="type">The specification type, used to determine if the title bar should be updated.</param>
144+ /// <param name="isInitialize">A flag indicating whether to add a callback for content layout initialization.</param>
145+ /// <returns>An IEnumerable of <see cref="StatementSyntax"/> representing the generated statements.</returns>
146+ private static IEnumerable < StatementSyntax > CreateContentLayoutBody ( SpecificationType type , bool isInitialize = false )
167147 {
168148 var statements = new List < StatementSyntax > ( ) ;
169149
170150 if ( type == SpecificationType . Window )
171- statements . Add ( SyntaxFactory . ParseStatement ( "RTLayoutService.UpdateTitleBar(this);" ) ) ;
151+ {
152+ statements . Add (
153+ SyntaxFactory . ExpressionStatement (
154+ SyntaxFactory . InvocationExpression (
155+ SyntaxFactory . MemberAccessExpression (
156+ SyntaxKind . SimpleMemberAccessExpression ,
157+ SyntaxFactory . IdentifierName ( ServiceVariableName ) ,
158+ SyntaxFactory . IdentifierName ( "UpdateTitleBar" ) ) )
159+ . WithArgumentList (
160+ SyntaxFactory . ArgumentList (
161+ SyntaxFactory . SingletonSeparatedList (
162+ SyntaxFactory . Argument ( SyntaxFactory . ThisExpression ( ) ) ) ) ) ) ) ;
163+ }
172164
173- statements . Add ( SyntaxFactory . ParseStatement ( "RTLayoutService.UpdateContent(this);" ) ) ;
165+ statements . Add (
166+ SyntaxFactory . ExpressionStatement (
167+ SyntaxFactory . InvocationExpression (
168+ SyntaxFactory . MemberAccessExpression (
169+ SyntaxKind . SimpleMemberAccessExpression ,
170+ SyntaxFactory . IdentifierName ( ServiceVariableName ) ,
171+ SyntaxFactory . IdentifierName ( "UpdateContent" ) ) )
172+ . WithArgumentList (
173+ SyntaxFactory . ArgumentList (
174+ SyntaxFactory . SingletonSeparatedList (
175+ SyntaxFactory . Argument ( SyntaxFactory . ThisExpression ( ) ) ) ) ) ) ) ;
176+
177+ if ( isInitialize )
178+ {
179+ statements . Add (
180+ SyntaxFactory . ExpressionStatement (
181+ SyntaxFactory . InvocationExpression (
182+ SyntaxFactory . MemberAccessExpression (
183+ SyntaxKind . SimpleMemberAccessExpression ,
184+ SyntaxFactory . IdentifierName ( ServiceVariableName ) ,
185+ SyntaxFactory . IdentifierName ( "AddCallback" ) ) )
186+ . WithArgumentList (
187+ SyntaxFactory . ArgumentList (
188+ SyntaxFactory . SeparatedList (
189+ new [ ]
190+ {
191+ SyntaxFactory . Argument ( SyntaxFactory . ThisExpression ( ) ) ,
192+ SyntaxFactory . Argument ( SyntaxFactory . IdentifierName ( "UpdateContentLayout" ) )
193+ } ) ) ) ) ) ;
194+ }
174195
175196 return statements ;
176197 }
@@ -188,6 +209,7 @@ private static string GetNamespace(SyntaxNode node)
188209 return namespaceDeclaration . Name . ToString ( ) ;
189210 node = node . Parent ! ;
190211 }
212+
191213 return "GlobalNamespace" ;
192214 }
193215 }
0 commit comments