コード例 #1
0
        private static void SetupStyleInfo(
            IDomElement <TDependencyObject> domElement,
            StyleSheet styleSheet,
            Dictionary <TDependencyObject, StyleUpdateInfo> styleUpdateInfos,
            ISwitchableTreeNodeProvider <TDependencyObject> switchableTreeNodeProvider,
            IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
            INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
            bool styleChanged,
            bool styleSheetRemoved)
        {
            var styleUpdateInfo = "get styleUpdateInfo".Measure(() => domElement.StyleInfo = domElement.StyleInfo ?? (styleUpdateInfos.ContainsKey(domElement.Element) ? styleUpdateInfos[domElement.Element] :
                                                                                                                      GetNewStyleUpdateInfo(domElement, dependencyPropertyService, nativeStyleService)));

            var styleSheetFromDom = "GetStyleSheet".Measure(() => dependencyPropertyService.GetStyleSheet(domElement.Element));

            if (styleSheetFromDom != null &&
                styleSheetFromDom != styleSheet)
            {
                // another stylesheet's domelement
                SetupStyleInfo(domElement, styleSheetFromDom, styleUpdateInfos, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, styleChanged, false);
                return;
            }

            "set styleUpdateInfo values".Measure(() =>
            {
                if (!styleSheetRemoved)
                {
                    styleUpdateInfo.CurrentStyleSheet = styleSheet;
                }

                if (styleChanged)
                {
                    styleUpdateInfo.OldMatchedSelectors = new List <string>();
                }
                styleUpdateInfo.CurrentMatchedSelectors.Clear();
                styleUpdateInfo.DoMatchCheck |= switchableTreeNodeProvider.CurrentSelectorType;

                styleUpdateInfos[domElement.Element] = styleUpdateInfo;
            });

            "fill DomElement".Measure(() =>
            {
                object a;
                "ClassList".Measure(() => a    = domElement.ClassList);
                "Id".Measure(() => a           = domElement.Id);
                "TagName".Measure(() => a      = domElement.TagName);
                "NamespaceUri".Measure(() => a = domElement.AssemblyQualifiedNamespaceName);
                //"HasAttribute".Measure(() => a = domElement.HasAttribute("Name"));

                /*// a = domElement.Parent;
                 */
            });

            foreach (var child in domElement.ChildNodes)
            {
                SetupStyleInfo(child, styleSheet, styleUpdateInfos, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, styleChanged, styleSheetRemoved);
            }
        }
コード例 #2
0
        public BaseCss(IDependencyPropertyService <TDependencyObject, TUIElement, TStyle, TDependencyProperty> dependencyPropertyService,
                       ISwitchableTreeNodeProvider <TDependencyObject> treeNodeProvider,
                       IStyleResourcesService applicationResourcesService,
                       INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
                       string defaultCssNamespace,
                       IMarkupExtensionParser markupExpressionParser,
                       Action <Action> uiInvoker,
                       ICssFileProvider fileProvider)
        {
            this.dependencyPropertyService   = dependencyPropertyService;
            this.treeNodeProvider            = treeNodeProvider;
            this.applicationResourcesService = applicationResourcesService;
            this.nativeStyleService          = nativeStyleService;
            this.markupExpressionParser      = markupExpressionParser;
            this.uiInvoker     = uiInvoker;
            this.cssTypeHelper = new CssTypeHelper <TDependencyObject, TUIElement, TDependencyProperty, TStyle>(markupExpressionParser, dependencyPropertyService);

            CssParser.Initialize(defaultCssNamespace, fileProvider);
            StyleSheet.GetParent     = parent => treeNodeProvider.GetParent((TDependencyObject)parent);
            StyleSheet.GetStyleSheet = treeNode => dependencyPropertyService.GetStyleSheet((TDependencyObject)treeNode);
        }
コード例 #3
0
        private static void SetAttachedToToNull(List <RenderInfo <TDependencyObject> > copy,
                                                IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
                                                ISwitchableTreeNodeProvider <TDependencyObject> switchableTreeNodeProvider,
                                                INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService)
        {
            var removedStyleSheets = copy
                                     .Where(x =>
                                            x.RenderTargetKind == RenderTargetKind.Stylesheet &&
                                            x.ChangeKind == ChangeKind.Remove)
                                     .Select(x => x.StyleSheet)
                                     .Distinct()
                                     .ToList();

            foreach (var removedStyleSheet in removedStyleSheets)
            {
                switchableTreeNodeProvider.Switch(SelectorType.LogicalTree);
                ReevaluateStylesheetInSubTree(switchableTreeNodeProvider.GetDomElement((TDependencyObject)removedStyleSheet.AttachedTo), removedStyleSheet, dependencyPropertyService, nativeStyleService);

                removedStyleSheet.AttachedTo = null;
            }
        }
