Ejemplo n.º 1
0
        protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService)
        {
            var textField = new NSTextField();

            var frame = (FigmaFrame)currentNode;

            frame.TryGetNativeControlType(out var controlType);
            frame.TryGetNativeControlVariant(out var controlVariant);


            if (controlType == FigmaControlType.SearchField)
            {
                textField = new NSSearchField();
            }


            FigmaNode optionsGroup = frame.Options();

            FigmaNode passwordNode = optionsGroup?.GetChildren()
                                     .OfType <FigmaNode>()
                                     .FirstOrDefault(s => s.name == ComponentString.PASSWORD && s.visible);

            if (passwordNode != null)
            {
                textField             = new NSSecureTextField();
                textField.StringValue = "Password";
            }


            FigmaText placeholderText = optionsGroup?.GetChildren()
                                        .OfType <FigmaText>()
                                        .FirstOrDefault(s => s.name == ComponentString.PLACEHOLDER && s.visible);

            if (placeholderText != null && !placeholderText.characters.Equals(ComponentString.PLACEHOLDER, StringComparison.InvariantCultureIgnoreCase))
            {
                textField.PlaceholderString = rendererService.GetTranslatedText(placeholderText);
            }

            FigmaText text = frame.children
                             .OfType <FigmaText> ()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE && s.visible);

            if (text != null)
            {
                textField.Alignment   = ViewHelper.GetNSTextAlignment(text);
                textField.StringValue = rendererService.GetTranslatedText(text);
            }

            textField.ControlSize = ViewHelper.GetNSControlSize(controlVariant);
            textField.Font        = ViewHelper.GetNSFont(controlVariant);

            return(new View(textField));
        }
Ejemplo n.º 2
0
        public static void Configure(this StringBuilder builder, string name, FigmaText text)
        {
            Configure(builder, name, (FigmaNode)text);

            var alignment = FigmaExtensions.ToNSTextAlignment(text.style.textAlignHorizontal);

            builder.AppendLine(string.Format("{0}.Alignment = {1};", name, alignment.ToDesignerString()));
            builder.AppendLine(string.Format("{0}.AlphaValue = {1};", name, text.opacity.ToDesignerString()));

            //label.LineBreakMode = NSLineBreakMode.ByWordWrapping;
            //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);

            var fills = text.fills.FirstOrDefault() as FigmaPaint;

            if (fills != null)
            {
                builder.AppendLine(string.Format("{0}.TextColor = {1};", name, fills.color.ToDesignerString()));
            }

            if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
            {
                var attributedTextName = "attributedText" + DateTime.Now.ToString("HHmmss");
                builder.AppendLine(string.Format("var {0} = new NSMutableAttributedString({1}.AttributedStringValue);", attributedTextName, name));

                //var attributedText = new NSMutableAttributedString(label.AttributedStringValue);
                for (int i = 0; i < text.characterStyleOverrides.Length; i++)
                {
                    var key = text.characterStyleOverrides[i].ToString();
                    if (!text.styleOverrideTable.ContainsKey(key))
                    {
                        continue;
                    }
                    var element = text.styleOverrideTable[key];
                    if (element.fontFamily == null)
                    {
                        continue;
                    }
                    builder.AppendLine(string.Format("{0}.AddAttribute(NSStringAttributeKey.Font, {1}, new NSRange({2}, 1));", attributedTextName, element.ToNSFontDesignerString(), i));

                    string color = fills?.color?.ToDesignerString();

                    if (element.fills != null && element.fills.Any())
                    {
                        if (element.fills.FirstOrDefault() is FigmaPaint paint)
                        {
                            color = paint.color.ToDesignerString();
                        }
                        builder.AppendLine(string.Format("{0}.AddAttribute(NSStringAttributeKey.ForegroundColor, {1}, new NSRange({2}, 1));", attributedTextName, color, i));
                    }
                }
                builder.AppendLine(string.Format("{0}.AttributedStringValue = {1};", name, attributedTextName));
            }
        }
Ejemplo n.º 3
0
        protected override IView OnConvertToView(FigmaNode currentNode, ProcessedNode parentNode, FigmaRendererService rendererService)
        {
            var button = new NSButton();

            var frame = (FigmaFrame)currentNode;

            frame.TryGetNativeControlType(out var controlType);
            frame.TryGetNativeControlVariant(out var controlVariant);

            switch (controlType)
            {
            case NativeControlType.Button:
                button.BezelStyle = NSBezelStyle.Rounded;
                break;

            case NativeControlType.ButtonHelp:
                button.BezelStyle = NSBezelStyle.HelpButton;
                button.Title      = string.Empty;
                break;
            }

            button.ControlSize = CocoaHelpers.GetNSControlSize(controlVariant);
            button.Font        = CocoaHelpers.GetNSFont(controlVariant);

            FigmaGroup group = frame.children
                               .OfType <FigmaGroup>()
                               .FirstOrDefault(s => s.visible);

            if (group != null)
            {
                FigmaText text = group.children
                                 .OfType <FigmaText>()
                                 .FirstOrDefault(s => s.name == ComponentString.TITLE);

                if (text != null && controlType != NativeControlType.ButtonHelp)
                {
                    button.Title = text.characters;
                }

                if (group.name == ComponentString.STATE_DISABLED)
                {
                    button.Enabled = false;
                }

                if (group.name == ComponentString.STATE_DEFAULT)
                {
                    button.KeyEquivalent = "\r";
                }
            }

            return(new View(button));
        }
        public static void Configure(this Label label, FigmaText text)
        {
            Configure(label, (FigmaNode)text);

            label.HorizontalTextAlignment = text.style.textAlignHorizontal == "CENTER" ? TextAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? TextAlignment.Start : TextAlignment.End;
            label.Opacity = text.opacity;
            label.VerticalTextAlignment = text.style.textAlignVertical == "CENTER" ? TextAlignment.Center : text.style.textAlignHorizontal == "TOP" ? TextAlignment.Start : TextAlignment.End;

            if (text.HasFills)
            {
                label.TextColor = text.fills[0].color.ToFormsColor();
            }
        }
