celina/widgets/button

Search:
Group by:

Button widget

This module provides interactive button widgets with click handling, styling, and focus support.

Types

Button = ref object of Widget
  text*: string
  normalStyle*: Style
  hoveredStyle*: Style
  pressedStyle*: Style
  focusedStyle*: Style
  disabledStyle*: Style
  state*: ButtonState
  enabled*: bool
  minWidth*: int
  padding*: int
  onClick*: proc ()
  onMouseEnter*: proc ()
  onMouseLeave*: proc ()
  onFocus*: proc ()
  onBlur*: proc ()
  onKeyPress*: proc (key: KeyEvent): bool
Interactive button widget
ButtonState = enum
  Normal,                   ## Default state
  Hovered,                  ## Mouse is over the button
  Pressed,                  ## Button is being pressed
  Focused,                  ## Button has keyboard focus
  Disabled                   ## Button is disabled
Button visual states

Procs

proc button(text: string; normalStyle: Style = style(White, Blue);
            hoveredStyle: Style = style(White, Cyan);
            pressedStyle: Style = style(Black, White);
            focusedStyle: Style = style(Yellow, Blue);
            disabledStyle: Style = style(BrightBlack, Reset); minWidth: int = 0;
            padding: int = 1; onClick: proc () = nil;
            onMouseEnter: proc () = nil; onMouseLeave: proc () = nil;
            onFocus: proc () = nil; onBlur: proc () = nil;
            onKeyPress: proc (key: KeyEvent): bool = nil): Button {....raises: [],
    tags: [], forbids: [].}
Convenience constructor for Button widget
proc dangerButton(text: string; onClick: proc () = nil): Button {....raises: [],
    tags: [], forbids: [].}
Create a danger (destructive action) button
proc getButtonText(widget: Button): string {....raises: [], tags: [], forbids: [].}
Get the formatted button text with padding
proc getCurrentStyle(widget: Button): Style {....raises: [], tags: [], forbids: [].}
Get the current style based on button state
proc handleClick(widget: Button) {....raises: [Exception], tags: [RootEffect],
                                   forbids: [].}
Handle button click
proc handleKeyEvent(widget: Button; event: KeyEvent): bool {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Handle keyboard input for the button Returns true if the event was handled
proc handleMouseEvent(widget: Button; event: MouseEvent; area: Rect): bool {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Handle mouse input for the button Returns true if the event was handled
proc isEnabled(widget: Button): bool {....raises: [], tags: [], forbids: [].}
Check if the button is enabled
proc newButton(text: string; normalStyle: Style = style(White, Blue);
               hoveredStyle: Style = style(White, Cyan);
               pressedStyle: Style = style(Black, White);
               focusedStyle: Style = style(Yellow, Blue);
               disabledStyle: Style = style(BrightBlack, Reset);
               minWidth: int = 0; padding: int = 1; onClick: proc () = nil;
               onMouseEnter: proc () = nil; onMouseLeave: proc () = nil;
               onFocus: proc () = nil; onBlur: proc () = nil;
               onKeyPress: proc (key: KeyEvent): bool = nil): Button {.
    ...raises: [], tags: [], forbids: [].}
Create a new Button widget
proc primaryButton(text: string; onClick: proc () = nil): Button {....raises: [],
    tags: [], forbids: [].}
Create a primary (prominent) button
proc secondaryButton(text: string; onClick: proc () = nil): Button {....raises: [],
    tags: [], forbids: [].}
Create a secondary button
proc setEnabled(widget: Button; enabled: bool) {....raises: [], tags: [],
    forbids: [].}
Enable or disable the button
proc setState(widget: Button; newState: ButtonState) {....raises: [Exception],
    tags: [RootEffect], forbids: [].}
Set the button state and trigger appropriate events
proc successButton(text: string; onClick: proc () = nil): Button {....raises: [],
    tags: [], forbids: [].}
Create a success button
proc withEventHandlers(widget: Button; onClick: proc () = nil;
                       onMouseEnter: proc () = nil; onMouseLeave: proc () = nil;
                       onFocus: proc () = nil; onBlur: proc () = nil;
                       onKeyPress: proc (key: KeyEvent): bool = nil): Button {.
    ...raises: [], tags: [], forbids: [].}
Create a copy with different event handlers
proc withMinWidth(widget: Button; minWidth: int): Button {....raises: [], tags: [],
    forbids: [].}
Create a copy with different minimum width
proc withOnClick(widget: Button; onClick: proc ()): Button {....raises: [],
    tags: [], forbids: [].}
Create a copy with different click handler
proc withPadding(widget: Button; padding: int): Button {....raises: [], tags: [],
    forbids: [].}
Create a copy with different padding
proc withStyles(widget: Button; normal: Style = defaultStyle();
                hovered: Style = defaultStyle();
                pressed: Style = defaultStyle();
                focused: Style = defaultStyle();
                disabled: Style = defaultStyle()): Button {....raises: [],
    tags: [], forbids: [].}
Create a copy with different styles
proc withText(widget: Button; text: string): Button {....raises: [], tags: [],
    forbids: [].}
Create a copy with different text

Methods

method canFocus(widget: Button): bool {....raises: [], tags: [], forbids: [].}
Buttons can receive focus when enabled
method getMinSize(widget: Button): Size {....raises: [], tags: [], forbids: [].}
Get minimum size for button widget
method getPreferredSize(widget: Button; available: Size): Size {.
    ...raises: [Exception], tags: [RootEffect], forbids: [].}
Get preferred size for button widget
method render(widget: Button; area: Rect; buf: var Buffer) {....raises: [],
    tags: [], forbids: [].}
Render the button widget