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

Fix dropdown input nodes when running from player with Dynamo open #13403

Merged
merged 4 commits into from
Nov 9, 2022
Merged
Changes from 3 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
58 changes: 29 additions & 29 deletions src/Libraries/CoreNodeModels/DropDown.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
Expand Down Expand Up @@ -32,8 +32,7 @@ public DynamoDropDownItem(string name, object item)

public int CompareTo(object obj)
{
var a = obj as DynamoDropDownItem;
if (a == null)
if (!(obj is DynamoDropDownItem a))
return 1;

return Name.CompareTo(a);
Expand All @@ -56,7 +55,7 @@ protected DSDropDownBase()
[JsonIgnore]
public ObservableCollection<DynamoDropDownItem> Items
{
get { return items; }
get => items;
set
{
items = value;
Expand Down Expand Up @@ -91,45 +90,55 @@ public override NodeInputData InputData
/// </summary>
public int SelectedIndex
{
get { return selectedIndex; }
get => selectedIndex;
set
{
//do not allow selected index to
//go out of range of the items collection
if (value > Items.Count - 1 || value < 0)
{
Warning(Dynamo.Properties.Resources.NothingIsSelectedWarning);
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
selectedIndex = -1;
selectedString = String.Empty;
selectedString = string.Empty;
}
else
{
selectedIndex = value;
selectedString = GetSelectedStringFromItem(Items.ElementAt(value));
}

RaisePropertyChanged("SelectedIndex");
RaisePropertyChanged("SelectedString");
}
}

private string selectedString = String.Empty;
private string selectedString = string.Empty;

/// <summary>
/// String form of current selected item, so derived class
/// can save customized data
/// </summary>
public string SelectedString
{
get { return selectedString; }
get => selectedString;
set
{
if (!string.IsNullOrEmpty(value) && value != selectedString)
if (value == selectedString)
{
return;
}

if (!string.IsNullOrEmpty(value))
{
var item = Items.FirstOrDefault(i => GetSelectedStringFromItem(i).Equals(value));
// In the case that SelectedString deserialize after SelectedIndex
// With a valid item from search, get the index of item and replace the current one.
// If no exact match found, fall back to use the default selectedIndex from deserialization.
selectedIndex = item != null ?
Items.IndexOf(item) :
selectedIndex;
if (item != null)
{
selectedIndex = Items.IndexOf(item);
RaisePropertyChanged("SelectedIndex");
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
}
}

selectedString = value;
Expand Down Expand Up @@ -173,32 +182,23 @@ protected override void DeserializeCore(XmlElement nodeElement, SaveContext cont
if (selectedIndex < 0)
{
Warning(Dynamo.Properties.Resources.NothingIsSelectedWarning);
selectedString = String.Empty;
selectedString = string.Empty;
}
else
{
selectedString = selectedIndex > Items.Count - 1 ? String.Empty : GetSelectedStringFromItem(Items.ElementAt(selectedIndex));
selectedString = selectedIndex > Items.Count - 1 ? string.Empty : GetSelectedStringFromItem(Items.ElementAt(selectedIndex));
}
}

protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
{
string name = updateValueParams.PropertyName;
string value = updateValueParams.PropertyValue;

if (name == "Value" && value != null)
if (updateValueParams.PropertyName == "Value" && value != null)
{
selectedIndex = ParseSelectedIndex(value, Items);
if (selectedIndex < 0)
{
Warning(Dynamo.Properties.Resources.NothingIsSelectedWarning);
selectedString = String.Empty;
QilongTang marked this conversation as resolved.
Show resolved Hide resolved
}
else
{
selectedString = selectedIndex > Items.Count - 1 ? String.Empty : GetSelectedStringFromItem(Items.ElementAt(selectedIndex));
}
return true; // UpdateValueCore handled.
SelectedIndex = ParseSelectedIndex(value, Items);

return true;
}

return base.UpdateValueCore(updateValueParams);
Expand Down Expand Up @@ -295,7 +295,7 @@ public void PopulateItems()
var selectionState = PopulateItemsCore(currentSelection);

// Restore the selection when selectedIndex is valid
if (selectionState == SelectionState.Restore && !String.IsNullOrEmpty(currentSelection))
if (selectionState == SelectionState.Restore && !string.IsNullOrEmpty(currentSelection))
{
SelectedIndex = -1;
for (int i = 0; i < items.Count; i++)
Expand Down Expand Up @@ -337,4 +337,4 @@ protected enum SelectionState
protected abstract SelectionState PopulateItemsCore(string currentSelection);

}
}
}