Esempio n. 1
0
        internal static Visual OptimizeContainers(Visual root)
        {
            var graph = ObjectGraph <Node> .FromCompositionObject(root, includeVertices : true);

            // Discover the parents of each container
            foreach (var node in graph.CompositionObjectNodes)
            {
                switch (node.Object.Type)
                {
                case CompositionObjectType.CompositionContainerShape:
                case CompositionObjectType.ShapeVisual:
                    foreach (var child in ((IContainShapes)node.Object).Shapes)
                    {
                        graph[child].Parent = node.Object;
                    }

                    break;

                case CompositionObjectType.ContainerVisual:
                    foreach (var child in ((ContainerVisual)node.Object).Children)
                    {
                        graph[child].Parent = node.Object;
                    }

                    break;
                }
            }

            RemoveTransparentShapes(graph);
            RemoveEmptyContainers(graph);
            SimplifyProperties(graph);
            CoalesceContainerShapes(graph);
            CoalesceContainerVisuals(graph);
            return(root);
        }
        internal void SetRootVisual(WinCompData.Visual rootVisual)
        {
            // Save the root visual.
            _wincompDataRootVisual = rootVisual;

            // Find the theming property set, if any.
            var graph = ObjectGraph <Graph.Node> .FromCompositionObject(_wincompDataRootVisual, includeVertices : false);

            _wincompDataThemingPropertySet = graph.
                                             CompositionObjectNodes.
                                             Where(n => n.Object is WinCompData.CompositionPropertySet cps && cps.Owner is null).
                                             Select(n => (WinCompData.CompositionPropertySet)n.Object).FirstOrDefault();
        }
Esempio n. 3
0
        public static Visual Optimize(Visual root, bool ignoreCommentProperties)
        {
            // Build the object graph.
            var graph = ObjectGraph <ObjectData> .FromCompositionObject(root, includeVertices : true);

            // Find the canonical objects in the graph.
            Canonicalizer.Canonicalize(graph, ignoreCommentProperties: ignoreCommentProperties);

            // Create a copy of the WinCompData objects from the canonical objects.
            // The copy is needed so that we can modify the tree without affecting the graph that
            // was given to us.
            var result = (Visual) new Optimizer(graph).GetCompositionObject(root);

            AssertGraphsAreDisjoint(result, root);

            // Try to optimize away redundant containers.
            result = TreeReducer.OptimizeContainers(result);

            return(result);
        }
Esempio n. 4
0
        XDocument ToXDocument(CompositionObject compositionObject)
        {
            // Build the graph of objects.
            _objectGraph = ObjectGraph <ObjectData> .FromCompositionObject(compositionObject, includeVertices : true);

            // Give names to each object.
            foreach ((var node, var name) in CodeGen.NodeNamer <ObjectData> .GenerateNodeNames(_objectGraph.Nodes))
            {
                node.Name = name;
            }

            // Initialize each node.
            foreach (var n in _objectGraph.Nodes)
            {
                n.Initialize(this);
            }

            // Second stage initialization - relies on all nodes having had the first stage of initialization.
            foreach (var n in _objectGraph.Nodes)
            {
                n.Initialize2();
            }

            var rootNode = _objectGraph[compositionObject];

            // Give the root object a special name and category.
            rootNode.Name     = $"{rootNode.Name} (Root)";
            rootNode.Category = CategoryRoot;

            // Get the groups.
            var groups = GroupTree(rootNode, null).ToArray();

            // Get the Nodes for the objects that are going to show up in the DGML.
            var objectNodes = _objectGraph.Nodes.Where(n => n.IsDgmlNode).ToArray();

            // Create the DGML nodes.
            var nodes =
                from n in objectNodes
                select CreateNodeXml(id : n.Id, label : n.Name, category : n.Category.Id);

            // Create the DGML nodes for the groups.
            nodes = nodes.Concat(
                from gn in groups
                select CreateNodeXml(id: gn.Id, label: gn.GroupName, @group: "Expanded"));

            // Create the categories used by object nodes.
            var categories =
                (from n in objectNodes
                 select n.Category).Distinct();

            // Create the links between the nodes.
            var links =
                from n in objectNodes
                from otherNode in n.Children
                select new XElement(ns + "Link", new XAttribute("Source", n.Id), new XAttribute("Target", otherNode.Id));

            // Create the "contains" links for the nodes contained in groups.
            var containsLinks =
                (from g in groups
                 from member in g.ItemsInGroup
                 select new XElement(ns + "Link", new XAttribute("Source", g.Id), new XAttribute("Target", member.Id), new XAttribute("Category", "Contains"))).ToArray();

            // Create the "contains" links for the groups contained in groups
            var groupContainsGroupsLinks =
                (from g in groups
                 from member in g.GroupsInGroup
                 select new XElement(ns + "Link", new XAttribute("Source", g.Id), new XAttribute("Target", member.Id), new XAttribute("Category", "Contains"))).ToArray();

            containsLinks = containsLinks.Concat(groupContainsGroupsLinks).ToArray();

            // Create the XML
            return(new XDocument(
                       new XElement(
                           ns + "DirectedGraph",
                           new XElement(ns + "Nodes", nodes),
                           new XElement(ns + "Links", links.Concat(containsLinks)),
                           new XElement(
                               ns + "Categories",
                               categories.Select(c => c.ToXElement()).Append(
                                   new XElement(
                                       ns + "Category",
                                       new XAttribute("Id", "Contains"),
                                       new XAttribute("Label", "Contains"),
                                       new XAttribute("Description", "Whether the source of the link contains the target object"),
                                       new XAttribute("CanBeDataDriven", "False"),
                                       new XAttribute("CanLinkedNodesBeDataDriven", "True"),
                                       new XAttribute("IncomingActionLabel", "Contained By"),
                                       new XAttribute("IsContainment", "True"),
                                       new XAttribute("OutgoingActionLabel", "Contains")))
                               ),
                           new XElement(
                               ns + "Properties",
                               CreatePropertyXml(id: "Bounds", dataType: "System.Windows.Rect"),
                               CreatePropertyXml(id: "CanBeDataDriven", label: "CanBeDataDriven", description: "CanBeDataDriven", dataType: "System.Boolean"),
                               CreatePropertyXml(id: "CanLinkedNodesBeDataDriven", label: "CanLinkedNodesBeDataDriven", description: "CanLinkedNodesBeDataDriven", dataType: "System.Boolean"),
                               CreatePropertyXml(id: "Group", label: "Group", description: "Display the node as a group", dataType: "Microsoft.VisualStudio.GraphModel.GraphGroupStyle"),
                               CreatePropertyXml(id: "IncomingActionLabel", label: "IncomingActionLabel", description: "IncomingActionLabel", dataType: "System.String"),
                               CreatePropertyXml(id: "IsContainment", dataType: "System.Boolean"),
                               CreatePropertyXml(id: "Label", label: "Label", description: "Displayable label of an Annotatable object", dataType: "System.String"),
                               CreatePropertyXml(id: "Layout", dataType: "System.String"),
                               CreatePropertyXml(id: "OutgoingActionLabel", label: "OutgoingActionLabel", description: "OutgoingActionLabel", dataType: "System.String"),
                               CreatePropertyXml(id: "UseManualLocation", dataType: "System.Boolean"),
                               CreatePropertyXml(id: "ZoomLevel", dataType: "System.String")
                               )
                           )
                       ));
        }