Ejemplo n.º 5
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, CodeRenderService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            code.WriteMethod(name, nameof(NSButton.SetButtonType), NSButtonType.Radio);

            code.WriteEquality(name, nameof(NSButton.ControlSize), ViewHelper.GetNSControlSize(controlVariant));
            code.WriteEquality(name, nameof(NSSegmentedControl.Font), CodeHelper.GetNSFontString(controlVariant));

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault();

            if (text != null)
            {
                var labelTranslated = CodeHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);

                code.WriteEquality(name, nameof(NSButton.Title), labelTranslated,
                                   inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
            }

            FigmaGroup group = frame.children
                               .OfType <FigmaGroup>()
                               .FirstOrDefault(s => (s.name == ComponentString.STATE_ON || s.name == ComponentString.STATE_OFF) && s.visible);

            if (group != null)
            {
                if (group.name == ComponentString.STATE_ON)
                {
                    code.WriteEquality(name, nameof(NSButton.State), NSCellStateValue.On);
                }

                if (group.name == ComponentString.STATE_OFF)
                {
                    code.WriteEquality(name, nameof(NSButton.State), NSCellStateValue.Off);
                }
            }

            return(code);
        }
Ejemplo n.º 6
0
        public static string GetNSFontWeightString(FigmaText text)
        {
            // The default macOS font is of medium weight
            string weight   = nameof(NSFontWeight.Medium);
            string fontName = text?.style?.fontPostScriptName;

            if (fontName != null)
            {
                if (fontName.EndsWith("-Black"))
                {
                    weight = nameof(NSFontWeight.Black);
                }

                if (fontName.EndsWith("-Heavy"))
                {
                    weight = nameof(NSFontWeight.Heavy);
                }

                if (fontName.EndsWith("-Bold"))
                {
                    weight = nameof(NSFontWeight.Bold);
                }

                if (fontName.EndsWith("-Semibold"))
                {
                    weight = nameof(NSFontWeight.Semibold);
                }

                if (fontName.EndsWith("-Regular"))
                {
                    weight = nameof(NSFontWeight.Regular);
                }

                if (fontName.EndsWith("-Light"))
                {
                    weight = nameof(NSFontWeight.Light);
                }

                if (fontName.EndsWith("-Thin"))
                {
                    weight = nameof(NSFontWeight.Thin);
                }

                if (fontName.EndsWith("-Ultralight"))
                {
                    weight = nameof(NSFontWeight.UltraLight);
                }
            }

            return($"{ typeof(NSFontWeight) }.{ weight }");
        }
Ejemplo n.º 7
0
        public static nfloat GetNSFontWeight(FigmaText text)
        {
            string fontName = text?.style?.fontPostScriptName;

            if (fontName != null)
            {
                if (fontName.EndsWith("-Black"))
                {
                    return(NSFontWeight.Black);
                }

                if (fontName.EndsWith("-Heavy"))
                {
                    return(NSFontWeight.Heavy);
                }

                if (fontName.EndsWith("-Bold"))
                {
                    return(NSFontWeight.Bold);
                }

                if (fontName.EndsWith("-Semibold"))
                {
                    return(NSFontWeight.Semibold);
                }

                if (fontName.EndsWith("-Regular"))
                {
                    return(NSFontWeight.Regular);
                }

                if (fontName.EndsWith("-Light"))
                {
                    return(NSFontWeight.Light);
                }

                if (fontName.EndsWith("-Thin"))
                {
                    return(NSFontWeight.Thin);
                }

                if (fontName.EndsWith("-Ultralight"))
                {
                    return(NSFontWeight.UltraLight);
                }
            }

            // The default macOS font is of medium weight
            return(NSFontWeight.Medium);
        }
