Class Widget
- Namespace
- Myra.Graphics2D.UI
- Assembly
- Myra.dll
The base class for all UI widgets in the Myra framework.
public class Widget : BaseObject, IItemWithId, INotifyAttachedPropertyChanged
- Inheritance
-
Widget
- Implements
- Derived
- Inherited Members
Constructors
Widget()
Initializes a new instance of the Widget class.
public Widget()
Fields
AfterRender
Gets or sets an action to be invoked after the widget is rendered.
[Browsable(false)]
public Action<RenderContext> AfterRender
Field Value
BeforeRender
Gets or sets an action to be invoked before the widget is rendered.
[Browsable(false)]
public Action<RenderContext> BeforeRender
Field Value
Properties
AcceptsKeyboardFocus
Gets or sets a value indicating whether the widget can accept keyboard focus.
[Browsable(false)]
public bool AcceptsKeyboardFocus { get; set; }
Property Value
AcceptsMouseWheel
Gets a value indicating whether this widget accepts mouse wheel input.
protected virtual bool AcceptsMouseWheel { get; }
Property Value
ActualBounds
Gets the actual bounds of the widget's content area, excluding margin, border, and padding.
[Browsable(false)]
public Rectangle ActualBounds { get; }
Property Value
- Rectangle
Background
Gets or sets the brush used to draw the background of the widget in its normal state.
public IBrush Background { get; set; }
Property Value
BackgroundBounds
Gets the bounds of the widget's background area, excluding the border.
[Browsable(false)]
protected Rectangle BackgroundBounds { get; }
Property Value
- Rectangle
Border
Gets or sets the brush used for the widget's border in its normal state.
public IBrush Border { get; set; }
Property Value
BorderThickness
Gets or sets the thickness of the border around the widget.
public Thickness BorderThickness { get; set; }
Property Value
Bounds
Zero-based bounds
[Browsable(false)]
public Rectangle Bounds { get; }
Property Value
- Rectangle
Children
Gets the collection of child widgets.
[Browsable(false)]
protected ObservableCollection<Widget> Children { get; }
Property Value
ChildrenCopy
Gets a copy of the children collection sorted by Z-index.
protected IEnumerable<Widget> ChildrenCopy { get; }
Property Value
ChildrenLayout
Gets or sets the layout that arranges this widget's child widgets.
[Browsable(false)]
public ILayout ChildrenLayout { get; set; }
Property Value
ClipToBounds
Gets or sets a value indicating whether the widget's content is clipped to its bounds.
public virtual bool ClipToBounds { get; set; }
Property Value
ContainerBounds
Gets the bounds of the widget's container area.
[Browsable(false)]
public Rectangle ContainerBounds { get; }
Property Value
- Rectangle
Desktop
Gets or sets the desktop that this widget is attached to.
[Browsable(false)]
public virtual Desktop Desktop { get; }
Property Value
DisabledBackground
Gets or sets the brush used to draw the background of the widget when it is disabled.
public IBrush DisabledBackground { get; set; }
Property Value
DisabledBorder
Gets or sets the brush used for the widget's border when it is disabled.
public IBrush DisabledBorder { get; set; }
Property Value
DragDirection
Gets or sets the direction(s) in which the widget can be dragged.
public virtual DragDirection DragDirection { get; set; }
Property Value
DragHandle
Gets or sets a widget that should be used as the drag handle for this widget. If set, dragging this handle widget will move the parent widget.
[Browsable(false)]
public Widget DragHandle { get; set; }
Property Value
Enabled
Gets or sets a value indicating whether the widget is enabled and can receive user input.
public bool Enabled { get; set; }
Property Value
FocusedBackground
Gets or sets the brush used to draw the background of the widget when it has keyboard focus.
public IBrush FocusedBackground { get; set; }
Property Value
FocusedBorder
Gets or sets the brush used for the widget's border when it has focus.
public IBrush FocusedBorder { get; set; }
Property Value
Height
Gets or sets the height of the widget in pixels.
public int? Height { get; set; }
Property Value
- int?
HorizontalAlignment
Gets or sets the horizontal alignment of the widget within its parent container.
public virtual HorizontalAlignment HorizontalAlignment { get; set; }
Property Value
IsKeyboardFocused
Gets a value indicating whether the widget currently has keyboard focus.
[Browsable(false)]
public bool IsKeyboardFocused { get; }
Property Value
IsModal
Gets or sets a value indicating whether this widget is modal and blocks interaction with other widgets.
[Browsable(false)]
public bool IsModal { get; set; }
Property Value
IsMouseInside
Gets a value indicating whether the mouse pointer is currently inside this widget.
[Browsable(false)]
public bool IsMouseInside { get; }
Property Value
IsPlaced
Determines whether the widget had been placed on Desktop
[Browsable(false)]
public bool IsPlaced { get; }
Property Value
IsPressed
Gets or sets a value indicating whether the button is currently in the pressed state.
[Browsable(false)]
public virtual bool IsPressed { get; set; }
Property Value
IsTouchInside
Gets a value indicating whether a touch point is currently inside this widget.
[Browsable(false)]
public bool IsTouchInside { get; }
Property Value
Left
Gets or sets the left position of the widget in pixels.
public int Left { get; set; }
Property Value
LocalMousePosition
Gets or sets the local coordinates of the mouse pointer relative to this widget, or null if the mouse is not over the widget. Setting this property automatically queues MouseEntered, MouseLeft, or MouseMoved events as appropriate.
[Browsable(false)]
public Point? LocalMousePosition { get; }
Property Value
- Point?
LocalTouchPosition
Gets or sets the local coordinates of the touch point relative to this widget, or null if there is no active touch on the widget. Setting this property automatically queues TouchDown, TouchUp, TouchEntered, TouchLeft, or TouchMoved events as appropriate.
[Browsable(false)]
public Point? LocalTouchPosition { get; }
Property Value
- Point?
MBPHeight
Gets the total height consumed by margin, border, and padding (MBP = Margin + Border + Padding).
[Browsable(false)]
public int MBPHeight { get; }
Property Value
MBPWidth
Gets the total width consumed by margin, border, and padding (MBP = Margin + Border + Padding).
[Browsable(false)]
public int MBPWidth { get; }
Property Value
Margin
Gets or sets the outer margin around the widget.
public Thickness Margin { get; set; }
Property Value
MaxHeight
Gets or sets the maximum height of the widget in pixels.
public int? MaxHeight { get; set; }
Property Value
- int?
MaxWidth
Gets or sets the maximum width of the widget in pixels.
public int? MaxWidth { get; set; }
Property Value
- int?
MinHeight
Gets or sets the minimum height of the widget in pixels.
public int? MinHeight { get; set; }
Property Value
- int?
MinWidth
Gets or sets the minimum width of the widget in pixels.
public int? MinWidth { get; set; }
Property Value
- int?
MouseCursor
Gets or sets the mouse cursor type displayed when the mouse is over the widget.
public virtual MouseCursorType? MouseCursor { get; set; }
Property Value
Opacity
Gets or sets the opacity of the widget, where 0.0 is fully transparent and 1.0 is fully opaque.
[Range(0, 1)]
public float Opacity { get; set; }
Property Value
OverBackground
Gets or sets the brush used to draw the background of the widget when the mouse is hovering over it.
public IBrush OverBackground { get; set; }
Property Value
OverBorder
Gets or sets the brush used for the widget's border when the mouse is over it.
public IBrush OverBorder { get; set; }
Property Value
Padding
Gets or sets the inner padding inside the widget's borders.
public Thickness Padding { get; set; }
Property Value
Parent
Gets the parent widget in the widget hierarchy.
[Browsable(false)]
public Widget Parent { get; }
Property Value
PressedBackground
Gets or sets the brush used for the widget's background when it is pressed.
public IBrush PressedBackground { get; set; }
Property Value
PressedBorder
Gets or sets the brush used for the widget's border when it is pressed.
public IBrush PressedBorder { get; set; }
Property Value
Rotation
Gets or sets the rotation angle of the widget in degrees, applied around the TransformOrigin point.
public float Rotation { get; set; }
Property Value
Scale
Gets or sets the scale factor applied to the widget. A value of (1, 1) is the original size.
public Vector2 Scale { get; set; }
Property Value
- Vector2
StyleName
Internal use only. (MyraPad)
public string StyleName { get; set; }
Property Value
SuppressInvalidateMeasure
Gets or sets a value indicating whether layout invalidation is temporarily suppressed.
When set to true, calls to InvalidateMeasure() have no effect, allowing batch updates
without triggering repeated layout passes.
protected bool SuppressInvalidateMeasure { get; set; }
Property Value
Tag
Gets or sets a custom object associated with the widget for application-specific use.
[Browsable(false)]
public object Tag { get; set; }
Property Value
Tooltip
Gets or sets the tooltip text to display when the mouse hovers over the widget.
public string Tooltip { get; set; }
Property Value
Top
Gets or sets the top position of the widget in pixels.
public int Top { get; set; }
Property Value
TransformOrigin
Gets or sets the origin point for transformations (scale and rotation), in normalized coordinates where (0, 0) is top-left and (1, 1) is bottom-right.
public Vector2 TransformOrigin { get; set; }
Property Value
- Vector2
UseOverBackground
Gets a value indicating whether the over background should be used for the widget's current state.
protected virtual bool UseOverBackground { get; }
Property Value
VerticalAlignment
Gets or sets the vertical alignment of the widget within its parent container.
public virtual VerticalAlignment VerticalAlignment { get; set; }
Property Value
Visible
Gets or sets a value indicating whether the widget is visible and should be rendered.
public bool Visible { get; set; }
Property Value
Width
Gets or sets the width of the widget in pixels.
public int? Width { get; set; }
Property Value
- int?
ZIndex
Gets or sets the z-order (depth) of the widget for rendering. Widgets with higher z-index values are rendered on top.
public int ZIndex { get; set; }
Property Value
Methods
ApplyStyle(WidgetStyle)
Applies the specified widget style to this widget.
protected virtual void ApplyStyle(WidgetStyle style)
Parameters
styleWidgetStyleThe widget style to apply.
ApplyWidgetStyle(WidgetStyle)
Applies the specified widget style to this widget, copying its sizing, background, border, and margin properties.
public void ApplyWidgetStyle(WidgetStyle style)
Parameters
styleWidgetStyleThe widget style to apply.
Arrange(Rectangle)
Arranges the widget within the specified container bounds.
public void Arrange(Rectangle containerBounds)
Parameters
containerBoundsRectangleThe bounds of the container in which to arrange the widget.
BringToBack()
Sends the widget to the back (bottom of the z-order) within its parent or desktop.
public void BringToBack()
BringToFront()
Brings the widget to the front (top of the z-order) within its parent or desktop.
public void BringToFront()
CalculateTotalChildCount(bool)
Calculates the total number of child widgets, optionally including only visible children and their descendants.
public int CalculateTotalChildCount(bool visibleOnly)
Parameters
visibleOnlyboolIf true, only counts visible widgets; if false, counts all widgets.
Returns
- int
The total count of child widgets.
Clone()
Creates a deep copy of this widget with all its properties and attached properties.
public Widget Clone()
Returns
- Widget
A new widget instance that is a copy of this widget.
ContainsGlobalPoint(Point)
Determines whether the specified global position is within the widget's bounds.
public bool ContainsGlobalPoint(Point globalPos)
Parameters
globalPosPointThe position in global coordinates.
Returns
- bool
True if the position is within the widget's bounds; otherwise, false.
CopyFrom(Widget)
Copies all properties from another widget to this widget.
protected virtual void CopyFrom(Widget w)
Parameters
wWidgetThe widget to copy properties from.
EnsureWidgetById(string)
Finds a child widget by its id, throwing an exception if not found.
public Widget EnsureWidgetById(string id)
Parameters
idstringThe id of the widget to find.
Returns
- Widget
The widget with the specified id.
Exceptions
- Exception
Thrown when a widget with the specified id is not found.
FindChild(Func<Widget, bool>)
Finds the first child found by predicate.
public Widget FindChild(Func<Widget, bool> predicate)
Parameters
Returns
- Widget
Widget instance if found otherwise null
FindChildById(string)
Finds the first widget with matching Id
public Widget FindChildById(string Id)
Parameters
IdstringId to match on
Returns
- Widget
Widget instance if found otherwise null
FindChildById<WidgetT>(string)
Finds first child widget of type WidgetT with specified Id
public WidgetT FindChildById<WidgetT>(string Id) where WidgetT : Widget
Parameters
IdstringId of widget
Returns
- WidgetT
Widget instance if found otherwise null
Type Parameters
WidgetTWidget type
FindChild<WidgetT>(Func<WidgetT, bool>)
Finds first child widget of type WidgetT. If predicate is null -
the first widget of WidgetT is returned,
otherwise the first widget of WidgetT matching predicate is returned.
public WidgetT FindChild<WidgetT>(Func<WidgetT, bool> predicate = null) where WidgetT : Widget
Parameters
Returns
- WidgetT
Widget instance if found otherwise null
Type Parameters
WidgetTWidget type
FireKeyDown(Keys)
Fires the KeyDown event for the specified key.
protected void FireKeyDown(Keys k)
Parameters
kKeysThe key that was pressed.
GetChildren(bool, Func<Widget, bool>)
Gets all children in Widget matching on optional predicate.
public IEnumerable<Widget> GetChildren(bool recursive = false, Func<Widget, bool> predicate = null)
Parameters
recursiveboolIf true, indicates that child Widgets will also be iterated.
predicateFunc<Widget, bool>Predicate to filter children
Returns
- IEnumerable<Widget>
Children found
GetCurrentBackground()
Gets the brush to use for the widget's background based on its current state.
public IBrush GetCurrentBackground()
Returns
- IBrush
The current background brush appropriate for the widget's state.
GetCurrentBorder()
Gets the brush to use for the widget's border based on its current state.
public IBrush GetCurrentBorder()
Returns
- IBrush
The current border brush appropriate for the widget's state.
GetCurrentVisual<T>(T[])
Gets the current visual element based on the widget's state, selecting from the provided array of visual values.
protected T GetCurrentVisual<T>(T[] values)
Parameters
valuesT[]Array of visual values indexed by WidgetVisualState constants.
Returns
- T
The current visual element appropriate for the widget's current state.
Type Parameters
TThe type of visual element.
HitTest(Point)
Performs hit testing to determine which widget at the given global position should receive input events.
public virtual Widget HitTest(Point p)
Parameters
pPointThe position in global coordinates to test.
Returns
- Widget
The widget at the specified position, or null if no widget is there or this widget is not visible.
InputFallsThrough(Point)
Determines whether input at the specified local position should fall through to widgets behind this widget.
public virtual bool InputFallsThrough(Point localPos)
Parameters
localPosPointThe position in the widget's local coordinates.
Returns
- bool
True if input should fall through; false if this widget should handle it.
InternalArrange()
Arranges the child widgets within this widget's bounds.
protected virtual void InternalArrange()
InternalMeasure(Point)
Measures the widget and its children to determine the required size.
protected virtual Point InternalMeasure(Point availableSize)
Parameters
availableSizePointThe available size for this widget and its children.
Returns
- Point
The measured size of this widget.
InternalRender(RenderContext)
Renders the widget's content (called after backgrounds and borders are rendered). Default implementation renders all child widgets.
public virtual void InternalRender(RenderContext context)
Parameters
contextRenderContextThe render context to draw to.
InvalidateArrange()
Marks the widget's arrangement as dirty, requiring a recalculation on the next update.
public void InvalidateArrange()
InvalidateChildren()
Invalidates the child layout, marking it as needing recalculation.
protected virtual void InvalidateChildren()
InvalidateMeasure()
Marks the widget's measurement as dirty, requiring a recalculation on the next update. This cascades up to parent widgets.
public virtual void InvalidateMeasure()
Measure(Point)
Measures the widget to determine its desired size based on available space.
public Point Measure(Point availableSize)
Parameters
availableSizePointThe available space for the widget.
Returns
- Point
The desired size of the widget.
OnChar(char)
Called when a character is entered while this widget has focus.
public virtual void OnChar(char c)
Parameters
ccharThe character that was entered.
OnChildAdded(Widget)
Called when a child widget is added to this widget.
protected virtual void OnChildAdded(Widget w)
Parameters
wWidgetThe child widget that was added.
OnChildRemoved(Widget)
Called when a child widget is removed from this widget.
protected virtual void OnChildRemoved(Widget w)
Parameters
wWidgetThe child widget that was removed.
OnGotKeyboardFocus()
Called when the widget receives keyboard focus.
public virtual void OnGotKeyboardFocus()
OnKeyDown(Keys)
Called when a keyboard key is pressed while this widget has focus.
public virtual void OnKeyDown(Keys k)
Parameters
kKeysThe key that was pressed.
OnKeyUp(Keys)
Called when a keyboard key is released while this widget has focus.
public virtual void OnKeyUp(Keys k)
Parameters
kKeysThe key that was released.
OnLostKeyboardFocus()
Called when the widget loses keyboard focus.
public virtual void OnLostKeyboardFocus()
OnMouseEntered()
Called when the mouse pointer enters the widget.
public virtual void OnMouseEntered()
OnMouseLeft()
Called when the mouse pointer leaves the widget.
public virtual void OnMouseLeft()
OnMouseMoved()
Called when the mouse pointer moves within the widget.
public virtual void OnMouseMoved()
OnMouseWheel(float)
Called when the mouse wheel is scrolled while over the widget.
public virtual void OnMouseWheel(float delta)
Parameters
deltafloatThe scroll delta value.
OnPlacedChanged()
Called when the widget's placement (position or size) has changed.
protected virtual void OnPlacedChanged()
OnPressedChanged()
Raises the PressedChanged event.
public virtual void OnPressedChanged()
OnTouchDoubleClick()
Called when the widget receives a double-tap touch event.
public virtual void OnTouchDoubleClick()
OnTouchDown()
Called when a touch point is pressed on the widget.
public virtual void OnTouchDown()
OnTouchEntered()
Called when a touch point enters the widget.
public virtual void OnTouchEntered()
OnTouchLeft()
Called when a touch point leaves the widget.
public virtual void OnTouchLeft()
OnTouchMoved()
Called when a touch point moves within the widget.
public virtual void OnTouchMoved()
OnTouchUp()
Called when a touch point is released on the widget.
public virtual void OnTouchUp()
OnVisibleChanged()
Called when the widget's visibility state changes.
public virtual void OnVisibleChanged()
ProcessInput(InputContext)
Processes input events for this widget, including mouse and touch input. Performs hit-testing, updates input positions, and recursively processes children. Marks input as handled if widget consumes it, preventing propagation to parent widgets.
protected virtual void ProcessInput(InputContext inputContext)
Parameters
inputContextInputContextThe input context containing the current input state and handling flags.
RemoveFromDesktop()
Removes this widget from the desktop's widgets collection.
public void RemoveFromDesktop()
RemoveFromParent()
Removes this widget from its parent widget's children collection.
public void RemoveFromParent()
Render(RenderContext)
Renders the widget and its children to the specified render context.
public void Render(RenderContext context)
Parameters
contextRenderContextThe render context to draw to.
SetIsPressedByUser(bool)
Sets the IsPressed state as a result of user interaction, raising the PressedChangingByUser event first.
protected void SetIsPressedByUser(bool value)
Parameters
valueboolThe new pressed state value.
SetKeyboardFocus()
Sets this widget to have keyboard focus.
public void SetKeyboardFocus()
SetStyle(Stylesheet, string)
Sets the style for this widget by name from the specified stylesheet.
public void SetStyle(Stylesheet stylesheet, string name)
Parameters
stylesheetStylesheetThe stylesheet containing the style to apply.
namestringThe name of the style to apply.
ToGlobal(Point)
Converts a position from the widget's local coordinates to global (screen) coordinates.
public Point ToGlobal(Point pos)
Parameters
posPointThe position in local coordinates.
Returns
- Point
The position in global coordinates.
ToGlobal(Vector2)
Converts a position from the widget's local coordinates to global (screen) coordinates.
public Vector2 ToGlobal(Vector2 pos)
Parameters
posVector2The position in local coordinates.
Returns
- Vector2
The position in global coordinates.
ToLocal(Point)
Converts a position from global (screen) coordinates to the widget's local coordinates.
public Point ToLocal(Point pos)
Parameters
posPointThe position in global coordinates.
Returns
- Point
The position in local coordinates.
ToLocal(Vector2)
Converts a position from global (screen) coordinates to the widget's local coordinates.
public Vector2 ToLocal(Vector2 pos)
Parameters
posVector2The position in global coordinates.
Returns
- Vector2
The position in local coordinates.
UpdateArrange()
Updates the widget's arrangement if it is marked as dirty.
public void UpdateArrange()
Events
ArrangeUpdated
Occurs when the widget's layout arrangement has been updated.
public event MyraEventHandler ArrangeUpdated
Event Type
Char
Occurs when a character is entered while the widget has keyboard focus.
public event MyraEventHandler<GenericEventArgs<char>> Char
Event Type
EnabledChanged
Occurs when the widget's enabled state has changed.
public event MyraEventHandler EnabledChanged
Event Type
KeyDown
Occurs when a key is pressed while the widget has keyboard focus.
public event MyraEventHandler<GenericEventArgs<Keys>> KeyDown
Event Type
- MyraEventHandler<GenericEventArgs<Keys>>
KeyUp
Occurs when a key is released while the widget has keyboard focus.
public event MyraEventHandler<GenericEventArgs<Keys>> KeyUp
Event Type
- MyraEventHandler<GenericEventArgs<Keys>>
KeyboardFocusChanged
Occurs when the keyboard focus on the widget has changed.
public event MyraEventHandler KeyboardFocusChanged
Event Type
LocationChanged
Occurs when the widget's location has changed.
public event MyraEventHandler LocationChanged
Event Type
MouseEntered
Occurs when the mouse pointer enters the widget.
public event MyraEventHandler MouseEntered
Event Type
MouseLeft
Occurs when the mouse pointer leaves the widget.
public event MyraEventHandler MouseLeft
Event Type
MouseMoved
Occurs when the mouse pointer moves within the widget.
public event MyraEventHandler MouseMoved
Event Type
MouseWheelChanged
Occurs when the mouse wheel is scrolled while over the widget.
public event MyraEventHandler<GenericEventArgs<float>> MouseWheelChanged
Event Type
PlacedChanged
Occurs when the widget's position has changed.
public event MyraEventHandler PlacedChanged
Event Type
PressedChanged
Occurs when the widget's pressed state changes.
public event MyraEventHandler PressedChanged
Event Type
PressedChangingByUser
Occurs when the widget's pressed state is being changed by user interaction, before the change is committed.
public event MyraEventHandler<ValueChangingEventArgs<bool>> PressedChangingByUser
Event Type
SizeChanged
Occurs when the widget's size has changed.
public event MyraEventHandler SizeChanged
Event Type
TouchDoubleClick
Occurs when the widget receives a double-tap touch event.
public event MyraEventHandler TouchDoubleClick
Event Type
TouchDown
Occurs when a touch point is pressed on the widget.
public event MyraEventHandler TouchDown
Event Type
TouchEntered
Occurs when a touch point enters the widget.
public event MyraEventHandler TouchEntered
Event Type
TouchLeft
Occurs when a touch point leaves the widget.
public event MyraEventHandler TouchLeft
Event Type
TouchMoved
Occurs when a touch point moves within the widget.
public event MyraEventHandler TouchMoved
Event Type
TouchUp
Occurs when a touch point is released on the widget.
public event MyraEventHandler TouchUp
Event Type
VisibleChanged
Occurs when the widget's visibility has changed.
public event MyraEventHandler VisibleChanged