Example #1
0
        protected void RecursivelyConfigureViews(ProcessedNode parentNode, FigmaViewRendererServiceOptions options)
        {
            var children = NodesProcessed.Where(s => s.ParentView == parentNode);

            foreach (var child in children)
            {
                if (child.View == null)
                {
                    Console.WriteLine("Node {0} has no view to process... skipping", child.FigmaNode);
                    continue;
                }

                if (RendersAddChild(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.AddChild, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                if (RendersSize(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.Frame, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                if (RendersConstraints(child, parentNode, this))
                {
                    PropertySetter.Configure(CodeProperties.Constraints, child.View, child.FigmaNode, parentNode.View, parentNode.FigmaNode, this);
                }

                RecursivelyConfigureViews(child, options);
            }
        }
Example #2
0
        public void Start(string figmaName, IView container, FigmaViewRendererServiceOptions options = null)
        {
            if (options == null)
            {
                options = new FigmaViewRendererServiceOptions();
            }

            Console.WriteLine("[FigmaViewRenderer] Starting process..");
            Console.WriteLine($"Reading {figmaName} from resources..");

            this.container = container;

            try
            {
                if (options.LoadFileProvider)
                {
                    fileProvider.Load(figmaName ?? fileProvider.File);
                }

                //we generate all the processed nodes
                Refresh(options);

                //we render only if there is a canvas and GenerateViews is enabled
                var canvas = NodesProcessed.FirstOrDefault(s => s.FigmaNode is FigmaCanvas);
                if (canvas != null && options.ConfigureViews)
                {
                    RecursivelyConfigureViews(canvas, options);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading resource");
                Console.WriteLine(ex);
            }
        }
Example #3
0
        public IView RenderFigmaNode(FigmaNode node, FigmaViewRendererServiceOptions options)
        {
            ProcessFromNode(node, null, options);
            var processedNode = FindProcessedNodeById(node.id);

            Recursively(processedNode);
            return(processedNode.View);
        }
Example #4
0
        public T RenderByName <T> (string figmaName, IView parent, FigmaViewRendererServiceOptions options = null) where T : IView
        {
            var node = FindNodeByName(figmaName);

            if (node == null)
            {
                return(default(T));
            }
            return((T)RenderByNode(node, parent, options));
        }
Example #5
0
        public T RenderByPath <T> (FigmaViewRendererServiceOptions options, IView parent, params string[] path) where T : IView
        {
            FigmaNode node = fileProvider.FindByPath(path);

            if (node == null)
            {
                return(default(T));
            }
            return((T)RenderByNode(node, parent, options));
        }
Example #6
0
        public void RenderInWindow(IWindow mainWindow, string nodeName, FigmaViewRendererServiceOptions options = null)
        {
            var node = fileProvider.Nodes
                       .FirstOrDefault(s => s.name == nodeName || (s.TryGetNodeCustomName(out string name) && name == nodeName));

            if (node == null)
            {
                throw new Exception($"nodename {nodeName} not found");
            }
            RenderInWindow(mainWindow, node, options);
        }
Example #7
0
        public void ProcessFromNode(FigmaNode figmaNode, IView View, FigmaViewRendererServiceOptions options)
        {
            try
            {
                var processedParentView = new ProcessedNode()
                {
                    FigmaNode = figmaNode, View = View
                };
                NodesProcessed.Add(processedParentView);

                //in canvas we want calculate the bounds size
                if (figmaNode is FigmaCanvas canvas)
                {
                    canvas.absoluteBoundingBox = canvas.GetCurrentBounds();
                }

                if (figmaNode is FigmaCanvas || !options.GenerateMainView)
                {
                    if (figmaNode is IFigmaNodeContainer container)
                    {
                        foreach (var item in container.children)
                        {
                            GenerateViewsRecursively(item, processedParentView, options);
                        }
                    }
                }
                else
                {
                    GenerateViewsRecursively(figmaNode, processedParentView, options);
                }

                //Images
                if (options.AreImageProcessed)
                {
                    foreach (var processedNode in NodesProcessed)
                    {
                        if (ProcessesImageFromNode(processedNode.FigmaNode))
                        {
                            ImageVectors.Add(processedNode);
                        }
                    }

                    fileProvider.ImageLinksProcessed += FileProvider_ImageLinksProcessed;
                    fileProvider.OnStartImageLinkProcessing(ImageVectors);
                }

                Console.WriteLine("View generation finished.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading resource");
                Console.WriteLine(ex);
            }
        }
Example #8
0
        public void Refresh(FigmaViewRendererServiceOptions options)
        {
            //on refresh we want clear results
            ImageVectors.Clear();
            NodesProcessed.Clear();

            Console.WriteLine($"Reading successfull");

            var canvas = fileProvider.Response.document.children.FirstOrDefault();

            ProcessFromNode(canvas, container, options);
        }
Example #9
0
        public virtual void RenderInWindow(IWindow mainWindow, FigmaNode node, FigmaViewRendererServiceOptions options = null)
        {
            if (node is IAbsoluteBoundingBox bounNode)
            {
                mainWindow.Size = new Size(bounNode.absoluteBoundingBox.Width, bounNode.absoluteBoundingBox.Height);
            }

            if (options == null)
            {
                options = new FigmaViewRendererServiceOptions();
            }

            ProcessFromNode(node, mainWindow.Content, options);

            var processedNode = FindProcessedNodeById(node.id);

            RecursivelyConfigureViews(processedNode, options);
        }
Example #10
0
        public IView RenderByNode(FigmaNode node, IView parent, FigmaViewRendererServiceOptions options = null)
        {
            if (options == null)
            {
                options = new FigmaViewRendererServiceOptions();
            }

            firstNode = node;

            ProcessFromNode(node, parent, options);
            var processedNode = FindProcessedNodeById(node.id);

            RecursivelyConfigureViews(processedNode, options);

            firstNode = null;

            return(processedNode.View);
        }
        public override void RenderInWindow(IWindow mainWindow, FigmaNode node, FigmaViewRendererServiceOptions options = null)
        {
            if (node is IAbsoluteBoundingBox bounNode)
            {
                mainWindow.Size = new Size(bounNode.absoluteBoundingBox.Width, bounNode.absoluteBoundingBox.Height);
            }

            if (options == null)
            {
                options = new FigmaViewRendererServiceOptions()
                {
                    GenerateMainView = false
                };
            }

            var content = node.GetWindowContent() ?? node;

            ProcessFromNode(content, mainWindow.Content, options);
            var processedNode = FindProcessedNodeById(content.id);

            RecursivelyConfigureViews(processedNode, options);

            var windowComponent = node.GetDialogInstanceFromParentContainer();

            if (windowComponent != null)
            {
                var optionsNode = windowComponent.Options();
                if (optionsNode is IFigmaNodeContainer figmaNodeContainer)
                {
                    mainWindow.IsClosable            = figmaNodeContainer.HasChildrenVisible("close");
                    mainWindow.Resizable             = figmaNodeContainer.HasChildrenVisible("resize");
                    mainWindow.ShowMiniaturizeButton = figmaNodeContainer.HasChildrenVisible("min");
                    mainWindow.ShowZoomButton        = figmaNodeContainer.HasChildrenVisible("max");
                }

                var titleText = optionsNode.FirstChild(s => s.name == "title" && s.visible) as FigmaText;
                if (titleText != null)
                {
                    mainWindow.Title = titleText.characters;
                }
            }
        }
Example #12
0
        public void RenderInWindow(IWindow mainWindow, FigmaViewRendererServiceOptions options = null)
        {
            var allCanvas = fileProvider.Nodes
                            .OfType <FigmaCanvas>()
                            .ToArray();

            if (allCanvas.Length == 0)
            {
                return;
            }

            var startPage = options != null ? options.StartPage : 0;
            var canvas    = allCanvas[startPage];

            if (canvas != null)
            {
                canvas.absoluteBoundingBox = canvas.GetCurrentBounds();
                RenderInWindow(mainWindow, canvas, options);
            }
        }
Example #13
0
        public void Refresh(FigmaViewRendererServiceOptions options)
        {
            //on refresh we want clear results
            ImageVectors.Clear();
            NodesProcessed.Clear();

            Console.WriteLine($"Reading successfull");

            FigmaCanvas canvas;

            if (options.StartPage >= 0 && options.StartPage <= fileProvider.Response.document.children.Length)
            {
                canvas = fileProvider.Response.document.children[options.StartPage];
            }
            else
            {
                canvas = fileProvider.Response.document.children.FirstOrDefault();
            }
            ProcessFromNode(canvas, container, options);
        }
Example #14
0
        public void Start(string file, IView container, FigmaViewRendererServiceOptions options)
        {
            Console.WriteLine("[FigmaRemoteFileService] Starting service process..");
            Console.WriteLine($"Reading {file} from resources..");

            this.container = container;

            try
            {
                if (options.LoadFileProvider)
                {
                    fileProvider.Load(file);
                }
                Refresh(options);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading resource");
                Console.WriteLine(ex);
            }
        }
Example #15
0
 public Task StartAsync(string file, IView container, FigmaViewRendererServiceOptions options) => Task.Run(() => Start(file, container, options: options));
Example #16
0
 public T RenderByNode <T>(FigmaNode node, IView parent, FigmaViewRendererServiceOptions options = null) where T : IView
 {
     return((T)RenderByNode <T>(node, parent, options));
 }
        protected override IEnumerable <FigmaNode> GetCurrentChildren(FigmaNode currentNode, FigmaNode parentNode, CustomViewConverter converter, FigmaViewRendererServiceOptions options)
        {
            var windowContent = currentNode.GetWindowContent();

            if (windowContent != null && windowContent is IFigmaNodeContainer nodeContainer)
            {
                return(nodeContainer.children);
            }
            return(base.GetCurrentChildren(currentNode, parentNode, converter, options));
        }
Example #18
0
 protected virtual bool SkipsNode(FigmaNode currentNode, ProcessedNode parent, FigmaViewRendererServiceOptions options)
 {
     if (options != null && options.ToIgnore != null && options.ToIgnore.Contains(currentNode))
     {
         return(true);
     }
     return(false);
 }
Example #19
0
 public T RenderByNode <T>(FigmaNode node, FigmaViewRendererServiceOptions options) where T : IView
 {
     return((T)RenderFigmaNode(node, options));
 }
Example #20
0
        //TODO: This
        protected void GenerateViewsRecursively(FigmaNode currentNode, ProcessedNode parent, FigmaViewRendererServiceOptions options)
        {
            Console.WriteLine("[{0}.{1}] Processing {2}..", currentNode?.id, currentNode?.name, currentNode?.GetType());

            bool scanChildren = true;

            var converter = GetProcessedConverter(currentNode, CustomConverters);

            if (converter == null)
            {
                converter = GetProcessedConverter(currentNode, DefaultConverters);
            }

            ProcessedNode currentProcessedNode = null;

            if (converter != null)
            {
                var currentView = options.IsToViewProcessed ? converter.ConvertTo(currentNode, parent, this) : null;
                currentProcessedNode = new ProcessedNode()
                {
                    FigmaNode = currentNode, View = currentView, ParentView = parent
                };
                NodesProcessed.Add(currentProcessedNode);

                //TODO: this need to be improved, handles special cases for native controls
                scanChildren = currentNode is FigmaInstance && options.ScanChildrenFromFigmaInstances ? true : converter.ScanChildren(currentNode);
            }
            else
            {
                scanChildren = false;
                Console.WriteLine("[{1}.{2}] There is no Converter for this type: {0}", currentNode.GetType(), currentNode.id, currentNode.name);
            }

            if (scanChildren && currentNode is IFigmaNodeContainer nodeContainer)
            {
                foreach (var item in nodeContainer.children)
                {
                    GenerateViewsRecursively(item, currentProcessedNode ?? parent, options);
                }
            }
        }
 protected override bool SkipsNode(FigmaNode currentNode, ProcessedNode parentNode, FigmaViewRendererServiceOptions options)
 {
     if (currentNode.IsDialog())
     {
         return(true);
     }
     return(false);
 }
Example #22
0
        protected virtual bool NodeScansChildren(FigmaNode currentNode, CustomViewConverter converter, FigmaViewRendererServiceOptions options)
        {
            if (converter == null)
            {
                return(false);
            }

            if (!converter.ScanChildren(currentNode))
            {
                return(false);
            }

            if (!options.ScanChildrenFromFigmaInstances && (currentNode is FigmaInstance || currentNode is FigmaComponentEntity))
            {
                return(false);
            }

            return(true);
        }
Example #23
0
        //TODO: This
        protected void GenerateViewsRecursively(FigmaNode currentNode, ProcessedNode parent, FigmaViewRendererServiceOptions options)
        {
            Console.WriteLine("[{0}.{1}] Processing {2}..", currentNode?.id, currentNode?.name, currentNode?.GetType());

            //if (currentNode.name.StartsWith ("#") || currentNode.name.StartsWith ("//")) {
            //    Console.WriteLine ("[{0}.{1}] Detected skipped flag in name.. Skipping...", currentNode?.id, currentNode?.name, currentNode?.GetType ());
            //    return;
            //}

            if (SkipsNode(currentNode, parent, options))
            {
                return;
            }

            var converter = GetProcessedConverter(currentNode, CustomConverters);

            if (converter == null)
            {
                converter = GetProcessedConverter(currentNode, DefaultConverters);
            }

            ProcessedNode currentProcessedNode = null;

            if (converter != null)
            {
                var currentView = options.IsToViewProcessed ? converter.ConvertTo(currentNode, parent, this) : null;
                currentProcessedNode = new ProcessedNode()
                {
                    FigmaNode = currentNode, View = currentView, ParentView = parent
                };
                NodesProcessed.Add(currentProcessedNode);
            }
            else
            {
                Console.WriteLine("[{1}.{2}] There is no Converter for this type: {0}", currentNode.GetType(), currentNode.id, currentNode.name);
            }

            if (NodeScansChildren(currentNode, converter, options))
            {
                foreach (var item in GetCurrentChildren(currentNode, parent?.FigmaNode, converter, options))
                {
                    GenerateViewsRecursively(item, currentProcessedNode ?? parent, options);
                }
            }
        }
Example #24
0
        public void ProcessFromNode(FigmaNode figmaNode, IView View, FigmaViewRendererServiceOptions options)
        {
            try
            {
                //in canvas we want calculate the bounds size
                if (figmaNode is FigmaCanvas canvas)
                {
                    //var canvas = fileProvider.Response.document.children.FirstOrDefault();
                    var processedParentView = new ProcessedNode()
                    {
                        FigmaNode = figmaNode, View = View
                    };
                    NodesProcessed.Add(processedParentView);

                    Rectangle contentRect = Rectangle.Zero;
                    for (int i = 0; i < canvas.children.Length; i++)
                    {
                        if (canvas.children[i] is IAbsoluteBoundingBox box)
                        {
                            if (i == 0)
                            {
                                contentRect = box.absoluteBoundingBox;
                            }
                            else
                            {
                                contentRect = contentRect.UnionWith(box.absoluteBoundingBox);
                            }
                        }
                    }

                    //figma cambas
                    canvas.absoluteBoundingBox = contentRect;

                    if (figmaNode is IFigmaNodeContainer container)
                    {
                        foreach (var item in container.children)
                        {
                            GenerateViewsRecursively(item, processedParentView, options);
                        }
                    }
                }
                else
                {
                    GenerateViewsRecursively(figmaNode, null, options);
                }

                //Images
                if (options.AreImageProcessed)
                {
                    foreach (var processedNode in NodesProcessed)
                    {
                        if (processedNode.FigmaNode is IFigmaImage figmaImage)
                        {
                            //TODO: this should be replaced by svg
                            if (figmaImage.HasImage())
                            {
                                ImageVectors.Add(processedNode);
                            }
                        }
                    }

                    fileProvider.ImageLinksProcessed += FileProvider_ImageLinksProcessed;
                    fileProvider.OnStartImageLinkProcessing(ImageVectors);
                }

                Console.WriteLine("View generation finished.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error reading resource");
                Console.WriteLine(ex);
            }
        }
Example #25
0
 protected virtual IEnumerable <FigmaNode> GetCurrentChildren(FigmaNode currentNode, FigmaNode parentNode, CustomViewConverter converter, FigmaViewRendererServiceOptions options)
 {
     if (currentNode is IFigmaNodeContainer nodeContainer)
     {
         return(nodeContainer.children);
     }
     return(Enumerable.Empty <FigmaNode>());
 }
        protected override bool NodeScansChildren(FigmaNode currentNode, CustomViewConverter converter, FigmaViewRendererServiceOptions options)
        {
            if (currentNode.IsFigmaImageViewNode())
            {
                return(false);
            }

            return(base.NodeScansChildren(currentNode, converter, options));
        }