예제 #1
0
        private void BuildAmbientResources(XamlFileDefinition[] files, XamlGlobalStaticResourcesMap map)
        {
            // Lookup for GlobalStaticResources classes in external assembly
            // references only, and in Uno.UI itself for generic.xaml-like resources.

            var query = from ext in _metadataHelper.Compilation.ExternalReferences
                        let sym = _metadataHelper.Compilation.GetAssemblyOrModuleSymbol(ext) as IAssemblySymbol
                                  where sym != null
                                  from module in sym.Modules
                                  from reference in module.ReferencedAssemblies
                                  where reference.Name == "Uno.UI" || sym.Name == "Uno.UI"
                                  from typeName in sym.GlobalNamespace.GetNamespaceTypes()
                                  where typeName.Name.EndsWith("GlobalStaticResources")
                                  select typeName;

            _ambientGlobalResources = query.Distinct().ToArray();

            foreach (var ambientResources in _ambientGlobalResources)
            {
                var publicProperties = from member in ambientResources.GetAllProperties()
                                       where member.DeclaredAccessibility == Microsoft.CodeAnalysis.Accessibility.Public
                                       select member;


                foreach (var member in publicProperties)
                {
                    map.Add(member.Name, ambientResources.ContainingNamespace.ToDisplayString(), XamlGlobalStaticResourcesMap.ResourcePrecedence.System);
                }
            }
        }
예제 #2
0
        private XamlGlobalStaticResourcesMap BuildAssemblyGlobalStaticResourcesMap(XamlFileDefinition[] files)
        {
            var map = new XamlGlobalStaticResourcesMap();

            BuildLocalProjectResources(files, map);
            BuildAmbientResources(files, map);

            return(map);
        }
예제 #3
0
        private XamlGlobalStaticResourcesMap BuildAssemblyGlobalStaticResourcesMap(XamlFileDefinition[] files, XamlFileDefinition[] filesFull, string[] links)
        {
            var map = new XamlGlobalStaticResourcesMap();

            BuildLocalProjectResources(files, map);
            BuildAmbientResources(files, map);
            map.BuildResourceDictionaryMap(filesFull, links);

            return(map);
        }
예제 #4
0
        private void BuildLocalProjectResources(XamlFileDefinition[] files, XamlGlobalStaticResourcesMap map)
        {
            foreach (var file in files)
            {
                var topLevelControl = file.Objects.FirstOrDefault();

                if (topLevelControl?.Type.Name == "ResourceDictionary")
                {
                    BuildResourceMap(topLevelControl, map);

                    var themeDictionaries = topLevelControl.Members.FirstOrDefault(m => m.Member.Name == "ThemeDictionaries");

                    if (themeDictionaries != null)
                    {
                        // We extract all distinct keys of all themed resource dictionaries defined and add them to global map

                        IEnumerable <string> GetResources(XamlObjectDefinition themeDictionary)
                        {
                            if (!(themeDictionary.Members
                                  .FirstOrDefault(x => x.Member.Name.Equals("Key"))
                                  ?.Value is string))
                            {
                                yield break;
                            }

                            var resources = themeDictionary.Members
                                            .FirstOrDefault(x => x.Member.Name.Equals("_UnknownContent"))
                                            ?.Objects;

                            if (resources != null)
                            {
                                foreach (var resource in resources)
                                {
                                    if (resource.Members.FirstOrDefault(x => x.Member.Name.Equals("Key"))
                                        ?.Value is string resourceKey)
                                    {
                                        yield return(resourceKey);
                                    }
                                }
                            }
                        }

                        var themeResources = themeDictionaries
                                             .Objects
                                             .SelectMany(GetResources)
                                             .Distinct();

                        foreach (var themeResource in themeResources)
                        {
                            map.Add(themeResource, _defaultNamespace, XamlGlobalStaticResourcesMap.ResourcePrecedence.Local);
                        }
                    }
                }
            }
        }
예제 #5
0
        private void BuildResourceMap(XamlObjectDefinition parentNode, XamlGlobalStaticResourcesMap map)
        {
            var contentNode = parentNode.Members.FirstOrDefault(m => m.Member.Name == "_UnknownContent");

            if (contentNode != null)
            {
                foreach (var resource in contentNode.Objects)
                {
                    var key = resource.Members.FirstOrDefault(m => m.Member.Name == "Key");

                    if (key != null)
                    {
                        map.Add(key.Value.ToString(), _defaultNamespace, XamlGlobalStaticResourcesMap.ResourcePrecedence.Local);
                    }
                }
            }
        }
