public override Margins Create(IParameterDescriptor parameter, IReadonlyContext context) => new Margins( RelativeProperty.Get(parameter.Metadata, Relative.Get(context)), Edges[0].Get(context), Edges[1].Get(context), Edges[2].Get(context), Edges[3].Get(context));
public PresentedParameter Present(IParameterDescriptor param, Action updateCallback) { var pathType = PathTypeProperty.Get(param.Metadata); var checkExistence = CheckExistenceProperty.Get(param.Metadata); var container = new Grid(); var textBox = new TextBox { MaxLength = 256 }; textBox.TextChanged += (sender, args) => updateCallback(); container.Children.Add(textBox); Button button = null; if (ShowSelectorProperty.Get(param.Metadata)) { button = new Button { Content = "...", HorizontalAlignment = HorizontalAlignment.Right, Width = 25 }; textBox.Margin = new Thickness { Right = ViewConstants.MinorSpacing + button.Width }; button.Click += (sender, args) => { switch (pathType) { case PathType.File: var openFileDialog = new OpenFileDialog { Title = $"Select File: {param.Name}", Multiselect = false, CheckFileExists = checkExistence, Filter = FilterProperty.Get(param.Metadata), }; if (!textBox.Text.IsBlank()) { openFileDialog.InitialDirectory = new FileInfo(textBox.Text).Directory?.FullName ?? ""; } if ((bool)openFileDialog.ShowDialog(Window.GetWindow(button))) { textBox.Text = openFileDialog.FileName; } break; case PathType.Directory: using (var dialog = new System.Windows.Forms.FolderBrowserDialog()) { if (!textBox.Text.IsBlank()) { dialog.SelectedPath = textBox.Text; } var result = dialog.ShowDialog(); if (result == System.Windows.Forms.DialogResult.OK) { textBox.Text = dialog.SelectedPath; } } break; } }; container.Children.Add(button); } void Setter(object file) => textBox.Text = ((Path)file)?.Value ?? ""; object Getter() => new Path(textBox.Text); bool Validate(object value) { if (!(value is Path file)) { return(false); } if (checkExistence) { switch (pathType) { case PathType.File: if (!File.Exists(file.Value)) { return(false); } break; case PathType.Directory: if (!Directory.Exists(file.Value)) { return(false); } break; } } return(param.IsValid(value)); } void Updater(ParameterStateType state, bool value) { switch (state) { case ParameterStateType.Enabled: textBox.IsEnabled = value; if (button != null) { button.IsEnabled = value; } break; case ParameterStateType.Valid: textBox.Background = value ? Brushes.Transparent : new SolidColorBrush(ViewConstants.InvalidColor); break; } } return(new PresentedParameter(param, container, new PresentedParameter.ParamDelegates(Getter, Setter, Validate, Updater))); }
public override IReadOnlyCollection <IParameterDescriptor> GetParameters(IParameterDescriptor parameter) => ColorKeysProperty.Get(parameter.Metadata).Select(GetParameter).ToArray <IParameterDescriptor>();
public override IReadOnlyCollection <IParameterDescriptor> GetParameters(IParameterDescriptor parameter) => FieldsProperty.Get(parameter.Metadata);
public PresentedParameter Present(IParameterDescriptor param, Action updateCallback) { var container = new Grid(); var checkFileExistence = CheckFileExistenceProperty.Get(param.Metadata); var textBox = new TextBox { MaxLength = 256 }; textBox.TextChanged += (sender, args) => updateCallback(); container.Children.Add(textBox); var supportedSchemes = new HashSet <string>((SupportedSchemesProperty.GetOrDefault(param.Metadata) ?? EmptyArray <string> .Instance).Select(scheme => scheme.ToLowerInvariant())); Button button = null; if ((!supportedSchemes.Any() || supportedSchemes.Contains("file")) && ShowFileSelectorProperty.GetOrDefault(param.Metadata)) { button = new Button { Content = "...", HorizontalAlignment = HorizontalAlignment.Right, Width = 25 }; textBox.Margin = new Thickness { Right = ViewConstants.MinorSpacing + button.Width }; button.Click += (sender, args) => { var dialog = new OpenFileDialog { Title = $"Select File: {param.Name}", Multiselect = false, CheckFileExists = CheckFileExistenceProperty.Get(param.Metadata), Filter = FileFilterProperty.Get(param.Metadata), }; if (!textBox.Text.IsBlank()) { Uri uri = null; try { uri = new Uri(textBox.Text, UriKind.RelativeOrAbsolute); } catch (Exception) { /* ignored */ } if (uri?.IsFile ?? false) { var localPath = uri.LocalPath; dialog.InitialDirectory = new FileInfo(localPath).Directory?.FullName ?? ""; } } if ((bool)dialog.ShowDialog(Window.GetWindow(button))) { textBox.Text = "file:\\" + dialog.FileName; } }; container.Children.Add(button); } void Setter(object uri) => textBox.Text = uri?.ToString() ?? ""; object Getter() => new Uri(textBox.Text); bool Validator(object val) { if (val is Uri uri) { if (!supportedSchemes.Any() && !supportedSchemes.Contains(uri.Scheme.ToLowerInvariant())) { return(false); } if (string.Equals(uri.Scheme, "file", StringComparison.OrdinalIgnoreCase) && checkFileExistence && !File.Exists(uri.LocalPath)) { return(false); } } return(param.IsValid(val)); } void Updater(ParameterStateType state, bool value) { switch (state) { case ParameterStateType.Enabled: textBox.IsEnabled = value; if (button != null) { button.IsEnabled = value; } break; case ParameterStateType.Valid: textBox.Background = value ? Brushes.Transparent : new SolidColorBrush(ViewConstants.InvalidColor); break; } } return(new PresentedParameter(param, container, new PresentedParameter.ParamDelegates(Getter, Setter, Validator, Updater))); }
public PresentedParameter PresentDirectly(IParameterDescriptor param, Action updateCallback) { var factory = param.GetParameterizedObjectFactory(); var subParameters = factory.GetParameters(param).ToArray(); var subParameterCount = subParameters.Length; var presentedSubParams = new PresentedParameter[subParameterCount]; var grid = new Grid(); if (subParameterCount == 0) { grid.Children.Add(new TextBlock { Text = "<EMPTY>", Foreground = Brushes.DimGray }); } else { var labelVisible = ParamLabelVisibilityProperty.Get(param.Metadata); if (labelVisible) { grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); } grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); for (var i = 0; i < subParameterCount; i++) { if (i != 0) { grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(ViewConstants.MinorSpacing, GridUnitType.Pixel) }); } grid.ColumnDefinitions.Add(new ColumnDefinition()); } for (var i = 0; i < subParameterCount; i++) { var columnIndex = i * 2; var subParam = subParameters[i]; if (labelVisible) { var nameTextBlock = ViewHelper.CreateParamNameTextBlock(subParam); nameTextBlock.FontSize = 8; nameTextBlock.TextWrapping = TextWrapping.NoWrap; nameTextBlock.TextAlignment = TextAlignment.Left; grid.Children.Add(nameTextBlock); Grid.SetRow(nameTextBlock, 0); Grid.SetColumn(nameTextBlock, columnIndex); } var presentedSubParam = presentedSubParams[i] = subParam.GetPresenter().Present(subParam, updateCallback); grid.Children.Add(presentedSubParam.Element); if (labelVisible) { Grid.SetRow(presentedSubParam.Element, 1); } Grid.SetColumn(presentedSubParam.Element, columnIndex); GridLength columnWidth; if (ColumnWidthProperty.TryGet(subParam.Metadata, out var propertyValue)) { columnWidth = propertyValue; } else if (presentedSubParam.Element.GetType() == typeof(CheckBox)) { columnWidth = GridLength.Auto; } else { columnWidth = ColumnWidthProperty.DefaultValue; } grid.ColumnDefinitions[columnIndex].Width = columnWidth; } } void Setter(object obj) { var context = factory.Parse(param, (IParameterizedObject)obj); for (var i = 0; i < subParameters.Length; i++) { if (context.TryGet(subParameters[i], out var val)) { presentedSubParams[i].Delegates.Setter(val); } } } object Getter() { var dict = new Context(); for (var i = 0; i < subParameters.Length; i++) { dict.Set(subParameters[i], presentedSubParams[i].Delegates.Getter()); } return(factory.Create(param, dict)); } bool Validator(object value) { var context = factory.Parse(param, (IParameterizedObject)value); var flag = true; for (var i = 0; i < subParameters.Length; i++) { var subParam = subParameters[i]; bool valid; try { var subValue = context.TryGet(subParam, out var val) ? val : subParam.DefaultValue; valid = presentedSubParams[i].Delegates.Validator?.Invoke(subValue) ?? true; } catch (Exception) { valid = false; } presentedSubParams[i].Delegates.Updater?.Invoke(ParameterStateType.Valid, valid); if (!valid) { flag = false; } } return(flag); } void Updater(ParameterStateType state, bool value) { if (state != ParameterStateType.Enabled) { return; } if (value) { var context = factory.Parse(param, (IParameterizedObject)Getter()); for (var i = 0; i < subParameters.Length; i++) { presentedSubParams[i].Delegates.Updater?.Invoke(ParameterStateType.Enabled, factory.IsEnabled(context, subParameters[i])); } } else { foreach (var presentedSubParam in presentedSubParams) { presentedSubParam.Delegates.Updater?.Invoke(ParameterStateType.Enabled, false); } } } return(new PresentedParameter(param, grid, Getter, Setter, Validator, Updater)); }
public PresentedParameter Present1D(IParameterDescriptor param, Action updateCallback) { var checkboxSize = CheckboxSizeProperty.Get(param.Metadata); var labelVisible = Position1DLabelVisibilityProperty.Get(param.Metadata); var enumNames = param.ValueType.GetEnumNames(); var enumValues = param.ValueType.GetEnumValues(); var checkboxes = new Rectangle[enumNames.Length]; var selectedIndex = -1; void Select(int index) { if (selectedIndex == index) { return; } for (var i = 0; i < checkboxes.Length; i++) { checkboxes[i].Fill = i == index ? Brushes.DimGray : Brushes.White; } selectedIndex = index; } var grid = new Grid(); if (labelVisible) { grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); /* Name row */ } grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); /* Checkbox row */ for (var i = 0; i < enumNames.Length; i++) { var index = i; /* Used in closure */ grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); if (labelVisible) { var nameTextBlock = new TextBlock { Text = enumNames[index], HorizontalAlignment = HorizontalAlignment.Center, FontSize = 9 }; grid.Children.Add(nameTextBlock); Grid.SetRow(nameTextBlock, 0); Grid.SetColumn(nameTextBlock, index); } var checkbox = checkboxes[index] = new Rectangle { HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(2), Width = checkboxSize, Height = checkboxSize, Stroke = Brushes.Black, Fill = Brushes.White }; checkbox.MouseLeftButtonUp += (sender, e) => Select(index); grid.Children.Add(checkbox); if (labelVisible) { Grid.SetRow(checkbox, 1); } Grid.SetColumn(checkbox, index); } void Setter(object val) => Select((int)val); object Getter() => enumValues.GetValue(selectedIndex); void Updater(ParameterStateType state, bool value) { switch (state) { case ParameterStateType.Enabled: foreach (var checkbox in checkboxes) { checkbox.IsEnabled = value; } break; case ParameterStateType.Valid: for (var i = 0; i < checkboxes.Length; i++) { checkboxes[i].Fill = value ? (i == selectedIndex ? Brushes.DimGray : Brushes.White) : new SolidColorBrush(ViewConstants.InvalidColor); } break; } } return(new PresentedParameter(param, grid, new PresentedParameter.ParamDelegates(Getter, Setter, param.IsValid, Updater))); }
public PresentedParameter Present2D(IParameterDescriptor param, Action updateCallback) { var checkboxSize = CheckboxSizeProperty.Get(param.Metadata); var enumNames = param.ValueType.GetEnumNames(); var enumValues = param.ValueType.GetEnumValues(); var checkboxes = new Rectangle[enumNames.Length]; var selectedIndex = -1; void Select(int index) { if (selectedIndex == index) { return; } for (var i = 0; i < checkboxes.Length; i++) { checkboxes[i].Fill = i == index ? Brushes.DimGray : Brushes.White; } selectedIndex = index; } var grid = new Grid { VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 3, 0, 3) }; switch (Position2DHorizontalAlignmentProperty.Get(param.Metadata)) { case PositionH1D.Left: grid.HorizontalAlignment = HorizontalAlignment.Left; break; case PositionH1D.Right: grid.HorizontalAlignment = HorizontalAlignment.Right; break; default: grid.HorizontalAlignment = HorizontalAlignment.Center; break; } for (var i = 0; i < 3; i++) { grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); } for (var i = 0; i < enumValues.Length; i++) { var index = i; /* Used in closure */ var rowIndex = index / 3; var colIndex = index % 3; var checkbox = checkboxes[index] = new Rectangle { HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(4), Width = checkboxSize, Height = checkboxSize, Stroke = Brushes.Black, Fill = Brushes.White, ToolTip = enumNames[index] }; checkbox.MouseLeftButtonUp += (sender, e) => Select(index); grid.Children.Add(checkbox); Grid.SetRow(checkbox, rowIndex); Grid.SetColumn(checkbox, colIndex); } void Setter(object val) => Select((int)val); object Getter() => enumValues.GetValue(selectedIndex); void Updater(ParameterStateType state, bool value) { switch (state) { case ParameterStateType.Enabled: foreach (var checkbox in checkboxes) { checkbox.IsEnabled = value; } break; case ParameterStateType.Valid: for (var i = 0; i < checkboxes.Length; i++) { checkboxes[i].Fill = value ? (i == selectedIndex ? Brushes.DimGray : Brushes.White) : new SolidColorBrush(ViewConstants.InvalidColor); } break; } } return(new PresentedParameter(param, grid, new PresentedParameter.ParamDelegates(Getter, Setter, param.IsValid, Updater))); }