protected void UnapplyMatchingStylesInternal(TDependencyObject bindableObject, StyleSheet styleSheet) { if (bindableObject == null) { return; } var currentStyleSheet = dependencyPropertyService.GetStyleSheet(bindableObject); if (currentStyleSheet != null && currentStyleSheet != styleSheet) { return; } foreach (var child in treeNodeProvider.GetChildren(bindableObject).ToList()) { UnapplyMatchingStylesInternal(child, styleSheet); } /*var matchingStyles = dependencyPropertyService.GetMatchingStyles(bindableObject) ?? new string[0]; * if (matchingStyles.Length > 0) * { * Debug.WriteLine($"Unapply: {string.Join(", ", dependencyPropertyService.GetMatchingStyles(bindableObject) ?? new string[0])}"); * }*/ dependencyPropertyService.SetHandledCss(bindableObject, false); dependencyPropertyService.SetMatchingStyles(bindableObject, null); dependencyPropertyService.SetAppliedMatchingStyles(bindableObject, null); nativeStyleService.SetStyle(bindableObject, dependencyPropertyService.GetInitialStyle(bindableObject)); }
private static StyleUpdateInfo GetNewStyleUpdateInfo(IDomElement <TDependencyObject> domElement, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService) { if (dependencyPropertyService.GetInitialStyle(domElement.Element) == null) { dependencyPropertyService.SetInitialStyle(domElement.Element, nativeStyleService.GetStyle(domElement.Element)); } return(new StyleUpdateInfo { MatchedType = domElement.Element.GetType() }); }
protected void RemoveOutdatedStylesFromElementInternal(TDependencyObject bindableObject, StyleSheet styleSheet, bool recursive, bool resetStyle) { if (bindableObject == null) { return; } var currentStyleSheet = dependencyPropertyService.GetStyleSheet(bindableObject); if (currentStyleSheet != null && currentStyleSheet != styleSheet) { return; } if (recursive) { foreach (var child in treeNodeProvider.GetChildren(bindableObject).ToList()) { RemoveOutdatedStylesFromElementInternal(child, styleSheet, recursive, resetStyle); } } var appliedMatchingStyles = dependencyPropertyService.GetAppliedMatchingStyles(bindableObject); var matchingStyles = dependencyPropertyService.GetMatchingStyles(bindableObject); if (!AppliedStyleIdsAreMatchedStyleIds(appliedMatchingStyles, matchingStyles)) { dependencyPropertyService.SetAppliedMatchingStyles(bindableObject, null); if (resetStyle) { nativeStyleService.SetStyle(bindableObject, dependencyPropertyService.GetInitialStyle(bindableObject)); } } }
private static void GenerateStyles(StyleSheet styleSheet, IDictionary <TDependencyObject, StyleUpdateInfo> styleMatchInfos, IStyleResourcesService applicationResourcesService, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService, CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper) { applicationResourcesService.EnsureResources(); foreach (var styleMatchInfoKeyValue in styleMatchInfos) { var styleMatchInfo = styleMatchInfoKeyValue.Value; var matchedElementType = styleMatchInfo.MatchedType; for (var i = 0; i < styleMatchInfo.CurrentMatchedSelectors.Count; i++) { var selector = styleMatchInfo.CurrentMatchedSelectors.ElementAt(i); var resourceKey = styleMatchInfo.CurrentMatchedResourceKeys.ElementAt(i); if (applicationResourcesService.Contains(resourceKey)) { // Debug.WriteLine($"GenerateStyles: Already contains '{s}' ({matchedElementType.Name})"); continue; } // Debug.WriteLine($"GenerateStyles: Generating '{s}' ({matchedElementType.Name})"); var rule = styleMatchInfo.CurrentStyleSheet.Rules.Where(x => x.SelectorString == selector.Value).First(); CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> result = null; try { result = CreateStyleDictionaryFromDeclarationBlock( styleSheet.Namespaces, rule.DeclarationBlock, matchedElementType, (TDependencyObject)styleSheet.AttachedTo, cssTypeHelper); var propertyStyleValues = result.PropertyStyleValues; foreach (var error in result.Errors) { // Debug.WriteLine($@" ERROR (normal) in Selector ""{rule.SelectorString}"": {error}"); styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {error}"); } var nativeTriggers = rule.DeclarationBlock.Triggers .Select(x => nativeStyleService.CreateTrigger(styleSheet, x, styleMatchInfo.MatchedType, (TDependencyObject)styleSheet.AttachedTo)) .ToList(); var initalStyle = dependencyPropertyService.GetInitialStyle(styleMatchInfoKeyValue.Key); if (initalStyle != null) { var subDict = nativeStyleService.GetStyleAsDictionary(initalStyle as TStyle); foreach (var item in subDict) { // only set not-overridden properties if (!propertyStyleValues.ContainsKey(item.Key)) { propertyStyleValues[item.Key] = item.Value; } } var triggers = nativeStyleService.GetTriggersAsList(initalStyle as TStyle); nativeTriggers.InsertRange(0, triggers); } //Debug.WriteLine(" Values: " + string.Join(", ", propertyStyleValues.Select(x => ((dynamic)x.Key).PropertyName + ": " + x.Value.ToString()))); var style = nativeStyleService.CreateFrom(propertyStyleValues, nativeTriggers, matchedElementType); applicationResourcesService.SetResource(resourceKey, style); // Debug.WriteLine("Finished generate Style " + resourceKey); } catch (Exception e) { // Debug.WriteLine($@" ERROR (exception) in Selector ""{rule.SelectorString}"": {e.Message}"); styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {e.Message}"); } } } }
private static IList <IDomElement <TDependencyObject, TDependencyProperty> > UpdateMatchingStyles( StyleSheet styleSheet, IDomElement <TDependencyObject, TDependencyProperty> startFrom, Dictionary <TDependencyObject, StyleUpdateInfo> styleUpdateInfos, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService) { // var requiredStyleInfos = new List<StyleMatchInfo>(); var found = new HashSet <IDomElement <TDependencyObject, TDependencyProperty> >(); if (startFrom == null || !startFrom.IsReady) { return(found.ToList()); } if (startFrom.StyleInfo.DoMatchCheck == SelectorType.None) { return(new List <IDomElement <TDependencyObject, TDependencyProperty> >()); } startFrom.XamlCssStyleSheets.Clear(); startFrom.XamlCssStyleSheets.Add(styleSheet); var traversed = SelectorType.None; foreach (var rule in styleSheet.Rules) { // // Debug.WriteLine($"--- RULE {rule.SelectorString} ----"); // apply our selector var type = SelectorType.LogicalTree; if (rule.Selectors[0].StartOnVisualTree()) { type = SelectorType.VisualTree; } if ((type == SelectorType.LogicalTree && !startFrom.IsInLogicalTree) || (type == SelectorType.VisualTree && !startFrom.IsInVisualTree) ) { continue; } traversed |= type; var matchedNodes = startFrom.QuerySelectorAllWithSelf(styleSheet, rule.Selectors[0], type) .Where(x => x != null) .Cast <IDomElement <TDependencyObject, TDependencyProperty> >() .ToList(); var matchedElementTypes = matchedNodes .Select(x => x.Element.GetType()) .Distinct() .ToList(); foreach (var matchingNode in matchedNodes) { var element = matchingNode.Element; if (!found.Contains(matchingNode)) { found.Add(matchingNode); } matchingNode.StyleInfo.CurrentMatchedSelectors.Add(rule.Selectors[0]); var initialStyle = dependencyPropertyService.GetInitialStyle(element); var discriminator = initialStyle != null?initialStyle.GetHashCode().ToString() : ""; var resourceKey = nativeStyleService.GetStyleResourceKey(styleSheet.Id + discriminator, element.GetType(), rule.SelectorString); //var resourceKey = nativeStyleService.GetStyleResourceKey(rule.StyleSheetId, element.GetType(), rule.SelectorString); if (!matchingNode.StyleInfo.CurrentMatchedResourceKeys.Contains(resourceKey)) { matchingNode.StyleInfo.CurrentMatchedResourceKeys.Add(resourceKey); } } } foreach (var f in found) { f.StyleInfo.DoMatchCheck = SelectorType.None; f.StyleInfo.CurrentMatchedSelectors = f.StyleInfo.CurrentMatchedSelectors.Distinct() .OrderBy(x => x.IdSpecificity) .ThenBy(x => x.ClassSpecificity) .ThenBy(x => x.SimpleSpecificity) .ToList() .ToLinkedHashSet(); } if ((traversed & SelectorType.VisualTree) > 0) { SetDoMatchCheckToNoneInSubTree(startFrom, styleSheet, SelectorType.VisualTree); } if ((traversed & SelectorType.LogicalTree) > 0) { SetDoMatchCheckToNoneInSubTree(startFrom, styleSheet, SelectorType.LogicalTree); } return(found.ToList()); }
public static void Render( List <RenderInfo <TDependencyObject> > copy, ITreeNodeProvider <TDependencyObject, TDependencyProperty> treeNodeProvider, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService, IStyleResourcesService applicationResourcesService, CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper) { try { applicationResourcesService.BeginUpdate(); RemoveOldStyleObjects(copy, nativeStyleService, applicationResourcesService); SetAttachedToToNull(copy, dependencyPropertyService, treeNodeProvider, nativeStyleService); SetAttachedToToNewStyleSheet(copy, dependencyPropertyService, treeNodeProvider, nativeStyleService); var styleUpdateInfos = new Dictionary <TDependencyObject, StyleUpdateInfo>(); var newOrUpdatedStyleSheets = copy .Where(x => x.RenderTargetKind == RenderTargetKind.Stylesheet) .Select(x => x.StyleSheet) .Distinct() .ToHashSet(); var newOrUpdatedStyleHolders = copy .Where(x => x.ChangeKind == ChangeKind.New || x.ChangeKind == ChangeKind.Update || x.ChangeKind == ChangeKind.Remove) .Select(x => new { x.ChangeKind, x.StartFrom, x.StyleSheet, x.StyleSheetHolder }) .Distinct() .ToHashSet(); foreach (var item in newOrUpdatedStyleHolders) { var start = item.StartFrom ?? item.StyleSheetHolder; var domElement = treeNodeProvider.GetDomElement(start); if (!domElement.IsInLogicalTree) { continue; } EnsureParents(domElement, treeNodeProvider, dependencyPropertyService, nativeStyleService, styleUpdateInfos, SelectorType.LogicalTree); var discardOldMatchingStyles = newOrUpdatedStyleSheets.Contains(item.StyleSheet); SetupStyleInfo(domElement, item.StyleSheet, styleUpdateInfos, treeNodeProvider, dependencyPropertyService, nativeStyleService, discardOldMatchingStyles, item.ChangeKind == ChangeKind.Remove, SelectorType.LogicalTree); } foreach (var item in newOrUpdatedStyleHolders) { var start = item.StartFrom ?? item.StyleSheetHolder; var domElement = treeNodeProvider.GetDomElement(start); if (!domElement.IsInVisualTree) { continue; } EnsureParents(domElement, treeNodeProvider, dependencyPropertyService, nativeStyleService, styleUpdateInfos, SelectorType.VisualTree); var discardOldMatchingStyles = newOrUpdatedStyleSheets.Contains(item.StyleSheet); SetupStyleInfo(domElement, item.StyleSheet, styleUpdateInfos, treeNodeProvider, dependencyPropertyService, nativeStyleService, discardOldMatchingStyles, item.ChangeKind == ChangeKind.Remove, SelectorType.VisualTree); } var tasks = new List <Task <IList <IDomElement <TDependencyObject, TDependencyProperty> > > >(); var distinctCopy = copy.Select(x => new { x.StartFrom, x.StyleSheetHolder, x.StyleSheet }).Distinct().ToList(); foreach (var item in distinctCopy) { var start = item.StartFrom ?? item.StyleSheetHolder; if (!styleUpdateInfos.ContainsKey(start)) { continue; } //treeNodeProvider.Switch(SelectorType.VisualTree); var domElement = treeNodeProvider.GetDomElement(start); //visual.StyleInfo = styleUpdateInfos[start]; //if (!switchableTreeNodeProvider.IsInTree(start)) //{ // visual = null; //} //var task = Task.Run(() => UpdateMatchingStyles(item.StyleSheet, domElement, styleUpdateInfos, dependencyPropertyService, nativeStyleService)); //tasks.Add(task); tasks.Add(Task.FromResult(UpdateMatchingStyles(item.StyleSheet, domElement, styleUpdateInfos, dependencyPropertyService, nativeStyleService))); } //Task.WaitAll(tasks.ToArray()); var allFound = tasks.SelectMany(x => x.Result).ToList(); var allFoundElements = allFound.Select(x => x.Element).ToHashSet(); var allNotFoundKeys = styleUpdateInfos .Where(x => !allFoundElements.Contains(x.Key)) .ToList(); foreach (var item in allNotFoundKeys) { var styleUpdateInfo = item.Value; // styleUpdateInfo.CurrentMatchedSelectors = new List<string>(); styleUpdateInfo.OldMatchedSelectors = new LinkedHashSet <ISelector>(); styleUpdateInfo.DoMatchCheck = SelectorType.None; // remove style nativeStyleService.SetStyle(item.Key, dependencyPropertyService.GetInitialStyle(item.Key)); } var groups = styleUpdateInfos.Where(x => allFoundElements.Contains(x.Key)).GroupBy(x => x.Value.CurrentStyleSheet).ToList(); foreach (var group in groups) { GenerateStyles( group.Key, group.ToDictionary(x => x.Key, x => x.Value), applicationResourcesService, dependencyPropertyService, nativeStyleService, cssTypeHelper); } foreach (var f in allFound) { ApplyMatchingStylesNode(f, applicationResourcesService, nativeStyleService); } } finally { applicationResourcesService.EndUpdate(); } }
private static void GenerateStyles(StyleSheet styleSheet, IDictionary <TDependencyObject, StyleUpdateInfo> styleMatchInfos, IStyleResourcesService applicationResourcesService, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService, CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper) { applicationResourcesService.EnsureResources(); foreach (var styleMatchInfoKeyValue in styleMatchInfos) { var styleMatchInfo = styleMatchInfoKeyValue.Value; var matchedElementType = styleMatchInfo.MatchedType; foreach (var resourceKey in styleMatchInfo.CurrentMatchedSelectors) { if (applicationResourcesService.Contains(resourceKey)) { continue; } var s = resourceKey.Split('{')[1]; var rule = styleMatchInfo.CurrentStyleSheet.Rules.Where(x => x.SelectorString == s).First(); // // Debug.WriteLine("Generate Style " + resourceKey); CreateStyleDictionaryFromDeclarationBlockResult <TDependencyProperty> result = null; try { result = "CreateStyleDictionaryFromDeclarationBlock".Measure(() => CreateStyleDictionaryFromDeclarationBlock( styleSheet.Namespaces, rule.DeclarationBlock, matchedElementType, (TDependencyObject)styleSheet.AttachedTo, cssTypeHelper)); var propertyStyleValues = result.PropertyStyleValues; foreach (var error in result.Errors) { Debug.WriteLine($@" ERROR (normal) in Selector ""{rule.SelectorString}"": {error}"); styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {error}"); } var nativeTriggers = $"CreateTriggers ({rule.DeclarationBlock.Triggers.Count})".Measure(() => rule.DeclarationBlock.Triggers .Select(x => nativeStyleService.CreateTrigger(styleSheet, x, styleMatchInfo.MatchedType, (TDependencyObject)styleSheet.AttachedTo)) .ToList()); var initalStyle = dependencyPropertyService.GetInitialStyle(styleMatchInfoKeyValue.Key); if (initalStyle != null) { var subDict = nativeStyleService.GetStyleAsDictionary(initalStyle as TStyle); foreach (var i in subDict) { propertyStyleValues[i.Key] = i.Value; } var triggers = nativeStyleService.GetTriggersAsList(initalStyle as TStyle); nativeTriggers.AddRange(triggers); } foreach (var item in propertyStyleValues) { if (item.Value == null) { } } var style = "Create Style".Measure(() => nativeStyleService.CreateFrom(propertyStyleValues, nativeTriggers, matchedElementType)); applicationResourcesService.SetResource(resourceKey, style); // // Debug.WriteLine("Finished generate Style " + resourceKey); } catch (Exception e) { Debug.WriteLine($@" ERROR (exception) in Selector ""{rule.SelectorString}"": {e.Message}"); styleSheet.AddError($@"ERROR in Selector ""{rule.SelectorString}"": {e.Message}"); } } } }
private static IList <IDomElement <TDependencyObject> > UpdateMatchingStyles( StyleSheet styleSheet, IDomElement <TDependencyObject> startFromLogical, IDomElement <TDependencyObject> startFromVisual, Dictionary <TDependencyObject, StyleUpdateInfo> styleUpdateInfos, IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService, INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService) { // var requiredStyleInfos = new List<StyleMatchInfo>(); IDomElement <TDependencyObject> root = null; IDomElement <TDependencyObject> visualTree = null; IDomElement <TDependencyObject> logicalTree = null; var found = new List <IDomElement <TDependencyObject> >(); if (startFromVisual?.StyleInfo.DoMatchCheck == SelectorType.None || startFromLogical?.StyleInfo.DoMatchCheck == SelectorType.None) { return(new List <IDomElement <TDependencyObject> >()); } return($"{startFromLogical?.GetPath() ?? startFromVisual?.GetPath() ?? "NULL!?!"}".Measure(() => { foreach (var rule in styleSheet.Rules) { $"{rule.SelectorString}".Measure(() => { // // Debug.WriteLine($"--- RULE {rule.SelectorString} ----"); if (rule.SelectorType == SelectorType.VisualTree) { if (startFromVisual == null) { //continue; return; } if (visualTree == null) { visualTree = startFromVisual; visualTree?.XamlCssStyleSheets.Clear(); visualTree?.XamlCssStyleSheets.Add(styleSheet); } root = visualTree; } else { if (startFromLogical == null) { //continue; return; } if (logicalTree == null) { logicalTree = startFromLogical; logicalTree?.XamlCssStyleSheets.Clear(); logicalTree?.XamlCssStyleSheets.Add(styleSheet); } root = logicalTree; } if (root == null) { //continue; return; } // apply our selector var matchedNodes = "QuerySelectorAllWithSelf".Measure(() => root.QuerySelectorAllWithSelf(styleSheet, rule.Selectors[0]) .Where(x => x != null) .Cast <IDomElement <TDependencyObject> >() .ToList()); var matchedElementTypes = matchedNodes .Select(x => x.Element.GetType()) .Distinct() .ToList(); $"foreach {matchedNodes.Count}".Measure(() => { foreach (var matchingNode in matchedNodes) { var element = matchingNode.Element; if (!found.Contains(matchingNode)) { found.Add(matchingNode); } var discriminator = "GetInitialStyle".Measure(() => dependencyPropertyService.GetInitialStyle(element) != null ? element.GetHashCode().ToString() : ""); var resourceKey = "GetStyleResourceKey".Measure(() => nativeStyleService.GetStyleResourceKey(styleSheet.Id + discriminator, element.GetType(), rule.SelectorString)); //var resourceKey = nativeStyleService.GetStyleResourceKey(rule.StyleSheetId, element.GetType(), rule.SelectorString); if (!matchingNode.StyleInfo.CurrentMatchedSelectors.Contains(resourceKey)) { matchingNode.StyleInfo.CurrentMatchedSelectors.Add(resourceKey); } } }); }); } "found".Measure(() => { found = found.Distinct().ToList(); foreach (var f in found) { f.StyleInfo.DoMatchCheck = SelectorType.None; f.StyleInfo.CurrentMatchedSelectors = f.StyleInfo.CurrentMatchedSelectors.Distinct().Select(x => new { key = x, SpecificityResult = SpecificityCalculator.Calculate(x.Split('{')[1]) }) .OrderBy(x => x.SpecificityResult.IdSpecificity) .ThenBy(x => x.SpecificityResult.ClassSpecificity) .ThenBy(x => x.SpecificityResult.SimpleSpecificity) .ToList() .Select(x => x.key) .ToList(); } }); "SetDoMatchCheckToNoneInSubTree".Measure(() => { SetDoMatchCheckToNoneInSubTree(startFromLogical, styleSheet); SetDoMatchCheckToNoneInSubTree(startFromVisual, styleSheet); }); return found; })); }