Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow refreshing property grid on value changed #13

Merged
merged 1 commit into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/Bonsai.Gui/PropertyGridBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class PropertyGridBuilder : ControlBuilderBase
{
internal readonly BehaviorSubject<bool> _HelpVisible = new(true);
internal readonly BehaviorSubject<bool> _ToolbarVisible = new(true);
internal readonly BehaviorSubject<bool> _RefreshProperties = new(false);

/// <summary>
/// Gets or sets a value specifying whether the help text box is visible.
Expand All @@ -35,5 +36,17 @@ public bool ToolbarVisible
get => _ToolbarVisible.Value;
set => _ToolbarVisible.OnNext(value);
}

/// <summary>
/// Gets or sets a value specifying whether the property grid should refresh
/// whenever the value of any property changes.
/// </summary>
[Category(nameof(CategoryAttribute.Behavior))]
[Description("Specifies whether the property grid should refresh whenever the value of any property changes.")]
public bool RefreshProperties
{
get => _RefreshProperties.Value;
set => _RefreshProperties.OnNext(value);
}
}
}
24 changes: 24 additions & 0 deletions src/Bonsai.Gui/PropertyGridVisualizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,42 @@ public class PropertyGridVisualizer : ControlVisualizerBase<PropertyGrid, Proper
/// <inheritdoc/>
protected override PropertyGrid CreateControl(IServiceProvider provider, PropertyGridBuilder builder)
{
var handlerRegistered = false;
var propertyGrid = new PropertyGrid();
propertyGrid.Tag = builder;
propertyGrid.Dock = DockStyle.Fill;
propertyGrid.Size = new Size(350, 450);
propertyGrid.Site = new ServiceProviderContext(provider);
propertyGrid.SubscribeTo(builder._HelpVisible, value => propertyGrid.HelpVisible = value);
propertyGrid.SubscribeTo(builder._ToolbarVisible, value => propertyGrid.ToolbarVisible = value);
propertyGrid.SubscribeTo(builder._RefreshProperties, value =>
{
if (value)
{
propertyGrid.Refresh();
if (!handlerRegistered)
{
propertyGrid.PropertyValueChanged += PropertyGrid_PropertyValueChanged;
handlerRegistered = true;
}
}
});

var workflowBuilder = (WorkflowBuilder)provider.GetService(typeof(WorkflowBuilder));
propertyGrid.SelectedObject = GetExpressionContext(workflowBuilder.Workflow, builder);
return propertyGrid;
}

static void PropertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e)
{
var propertyGrid = (PropertyGrid)sender;
var builder = (PropertyGridBuilder)propertyGrid.Tag;
if (builder._RefreshProperties.Value)
{
propertyGrid.Refresh();
}
}

static ExpressionBuilderGraph GetExpressionContext(ExpressionBuilderGraph source, ExpressionBuilder target)
{
foreach (var element in source.Elements())
Expand Down
Loading