Ejemplo n.º 8
0
        protected override StringBuilder OnConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out NativeControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            FigmaNode optionsGroup = frame.Options();

            FigmaNode passwordNode = optionsGroup?.GetChildren()
                                     .OfType <FigmaNode>()
                                     .FirstOrDefault(s => s.name == ComponentString.PASSWORD && s.visible);

            if (passwordNode != null)
            {
                code.WriteEquality(name, nameof(NSSecureTextField.StringValue), ComponentString.PASSWORD, inQuotes: true);
            }

            FigmaText placeholderText = optionsGroup?.GetChildren().
                                        OfType <FigmaText>().
                                        FirstOrDefault(s => s.name == ComponentString.PLACEHOLDER && s.visible);

            if (placeholderText != null && !placeholderText.characters.Equals(ComponentString.PLACEHOLDER, StringComparison.InvariantCultureIgnoreCase))
            {
                var stringLabel = NativeControlHelper.GetTranslatableString(placeholderText.characters, rendererService.CurrentRendererOptions.TranslateLabels);
                code.WriteEquality(name, nameof(NSTextField.PlaceholderString), stringLabel, true);
            }

            FigmaText text = frame.children.
                             OfType <FigmaText> ().
                             FirstOrDefault(s => s.name == ComponentString.TITLE && s.visible);

            if (text != null)
            {
                code.WriteEquality(name, nameof(NSTextField.Font), CocoaCodeHelpers.GetNSFontString(controlVariant, text, withWeight: false));

                var stringLabel = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);
                code.WriteEquality(name, nameof(NSTextField.StringValue), stringLabel, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
            }

            return(code);
        }
Ejemplo n.º 9
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            code.WritePropertyEquality(name, nameof(NSComboBox.ControlSize), ViewHelper.GetNSControlSize(controlVariant));
            code.WritePropertyEquality(name, nameof(NSComboBox.Font), CodeHelper.GetNSFontString(controlVariant));


            FigmaNode optionsGroup = frame.Options();

            FigmaText placeholderText = optionsGroup?.GetChildren().
                                        OfType <FigmaText>().
                                        FirstOrDefault(s => s.name == ComponentString.PLACEHOLDER && s.visible);

            if (placeholderText != null && !placeholderText.characters.Equals(ComponentString.PLACEHOLDER, StringComparison.InvariantCultureIgnoreCase))
            {
                code.WriteTranslatedEquality(name, nameof(NSComboBox.PlaceholderString), placeholderText, rendererService);
            }


            FigmaText text = frame.children
                             .OfType <FigmaText> ()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null && !string.IsNullOrEmpty(text.characters))
            {
                code.WriteTranslatedEquality(name, nameof(NSComboBox.StringValue), text, rendererService);

                //string textLabel = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);

                //if (!rendererService.CurrentRendererOptions.TranslateLabels)
                //	textLabel = $"\"{textLabel}\"";

                //string nsstringcontructor = typeof (Foundation.NSString).GetConstructor (new[] { textLabel });
                //code.WriteMethod (name, nameof (NSComboBox.Add), nsstringcontructor);
            }

            return(code);
        }
Ejemplo n.º 10
0
        protected override IView OnConvertToView(FigmaNode currentNode, ProcessedNode parentNode, FigmaRendererService rendererService)
        {
            var frame = (FigmaFrame)currentNode;

            var checkBox = new NSButton();

            checkBox.SetButtonType(NSButtonType.Switch);

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null)
            {
                checkBox.Title = text.characters;
            }

            frame.TryGetNativeControlVariant(out var controlVariant);

            checkBox.ControlSize = CocoaHelpers.GetNSControlSize(controlVariant);
            checkBox.Font        = CocoaHelpers.GetNSFont(controlVariant, text);

            FigmaGroup group = frame.children
                               .OfType <FigmaGroup>()
                               .FirstOrDefault(s => s.name.In(ComponentString.STATE_ON,
                                                              ComponentString.STATE_OFF,
                                                              ComponentString.STATE_MIXED) && s.visible);

            if (group != null)
            {
                if (group.name == ComponentString.STATE_ON)
                {
                    checkBox.State = NSCellStateValue.On;
                }

                if (group.name == ComponentString.STATE_OFF)
                {
                    checkBox.State = NSCellStateValue.Off;
                }

                if (group.name == ComponentString.STATE_MIXED)
                {
                    checkBox.AllowsMixedState = true;
                    checkBox.State            = NSCellStateValue.Mixed;
                }
            }

            return(new View(checkBox));
        }
Ejemplo n.º 11
0
        protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService)
        {
            var label = new NSTextField();

            label.Editable                = false;
            label.DrawsBackground         = false;
            label.Bordered                = false;
            label.PreferredMaxLayoutWidth = 1;

            var frame = (FigmaFrame)currentNode;

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            currentNode.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (text != null)
            {
                label.StringValue = rendererService.GetTranslatedText(text);
                label.StringValue = label.StringValue.Replace("\\n", Environment.NewLine);

                label.Alignment = ViewHelper.GetNSTextAlignment(text);
                label.Font      = ViewHelper.GetNSFont(controlVariant, text);
            }

            if (controlType == FigmaControlType.LabelHeader)
            {
                label.Font = NSFont.SystemFontOfSize(headerFontSize, ViewHelper.GetNSFontWeight(text));
            }

            if (text?.styles != null)
            {
                foreach (var styleMap in text.styles)
                {
                    if (rendererService.NodeProvider.TryGetStyle(styleMap.Value, out FigmaStyle style))
                    {
                        if (styleMap.Key == "fill")
                        {
                            label.TextColor = ColorService.GetNSColor(style.name);
                        }
                    }
                }
            }

            return(new View(label));
        }
