Exemple #1
0
        public void SetInlineRule(StyleSheet sheet, StyleRule rule)
        {
            m_InlineRule.sheet       = sheet;
            m_InlineRule.properties  = rule.properties;
            m_InlineRule.propertyIds = StyleSheetCache.GetPropertyIds(rule);

            ApplyInlineStyles(ref ve.computedStyle);
        }
 private void ProcessMatchedVariables(StyleSheet sheet, StyleRule rule)
 {
     foreach (var property in rule.properties)
     {
         if (property.isCustomProperty)
         {
             var sv = new StyleVariable(
                 property.name,
                 sheet,
                 property.values
                 );
             m_ProcessVarContext.Add(sv);
         }
     }
 }
Exemple #3
0
        void ProcessMatchedRules(VisualElement element, List <SelectorMatchRecord> matchingSelectors)
        {
            matchingSelectors.Sort(SelectorMatchRecord.Compare);

            Int64 matchingRulesHash = element.fullTypeName.GetHashCode();

            // Let current DPI contribute to the hash so cache is invalidated when this changes
            matchingRulesHash = (matchingRulesHash * 397) ^ currentPixelsPerPoint.GetHashCode();

            foreach (var record in matchingSelectors)
            {
                StyleRule rule        = record.complexSelector.rule;
                int       specificity = record.complexSelector.specificity;
                matchingRulesHash = (matchingRulesHash * 397) ^ rule.GetHashCode();
                matchingRulesHash = (matchingRulesHash * 397) ^ specificity;
            }

            VisualElementStylesData resolvedStyles;

            if (StyleCache.TryGetValue(matchingRulesHash, out resolvedStyles))
            {
                element.SetSharedStyles(resolvedStyles);
            }
            else
            {
                resolvedStyles = new VisualElementStylesData(isShared: true);

                foreach (var record in matchingSelectors)
                {
                    StylePropertyID[] propertyIDs = StyleSheetCache.GetPropertyIDs(record.sheet, record.complexSelector.ruleIndex);
                    resolvedStyles.ApplyRule(record.sheet, record.complexSelector.specificity, record.complexSelector.rule, propertyIDs);
                }

                resolvedStyles.ApplyLayoutValues();

                StyleCache.SetValue(matchingRulesHash, resolvedStyles);

                element.SetSharedStyles(resolvedStyles);
            }
        }
Exemple #4
0
        private VisualElement CloneSetupRecursively(VisualElementAsset root,
                                                    Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            VisualElement ve = Create(root, context);

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;

            if (context.slotInsertionPoints != null && TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    ve.AddToClassList(root.classes[i]);
                }
            }

            if (root.ruleIndex != -1)
            {
                if (inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule r          = inlineSheet.rules[root.ruleIndex];
                    var       stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    stylesData.ApplyRule(inlineSheet, Int32.MaxValue, r,
                                         StyleSheetCache.GetPropertyIDs(inlineSheet, root.ruleIndex));
                }
            }

            var templateAsset = root as TemplateAsset;
            List <VisualElementAsset> children;

            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }
