16 private readonly Action? _action;
17 private readonly Action<bool>? _checkedAction;
18 private readonly MenuIndicatorDrawing _marker;
19 private readonly MenuIndicatorDrawing _arrow;
20 private bool _disposed;
21 private bool _isPointerOver;
22 private bool _isSelected;
23 private bool _isEnabled =
true;
25 internal event Action<MenuItemWidget>? Hovered;
28 public event Action<MenuItemWidget>?
Invoked;
30 internal MenuItemWidget(
46 Action<bool>? checkedAction,
57 _checkedAction = checkedAction;
69 theme.ItemNormalColor,
73 $
"{Nickname}.background")
81 SKTextAlign.Left).SetText(
Text);
90 _marker =
new MenuIndicatorDrawing(
94 radioGroup is
null ? MenuIndicatorDrawing.Shape.Check : MenuIndicatorDrawing.Shape.Radio,
96 $
"{Nickname}.marker");
98 _arrow =
new MenuIndicatorDrawing(
102 MenuIndicatorDrawing.Shape.Arrow,
104 $
"{Nickname}.arrow");
112 if (icon is not
null)
132 public string?
Key {
get; }
185 internal bool CanInvoke => !_disposed && _isEnabled && _owner.CanInvoke;
192 ObjectDisposedException.ThrowIf(_disposed,
this);
193 _isEnabled = enabled;
194 ApplyInputState(_owner.IsOpen);
197 _owner.OnItemDisabled(
this);
208 ObjectDisposedException.ThrowIf(_disposed,
this);
211 throw new InvalidOperationException(
"This menu item is not checkable.");
214 _owner.UncheckRadioPeers(
this);
230 _owner.OpenSubMenu(
this, selectFirst:
true);
238 _owner.NotifyInvoked(
this);
240 _checkedAction?.Invoke(checkedState);
244 internal void SetSelected(
bool selected)
246 _isSelected = selected;
249 _isPointerOver =
false;
254 internal void ApplyInputState(
bool menuAcceptsInput)
256 bool enabled = menuAcceptsInput && _isEnabled && !_disposed;
263 internal void SetItemZOrder(
int zOrder)
265 Background.ZOrder = zOrder;
268 drawing.ZOrder = zOrder + 1;
271 IconDrawing.ZOrder = zOrder + 1;
274 internal void SetBounds(
282 Background.ScreenBounds = bounds;
283 int x = bounds.X + _theme.ItemHorizontalPadding;
290 Math.Max(1, markerWidth),
300 bounds.Y + (bounds.Height - _theme.IconSize) / 2,
305 int right = bounds.Right - _theme.ItemHorizontalPadding;
312 Math.Max(1, arrowWidth),
320 right - shortcutWidth,
322 Math.Max(1, shortcutWidth),
325 if (shortcutWidth > 0)
326 right -= shortcutWidth + _theme.ShortcutGap;
333 Math.Max(1, right - x),
336 Label.HorizontalPadding = ShortcutLabel.HorizontalPadding = 0;
339 private void Place(DirectDrawingBase drawing, Rectangle bounds)
342 (IDirectCompositeChild)drawing,
345 drawing.ScreenBounds = bounds;
348 private TextBlock CreateText(
349 RenderSurfaceHostBase host,
353 SKTextAlign alignment) =>
358 $
"{Nickname}.{name}")
359 .SetFont(SKTypeface.Default, _theme.FontSize, _theme.MinimumFontSize)
360 .SetAlignment(alignment, TextBlock.VerticalAlign.Center).EnableWrapping(
false);
369 _owner.UnregisterItem(
this);
378 base.OnPointerEnter(args);
383 _isPointerOver =
true;
384 Hovered?.Invoke(
this);
391 base.OnPointerLeave(args);
392 _isPointerOver =
false;
399 base.OnPointerDown(args);
402 SetBackgroundColor(_theme.ItemPressedColor);
408 base.OnPointerUp(args);
415 base.OnPointerClick(args);
427 base.OnKeyboardInput(args);
440 private void UpdateVisualState()
443 _isEnabled && (_isPointerOver || _isSelected) ? _theme.ItemHoverColor : _theme.ItemNormalColor);
445 Label.
SetColors(ToSkColor(_isEnabled ? _theme.TextColor : _theme.DisabledTextColor), SKColors.Transparent);
448 ToSkColor(_isEnabled ? _theme.ShortcutTextColor : _theme.DisabledTextColor),
449 SKColors.Transparent);
451 var indicator = ToSkColor(_isEnabled ? _theme.IndicatorColor : _theme.DisabledTextColor);
453 _arrow.SetState(
SubMenu is not
null, indicator);
454 IconDrawing?.
SetTint(_isEnabled ? SKColors.White : ToSkColor(_theme.DisabledTextColor));
457 private void SetBackgroundColor(Color color)
463 private static SKColor ToSkColor(Color color) =>
new(
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.
void SetPosition(Vector2 position)
Sets the composite anchor and repositions all children from their stored offsets.
Provides the base implementation for direct drawing objects that can be rendered to the backbuffer wi...
Provides an abstract base class for direct drawings that support physics-based movement.
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 bitmap or image to a rectangular region with support for scaling modes, source clipping,...
DirectImage SetScaleMode(ScaleMode mode)
Sets how the image is scaled to fit within the destination rectangle.
DirectImage SetTint(SKColor color)
Applies a color tint (multiplicative blend) to the image.
ScaleMode
Defines how an image is scaled to fit within the destination rectangle.
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 SetColor(Color color)
Sets the base color used for both fill and stroke (unless a distinct border color is specified).
DirectRectangle SetStrokeWidth(float width)
Sets the width (thickness) of the rectangle's stroke (outline) in pixels.
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.
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.