Ejemplo n.º 12
0
        public static NSTextAlignment GetNSTextAlignment(FigmaText text)
        {
            FigmaTypeStyle style = text.style;

            if (style.textAlignHorizontal == "RIGHT")
            {
                return(NSTextAlignment.Right);
            }

            if (style.textAlignHorizontal == "CENTER")
            {
                return(NSTextAlignment.Center);
            }

            return(NSTextAlignment.Left);
        }
Ejemplo n.º 13
0
        protected override StringBuilder OnConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out NativeControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            var itemNodes = frame.FirstChild(s => s.name == ComponentString.ITEMS);

            if (itemNodes == null)
            {
                return(null);
            }

            code.AppendLine();
            code.AppendLine($"{ name }.{ nameof(NSTabView.SetItems) }(");
            code.AppendLine($"\tnew { typeof(NSTabViewItem[]) }");
            code.AppendLine("\t{");

            foreach (FigmaNode tabNode in itemNodes.GetChildren(t => t.visible, reverseChildren: true))
            {
                var firstChild = tabNode.FirstChild(s => s.name.In(ComponentString.STATE_REGULAR, ComponentString.STATE_SELECTED) && s.visible);

                if (firstChild != null)
                {
                    FigmaText text = firstChild.FirstChild(s => s.name == ComponentString.TITLE) as FigmaText;

                    if (text != null)
                    {
                        code.AppendLine($"\t\tnew {typeof(NSTabViewItem)}() {{ {nameof(NSTabViewItem.Label)} = \"{text.characters}\" }},");
                    }
                }
            }

            code.AppendLine("\t}");
            code.AppendLine(");");

            return(code);
        }
Ejemplo n.º 14
0
        public static void Configure(this Label label, FigmaText text)
        {
            Configure(label, (FigmaNode)text);

            label.ConfigureStyle(text.style);

            label.HorizontalAlignment = text.style.textAlignHorizontal == "CENTER" ? HorizontalAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? HorizontalAlignment.Left : HorizontalAlignment.Right;
            label.VerticalAlignment   = text.style.textAlignVertical == "CENTER" ? VerticalAlignment.Center : text.style.textAlignVertical == "TOP" ? VerticalAlignment.Top : VerticalAlignment.Bottom;

            label.Opacity = text.opacity;

            var fills = text.fills.FirstOrDefault();

            if (fills != null)
            {
                label.Foreground = fills.color.ToColor();
            }
        }
Ejemplo n.º 15
0
        protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService)
        {
            var frame = (FigmaFrame)currentNode;

            var radio = new NSButton();

            radio.SetButtonType(NSButtonType.Radio);

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null)
            {
                radio.Title = text.characters;
            }

            frame.TryGetNativeControlVariant(out var controlVariant);

            radio.ControlSize = ViewHelper.GetNSControlSize(controlVariant);
            radio.Font        = ViewHelper.GetNSFont(controlVariant, text);

            FigmaGroup group = frame.children
                               .OfType <FigmaGroup>()
                               .FirstOrDefault(s => (s.name == ComponentString.STATE_ON || s.name == ComponentString.STATE_OFF) && s.visible);

            if (group != null)
            {
                if (group.name == ComponentString.STATE_ON)
                {
                    radio.State = NSCellStateValue.On;
                }

                if (group.name == ComponentString.STATE_OFF)
                {
                    radio.State = NSCellStateValue.Off;
                }
            }

            return(new View(radio));
        }
Ejemplo n.º 16
0
        protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService)
        {
            var frame = (FigmaFrame)currentNode;

            frame.TryGetNativeControlType(out var controlType);

            var scrollView = new NSScrollView();

            var textView = new NSTextView(
                new CoreGraphics.CGRect(0, 0, scrollView.ContentSize.Width, scrollView.ContentSize.Height));

            textView.Font               = NSFont.SystemFontOfSize(NSFont.SystemFontSize);
            textView.AutoresizingMask   = NSViewResizingMask.WidthSizable;
            textView.TextContainer.Size = new CoreGraphics.CGSize(scrollView.ContentSize.Width, float.MaxValue);

            var       texts = frame.children.OfType <FigmaText>();
            FigmaText text  = texts.FirstOrDefault(s => s.name == ComponentString.TEXT && s.visible);

            if (text != null)
            {
                textView.Value = rendererService.GetTranslatedText(text.characters);
                textView.Value = textView.Value.Replace("\\n", Environment.NewLine);

                // TODO: text styling
                // textView.TextStorage.Append(new Foundation.NSAttributedString(""), null);
            }

            scrollView.BorderType            = NSBorderType.LineBorder;
            scrollView.HasHorizontalScroller = false;
            scrollView.HasVerticalScroller   = true;
            scrollView.DocumentView          = textView;

            frame.TryGetNativeControlVariant(out var controlVariant);

            if (controlVariant == NativeControlVariant.Small)
            {
                textView.Font = NSFont.SystemFontOfSize(NSFont.SmallSystemFontSize);
            }

            return(new View(scrollView));
        }
