Skip to content
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
27 changes: 27 additions & 0 deletions Terminal.Gui/Views/Selectors/OptionSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,42 @@ private void Cycle ()
/// Updates the checked state of all checkbox subviews so that only the checkbox corresponding
/// to the current <see cref="SelectorBase.Value"/> is checked.
/// </summary>
/// <remarks>
/// If <see cref="SelectorBase.Value"/> doesn't exist in the list of checkbox values, then the first checkbox will be checked by default
/// and will raise the <see cref="SelectorBase.ValueChanging"/>/<see cref="SelectorBase.ValueChanged"/> events.
/// </remarks>
public override void UpdateChecked ()
{
Dictionary<CheckBox, int> checkBoxValueMap = SubViews.OfType<CheckBox> ().ToDictionary (cb => cb, GetCheckBoxValue);

foreach (CheckBox cb in SubViews.OfType<CheckBox> ())
{
int value = GetCheckBoxValue (cb);

cb.Value = value == Value ? CheckState.Checked : CheckState.UnChecked;
}

// If Value doesn't exist in any checkbox, use the first checkbox's value
if (Value is not null && checkBoxValueMap.Count > 0 && Values!.All (v => v != Value) && checkBoxValueMap.Values.All (v => v != Value))
{
Value = checkBoxValueMap.Values.First ();

Comment thread
BDisp marked this conversation as resolved.
foreach (KeyValuePair<CheckBox, int> kvp in checkBoxValueMap)
{
if (kvp.Value != Value)
{
continue;
}
kvp.Key.Value = CheckState.Checked;

break;
}

// Sanity checks to verify the assumptions above
Debug.Assert (checkBoxValueMap.Values.First () != (int)SubViews.OfType<CheckBox> ().First ().Value);
Debug.Assert (checkBoxValueMap.Values.First () == Values! [0]);
}
Comment thread
BDisp marked this conversation as resolved.

// Verify at most one is checked
Debug.Assert (SubViews.OfType<CheckBox> ().Count (cb => cb.Value == CheckState.Checked) <= 1);
}
Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Views/Selectors/SelectorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ public SelectorStyles Styles
field = value;

CreateSubViews ();
UpdateChecked ();
}
}

Expand Down Expand Up @@ -316,7 +315,6 @@ public virtual IReadOnlyList<int>? Values
}

CreateSubViews ();
UpdateChecked ();
}
}

Expand All @@ -331,7 +329,6 @@ public IReadOnlyList<string>? Labels
field = value;

CreateSubViews ();
UpdateChecked ();
}
}

Expand Down Expand Up @@ -436,6 +433,9 @@ public virtual void CreateSubViews ()
// Note: Hotkey assignment is now handled automatically by the base class
// when SubViews are added via Add(). No need to call AssignUniqueHotKeys() here.
SetLayout ();

// Ensure the checked state of the checkboxes is correct after recreating subviews
UpdateChecked ();
Comment thread
BDisp marked this conversation as resolved.
}

/// <summary>
Expand Down
55 changes: 55 additions & 0 deletions Tests/UnitTestsParallelizable/Views/SelectorBaseTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,61 @@ public void CreateSubViews_SetsCheckBoxProperties ()
Assert.True (checkBox.CanFocus);
}

[Fact]
public void CreateSubViews_ResetsValue_WhenCurrentValueNotInNewValues ()
{
OptionSelector selector = new ();

selector.Labels = ["Option1", "Option2"];
selector.Values = [10, 20];

// Verify initial value is set to first value (10)
Assert.Equal (10, selector.Value);

// Change to new labels/values where current value (10) is not present
selector.Labels = ["New1", "New2"];
selector.Values = [30, 40];

// Value should reset to first value (30)
Assert.Equal (30, selector.Value);
}

[Fact]
public void Setting_TabBehavior_AfterSubViewsAreCreated_DoesNotLoseCheckedState ()
{
OptionSelector selector = new ()
{
Labels = ["Option1", "Option2"], Values = [10, 20], Orientation = Orientation.Vertical, TabBehavior = TabBehavior.NoStop
};

List<CheckBox> checkBoxes = [.. selector.SubViews.OfType<CheckBox> ()];

Assert.Equal (2, checkBoxes.Count);
Assert.Equal (CheckState.Checked, checkBoxes [0].Value);
Assert.Equal (CheckState.UnChecked, checkBoxes [1].Value);
Assert.Equal (10, selector.Value);
Assert.Equal (TabBehavior.NoStop, selector.TabBehavior);
}

[Fact]
public void Setting_TabBehavior_AfterSubViewsAreCreated_DoesNotLoseCheckedState_With_Enum ()
{
OptionSelector<Side> selector = new ()
{
Orientation = Orientation.Vertical, TabBehavior = TabBehavior.NoStop
};

List<CheckBox> checkBoxes = [.. selector.SubViews.OfType<CheckBox> ()];

Assert.Equal (4, checkBoxes.Count);
Assert.Equal (CheckState.Checked, checkBoxes [0].Value);
Assert.Equal (CheckState.UnChecked, checkBoxes [1].Value);
Assert.Equal (CheckState.UnChecked, checkBoxes [2].Value);
Assert.Equal (CheckState.UnChecked, checkBoxes [3].Value);
Assert.Equal (Side.Left, selector.Value);
Assert.Equal (TabBehavior.NoStop, selector.TabBehavior);
}

#endregion

#region HotKey Command Tests
Expand Down
Loading