コード例 #4
0
        private static void SetAttachedToToNewStyleSheet(List <RenderInfo <TDependencyObject> > copy,
                                                         IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
                                                         ISwitchableTreeNodeProvider <TDependencyObject> switchableTreeNodeProvider,
                                                         INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService)
        {
            var addedStyleSheets = copy
                                   .Where(x => x.RenderTargetKind == RenderTargetKind.Stylesheet &&
                                          x.ChangeKind == ChangeKind.New)
                                   .Select(x => new { x.StyleSheet, x.StyleSheetHolder })
                                   .Distinct()
                                   .ToList();

            foreach (var item in addedStyleSheets)
            {
                var domElement = switchableTreeNodeProvider.GetDomElement(item.StyleSheetHolder);

                item.StyleSheet.AttachedTo = item.StyleSheetHolder;

                ReevaluateStylesheetInSubTree(domElement, domElement.StyleInfo?.CurrentStyleSheet, dependencyPropertyService, nativeStyleService);
            }
        }
コード例 #5
0
        private static void EnsureParents(IDomElement <TDependencyObject> domElement, ISwitchableTreeNodeProvider <TDependencyObject> switchableTreeNodeProvider,
                                          IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
                                          INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
                                          IDictionary <TDependencyObject, StyleUpdateInfo> styleUpdateInfos)
        {
            var current = domElement.Parent;

            while (current != null)
            {
                var styleUpdateInfo = current.StyleInfo = current.StyleInfo ?? (styleUpdateInfos.ContainsKey(current.Element) ? styleUpdateInfos[current.Element] :
                                                                                GetNewStyleUpdateInfo(current, dependencyPropertyService, nativeStyleService));

                if ((styleUpdateInfo.DoMatchCheck & switchableTreeNodeProvider.CurrentSelectorType) == switchableTreeNodeProvider.CurrentSelectorType)
                {
                    return;
                }

                object a;
                "ClassList".Measure(() => a = current.ClassList);
                //"Id".Measure(() => a = current.Id);
                "TagName".Measure(() => a = current.TagName);
                "AssemblyQualifiedNamespaceName".Measure(() => a = current.AssemblyQualifiedNamespaceName);
                //"HasAttribute".Measure(() => a = current.HasAttribute("Name"));

                /*// a = domElement.Parent;
                 */

                a = current.ChildNodes;

                current = current.Parent;
            }
        }