Ejemplo n.º 17
0
        public static void Configure(this UITextField label, FigmaText text)
        {
            Configure(label, (FigmaNode)text);

            label.TextAlignment = text.style.textAlignHorizontal == "CENTER" ? UITextAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? UITextAlignment.Left : UITextAlignment.Right;
            label.Alpha         = text.opacity;
            //label.l = NSLineBreakMode.ByWordWrapping;
            //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);

            var fills = text.fills.FirstOrDefault();

            if (fills != null)
            {
                label.TextColor = FigmaExtensions.ToUIColor(fills.color);
            }

            if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
            {
                var attributedText = new NSMutableAttributedString(label.AttributedText);
                for (int i = 0; i < text.characterStyleOverrides.Length; i++)
                {
                    var key = text.characterStyleOverrides[i].ToString();
                    if (!text.styleOverrideTable.ContainsKey(key))
                    {
                        continue;
                    }
                    var element = text.styleOverrideTable[key];
                    if (element.fontFamily == null)
                    {
                        continue;
                    }
                    var localFont = FigmaExtensions.ToUIFont(element);
                    var range     = new NSRange(i, 1);
                    attributedText.AddAttribute(UIStringAttributeKey.Font, localFont, range);
                    attributedText.AddAttribute(UIStringAttributeKey.ForegroundColor, label.TextColor, range);
                }

                label.AttributedText = attributedText;
            }
        }
Ejemplo n.º 18
0
        public static void Configure(this TransparentLabel label, FigmaText text)
        {
            Configure(label, (FigmaNode)text);

            //label.TextAlign = text.style.textAlignHorizontal == "CENTER" ? System.Drawing.ContentAlignment.TopCenter : text.style.textAlignHorizontal == "LEFT" ? System.Drawing.ContentAlignment.TopLeft : System.Drawing.ContentAlignment.TopRight;
            //label.AlphaValue = text.opacity;
            //label.LineBreakMode = NSLineBreakMode.ByWordWrapping;
            //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);

            var fills = text.fills.FirstOrDefault();

            if (fills != null)
            {
                label.ForeColor = FigmaExtensions.ToColor(fills.color);
            }

            //if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
            //{
            //    var attributedText = new NSMutableAttributedString(label.AttributedStringValue);
            //    for (int i = 0; i < text.characterStyleOverrides.Length; i++)
            //    {
            //        var key = text.characterStyleOverrides[i].ToString();
            //        if (!text.styleOverrideTable.ContainsKey(key))
            //        {
            //            continue;
            //        }
            //        var element = text.styleOverrideTable[key];
            //        if (element.fontFamily == null)
            //        {
            //            continue;
            //        }
            //        var localFont = FigmaExtensions.ToNSFont(element);
            //        var range = new NSRange(i, 1);
            //        attributedText.AddAttribute(NSStringAttributeKey.Font, localFont, range);
            //        attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, label.TextColor, range);
            //    }

            //    label.AttributedStringValue = attributedText;
            //}
        }
Ejemplo n.º 19
0
        protected override StringBuilder OnConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out NativeControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            code.WriteEquality(name, nameof(NSButton.BezelStyle), NSBezelStyle.Rounded);

            if (controlType == NativeControlType.PopUpButtonPullDown)
            {
                code.WriteEquality(name, nameof(NSPopUpButton.PullsDown), true);
            }

            code.WriteEquality(name, nameof(NSButton.ControlSize), CocoaHelpers.GetNSControlSize(controlVariant));
            code.WriteEquality(name, nameof(NSSegmentedControl.Font), CocoaCodeHelpers.GetNSFontString(controlVariant));

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null && !string.IsNullOrEmpty(text.characters))
            {
                var stringLabel = NativeControlHelper.GetTranslatableString(text.characters,
                                                                            rendererService.CurrentRendererOptions.TranslateLabels);

                code.WriteMethod(name, nameof(NSPopUpButton.AddItem), stringLabel,
                                 inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
            }

            return(code);
        }
Ejemplo n.º 20
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node).FullName, rendererService.NodeRendersVar(currentNode, parentNode));
            }

            FigmaText text = frame.children
                             .OfType <FigmaText>()
                             .FirstOrDefault(s => s.name == ComponentString.SYMBOL);

            code.WritePropertyEquality(name, nameof(NSButton.BezelStyle), NSBezelStyle.RegularSquare);
            code.WritePropertyEquality(name, nameof(NSButton.Bordered), false);
            code.WritePropertyEquality(name, nameof(NSButton.Title), text.characters, inQuotes: true);

            code.WritePropertyEquality(name, nameof(NSButton.Font),
                                       $"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ text.style.fontSize }, { CodeHelper.GetNSFontWeightString(text) })");

            foreach (var styleMap in text.styles)
            {
                if (rendererService.NodeProvider.TryGetStyle(styleMap.Value, out FigmaStyle style))
                {
                    if (styleMap.Key == "fill")
                    {
                        code.WritePropertyEquality(name, nameof(NSButton.ContentTintColor), ColorService.GetNSColorString(style.name));
                    }
                }
            }

            return(code);
        }
