Class: Shoes::Button

Inherits:
Drawable show all
Includes:
Log
Defined in:
lacci/lib/shoes/drawables/button.rb

Constant Summary

Constants included from Log

Log::DEFAULT_COMPONENT, Log::DEFAULT_DEBUG_LOG_CONFIG, Log::DEFAULT_LOG_CONFIG

Instance Attribute Summary

Attributes inherited from Drawable

#debug_id, #destroyed, #parent

Attributes inherited from Linkable

#linkable_id

Instance Method Summary collapse

Methods included from Log

configure_logger, #log_init, logger

Methods inherited from Drawable

allocate_drawable_id, #app, #banner, #caption, #destroy, #download, drawable_by_id, drawable_class_by_name, dsl_name, #event, get_shoes_events, #hide, #inscription, #inspect, is_widget_class?, #method_missing, register_drawable_id, #respond_to_missing?, #set_parent, shoes_events, shoes_style, shoes_style_hashes, shoes_style_name?, shoes_style_names, #shoes_style_values, shoes_styles, #show, #style, #subtitle, #tagline, #title, #toggle, unregister_drawable_id, validate_as

Methods included from Colors

#gray, #rgb, #to_rgb

Methods inherited from Linkable

#bind_shoes_event, #send_self_event, #send_shoes_event, #unsub_all_shoes_events, #unsub_shoes_event

Constructor Details

#initialize(text, width: nil, height: nil, top: nil, left: nil, color: nil, padding_top: nil, padding_bottom: nil, size: 12, text_color: nil, font_size: nil, tooltip: nil) { ... } ⇒ Shoes::Button

Creates a new Button object.

Examples:

Shoes.app do
  @push = button "Push me"
  @note = para "Nothing pushed so far"
  @push.click {
    @note.replace(
      "Aha! Click! ",
      link("Go back") { @note.replace("Nothing pushed so far") }
    )
  }
end

Parameters:

  • text (String)

    The text displayed on the button.

  • width (Integer) (defaults to: nil)

    The requested width of the button in pixels.

  • height (Integer) (defaults to: nil)

    The requested height of the button in pixels.

  • top (Integer) (defaults to: nil)

    The position of the top edge of the button relative to its parent widget.

  • left (Integer) (defaults to: nil)

    The position of the left edge of the button relative to its parent widget.

  • size (Integer) (defaults to: 12)

    The font size of the button text.

  • color (String) (defaults to: nil)

    The background color of the button.

  • padding_top (Integer) (defaults to: nil)

    The padding above the button text.

  • padding_bottom (Integer) (defaults to: nil)

    The padding below the button text.

  • text_color (String) (defaults to: nil)

    The color of the button text.

Yields:

  • A block of code to be executed when the button is clicked.



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lacci/lib/shoes/drawables/button.rb', line 35

def initialize(text, width: nil, height: nil, top: nil, left: nil, color: nil, padding_top: nil, padding_bottom: nil, size: 12, text_color: nil,
  font_size: nil, tooltip: nil, &block)

  log_init("Button")

  # Properties passed as positional args, not keywords, don't get auto-set
  @text = text
  @block = block

  super

  # Bind to a handler named "click"
  bind_self_event("click") do
    @log.debug("Button clicked, calling handler") if @block
    @block&.call
  end

  bind_self_event("hover") do
    @hover&.call
  end

  create_display_drawable
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Shoes::Drawable

Instance Method Details

#click { ... } ⇒ Object

Set the click handler

Yields:

  • A block to be called when the button is clicked.



62
63
64
# File 'lacci/lib/shoes/drawables/button.rb', line 62

def click(&block)
  @block = block
end

#hover { ... } ⇒ Object

Set the hover handler

Yields:

  • A block to be called when the cursor moves to be over the button.



69
70
71
# File 'lacci/lib/shoes/drawables/button.rb', line 69

def hover(&block)
  @hover = block
end