Class: Shoes::SubscriptionItem

Inherits:
Drawable show all
Defined in:
lacci/lib/shoes/drawables/subscription_item.rb

Overview

Certain Shoes calls like motion and keydown are basically an event subscription, with no other visible presence. However, they have a place in the drawable tree and can be deleted.

Depending on the display library they may not have any direct visual (or similar) presence there either.

Inheriting from Drawable gives these a parent slot and a linkable_id automatically.

Events not yet implemented: start, finish events for slots - start is first draw, finish is drawable destroyed

Constant Summary

Constants inherited from Drawable

Drawable::DRAW_CONTEXT_STYLES

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 inherited from Drawable

allocate_drawable_id, #app, #banner, #caption, convert_to_float, convert_to_integer, #download, drawable_by_id, drawable_class_by_name, dsl_name, #event, expects_parent?, feature_for_shoes_style, get_shoes_events, #hide, #hover, init_args, #inscription, #inspect, is_widget_class?, #leave, #method_missing, #motion, opt_init_args, optional_init_args, register_drawable_id, registered_shoes_events?, required_init_args, #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, use_current_app, validate_as, with_current_app

Methods included from MarginHelper

#margin_parse

Methods included from Colors

#gray, #rgb, #to_rgb

Methods included from Log

configure_logger, #log_init, logger

Methods inherited from Linkable

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

Constructor Details

#initialize(args: [], shoes_api_name:, &block) ⇒ SubscriptionItem

Returns a new instance of SubscriptionItem.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lacci/lib/shoes/drawables/subscription_item.rb', line 19

def initialize(args: [], shoes_api_name:, &block)
  super

  @callback = block

  case shoes_api_name
  when "animate"
    @unsub_id = bind_self_event("animate") do |frame|
      @callback.call(frame)
    end
  when "every"
    @unsub_id = bind_self_event("every") do |count|
      @callback.call(count)
    end
  when "timer"
    @unsub_id = bind_self_event("timer") do
      @callback.call
    end
  when "hover"
    # Hover passes the Shoes drawable as the block param
    @unsub_id = bind_self_event("hover") do
      @callback&.call(self)
    end
  when "leave"
    # Leave passes the Shoes drawable as the block param
    @unsub_id = bind_self_event("leave") do
      @callback&.call(self)
    end
  when "motion"
    # Shoes sends back x, y, mods as the args.
    # Shoes3 uses the strings "control" "shift" and
    # "control_shift" as the mods arg.
    @unsub_id = bind_self_event("motion") do |x, y, ctrl_key, shift_key, **_kwargs|
      mods = [ctrl_key ? "control" : nil, shift_key ? "shift" : nil].compact.join("_")
      @callback&.call(x, y, mods)
    end
  when "click"
    # Click has block params button, left, top
    # button is the button number, left and top are coords
    @unsub_id = bind_self_event("click") do |button, x, y, **_kwargs|
      @callback&.call(button, x, y)
    end
  when "release"
    # Click has block params button, left, top
    # button is the button number, left and top are coords
    @unsub_id = bind_self_event("release") do |button, x, y, **_kwargs|
      @callback&.call(button, x, y)
    end
  when "keypress"
    # Keypress passes the key string or symbol to the handler
    # Do anything special for serialisation here?
    @unsub_id = bind_self_event("keypress") do |key|
      @callback&.call(key)
    end
  else
    raise "Unknown Shoes event #{shoes_api_name.inspect} passed to SubscriptionItem!"
  end

  @unsub_id = bind_self_event(shoes_api_name) do |*args|
    @callback&.call(*args)
  end

  # This won't create a visible display drawable, but will turn into
  # an invisible drawable and a stream of events.
  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

#destroyObject



86
87
88
89
90
91
92
# File 'lacci/lib/shoes/drawables/subscription_item.rb', line 86

def destroy
  # TODO: we need a better way to do this automatically. See https://github.com/scarpe-team/scarpe/issues/291
  unsub_shoes_event(@unsub_id) if @unsub_id
  @unsub_id = nil

  super
end