Skip to content

Commit

Permalink
Show component output ports
Browse files Browse the repository at this point in the history
  • Loading branch information
KanaHayama committed Oct 9, 2023
1 parent 565fd56 commit 0b4a4bc
Show file tree
Hide file tree
Showing 11 changed files with 357 additions and 66 deletions.
56 changes: 1 addition & 55 deletions WpfApplication/Pipeline/OutputSelectionControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,7 @@ public OutputSelectionControl(ComponentConfiguration configuration, InputConfigu
Configurations = configurations;

var inputMetadata = configuration.FindPortMetadata(inputConfiguration.LocalPort);
var localOutputs = configuration.FindOutputPortDataTypes(configurations);
var localInputs = configuration.FindInputPortDataTypes(configurations, inputMetadata);
string inputDataTypeName;
if (inputMetadata.CanConnectDataType(null, localOutputs, localInputs)) {
inputDataTypeName = "Any";
} else {
var inputPortDataType = inputMetadata.GetTransmissionDataType(null, localOutputs, localInputs);
if (inputPortDataType is null) {
inputDataTypeName = "Unknown";
} else {
inputDataTypeName = GetCSharpStyleTypeName(inputPortDataType);
}
}
var inputDataTypeName = TypeDisplayHelpers.FindInputDataTypeName(configuration, inputMetadata, configurations);
TextBlockPortDataType.Text = inputDataTypeName;

var selections = LegalOutputSelections(configuration, inputConfiguration, configurations);
Expand All @@ -104,47 +92,5 @@ private void ListBoxOutputs_SelectionChanged(object sender, RoutedEventArgs e) {
};
RemoveIllegalInputs(Configurations);
}

private static readonly Dictionary<Type, string> TypeMappings = new Dictionary<Type, string>() {
{ typeof(object), "object" },
{ typeof(string), "string" },
{ typeof(bool), "bool" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(int), "int" },
{ typeof(uint), "uint" },
{ typeof(long), "long" },
{ typeof(ulong), "ulong" },
{ typeof(short), "short" },
{ typeof(ushort), "ushort" },

};

private static string GetCSharpStyleTypeName(Type type) {
if (TypeMappings.TryGetValue(type, out var name)) {
return name;
}
if (type.IsArray) {
return $"{GetCSharpStyleTypeName(type.GetElementType())}[]";
}
name = $"{type.Namespace}.{type.Name}";
if (!type.IsGenericType) {
return name;
}
var sb = new StringBuilder();
var innerText = string.Join(", ", type.GetGenericArguments().Select(GetCSharpStyleTypeName));
if (name.StartsWith("System.ValueTuple`")) {
sb.Append('(');
sb.Append(innerText);
sb.Append(')');
} else {
sb.Append(name.Substring(0, name.IndexOf('`')));
sb.Append('<');
sb.Append(innerText);
sb.Append('>');
}
var result = sb.ToString();
return result;
}
}
}
44 changes: 39 additions & 5 deletions WpfApplication/Pipeline/PipelineEditorWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
<ColumnDefinition />
<ColumnDefinition
Width="auto"
MinWidth="16"/>
MinWidth="16" />
<ColumnDefinition
Width="auto"
MinWidth="16" />
Expand Down Expand Up @@ -153,7 +153,10 @@
<Grid.RowDefinitions>
<RowDefinition
Height="auto" />
<RowDefinition />
<RowDefinition
Height="1*" />
<RowDefinition
Height="auto"/>
</Grid.RowDefinitions>

<GroupBox
Expand All @@ -162,9 +165,40 @@

<GroupBox
Grid.Row="1"
Header="Connection">
<Grid
Name="ContentControlConnection" />
Header="Connection"
Name="ContentControlConnection">
</GroupBox>

<GroupBox
Grid.Row="2"
>
<GroupBox.Header>
<ToggleButton
Name="ToggleButtonOutputs">
Show Outputs
</ToggleButton>
</GroupBox.Header>

