Class: Shoes::Widget

Inherits:
Flow show all
Defined in:
shoes-core/lib/shoes/widget.rb

Overview

This is the superclass for creating custom Shoes widgets.

To use, inherit from Widget and implement a initialize_widget method. You get a few magical effects:

  • When you inherit from Widget, you get a method in your Apps to create your widgets. The method is lower- and snake-cased. It returns an instance of your widget class.

  • Your widgets delegate missing methods to their app object. This allows you to use the Shoes DSL within your widgets.

  • Your widget otherwise behaves like a flow. If the final parameter to the widget method is a Hash, that will initialize the flow as well.

Examples:

class SayHello < Shoes::Widget
  def initialize_widget word
    para "Hello #{word}", stroke: green, size: 80
  end
end

Shoes.app do
  say_hello 'Shoes'
end

Constant Summary

Constants inherited from Slot

Slot::NEXT_ELEMENT_OFFSET, Slot::STYLES

Constants included from Common::Style

Common::Style::DEFAULT_STYLES, Common::Style::STYLE_GROUPS

Instance Attribute Summary collapse

Attributes included from Common::Hover

#hover_blk, #leave_blk

Attributes inherited from Slot

#blk, #contents, #dimensions, #gui, #parent, #scroll_height, #scroll_top

Attributes included from Common::Clickable

#pass_coordinates

Attributes inherited from Common::UIElement

#app, #dimensions, #gui, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Flow

#position_element

Methods included from Common::Hover

#add_mouse_hover_control, #apply_style_from_hover_class, #apply_style_from_pre_hover, create_hover_class, #eval_hover_block, #hover, #hover_class, #hovered?, #leave, #mouse_hovered, #mouse_left

Methods inherited from Slot

#add_child, #add_mouse_hover_control, #any_sibling_slots_following?, #app, #append, #before_initialize, #bump_current_position, #bump_parent_current_position, #clear, #contents_alignment, #create_bound_block, #create_dimensions, #eval_block, #fixed_height?, #handle_block, #inspect, #prepend, #remove, #remove_child, #scroll_max, #set_default_dimension_values, #slot_grew_by, #snapshot_current_position, #variable_height?

Methods included from Common::Clickable

#click, #pass_coordinates?, #register_click, #release

Methods inherited from Common::UIElement

#add_to_parent, #after_initialize, #before_initialize, #create_backend, #create_dimensions, #handle_block, #needs_rotate?, #painted?, #redraw_height, #redraw_left, #redraw_top, #redraw_width, #update_fill, #update_stroke

Methods included from Common::Style

#applicable_app_styles, #create_style_hash, included, #style, #style_init

Methods included from Common::SafelyEvaluate

#safely_evaluate

Methods included from Common::Remove

#remove

Methods included from Common::Positioning

#_position, #displace, #move

Methods included from Common::Visibility

#hidden?, #hidden_from_view?, #hide, #outside_parent_view?, #show, #toggle, #visible?

Methods included from Common::Inspect

#inspect, #to_s

Methods included from Common::Attachable

#attached_to

Constructor Details

#initialize(*_) ⇒ Widget

Having Widget define initialize makes it easier for us to detect whether subclasses have inappropriately overridden it or not.


37
38
39
# File 'shoes-core/lib/shoes/widget.rb', line 37

def initialize(*_)
  super
end

Instance Attribute Details

#original_argsObject

Returns the value of attribute original_args


33
34
35
# File 'shoes-core/lib/shoes/widget.rb', line 33

def original_args
  @original_args
end

Class Method Details

.dsl_method_name(klass) ⇒ Object


75
76
77
78
79
# File 'shoes-core/lib/shoes/widget.rb', line 75

def self.dsl_method_name(klass)
  klass.to_s[/(^|::)(\w+)$/, 2]
       .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
       .gsub(/([a-z\d])([A-Z])/, '\1_\2').downcase
end

.warn_about_initializeObject


81
82
83
84
85
86
87
88
89
90
# File 'shoes-core/lib/shoes/widget.rb', line 81

def self.warn_about_initialize
  return if instance_method(:initialize).owner == Shoes::Widget

  Shoes.logger.warn <<~EOS
    You've defined an `initialize` method on class '#{self}'. This is no longer supported.
          Your widget likely won't display, and you'll potentially receive argument errors.

          Instead, define `initialize_widget` and we'll call it from your generated widget method.
  EOS
end

Instance Method Details

#initialize_widgetObject


41
42
43
# File 'shoes-core/lib/shoes/widget.rb', line 41

def initialize_widget
  # Expected to be overridden by children but not guaranteed
end

#shoes_base_classObject


45
46
47
# File 'shoes-core/lib/shoes/widget.rb', line 45

def shoes_base_class
  Shoes::Widget
end