Ejemplo n.º 21
0
        protected override IView OnConvertToView(FigmaNode currentNode, ProcessedNode parentNode, FigmaRendererService rendererService)
        {
            var combobox = new NSComboBox();

            var frame = (FigmaFrame)currentNode;

            frame.TryGetNativeControlVariant(out var controlVariant);

            combobox.ControlSize = CocoaHelpers.GetNSControlSize(controlVariant);
            combobox.Font        = CocoaHelpers.GetNSFont(controlVariant);

            FigmaText text = frame.children
                             .OfType <FigmaText> ()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null && !string.IsNullOrEmpty(text.characters))
            {
                combobox.StringValue = text.characters;
            }

            return(new View(combobox));
        }
Ejemplo n.º 22
0
        protected override StringBuilder OnConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out NativeControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            code.WriteEquality(name, nameof(NSButton.ControlSize), CocoaHelpers.GetNSControlSize(controlVariant));
            code.WriteEquality(name, nameof(NSSegmentedControl.Font), CocoaCodeHelpers.GetNSFontString(controlVariant));

            FigmaText text = frame.children
                             .OfType <FigmaText> ()
                             .FirstOrDefault(s => s.name == ComponentString.TITLE);

            if (text != null && !string.IsNullOrEmpty(text.characters))
            {
                code.WriteEquality(name, nameof(NSButton.StringValue), text.characters, inQuotes: true);

                //string textLabel = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);

                //if (!rendererService.CurrentRendererOptions.TranslateLabels)
                //	textLabel = $"\"{textLabel}\"";

                //string nsstringcontructor = typeof (Foundation.NSString).GetConstructor (new[] { textLabel });
                //code.WriteMethod (name, nameof (NSComboBox.Add), nsstringcontructor);
            }

            return(code);
        }
Ejemplo n.º 23
0
 public static string GetNSTextAlignmentString(FigmaText text)
 {
     return($"{nameof(NSTextAlignment)}.{ViewHelper.GetNSTextAlignment(text)}");
 }
Ejemplo n.º 24
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var    code = new StringBuilder();
            string name = FigmaSharp.Resources.Ids.Conversion.NameIdentifier;

            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(name, GetControlType(currentNode.Node).FullName, rendererService.NodeRendersVar(currentNode, parentNode));
            }

            switch (controlType)
            {
            case FigmaControlType.Button:
                code.WritePropertyEquality(name, nameof(NSButton.BezelStyle), NSBezelStyle.Rounded);
                break;

            case FigmaControlType.ButtonRoundRect:
                code.WritePropertyEquality(name, nameof(NSButton.BezelStyle), NSBezelStyle.RoundRect);
                break;

            case FigmaControlType.ButtonHelp:
                code.WritePropertyEquality(name, nameof(NSButton.BezelStyle), NSBezelStyle.HelpButton);
                code.WritePropertyEquality(name, nameof(NSButton.Title), string.Empty, inQuotes: true);
                break;
            }

            code.WritePropertyEquality(name, nameof(NSButton.ControlSize), ViewHelper.GetNSControlSize(controlVariant));
            code.WritePropertyEquality(name, nameof(NSSegmentedControl.Font), CodeHelper.GetNSFontString(controlVariant));

            FigmaGroup group = frame.children
                               .OfType <FigmaGroup> ()
                               .FirstOrDefault(s => s.visible);

            if (group != null)
            {
                FigmaText text = group.children
                                 .OfType <FigmaText> ()
                                 .FirstOrDefault(s => s.name == ComponentString.TITLE);

                if (text != null && controlType != FigmaControlType.ButtonHelp)
                {
                    code.WriteTranslatedEquality(name, nameof(NSButton.Title), text, rendererService);
                }

                if (group.name == ComponentString.STATE_DISABLED)
                {
                    code.WritePropertyEquality(name, nameof(NSButton.Enabled), false);
                }

                if (group.name == ComponentString.STATE_DEFAULT)
                {
                    code.WritePropertyEquality(name, nameof(NSButton.KeyEquivalent), "\\r", true);
                }
            }

            return(code);
        }
Ejemplo n.º 25
0
 public static void WriteTranslatedEquality(this StringBuilder builder, string viewName, string propertyName, FigmaText value, ICodeRenderService codeRenderService, bool instanciate = false)
 {
     WriteTranslatedEquality(builder, viewName, propertyName, value.characters, codeRenderService, instanciate, value.visible);
 }
Ejemplo n.º 26
0
 public static void Configure(this StringBuilder builder, string name, FigmaText text)
 {
     Configure(builder, name, (FigmaNode)text);
 }