Exemple #5
0
        private void SetupReferences()
        {
            bool flag = this.complexSelectors == null || this.rules == null;

            if (!flag)
            {
                StyleRule[] rules = this.rules;
                for (int i = 0; i < rules.Length; i++)
                {
                    StyleRule       styleRule  = rules[i];
                    StyleProperty[] properties = styleRule.properties;
                    for (int j = 0; j < properties.Length; j++)
                    {
                        StyleProperty styleProperty = properties[j];
                        bool          flag2         = StyleSheet.CustomStartsWith(styleProperty.name, StyleSheet.kCustomPropertyMarker);
                        if (flag2)
                        {
                            styleRule.customPropertiesCount++;
                            styleProperty.isCustomProperty = true;
                        }
                        StyleValueHandle[] values = styleProperty.values;
                        for (int k = 0; k < values.Length; k++)
                        {
                            StyleValueHandle handle = values[k];
                            bool             flag3  = handle.IsVarFunction();
                            if (flag3)
                            {
                                styleProperty.requireVariableResolve = true;
                                break;
                            }
                        }
                    }
                }
                int l   = 0;
                int num = this.complexSelectors.Length;
                while (l < num)
                {
                    this.complexSelectors[l].CachePseudoStateMasks();
                    l++;
                }
                this.orderedClassSelectors = new Dictionary <string, StyleComplexSelector>(StringComparer.Ordinal);
                this.orderedNameSelectors  = new Dictionary <string, StyleComplexSelector>(StringComparer.Ordinal);
                this.orderedTypeSelectors  = new Dictionary <string, StyleComplexSelector>(StringComparer.Ordinal);
                int m = 0;
                while (m < this.complexSelectors.Length)
                {
                    StyleComplexSelector styleComplexSelector = this.complexSelectors[m];
                    bool flag4 = styleComplexSelector.ruleIndex < this.rules.Length;
                    if (flag4)
                    {
                        styleComplexSelector.rule = this.rules[styleComplexSelector.ruleIndex];
                    }
                    styleComplexSelector.orderInStyleSheet = m;
                    StyleSelector     styleSelector     = styleComplexSelector.selectors[styleComplexSelector.selectors.Length - 1];
                    StyleSelectorPart styleSelectorPart = styleSelector.parts[0];
                    string            key = styleSelectorPart.value;
                    Dictionary <string, StyleComplexSelector> dictionary = null;
                    switch (styleSelectorPart.type)
                    {
                    case StyleSelectorType.Wildcard:
                    case StyleSelectorType.Type:
                        key        = (styleSelectorPart.value ?? "*");
                        dictionary = this.orderedTypeSelectors;
                        break;

                    case StyleSelectorType.Class:
                        dictionary = this.orderedClassSelectors;
                        break;

                    case StyleSelectorType.PseudoClass:
                        key        = "*";
                        dictionary = this.orderedTypeSelectors;
                        break;

                    case StyleSelectorType.RecursivePseudoClass:
                        goto IL_22B;

                    case StyleSelectorType.ID:
                        dictionary = this.orderedNameSelectors;
                        break;

                    default:
                        goto IL_22B;
                    }
IL_249:
                    bool flag5 = dictionary != null;
                    if (flag5)
                    {
                        StyleComplexSelector nextInTable;
                        bool flag6 = dictionary.TryGetValue(key, out nextInTable);
                        if (flag6)
                        {
                            styleComplexSelector.nextInTable = nextInTable;
                        }
                        dictionary[key] = styleComplexSelector;
                    }
                    m++;
                    continue;
IL_22B:
                    Debug.LogError(string.Format("Invalid first part type {0}", styleSelectorPart.type));
                    goto IL_249;
                }
            }
        }