예제 #6
0
        private void BuildLocalProjectResources(XamlFileDefinition[] files, XamlGlobalStaticResourcesMap map)
        {
            foreach (var file in files)
            {
                var topLevelControl = file.Objects.FirstOrDefault();

                if (topLevelControl?.Type.Name == "ResourceDictionary")
                {
                    var resources = new Dictionary <string, XamlObjectDefinition>();

                    BuildResourceMap(topLevelControl, map);

                    var themeResources = topLevelControl.Members.FirstOrDefault(m => m.Member.Name == "ThemeDictionaries");

                    if (themeResources != null)
                    {
                        // Theme resources are not supported for now, so we take the default key
                        // and consider everthing inside as a standard StaticResource.

                        var defaultTheme = themeResources
                                           .Objects
                                           .FirstOrDefault(o => o
                                                           .Members
                                                           .Any(m =>
                                                                m.Member.Name == "Key" &&
                                                                m.Value.ToString() == "Default"
                                                                )
                                                           );

                        if (defaultTheme != null)
                        {
                            BuildResourceMap(defaultTheme, map);
                        }
                    }
                }
            }
        }
예제 #7
0
 /// <summary>
 /// Should this Xaml be included when defining default styles? Outside of Uno this is always true. In Uno, this excludes WinUI Fluent resources (which consumer code accesses via XamlControlsResources)
 /// </summary>
 private bool IsIncludedResource(XamlFileDefinition xamlFileDefinition, XamlGlobalStaticResourcesMap map) => !IsUnoAssembly ||
 _nonSystemResources.None(s => map.GetSourceLink(xamlFileDefinition).EndsWith(s));