Ejemplo n.º 27
0
        public static void Configure(this NSTextField label, FigmaText text)
        {
            Configure(label, (FigmaNode)text);

            label.Alignment  = text.style.textAlignHorizontal == "CENTER" ? NSTextAlignment.Center : text.style.textAlignHorizontal == "LEFT" ? NSTextAlignment.Left : NSTextAlignment.Right;
            label.AlphaValue = text.opacity;
            //label.LineBreakMode = NSLineBreakMode.ByWordWrapping;
            //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);
            if (label.Cell is VerticalAlignmentTextCell cell)
            {
                cell.VerticalAligment = text.style.textAlignVertical == "CENTER" ? VerticalTextAlignment.Center : text.style.textAlignVertical == "TOP" ? VerticalTextAlignment.Top : VerticalTextAlignment.Bottom;
            }

            var fills = text.fills.FirstOrDefault();

            if (fills != null && fills.visible)
            {
                label.TextColor = fills.color.ToNSColor();
            }

            if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
            {
                var attributedText = new NSMutableAttributedString(label.AttributedStringValue);
                for (int i = 0; i < text.characterStyleOverrides.Length; i++)
                {
                    var range = new NSRange(i, 1);

                    var key = text.characterStyleOverrides[i].ToString();
                    if (!text.styleOverrideTable.ContainsKey(key))
                    {
                        //we want the default values
                        attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, label.TextColor, range);
                        attributedText.AddAttribute(NSStringAttributeKey.Font, label.Font, range);
                        continue;
                    }

                    //if there is a style to override
                    var styleOverrided = text.styleOverrideTable[key];

                    //set the color
                    NSColor fontColorOverrided = label.TextColor;
                    var     fillOverrided      = styleOverrided.fills?.FirstOrDefault();
                    if (fillOverrided != null && fillOverrided.visible)
                    {
                        fontColorOverrided = fillOverrided.color.ToNSColor();
                    }

                    attributedText.AddAttribute(NSStringAttributeKey.ForegroundColor, fontColorOverrided, range);

                    //TODO: we can improve this
                    //set the font for this character
                    NSFont fontOverrided = label.Font;
                    if (styleOverrided.fontFamily != null)
                    {
                        fontOverrided = FigmaExtensions.ToNSFont(styleOverrided);
                    }
                    attributedText.AddAttribute(NSStringAttributeKey.Font, fontOverrided, range);
                }

                label.AttributedStringValue = attributedText;
            }
        }
Ejemplo n.º 28
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var code  = new StringBuilder();
            var frame = (FigmaFrame)currentNode.Node;

            currentNode.Node.TryGetNativeControlType(out FigmaControlType controlType);
            currentNode.Node.TryGetNativeControlVariant(out NativeControlVariant controlVariant);

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(currentNode.Name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            if (controlType == FigmaControlType.Separator)
            {
                code.WritePropertyEquality(currentNode.Name, nameof(NSBox.BoxType), NSBoxType.NSBoxSeparator);
            }

            if (controlType == FigmaControlType.BoxCustom)
            {
                code.WritePropertyEquality(currentNode.Name, nameof(NSBox.BoxType), NSBoxType.NSBoxCustom);

                RectangleVector rectangle = frame.children
                                            .OfType <RectangleVector>()
                                            .FirstOrDefault();

                if (rectangle != null)
                {
                    if (rectangle.styles != null)
                    {
                        bool borderSet = false;
                        foreach (var styleMap in rectangle.styles)
                        {
                            if ((rendererService.NodeProvider is NodeProvider nodeProvider) && nodeProvider.TryGetStyle(styleMap.Value, out FigmaStyle style))
                            {
                                if (styleMap.Key == "fill")
                                {
                                    code.WritePropertyEquality(currentNode.Name, nameof(NSBox.FillColor), ColorService.GetNSColorString(style.name));
                                }

                                if (styleMap.Key == "stroke")
                                {
                                    code.WritePropertyEquality(currentNode.Name, nameof(NSBox.BorderColor), ColorService.GetNSColorString(style.name));
                                    code.WritePropertyEquality(currentNode.Name, nameof(NSBox.BorderWidth), rectangle.strokeWeight.ToString());
                                    borderSet = true;
                                }
                            }
                        }

                        if (!borderSet)
                        {
                            code.WritePropertyEquality(currentNode.Name, nameof(NSBox.BorderWidth), "0");
                        }
                    }
                    code.WritePropertyEquality(currentNode.Name, nameof(NSBox.CornerRadius), rectangle.cornerRadius.ToString());
                }
            }

            FigmaText text = null;

            if (frame.children != null)
            {
                text = frame.children
                       .OfType <FigmaText>()
                       .FirstOrDefault(s => (s.name == ComponentString.TITLE && s.visible));
            }

            if (text != null)
            {
                code.WriteTranslatedEquality(currentNode.Name, nameof(NSBox.Title), text.characters, rendererService);
            }
            else
            {
                code.WritePropertyEquality(currentNode.Name, nameof(NSBox.Title), string.Empty, inQuotes: true);
            }

            return(code);
        }
