Skip to content

Commit 90e8a34

Browse files
committed
Ensure only visible tabs are selected
1 parent 8ecc496 commit 90e8a34

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Microsoft.Toolkit.Uwp.UI.Controls.Input/ColorPicker/ColorPicker.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ namespace Microsoft.Toolkit.Uwp.UI.Controls
4040
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground8Border), Type = typeof(Border))]
4141
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground9Border), Type = typeof(Border))]
4242
[TemplatePart(Name = nameof(ColorPicker.CheckeredBackground10Border), Type = typeof(Border))]
43+
[TemplatePart(Name = nameof(ColorPicker.ColorPanelSelector), Type = typeof(ListBox))]
4344
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumControl), Type = typeof(ColorSpectrum))]
4445
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumAlphaSlider), Type = typeof(ColorPickerSlider))]
4546
[TemplatePart(Name = nameof(ColorPicker.ColorSpectrumThirdDimensionSlider), Type = typeof(ColorPickerSlider))]
@@ -78,6 +79,7 @@ public partial class ColorPicker : Microsoft.UI.Xaml.Controls.ColorPicker
7879
private Color? updatedRgbColor = null;
7980
private DispatcherQueueTimer dispatcherQueueTimer = null;
8081

82+
private ListBox ColorPanelSelector;
8183
private ColorSpectrum ColorSpectrumControl;
8284
private ColorPickerSlider ColorSpectrumAlphaSlider;
8385
private ColorPickerSlider ColorSpectrumThirdDimensionSlider;
@@ -177,6 +179,8 @@ protected override void OnApplyTemplate()
177179
// We need to disconnect old events first
178180
this.ConnectEvents(false);
179181

182+
this.ColorPanelSelector = this.GetTemplateChild<ListBox>(nameof(ColorPanelSelector));
183+
180184
this.ColorSpectrumControl = this.GetTemplateChild<ColorSpectrum>(nameof(ColorSpectrumControl));
181185
this.ColorSpectrumAlphaSlider = this.GetTemplateChild<ColorPickerSlider>(nameof(ColorSpectrumAlphaSlider));
182186
this.ColorSpectrumThirdDimensionSlider = this.GetTemplateChild<ColorPickerSlider>(nameof(ColorSpectrumThirdDimensionSlider));
@@ -216,6 +220,7 @@ protected override void OnApplyTemplate()
216220

217221
base.OnApplyTemplate();
218222
this.UpdateVisualState(false);
223+
this.ValidateSelectedPanel();
219224
this.isInitialized = true;
220225
this.SetActiveColorRepresentation(ColorRepresentation.Rgba);
221226
this.UpdateColorControlValues(); // TODO: This will also connect events after, can we optimize vs. doing it twice with the ConnectEvents above?
@@ -1065,6 +1070,48 @@ private void SetDefaultPalette()
10651070
return;
10661071
}
10671072

1073+
/// <summary>
1074+
/// Validates and updates the current 'tab' or 'panel' selection.
1075+
/// If the currently selected tab is collapsed, the next visible tab will be selected.
1076+
/// </summary>
1077+
private void ValidateSelectedPanel()
1078+
{
1079+
object selectedItem = null;
1080+
1081+
if (this.ColorPanelSelector != null)
1082+
{
1083+
if (this.ColorPanelSelector.SelectedItem == null &&
1084+
this.ColorPanelSelector.Items.Count > 0)
1085+
{
1086+
// As a failsafe, forcefully select the first item
1087+
selectedItem = this.ColorPanelSelector.Items[0];
1088+
}
1089+
else
1090+
{
1091+
selectedItem = this.ColorPanelSelector.SelectedItem;
1092+
}
1093+
1094+
if (selectedItem is UIElement selectedElement &&
1095+
selectedElement.Visibility == Visibility.Collapsed)
1096+
{
1097+
// Select the first visible item instead
1098+
foreach (object item in this.ColorPanelSelector.Items)
1099+
{
1100+
if (item is UIElement element &&
1101+
element.Visibility == Visibility.Visible)
1102+
{
1103+
selectedItem = item;
1104+
break;
1105+
}
1106+
}
1107+
}
1108+
1109+
this.ColorPanelSelector.SelectedItem = selectedItem;
1110+
}
1111+
1112+
return;
1113+
}
1114+
10681115
/***************************************************************************************
10691116
*
10701117
* Color Update Timer

0 commit comments

Comments
 (0)