コード例 #1
0
        public static void WriteTranslatedEquality(this StringBuilder builder, string viewName, string propertyName, string value, ICodeRenderService codeRenderService, bool instanciate = false, bool textCondition = true)
        {
            bool   needQuotes;
            string result;

            if (textCondition && !string.IsNullOrEmpty(value))
            {
                if (codeRenderService.Options != null && codeRenderService.Options.TranslateLabels)
                {
                    result     = codeRenderService.GetTranslatedText(value);
                    needQuotes = false;
                }
                else
                {
                    result     = value;
                    needQuotes = true;
                }
            }
            else
            {
                result     = string.Empty;
                needQuotes = true;
            }
            builder.AppendLine(CodeGenerationHelpers.GetPropertyEquality(viewName, propertyName, result, inQuotes: needQuotes, instanciate: instanciate));
        }
コード例 #2
0
        public override string ConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var builder = OnConvertToCode(currentNode, parentNode, rendererService);

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

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

                builder.WritePropertyEquality(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.WritePropertyEquality(currentNode.Name, nameof(AppKit.NSView.AccessibilityRole), fullRoleName);

                        hasAccessibility = true;
                    }

                    if (CanSetAccessibilityLabel && currentNode.Node.TrySearchA11Label(out var label))
                    {
                        builder.WriteTranslatedEquality(currentNode.Name, GetAccessibilityTitle(nativeControlType), label, rendererService);
                        hasAccessibility = true;
                    }

                    if (CanSetAccessibilityHelp && currentNode.Node.TrySearchA11Help(out var help))
                    {
                        help = rendererService.GetTranslatedText(help);
                        builder.WritePropertyEquality(currentNode.Name, nameof(AppKit.NSView.AccessibilityHelp), help, inQuotes: !rendererService.Options.TranslateLabels);

                        hasAccessibility = true;
                    }

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

                return(builder.ToString());
            }
            return(string.Empty);
        }
コード例 #3
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(NSPopUpButton.BezelStyle), NSBezelStyle.Rounded);

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

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

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

            if (text != null && !string.IsNullOrEmpty(text.characters))
            {
                var stringLabel = rendererService.GetTranslatedText(text);
                code.WriteMethod(name, nameof(NSPopUpButton.AddItem), stringLabel,
                                 inQuotes: !rendererService.Options.TranslateLabels);
            }

            return(code);
        }
コード例 #4
0
        protected override StringBuilder OnConvertToCode(CodeNode currentNode, CodeNode parentNode, ICodeRenderService rendererService)
        {
            var code = new StringBuilder();

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

            currentNode.Name = name;
            var frame = (FigmaFrame)currentNode.Node;

            code.WriteConstructor(name, typeof(NSScrollView));
            code.WritePropertyEquality(name, nameof(NSScrollView.AutoresizingMask),
                                       $"{nameof(NSViewResizingMask)}.{nameof(NSViewResizingMask.WidthSizable)} | " +
                                       $"{nameof(NSViewResizingMask)}.{nameof(NSViewResizingMask.HeightSizable)}");

            code.WritePropertyEquality(name, nameof(NSScrollView.BorderType), NSBorderType.BezelBorder);
            code.WritePropertyEquality(name, nameof(NSScrollView.DrawsBackground), true);
            code.AppendLine();

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

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

            var columnNodes = frame.FirstChild(s => s.name == ComponentString.COLUMNS && s.visible);

            // TODO: Parse options layers
            code.WritePropertyEquality(tableViewName, nameof(NSTableView.UsesAlternatingRowBackgroundColors), false);
            code.WritePropertyEquality(tableViewName, nameof(NSTableView.AllowsMultipleSelection), false);
            code.WritePropertyEquality(tableViewName, nameof(NSTableView.AllowsColumnResizing), true);
            code.WritePropertyEquality(tableViewName, nameof(NSTableView.AllowsColumnReordering), false);
            code.WritePropertyEquality(tableViewName, nameof(NSTableView.AllowsEmptySelection), false);
            code.AppendLine();

            int columnCount = 1;

            foreach (FigmaNode tableColumNode in columnNodes.GetChildren(t => t.visible))
            {
                FigmaText text = tableColumNode.FirstChild(s => s.name == ComponentString.TITLE) as FigmaText;

                if (text == null)
                {
                    continue;
                }

                string columnId = "TableColumn" + columnCount;

                code.WriteConstructor(columnId, typeof(NSTableColumn));

                code.WritePropertyEquality(columnId,
                                           $"{nameof(NSTableColumn.HeaderCell)}.{nameof(NSTableColumn.HeaderCell.Alignment)}",
                                           Helpers.CodeHelper.GetNSTextAlignmentString(text));

                code.WritePropertyEquality(columnId, nameof(NSTableColumn.Identifier), columnId, inQuotes: true);
                code.WritePropertyEquality(columnId, nameof(NSTableColumn.Title), rendererService.GetTranslatedText(text), inQuotes: true);
                code.WritePropertyEquality(columnId, nameof(NSTableColumn.Width), text.absoluteBoundingBox.Width.ToString(), inQuotes: false);
                code.WriteMethod(tableViewName, nameof(NSTableView.AddColumn), columnId);
                code.AppendLine();

                columnCount++;
            }

            code.WritePropertyEquality(name, nameof(NSScrollView.DocumentView), tableViewName, inQuotes: false);
            code.AppendLine();


            var rectangle = (RectangleVector)frame.FirstChild(s => s.name == ComponentString.BACKGROUND && s.visible);

            if (rectangle != null)
            {
                foreach (var styleMap in rectangle.styles)
                {
                    if (rendererService.NodeProvider.TryGetStyle(styleMap.Value, out FigmaStyle style) &&
                        styleMap.Key == "fill")
                    {
                        code.WritePropertyEquality(tableViewName, nameof(NSTableView.BackgroundColor), ColorService.GetNSColorString(style.name));
                    }
                }
            }


            return(code);
        }