Ejemplo n.º 29
0
        public static void Configure(this StringBuilder builder, FigmaText text, string name)
        {
            //Configure(builder, name, (FigmaNode)text);


            var propertyAttributedStringValue = $"{name}.{nameof (NSTextField.AttributedStringValue)}";
            var propertyTextColor             = $"{name}.{nameof (NSTextField.TextColor)}";
            var propertyFont       = $"{name}.{nameof (NSTextField.Font)}";
            var propertyAlignment  = $"{name}.{nameof (NSTextField.Alignment)}";
            var propertyAlphaValue = $"{name}.{nameof (NSTextField.AlphaValue)}";

            builder.AppendLine(string.Format("{0} = {1};", propertyFont, text.style.ToNSFontDesignerString()));

            builder.AppendLine(string.Format("{0} = {1};", propertyAlphaValue, text.opacity.ToDesignerString()));

            //label.LineBreakMode = NSLineBreakMode.ByWordWrapping;
            //label.SetContentCompressionResistancePriority(250, NSLayoutConstraintOrientation.Horizontal);

            var fills = text.fills.FirstOrDefault() as FigmaPaint;

            if (fills != null)
            {
                builder.AppendLine(string.Format("{0} = {1};", propertyTextColor, fills.color.ToDesignerString()));
            }

            if (text.characterStyleOverrides != null && text.characterStyleOverrides.Length > 0)
            {
                var attributedTextName = string.Format("{0}AttributedText", Resources.Ids.Conversion.NameIdentifier);
                builder.AppendLine(string.Format("var {0} = new {1} ({2});", attributedTextName, typeof(NSMutableAttributedString).FullName, propertyAttributedStringValue));

                var attributedStringKey = typeof(NSStringAttributeKey).FullName;
                var attributtedStringForegroundColor = $"{attributedStringKey}.{nameof (NSStringAttributeKey.ForegroundColor)}";
                var attributtedStringFont            = $"{attributedStringKey}.{nameof (NSStringAttributeKey.Font)}";

                //var attributedText = new NSMutableAttributedString (label.AttributedStringValue);
                for (int i = 0; i < text.characterStyleOverrides.Length; i++)
                {
                    var range = $"new {typeof (NSRange).FullName} ({i}, 1)";

                    var key = text.characterStyleOverrides[i].ToString();
                    if (!text.styleOverrideTable.ContainsKey(key))
                    {
                        //we want the default values
                        //builder.AppendLine (string.Format ("{0}.AddAttribute(AppKit.NSStringAttributeKey.Font, {1}, new NSRange({2}, 1));", attributedTextName, element.ToNSFontDesignerString (), i));

                        builder.AppendLine(string.Format("{0}.AddAttribute({1}, {2}, {3});", attributedTextName, attributtedStringForegroundColor, propertyTextColor, range));
                        builder.AppendLine(string.Format("{0}.AddAttribute({1}, {2}, {3});", attributedTextName, attributtedStringFont, propertyTextColor, range));
                        continue;
                    }

                    //if there is a style to override
                    var styleOverrided = text.styleOverrideTable[key];

                    //set the color

                    string fontColorOverrided;
                    var    fillOverrided = styleOverrided.fills?.FirstOrDefault();
                    if (fillOverrided != null && fillOverrided.visible)
                    {
                        fontColorOverrided = fillOverrided.color.ToDesignerString();
                    }
                    else
                    {
                        fontColorOverrided = propertyTextColor;
                    }

                    builder.AppendLine(string.Format("{0}.AddAttribute({1}, {2}, {3});", attributedTextName, attributtedStringForegroundColor, fontColorOverrided, range));

                    //TODO: we can improve this
                    //set the font for this character
                    string fontOverrided;
                    if (styleOverrided?.fontFamily != null)
                    {
                        fontOverrided = styleOverrided.ToNSFontDesignerString();
                    }
                    else
                    {
                        fontOverrided = propertyFont;
                    }

                    builder.AppendLine(string.Format("{0}.AddAttribute ({1}, {2}, {3});", attributedTextName, attributtedStringFont, fontOverrided, range));
                }

                builder.AppendLine(string.Format("{0} = {1};", propertyAttributedStringValue, attributedTextName));
            }
        }
Ejemplo n.º 30
0
        protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService)
        {
            var frame = (FigmaFrame)currentNode;
            var box   = new NSBox();

            currentNode.TryGetNativeControlType(out FigmaControlType controlType);

            if (controlType == FigmaControlType.Separator)
            {
                box.BoxType = NSBoxType.NSBoxSeparator;
            }

            if (controlType == FigmaControlType.BoxCustom)
            {
                box.BoxType     = NSBoxType.NSBoxCustom;
                box.BorderWidth = 0;

                RectangleVector rectangle = frame.children
                                            .OfType <RectangleVector>()
                                            .FirstOrDefault();

                if (rectangle != null)
                {
                    if (rectangle.styles != null)
                    {
                        foreach (var styleMap in rectangle.styles)
                        {
                            if (rendererService.NodeProvider.TryGetStyle(styleMap.Value, out FigmaStyle style))
                            {
                                if (styleMap.Key == "fill")
                                {
                                    box.FillColor = ColorService.GetNSColor(style.name);
                                }

                                if (styleMap.Key == "stroke")
                                {
                                    box.BorderColor = ColorService.GetNSColor(style.name);
                                    box.BorderWidth = rectangle.strokeWeight;
                                }
                            }
                        }
                    }

                    box.CornerRadius = rectangle.cornerRadius;
                }
            }

            if (controlType == FigmaControlType.Box)
            {
                FigmaText text = frame.children
                                 .OfType <FigmaText>()
                                 .FirstOrDefault(s => (s.name == ComponentString.TITLE && s.visible));

                if (text != null)
                {
                    box.Title = rendererService.GetTranslatedText(text);
                }
                else
                {
                    box.Title = string.Empty;
                }
            }

            return(new View(box));
        }