protected override IView OnConvertToView(FigmaNode currentNode, ProcessedNode parentNode, FigmaRendererService rendererService) { var textField = new NSTextField(); var frame = (FigmaFrame)currentNode; frame.TryGetNativeControlType(out var controlType); frame.TryGetNativeControlVariant(out var controlVariant); if (controlType == NativeControlType.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 = placeholderText.characters; } FigmaText text = frame.children .OfType <FigmaText> () .FirstOrDefault(s => s.name == ComponentString.TITLE && s.visible); if (text != null) { textField.Alignment = CocoaHelpers.GetNSTextAlignment(text); textField.StringValue = text.characters; } textField.ControlSize = CocoaHelpers.GetNSControlSize(controlVariant); textField.Font = CocoaHelpers.GetNSFont(controlVariant); return(new View(textField)); }
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); }
protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService) { var combobox = new NSComboBox(); var frame = (FigmaFrame)currentNode; frame.TryGetNativeControlVariant(out var controlVariant); combobox.ControlSize = ViewHelper.GetNSControlSize(controlVariant); combobox.Font = ViewHelper.GetNSFont(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)) { combobox.PlaceholderString = rendererService.GetTranslatedText(placeholderText); } FigmaText text = frame.children .OfType <FigmaText> () .FirstOrDefault(s => s.name == ComponentString.TITLE); if (text != null && !string.IsNullOrEmpty(text.characters)) { combobox.StringValue = rendererService.GetTranslatedText(text); } return(new View(combobox)); }
protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService) { var segmentedControl = new NSSegmentedControl(); var frame = (FigmaFrame)currentNode; frame.TryGetNativeControlType(out var controlType); frame.TryGetNativeControlVariant(out var controlVariant); segmentedControl.ControlSize = ViewHelper.GetNSControlSize(controlVariant); segmentedControl.Font = ViewHelper.GetNSFont(controlVariant); FigmaNode items = frame.FirstChild(s => s.name == ComponentString.ITEMS); if (items != null) { segmentedControl.SegmentCount = items.GetChildren(t => t.visible).Count(); segmentedControl.SegmentDistribution = NSSegmentDistribution.FillEqually; segmentedControl.SegmentStyle = NSSegmentStyle.Rounded; segmentedControl.SelectedSegment = 0; int i = 0; foreach (FigmaNode button in items.GetChildren(t => t.visible)) { FigmaNode state = button.FirstChild(s => s.visible && s.name.In(ComponentString.STATE_REGULAR, ComponentString.STATE_SELECTED)); if (state == null) { continue; } var text = (FigmaText)state.FirstChild(s => s.name == ComponentString.TITLE); segmentedControl.SetLabel(rendererService.GetTranslatedText(text), i); i++; } } segmentedControl.TrackingMode = NSSegmentSwitchTracking.SelectOne; return(new View(segmentedControl)); }
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; frame.TryGetNativeControlType(out var controlType); frame.TryGetNativeControlVariant(out var controlVariant); if (rendererService.NeedsRenderConstructor(currentNode, parentNode)) { code.WriteConstructor(name, GetControlType(currentNode.Node), rendererService.NodeRendersVar(currentNode, parentNode)); } code.WritePropertyEquality(name, nameof(NSButton.ControlSize), ViewHelper.GetNSControlSize(controlVariant)); code.WritePropertyEquality(name, nameof(NSSegmentedControl.Font), CodeHelper.GetNSFontString(controlVariant)); FigmaNode items = frame.FirstChild(s => s.name == ComponentString.ITEMS); if (items != null) { code.WritePropertyEquality(name, nameof(NSSegmentedControl.SegmentCount), "" + items.GetChildren(t => t.visible).Count()); code.WritePropertyEquality(name, nameof(NSSegmentedControl.SegmentDistribution), NSSegmentDistribution.FillEqually); code.WritePropertyEquality(name, nameof(NSSegmentedControl.SegmentStyle), NSSegmentStyle.Rounded); code.WritePropertyEquality(name, nameof(NSSegmentedControl.SelectedSegment), "0"); code.WritePropertyEquality(name, nameof(NSSegmentedControl.TrackingMode), NSSegmentSwitchTracking.SelectOne); code.AppendLine(); int i = 0; foreach (FigmaNode button in items.GetChildren(t => t.visible)) { FigmaNode state = button.FirstChild(s => s.visible && s.name.In(ComponentString.STATE_REGULAR, ComponentString.STATE_SELECTED)); if (state == null) { continue; } var text = (FigmaText)state.FirstChild(s => s.name == ComponentString.TITLE); code.WriteMethod(name, nameof(NSSegmentedControl.SetLabel), $"\"{ text.characters }\", { i }"); i++; } code.AppendLine(); } return(code); }
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); }
public override Type GetControlType(FigmaNode currentNode) { FigmaNode optionsGroup = currentNode.Options(); FigmaNode passwordNode = optionsGroup?.GetChildren() .OfType <FigmaNode>() .FirstOrDefault(s => s.name == ComponentString.PASSWORD && s.visible); if (passwordNode != null) { return(typeof(NSSecureTextField)); } currentNode.TryGetNativeControlType(out var controlType); if (controlType == FigmaControlType.SearchField) { return(typeof(NSSearchField)); } return(typeof(NSTextField)); }
public override IView ConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService) { string title = ""; var frame = (FigmaFrame)currentNode; var nativeView = new FakeWindowView(title); nativeView.LiveButtonAlwaysVisible = LiveButtonAlwaysVisible; nativeView.Configure(currentNode); var view = new View(nativeView); var windowComponent = currentNode.GetDialogInstanceFromParentContainer(); if (windowComponent != null) { var optionsNode = windowComponent.Options(); if (optionsNode is IFigmaNodeContainer figmaNodeContainer) { nativeView.CloseButtonHidden = (figmaNodeContainer.HasChildrenVisible("close") == false); nativeView.MinButtonHidden = (figmaNodeContainer.HasChildrenVisible("min") == false); nativeView.MaxButtonHidden = (figmaNodeContainer.HasChildrenVisible("max") == false); var titleText = (FigmaText)optionsNode.GetChildren().FirstOrDefault(s => s.name == "title" && s.visible); if (titleText != null) { nativeView.Title = titleText.characters; } } } nativeView.LiveButton.Activated += async(s, e) => { var window = new Window(view.Allocation); LivePreviewLoading?.Invoke(this, EventArgs.Empty); await newWindowProvider.LoadAsync(rendererService.NodeProvider.File); var secondaryRender = new ControlViewRenderingService(newWindowProvider); var options = new ViewRenderServiceOptions() { GenerateMainView = false }; secondaryRender.RenderInWindow(window, currentNode, options); var mainNodes = currentNode.GetChildren() .ToArray(); ViewNode[] processedNodes = secondaryRender.GetProcessedNodes(mainNodes); var layoutManager = new StoryboardLayoutManager() { UsesConstraints = true }; layoutManager.Run(processedNodes, window.Content, secondaryRender); var nativeWindow = (NSWindow)window.NativeObject; nativeWindow.Appearance = nativeView.EffectiveAppearance; nativeWindow.ContentMinSize = nativeWindow.ContentView.Frame.Size; nativeWindow.Center(); nativeWindow.MakeKeyAndOrderFront(null); LivePreviewLoaded?.Invoke(this, EventArgs.Empty); }; return(view); }
protected override IView OnConvertToView(FigmaNode currentNode, ViewNode parentNode, ViewRenderService rendererService) { var segmentedControl = new NSSegmentedControl(); var frame = (FigmaFrame)currentNode; frame.TryGetNativeControlType(out var controlType); frame.TryGetNativeControlVariant(out var controlVariant); segmentedControl.ControlSize = ViewHelper.GetNSControlSize(controlVariant); segmentedControl.Font = ViewHelper.GetNSFont(controlVariant); FigmaNode items = frame.FirstChild(s => s.name == ComponentString.ITEMS); if (items != null) { segmentedControl.SegmentCount = items.GetChildren(t => t.visible).Count(); if (controlType == FigmaControlType.SegmentedControlRoundRect) { segmentedControl.SegmentStyle = NSSegmentStyle.RoundRect; } else { segmentedControl.SegmentStyle = NSSegmentStyle.Rounded; } int i = 0; foreach (FigmaNode button in items.GetChildren(t => t.visible)) { FigmaNode state = button.FirstChild(s => s.visible && s.name.In(ComponentString.STATE_REGULAR, ComponentString.STATE_SELECTED)); if (state == null) { continue; } var text = (FigmaText)state.FirstChild(s => s.name == ComponentString.TITLE); segmentedControl.SetLabel(rendererService.GetTranslatedText(text), i); segmentedControl.SetSelected(selected: state.name == ComponentString.STATE_SELECTED, i); i++; } bool hasSelection = (segmentedControl.SelectedSegment > -1); // Use tab-like behaviour if there is a selected item. Otherwise use the button-like behaviour if (hasSelection) { segmentedControl.TrackingMode = NSSegmentSwitchTracking.SelectOne; segmentedControl.SegmentDistribution = NSSegmentDistribution.Fill; } else { segmentedControl.TrackingMode = NSSegmentSwitchTracking.Momentary; segmentedControl.SegmentDistribution = NSSegmentDistribution.FillEqually; } } return(new View(segmentedControl)); }
public static FigmaNode FirstChild(this FigmaNode figmaNode, Func <FigmaNode, bool> func = null) { var item = figmaNode.GetChildren(s => func?.Invoke(s) ?? true); return(item.FirstOrDefault()); }