17 private const int ContentPadding = 2;
18 private const int DefaultItemHeight = 24;
19 private const int ScrollBarWidth = 12;
20 private const int ScrollBarMargin = 3;
21 private const int MinimumScrollBarThumbHeight = 18;
23 private readonly List<string> _items = [];
24 private readonly List<TextBlock> _rowTextBlocks = [];
26 private int _selectedIndex = -1;
27 private int _topIndex;
28 private int _itemHeight = DefaultItemHeight;
29 private int _baseZOrder;
30 private int _mouseWheelScrollItems = 3;
32 private bool _isDraggingScrollBarThumb;
33 private float _scrollBarDragOffset;
34 private int? _scrollBarPointerId;
35 private bool _suppressScrollBarClick;
59 IEnumerable<string>? items =
null,
60 string? nickname =
null)
64 ValidateBounds(bounds).Location,
67 ArgumentNullException.ThrowIfNull(view);
69 Background = CreateBackground(renderSurfaceHost, view, bounds);
70 SelectionHighlight = CreateSelectionHighlight(renderSurfaceHost, view, GetInitialSelectionBounds(bounds));
79 CompleteInitialization(items);
94 IEnumerable<string>? items =
null,
95 string? nickname =
null)
99 ValidateBounds(bounds).Location,
102 ArgumentNullException.ThrowIfNull(sceneLayer);
104 Background = CreateBackground(renderSurfaceHost, sceneLayer, bounds);
105 SelectionHighlight = CreateSelectionHighlight(renderSurfaceHost, sceneLayer, GetInitialSelectionBounds(bounds));
114 CompleteInitialization(items);
136 public IReadOnlyList<string>
Items => _items;
142 ? Background.ScreenBounds
154 throw new ArgumentOutOfRangeException(nameof(value),
"Item height must be at least 12 pixels.");
156 if (_itemHeight == value)
161 EnsureSelectionVisible();
174 get => _verticalScrollBarVisibility;
177 if (!Enum.IsDefined(value))
178 throw new ArgumentOutOfRangeException(nameof(value));
180 if (_verticalScrollBarVisibility == value)
183 _verticalScrollBarVisibility = value;
198 get => _mouseWheelScrollItems;
199 set => _mouseWheelScrollItems = value > 0
201 :
throw new ArgumentOutOfRangeException(
203 "Mouse-wheel scrolling must advance at least one item.");
217 int clamped = Math.Clamp(value, 0, GetMaximumTopIndex());
219 if (_topIndex == clamped)
232 get => _selectedIndex;
239 public string?
SelectedItem => _selectedIndex >= 0 && _selectedIndex < _items.Count
240 ? _items[_selectedIndex]
250 ArgumentNullException.ThrowIfNull(items);
252 bool hadSelection = _selectedIndex >= 0;
253 string[] replacement = items.Select(
static item => item ??
string.Empty).ToArray();
255 _items.AddRange(replacement);
273 _items.
Add(item ??
string.Empty);
286 if (index < 0 || index >= _items.Count)
287 throw new ArgumentOutOfRangeException(nameof(index));
289 int oldSelectedIndex = _selectedIndex;
290 bool selectedItemWasRemoved = index == oldSelectedIndex;
292 _items.RemoveAt(index);
294 if (_items.Count == 0)
298 else if (index < oldSelectedIndex)
300 _selectedIndex = oldSelectedIndex - 1;
302 else if (selectedItemWasRemoved)
304 _selectedIndex = Math.Min(index, _items.Count - 1);
308 EnsureSelectionVisible();
311 if (_selectedIndex != oldSelectedIndex || selectedItemWasRemoved)
323 bool hadSelection = _selectedIndex >= 0;
342 if (index < -1 || index >= _items.Count)
343 throw new ArgumentOutOfRangeException(nameof(index));
345 if (_selectedIndex == index)
347 EnsureSelectionVisible();
351 _selectedIndex = index;
352 EnsureSelectionVisible();
353 RefreshSelectionHighlight();
364 if (_items.Count == 0)
367 int next = _selectedIndex < 0 ? 0 : Math.Min(_selectedIndex + 1, _items.Count - 1);
377 if (_items.Count == 0)
380 int previous = _selectedIndex < 0 ? 0 : Math.Max(_selectedIndex - 1, 0);
391 _baseZOrder = zOrder;
392 Background.ZOrder = zOrder;
393 SelectionHighlight.ZOrder = zOrder + 1;
395 foreach (
TextBlock row
in _rowTextBlocks)
396 row.ZOrder = zOrder + 2;
398 VerticalScrollBarTrack.ZOrder = zOrder + 3;
399 VerticalScrollBarThumb.ZOrder = zOrder + 4;
421 RefreshSelectionHighlight();
427 _isDraggingScrollBarThumb =
false;
428 _scrollBarPointerId =
null;
429 _suppressScrollBarClick =
false;
430 base.ProcessHidden();
436 base.OnPointerClick(args);
441 if (_suppressScrollBarClick || IsPointerOverScrollBar(args))
443 _suppressScrollBarClick =
false;
448 RectangleF screenBounds =
Background.GetDrawLocationScreen(args.
View);
454 if (screenBounds.Height <= 0f ||
Bounds.Height <= 0)
457 float scaleY = screenBounds.Height /
Bounds.Height;
458 float localY = args.ScreenPositionPx.Y - screenBounds.Top - ContentPadding * scaleY;
461 if (localY < 0f || rowHeightScreen <= 0f)
464 int row = (int)(localY / rowHeightScreen);
478 base.OnPointerDown(args);
480 if (_scrollBarPointerId.HasValue)
483 _suppressScrollBarClick =
false;
488 PointF local = GetLocalPointerPosition(args);
489 Rectangle thumbBounds = GetScrollBarThumbBounds(
Bounds);
490 Rectangle trackBounds = GetScrollBarTrackBounds(
Bounds);
492 if (!trackBounds.Contains(Point.Round(local)))
497 _suppressScrollBarClick =
true;
499 if (thumbBounds.Contains(Point.Round(local)))
501 _isDraggingScrollBarThumb =
true;
502 _scrollBarDragOffset = local.Y - thumbBounds.Top;
512 base.OnPointerMove(args);
517 Rectangle trackBounds = GetScrollBarTrackBounds(
Bounds);
518 Rectangle thumbBounds = GetScrollBarThumbBounds(
Bounds);
519 float travel = trackBounds.Height - thumbBounds.Height;
521 if (travel <= 0f || GetMaximumTopIndex() == 0)
524 float desiredTop = GetLocalPointerPosition(args).Y - _scrollBarDragOffset;
525 float fraction = Math.Clamp((desiredTop - trackBounds.Top) / travel, 0f, 1f);
526 TopIndex = (int)MathF.Round(fraction * GetMaximumTopIndex());
533 base.OnPointerUp(args);
535 if (_scrollBarPointerId == args.
PointerId)
537 _isDraggingScrollBarThumb =
false;
538 _scrollBarPointerId =
null;
546 base.OnMouseWheel(args);
548 if (args.
Delta == 0 || GetMaximumTopIndex() == 0)
551 long notches = Math.Max(1, Math.Abs((
long)args.
Delta) / 120);
560 base.OnKeyboardInput(args);
562 if (_items.Count == 0)
597 Math.Min(_items.Count - 1, (_selectedIndex < 0 ? 0 : _selectedIndex) +
VisibleItemCount));
607 private void CompleteInitialization(IEnumerable<string>? items)
609 SelectionHighlight.Visible =
false;
610 VerticalScrollBarTrack.Visible =
false;
611 VerticalScrollBarThumb.Visible =
false;
616 if (items is not
null)
617 _items.AddRange(items.Select(
static item => item ??
string.Empty));
622 private void EnsureSelectionVisible()
624 if (_selectedIndex < 0)
627 if (_selectedIndex < _topIndex)
629 _topIndex = _selectedIndex;
636 if (_selectedIndex > lastVisible)
638 _topIndex = Math.Min(GetMaximumTopIndex(), _selectedIndex -
VisibleItemCount + 1);
643 private void ClampTopIndex()
645 _topIndex = Math.Clamp(_topIndex, 0, GetMaximumTopIndex());
648 private int GetMaximumTopIndex()
653 private void RefreshRows()
659 foreach (TextBlock row
in _rowTextBlocks)
665 _rowTextBlocks.Clear();
670 for (
int rowIndex = 0; rowIndex < count; rowIndex++)
672 int itemIndex =
TopIndex + rowIndex;
673 Rectangle rowBounds = GetRowBounds(bounds, rowIndex);
674 TextBlock row = CreateRowText(rowBounds, _items[itemIndex]);
675 row.ZOrder = _baseZOrder + 2;
677 _rowTextBlocks.Add(row);
680 RefreshSelectionHighlight();
686 private TextBlock CreateRowText(Rectangle bounds,
string text)
688 TextBlock row =
Mode == DirectDrawingMode.View
694 worldBounds: bounds);
697 .SetFont(SKTypeface.Default, 15f, minSize: 9f)
698 .
SetColors(SKColors.White, SKColors.Transparent)
699 .
SetAlignment(SKTextAlign.Left, TextBlock.VerticalAlign.Center)
704 private void RefreshSelectionHighlight()
707 _selectedIndex >= _items.Count)
709 SelectionHighlight.Visible =
false;
713 int rowIndex = _selectedIndex -
TopIndex;
717 ? SelectionHighlight.ScreenBounds
720 Rectangle resizedBounds =
new(currentBounds.Location, selectionBounds.Size);
723 SelectionHighlight.ScreenBounds = resizedBounds;
725 SelectionHighlight.WorldBounds = resizedBounds;
729 new Vector2(ContentPadding, ContentPadding + rowIndex *
ItemHeight));
731 SelectionHighlight.Visible =
true;
734 private Rectangle GetRowBounds(Rectangle bounds,
int rowIndex)
736 int y = bounds.Y + ContentPadding + rowIndex *
ItemHeight;
739 bounds.X + ContentPadding,
743 bounds.Width - ContentPadding * 2 -
744 (NeedsVerticalScrollBar ? ScrollBarWidth + ScrollBarMargin * 2 : 0)),
745 Math.Min(
ItemHeight, Math.Max(1, bounds.Bottom - ContentPadding - y)));
748 private void RefreshScrollBar()
751 VerticalScrollBarTrack.Visible = visible;
752 VerticalScrollBarThumb.Visible = visible;
763 private Rectangle GetScrollBarTrackBounds(Rectangle bounds)
766 bounds.Right - ScrollBarMargin - ScrollBarWidth,
767 bounds.Top + ContentPadding + ScrollBarMargin,
769 Math.Max(1, bounds.Height - (ContentPadding + ScrollBarMargin) * 2));
772 private Rectangle GetScrollBarThumbBounds(Rectangle bounds)
774 Rectangle trackBounds = GetScrollBarTrackBounds(bounds);
776 int thumbHeight = _items.Count == 0
779 (
int)MathF.Round(trackBounds.Height * Math.Min(1f, (
float)
VisibleItemCount / _items.Count)),
780 Math.Min(MinimumScrollBarThumbHeight, trackBounds.Height),
783 int travel = trackBounds.Height - thumbHeight;
784 int maximumTopIndex = GetMaximumTopIndex();
785 int offset = maximumTopIndex == 0 ? 0 : (int)MathF.Round(travel * ((
float)
TopIndex / maximumTopIndex));
789 trackBounds.Top + offset,
794 private PointF GetLocalPointerPosition(WidgetPointerEventArgs args)
796 RectangleF screenBounds =
Background.GetDrawLocationScreen(args.View);
799 args.WrappedOffsetWorldPx.X * args.View.Viewport.Zoom,
800 args.WrappedOffsetWorldPx.Y * args.View.Viewport.Zoom);
802 float scaleX = screenBounds.Width /
Bounds.Width;
803 float scaleY = screenBounds.Height /
Bounds.Height;
806 Bounds.Left + (args.ScreenPositionPx.X - screenBounds.Left) / scaleX,
807 Bounds.Top + (args.ScreenPositionPx.Y - screenBounds.Top) / scaleY);
810 private bool IsPointerOverScrollBar(WidgetPointerEventArgs args)
816 private void SetRectangleBounds(DirectRectangle rectangle, Rectangle bounds)
819 rectangle.ScreenBounds = bounds;
821 rectangle.WorldBounds = bounds;
826 private static DirectRectangle CreateBackground(
827 RenderSurfaceHostBase host,
831 return ConfigureBackground(
843 private static DirectRectangle CreateBackground(
844 RenderSurfaceHostBase host,
848 return ConfigureBackground(
860 private static DirectRectangle ConfigureBackground(DirectRectangle background)
873 private static DirectRectangle CreateSelectionHighlight(
874 RenderSurfaceHostBase host,
878 return ConfigureSelectionHighlight(
890 private static DirectRectangle CreateSelectionHighlight(
891 RenderSurfaceHostBase host,
895 return ConfigureSelectionHighlight(
907 private static DirectRectangle ConfigureSelectionHighlight(DirectRectangle highlight)
914 private static DirectRectangle CreateScrollBarTrack(RenderSurfaceHostBase host,
View view, Rectangle bounds)
916 return ConfigureScrollBarTrack(
928 private static DirectRectangle CreateScrollBarTrack(RenderSurfaceHostBase host,
SceneLayer layer, Rectangle bounds)
930 return ConfigureScrollBarTrack(
942 private static DirectRectangle ConfigureScrollBarTrack(DirectRectangle track)
947 private static DirectRectangle CreateScrollBarThumb(RenderSurfaceHostBase host,
View view, Rectangle bounds)
949 return ConfigureScrollBarThumb(
961 private static DirectRectangle CreateScrollBarThumb(RenderSurfaceHostBase host,
SceneLayer layer, Rectangle bounds)
963 return ConfigureScrollBarThumb(
975 private static DirectRectangle ConfigureScrollBarThumb(DirectRectangle thumb)
980 private static Rectangle GetInitialSelectionBounds(Rectangle bounds)
983 bounds.X + ContentPadding,
984 bounds.Y + ContentPadding,
985 Math.Max(1, bounds.Width - ContentPadding * 2),
989 private static void Refresh(DirectRectangle rectangle)
994 private static Rectangle ValidateBounds(Rectangle bounds)
996 if (bounds.Width < 24 || bounds.Height < DefaultItemHeight + ContentPadding * 2)
997 throw new ArgumentOutOfRangeException(nameof(bounds), bounds,
"List-box bounds are too small.");
bool Visible
Gets or sets the visibility of all descendants.
virtual DirectComposite Remove(IDirectCompositeChild child)
Removes a child without disposing it.
RenderSurfaceHostBase RenderSurfaceHost
Gets the render surface host shared by the composite and its children.
SceneLayer? SceneLayer
Gets the scene layer shared by all children in scene-layer mode.
DirectComposite SetLocalOffset(IDirectCompositeChild child, Vector2 newLocalOffsetPx)
Changes one child's local offset and reapplies its position.
DirectComposite SetIsVisible(bool visible)
Applies one visibility value to every descendant.
virtual DirectComposite Clear()
Removes every child without disposing them.
View? View
Gets the view shared by all children in view mode.
Vector2 GetPosition()
Gets the current composite anchor.
virtual DirectComposite Add(IDirectCompositeChild child, bool keepCurrentOffset=true, Vector2? explicitLocalOffsetPx=null)
Adds a composite-compatible direct drawable as a child.
DirectDrawingMode Mode
Gets the coordinate mode shared by the composite and its children.
Vector2 GetPosition()
Gets the current position of this direct drawing in pixel coordinates.
void SetPosition(Vector2 p)
Sets the position of this direct drawing to the specified pixel coordinates.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
DirectRectangle SetFilled(bool isFilled)
Sets whether the rectangle interior is filled with the base color (or pattern).
DirectRectangle SetBorderColor(Color color)
Sets a distinct border (stroke) color, independent of the fill color.
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
DirectRectangle SetCornerRadius(float radius)
Sets the radius for rounded corners in pixels.
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
TextBlock EnableWrapping(bool enable=true)
Enables or disables word wrapping when laying out paragraphs.
TextBlock SetAlignment(SKTextAlign h, VerticalAlign v)
Sets the horizontal and vertical alignment used when drawing the laid-out lines within the bounds.
TextBlock SetPadding(float horizontal, float vertical)
Sets symmetric horizontal and vertical padding and invalidates cached layout.
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
override void Dispose(bool disposing)
Releases managed resources used by the text block.
TextBlock SetText(string text)
Sets the current text content and rebuilds layout as needed. If no reveal animation is active,...
Represents a base class for hosting a render surface, providing functionality for managing rendering ...
Viewport Viewport
Gets the viewport that defines the screen-space rectangle, zoom level, and other rendering parameters...
float Zoom
Zoom factor applied to the world when rendering this view. Values greater than 1 zoom in,...
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.
DirectDrawingMode
Specifies where direct drawing operations are applied within the rendering pipeline.
@ View
Performs direct drawing at the view level using screen-space coordinates.