Exemple #6
0
        void ProcessMatchedRules(VisualElement element, List <SelectorMatchRecord> matchingSelectors)
        {
            matchingSelectors.Sort((a, b) => SelectorMatchRecord.Compare(a, b));

            Int64 matchingRulesHash = element.fullTypeName.GetHashCode();

            // Let current DPI contribute to the hash so cache is invalidated when this changes
            matchingRulesHash = (matchingRulesHash * 397) ^ currentPixelsPerPoint.GetHashCode();

            int oldVariablesHash      = m_StyleMatchingContext.variableContext.GetVariableHash();
            int customPropertiesCount = 0;

            foreach (var record in matchingSelectors)
            {
                StyleRule rule        = record.complexSelector.rule;
                int       specificity = record.complexSelector.specificity;
                matchingRulesHash = (matchingRulesHash * 397) ^ rule.GetHashCode();
                matchingRulesHash = (matchingRulesHash * 397) ^ specificity;

                if (rule.customPropertiesCount > 0)
                {
                    customPropertiesCount += rule.customPropertiesCount;
                    ProcessMatchedVariables(record.sheet, rule);
                }
            }

            var parent             = element.hierarchy.parent;
            int inheritedStyleHash = parent != null ? parent.inheritedStylesHash : 0;

            matchingRulesHash = (matchingRulesHash * 397) ^ inheritedStyleHash;

            int variablesHash = oldVariablesHash;

            if (customPropertiesCount > 0)
            {
                // Element defines new variables, add the parents variables at the beginning of the processing context
                m_ProcessVarContext.InsertRange(0, m_StyleMatchingContext.variableContext);
                variablesHash = m_ProcessVarContext.GetVariableHash();
            }
            matchingRulesHash = (matchingRulesHash * 397) ^ variablesHash;

            if (oldVariablesHash != variablesHash)
            {
                StyleVariableContext ctx;
                if (!StyleCache.TryGetValue(variablesHash, out ctx))
                {
                    ctx = new StyleVariableContext(m_ProcessVarContext);
                    StyleCache.SetValue(variablesHash, ctx);
                }

                m_StyleMatchingContext.variableContext = ctx;
            }
            element.variableContext = m_StyleMatchingContext.variableContext;
            m_ProcessVarContext.Clear();

            ComputedStyle resolvedStyles;

            if (StyleCache.TryGetValue(matchingRulesHash, out resolvedStyles))
            {
                element.SetSharedStyles(resolvedStyles);
            }
            else
            {
                var parentStyle = parent?.computedStyle;
                resolvedStyles = ComputedStyle.Create(parentStyle, true);

                float dpiScaling = element.scaledPixelsPerPoint;
                foreach (var record in matchingSelectors)
                {
                    m_StylePropertyReader.SetContext(record.sheet, record.complexSelector, m_StyleMatchingContext.variableContext, dpiScaling);
                    resolvedStyles.ApplyProperties(m_StylePropertyReader, parentStyle);
                }

                resolvedStyles.FinalizeApply(parentStyle);

                StyleCache.SetValue(matchingRulesHash, resolvedStyles);

                element.SetSharedStyles(resolvedStyles);
            }
        }
        void ProcessMatchedRules(VisualElement element, List <SelectorMatchRecord> matchingSelectors)
        {
            matchingSelectors.Sort(SelectorMatchRecord.Compare);

            Int64 matchingRulesHash = element.fullTypeName.GetHashCode();

            // Let current DPI contribute to the hash so cache is invalidated when this changes
            matchingRulesHash = (matchingRulesHash * 397) ^ currentPixelsPerPoint.GetHashCode();

            int oldVariablesHash      = m_StyleMatchingContext.variableContext.GetVariableHash();
            int customPropertiesCount = 0;

            foreach (var record in matchingSelectors)
            {
                StyleRule rule        = record.complexSelector.rule;
                int       specificity = record.complexSelector.specificity;
                matchingRulesHash = (matchingRulesHash * 397) ^ rule.GetHashCode();
                matchingRulesHash = (matchingRulesHash * 397) ^ specificity;

                if (rule.customPropertiesCount > 0)
                {
                    customPropertiesCount += rule.customPropertiesCount;
                    ProcessMatchedVariables(record.sheet, rule);
                }
            }

            int variablesHash = customPropertiesCount > 0 ? m_ProcessVarContext.GetVariableHash() : oldVariablesHash;

            matchingRulesHash = (matchingRulesHash * 397) ^ variablesHash;

            if (oldVariablesHash != variablesHash)
            {
                StyleVariableContext ctx;
                if (!StyleCache.TryGetValue(variablesHash, out ctx))
                {
                    ctx = new StyleVariableContext(m_ProcessVarContext);
                    StyleCache.SetValue(variablesHash, ctx);
                }

                m_StyleMatchingContext.variableContext = ctx;
            }
            element.variableContext = m_StyleMatchingContext.variableContext;

            VisualElementStylesData resolvedStyles;

            if (StyleCache.TryGetValue(matchingRulesHash, out resolvedStyles))
            {
                element.SetSharedStyles(resolvedStyles);
            }
            else
            {
                resolvedStyles = new VisualElementStylesData(isShared: true);

                foreach (var record in matchingSelectors)
                {
                    m_StylePropertyReader.SetContext(record.sheet, record.complexSelector, m_StyleMatchingContext.variableContext);
                    resolvedStyles.ApplyProperties(m_StylePropertyReader, m_StyleMatchingContext.inheritedStyle);
                }

                resolvedStyles.ApplyLayoutValues();

                StyleCache.SetValue(matchingRulesHash, resolvedStyles);

                element.SetSharedStyles(resolvedStyles);
            }
        }