Esempio n. 1
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.WriteMethod(name, nameof(NSButton.SetButtonType), NSButtonType.Switch);

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

            if (text != null)
            {
                var labelTranslated = NativeControlHelper.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.In(ComponentString.STATE_ON,
                                                              ComponentString.STATE_OFF,
                                                              ComponentString.STATE_MIXED) && 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);
                }

                if (group.name == ComponentString.STATE_MIXED)
                {
                    code.WriteEquality(name, nameof(NSButton.AllowsMixedState), true);
                    code.WriteEquality(name, nameof(NSButton.State), NSCellStateValue.Mixed);
                }
            }

            return(code);
        }
Esempio n. 2
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).FullName, rendererService.NodeRendersVar(currentNode, parentNode));
            }

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

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

            code.WriteEquality(name, nameof(NSButton.ControlSize), CocoaHelpers.GetNSControlSize(controlVariant));
            code.WriteEquality(name, nameof(NSSegmentedControl.Font), CocoaCodeHelpers.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 != NativeControlType.ButtonHelp)
                {
                    string labelTranslated = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);
                    code.WriteEquality(name, nameof(NSButton.Title), labelTranslated, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
                }

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

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

            return(code);
        }
Esempio n. 3
0
        protected override StringBuilder OnConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var code = new StringBuilder();

            // TODO: Get name from generator
            var    name         = currentNode.Name + "ScrollView";
            string textViewName = currentNode.Name;

            currentNode.Name = name;

            var frame = (FigmaFrame)currentNode.Node;

            frame.TryGetNativeControlVariant(out var controlVariant);

            code.WriteConstructor(name, typeof(NSScrollView));
            code.WriteEquality(name, nameof(NSScrollView.BorderType), NSBorderType.LineBorder.GetFullName());
            code.WriteEquality(name, nameof(NSScrollView.HasHorizontalRuler), false);
            code.WriteEquality(name, nameof(NSScrollView.HasVerticalScroller), true);

            code.AppendLine();

            if (rendererService.NeedsRenderConstructor(currentNode, parentNode))
            {
                code.WriteConstructor(textViewName, typeof(NSTextView), rendererService.NodeRendersVar(currentNode, parentNode));
            }

            code.WriteEquality(textViewName,
                               nameof(NSTextView.Frame),
                               string.Format("new {0} ({1}, {2}, {3}, {4})",
                                             typeof(CoreGraphics.CGRect), 0, 0, name + ".ContentSize.Width", name + ".ContentSize.Height"));

            code.WriteEquality(textViewName, nameof(NSTextView.AutoresizingMask), NSViewResizingMask.WidthSizable.GetFullName());

            // TODO: Use CocoaConverter methods
            switch (controlVariant)
            {
            case NativeControlVariant.Regular:
                code.WriteEquality(textViewName, nameof(NSTextView.Font), CodeGenerationHelpers.Font.SystemFontOfSize(CodeGenerationHelpers.Font.SystemFontSize));
                break;

            case NativeControlVariant.Small:
                code.WriteEquality(textViewName, nameof(NSTextView.Font), CodeGenerationHelpers.Font.SystemFontOfSize(CodeGenerationHelpers.Font.SmallSystemFontSize));
                break;
            }

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

            if (text != null)
            {
                var stringLabel = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);
                code.WriteEquality(textViewName, nameof(NSTextView.Value), stringLabel, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
            }

            code.AppendLine();
            code.WriteEquality(name, nameof(NSScrollView.DocumentView), textViewName);

            return(code);
        }
