Class MenuControl
- Namespace
- SharpConsoleUI.Controls
- Assembly
- SharpConsoleUI.dll
A full-featured menu control supporting horizontal (menu bar) and vertical (sidebar) orientations, unlimited submenu nesting, keyboard and mouse navigation, and overlay rendering.
public class MenuControl : IInteractiveControl, IFocusableControl, IMouseAwareControl, IWindowControl, IDisposable, IDOMPaintable, IContainer
- Inheritance
-
MenuControl
- Implements
- Inherited Members
- Extension Methods
Properties
ActualWidth
Gets the actual rendered width of the control, or null if not yet rendered.
public int? ActualWidth { get; }
Property Value
- int?
BackgroundColor
Gets or sets the background color for dropdowns. Alias for DropdownBackgroundColor.
[Obsolete("Use DropdownBackgroundColor instead")]
public Color BackgroundColor { get; set; }
Property Value
- Color
CanFocusWithMouse
Whether this control can receive focus via mouse clicks
public bool CanFocusWithMouse { get; }
Property Value
CanReceiveFocus
Whether this control can receive focus
public bool CanReceiveFocus { get; }
Property Value
Container
Gets or sets the parent container that hosts this control.
public IContainer? Container { get; set; }
Property Value
DropdownBackgroundColor
Gets or sets the background color for dropdowns. Null uses theme default.
public Color? DropdownBackgroundColor { get; set; }
Property Value
- Color?
DropdownForegroundColor
Gets or sets the foreground color for dropdown items. Null uses theme default.
public Color? DropdownForegroundColor { get; set; }
Property Value
- Color?
DropdownHighlightBackgroundColor
Gets or sets the background color for highlighted dropdown items. Null uses theme default.
public Color? DropdownHighlightBackgroundColor { get; set; }
Property Value
- Color?
DropdownHighlightForegroundColor
Gets or sets the foreground color for highlighted dropdown items. Null uses theme default.
public Color? DropdownHighlightForegroundColor { get; set; }
Property Value
- Color?
ForegroundColor
Gets or sets the foreground color for dropdown items. Alias for DropdownForegroundColor.
[Obsolete("Use DropdownForegroundColor instead")]
public Color ForegroundColor { get; set; }
Property Value
- Color
HasFocus
Gets or sets whether this control currently has keyboard focus.
public bool HasFocus { get; set; }
Property Value
HighlightColor
Gets or sets the background color for highlighted items. Alias for DropdownHighlightBackgroundColor.
[Obsolete("Use DropdownHighlightBackgroundColor instead")]
public Color HighlightColor { get; set; }
Property Value
- Color
HighlightForeground
Gets or sets the foreground color for highlighted items. Alias for DropdownHighlightForegroundColor.
[Obsolete("Use DropdownHighlightForegroundColor instead")]
public Color HighlightForeground { get; set; }
Property Value
- Color
HorizontalAlignment
Gets or sets the horizontal alignment of the control within its container.
public HorizontalAlignment HorizontalAlignment { get; set; }
Property Value
IsEnabled
Gets or sets whether this control is enabled and can receive input.
public bool IsEnabled { get; set; }
Property Value
IsSticky
Gets or sets whether the menu keeps focus when a dropdown is open (sticky mode).
public bool IsSticky { get; set; }
Property Value
Items
Gets the list of top-level menu items.
public IReadOnlyList<MenuItem> Items { get; }
Property Value
Margin
Gets or sets the margin (spacing) around the control.
public Margin Margin { get; set; }
Property Value
MenuBarBackgroundColor
Gets or sets the background color for the menu bar. Null uses theme default.
public Color? MenuBarBackgroundColor { get; set; }
Property Value
- Color?
MenuBarForegroundColor
Gets or sets the foreground color for the menu bar. Null uses theme default.
public Color? MenuBarForegroundColor { get; set; }
Property Value
- Color?
MenuBarHighlightBackgroundColor
Gets or sets the background color for highlighted menu bar items. Null uses theme default.
public Color? MenuBarHighlightBackgroundColor { get; set; }
Property Value
- Color?
MenuBarHighlightForegroundColor
Gets or sets the foreground color for highlighted menu bar items. Null uses theme default.
public Color? MenuBarHighlightForegroundColor { get; set; }
Property Value
- Color?
Name
Gets or sets the unique name identifier for this control, used for lookup.
public string? Name { get; set; }
Property Value
Orientation
Gets or sets the orientation of the menu (Horizontal for menu bar, Vertical for sidebar).
public MenuOrientation Orientation { get; set; }
Property Value
StickyPosition
Gets or sets whether this control should stick to the top or bottom during scrolling.
public StickyPosition StickyPosition { get; set; }
Property Value
Tag
Gets or sets an arbitrary object value that can be used to store custom data.
public object? Tag { get; set; }
Property Value
VerticalAlignment
Gets or sets the vertical alignment of the control within its container.
public VerticalAlignment VerticalAlignment { get; set; }
Property Value
Visible
Gets or sets whether this control is visible.
public bool Visible { get; set; }
Property Value
WantsMouseEvents
Whether this control wants to receive mouse events
public bool WantsMouseEvents { get; }
Property Value
Width
Gets or sets the explicit width of the control, or null for automatic sizing.
public int? Width { get; set; }
Property Value
- int?
Methods
AddItem(MenuItem)
Adds a menu item to the menu.
public void AddItem(MenuItem item)
Parameters
itemMenuItem
ClearItems()
Removes all menu items.
public void ClearItems()
CloseAllMenus()
Closes all open dropdowns and submenus.
public void CloseAllMenus()
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
FindItemByPath(string)
Finds a menu item by its path (e.g., "File/Recent/File1.txt").
public MenuItem? FindItemByPath(string path)
Parameters
pathstring
Returns
Focus()
Sets focus to the menu control.
public void Focus()
GetLogicalContentSize()
Gets the logical size of the control's content without rendering.
public Size GetLogicalContentSize()
Returns
- Size
The size representing the content's natural dimensions.
Invalidate()
Marks this control as needing to be re-rendered.
public void Invalidate()
Invalidate(bool)
public void Invalidate(bool fullRender)
Parameters
fullRenderbool
MeasureDOM(LayoutConstraints)
Measures the control's desired size given the available constraints.
public LayoutSize MeasureDOM(LayoutConstraints constraints)
Parameters
constraintsLayoutConstraintsThe layout constraints (min/max width/height).
Returns
- LayoutSize
The desired size of the control.
OpenDropdown(string)
Opens the dropdown for a top-level menu item.
public void OpenDropdown(string itemText)
Parameters
itemTextstring
PaintDOM(CharacterBuffer, LayoutRect, LayoutRect, Color, Color)
Paints the control's content directly to a CharacterBuffer.
public void PaintDOM(CharacterBuffer buffer, LayoutRect bounds, LayoutRect clipRect, Color defaultFg, Color defaultBg)
Parameters
bufferCharacterBufferThe buffer to paint to.
boundsLayoutRectThe absolute bounds where the control should paint.
clipRectLayoutRectThe clipping rectangle (visible area).
defaultFgColordefaultBgColor
ProcessKey(ConsoleKeyInfo)
Processes a keyboard input event.
public bool ProcessKey(ConsoleKeyInfo key)
Parameters
keyConsoleKeyInfoThe key information for the pressed key.
Returns
- bool
True if the key was handled by this control; otherwise, false.
ProcessMouseEvent(MouseEventArgs)
Processes a mouse event for this control
public bool ProcessMouseEvent(MouseEventArgs args)
Parameters
argsMouseEventArgsMouse event arguments with control-relative coordinates
Returns
- bool
True if the event was handled and should not propagate further
RemoveItem(MenuItem)
Removes a menu item from the menu.
public void RemoveItem(MenuItem item)
Parameters
itemMenuItem
SetFocus(bool, FocusReason)
Sets focus to this control
public void SetFocus(bool focus, FocusReason reason = FocusReason.Programmatic)
Parameters
focusboolWhether to give or remove focus
reasonFocusReasonThe reason for the focus change
SetItemEnabled(string, bool)
Sets whether a menu item is enabled by its path.
public void SetItemEnabled(string path, bool enabled)
Parameters
Events
GotFocus
Event fired when the control gains focus
public event EventHandler? GotFocus
Event Type
ItemHovered
Event fired when a menu item is hovered.
public event EventHandler<MenuItem>? ItemHovered
Event Type
ItemSelected
Event fired when a menu item is selected (executed).
public event EventHandler<MenuItem>? ItemSelected
Event Type
LostFocus
Event fired when the control loses focus
public event EventHandler? LostFocus
Event Type
MouseClick
Event fired when the control is clicked
public event EventHandler<MouseEventArgs>? MouseClick
Event Type
MouseDoubleClick
Event fired when the control is double-clicked
public event EventHandler<MouseEventArgs>? MouseDoubleClick
Event Type
MouseEnter
Event fired when the mouse enters the control area
public event EventHandler<MouseEventArgs>? MouseEnter
Event Type
MouseLeave
Event fired when the mouse leaves the control area
public event EventHandler<MouseEventArgs>? MouseLeave
Event Type
MouseMove
Event fired when the mouse moves over the control
public event EventHandler<MouseEventArgs>? MouseMove