コード例 #6
0
        public static void Render(
            List <RenderInfo <TDependencyObject> > copy,
            ISwitchableTreeNodeProvider <TDependencyObject> switchableTreeNodeProvider,
            IDependencyPropertyService <TDependencyObject, TStyle, TDependencyProperty> dependencyPropertyService,
            INativeStyleService <TStyle, TDependencyObject, TDependencyProperty> nativeStyleService,
            IStyleResourcesService applicationResourcesService,
            CssTypeHelper <TDependencyObject, TDependencyProperty, TStyle> cssTypeHelper)
        {
            try
            {
                "BeginUpdate".Measure(() => applicationResourcesService.BeginUpdate());

                "RemoveOldStyleObjects".Measure(() =>
                {
                    RemoveOldStyleObjects(copy, nativeStyleService, applicationResourcesService);
                });
                "SetAttachedToToNull".Measure(() =>
                {
                    SetAttachedToToNull(copy, dependencyPropertyService, switchableTreeNodeProvider, nativeStyleService);
                });
                "SetAttachedToToNewStyleSheet".Measure(() =>
                {
                    SetAttachedToToNewStyleSheet(copy, dependencyPropertyService, switchableTreeNodeProvider, nativeStyleService);
                });

                var styleUpdateInfos = new Dictionary <TDependencyObject, StyleUpdateInfo>();

                var newOrUpdatedStyleSheets = copy
                                              .Where(x =>
                                                     x.RenderTargetKind == RenderTargetKind.Stylesheet)
                                              .Select(x => x.StyleSheet)
                                              .Distinct()
                                              .ToList();

                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()
                                               .ToList();

                "SetupStyleInfo LOGICAL".Measure(() =>
                {
                    switchableTreeNodeProvider.Switch(SelectorType.LogicalTree);

                    foreach (var item in newOrUpdatedStyleHolders)
                    {
                        var start = item.StartFrom ?? item.StyleSheetHolder;
                        if (!switchableTreeNodeProvider.IsInTree(start))
                        {
                            continue;
                        }
                        var domElement = switchableTreeNodeProvider.GetDomElement(start);

                        "EnsureParents".Measure(() =>
                        {
                            EnsureParents(domElement, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, styleUpdateInfos);
                        });

                        SetupStyleInfo(domElement, item.StyleSheet, styleUpdateInfos, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, false, item.ChangeKind == ChangeKind.Remove);
                    }
                });

                "SetupStyleInfo VisualTree".Measure(() =>
                {
                    switchableTreeNodeProvider.Switch(SelectorType.VisualTree);
                    foreach (var item in newOrUpdatedStyleHolders)
                    {
                        var start = item.StartFrom ?? item.StyleSheetHolder;
                        if (!switchableTreeNodeProvider.IsInTree(start))
                        {
                            continue;
                        }

                        var domElement = switchableTreeNodeProvider.GetDomElement(start);

                        "EnsureParents".Measure(() =>
                        {
                            EnsureParents(domElement, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, styleUpdateInfos);
                        });

                        var discardOldMatchingStyles = newOrUpdatedStyleSheets.Contains(item.StyleSheet);
                        SetupStyleInfo(domElement, item.StyleSheet, styleUpdateInfos, switchableTreeNodeProvider, dependencyPropertyService, nativeStyleService, discardOldMatchingStyles, item.ChangeKind == ChangeKind.Remove);
                    }
                });

                var tasks = new List <Task <IList <IDomElement <TDependencyObject> > > >();
                foreach (var item in copy.Select(x => new { x.StartFrom, x.StyleSheetHolder, x.StyleSheet }).Distinct().ToList())
                {
                    var start = item.StartFrom ?? item.StyleSheetHolder;

                    if (!styleUpdateInfos.ContainsKey(start))
                    {
                        continue;
                    }

                    switchableTreeNodeProvider.Switch(SelectorType.LogicalTree);
                    var logical = switchableTreeNodeProvider.GetDomElement(start);
                    logical.StyleInfo = styleUpdateInfos[start];

                    if (!switchableTreeNodeProvider.IsInTree(start))
                    {
                        logical = null;
                    }

                    switchableTreeNodeProvider.Switch(SelectorType.VisualTree);
                    var visual = switchableTreeNodeProvider.GetDomElement(start);
                    visual.StyleInfo = styleUpdateInfos[start];

                    if (!switchableTreeNodeProvider.IsInTree(start))
                    {
                        visual = null;
                    }

                    "UpdateMatchingStyles".Measure(() =>
                    {
                        //var task = Task.Run(() =>
                        //{
                        tasks.Add(Task.FromResult(UpdateMatchingStyles(item.StyleSheet, logical, visual, styleUpdateInfos, dependencyPropertyService, nativeStyleService)));
                        //return UpdateMatchingStyles(item.StyleSheet, logical, visual, styleUpdateInfos, dependencyPropertyService, nativeStyleService);

                        //});
                        //tasks.Add(task);
                    });
                }

                //Task.WaitAll(tasks.ToArray());
                var allFound         = tasks.SelectMany(x => x.Result).ToList();
                var allFoundElements = allFound.Select(x => x.Element).ToList();
                "allNotFoundElements".Measure(() =>
                {
                    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 List <string>();
                        styleUpdateInfo.DoMatchCheck        = SelectorType.None;
                        // remove style
                        nativeStyleService.SetStyle(item.Key, dependencyPropertyService.GetInitialStyle(item.Key));
                    }
                });

                "GenerateStyles".Measure(() =>
                {
                    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);
                    }
                });
                "ApplyMatchingStyles".Measure(() =>
                {
                    foreach (var item in copy.Select(x => new { x.StartFrom, x.StyleSheetHolder, x.StyleSheet }).Distinct().ToList())
                    {
                        var start = item.StartFrom ?? item.StyleSheetHolder;

                        switchableTreeNodeProvider.Switch(SelectorType.LogicalTree);

                        if (switchableTreeNodeProvider.IsInTree(start))
                        {
                            var logical = switchableTreeNodeProvider.GetDomElement(start);
                            ApplyMatchingStyles(logical, item.StyleSheet, applicationResourcesService, nativeStyleService);
                        }

                        switchableTreeNodeProvider.Switch(SelectorType.VisualTree);
                        if (switchableTreeNodeProvider.IsInTree(start))
                        {
                            var visual = switchableTreeNodeProvider.GetDomElement(start);

                            ApplyMatchingStyles(visual, item.StyleSheet, applicationResourcesService, nativeStyleService);
                        }
                    }
                });
            }
            finally
            {
                "EndUpdate".Measure(() => applicationResourcesService.EndUpdate());
            }
        }