16 private const int ScrollBarWidth = 12;
17 private const int ScrollBarMargin = 3;
18 private const int MinimumScrollBarThumbHeight = 18;
20 private SKColor _foregroundColor = SKColors.White;
21 private SKColor _backgroundColor = SKColors.Transparent;
25 private int _mouseWheelScrollPixels = 48;
26 private float _verticalScrollOffsetPx;
27 private float _verticalScrollEndPaddingPx;
28 private float _maximumVerticalScrollOffsetPx;
29 private bool _isDraggingScrollBarThumb;
30 private float _scrollBarDragOffset;
31 private int? _scrollBarPointerId;
46 string? nickname =
null)
50 ValidateBounds(bounds).Location,
53 ArgumentNullException.ThrowIfNull(view);
55 _text = text ??
string.Empty;
64 .
SetColors(_foregroundColor, _backgroundColor);
69 Add(
TextBlock, keepCurrentOffset:
false, explicitLocalOffsetPx: Vector2.Zero);
72 CompleteInitialization();
88 string? nickname =
null)
92 ValidateBounds(bounds).Location,
95 ArgumentNullException.ThrowIfNull(sceneLayer);
97 _text = text ??
string.Empty;
105 nickname: $
"{Nickname}.text")
107 .
SetColors(_foregroundColor, _backgroundColor);
112 Add(
TextBlock, keepCurrentOffset:
false, explicitLocalOffsetPx: Vector2.Zero);
115 CompleteInitialization();
143 return new Rectangle(
144 (
int)MathF.Round(position.X),
145 (
int)MathF.Round(position.Y),
161 RefreshScrollState();
173 get => _verticalScrollBarVisibility;
176 if (!Enum.IsDefined(value))
177 throw new ArgumentOutOfRangeException(nameof(value));
179 if (_verticalScrollBarVisibility == value)
182 _verticalScrollBarVisibility = value;
183 RefreshScrollState();
193 get => _mouseWheelScrollPixels;
194 set => _mouseWheelScrollPixels = value > 0
196 :
throw new ArgumentOutOfRangeException(
198 "Mouse-wheel scrolling must advance at least one pixel.");
211 get => _verticalScrollEndPaddingPx;
214 if (!
float.IsFinite(value) || value < 0f)
215 throw new ArgumentOutOfRangeException(nameof(value));
217 if (Math.Abs(_verticalScrollEndPaddingPx - value) < 0.001f)
220 _verticalScrollEndPaddingPx = value;
222 RefreshScrollState();
229 get => _verticalScrollOffsetPx;
232 if (!
float.IsFinite(value) || value < 0f)
233 throw new ArgumentOutOfRangeException(nameof(value));
235 SetVerticalScrollOffset(value);
249 _text = text ??
string.Empty;
251 RefreshScrollState();
264 ArgumentNullException.ThrowIfNull(typeface);
265 TextBlock.SetFont(typeface, size, minSize);
266 RefreshScrollState();
278 _foregroundColor = foreground;
279 _backgroundColor = background;
280 TextBlock.SetColors(_foregroundColor, _backgroundColor);
291 _foregroundColor = color;
292 TextBlock.SetColors(_foregroundColor, _backgroundColor);
303 _backgroundColor = color;
304 TextBlock.SetColors(_foregroundColor, _backgroundColor);
316 TextBlock.SetAlignment(horizontal, vertical);
328 RefreshScrollState();
340 if (!
float.IsFinite(horizontal) || horizontal < 0f)
341 throw new ArgumentOutOfRangeException(nameof(horizontal));
343 if (!
float.IsFinite(vertical) || vertical < 0f)
344 throw new ArgumentOutOfRangeException(nameof(vertical));
346 TextBlock.SetPadding(horizontal, vertical);
347 RefreshScrollState();
357 VerticalScrollBarTrack.ZOrder = zOrder + 1;
358 VerticalScrollBarThumb.ZOrder = zOrder + 2;
366 RefreshScrollState();
372 _isDraggingScrollBarThumb =
false;
373 _scrollBarPointerId =
null;
374 base.ProcessHidden();
380 base.OnPointerDown(args);
385 PointF local = GetLocalPointerPosition(args);
386 Rectangle trackBounds = GetScrollBarTrackBounds();
387 Rectangle thumbBounds = GetScrollBarThumbBounds();
389 if (!trackBounds.Contains(Point.Round(local)))
395 if (thumbBounds.Contains(Point.Round(local)))
397 _isDraggingScrollBarThumb =
true;
398 _scrollBarDragOffset = local.Y - thumbBounds.Top;
402 float page = Math.Max(1f, GetViewportContentHeight());
403 SetVerticalScrollOffset(_verticalScrollOffsetPx + (local.Y < thumbBounds.Top ? -page : page));
409 base.OnPointerMove(args);
411 if (!_isDraggingScrollBarThumb ||
418 Rectangle trackBounds = GetScrollBarTrackBounds();
419 Rectangle thumbBounds = GetScrollBarThumbBounds();
420 float travel = trackBounds.Height - thumbBounds.Height;
421 float maximumScroll = _maximumVerticalScrollOffsetPx;
423 if (travel <= 0f || maximumScroll <= 0f)
426 float desiredTop = GetLocalPointerPosition(args).Y - _scrollBarDragOffset;
427 float fraction = Math.Clamp((desiredTop - trackBounds.Top) / travel, 0f, 1f);
428 SetVerticalScrollOffset(fraction * maximumScroll);
435 base.OnPointerUp(args);
437 if (_scrollBarPointerId != args.
PointerId)
440 _isDraggingScrollBarThumb =
false;
441 _scrollBarPointerId =
null;
448 base.OnMouseWheel(args);
450 float maximumScroll = _maximumVerticalScrollOffsetPx;
452 if (args.
Delta == 0 || maximumScroll <= 0f)
455 long notches = Math.Max(1, Math.Abs((
long)args.
Delta) / 120);
457 SetVerticalScrollOffset(_verticalScrollOffsetPx + change);
461 private bool NeedsVerticalScrollBar =>
464 _maximumVerticalScrollOffsetPx > 0.5f);
466 private void CompleteInitialization()
468 VerticalScrollBarTrack.Visible =
false;
469 VerticalScrollBarThumb.Visible =
false;
471 RefreshScrollState();
474 private void RefreshScrollState()
478 Rectangle bounds =
Bounds;
479 SetTextBlockBounds(bounds);
481 if (!scrollingEnabled)
483 _verticalScrollOffsetPx = 0f;
491 _maximumVerticalScrollOffsetPx > 0.5f);
495 Rectangle contentBounds =
new(
498 Math.Max(1, bounds.Width - ScrollBarWidth - ScrollBarMargin * 2),
501 SetTextBlockBounds(contentBounds);
510 SetVerticalScrollOffset(_verticalScrollOffsetPx);
512 bool visible =
Visible && showScrollBar;
513 VerticalScrollBarTrack.Visible = visible;
514 VerticalScrollBarThumb.Visible = visible;
517 RefreshScrollBarBounds();
520 private void SetVerticalScrollOffset(
float value)
522 _verticalScrollOffsetPx = Math.Clamp(value, 0f, _maximumVerticalScrollOffsetPx);
523 TextBlock.VerticalScrollOffsetPx = _verticalScrollOffsetPx;
524 RefreshScrollBarBounds();
527 private float GetViewportContentHeight()
530 ? TextBlock.ScreenBounds
533 return Math.Max(0f, textBounds.Height -
TextBlock.VerticalPadding * 2f);
536 private void RefreshScrollBarBounds()
545 private Rectangle GetScrollBarTrackBounds()
548 Bounds.Right - ScrollBarMargin - ScrollBarWidth,
549 Bounds.Top + ScrollBarMargin,
551 Math.Max(1,
Bounds.Height - ScrollBarMargin * 2));
554 private Rectangle GetScrollBarThumbBounds()
556 Rectangle trackBounds = GetScrollBarTrackBounds();
557 float maximumScroll = _maximumVerticalScrollOffsetPx;
558 float viewportHeight = GetViewportContentHeight();
560 if (maximumScroll <= 0f || viewportHeight <= 0f)
563 float contentHeight = viewportHeight + maximumScroll;
565 int thumbHeight = Math.Clamp(
566 (
int)MathF.Round(trackBounds.Height * Math.Min(1f, viewportHeight / contentHeight)),
567 Math.Min(MinimumScrollBarThumbHeight, trackBounds.Height),
570 int travel = trackBounds.Height - thumbHeight;
572 int offset = maximumScroll <= 0f
574 : (int)MathF.Round(travel * (_verticalScrollOffsetPx / maximumScroll));
578 trackBounds.Top + offset,
583 private PointF GetLocalPointerPosition(WidgetPointerEventArgs args)
585 RectangleF screenBounds =
TextBlock.GetDrawLocationScreen(args.View);
588 args.WrappedOffsetWorldPx.X * args.View.Viewport.Zoom,
589 args.WrappedOffsetWorldPx.Y * args.View.Viewport.Zoom);
592 ? TextBlock.ScreenBounds
595 float scaleX = screenBounds.Width / textBounds.Width;
596 float scaleY = screenBounds.Height / textBounds.Height;
599 Bounds.Left + (args.ScreenPositionPx.X - screenBounds.Left) / scaleX,
600 Bounds.Top + (args.ScreenPositionPx.Y - screenBounds.Top) / scaleY);
603 private void SetTextBlockBounds(Rectangle bounds)
607 TextBlock.SetPosition(
new Vector2(bounds.X, bounds.Y));
613 new Vector2(bounds.X - overallBounds.X, bounds.Y - overallBounds.Y));
616 private void SetRectangleBounds(DirectRectangle rectangle, Rectangle bounds)
619 rectangle.ScreenBounds = bounds;
621 rectangle.WorldBounds = bounds;
628 private static DirectRectangle CreateScrollBarTrack(
629 RenderSurfaceHostBase host,
632 => ConfigureScrollBarTrack(
643 private static DirectRectangle CreateScrollBarTrack(
644 RenderSurfaceHostBase host,
647 => ConfigureScrollBarTrack(
658 private static DirectRectangle ConfigureScrollBarTrack(DirectRectangle track)
659 => track.SetFilled(
true).SetStrokeWidth(0f).SetCornerRadius(3f);
661 private static DirectRectangle CreateScrollBarThumb(
662 RenderSurfaceHostBase host,
665 => ConfigureScrollBarThumb(
676 private static DirectRectangle CreateScrollBarThumb(
677 RenderSurfaceHostBase host,
680 => ConfigureScrollBarThumb(
691 private static DirectRectangle ConfigureScrollBarThumb(DirectRectangle thumb)
692 => thumb.SetFilled(
true).SetStrokeWidth(0f).SetCornerRadius(3f);
694 private static Rectangle ValidateBounds(Rectangle bounds)
696 ValidateSize(bounds.Size);
700 private static void ValidateSize(
Size size)
702 if (size.Width <= 0 || size.Height <= 0)
703 throw new ArgumentOutOfRangeException(nameof(size), size,
"Label size must be positive.");
bool Visible
Gets or sets the visibility of all descendants.
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.
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.
int ZOrder
Gets or sets the Z-order (depth) used for sorting this direct drawing relative to other drawables.
Renders a configurable rectangle with support for fill, border, rounded corners, dash patterns,...
A retained-mode text drawable with wrapping, multi-line ( ) support, horizontal/vertical alignment,...
TextBlock SetColors(SKColor fg, SKColor bg)
Sets the foreground (text) and background colors using Skia colors.
float VerticalScrollEndPaddingPx
Gets or sets extra scrollable space after the final laid-out line.
float MeasureMaximumVerticalScrollOffsetPx()
Measures the maximum vertical scroll offset required to expose all currently laid-out text.
float VerticalScrollOffsetPx
Gets or sets the vertical text-content scroll offset in native pixels.
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 ...
@ 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.