Skip to content

Commit 124782f

Browse files
committed
Fix DropdownControl: auto-select first item in constructor, prevent reopen on header click dismiss
1 parent 9375f25 commit 124782f

4 files changed

Lines changed: 31 additions & 2 deletions

File tree

SharpConsoleUI.Tests/Controls/DropdownControlTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ public void Color_Properties_HaveDefaults()
782782
var ffg = dd.FocusedForegroundColor;
783783
var hbg = dd.HighlightBackgroundColor;
784784
var hfg = dd.HighlightForegroundColor;
785-
Assert.NotEqual(default, bg);
785+
Assert.Equal(Color.Black, bg);
786786
}
787787

788788
[Fact]

SharpConsoleUI/Controls/DropdownControl/DropdownControl.Mouse.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ public bool ProcessMouseEvent(MouseEventArgs args)
9292
{
9393
if (isOnHeader)
9494
{
95+
// If the dropdown was just dismissed by an outside-click on this
96+
// same mouse-down, suppress so the release doesn't reopen it.
97+
if (_dismissedByOutsideClick)
98+
{
99+
_dismissedByOutsideClick = false;
100+
_isHeaderPressed = false;
101+
102+
if (!_hasFocus)
103+
SetFocus(true, FocusReason.Mouse);
104+
105+
Container?.Invalidate(true);
106+
return true;
107+
}
108+
95109
_isHeaderPressed = true;
96110

97111
// Capture focus on mouse down

SharpConsoleUI/Controls/DropdownControl/DropdownControl.Portal.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ private void OpenDropdown()
3636
{
3737
_portalContent = new DropdownPortalContent(this);
3838
_portalContent.DismissOnOutsideClick = true;
39-
_portalContent.DismissRequested += (s, e) => CloseDropdown();
39+
_portalContent.DismissRequested += (s, e) =>
40+
{
41+
_dismissedByOutsideClick = true;
42+
CloseDropdown();
43+
};
4044
_dropdownPortal = window.CreatePortal(this, _portalContent);
4145
}
4246

SharpConsoleUI/Controls/DropdownControl/DropdownControl.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public partial class DropdownControl : BaseControl, IInteractiveControl, IFocusa
5555
// Mouse state tracking
5656
private bool _isHeaderPressed;
5757
private int _mouseHoveredIndex = -1;
58+
private bool _dismissedByOutsideClick;
5859

5960
// Portal state for dropdown overlay
6061
private LayoutNode? _dropdownPortal;
@@ -92,6 +93,11 @@ public DropdownControl(string prompt = "Select an item:", IEnumerable<string>? i
9293
{
9394
_items.Add(new DropdownItem(item));
9495
}
96+
if (_items.Count > 0)
97+
{
98+
_selectedIndex = 0;
99+
_highlightedIndex = 0;
100+
}
95101
}
96102
}
97103

@@ -106,6 +112,11 @@ public DropdownControl(string prompt, IEnumerable<DropdownItem>? items = null)
106112
if (items != null)
107113
{
108114
_items.AddRange(items);
115+
if (_items.Count > 0)
116+
{
117+
_selectedIndex = 0;
118+
_highlightedIndex = 0;
119+
}
109120
}
110121
}
111122

0 commit comments

Comments
 (0)