Example #1
0
 /// <summary> Adds the given <paramref name="variant"/> to this style keyed by name. </summary>
 /// <param name="variant"> The variant to add. </param>
 public void AddVariant(StyleVariant variant)
 {
     // Add the variant to the dictionary using its name.
     if (!styleVariantsByName.ContainsKey(variant.Name))
     {
         styleVariantsByName.Add(variant.Name, variant);
     }
     else
     {
         throw new Exception($"Style variant with name {variant.Name} has already been defined for style {Name}.");
     }
 }
Example #2
0
        private StyleVariant(StyleVariant original)
        {
            if (original == null)
            {
                throw new ArgumentNullException(nameof(original));
            }

            Name = original.Name;

            // Copy each attribute.
            foreach (IStyleAttribute attribute in original.styleAttributesByTypeAndName.Values)
            {
                AddAttribute(attribute.CreateCopy());
            }
        }
Example #3
0
 public void CombineOverBase(StyleVariant baseVariant)
 {
     // Go over each attribute in the base.
     foreach (IStyleAttribute baseAttribute in baseVariant.styleAttributesByTypeAndName.Values)
     {
         // If this variant does not have the attribute, take a copy of it as it is.
         string key = calculateKey(baseAttribute.GetType(), baseAttribute);
         if (!styleAttributesByTypeAndName.TryGetValue(key, out IStyleAttribute derivedAttribute))
         {
             AddAttribute(baseAttribute.CreateCopy());
         }
         // Otherwise; combine the derived attribute over the base attribute.
         else
         {
             derivedAttribute.OverrideBaseAttribute(baseAttribute);
         }
     }
 }
Example #4
0
        /// <summary> Creates a style from the given <paramref name="styleNode"/>. </summary>
        /// <param name="styleNode"> The <see cref="XmlNode"/> that contains the element style. </param>
        public Style(ResourceManager resourceManager, ConstructorCache <IStyleAttribute> attributeCache, XmlNode styleNode)
        {
            // Set the name of this style based on the name of the node.
            Name = styleNode.Name;

            // Create attributes from this style node.
            IReadOnlyAttributes attributes = new AttributeCollection(styleNode);

            // Set the name of the base style if one was given.
            BaseStyleName = attributes.GetAttributeOrDefault(BaseVariantName, string.Empty);

            // Hold collections of the loaded variants and attributes.
            List <StyleVariant>    variants        = new List <StyleVariant>();
            List <IStyleAttribute> styleAttributes = new List <IStyleAttribute>();

            // Read each child node.
            foreach (XmlNode childNode in styleNode)
            {
                // If the child node has children, load it as a variant.
                if (childNode.HasChildNodes)
                {
                    variants.Add(new StyleVariant(childNode, resourceManager, attributeCache));
                }
                // Otherwise; dynamically create the style attribute and add it to the list.
                else
                {
                    styleAttributes.Add(attributeCache.CreateInstance(childNode.Name, resourceManager, new AttributeCollection(childNode)));
                }
            }

            // Create the base variant using the loaded attributes.
            BaseVariant = new StyleVariant(BaseVariantName, styleAttributes);
            AddVariant(BaseVariant);

            // Create the derived variants using the base variant.
            foreach (StyleVariant variant in variants)
            {
                // Combine the variant with the base.
                variant.CombineOverBase(BaseVariant);

                // Add the variant to the dictionary using its name.
                AddVariant(variant);
            }
        }
Example #5
0
        private Style(Style original)
        {
            if (original == null)
            {
                throw new ArgumentNullException(nameof(original));
            }

            Name          = original.Name;
            BaseStyleName = original.BaseStyleName;

            // Copy each variant over.
            foreach (StyleVariant variant in original.styleVariantsByName.Values)
            {
                AddVariant(variant.CreateCopy());
            }

            // Set the base variant to the base variant from the newly populated dictionary, ensuring it's the new copy and not the original.
            BaseVariant = styleVariantsByName[BaseVariantName];
        }
Example #6
0
        public void CombineWithBase(Style baseStyle)
        {
            // Go over each style variant in the base style.
            foreach (StyleVariant baseVariant in baseStyle.styleVariantsByName.Values)
            {
                // Try get the variant from this style, if it exists, override the base with it.
                if (styleVariantsByName.TryGetValue(baseVariant.Name, out StyleVariant derivedVariant))
                {
                    derivedVariant.CombineOverBase(baseVariant);
                }
                // Otherwise; copy it from the base to the derived style as-is.
                else
                {
                    // Copy the variant from the base style.
                    StyleVariant baseVariantCopy = baseVariant.CreateCopy();

                    // Combine the copy with the base variant of this style, filling in any gaps.
                    baseVariantCopy.CombineOverBase(BaseVariant);

                    // Add the new copy.
                    AddVariant(baseVariantCopy);
                }
            }
        }