Esempio n. 4
0
        public override string ConvertToCode(FigmaCodeNode currentNode, FigmaCodeNode parentNode, FigmaCodeRendererService rendererService)
        {
            var builder = OnConvertToCode(currentNode, parentNode, rendererService);

            if (builder != null)
            {
                currentNode.Node.TryGetNativeControlType(out var nativeControlType);

                if (!currentNode.Node.visible)
                {
                    builder.WriteEquality(currentNode.Name, nameof(NSView.Hidden), true);
                }

                builder.WriteEquality(currentNode.Name, nameof(NSView.TranslatesAutoresizingMaskIntoConstraints), false);

                if (currentNode.Node.IsA11Enabled())
                {
                    bool hasAccessibility = false;

                    if (CanSetAccessibilityRole && currentNode.Node.IsA11Group())
                    {
                        var fullRoleName = $"{typeof(AppKit.NSAccessibilityRoles).FullName}.{nameof(AppKit.NSAccessibilityRoles.GroupRole)}";
                        builder.WriteEquality(currentNode.Name, nameof(AppKit.NSView.AccessibilityRole), fullRoleName);

                        hasAccessibility = true;
                    }

                    if (CanSetAccessibilityLabel && currentNode.Node.TrySearchA11Label(out var label))
                    {
                        label = NativeControlHelper.GetTranslatableString(label, rendererService.CurrentRendererOptions.TranslateLabels);
                        builder.WriteEquality(currentNode.Name, GetAccessibilityTitle(nativeControlType), label, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);

                        hasAccessibility = true;
                    }

                    if (CanSetAccessibilityHelp && currentNode.Node.TrySearchA11Help(out var help))
                    {
                        help = NativeControlHelper.GetTranslatableString(help, rendererService.CurrentRendererOptions.TranslateLabels);
                        builder.WriteEquality(currentNode.Name, nameof(AppKit.NSView.AccessibilityHelp), help, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);

                        hasAccessibility = true;
                    }

                    if (hasAccessibility)
                    {
                        builder.AppendLine();
                    }
                }

                return(builder.ToString());
            }
            return(string.Empty);
        }
Esempio n. 5
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);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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));
            }

            if (controlType == NativeControlType.Separator)
            {
                code.WriteEquality(name, nameof(NSBox.BoxType), NSBoxType.NSBoxSeparator);
            }

            if (controlType == NativeControlType.BoxCustom)
            {
                code.WriteEquality(name, nameof(NSBox.BoxType), NSBoxType.NSBoxCustom);
                bool borderSet = false;

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

                foreach (var styleMap in rectangle?.styles)
                {
                    if ((rendererService.figmaProvider as FigmaFileProvider).TryGetStyle(styleMap.Value, out FigmaStyle style))
                    {
                        if (styleMap.Key == "fill")
                        {
                            code.WriteEquality(name, nameof(NSBox.FillColor), CocoaCodeHelpers.GetNSColorString(style.name));
                        }

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

                if (!borderSet)
                {
                    code.WriteEquality(name, nameof(NSBox.BorderWidth), "0");
                }
            }

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

            if (text != null)
            {
                string stringTitle = NativeControlHelper.GetTranslatableString(text.characters,
                                                                               rendererService.CurrentRendererOptions.TranslateLabels);

                code.WriteEquality(name, nameof(NSBox.Title), stringTitle,
                                   inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);
            }
            else
            {
                code.WriteEquality(name, nameof(NSBox.Title), "", inQuotes: true);
            }

            return(code);
        }
Esempio 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));
            }

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

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

            code.WriteEquality(name, nameof(NSTextField.Editable), false);
            code.WriteEquality(name, nameof(NSTextField.Bordered), false);
            code.WriteEquality(name, nameof(NSTextField.DrawsBackground), false);
            code.WriteEquality(name, nameof(NSTextField.PreferredMaxLayoutWidth), "1");

            var labelComponent = NativeControlHelper.GetTranslatableString(text.characters, rendererService.CurrentRendererOptions.TranslateLabels);

            code.WriteEquality(name, nameof(NSTextField.StringValue), labelComponent, inQuotes: !rendererService.CurrentRendererOptions.TranslateLabels);

            if (text != null)
            {
                code.WriteEquality(name, nameof(NSTextField.Alignment), CocoaCodeHelpers.GetNSTextAlignmentString(text).ToString());
            }

            if (controlType == NativeControlType.LabelHeader)
            {
                code.WriteEquality(name, nameof(NSTextField.Font),
                                   $"{ typeof(NSFont) }.{ nameof(NSFont.SystemFontOfSize) }({ headerFontSize }, { CocoaCodeHelpers.GetNSFontWeightString(text) })");
            }
            else
            {
                code.WriteEquality(name, nameof(NSTextField.Font), CocoaCodeHelpers.GetNSFontString(controlVariant, text));
            }

            foreach (var styleMap in text?.styles)
            {
                if ((rendererService.figmaProvider as FigmaFileProvider).TryGetStyle(styleMap.Value, out FigmaStyle style))
                {
                    if (styleMap.Key == "fill")
                    {
                        code.WriteEquality(name, nameof(NSTextField.TextColor), CocoaCodeHelpers.GetNSColorString(style.name));
                    }
                }
            }

            return(code);
        }