Class: Iup::Timer

Inherits:
Object
  • Object
show all
Extended by:
AttributeBuilders
Includes:
CallbackSetter
Defined in:
lib/wrapped/timer.rb

Overview

The timer is used to trigger a callback function at regular intervals without interrupting the application's main event loop. It can be used to safely update the user interface.

For example, the following creates a timer which counts down from 10, updating a label's title. When the count reaches 0, the application will close.

count = 10
timer = Iup::Timer.new do |t|
t.time = 1000

t.action_cb = ->{
  if count.zero?
    Iup::CLOSE
  else
    count -= 1
    label.title = "Time remaining: #{count}"
    Iup::DEFAULT
  end
}
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AttributeBuilders

define_attribute, define_id_attribute, define_id_reader, define_id_writer, define_property_attribute, define_property_reader, define_property_writer, define_reader, define_writer

Methods included from CallbackSetter

#define_callback

Constructor Details

#initialize {|_self| ... } ⇒ Timer

Returns a new instance of Timer.

Yields:

  • (_self)

Yield Parameters:

  • _self (Iup::Timer)

    the object that the method was called on



33
34
35
36
37
38
# File 'lib/wrapped/timer.rb', line 33

def initialize &block
  @handle = IupLib.IupTimer

  # run any provided block on instance, to set up further attributes
  yield self if block_given?
end

Instance Attribute Details

#handleObject (readonly)

Unique reference to widget.



31
32
33
# File 'lib/wrapped/timer.rb', line 31

def handle
  @handle
end

Instance Method Details

#action_cb=(callback) ⇒ Object

--



71
72
73
74
75
76
77
78
79
# File 'lib/wrapped/timer.rb', line 71

def action_cb= callback
  unless callback.arity.zero?
    raise ArgumentError, 'action_cb must take 0 arguments'
  end
  cb = Proc.new do |ih|
    callback.call
  end
  define_callback(cb, 'ACTION_CB', :plain)
end

#destroyObject

must be called when Timer is finished with



41
42
43
# File 'lib/wrapped/timer.rb', line 41

def destroy
  IupLib.IupDestroy @handle
end

#runObject

:attr: run Values 'yes' / 'no' to start or stop the timer.



55
# File 'lib/wrapped/timer.rb', line 55

define_attribute :run

#timeObject

:attr: time The time interval before the action_cb is called, in milliseconds.



50
# File 'lib/wrapped/timer.rb', line 50

define_attribute :time

#widObject

:attr_reader: wid The native serial number of the timer: -1 if not running.



60
# File 'lib/wrapped/timer.rb', line 60

define_reader :wid