<ContentControl
Name="OutputPortInspector">
<ContentControl.Style>
<Style
TargetType="ContentControl">
<Setter
Property="Visibility"
Value="Collapsed" />
<Style.Triggers>
<DataTrigger
Binding="{Binding IsChecked, ElementName=ToggleButtonOutputs}"
Value="True">
<Setter
Property="Visibility"
Value="Visible" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentControl.Style>
</ContentControl>
</GroupBox>
</Grid>
<GridSplitter
Expand Down
9 changes: 6 additions & 3 deletions WpfApplication/Pipeline/PipelineEditorWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Microsoft.Psi;
using OpenSense.Components;
using OpenSense.Pipeline;
using OpenSense.WPF.Views.Editors;
using OpenSense.WPF.Views.Runners;

namespace OpenSense.WPF.Pipeline {
Expand Down Expand Up @@ -91,14 +92,16 @@ private void ListBoxInstances_SelectionChanged(object sender, SelectionChangedEv
var configuration = (ComponentConfiguration)ListBoxInstances.SelectedItem;
var configurations = ListBoxInstances.ItemsSource.Cast<ComponentConfiguration>().ToArray();
ContentControlComponentBasics.Content = null;
ContentControlConnection.Children.Clear();
ContentControlConnection.Content = null;
ContentControlConnection.DataContext = configuration;
OutputPortInspector.Content = null;
OutputPortInspector.DataContext = configuration;
ContentControlSettings.Children.Clear();
ContentControlSettings.DataContext = ListBoxInstances.SelectedItem;
if (configuration != null) {
ContentControlComponentBasics.Content = new InstanceBasicInformationControl(configuration);
var connection = new InstanceConnectionControl(configuration, configurations);
ContentControlConnection.Children.Add(connection);
ContentControlConnection.Content = new InstanceConnectionControl(configuration, configurations);
OutputPortInspector.Content = new OutputPortsInspector(configuration, configurations);
try {
var manager = new ConfigurationControlCreatorManager();//throws ReflectionTypeLoadException
var control = manager.Create(configuration);
Expand Down
2 changes: 1 addition & 1 deletion WpfApplication/Pipeline/PortSelectionWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
Binding="{Binding Description}"
Width="*" />
<DataGridTextColumn
Header="Type"
Header="Kind"
Binding="{Binding Aggregation, Mode=OneWay}" />
</DataGrid.Columns>
</DataGrid>
Expand Down
89 changes: 89 additions & 0 deletions WpfApplication/Pipeline/TypeDisplayHelpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using OpenSense.Components;

namespace OpenSense.WPF.Pipeline {
internal static class TypeDisplayHelpers {

public static string FindInputDataTypeName(ComponentConfiguration configuration, IPortMetadata inputMetadata, IReadOnlyList<ComponentConfiguration> configurations) {
Debug.Assert(inputMetadata.Direction == PortDirection.Input);
var localOutputs = configuration.FindOutputPortDataTypes(configurations);
var localInputs = configuration.FindInputPortDataTypes(configurations, exclude: inputMetadata);
string result;
if (inputMetadata.CanConnectDataType(null, localOutputs, localInputs)) {
result = "Any Type";
} else {
var inputPortDataType = inputMetadata.GetTransmissionDataType(null, localOutputs, localInputs);
if (inputPortDataType is null) {
result = "Type Unknown";
} else {
result = GetCSharpStyleTypeName(inputPortDataType);
}
}
return result;
}

public static string FindOutputDataTypeName(ComponentConfiguration configuration, IPortMetadata outputMetadata, IReadOnlyList<ComponentConfiguration> configurations) {
Debug.Assert(outputMetadata.Direction == PortDirection.Output);
var localInputs = configuration.FindInputPortDataTypes(configurations);
var localOutputs = configuration.FindOutputPortDataTypes(configurations, exclude: outputMetadata);
string result;
if (outputMetadata.CanConnectDataType(null, localInputs, localOutputs)) {
result = "Any Type";
} else {
var outputPortDataType = outputMetadata.GetTransmissionDataType(null, localInputs, localOutputs);
if (outputPortDataType is null) {
result = "Type Unknown";
} else {
result = GetCSharpStyleTypeName(outputPortDataType);
}
}
return result;
}

private static string GetCSharpStyleTypeName(Type type) {
if (TypeMappings.TryGetValue(type, out var name)) {
return name;
}
if (type.IsArray) {
return $"{GetCSharpStyleTypeName(type.GetElementType())}[]";
}
name = $"{type.Namespace}.{type.Name}";
if (!type.IsGenericType) {
return name;
}
var sb = new StringBuilder();
var innerText = string.Join(", ", type.GetGenericArguments().Select(GetCSharpStyleTypeName));
if (name.StartsWith("System.ValueTuple`")) {
sb.Append('(');
sb.Append(innerText);
sb.Append(')');
} else {
sb.Append(name.Substring(0, name.IndexOf('`')));
sb.Append('<');
sb.Append(innerText);
sb.Append('>');
}
var result = sb.ToString();
return result;
}

private static readonly Dictionary<Type, string> TypeMappings = new Dictionary<Type, string>() {
{ typeof(object), "object" },
{ typeof(string), "string" },
{ typeof(bool), "bool" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(int), "int" },
{ typeof(uint), "uint" },
{ typeof(long), "long" },
{ typeof(ulong), "ulong" },
{ typeof(short), "short" },
{ typeof(ushort), "ushort" },

};
}
}
62 changes: 62 additions & 0 deletions WpfApplication/Views/Editors/OutputPortView.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<UserControl
x:Class="OpenSense.WPF.Views.Editors.OutputPortView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OpenSense.WPF.Views.Editors"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800">
<UserControl.Resources>
<Style
x:Key="HideDescriptionStyle"
TargetType="TextBlock">
<Style.Triggers>
<DataTrigger
Binding="{Binding Description}"
Value="{x:Null}">
<Setter
Property="Visibility"
Value="Collapsed" />
</DataTrigger>
<DataTrigger
Binding="{Binding Description}"
Value="">
<Setter
Property="Visibility"
Value="Collapsed" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
</Grid.ColumnDefinitions>

<Grid.RowDefinitions>
<RowDefinition
Height="auto" />
<RowDefinition
Height="auto" />
<RowDefinition
Height="auto" />
</Grid.RowDefinitions>

<TextBlock
Grid.Row="0"
FontWeight="Bold"
Text="{Binding Name}" />

<TextBlock
Grid.Row="2"
Foreground="SlateGray"
Style="{StaticResource HideDescriptionStyle}"
Text="{Binding Description}" />

<TextBlock
Grid.Row="1"
Text="{Binding Type}" />
</Grid>
</UserControl>
9 changes: 9 additions & 0 deletions WpfApplication/Views/Editors/OutputPortView.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Windows.Controls;

namespace OpenSense.WPF.Views.Editors {
public sealed partial class OutputPortView : UserControl {
public OutputPortView() {
InitializeComponent();
}
}
}
18 changes: 18 additions & 0 deletions WpfApplication/Views/Editors/OutputPortViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#nullable enable

namespace OpenSense.WPF.Views.Editors {
internal sealed class OutputPortViewModel {

public string Name { get; }

public string? Description { get; }

public string Type { get; }

public OutputPortViewModel(string name, string? description, string type) {
Name = name;
Description = description;
Type = type;
}
}
}
38 changes: 38 additions & 0 deletions WpfApplication/Views/Editors/OutputPortsInspector.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<UserControl
x:Class="OpenSense.WPF.Views.Editors.OutputPortsInspector"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:OpenSense.WPF.Views.Editors"
mc:Ignorable="d"
d:DesignHeight="450"
d:DesignWidth="800"
Unloaded="UserControl_Unloaded">
<Grid>
<TextBlock
Visibility="Collapsed"
d:Visibility="Visible"
Name="TextBlockNoOutputs"
VerticalAlignment="Center"
HorizontalAlignment="Center">
No Outpus
</TextBlock>

<ScrollViewer
Visibility="Visible"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto">
<ItemsControl
Grid.Column="0"
Name="ListBoxPorts">
<ItemsControl.ItemTemplate>
<DataTemplate>
<local:OutputPortView
Margin="5" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</ScrollViewer>
</Grid>
</UserControl>
Loading

0 comments on commit 0b4a4bc

Please sign in to comment.