예제 #8
0
        private string GenerateGlobalResources(IEnumerable <XamlFileDefinition> files, XamlGlobalStaticResourcesMap map)
        {
            var outputFile = Path.Combine(_targetPath, "GlobalStaticResources.g.cs");

            var writer = new IndentedStringBuilder();

            writer.AppendLineInvariant("// <autogenerated />");
            writer.AppendLineInvariant("#pragma warning disable 618  // Ignore obsolete members warnings");
            writer.AppendLineInvariant("#pragma warning disable 105  // Ignore duplicate namespaces");
            writer.AppendLineInvariant("#pragma warning disable 1591 // Ignore missing XML comment warnings");
            writer.AppendLineInvariant("using System;");
            writer.AppendLineInvariant("using System.Linq;");
            writer.AppendLineInvariant("using System.Collections.Generic;");
            writer.AppendLineInvariant("using Uno.Extensions;");
            writer.AppendLineInvariant("using Uno;");
            writer.AppendLineInvariant("using System.Diagnostics;");

            //TODO Determine the list of namespaces to use
            writer.AppendLineInvariant($"using {XamlConstants.BaseXamlNamespace};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Controls};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Data};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Documents};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.Media};");
            writer.AppendLineInvariant($"using {XamlConstants.Namespaces.MediaAnimation};");
            writer.AppendLineInvariant("using {0};", _defaultNamespace);
            writer.AppendLineInvariant("");

            using (writer.BlockInvariant("namespace {0}", _defaultNamespace))
            {
                writer.AppendLineInvariant("/// <summary>");
                writer.AppendLineInvariant("/// Contains all the static resources defined for the application");
                writer.AppendLineInvariant("/// </summary>");

                AnalyzerSuppressionsGenerator.Generate(writer, _analyzerSuppressions);
                using (writer.BlockInvariant("public sealed partial class GlobalStaticResources"))
                {
                    writer.AppendLineInvariant("static bool _initialized;");
                    writer.AppendLineInvariant("private static bool _stylesRegistered;");
                    writer.AppendLineInvariant("private static bool _dictionariesRegistered;");

                    using (writer.BlockInvariant("internal static {0} {1} {{get; }} = new {0}()", ParseContextPropertyType, ParseContextPropertyName))
                    {
                        writer.AppendLineInvariant("AssemblyName = \"{0}\",", _projectInstance.GetPropertyValue("AssemblyName"));
                    }

                    writer.AppendLineInvariant(";");
                    writer.AppendLine();

                    using (writer.BlockInvariant("static GlobalStaticResources()"))
                    {
                        writer.AppendLineInvariant("Initialize();");
                    }

                    using (writer.BlockInvariant("public static void Initialize()"))
                    {
                        using (writer.BlockInvariant("if (!_initialized)"))
                        {
                            using (IsUnoAssembly ? writer.BlockInvariant("using (ResourceResolver.WriteInitiateGlobalStaticResourcesEventActivity())") : null)
                            {
                                writer.AppendLineInvariant("_initialized = true;");

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    if (ambientResource.GetMethods().Any(m => m.Name == "Initialize"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.Initialize();", ambientResource.GetFullName());
                                    }
                                }

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    // Note: we do *not* call RegisterDefaultStyles for the current assembly, because those styles are treated as implicit styles, not default styles
                                    if (ambientResource.GetMethods().Any(m => m.Name == "RegisterDefaultStyles"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.RegisterDefaultStyles();", ambientResource.GetFullName());
                                    }
                                }

                                foreach (var ambientResource in _ambientGlobalResources)
                                {
                                    if (ambientResource.GetMethods().Any(m => m.Name == "RegisterResourceDictionariesBySource"))
                                    {
                                        writer.AppendLineInvariant("global::{0}.RegisterResourceDictionariesBySource();", ambientResource.GetFullName());
                                    }
                                }

                                if (IsUnoAssembly && _xamlSourceFiles.Any())
                                {
                                    // Build master dictionary
                                    foreach (var dictProperty in map.GetAllDictionaryProperties(_baseResourceDependencies, _nonSystemResources))
                                    {
                                        writer.AppendLineInvariant("MasterDictionary.MergedDictionaries.Add({0});", dictProperty);
                                    }
                                }
                            }
                        }
                    }

                    using (writer.BlockInvariant("public static void RegisterDefaultStyles()"))
                    {
                        using (writer.BlockInvariant("if(!_stylesRegistered)"))
                        {
                            writer.AppendLineInvariant("_stylesRegistered = true;");
                            foreach (var file in files.Where(f => IsIncludedResource(f, map)).Select(f => f.UniqueID).Distinct())
                            {
                                writer.AppendLineInvariant("RegisterDefaultStyles_{0}();", file);
                            }
                        }
                    }

                    writer.AppendLineInvariant("// Register ResourceDictionaries using ms-appx:/// syntax, this is called for external resources");
                    using (writer.BlockInvariant("public static void RegisterResourceDictionariesBySource()"))
                    {
                        using (writer.BlockInvariant("if(!_dictionariesRegistered)"))
                        {
                            writer.AppendLineInvariant("_dictionariesRegistered = true;");

                            if (!IsUnoAssembly && !IsUnoFluentAssembly)
                            {
                                // For third-party libraries, expose all files using standard uri
                                foreach (var file in files.Where(IsResourceDictionary))
                                {
                                    var url = "{0}/{1}".InvariantCultureFormat(_metadataHelper.AssemblyName, map.GetSourceLink(file));
                                    RegisterForFile(file, url);
                                }
                            }
                            else if (files.Any())                             // The NETSTD reference assembly contains no Xaml files
                            {
                                // For Uno assembly, we expose WinUI resources using same uri as on Windows
                                RegisterForFile(files.FirstOrDefault(f => map.GetSourceLink(f).EndsWith(WinUIThemeResourcePathSuffix)), XamlFilePathHelper.WinUIThemeResourceURL);
                                RegisterForFile(files.FirstOrDefault(f => map.GetSourceLink(f).EndsWith(WinUICompactPathSuffix)), XamlFilePathHelper.WinUICompactURL);
                            }

                            void RegisterForFile(XamlFileDefinition file, string url)
                            {
                                if (file != null)
                                {
                                    writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"ms-appx:///{0}\", context: {1}, dictionary: () => {2}_ResourceDictionary);",
                                                               url,
                                                               ParseContextPropertyName,
                                                               file.UniqueID
                                                               );
                                }
                            }
                        }
                    }

                    writer.AppendLineInvariant("// Register ResourceDictionaries using ms-resource:/// syntax, this is called for local resources");
                    using (writer.BlockInvariant("internal static void RegisterResourceDictionariesBySourceLocal()"))
                    {
                        foreach (var file in files.Where(IsResourceDictionary))
                        {
                            // We leave context null because local resources should be found through Application.Resources
                            writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"{0}{1}\", context: null, dictionary: () => {2}_ResourceDictionary);",
                                                       XamlFilePathHelper.LocalResourcePrefix,
                                                       map.GetSourceLink(file),
                                                       file.UniqueID
                                                       );
                            // Local resources can also be found through the ms-appx:/// prefix
                            writer.AppendLineInvariant("global::Uno.UI.ResourceResolver.RegisterResourceDictionaryBySource(uri: \"{0}{1}\", context: null, dictionary: () => {2}_ResourceDictionary);",
                                                       XamlFilePathHelper.AppXIdentifier,
                                                       map.GetSourceLink(file),
                                                       file.UniqueID
                                                       );
                        }
                    }

                    if (IsUnoAssembly)
                    {
                        // Declare master dictionary
                        writer.AppendLine();
                        writer.AppendLineInvariant("internal static ResourceDictionary MasterDictionary {{get; }} = new ResourceDictionary();");
                    }

                    // Generate all the partial methods, even if they don't exist. That avoids
                    // having to sync the generation of the files with this global table.
                    foreach (var file in files.Select(f => f.UniqueID).Distinct())
                    {
                        writer.AppendLineInvariant("static partial void RegisterDefaultStyles_{0}();", file);
                    }

                    writer.AppendLineInvariant("[global::System.Obsolete(\"This method is provided for binary backward compatibility. It will always return null.\")]");
                    writer.AppendLineInvariant("[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]");
                    writer.AppendLineInvariant("public static object FindResource(string name) => null;");

                    writer.AppendLineInvariant("");
                }
            }

            return(writer.ToString());
        }